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

111 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\UserCopy\UserCopyManifest;
$root = dirname(__DIR__);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$manifest = UserCopyManifest::load();
$entries = [];
foreach ($manifest['entries'] as $entry) {
$entries[(string) $entry['key']] = $entry;
}
$files = [];
$templateRoot = $root . '/resources/views/venix';
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$templateRoot,
FilesystemIterator::SKIP_DOTS
)
);
foreach ($iterator as $file) {
if ($file->isFile()
&& $file->getExtension() === 'tpl'
&& !str_starts_with(
$file->getPathname(),
$templateRoot . '/admin/'
)) {
$files[] = $file->getPathname();
}
}
sort($files, SORT_STRING);
$bindingCount = 0;
$boundGroups = [];
foreach ($files as $file) {
$source = file_get_contents($file);
$relative = substr($file, strlen($root) + 1);
$assert(is_string($source), 'Cannot read ' . $relative . '.');
preg_match_all(
'/\{user_copy_binding\b[^}]*\}/',
$source,
$calls,
PREG_OFFSET_CAPTURE
);
foreach ($calls[0] as [$call, $offset]) {
$bindingCount++;
$strict = preg_match(
'/\A\{user_copy_binding\s+key=([\'\"])([a-z0-9][a-z0-9._-]{1,190})\1\s*\}\z/D',
$call,
$match
);
$assert($strict === 1,
'Copy binding must use one literal key in ' . $relative . '.');
$key = (string) $match[2];
$assert(isset($entries[$key]),
'Copy binding references an unknown key in ' . $relative . ': ' . $key);
$assert($entries[$key]['editor_surface'] === 'page',
'Advanced copy cannot be bound to a page element: ' . $key);
$boundGroups[(string) $entries[$key]['editor_group']] = true;
$tagStart = strrpos(substr($source, 0, (int) $offset), '<');
$assert($tagStart !== false,
'Copy binding is outside an HTML start tag in ' . $relative . '.');
$prefix = substr(
$source,
(int) $tagStart,
(int) $offset - (int) $tagStart
);
$assert(preg_match(
'/\A<(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)\b/i',
$prefix
) !== 1,
'Copy binding cannot target a void element in ' . $relative . '.');
$assert(!str_contains($prefix, '>')
&& substr_count($prefix, '"') % 2 === 0
&& substr_count($prefix, "'") % 2 === 0,
'Copy binding is inside an attribute value in ' . $relative . '.');
$after = substr($source, (int) $offset + strlen($call));
$assert(preg_match('/\A\s*\/?>/', $after) === 1,
'Copy binding must be the final start-tag attribute in '
. $relative . '.');
}
}
foreach ([
'auth.login',
'auth.password',
'auth.register',
'common.shared',
'legal.privacy',
'legal.terms',
'public.error',
'public.home',
] as $requiredGroup) {
$assert(isset($boundGroups[$requiredGroup]),
'Public/shared copy group lacks safe bindings: ' . $requiredGroup);
}
$assert($bindingCount >= 150,
'Public/shared copy binding coverage unexpectedly regressed.');
echo "User copy binding interface contracts passed.\n";