language.test 10.9 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * Tests for language.module.
 *
 * The test file includes:
 *  - a functional test for the language configuration forms;
9
 *  - comparison of $GLOBALS default language against dependency injection;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
 */

/**
 * Functional tests for the language list configuration forms.
 */
class LanguageListTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Language list configuration',
      'description' => 'Adds a new language and tests changing its status and the default language.',
      'group' => 'Language',
    );
  }

  function setUp() {
    parent::setUp('language');
  }

  /**
   * Functional tests for adding, editing and deleting languages.
   */
  function testLanguageList() {
    global $base_url;

    // User to add and remove language.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
    $this->drupalLogin($admin_user);

    // Add predefined language.
    $edit = array(
      'predefined_langcode' => 'fr',
    );
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
    $this->assertText('French', t('Language added successfully.'));
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));

    // Add custom language.
    $langcode = 'xx';
    $name = $this->randomName(16);
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
      'name' => $name,
      'direction' => '0',
    );
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
    $this->assertRaw('"edit-site-default-' . $langcode .'"', t('Language code found.'));
    $this->assertText(t($name), t('Test language added.'));

    // Check if we can change the default language.
    $path = 'admin/config/regional/language';
    $this->drupalGet($path);
    $this->assertFieldChecked('edit-site-default-en', t('English is the default language.'));
    // Change the default language.
    $edit = array(
      'site_default' => $langcode,
    );
    $this->drupalPost(NULL, $edit, t('Save configuration'));
    $this->assertNoFieldChecked('edit-site-default-en', t('Default language updated.'));
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));

    // Ensure we can't delete the default language.
    $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
    $this->assertText(t('The default language cannot be deleted.'), t('Failed to delete the default language.'));

    // Ensure 'edit' link works.
    $this->clickLink(t('edit'));
    $this->assertTitle(t('Edit language | Drupal'), t('Page title is "Edit language".'));
    // Edit a language.
    $name = $this->randomName(16);
    $edit = array(
      'name' => $name,
    );
    $this->drupalPost('admin/config/regional/language/edit/' . $langcode, $edit, t('Save language'));
    $this->assertRaw($name, t('The language has been updated.'));
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));

89 90 91 92 93
    // Change back the default language.
    $edit = array(
      'site_default' => 'en',
    );
    $this->drupalPost(NULL, $edit, t('Save configuration'));
94 95 96 97
    // Ensure 'delete' link works.
    $this->drupalGet('admin/config/regional/language');
    $this->clickLink(t('delete'));
    $this->assertText(t('Are you sure you want to delete the language'), t('"delete" link is correct.'));
98
    // Delete a language.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
    // First test the 'cancel' link.
    $this->clickLink(t('Cancel'));
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
    $this->assertRaw($name, t('The language was not deleted.'));
    // Delete the language for real. This a confirm form, we do not need any
    // fields changed.
    $this->drupalPost('admin/config/regional/language/delete/' . $langcode, array(), t('Delete'));
    // We need raw here because %language and %langcode will add HTML.
    $t_args = array('%language' => $name, '%langcode' => $langcode);
    $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The test language has been removed.'));
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
    // Verify that language is no longer found.
    $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
    $this->assertResponse(404, t('Language no longer found.'));
    // Make sure the "language_count" variable has been updated correctly.
    drupal_static_reset('language_list');
116 117 118
    $languages = language_list();
    $this->assertEqual(variable_get('language_count', 1), count($languages), t('Language count is correct.'));
    // Delete French.
119
    $this->drupalPost('admin/config/regional/language/delete/fr', array(), t('Delete'));
120 121 122
    // Get the count of languages.
    drupal_static_reset('language_list');
    $languages = language_list();
123 124 125 126 127 128 129 130
    // We need raw here because %language and %langcode will add HTML.
    $t_args = array('%language' => 'French', '%langcode' => 'fr');
    $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('Disabled language has been removed.'));
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
    // Verify that language is no longer found.
    $this->drupalGet('admin/config/regional/language/delete/fr');
    $this->assertResponse(404, t('Language no longer found.'));
    // Make sure the "language_count" variable has not changed.
131
    $this->assertEqual(variable_get('language_count', 1), count($languages), t('Language count is correct.'));
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    // Ensure we can delete the English language. Right now English is the only
    // language so we must add a new language and make it the default before
    // deleting English.
    $langcode = 'xx';
    $name = $this->randomName(16);
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
      'name' => $name,
      'direction' => '0',
    );
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
    $this->assertText($name, t('Name found.'));

    // Check if we can change the default language.
    $path = 'admin/config/regional/language';
    $this->drupalGet($path);
    $this->assertFieldChecked('edit-site-default-en', t('English is the default language.'));
    // Change the default language.
    $edit = array(
      'site_default' => $langcode,
    );
    $this->drupalPost(NULL, $edit, t('Save configuration'));
    $this->assertNoFieldChecked('edit-site-default-en', t('Default language updated.'));
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));

    $this->drupalPost('admin/config/regional/language/delete/en', array(), t('Delete'));
    // We need raw here because %language and %langcode will add HTML.
    $t_args = array('%language' => 'English', '%langcode' => 'en');
    $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The English language has been removed.'));
  }
}
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

/**
 * Test for dependency injected language object.
 */
class LanguageDependencyInjectionTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
        'name' => 'Language dependency injection',
        'description' => 'Compares the default language from $GLOBALS against the dependency injected language object.',
        'group' => 'Language',
    );
  }

  function setUp() {
    parent::setUp('language');

    // Set up a new container to ensure we are building a new Language object
    // for each test.
    drupal_container(TRUE);
  }

  /**
   * Test dependency injected Language against the GLOBAL language object.
   *
   * @todo Once the PHP global is gone, we won't need this test as the same
   * test is done without the PHP global in the following test.
   */
  function testDependencyInjectedLanguage() {
    // Initialize the language system.
    drupal_language_initialize();

    $expected = $GLOBALS[LANGUAGE_TYPE_INTERFACE];
    $result = drupal_container()->get(LANGUAGE_TYPE_INTERFACE);
    foreach ($expected as $property => $value) {
      $this->assertEqual($expected->$property, $result->$property, t('The dependency injected language object %prop property equals the $GLOBAL language object %prop property.', array('%prop' => $property)));
    }
  }

  /**
   * Test dependency injected languages against a new Language object.
   *
   * @see Drupal\Core\Language\Language
   */
  function testDependencyInjectedNewLanguage() {
    // Initialize the language system.
    drupal_language_initialize();

    $expected = new Drupal\Core\Language\Language((array) language_default());
    $result = drupal_container()->get(LANGUAGE_TYPE_INTERFACE);
    foreach ($expected as $property => $value) {
      $this->assertEqual($expected->$property, $result->$property, t('The dependency injected language object %prop property equals the new Language object %prop property.', array('%prop' => $property)));
    }
  }

  /**
   * Test dependency injected Language object against a new default language
   * object.
   *
   * @see Drupal\Core\Language\Language
   */
  function testDependencyInjectedNewDefaultLanguage() {
    // Change the language default object to different values.
    $new_language_default = (object) array(
      'langcode' => 'fr',
      'name' => 'French',
      'direction' => 0,
      'weight' => 0,
      'default' => TRUE,
    );
    variable_set('language_default', $new_language_default);

    // Initialize the language system.
    drupal_language_initialize();

    // The langauge system creates a Language object which contains the
    // same properties as the new default language object.
    $expected = $new_language_default;
    $result = drupal_container()->get(LANGUAGE_TYPE_INTERFACE);
    foreach ($expected as $property => $value) {
      $this->assertEqual($expected->$property, $result->$property, t('The dependency injected language object %prop property equals the default language object %prop property.', array('%prop' => $property)));
    }

    // Delete the language_default variable we previously set.
    variable_del('language_default');
  }
}