|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Services\PublicLocale;
|
|
|
|
|
|
|
|
|
|
$assertSame = static function ($expected, $actual, string $message): void {
|
|
|
|
|
if ($expected !== $actual) {
|
|
|
|
|
throw new RuntimeException($message . ' Expected ' . var_export($expected, true)
|
|
|
|
|
. ', got ' . var_export($actual, true) . '.');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$assertSame(PublicLocale::ENGLISH, PublicLocale::resolve(
|
|
|
|
|
['lang' => 'en-US'],
|
|
|
|
|
[PublicLocale::COOKIE_NAME => 'zh-CN'],
|
|
|
|
|
'zh-CN,zh;q=0.9'
|
|
|
|
|
), 'An explicit English selection must override the cookie and browser preference.');
|
|
|
|
|
$assertSame(PublicLocale::CHINESE, PublicLocale::resolve(
|
|
|
|
|
['lang' => 'zh'],
|
|
|
|
|
[PublicLocale::COOKIE_NAME => 'en'],
|
|
|
|
|
'en-US,en;q=0.9'
|
|
|
|
|
), 'An explicit Chinese selection must override the cookie and browser preference.');
|
|
|
|
|
$assertSame(PublicLocale::ENGLISH, PublicLocale::resolve(
|
|
|
|
|
[],
|
|
|
|
|
[PublicLocale::COOKIE_NAME => 'en'],
|
|
|
|
|
'zh-CN,zh;q=0.9'
|
|
|
|
|
), 'A saved manual selection must override automatic browser detection.');
|
|
|
|
|
$assertSame(PublicLocale::CHINESE, PublicLocale::resolve(
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
'zh-CN,zh;q=0.9,en;q=0.8'
|
|
|
|
|
), 'A Chinese browser preference was not detected.');
|
|
|
|
|
$assertSame(PublicLocale::ENGLISH, PublicLocale::resolve(
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
'en-US,en;q=0.9,zh;q=0.8'
|
|
|
|
|
), 'An English browser preference was not detected.');
|
|
|
|
|
$assertSame(PublicLocale::CHINESE, PublicLocale::resolve(
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
'fr-FR,zh;q=0.8,en;q=0.6'
|
|
|
|
|
), 'Unsupported languages must be skipped in favor of the best supported preference.');
|
|
|
|
|
$assertSame(PublicLocale::ENGLISH, PublicLocale::resolve(
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
'fr-FR,fr;q=0.9'
|
|
|
|
|
), 'A non-Chinese browser with no supported preference must fall back to English.');
|
|
|
|
|
$assertSame(PublicLocale::CHINESE, PublicLocale::resolve([], [], ''),
|
|
|
|
|
'Requests without a browser language must retain the existing Chinese default.');
|
|
|
|
|
$assertSame(PublicLocale::ENGLISH, PublicLocale::browserLocale('zh-CN;q=0,en;q=0.5'),
|
|
|
|
|
'A language excluded with q=0 must not be selected.');
|
|
|
|
|
|
|
|
|
|
$dynamic = [
|
|
|
|
|
['locale' => 'zh-CN', 'display_name' => '简体中文'],
|
|
|
|
|
['locale' => 'en', 'display_name' => 'English'],
|
|
|
|
|
['locale' => 'ja', 'display_name' => '日本語'],
|
|
|
|
|
['locale' => 'pt-BR', 'display_name' => 'Português'],
|
|
|
|
|
];
|
|
|
|
|
$assertSame('ja', PublicLocale::resolve(
|
|
|
|
|
['lang' => 'ja-JP'],
|
|
|
|
|
[PublicLocale::COOKIE_NAME => 'en'],
|
|
|
|
|
'en-US',
|
|
|
|
|
$dynamic
|
|
|
|
|
), 'An enabled dynamic locale did not accept a compatible explicit tag.');
|
|
|
|
|
$assertSame('pt-BR', PublicLocale::resolve(
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
'pt-PT;q=0.9,en;q=0.8',
|
|
|
|
|
$dynamic
|
|
|
|
|
), 'Browser negotiation did not select an enabled target by base language.');
|
|
|
|
|
$assertSame('en', PublicLocale::resolve(
|
|
|
|
|
['lang' => 'de'],
|
|
|
|
|
[PublicLocale::COOKIE_NAME => 'de'],
|
|
|
|
|
'de-DE,en;q=0.7',
|
|
|
|
|
$dynamic
|
|
|
|
|
), 'An unavailable URL or cookie locale bypassed the runtime directory.');
|
|
|
|
|
$assertSame('zh-Hant-TW', PublicLocale::canonicalLocale('ZH_hant_tw'),
|
|
|
|
|
'Locale script and region casing was not canonicalized.');
|
|
|
|
|
|
|
|
|
|
$englishUrl = PublicLocale::switchUrl('en', '/auth/register?aff=ABCDEFGH&source=landing');
|
|
|
|
|
$assertSame('/auth/register?aff=ABCDEFGH&source=landing&lang=en', $englishUrl,
|
|
|
|
|
'Language switching must preserve registration referral parameters.');
|
|
|
|
|
$chineseUrl = PublicLocale::switchUrl('zh-CN', '/auth/register?aff=ABCDEFGH&lang=en');
|
|
|
|
|
$assertSame('/auth/register?aff=ABCDEFGH&lang=zh-CN', $chineseUrl,
|
|
|
|
|
'Language switching must replace, rather than duplicate, the current locale.');
|
|
|
|
|
$assertSame('/?lang=en', PublicLocale::switchUrl('en', '//external.example/path?x=1'),
|
|
|
|
|
'Language switching must never emit a protocol-relative external URL.');
|
|
|
|
|
|
|
|
|
|
$viewData = PublicLocale::viewData('en', '/password/reset?from=login');
|
|
|
|
|
$assertSame('en', $viewData['public_locale'], 'The resolved public locale is missing from view data.');
|
|
|
|
|
$assertSame('true', $viewData['language'], 'English must retain the legacy template language flag.');
|
|
|
|
|
$assertSame('/password/reset?from=login&lang=zh-CN', $viewData['language_zh_url'],
|
|
|
|
|
'The Chinese switch URL is incorrect.');
|
|
|
|
|
$assertSame('/password/reset?from=login&lang=en', $viewData['language_en_url'],
|
|
|
|
|
'The English switch URL is incorrect.');
|
|
|
|
|
|
|
|
|
|
echo "Public locale detection and switching tests passed.\n";
|