Security update for Core, with self-updated composer
[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\Random;
6 use Drupal\Component\Utility\UserAgent;
7 use PHPUnit\Framework\TestCase;
8
9 /**
10  * Tests bytes size parsing helper methods.
11  *
12  * @group Utility
13  *
14  * @coversDefaultClass \Drupal\Component\Utility\UserAgent
15  */
16 class UserAgentTest extends TestCase {
17
18   /**
19    * Helper method to supply language codes to testGetBestMatchingLangcode().
20    *
21    * @return array
22    *   Language codes, ordered by priority.
23    */
24   protected function getLanguages() {
25     return [
26       // In our test case, 'en' has priority over 'en-US'.
27       'en',
28       'en-US',
29       // But 'fr-CA' has priority over 'fr'.
30       'fr-CA',
31       'fr',
32       // 'es-MX' is alone.
33       'es-MX',
34       // 'pt' is alone.
35       'pt',
36       // Language codes with more then one dash are actually valid.
37       // eh-oh-laa-laa is the official language code of the Teletubbies.
38       'eh-oh-laa-laa',
39       // Chinese languages.
40       'zh-hans',
41       'zh-hant',
42       'zh-hant-tw',
43     ];
44   }
45
46   /**
47    * Helper method to supply language mappings to testGetBestMatchingLangcode().
48    *
49    * @return array
50    *   Language mappings.
51    */
52   protected function getMappings() {
53     return [
54       'no' => 'nb',
55       'pt' => 'pt-pt',
56       'zh' => 'zh-hans',
57       'zh-tw' => 'zh-hant',
58       'zh-hk' => 'zh-hant',
59       'zh-mo' => 'zh-hant',
60       'zh-cht' => 'zh-hant',
61       'zh-cn' => 'zh-hans',
62       'zh-sg' => 'zh-hans',
63       'zh-chs' => 'zh-hans',
64     ];
65   }
66
67   /**
68    * Test matching language from user agent.
69    *
70    * @dataProvider providerTestGetBestMatchingLangcode
71    * @covers ::getBestMatchingLangcode
72    */
73   public function testGetBestMatchingLangcode($accept_language, $expected) {
74     $result = UserAgent::getBestMatchingLangcode($accept_language, $this->getLanguages(), $this->getMappings());
75     $this->assertSame($expected, $result);
76   }
77
78   /**
79    * Data provider for testGetBestMatchingLangcode().
80    *
81    * @return array
82    *   - An accept-language string.
83    *   - Expected best matching language code.
84    */
85   public function providerTestGetBestMatchingLangcode() {
86     // Random generator.
87     $random = new Random();
88
89     return [
90       // Equal qvalue for each language, choose the site preferred one.
91       ['en,en-US,fr-CA,fr,es-MX', 'en'],
92       ['en-US,en,fr-CA,fr,es-MX', 'en'],
93       ['fr,en', 'en'],
94       ['en,fr', 'en'],
95       ['en-US,fr', 'en-US'],
96       ['fr,en-US', 'en-US'],
97       ['fr,fr-CA', 'fr-CA'],
98       ['fr-CA,fr', 'fr-CA'],
99       ['fr', 'fr-CA'],
100       ['fr;q=1', 'fr-CA'],
101       ['fr,es-MX', 'fr-CA'],
102       ['fr,es', 'fr-CA'],
103       ['es,fr', 'fr-CA'],
104       ['es-MX,de', 'es-MX'],
105       ['de,es-MX', 'es-MX'],
106
107       // Different cases and whitespace.
108       ['en', 'en'],
109       ['En', 'en'],
110       ['EN', 'en'],
111       [' en', 'en'],
112       ['en ', 'en'],
113       ['en, fr', 'en'],
114
115       // A less specific language from the browser matches a more specific one
116       // from the website, and the other way around for compatibility with
117       // some versions of Internet Explorer.
118       ['es', 'es-MX'],
119       ['es-MX', 'es-MX'],
120       ['pt', 'pt'],
121       ['pt-PT', 'pt'],
122       ['pt-PT;q=0.5,pt-BR;q=1,en;q=0.7', 'en'],
123       ['pt-PT;q=1,pt-BR;q=0.5,en;q=0.7', 'en'],
124       ['pt-PT;q=0.4,pt-BR;q=0.1,en;q=0.7', 'en'],
125       ['pt-PT;q=0.1,pt-BR;q=0.4,en;q=0.7', 'en'],
126
127       // Language code with several dashes are valid. The less specific language
128       // from the browser matches the more specific one from the website.
129       ['eh-oh-laa-laa', 'eh-oh-laa-laa'],
130       ['eh-oh-laa', 'eh-oh-laa-laa'],
131       ['eh-oh', 'eh-oh-laa-laa'],
132       ['eh', 'eh-oh-laa-laa'],
133
134       // Different qvalues.
135       ['fr,en;q=0.5', 'fr-CA'],
136       ['fr,en;q=0.5,fr-CA;q=0.25', 'fr'],
137
138       // Silly wildcards are also valid.
139       ['*,fr-CA;q=0.5', 'en'],
140       ['*,en;q=0.25', 'fr-CA'],
141       ['en,en-US;q=0.5,fr;q=0.25', 'en'],
142       ['en-US,en;q=0.5,fr;q=0.25', 'en-US'],
143
144       // Unresolvable cases.
145       ['', FALSE],
146       ['de,pl', FALSE],
147       ['iecRswK4eh', FALSE],
148       [$random->name(10, TRUE), FALSE],
149
150       // Chinese langcodes.
151       ['zh-cn, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hans'],
152       ['zh-tw, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hant'],
153       ['zh-hant, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hant'],
154       ['zh-hans, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hans'],
155       // @todo: This is copied from RFC4647 but our regex skips the numbers so
156       // they where removed. Our code should be updated so private1-private2 is
157       // valid. http://tools.ietf.org/html/rfc4647#section-3.4
158       ['zh-hant-CN-x-private-private, en-us;q=0.90, en;q=0.80, zh;q=0.70', 'zh-hant'],
159       ['zh-cn', 'zh-hans'],
160       ['zh-sg', 'zh-hans'],
161       ['zh-tw', 'zh-hant'],
162       ['zh-hk', 'zh-hant'],
163       ['zh-mo', 'zh-hant'],
164       ['zh-hans', 'zh-hans'],
165       ['zh-hant', 'zh-hant'],
166       ['zh-chs', 'zh-hans'],
167       ['zh-cht', 'zh-hant'],
168     ];
169   }
170
171 }