|
|
|
|
<?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\UserCopySourceCatalog;
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$manifest = UserCopyManifest::validate([
|
|
|
|
|
'version' => 1,
|
|
|
|
|
'pages' => [
|
|
|
|
|
'test.page' => ['label' => '测试页', 'scopes' => ['common']],
|
|
|
|
|
],
|
|
|
|
|
'entries' => [[
|
|
|
|
|
'key' => 'common.test.title',
|
|
|
|
|
'scope' => 'common',
|
|
|
|
|
'page' => 'test.page',
|
|
|
|
|
'kind' => 'title',
|
|
|
|
|
'source' => '中文安全基线',
|
|
|
|
|
'format' => 'plain_text',
|
|
|
|
|
'parameters' => [],
|
|
|
|
|
'criticality' => 'required',
|
|
|
|
|
'allow_hide' => false,
|
|
|
|
|
'toast_tone' => null,
|
|
|
|
|
'editor_surface' => 'page',
|
|
|
|
|
'editor_group' => 'test.page',
|
|
|
|
|
]],
|
|
|
|
|
]);
|
|
|
|
|
$compiler = new UserCopyCatalogCompiler(new UserCopySourceCatalog($manifest));
|
|
|
|
|
$root = sys_get_temp_dir() . '/virt-user-copy-store-'
|
|
|
|
|
. bin2hex(random_bytes(8));
|
|
|
|
|
mkdir($root, 0750, true);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$store = new UserCopyPublicationStore($root);
|
|
|
|
|
$first = $compiler->compile('en', [
|
|
|
|
|
'common.test.title' => '管理员发布版本一',
|
|
|
|
|
]);
|
|
|
|
|
$firstPointer = $store->publish($first);
|
|
|
|
|
$assert($firstPointer['generation'] === $first->generation,
|
|
|
|
|
'First immutable generation was not activated.');
|
|
|
|
|
$store->validateGeneration($first->generation);
|
|
|
|
|
|
|
|
|
|
$second = $compiler->compile('en', [
|
|
|
|
|
'common.test.title' => '管理员发布版本二',
|
|
|
|
|
]);
|
|
|
|
|
$secondPointer = $store->publish($second);
|
|
|
|
|
$assert($secondPointer['generation'] === $second->generation
|
|
|
|
|
&& $first->generation !== $second->generation,
|
|
|
|
|
'A changed catalog did not create a new generation.');
|
|
|
|
|
$assert(is_dir($root . '/generations/' . $first->generation)
|
|
|
|
|
&& is_dir($root . '/generations/' . $second->generation),
|
|
|
|
|
'Publishing removed a retained immutable generation.');
|
|
|
|
|
|
|
|
|
|
$rollback = $store->rollback('en', $first->generation);
|
|
|
|
|
$assert($rollback['generation'] === $first->generation,
|
|
|
|
|
'Rollback did not reactivate the retained generation.');
|
|
|
|
|
$current = json_decode(
|
|
|
|
|
(string) file_get_contents($root . '/current.json'),
|
|
|
|
|
true,
|
|
|
|
|
512,
|
|
|
|
|
JSON_THROW_ON_ERROR
|
|
|
|
|
);
|
|
|
|
|
$assert($current['locales']['en']['generation'] === $first->generation,
|
|
|
|
|
'The authoritative pointer disagrees with the rollback result.');
|
|
|
|
|
} finally {
|
|
|
|
|
$removeTree($root);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo "User copy publication store tests passed.\n";
|