Просмотр исходного кода

test: add language detection cases where ICU disagrees with our list

The locale_accept_from_http() shortcut added in 2ac4004f matches the
Accept-Language header against the locales ICU ships instead of our
translations, and its matching changed in ICU 67 (LocaleMatcher).
Before ICU 67 it sorts equal quality values alphabetically and accepts
the first exact match anywhere in the list before trying any fallback.
This only showed on Scrutinizer, which presumably still runs an ICU
older than 67 (#2078).

tst/I18nTest.php:
- testEqualQualityPreservesBrowserOrder now uses a data provider and
  adds "fr,de", a tie at the implicit quality of 1 (ICU < 67: de)
- testBrowserLanguageUnknownToIcuDetection: co, jbo and la have
  translations but no ICU locale data, so ICU matches the next language
  in the header instead (fr, de, it), regardless of the ICU version
- testBrowserLanguageZhHantWithZhFallbackDetection: typical Chrome
  headers from Taiwan and Hong Kong, ICU < 67 skips the zh_TW and zh_HK
  aliases and returns zh, i.e. Simplified Chinese

Expected to pass without the shortcut (#2098). With it (#2103), the
ICU-unknown cases should fail on any ICU version and the other two
tests only on ICU older than 67.
El RIDO 4 дней назад
Родитель
Сommit
ffd3b35a0f
1 измененных файлов с 54 добавлено и 3 удалено
  1. 54 3
      tst/I18nTest.php

+ 54 - 3
tst/I18nTest.php

@@ -80,12 +80,43 @@ class I18nTest extends TestCase
         $this->assertEquals('2 heures', I18n::_('%d hours', 2), '2 hours in French');
     }
 
-    public function testEqualQualityPreservesBrowserOrder()
+    /**
+     * @dataProvider provideEqualQualityHeaders
+     */
+    public function testEqualQualityPreservesBrowserOrder($acceptedLanguage, $language)
     {
-        $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr;q=0.8,de;q=0.8';
+        $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $acceptedLanguage;
         I18n::loadTranslations();
+        $this->assertEquals($language, I18n::getLanguage(), 'browser language ' . $acceptedLanguage);
+    }
+
+    public function provideEqualQualityHeaders()
+    {
+        // ICU before version 67 sorts languages of equal quality alphabetically
+        return [
+            'fr;q=0.8,de;q=0.8' => ['fr;q=0.8,de;q=0.8', 'fr'],
+            'fr,de' => ['fr,de', 'fr'],
+        ];
+    }
+
+    /**
+     * @dataProvider provideLanguagesUnknownToIcuHeaders
+     */
+    public function testBrowserLanguageUnknownToIcuDetection($acceptedLanguage, $language)
+    {
+        $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $acceptedLanguage;
+        I18n::loadTranslations();
+        $this->assertEquals($language, I18n::getLanguage(), 'browser language ' . $acceptedLanguage);
+    }
 
-        $this->assertEquals('fr', I18n::getLanguage());
+    public function provideLanguagesUnknownToIcuHeaders()
+    {
+        // ICU has no locale data for these languages, so it can only match the next one in the header
+        return [
+            'co,fr;q=0.8' => ['co,fr;q=0.8', 'co'],
+            'jbo,de;q=0.8' => ['jbo,de;q=0.8', 'jbo'],
+            'la,it;q=0.8' => ['la,it;q=0.8', 'la'],
+        ];
     }
 
     public function testBulgarianMonthExpirationTranslation()
@@ -168,6 +199,26 @@ class I18nTest extends TestCase
         }
     }
 
+    /**
+     * @dataProvider provideTraditionalChineseWithZhFallbackHeaders
+     */
+    public function testBrowserLanguageZhHantWithZhFallbackDetection($acceptedLanguage, $language)
+    {
+        $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $acceptedLanguage;
+        I18n::loadTranslations();
+        $this->assertEquals($language, I18n::getLanguage(), 'browser language ' . $acceptedLanguage);
+    }
+
+    public function provideTraditionalChineseWithZhFallbackHeaders()
+    {
+        // typical Chrome headers from Taiwan and Hong Kong, ICU before version 67
+        // has zh_TW and zh_HK only as aliases, skips them and returns zh instead
+        return [
+            'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' => ['zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'zh-tw'],
+            'zh-HK,zh;q=0.9,en;q=0.8' => ['zh-HK,zh;q=0.9,en;q=0.8', 'zh-tw'],
+        ];
+    }
+
     public function testBrowserLanguagePlDetection()
     {
         $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';