Add published-page accessibility and link checks with localized reports, and add responsive viewport controls to the page builder. Detect schema drift beyond matching hashes and apply lowercase definitions when creating or updating tables. Refresh generated admin, page-builder, and website-manager assets, and add coverage for quality checks, viewport selection, and schema migrations.
298 lines
12 KiB
PHP
298 lines
12 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../src/Modules/WebsiteManager/SiteQualityAnalyzer.php';
|
|
require_once __DIR__ . '/../src/Modules/WebsiteManager/SiteQualityCheckService.php';
|
|
|
|
$siteQualityConfig = [
|
|
'page' => ['translation' => ['foreign_key' => 'page_id']],
|
|
'general' => ['uploads_url' => '/uploads'],
|
|
'storage' => ['uploads_folder' => sys_get_temp_dir()],
|
|
];
|
|
$siteQualityLanguages = ['en' => 'English', 'de' => 'German'];
|
|
|
|
function phpb_config(?string $key = null)
|
|
{
|
|
global $siteQualityConfig;
|
|
if ($key === null) {
|
|
return $siteQualityConfig;
|
|
}
|
|
$value = $siteQualityConfig;
|
|
foreach (explode('.', $key) as $segment) {
|
|
if (! is_array($value) || ! array_key_exists($segment, $value)) {
|
|
return '';
|
|
}
|
|
$value = $value[$segment];
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
function phpb_active_languages(): array
|
|
{
|
|
global $siteQualityLanguages;
|
|
return $siteQualityLanguages;
|
|
}
|
|
|
|
function phpb_trans(string $key): string
|
|
{
|
|
return $key;
|
|
}
|
|
|
|
function phpb_e($value): string
|
|
{
|
|
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
function phpb_url(string $module, array $parameters = []): string
|
|
{
|
|
return '/' . $module . '?' . http_build_query($parameters);
|
|
}
|
|
|
|
function phpb_csrf_field(): void
|
|
{
|
|
echo '<input type="hidden" name="_csrf_token" value="test-token">';
|
|
}
|
|
|
|
function assertSiteQuality(bool $condition, string $message): void
|
|
{
|
|
if (! $condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
}
|
|
|
|
function runSiteQualityCase(string $name, callable $case): bool
|
|
{
|
|
try {
|
|
$case();
|
|
fwrite(STDOUT, "PASS: {$name}\n");
|
|
return true;
|
|
} catch (Throwable $exception) {
|
|
fwrite(STDERR, "FAIL: {$name}: {$exception->getMessage()}\n");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
class SiteQualityTestPage
|
|
{
|
|
public function __construct(public string $id, public string $name)
|
|
{
|
|
}
|
|
|
|
public function getId(): string
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
}
|
|
|
|
class SiteQualityTestPageRepository
|
|
{
|
|
public function __construct(private array $pages)
|
|
{
|
|
}
|
|
|
|
public function getAll(): array
|
|
{
|
|
return $this->pages;
|
|
}
|
|
}
|
|
|
|
class SiteQualityTestTranslationRepository
|
|
{
|
|
public function __construct(private array $translations)
|
|
{
|
|
}
|
|
|
|
public function getForPages(array $pageIds): array
|
|
{
|
|
return $this->translations;
|
|
}
|
|
}
|
|
|
|
class SiteQualityTestUploadRepository
|
|
{
|
|
public function __construct(private array $uploads)
|
|
{
|
|
}
|
|
|
|
public function getAll(): array
|
|
{
|
|
return $this->uploads;
|
|
}
|
|
}
|
|
|
|
function siteQualityTranslation(string $pageId, string $locale, string $route): object
|
|
{
|
|
return (object) ['page_id' => $pageId, 'locale' => $locale, 'route' => $route];
|
|
}
|
|
|
|
function siteQualityRules(array $findings): array
|
|
{
|
|
return array_column($findings, 'rule');
|
|
}
|
|
|
|
$cases = [
|
|
'compliant markup, active routes, and known assets produce no findings' => function (): void {
|
|
$analyzer = new Plugi\Modules\WebsiteManager\SiteQualityAnalyzer(
|
|
['/home', '/contact', '/de/about'],
|
|
['/uploads/file-1/logo.png'],
|
|
'https://example.test/plugi',
|
|
'/uploads'
|
|
);
|
|
$html = '<h1>Home</h1><h2>Section</h2><label for="email">Email</label><input id="email" type="email">'
|
|
. '<img alt="Logo" src="https://example.test/plugi/uploads/file-1/logo.png">'
|
|
. '<a href="https://example.test/plugi/contact?source=home#form">Contact</a>';
|
|
|
|
assertSiteQuality($analyzer->analyze($html, '/home') === [], 'Compliant markup or known links produced unexpected findings.');
|
|
},
|
|
'missing image alt, unnamed controls, and skipped heading levels are reported' => function (): void {
|
|
$analyzer = new Plugi\Modules\WebsiteManager\SiteQualityAnalyzer(['/home'], [], 'https://example.test/plugi', '/uploads');
|
|
$findings = $analyzer->analyze('<img src="/plugi/photo.png"><input type="text"><h1>Title</h1><h3>Subheading</h3>', '/home');
|
|
|
|
assertSiteQuality(in_array('image-alt', siteQualityRules($findings), true), 'An image without an alt attribute was not reported.');
|
|
assertSiteQuality(in_array('form-control-name', siteQualityRules($findings), true), 'An unnamed form control was not reported.');
|
|
assertSiteQuality(in_array('heading-level', siteQualityRules($findings), true), 'A skipped heading level was not reported.');
|
|
},
|
|
'localized internal routes and ignored external or non-navigation links are handled offline' => function (): void {
|
|
$analyzer = new Plugi\Modules\WebsiteManager\SiteQualityAnalyzer(
|
|
['/home', '/de/ueber-uns'],
|
|
['/uploads/image-1/photo.jpg'],
|
|
'https://example.test/plugi',
|
|
'/uploads'
|
|
);
|
|
$html = '<a href="/plugi/de/ueber-uns">German route</a>'
|
|
. '<a href="https://outside.test/anything">External</a>'
|
|
. '<a href="mailto:team@example.test">Email</a><a href="tel:+123">Phone</a>'
|
|
. '<a href="#details">Fragment</a>'
|
|
. '<img alt="Photo" src="/plugi/uploads/image-1/photo.jpg">';
|
|
|
|
assertSiteQuality($analyzer->analyze($html, '/home') === [], 'A valid localized route, local asset, or ignored external link was incorrectly reported.');
|
|
},
|
|
'same-site links outside the configured application base are not treated as local uploads' => function (): void {
|
|
$analyzer = new Plugi\Modules\WebsiteManager\SiteQualityAnalyzer(
|
|
['/home'],
|
|
['/uploads/image-1/photo.jpg'],
|
|
'https://example.test/plugi',
|
|
'/uploads'
|
|
);
|
|
|
|
$findings = $analyzer->analyze('<a href="/uploads/image-1/photo.jpg">Photo</a>', '/home');
|
|
assertSiteQuality(
|
|
in_array('missing-internal-route', siteQualityRules($findings), true),
|
|
'A root-level path outside the application base was incorrectly resolved to an application upload.'
|
|
);
|
|
},
|
|
'missing same-site routes and uploads are reported with actionable targets' => function (): void {
|
|
$analyzer = new Plugi\Modules\WebsiteManager\SiteQualityAnalyzer(
|
|
['/home'],
|
|
[],
|
|
'https://example.test/plugi',
|
|
'/uploads'
|
|
);
|
|
$findings = $analyzer->analyze('<a href="/plugi/not-found">Missing page</a><img alt="Missing" src="/plugi/uploads/no-file.png">', '/home');
|
|
|
|
assertSiteQuality(in_array('missing-internal-route', siteQualityRules($findings), true), 'A missing same-site route was not reported.');
|
|
assertSiteQuality(in_array('missing-local-asset', siteQualityRules($findings), true), 'A missing local upload was not reported.');
|
|
assertSiteQuality(count(array_filter($findings, static fn(array $finding): bool => $finding['target'] !== '')) === 2, 'Findings did not identify affected targets.');
|
|
},
|
|
'only active language routes are scanned' => function (): void {
|
|
global $siteQualityLanguages;
|
|
$siteQualityLanguages = ['en' => 'English'];
|
|
$renderedLocales = [];
|
|
$service = new Plugi\Modules\WebsiteManager\SiteQualityCheckService(
|
|
new SiteQualityTestPageRepository([new SiteQualityTestPage('1', 'Localized page')]),
|
|
new SiteQualityTestTranslationRepository([
|
|
siteQualityTranslation('1', 'en', '/localized'),
|
|
siteQualityTranslation('1', 'fr', '/fr/localized'),
|
|
]),
|
|
new SiteQualityTestUploadRepository([]),
|
|
static function (SiteQualityTestPage $page, string $locale) use (&$renderedLocales): string {
|
|
$renderedLocales[] = $locale;
|
|
return '<h1>Page</h1>';
|
|
}
|
|
);
|
|
|
|
$report = $service->run();
|
|
assertSiteQuality($report['pages_checked'] === 1 && $renderedLocales === ['en'], 'An inactive translation was included in the live-site scan.');
|
|
},
|
|
'renderer failure is page-scoped and does not stop later page locales' => function (): void {
|
|
global $siteQualityLanguages;
|
|
$siteQualityLanguages = ['en' => 'English'];
|
|
$pages = [new SiteQualityTestPage('1', 'Broken render'), new SiteQualityTestPage('2', 'Available page')];
|
|
$translations = [siteQualityTranslation('1', 'en', '/broken'), siteQualityTranslation('2', 'en', '/available')];
|
|
$service = new Plugi\Modules\WebsiteManager\SiteQualityCheckService(
|
|
new SiteQualityTestPageRepository($pages),
|
|
new SiteQualityTestTranslationRepository($translations),
|
|
new SiteQualityTestUploadRepository([]),
|
|
static function (SiteQualityTestPage $page): string {
|
|
if ($page->id === '1') {
|
|
throw new RuntimeException('template failure');
|
|
}
|
|
return '<h1>Available</h1>';
|
|
}
|
|
);
|
|
|
|
$report = $service->run();
|
|
assertSiteQuality($report['pages_checked'] === 2, 'The audit did not continue after a renderer failure.');
|
|
assertSiteQuality(count($report['errors']) === 1, 'The renderer failure was not captured as one page-scoped error.');
|
|
assertSiteQuality($report['errors'][0]['page_id'] === '1' && $report['errors'][0]['locale'] === 'en', 'The coverage error omitted its page or locale.');
|
|
},
|
|
'upload inventory rejects file paths resolved outside the configured uploads directory' => function (): void {
|
|
global $siteQualityConfig, $siteQualityLanguages;
|
|
$siteQualityLanguages = ['en' => 'English'];
|
|
$siteQualityConfig['storage']['uploads_folder'] = sys_get_temp_dir();
|
|
$page = new SiteQualityTestPage('1', 'Path safety');
|
|
$outsidePath = str_repeat('../', 64) . ltrim(__FILE__, DIRECTORY_SEPARATOR);
|
|
$upload = (object) ['public_id' => 'outside', 'original_file' => 'test.php', 'server_file' => $outsidePath];
|
|
$service = new Plugi\Modules\WebsiteManager\SiteQualityCheckService(
|
|
new SiteQualityTestPageRepository([$page]),
|
|
new SiteQualityTestTranslationRepository([siteQualityTranslation('1', 'en', '/safety')]),
|
|
new SiteQualityTestUploadRepository([$upload]),
|
|
static fn(): string => '<img alt="Outside" src="/uploads/outside/test.php">'
|
|
);
|
|
|
|
$report = $service->run();
|
|
assertSiteQuality(
|
|
in_array('missing-local-asset', siteQualityRules($report['findings']), true),
|
|
'An upload path outside the configured public area was trusted as a valid asset.'
|
|
);
|
|
},
|
|
'quality tab groups findings and escapes actionable context' => function (): void {
|
|
$qualityReport = [
|
|
'pages_checked' => 1,
|
|
'findings' => [[
|
|
'page_id' => '1',
|
|
'page_name' => '<script>alert(1)</script>',
|
|
'route' => '/home',
|
|
'locale' => 'en',
|
|
'rule' => 'missing-internal-route',
|
|
'severity' => 'warning',
|
|
'explanation' => 'The linked page was not found.',
|
|
'target' => '/missing"><script>alert(2)</script>',
|
|
]],
|
|
'errors' => [],
|
|
];
|
|
ob_start();
|
|
require __DIR__ . '/../src/Modules/WebsiteManager/resources/views/tabs/quality.php';
|
|
$html = ob_get_clean();
|
|
|
|
assertSiteQuality(str_contains($html, 'route=quality&action=scan&tab=quality'), 'The quality scan form does not target its authenticated action.');
|
|
assertSiteQuality(str_contains($html, 'name="_csrf_token"'), 'The quality scan form is missing its CSRF token field.');
|
|
assertSiteQuality(str_contains($html, 'website-manager.quality-rule-missing-internal-route'), 'Findings were not grouped under their rule label.');
|
|
assertSiteQuality(str_contains($html, '<script>alert(1)</script>'), 'The affected page name was not escaped.');
|
|
assertSiteQuality(str_contains($html, '<script>alert(2)</script>'), 'The affected target was not escaped.');
|
|
},
|
|
];
|
|
|
|
$passed = 0;
|
|
foreach ($cases as $name => $case) {
|
|
if (runSiteQualityCase($name, $case)) {
|
|
$passed++;
|
|
}
|
|
}
|
|
|
|
fwrite(STDOUT, sprintf("%d/%d site quality check cases passed.\n", $passed, count($cases)));
|
|
exit($passed === count($cases) ? 0 : 1); |