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.
144 lines
5.5 KiB
PHP
144 lines
5.5 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\UserCopy\UserCopyCatalogCompiler;
|
|
use App\Services\UserCopy\UserCopyManifest;
|
|
use App\Services\UserCopy\UserCopyPublicationStore;
|
|
use App\Services\UserCopy\UserCopyRuntimeReader;
|
|
use App\Services\UserCopy\UserCopySourceCatalog;
|
|
use App\Services\UserCopy\UserCopyViewRuntime;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$removeTree = static function (string $directory): void {
|
|
if (!is_dir($directory)) return;
|
|
$iterator = new RecursiveIteratorIterator(
|
|
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
|
|
RecursiveIteratorIterator::CHILD_FIRST
|
|
);
|
|
foreach ($iterator as $item) {
|
|
$item->isDir() ? rmdir($item->getPathname()) : unlink($item->getPathname());
|
|
}
|
|
rmdir($directory);
|
|
};
|
|
|
|
$runtimeRoot = sys_get_temp_dir() . '/virt-user-copy-view-'
|
|
. bin2hex(random_bytes(8));
|
|
$smartyRoot = $runtimeRoot . '/smarty';
|
|
mkdir($smartyRoot, 0700, true);
|
|
|
|
try {
|
|
$source = new UserCopySourceCatalog(UserCopyManifest::load());
|
|
$compiled = (new UserCopyCatalogCompiler($source))->compile('en', [
|
|
// Test fixture copy is runtime publication data, never source copy.
|
|
'common.shell.user_center' => '运行时发布的账户外壳',
|
|
'common.shell.nav.instances' => '运行时发布的实例导航',
|
|
'common.toast.success.title' => '运行时发布的成功标题',
|
|
'common.toast.success.body' => '运行时发布的成功内容',
|
|
'common.toast.error.title' => '运行时发布的失败标题',
|
|
'common.toast.warning.title' => '运行时发布的警告标题',
|
|
'common.toast.close.aria' => '运行时发布的关闭标签',
|
|
]);
|
|
(new UserCopyPublicationStore($runtimeRoot))->publish($compiled);
|
|
$reader = new UserCopyRuntimeReader($runtimeRoot, $source);
|
|
$runtime = new UserCopyViewRuntime($reader);
|
|
|
|
$smarty = new Smarty();
|
|
$smarty->setCompileDir($smartyRoot);
|
|
$smarty->setCacheDir($smartyRoot);
|
|
$catalog = $runtime->bind(
|
|
$smarty,
|
|
'en',
|
|
'/user/cloud/instances?page=2',
|
|
'cloud.instances'
|
|
);
|
|
|
|
$html = $smarty->fetch(
|
|
'string:<html lang="{$user_copy_locale}">'
|
|
. '{user_copy key="common.shell.user_center"}|'
|
|
. '{user_copy key="common.shell.nav.instances"}|'
|
|
. '{$user_copy_language_zh_url}|{$user_copy_language_en_url}'
|
|
. '</html>'
|
|
);
|
|
$assert(str_contains($html, 'lang="en"')
|
|
&& str_contains($html, '运行时发布的账户外壳')
|
|
&& str_contains($html, '运行时发布的实例导航'),
|
|
'Smarty did not render the active runtime publication.');
|
|
$assert(str_contains($html, 'page=2&lang=zh-CN')
|
|
&& str_contains($html, 'page=2&lang=en'),
|
|
'Language switching did not preserve the current query.');
|
|
$assert($catalog->generation() === $compiled->generation,
|
|
'The user view did not bind the active immutable generation.');
|
|
|
|
$variables = $smarty->getTemplateVars();
|
|
$bootstrap = json_decode(
|
|
(string) ($variables['user_copy_bootstrap_json'] ?? ''),
|
|
true,
|
|
512,
|
|
JSON_THROW_ON_ERROR
|
|
);
|
|
$assert(($bootstrap['locale'] ?? null) === 'en'
|
|
&& ($bootstrap['generation'] ?? null) === $compiled->generation
|
|
&& ($bootstrap['messages']['common.toast.success.title'] ?? null)
|
|
=== '运行时发布的成功标题',
|
|
'Browser bootstrap does not match the server request snapshot.');
|
|
|
|
$fallbackSmarty = new Smarty();
|
|
$fallbackSmarty->setCompileDir($smartyRoot);
|
|
$fallbackSmarty->setCacheDir($smartyRoot);
|
|
$fallback = $runtime->bind(
|
|
$fallbackSmarty,
|
|
'fr',
|
|
'/user/ticket',
|
|
null
|
|
);
|
|
$assert($fallback->generation() === null
|
|
&& $fallback->text('common.shell.user_center') === '用户中心',
|
|
'An unpublished locale did not fall back to source copy.');
|
|
|
|
$changedManifest = UserCopyManifest::load();
|
|
foreach ($changedManifest['entries'] as &$entry) {
|
|
if ($entry['key'] === 'common.shell.user_center') {
|
|
$entry['source'] = '更新后的用户中心';
|
|
}
|
|
}
|
|
unset($entry);
|
|
$changedManifest['entries'][] = [
|
|
'key' => 'common.runtime.new_notice',
|
|
'scope' => 'common',
|
|
'page' => 'cloud.instances',
|
|
'kind' => 'notice',
|
|
'source' => '新增中文提示',
|
|
'format' => 'plain_text',
|
|
'parameters' => [],
|
|
'criticality' => 'required',
|
|
'allow_hide' => false,
|
|
'toast_tone' => null,
|
|
];
|
|
$changedSource = new UserCopySourceCatalog(
|
|
UserCopyManifest::validate($changedManifest)
|
|
);
|
|
$changedReader = new UserCopyRuntimeReader($runtimeRoot, $changedSource);
|
|
$changedCatalog = new App\Services\UserCopy\UserCopyCatalog(
|
|
$changedReader,
|
|
'en',
|
|
['common']
|
|
);
|
|
$assert($changedCatalog->generation() === $compiled->generation
|
|
&& $changedCatalog->text('common.shell.nav.instances')
|
|
=== '运行时发布的实例导航'
|
|
&& $changedCatalog->text('common.shell.user_center')
|
|
=== '更新后的用户中心'
|
|
&& $changedCatalog->text('common.runtime.new_notice')
|
|
=== '新增中文提示',
|
|
'A source update discarded compatible published copy or retained stale copy.');
|
|
} finally {
|
|
$removeTree($runtimeRoot);
|
|
}
|
|
|
|
echo "User copy locale, Smarty and browser-bootstrap runtime checks passed.\n";
|