Files
zyt/server/tests/AdminMultiRoleRegressionTest.php
T
2026-08-17 09:06:49 +08:00

57 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
use app\adminapi\logic\auth\AdminLogic;
use app\common\cache\AdminAuthCache;
require dirname(__DIR__) . '/vendor/autoload.php';
function adminMultiRoleExpect(bool $condition, string $message): void
{
if (!$condition) {
throw new RuntimeException($message);
}
}
$adminReflection = new ReflectionClass(AdminLogic::class);
$normalizeRoleIds = $adminReflection->getMethod('normalizeRoleIds');
$roleIdsChanged = $adminReflection->getMethod('roleIdsChanged');
$normalizeRoleIds->setAccessible(true);
$roleIdsChanged->setAccessible(true);
adminMultiRoleExpect(
$normalizeRoleIds->invoke(null, ['7', 2, 7, 0, -1]) === [2, 7],
'Role ids must be normalized, deduplicated and sorted'
);
adminMultiRoleExpect(
$roleIdsChanged->invoke(null, [2], [2, 7]) === true,
'Adding a role must invalidate the existing token'
);
adminMultiRoleExpect(
$roleIdsChanged->invoke(null, [2, 7], [2]) === true,
'Removing a role must invalidate the existing token'
);
adminMultiRoleExpect(
$roleIdsChanged->invoke(null, [2, 7], [7, 2]) === false,
'Changing only role order must not invalidate the token'
);
$cacheMethod = new ReflectionMethod(AdminAuthCache::class, 'clearAuthCache');
$sourceLines = file($cacheMethod->getFileName());
$methodSource = implode('', array_slice(
$sourceLines,
$cacheMethod->getStartLine() - 1,
$cacheMethod->getEndLine() - $cacheMethod->getStartLine() + 1
));
adminMultiRoleExpect(
str_contains($methodSource, 'delete($this->cacheUrlKey)'),
'Single-admin permission cache must delete its concrete cache key'
);
adminMultiRoleExpect(
!str_contains($methodSource, 'tag($this->cacheUrlKey)'),
'Single-admin permission cache must not clear a tag that was never assigned'
);
echo "AdminMultiRoleRegressionTest passed\n";