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/UserCopyAuthoringProjection...

245 lines
8.0 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\UserCopy\UserCopyAdminException;
use App\Services\UserCopy\UserCopyAuthoringProjection;
use App\Services\UserCopy\UserCopyCatalogException;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$rejects = static function (
callable $callback,
string $exceptionClass,
string $message
): void {
try {
$callback();
} catch (Throwable $exception) {
if ($exception instanceof $exceptionClass) {
return;
}
throw new RuntimeException(
$message . ' Wrong exception: ' . get_class($exception)
);
}
throw new RuntimeException($message);
};
$entry = static function (
string $key,
string $scope,
string $page,
string $source,
string $kind,
string $surface,
string $group
): array {
return [
'key' => $key,
'scope' => $scope,
'page' => $page,
'kind' => $kind,
'source' => $source,
'format' => 'plain_text',
'parameters' => [],
'criticality' => 'required',
'allow_hide' => false,
'toast_tone' => null,
'editor_surface' => $surface,
'editor_group' => $group,
];
};
$manifest = [
'version' => 1,
'pages' => [
'a.page' => ['label' => '页面 A', 'scopes' => ['common', 'a']],
'b.page' => ['label' => '页面 B', 'scopes' => ['common', 'b']],
],
'entries' => [
$entry(
'a.title', 'a', 'a.page', '页面 A 标题', 'title', 'page', 'a.page'
),
$entry(
'b.admin', 'b', 'b.page', '后台补充文案', 'label',
'admin', 'b.advanced'
),
$entry(
'b.common_title', 'common', 'b.page', '页面 B 公共词条', 'label',
'page', 'b.page'
),
$entry(
'b.dialog', 'b', 'b.page', '页面 B 弹窗', 'confirmation',
'dialog', 'b.advanced'
),
$entry(
'b.title', 'b', 'b.page', '页面 B 标题', 'title', 'page', 'b.page'
),
$entry(
'common.shell.title', 'common', 'a.page', '公共导航',
'navigation', 'shared', 'shell.user'
),
$entry(
'common.shell.aria', 'common', 'a.page', '公共导航无障碍标签',
'aria_label', 'accessibility', 'shell.user'
),
$entry(
'common.warning', 'common', 'a.page', '公共提醒', 'warning',
'feedback', 'common.shared'
),
],
];
$rows = array_map(
static fn (array $definition): object => (object) [
'copy_key' => $definition[0],
'scope' => $definition[1],
],
[
['a.title', 'a'],
['b.admin', 'b'],
['b.common_title', 'common'],
['b.dialog', 'b'],
['b.title', 'b'],
['common.shell.aria', 'common'],
['common.shell.title', 'common'],
['common.warning', 'common'],
]
);
$keys = static fn (array $projection): array => array_map(
static fn (object $row): string => (string) $row->copy_key,
$projection['entry_rows']
);
$projection = new UserCopyAuthoringProjection($manifest);
$default = $projection->project([], $rows);
$assert($default['selected_page'] === 'a.page'
&& $default['channel'] === 'legacy'
&& $default['selected_group'] === ''
&& $keys($default) === [
'a.title', 'b.common_title', 'common.shell.aria',
'common.shell.title', 'common.warning',
],
'Default legacy authoring projection changed its page-scope behavior.');
$assert($default['pages'] === [
['id' => 'a.page', 'label' => '页面 A'],
['id' => 'b.page', 'label' => '页面 B'],
]
&& $default['groups'] === [
['id' => 'a.page', 'label' => '页面 A'],
['id' => 'b.page', 'label' => '页面 B'],
['id' => 'common.shared', 'label' => '公共框架'],
['id' => 'shell.user', 'label' => 'shell.user'],
],
'Legacy groups were not limited to groups visible in the channel.');
$pageA = $projection->project([
'page' => 'a.page',
'channel' => 'page',
], $rows);
$assert($keys($pageA) === ['a.title'],
'Page channel did not isolate entries owned by the selected page.');
$assert($pageA['groups'] === [
['id' => 'a.page', 'label' => '页面 A'],
],
'Page channel exposed groups owned by another channel.');
$pageB = $projection->project([
'page' => 'b.page',
'channel' => 'page',
], $rows);
$assert($keys($pageB) === ['b.common_title', 'b.title'],
'Page channel did not follow explicit authoring ownership.');
$assert($pageB['groups'] === [
['id' => 'b.page', 'label' => '页面 B'],
],
'Page channel did not expose its current authoring group.');
$shared = $projection->project([
'page' => 'b.page',
'channel' => 'shared',
], $rows);
$assert($keys($shared) === ['common.shell.title'],
'Shared channel did not isolate visible reusable copy.');
$assert($shared['groups'] === [
['id' => 'shell.user', 'label' => 'shell.user'],
],
'Shared channel exposed groups without shared entries.');
$advanced = $projection->project([
'page' => 'a.page',
'channel' => 'advanced',
], $rows);
$assert($keys($advanced) === [
'b.admin', 'b.dialog', 'common.shell.aria', 'common.warning',
],
'Advanced channel did not retain administrator-owned supplemental copy.');
$assert($advanced['groups'] === [
['id' => 'b.advanced', 'label' => 'b.advanced'],
['id' => 'common.shared', 'label' => '公共框架'],
['id' => 'shell.user', 'label' => 'shell.user'],
],
'Advanced channel group list did not follow its complete projection.');
$all = $projection->project([
'page' => 'b.page',
'channel' => 'all',
], $rows);
$assert($keys($all) === [
'a.title', 'b.admin', 'b.common_title', 'b.dialog', 'b.title',
'common.shell.aria', 'common.shell.title',
'common.warning',
],
'All-copy authoring projection no longer includes every Manifest entry.');
$assert($all['groups'] === [
['id' => 'a.page', 'label' => '页面 A'],
['id' => 'b.advanced', 'label' => 'b.advanced'],
['id' => 'b.page', 'label' => '页面 B'],
['id' => 'common.shared', 'label' => '公共框架'],
['id' => 'shell.user', 'label' => 'shell.user'],
],
'All-copy channel omitted a globally available authoring group.');
$grouped = $projection->project([
'page' => 'b.page',
'channel' => 'shared',
'group' => 'shell.user',
], $rows);
$assert($keys($grouped) === ['common.shell.title'],
'Authoring group filtering no longer runs after channel selection.');
$assert($grouped['groups'] === [
['id' => 'shell.user', 'label' => 'shell.user'],
],
'Selected group incorrectly narrowed the channel group list.');
$crossChannelGroup = $projection->project([
'page' => 'a.page',
'channel' => 'page',
'group' => 'b.advanced',
], $rows);
$assert($keys($crossChannelGroup) === []
&& $crossChannelGroup['groups'] === [
['id' => 'a.page', 'label' => '页面 A'],
],
'A valid cross-channel group did not produce an empty page projection.');
$assert(isset($grouped['metadata_by_key']['common.shell.title'])
&& $grouped['metadata_by_key']['common.shell.title']['editor_surface']
=== 'shared',
'Projection omitted the Manifest metadata needed by workspace DTO hydration.');
$rejects(
static fn () => $projection->project(['page' => 'missing.page'], $rows),
UserCopyCatalogException::class,
'Unknown authoring page was accepted.'
);
$rejects(
static fn () => $projection->project(['channel' => 'invalid'], $rows),
UserCopyAdminException::class,
'Unknown authoring channel was accepted.'
);
$rejects(
static fn () => $projection->project(['group' => 'missing.group'], $rows),
UserCopyAdminException::class,
'Unknown authoring group was accepted.'
);
echo "User copy authoring projection checks passed.\n";