You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/PublicPageLocaleRenderTest.php

136 lines
5.2 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\PublicLocale;
use App\Services\UserCopy\UserCopyManifest;
use App\Services\UserCopy\UserCopyRuntimeReader;
use App\Services\UserCopy\UserCopySourceCatalog;
use App\Services\UserCopy\UserCopyViewRuntime;
final class PublicPageGuestForLocaleTest
{
public bool $isLogin = false;
public function isAdmin(): bool
{
return false;
}
}
$directory = sys_get_temp_dir() . '/virt-public-locale-render-' . bin2hex(random_bytes(8));
if (!mkdir($directory, 0700)) {
throw new RuntimeException('Cannot create private public-page render directory.');
}
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$smarty = new Smarty();
$smarty->setTemplateDir(dirname(__DIR__) . '/resources/views/venix');
$smarty->setCompileDir($directory);
$smarty->setCacheDir($directory);
foreach (['urlencode', 'number_format', 'date'] as $modifier) {
$smarty->registerPlugin('modifier', $modifier, $modifier);
}
$smarty->assign([
'appName' => 'Locale Test Cloud',
'config' => [
'appName' => 'Locale Test Cloud',
'jump_delay' => 500,
],
'cdn' => '',
'backup_cdn' => '',
'seo' => '',
'user' => new PublicPageGuestForLocaleTest(),
'captch_login' => 'false',
'captch_reg' => 'false',
'captch_respass' => 'false',
'enable_invite_code' => 'false',
'enable_email_verify' => 'false',
'invite_required' => false,
'tos_reg' => 'true',
'code' => '',
'aff_code' => '',
'aff' => 0,
'aff_method' => '',
'token' => str_repeat('a', 64),
]);
$pages = [
'index.tpl' => ['public.home', '你的云资源'],
'auth/login.tpl' => ['auth.login', '登录账户'],
'auth/register.tpl' => ['auth.register', '注册新账户'],
'password/reset.tpl' => ['auth.password', '重置密码请求'],
'password/token.tpl' => ['auth.password', '输入并确认你的新密码。'],
];
$errorPages = [
'404.tpl' => '页面不存在',
'405.tpl' => '请求方式不被允许',
'500.tpl' => '服务暂时不可用',
];
try {
foreach ([PublicLocale::ENGLISH, PublicLocale::CHINESE] as $locale) {
$smarty->assign(PublicLocale::viewData($locale, '/auth/register?aff=ABCDEFGH'));
foreach ($pages as $template => $copy) {
(new UserCopyViewRuntime(new UserCopyRuntimeReader(
$directory . '/missing-runtime',
new UserCopySourceCatalog(UserCopyManifest::load())
)))->bind(
$smarty,
$locale,
'/auth/register?aff=ABCDEFGH',
$copy[0]
);
$html = $smarty->fetch($template);
$assert(str_contains($html, '<html lang="' . $locale . '">'),
$template . ' rendered the wrong HTML language for ' . $locale . '.');
$assert(str_contains($html, $copy[1]),
$template . ' did not use the Chinese baseline for an unpublished locale.');
$assert(substr_count($html, 'class="landing-site-header"') === 1,
$template . ' did not render exactly one shared site header.');
$assert(substr_count($html, 'class="landing-site-footer"') === 1,
$template . ' did not render exactly one shared site footer.');
$assert(str_contains($html, 'lang="zh-CN" hreflang="zh-CN"')
&& str_contains($html, 'lang="en" hreflang="en"'),
$template . ' is missing one of the language switch links.');
$assert(str_contains($html, 'aff=ABCDEFGH&amp;lang=en')
&& str_contains($html, 'aff=ABCDEFGH&amp;lang=zh-CN'),
$template . ' did not preserve query parameters in language switch links.');
$assert(str_contains($html, '© 2026 Locale Test Cloud. 保留所有权利。'),
$template . ' is missing the persistent dynamic copyright footer.');
$assert(!str_contains($html, 'auth-site-nav') && !str_contains($html, 'auth-copyright'),
$template . ' rendered a retired authentication-only header or footer.');
}
foreach ($errorPages as $template => $expected) {
(new UserCopyViewRuntime(new UserCopyRuntimeReader(
$directory . '/missing-runtime',
new UserCopySourceCatalog(UserCopyManifest::load())
)))->bind($smarty, $locale, '/missing', 'public.error');
$html = $smarty->fetch($template);
$assert(str_contains($html, '<html lang="' . $locale . '">')
&& str_contains($html, $expected),
$template . ' did not bind the public error catalog.');
}
}
} finally {
$generatedFiles = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($generatedFiles as $generated) {
if ($generated->isFile()) {
unlink($generated->getPathname());
} elseif ($generated->isDir()) {
rmdir($generated->getPathname());
}
}
rmdir($directory);
}
echo "Public pages bind browser locales and safely fall back to Chinese source copy.\n";