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/UserCopyUserRuntimeTest.php

239 lines
9.2 KiB
PHTML

<?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.');
$ordinaryBinding = $smarty->fetch(
'string:<h1{user_copy_binding key="common.shell.user_center"}>'
. 'Heading</h1>'
);
$assert($ordinaryBinding === '<h1>Heading</h1>',
'Ordinary users received administrator copy-binding metadata.');
$ordinaryVariables = $smarty->getTemplateVars();
$assert(($ordinaryVariables['user_copy_page'] ?? null)
=== 'cloud.instances'
&& ($ordinaryVariables['user_copy_editor_enabled'] ?? true) === false
&& ($ordinaryVariables['user_copy_editor_bootstrap_json'] ?? null)
=== '{}',
'Ordinary users received page-editor capability or bootstrap data.');
$authorSmarty = new Smarty();
$authorSmarty->setCompileDir($smartyRoot);
$authorSmarty->setCacheDir($smartyRoot);
$authorSmarty->assign('admin_can_manage', true);
$runtime->bind(
$authorSmarty,
'en',
'/user/cloud/instances',
'cloud.instances'
);
$authorBinding = $authorSmarty->fetch(
'string:<h1{user_copy_binding key="common.shell.user_center"}>'
. 'Heading</h1>'
);
$assert($authorBinding === '<h1 data-user-copy-key="common.shell.user_center">'
. 'Heading</h1>',
'Super administrator did not receive the safe copy-binding attribute.');
$assert($authorSmarty->getTemplateVars('user_copy_editor_enabled') === false,
'An author without a valid CSRF token received the page editor.');
foreach (['missing.copy.key', 'Invalid key'] as $invalidBindingKey) {
try {
$authorSmarty->fetch(
'string:<span{user_copy_binding key="'
. $invalidBindingKey . '"}>Copy</span>'
);
throw new RuntimeException(
'Invalid user-copy binding key was accepted: '
. $invalidBindingKey
);
} catch (Throwable $exception) {
$assert(str_contains(
$exception->getMessage(),
'Unknown user copy binding key'
), 'Invalid binding failed without a catalog contract error.');
}
}
$editorSmarty = new Smarty();
$editorSmarty->setCompileDir($smartyRoot);
$editorSmarty->setCacheDir($smartyRoot);
$editorSmarty->assign([
'admin_can_manage' => true,
'admin_csrf_token' => str_repeat('b', 64),
]);
$runtime->bind(
$editorSmarty,
'en',
'/user/cloud/instances',
'cloud.instances'
);
$editorVariables = $editorSmarty->getTemplateVars();
$editorBootstrap = json_decode(
(string) $editorVariables['user_copy_editor_bootstrap_json'],
true,
512,
JSON_THROW_ON_ERROR
);
$assert($editorVariables['user_copy_editor_enabled'] === true
&& array_keys($editorBootstrap) === [
'schema', 'page', 'runtime_locale', 'csrf_token',
]
&& $editorBootstrap['page'] === 'cloud.instances'
&& $editorBootstrap['runtime_locale'] === 'en'
&& $editorBootstrap['csrf_token'] === str_repeat('b', 64),
'The authorized page editor did not receive its minimal bootstrap.');
$noPageSmarty = new Smarty();
$noPageSmarty->setCompileDir($smartyRoot);
$noPageSmarty->setCacheDir($smartyRoot);
$noPageSmarty->assign([
'admin_can_manage' => true,
'admin_csrf_token' => str_repeat('c', 64),
]);
$runtime->bind($noPageSmarty, 'en', '/user/unmapped', null);
$assert($noPageSmarty->getTemplateVars('user_copy_editor_enabled') === false
&& $noPageSmarty->getTemplateVars('user_copy_editor_bootstrap_json')
=== '{}',
'A request without a mapped page received the page editor.');
$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,
'editor_surface' => 'feedback',
'editor_group' => 'common.shared',
];
$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";