LanguageBrowserDetectionUnitTest.php 8.14 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\language\Tests\LanguageBrowserDetectionUnitTest.
 */

namespace Drupal\language\Tests;

10
use Drupal\simpletest\WebTestBase;
11
use Drupal\Core\Language\Language;
12 13 14 15

/**
 * Test browser language detection.
 */
16 17 18
class LanguageBrowserDetectionUnitTest extends WebTestBase {

  public static $modules = array('language');
19 20 21 22 23 24 25 26 27 28 29

  public static function getInfo() {
    return array(
      'name' => 'Browser language detection',
      'description' => 'Tests for the browser language detection.',
      'group' => 'Language',
    );
  }

  /**
   * Unit tests for the language_from_browser() function.
30 31
   *
   * @see language_from_browser().
32 33
   */
  function testLanguageFromBrowser() {
34 35 36 37
    // The order of the languages is only important if the browser language
    // codes are having the same qvalue, otherwise the one with the highest
    // qvalue is prefered. The automatically generated generic tags are always
    // having a lower qvalue.
38 39 40

    $languages = array(
      // In our test case, 'en' has priority over 'en-US'.
41
      'en' => new Language(array(
42
        'langcode' => 'en',
43 44
      )),
      'en-US' => new Language(array(
45
        'langcode' => 'en-US',
46
      )),
47
      // But 'fr-CA' has priority over 'fr'.
48
      'fr-CA' => new Language(array(
49
        'langcode' => 'fr-CA',
50 51
      )),
      'fr' => new Language(array(
52
        'langcode' => 'fr',
53
      )),
54
      // 'es-MX' is alone.
55
      'es-MX' => new Language(array(
56
        'langcode' => 'es-MX',
57
      )),
58
      // 'pt' is alone.
59
      'pt' => new Language(array(
60
        'langcode' => 'pt',
61
      )),
62 63
      // Language codes with more then one dash are actually valid.
      // eh-oh-laa-laa is the official language code of the Teletubbies.
64
      'eh-oh-laa-laa' => new Language(array(
65
        'langcode' => 'eh-oh-laa-laa',
66
      )),
67 68 69 70 71 72 73 74 75 76
      // Chinese languages.
      'zh-hans' => new Language(array(
        'langcode' => 'zh-hans',
      )),
      'zh-hant' => new Language(array(
        'langcode' => 'zh-hant',
      )),
      'zh-hant-tw' => new Language(array(
        'langcode' => 'zh-hant',
      )),
77 78 79 80 81 82 83 84
    );

    $test_cases = array(
      // Equal qvalue for each language, choose the site prefered one.
      'en,en-US,fr-CA,fr,es-MX' => 'en',
      'en-US,en,fr-CA,fr,es-MX' => 'en',
      'fr,en' => 'en',
      'en,fr' => 'en',
85 86
      'en-US,fr' => 'en-US',
      'fr,en-US' => 'en-US',
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      'fr,fr-CA' => 'fr-CA',
      'fr-CA,fr' => 'fr-CA',
      'fr' => 'fr-CA',
      'fr;q=1' => 'fr-CA',
      'fr,es-MX' => 'fr-CA',
      'fr,es' => 'fr-CA',
      'es,fr' => 'fr-CA',
      'es-MX,de' => 'es-MX',
      'de,es-MX' => 'es-MX',

      // Different cases and whitespace.
      'en' => 'en',
      'En' => 'en',
      'EN' => 'en',
      ' en' => 'en',
      'en ' => 'en',
      'en, fr' => 'en',

      // A less specific language from the browser matches a more specific one
      // from the website, and the other way around for compatibility with
      // some versions of Internet Explorer.
      'es' => 'es-MX',
      'es-MX' => 'es-MX',
      'pt' => 'pt',
      'pt-PT' => 'pt',
      'pt-PT;q=0.5,pt-BR;q=1,en;q=0.7' => 'en',
      'pt-PT;q=1,pt-BR;q=0.5,en;q=0.7' => 'en',
      'pt-PT;q=0.4,pt-BR;q=0.1,en;q=0.7' => 'en',
      'pt-PT;q=0.1,pt-BR;q=0.4,en;q=0.7' => 'en',

      // Language code with several dashes are valid. The less specific language
      // from the browser matches the more specific one from the website.
      'eh-oh-laa-laa' => 'eh-oh-laa-laa',
      'eh-oh-laa' => 'eh-oh-laa-laa',
      'eh-oh' => 'eh-oh-laa-laa',
      'eh' => 'eh-oh-laa-laa',

      // Different qvalues.
      'fr,en;q=0.5' => 'fr-CA',
      'fr,en;q=0.5,fr-CA;q=0.25' => 'fr',

      // Silly wildcards are also valid.
      '*,fr-CA;q=0.5' => 'en',
      '*,en;q=0.25' => 'fr-CA',
      'en,en-US;q=0.5,fr;q=0.25' => 'en',
      'en-US,en;q=0.5,fr;q=0.25' => 'en-US',

      // Unresolvable cases.
      '' => FALSE,
      'de,pl' => FALSE,
      'iecRswK4eh' => FALSE,
      $this->randomName(10) => FALSE,
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

      // Chinese langcodes.
      'zh-cn, en-us;q=0.90, en;q=0.80, zh;q=0.70' => 'zh-hans',
      'zh-tw, en-us;q=0.90, en;q=0.80, zh;q=0.70' => 'zh-hant',
      'zh-hant, en-us;q=0.90, en;q=0.80, zh;q=0.70' => 'zh-hant',
      'zh-hans, en-us;q=0.90, en;q=0.80, zh;q=0.70' => 'zh-hans',
      'zh-cn' => 'zh-hans',
      'zh-sg' => 'zh-hans',
      'zh-tw' => 'zh-hant',
      'zh-hk' => 'zh-hant',
      'zh-mo' => 'zh-hant',
      'zh-hans' => 'zh-hans',
      'zh-hant' => 'zh-hant',
      'zh-chs' => 'zh-hans',
      'zh-cht' => 'zh-hant',
154 155 156 157 158 159 160 161
    );

    foreach ($test_cases as $accept_language => $expected_result) {
      $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $accept_language;
      $result = language_from_browser($languages);
      $this->assertIdentical($result, $expected_result, t("Language selection '@accept-language' selects '@result', result = '@actual'", array('@accept-language' => $accept_language, '@result' => $expected_result, '@actual' => isset($result) ? $result : 'none')));
    }
  }
162 163 164 165 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

  /**
   * Tests for adding, editing and deleting mappings between browser language
   * codes and Drupal language codes.
   */
  function testUIBrowserLanguageMappings() {
    // User to manage languages.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
    $this->drupalLogin($admin_user);

    // Check that the configure link exists.
    $this->drupalGet('admin/config/regional/language/detection');
    $this->assertLinkByHref('admin/config/regional/language/detection/browser');

    // Check that defaults are loaded from language.mappings.yml.
    $this->drupalGet('admin/config/regional/language/detection/browser');
    $this->assertField('edit-mappings-zh-cn-browser-langcode', 'zh-cn', 'Chinese browser language code found.');
    $this->assertField('edit-mappings-zh-cn-drupal-langcode', 'zh-hans-cn', 'Chinese Drupal language code found.');

    // Delete zh-cn language code.
    $browser_langcode = 'zh-cn';
    $this->drupalGet('admin/config/regional/language/detection/browser/delete/' . $browser_langcode);
    $message = t('Are you sure you want to delete @browser_langcode?', array(
      '@browser_langcode' => $browser_langcode,
    ));
    $this->assertRaw($message);

    // Confirm the delete.
    $edit = array();
    $this->drupalPost('admin/config/regional/language/detection/browser/delete/' . $browser_langcode, $edit, t('Confirm'));

    // Check that ch-zn no longer exists.
    $this->assertNoField('edit-mappings-zh-cn-browser-langcode', 'Chinese browser language code no longer exists.');

    // Add a new custom mapping.
    $edit = array(
      'new_mapping[browser_langcode]' => 'xx',
      'new_mapping[drupal_langcode]' => 'en',
    );
    $this->drupalPost('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
    $this->drupalGet('admin/config/regional/language/detection/browser');
    $this->assertField('edit-mappings-xx-browser-langcode', 'xx', 'Browser language code found.');
    $this->assertField('edit-mappings-xx-drupal-langcode', 'en', 'Drupal language code found.');

    // Add the same custom mapping again.
    $this->drupalPost('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
    $this->assertText('Browser language codes must be unique.');

    // Change browser language code of our custom mapping to zh-sg.
    $edit = array(
      'mappings[xx][browser_langcode]' => 'zh-sg',
      'mappings[xx][drupal_langcode]' => 'en',
    );
    $this->drupalPost('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
    $this->assertText(t('Browser language codes must be unique.'));

    // Change Drupal language code of our custom mapping to zh-hans.
    $edit = array(
      'mappings[xx][browser_langcode]' => 'xx',
      'mappings[xx][drupal_langcode]' => 'zh-hans',
    );
    $this->drupalPost('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
    $this->drupalGet('admin/config/regional/language/detection/browser');
    $this->assertField('edit-mappings-xx-browser-langcode', 'xx', 'Browser language code found.');
    $this->assertField('edit-mappings-xx-drupal-langcode', 'zh-hans', 'Drupal language code found.');
  }
228
}