forked from fa/breadcrumb-the-shire
Split filter drawer contracts
This commit is contained in:
@@ -45,6 +45,112 @@ final class ModulePermissionSynchronizerTest extends TestCase
|
||||
self::assertSame(1, $second['unchanged']);
|
||||
}
|
||||
|
||||
public function testSyncDeactivatesOrphanedSystemPermissions(): void
|
||||
{
|
||||
// Module claims 'mod.perm_a' — 'mod.perm_b' is orphaned (no module owns it)
|
||||
$registry = $this->createRegistryWithPermissions([[
|
||||
'key' => 'mod.perm_a',
|
||||
'description' => 'Active permission',
|
||||
'active' => 1,
|
||||
'is_system' => 1,
|
||||
]]);
|
||||
|
||||
$repository = new InMemoryPermissionRepository();
|
||||
$repository->create([
|
||||
'key' => 'mod.perm_a',
|
||||
'description' => 'Active permission',
|
||||
'active' => 1,
|
||||
'is_system' => 1,
|
||||
]);
|
||||
$repository->create([
|
||||
'key' => 'mod.perm_b',
|
||||
'description' => 'Orphaned module permission',
|
||||
'active' => 1,
|
||||
'is_system' => 1,
|
||||
]);
|
||||
|
||||
$synchronizer = new ModulePermissionSynchronizer($registry, $repository);
|
||||
$result = $synchronizer->sync();
|
||||
|
||||
self::assertSame(1, $result['deactivated']);
|
||||
self::assertSame(1, $result['unchanged']);
|
||||
|
||||
$orphaned = $repository->findByKey('mod.perm_b');
|
||||
self::assertSame(0, (int) ($orphaned['active'] ?? 1), 'Orphaned permission should be deactivated');
|
||||
|
||||
$active = $repository->findByKey('mod.perm_a');
|
||||
self::assertSame(1, (int) ($active['active'] ?? 0), 'Claimed permission should stay active');
|
||||
}
|
||||
|
||||
public function testSyncDoesNotDeactivateAdminCreatedPermissions(): void
|
||||
{
|
||||
// Empty module — no permissions declared
|
||||
$registry = $this->createRegistryWithPermissions([]);
|
||||
|
||||
$repository = new InMemoryPermissionRepository();
|
||||
$repository->create([
|
||||
'key' => 'admin.custom',
|
||||
'description' => 'Admin-created permission',
|
||||
'active' => 1,
|
||||
'is_system' => 0,
|
||||
]);
|
||||
|
||||
$synchronizer = new ModulePermissionSynchronizer($registry, $repository);
|
||||
$result = $synchronizer->sync();
|
||||
|
||||
self::assertSame(0, $result['deactivated']);
|
||||
|
||||
$adminPerm = $repository->findByKey('admin.custom');
|
||||
self::assertSame(1, (int) ($adminPerm['active'] ?? 0), 'Admin-created permission must not be touched');
|
||||
}
|
||||
|
||||
public function testSyncReactivatesPermissionWhenModuleReEnabled(): void
|
||||
{
|
||||
$registry = $this->createRegistryWithPermissions([[
|
||||
'key' => 'mod.perm_a',
|
||||
'description' => 'Reactivated permission',
|
||||
'active' => 1,
|
||||
'is_system' => 1,
|
||||
]]);
|
||||
|
||||
$repository = new InMemoryPermissionRepository();
|
||||
// Simulate previously deactivated permission
|
||||
$repository->create([
|
||||
'key' => 'mod.perm_a',
|
||||
'description' => 'Reactivated permission',
|
||||
'active' => 0,
|
||||
'is_system' => 1,
|
||||
]);
|
||||
|
||||
$synchronizer = new ModulePermissionSynchronizer($registry, $repository);
|
||||
$result = $synchronizer->sync();
|
||||
|
||||
self::assertSame(0, $result['created']);
|
||||
self::assertSame(1, $result['updated']);
|
||||
self::assertSame(0, $result['deactivated']);
|
||||
|
||||
$perm = $repository->findByKey('mod.perm_a');
|
||||
self::assertSame(1, (int) ($perm['active'] ?? 0), 'Permission should be reactivated by sync');
|
||||
}
|
||||
|
||||
public function testSyncSkipsAlreadyDeactivatedOrphans(): void
|
||||
{
|
||||
$registry = $this->createRegistryWithPermissions([]);
|
||||
|
||||
$repository = new InMemoryPermissionRepository();
|
||||
$repository->create([
|
||||
'key' => 'mod.old_perm',
|
||||
'description' => 'Already deactivated',
|
||||
'active' => 0,
|
||||
'is_system' => 1,
|
||||
]);
|
||||
|
||||
$synchronizer = new ModulePermissionSynchronizer($registry, $repository);
|
||||
$result = $synchronizer->sync();
|
||||
|
||||
self::assertSame(0, $result['deactivated'], 'Already-inactive permission should not be counted');
|
||||
}
|
||||
|
||||
public function testSyncUpdatesExistingPermissionWhenDefinitionChanged(): void
|
||||
{
|
||||
$registry = $this->createRegistryWithPermissions([[
|
||||
@@ -178,4 +284,9 @@ final class InMemoryPermissionRepository implements PermissionRepositoryInterfac
|
||||
unset($this->rows[$id]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function listSystem(): array
|
||||
{
|
||||
return array_values(array_filter($this->rows, static fn (array $row): bool => (int) ($row['is_system'] ?? 0) === 1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user