3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\Random;
6 use Drupal\Component\Utility\UserAgent;
7 use PHPUnit\Framework\TestCase;
10 * Tests bytes size parsing helper methods.
14 * @coversDefaultClass \Drupal\Component\Utility\UserAgent
16 class UserAgentTest extends TestCase {
19 * Helper method to supply language codes to testGetBestMatchingLangcode().
22 * Language codes, ordered by priority.
24 protected function getLanguages() {
26 // In our test case, 'en' has priority over 'en-US'.
29 // But 'fr-CA' has priority over 'fr'.
36 // Language codes with more then one dash are actually valid.
37 // eh-oh-laa-laa is the official language code of the Teletubbies.
47 * Helper method to supply language mappings to testGetBestMatchingLangcode().
52 protected function getMappings() {
60 'zh-cht' => 'zh-hant',
63 'zh-chs' => 'zh-hans',
68 * Test matching language from user agent.
70 * @dataProvider providerTestGetBestMatchingLangcode
71 * @covers ::getBestMatchingLangcode
73 public function testGetBestMatchingLangcode($accept_language, $expected) {
74 $result = UserAgent::getBestMatchingLangcode($accept_language, $this->getLanguages(), $this->getMappings());
75 $this->assertSame($expected, $result);
79 * Data provider for testGetBestMatchingLangcode().
82 * - An accept-language string.
83 * - Expected best matching language code.
85 public function providerTestGetBestMatchingLangcode() {
87 $random = new Random();
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'],
95 ['en-US,fr', 'en-US'],
96 ['fr,en-US', 'en-US'],
97 ['fr,fr-CA', 'fr-CA'],
98 ['fr-CA,fr', 'fr-CA'],
101 ['fr,es-MX', 'fr-CA'],
104 ['es-MX,de', 'es-MX'],
105 ['de,es-MX', 'es-MX'],
107 // Different cases and whitespace.
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.
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'],
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'],
134 // Different qvalues.
135 ['fr,en;q=0.5', 'fr-CA'],
136 ['fr,en;q=0.5,fr-CA;q=0.25', 'fr'],
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'],
144 // Unresolvable cases.
147 ['iecRswK4eh', FALSE],
148 [$random->name(10, TRUE), FALSE],
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'],