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.
234 lines
8.4 KiB
PHP
234 lines
8.4 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\UserCopy\UserCopyCatalog;
|
|
use App\Services\UserCopy\UserCopyCatalogCompiler;
|
|
use App\Services\UserCopy\UserCopyCatalogException;
|
|
use App\Services\UserCopy\UserCopyManifest;
|
|
use App\Services\UserCopy\UserCopyPublicationStore;
|
|
use App\Services\UserCopy\UserCopyRuntimeReader;
|
|
use App\Services\UserCopy\UserCopySourceCatalog;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$reject = static function (callable $call, string $message) use ($assert): void {
|
|
try {
|
|
$call();
|
|
} catch (UserCopyCatalogException) {
|
|
return;
|
|
}
|
|
$assert(false, $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);
|
|
};
|
|
|
|
$manifest = UserCopyManifest::validate([
|
|
'version' => 1,
|
|
'pages' => [
|
|
'cloud.empty' => [
|
|
'label' => '预声明页面',
|
|
'scopes' => ['common', 'empty_scope'],
|
|
],
|
|
'cloud.instances' => [
|
|
'label' => '实例页面',
|
|
'scopes' => ['common', 'instances'],
|
|
],
|
|
],
|
|
'entries' => [
|
|
[
|
|
'key' => 'common.toast.success.title',
|
|
'scope' => 'common',
|
|
'page' => 'cloud.instances',
|
|
'kind' => 'toast_title',
|
|
'source' => '操作完成',
|
|
'format' => 'plain_text',
|
|
'parameters' => [],
|
|
'criticality' => 'required',
|
|
'allow_hide' => false,
|
|
'toast_tone' => 'success',
|
|
],
|
|
[
|
|
'key' => 'common.toast.success.body',
|
|
'scope' => 'common',
|
|
'page' => 'cloud.instances',
|
|
'kind' => 'toast_body',
|
|
'source' => '操作已完成。',
|
|
'format' => 'plain_text',
|
|
'parameters' => [],
|
|
'criticality' => 'required',
|
|
'allow_hide' => false,
|
|
'toast_tone' => 'success',
|
|
],
|
|
[
|
|
'key' => 'instances.title',
|
|
'scope' => 'instances',
|
|
'page' => 'cloud.instances',
|
|
'kind' => 'title',
|
|
'source' => '实例',
|
|
'format' => 'plain_text',
|
|
'parameters' => [],
|
|
'criticality' => 'required',
|
|
'allow_hide' => false,
|
|
'toast_tone' => null,
|
|
],
|
|
[
|
|
'key' => 'instances.count',
|
|
'scope' => 'instances',
|
|
'page' => 'cloud.instances',
|
|
'kind' => 'description',
|
|
'source' => '{count, plural, =0 {没有实例} other {# 个实例}}',
|
|
'format' => 'icu',
|
|
'parameters' => ['count' => 'integer'],
|
|
'criticality' => 'required',
|
|
'allow_hide' => false,
|
|
'toast_tone' => null,
|
|
],
|
|
[
|
|
'key' => 'instances.optional_help',
|
|
'scope' => 'instances',
|
|
'page' => 'cloud.instances',
|
|
'kind' => 'help',
|
|
'source' => '中文后备说明',
|
|
'format' => 'plain_text',
|
|
'parameters' => [],
|
|
'criticality' => 'normal',
|
|
'allow_hide' => true,
|
|
'toast_tone' => null,
|
|
],
|
|
],
|
|
]);
|
|
|
|
$root = sys_get_temp_dir() . '/virt-user-copy-'
|
|
. bin2hex(random_bytes(8));
|
|
mkdir($root, 0750, true);
|
|
|
|
try {
|
|
$source = new UserCopySourceCatalog($manifest);
|
|
$assert(in_array('empty_scope', $source->pageScopes()['cloud.empty'], true),
|
|
'A predeclared scope without entries was rejected.');
|
|
$compiler = new UserCopyCatalogCompiler($source);
|
|
$store = new UserCopyPublicationStore($root);
|
|
|
|
$versionOne = $compiler->compile('en', [
|
|
'common.toast.success.title' => '管理员发布标题',
|
|
'common.toast.success.body' => '管理员发布内容。',
|
|
'instances.title' => '管理员发布实例标题',
|
|
'instances.count' => '{count, plural, =1 {管理员发布 1 台} other {管理员发布 # 台}}',
|
|
]);
|
|
$firstPointer = $store->publish($versionOne);
|
|
$assert($firstPointer['generation'] === $versionOne->generation,
|
|
'Publication did not switch to the compiled generation.');
|
|
$assert(is_file($root . '/current.json')
|
|
&& is_file($root . '/generations/' . $versionOne->generation
|
|
. '/en/common.php')
|
|
&& is_file($root . '/generations/' . $versionOne->generation
|
|
. '/en/empty_scope.php')
|
|
&& is_file($root . '/generations/' . $versionOne->generation
|
|
. '/en/instances.json'),
|
|
'Publication did not materialize both immutable catalog formats.');
|
|
|
|
$reader = new UserCopyRuntimeReader($root, $source);
|
|
$catalogOne = UserCopyCatalog::forPage(
|
|
$reader,
|
|
'en',
|
|
'cloud.instances'
|
|
);
|
|
$assert($catalogOne->text('instances.title') === '管理员发布实例标题',
|
|
'The active target translation was not read.');
|
|
$assert($catalogOne->text('instances.optional_help') === '中文后备说明',
|
|
'A missing target translation did not fall back to Chinese source.');
|
|
$assert($catalogOne->text('instances.count', ['count' => 2]) === '管理员发布 2 台',
|
|
'ICU parameters were not rendered with the target locale.');
|
|
$toast = $catalogOne->toast('common.toast.success');
|
|
$assert($toast === [
|
|
'title' => '管理员发布标题',
|
|
'body' => '管理员发布内容。',
|
|
'tone' => 'success',
|
|
], 'Toast title, body, or source-owned tone drifted.');
|
|
$assert($catalogOne->catalog()['fallback_scopes'] === [],
|
|
'A complete immutable catalog was incorrectly marked as fallback.');
|
|
|
|
$versionTwo = $compiler->compile('en', [
|
|
'common.toast.success.title' => '管理员发布标题',
|
|
'common.toast.success.body' => '管理员发布内容。',
|
|
'instances.title' => '管理员发布的新实例标题',
|
|
'instances.count' => '{count, plural, =1 {新版 1 台} other {新版 # 台}}',
|
|
]);
|
|
$store->publish($versionTwo);
|
|
$catalogTwo = UserCopyCatalog::forPage(
|
|
$reader,
|
|
'en',
|
|
'cloud.instances'
|
|
);
|
|
$assert($catalogOne->text('instances.title') === '管理员发布实例标题'
|
|
&& $catalogTwo->text('instances.title') === '管理员发布的新实例标题',
|
|
'An atomic generation switch mutated an existing request snapshot.');
|
|
|
|
$store->rollback('en', $versionOne->generation);
|
|
$rolledBack = UserCopyCatalog::forPage(
|
|
$reader,
|
|
'en',
|
|
'cloud.instances'
|
|
);
|
|
$assert($rolledBack->text('instances.title') === '管理员发布实例标题',
|
|
'Rollback did not atomically reactivate the retained generation.');
|
|
|
|
$pointerBeforeFailure = file_get_contents($root . '/current.json');
|
|
$reject(static fn () => $compiler->compile('en', [
|
|
'instances.count' => '{total, plural, =1 {错误 1 台} other {错误 # 台}}',
|
|
]), 'A placeholder contract mismatch was accepted.');
|
|
$assert(file_get_contents($root . '/current.json') === $pointerBeforeFailure,
|
|
'A failed compilation changed the active generation pointer.');
|
|
|
|
$unpublished = UserCopyCatalog::forPage(
|
|
$reader,
|
|
'fr',
|
|
'cloud.instances'
|
|
);
|
|
$assert($unpublished->generation() === null
|
|
&& $unpublished->text('instances.title') === '实例',
|
|
'An unpublished locale did not use the zero-database Chinese fallback.');
|
|
|
|
$hidden = $compiler->compile('en', [
|
|
'instances.optional_help' => ['hidden' => true],
|
|
]);
|
|
$store->publish($hidden);
|
|
$hiddenCatalog = UserCopyCatalog::forPage(
|
|
$reader,
|
|
'en',
|
|
'cloud.instances'
|
|
);
|
|
$assert($hiddenCatalog->text('instances.optional_help') === '',
|
|
'An allowed optional copy slot was not hidden.');
|
|
$reject(static fn () => $compiler->compile('en', [
|
|
'instances.title' => ['hidden' => true],
|
|
]), 'Required interface copy was allowed to become hidden.');
|
|
|
|
$store->validateGeneration($versionOne->generation);
|
|
$store->validateGeneration($versionTwo->generation);
|
|
} finally {
|
|
$removeTree($root);
|
|
}
|
|
|
|
echo "User copy runtime publication tests passed.\n";
|