d804e1aab37677d6da7d41c77a233673023ac62d
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / UserAgentTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Utility;
4
5 use Drupal\Component\Utility\UserAgent;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests bytes size parsing helper methods.
10  *
11  * @group Utility
12  *
13  * @coversDefaultClass \Drupal\Component\Utility\UserAgent
14  */
15 class UserAgentTest extends UnitTestCase {
16
17   /**
18    * Helper method to supply language codes to testGetBestMatchingLangcode().
19    *
20    * @return array
21    *   Language codes, ordered by priority.
22    */
23   protected function getLanguages() {
24     return [
25       // In our test case, 'en' has priority over 'en-US'.
26       'en',
27       'en-US',
28       // But 'fr-CA' has priority over 'fr'.
29       'fr-CA',
30       'fr',
31       // 'es-MX' is alone.
32       'es-MX',
33       // 'pt' is alone.
34       'pt',
35       // Language codes with more then one dash are actually valid.
36       // eh-oh-laa-laa is the official language code of the Teletubbies.
37       'eh-oh-laa-laa',
38       // Chinese languages.
39       'zh-hans',
40       'zh-hant',
41       'zh-hant-tw',
42     ];
43   }
44
45   /**
46    * Helper method to supply language mappings to testGetBestMatchingLangcode().
47    *
48    * @return array
49    *   Language mappings.
50    */
51   protected function getMappings() {
52     return [
53       'no' => 'nb',
54       'pt' => 'pt-pt',
55       'zh' => 'zh-hans',
56       'zh-tw' => 'zh-hant',
57       'zh-hk' => 'zh-hant',
58       'zh-mo' => 'zh-hant',
59       'zh-cht' => 'zh-hant',
60       'zh-cn' => 'zh-hans',
61       'zh-sg' => 'zh-hans',
62       'zh-chs' => 'zh-hans',
63     ];
64   }
65
66   /**
67    * Test matching language from user agent.
68    *
69    * @dataProvider providerTestGetBestMatchingLangcode
70    * @covers ::getBestMatchingLangcode
71    */
72   public function testGetBestMatchingLangcode($accept_language, $expected) {
73     $result = UserAgent::getBestMatchingLangcode($accept_language, $this->getLanguages(), $this->getMappings());
74     $this->assertSame($expected, $result);
75   }
76
77   /**
78    * Data provider for testGetBestMatchingLangcode().
79    *
80    * @return array
81    *   - An accept-language string.
82    *   - Expected best matching language code.
83    */
84   public function providerTestGetBestMatchingLangcode() {
85     return [
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'],
89       ['fr,en', 'en'],
90       ['en,fr', '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'],
95       ['fr', 'fr-CA'],
96       ['fr;q=1', 'fr-CA'],
97       ['fr,es-MX', 'fr-CA'],
98       ['fr,es', 'fr-CA'],
99       ['es,fr', 'fr-CA'],
100       ['es-MX,de', 'es-MX'],
101       ['de,es-MX', 'es-MX'],
102
103       // Different cases and whitespace.
104       ['en', 'en'],
105       ['En', 'en'],
106       ['EN', 'en'],
107       [' en', 'en'],
108       ['en ', 'en'],
109       ['en, fr', 'en'],
110
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.
114       ['es', 'es-MX'],
115       ['es-MX', 'es-MX'],
116       ['pt', 'pt'],
117       ['pt-PT', 'pt'],
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'],
122
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'],
129
130       // Different qvalues.
131       ['fr,en;q=0.5', 'fr-CA'],
132       ['fr,en;q=0.5,fr-CA;q=0.25', 'fr'],
133
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'],
139
140       // Unresolvable cases.
141       ['', FALSE],
142       ['de,pl', FALSE],
143       ['iecRswK4eh', FALSE],
144       [$this->randomMachineName(10), FALSE],
145
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'],
164     ];
165   }
166
167 }