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

141 lines
5.4 KiB
PHP

<?php
// Ordinary user pages must keep confirmations and notices inside the product UI.
// Administrator scripts and third-party assets are intentionally outside this scope.
$root = dirname(__DIR__);
$public = $root . '/public';
$views = $root . '/resources/views/venix';
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$files = glob($public . '/cloud/*.js') ?: [];
$files = array_values(array_filter(
$files,
static fn (string $path): bool => !str_starts_with(basename($path), 'admin')
));
foreach (['auth.js', 'user-home.js', 'ticket-page.js', 'recharge.js', 'fuck.js'] as $script) {
$files[] = $public . '/theme/venix/js/' . $script;
}
foreach (['auth', 'cloud', 'home', 'password', 'user'] as $viewDirectory) {
$directory = $views . '/' . $viewDirectory;
if (!is_dir($directory)) {
continue;
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS)
);
foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'tpl') {
$files[] = $file->getPathname();
}
}
}
$violations = [];
$patterns = [
'/\b(?:window|globalThis|self|top|parent)\s*\.\s*(?:alert|confirm|prompt)\s*\(/',
'/(?<![A-Za-z0-9_$.])\b(?:alert|confirm|prompt)\s*\(/',
'/\bonbeforeunload\b/',
'/addEventListener\s*\(\s*[\'\"]beforeunload[\'\"]/',
];
foreach (array_unique($files) as $path) {
$source = file_get_contents($path);
if (!is_string($source)) {
throw new RuntimeException('Unable to read ' . $path);
}
foreach ($patterns as $pattern) {
if (preg_match_all($pattern, $source, $matches, PREG_OFFSET_CAPTURE) !== false) {
foreach ($matches[0] as [$match, $offset]) {
$line = substr_count(substr($source, 0, $offset), "\n") + 1;
$violations[$path . ':' . $line] = trim($match);
}
}
}
}
if ($violations !== []) {
$details = [];
foreach ($violations as $location => $call) {
$details[] = str_replace($root . '/', '', $location) . ' (' . $call . ')';
}
throw new RuntimeException(
"Ordinary user pages still invoke browser-native dialogs:\n- "
. implode("\n- ", $details)
);
}
$topbar = file_get_contents($views . '/user/panel_topbar.tpl');
$markup = file_get_contents($views . '/user/dialog.tpl');
$runtime = file_get_contents($public . '/cloud/user-dialog.js');
$styles = file_get_contents($public . '/cloud/client.css');
$assets = file_get_contents($views . '/layout_assets.tpl');
$entry = file_get_contents($public . '/cloud/app.js');
$cloudLayout = file_get_contents($views . '/cloud/page.tpl');
$copyBootstrap = file_get_contents($views . '/user/user_copy_bootstrap.tpl');
foreach (compact(
'topbar',
'markup',
'runtime',
'styles',
'assets',
'entry',
'cloudLayout',
'copyBootstrap'
) as $name => $source) {
$assert(is_string($source), 'Unable to read user dialog asset: ' . $name);
}
$assert(str_contains($topbar, "{include file='user/dialog.tpl'}")
&& str_contains(
$topbar,
'script defer src="/cloud/user-dialog.js?v={$venix_asset_version'
),
'The shared user shell must defer the in-product dialog until the user-copy runtime is available.');
$topbarAt = strpos($cloudLayout, "{include file='user/panel_topbar.tpl'");
$copyBootstrapAt = strpos(
$cloudLayout,
"{include file='user/user_copy_bootstrap.tpl'}"
);
$assert($topbarAt !== false && $copyBootstrapAt !== false
&& $topbarAt < $copyBootstrapAt
&& str_contains(
$copyBootstrap,
'<script src="/cloud/user-copy-runtime.js?v={$venix_asset_version'
)
&& !str_contains(
$copyBootstrap,
'<script defer src="/cloud/user-copy-runtime.js?v={$venix_asset_version'
),
'Cloud pages must synchronously publish the user-copy runtime before deferred dialogs execute.');
$assert(str_contains($markup, 'id="panel-user-dialog"')
&& str_contains($markup, 'aria-labelledby="panel-user-dialog-title"')
&& str_contains($markup, 'aria-describedby="panel-user-dialog-message panel-user-dialog-verification-hint"'),
'The shared user dialog must expose an accessible name and description.');
$assert(str_contains($runtime, 'window.VenixDialog = Object.freeze')
&& str_contains($runtime, 'textContent =')
&& !str_contains($runtime, 'innerHTML'),
'The dialog API must render dynamic copy as text and expose the shared browser contract.');
$assert(str_contains($styles, '.panel-user-dialog::backdrop')
&& str_contains($styles, '.panel-user-dialog.is-dialog-fallback')
&& str_contains($styles, '@media (prefers-reduced-motion: reduce)'),
'The user dialog must retain a styled fallback and reduced-motion behavior.');
$assert(preg_match("/value='([0-9]+)'/", $assets) === 1
&& str_contains($entry, "const assetVersion = new URL(import.meta.url).searchParams.get('v');")
&& str_contains($entry, 'function versionedModuleUrl(path)')
&& str_contains($entry, 'new URL(path, import.meta.url)')
&& str_contains($entry, "url.searchParams.set('v', assetVersion)")
&& str_contains($entry, "import(versionedModuleUrl('./task-center.js'))")
&& preg_match('/task-center\\.js\\?v=[0-9]+/', $entry) !== 1,
'Nested application modules must inherit the single shared asset version at runtime.');
echo "Ordinary user native-dialog boundary checks passed.\n";