Files
breadcrumb-the-shire/tests/Architecture/AuthzUiActionContractTest.php
fs e10b1215da fix(audit): restructure module pages to avoid admin/ path collision
Module pages must use a unique top-level directory (audit/) instead of
nesting under admin/ which collides with core's pages/admin/ during
module:build symlink creation. Routes still map admin/* URLs to the
audit/ page directory via manifest route targets.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 08:13:37 +01:00

87 lines
3.3 KiB
PHP

<?php
namespace MintyPHP\Tests\Architecture;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class AuthzUiActionContractTest extends TestCase
{
use AuthzUiContractSupport;
use ProjectFileAssertionSupport;
#[DataProvider('adminPageAuthWiringProvider')]
public function testAdminPageAuthKeysAreWiredFromActions(
string $templateFile,
string $actionFile,
?string $uiCapabilityMapConstant
): void {
$templateContent = $this->readProjectFile($templateFile);
$requiredKeys = $this->extractPageAuthKeys($templateContent);
$this->assertNotEmpty($requiredKeys, $templateFile . ' must read at least one $pageAuth key.');
$actionContent = $this->readProjectFile($actionFile);
$this->assertStringContainsString("\$viewAuth['page']", $actionContent, $actionFile);
$wiredKeys = $this->extractViewAuthPageKeys($actionContent);
if ($uiCapabilityMapConstant !== null) {
$this->assertStringContainsString('->pageCapabilities(', $actionContent, $actionFile);
$this->assertStringContainsString($uiCapabilityMapConstant, $actionContent, $actionFile);
$wiredKeys = array_values(array_unique([
...$wiredKeys,
...$this->extractUiCapabilityMapKeys($uiCapabilityMapConstant),
]));
}
$missingKeys = array_values(array_diff($requiredKeys, $wiredKeys));
$this->assertSame(
[],
$missingKeys,
sprintf(
'Missing $pageAuth wiring in %s (from %s): %s',
$templateFile,
$actionFile,
implode(', ', $missingKeys)
)
);
}
public function testTenantEditActionExposesDeleteCapabilityForTemplate(): void
{
$content = $this->readProjectFile('pages/admin/tenants/edit($id).php');
$this->assertStringContainsString("'can_delete_tenant' =>", $content);
}
public function testMapDrivenHardCutActionsUseUiCapabilityMaps(): void
{
$actions = [
'pages/admin/users/index().php' => 'UiCapabilityMap::PAGE_USERS_INDEX',
'pages/admin/users/create().php' => 'UiCapabilityMap::PAGE_USERS_CREATE',
'pages/admin/stats/index().php' => 'UiCapabilityMap::PAGE_STATS_INDEX',
'pages/admin/scheduled-jobs/index().php' => 'UiCapabilityMap::PAGE_JOBS_PURGE',
];
foreach ($actions as $action => $mapConstant) {
$content = $this->readProjectFile($action);
$this->assertStringContainsString('->pageCapabilities(', $content, $action);
$this->assertStringContainsString($mapConstant, $content, $action);
}
}
public function testModuleAuditActionsUseInlinePageCapabilities(): void
{
$actions = [
'modules/audit/pages/audit/api-audit/index().php',
'modules/audit/pages/audit/system-audit/index().php',
'modules/audit/pages/audit/import-audit/index().php',
'modules/audit/pages/audit/user-lifecycle-audit/index().php',
'modules/audit/pages/audit/user-lifecycle-audit/view($id).php',
];
foreach ($actions as $action) {
$content = $this->readProjectFile($action);
$this->assertStringContainsString('->pageCapabilities(', $content, $action);
}
}
}