3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\UserAgent;
6 use Drupal\Tests\UnitTestCase;
9 * Tests bytes size parsing helper methods.
13 * @coversDefaultClass \Drupal\Component\Utility\UserAgent
15 class UserAgentTest extends UnitTestCase {
18 * Helper method to supply language codes to testGetBestMatchingLangcode().
21 * Language codes, ordered by priority.
23 protected function getLanguages() {
25 // In our test case, 'en' has priority over 'en-US'.
28 // But 'fr-CA' has priority over 'fr'.
35 // Language codes with more then one dash are actually valid.
36 // eh-oh-laa-laa is the official language code of the Teletubbies.
46 * Helper method to supply language mappings to testGetBestMatchingLangcode().
51 protected function getMappings() {
59 'zh-cht' => 'zh-hant',
62 'zh-chs' => 'zh-hans',
67 * Test matching language from user agent.
69 * @dataProvider providerTestGetBestMatchingLangcode
70 * @covers ::getBestMatchingLangcode
72 public function testGetBestMatchingLangcode($accept_language, $expected) {
73 $result = UserAgent::getBestMatchingLangcode($accept_language, $this->getLanguages(), $this->getMappings());
74 $this->assertSame($expected, $result);
78 * Data provider for testGetBestMatchingLangcode().
81 * - An accept-language string.
82 * - Expected best matching language code.
84 public function providerTestGetBestMatchingLangcode() {
86 // Equal qvalue for each language, choose the site preferred one.
87 ['en,en-US,fr-CA,fr,es-MX', 'en'],
88 ['en-US,en,fr-CA,fr,es-MX', 'en'],
91 ['en-US,fr', 'en-US'],
92 ['fr,en-US', 'en-US'],
93 ['fr,fr-CA', 'fr-CA'],
94 ['fr-CA,fr', 'fr-CA'],
97 ['fr,es-MX', 'fr-CA'],
100 ['es-MX,de', 'es-MX'],
101 ['de,es-MX', 'es-MX'],
103 // Different cases and whitespace.
111 // A less specific language from the browser matches a more specific one
112 // from the website, and the other way around for compatibility with
113 // some versions of Internet Explorer.
118 ['pt-PT;q=0.5,pt-BR;q=1,en;q=0.7', 'en'],
119 ['pt-PT;q=1,pt-BR;q=0.5,en;q=0.7', 'en'],
120 ['pt-PT;q=0.4,pt-BR;q=0.1,en;q=0.7', 'en'],
121 ['pt-PT;q=0.1,pt-BR;q=0.4,en;q=0.7', 'en'],
123 // Language code with several dashes are valid. The less specific language
124 // from the browser matches the more specific one from the website.
125 ['eh-oh-laa-laa', 'eh-oh-laa-laa'],
126 ['eh-oh-laa', 'eh-oh-laa-laa'],
127 ['eh-oh', 'eh-oh-laa-laa'],
128 ['eh', 'eh-oh-laa-laa'],
130 // Different qvalues.
131 ['fr,en;q=0.5', 'fr-CA'],
132 ['fr,en;q=0.5,fr-CA;q=0.25', 'fr'],
134 // Silly wildcards are also valid.
135 ['*,fr-CA;q=0.5', 'en'],
136 ['*,en;q=0.25', 'fr-CA'],
137 ['en,en-US;q=0.5,fr;q=0.25', 'en'],
138 ['en-US,en;q=0.5,fr;q=0.25', 'en-US'],
140 // Unresolvable cases.
143 ['iecRswK4eh', FALSE],
144 [$this->randomMachineName(10), FALSE],
146 // Chinese langcodes.
147 ['zh-cn, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hans'],
148 ['zh-tw, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hant'],
149 ['zh-hant, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hant'],
150 ['zh-hans, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hans'],
151 // @todo: This is copied from RFC4647 but our regex skips the numbers so
152 // they where removed. Our code should be updated so private1-private2 is
153 // valid. http://tools.ietf.org/html/rfc4647#section-3.4
154 ['zh-hant-CN-x-private-private, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hant'],
155 ['zh-cn', 'zh-hans'],
156 ['zh-sg', 'zh-hans'],
157 ['zh-tw', 'zh-hant'],
158 ['zh-hk', 'zh-hant'],
159 ['zh-mo', 'zh-hant'],
160 ['zh-hans', 'zh-hans'],
161 ['zh-hant', 'zh-hant'],
162 ['zh-chs', 'zh-hans'],
163 ['zh-cht', 'zh-hant'],