Files
xuetang/server/tests/DataScopeMultiRoleTest.php
2026-09-08 11:40:15 +08:00

51 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
use app\common\service\DataScope\DataScopeService;
require dirname(__DIR__) . '/vendor/autoload.php';
function dataScopeMultiRoleExpect(bool $condition, string $message): void
{
if (!$condition) {
throw new RuntimeException($message);
}
}
$mergeRoleScopes = (new ReflectionClass(DataScopeService::class))->getMethod('mergeRoleScopes');
$mergeRoleScopes->setAccessible(true);
$role = static fn (string $name, int $scope): array => ['name' => $name, 'data_scope' => $scope];
dataScopeMultiRoleExpect(
$mergeRoleScopes->invoke(null, [$role('医助', 4)]) === DataScopeService::SCOPE_SELF,
'Single assistant role must remain self-only'
);
dataScopeMultiRoleExpect(
$mergeRoleScopes->invoke(null, [$role('诊室组长', 3), $role('医助', 4)]) === DataScopeService::SCOPE_DEPT,
'Group leader plus assistant must use the group scope'
);
dataScopeMultiRoleExpect(
$mergeRoleScopes->invoke(null, [$role('经理', 2), $role('诊室组长', 3), $role('医助', 4)])
=== DataScopeService::SCOPE_DEPT_AND_CHILD,
'Manager plus narrower roles must use department-and-child scope'
);
dataScopeMultiRoleExpect(
$mergeRoleScopes->invoke(null, [$role('医生', 1), $role('医助', 4)]) === DataScopeService::SCOPE_SELF,
'Legacy all-scope functional role must not widen a bounded role'
);
dataScopeMultiRoleExpect(
$mergeRoleScopes->invoke(null, [$role('管理员', 1), $role('医助', 4)]) === DataScopeService::SCOPE_ALL,
'Explicit administrator role must retain all-data scope'
);
dataScopeMultiRoleExpect(
$mergeRoleScopes->invoke(null, [$role('下单', 1)]) === DataScopeService::SCOPE_ALL,
'A standalone legacy all-scope role must keep its existing behavior'
);
dataScopeMultiRoleExpect(
$mergeRoleScopes->invoke(null, [$role('脏数据', 0)]) === DataScopeService::SCOPE_SELF,
'Invalid role scopes must fail closed to self-only'
);
echo "DataScopeMultiRoleTest passed\n";