210 lines
6.5 KiB
PHP
210 lines
6.5 KiB
PHP
<?php
|
|
|
|
use App\Models\BackupItem;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
uses(RefreshDatabase::class);
|
|
test('assignments cast works', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'assignments' => [
|
|
['id' => 'abc-123', 'target' => ['groupId' => 'group-1']],
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->assignments)->toBeArray()
|
|
->and($backupItem->assignments)->toHaveCount(1);
|
|
});
|
|
|
|
test('getAssignmentCountAttribute returns correct count', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'assignments' => [
|
|
['id' => 'abc-123', 'target' => ['groupId' => 'group-1']],
|
|
['id' => 'def-456', 'target' => ['groupId' => 'group-2']],
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->assignment_count)->toBe(2);
|
|
});
|
|
|
|
test('getAssignmentCountAttribute returns zero for null assignments', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'assignments' => null,
|
|
]);
|
|
|
|
expect($backupItem->assignment_count)->toBe(0);
|
|
});
|
|
|
|
test('hasAssignments returns true when assignments exist', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'assignments' => [
|
|
['id' => 'abc-123', 'target' => ['groupId' => 'group-1']],
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->hasAssignments())->toBeTrue();
|
|
});
|
|
|
|
test('hasAssignments returns false when assignments are null', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'assignments' => null,
|
|
]);
|
|
|
|
expect($backupItem->hasAssignments())->toBeFalse();
|
|
});
|
|
|
|
test('getGroupIdsAttribute extracts unique group IDs', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'assignments' => [
|
|
['id' => 'abc-123', 'target' => ['groupId' => 'group-1']],
|
|
['id' => 'def-456', 'target' => ['groupId' => 'group-2']],
|
|
['id' => 'ghi-789', 'target' => ['groupId' => 'group-1']], // duplicate
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->group_ids)->toHaveCount(2)
|
|
->and($backupItem->group_ids)->toContain('group-1', 'group-2');
|
|
});
|
|
|
|
test('getScopeTagIdsAttribute returns scope tag IDs from metadata', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [
|
|
'scope_tag_ids' => ['0', 'abc-123', 'def-456'],
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->scope_tag_ids)->toHaveCount(3)
|
|
->and($backupItem->scope_tag_ids)->toContain('0', 'abc-123', 'def-456');
|
|
});
|
|
|
|
test('getScopeTagIdsAttribute returns default when not in metadata', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [],
|
|
]);
|
|
|
|
expect($backupItem->scope_tag_ids)->toBe(['0']);
|
|
});
|
|
|
|
test('getScopeTagNamesAttribute returns scope tag names from metadata', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [
|
|
'scope_tag_names' => ['Default', 'HR-Admins', 'Finance'],
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->scope_tag_names)->toHaveCount(3)
|
|
->and($backupItem->scope_tag_names)->toContain('Default', 'HR-Admins', 'Finance');
|
|
});
|
|
|
|
test('getScopeTagNamesAttribute returns default when not in metadata', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [],
|
|
]);
|
|
|
|
expect($backupItem->scope_tag_names)->toBe(['Default']);
|
|
});
|
|
|
|
test('hasOrphanedAssignments returns true when flag is set', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [
|
|
'has_orphaned_assignments' => true,
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->hasOrphanedAssignments())->toBeTrue();
|
|
});
|
|
|
|
test('hasOrphanedAssignments returns false when flag is not set', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [],
|
|
]);
|
|
|
|
expect($backupItem->hasOrphanedAssignments())->toBeFalse();
|
|
});
|
|
|
|
test('assignmentsFetchFailed returns true when flag is set', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [
|
|
'assignments_fetch_failed' => true,
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->assignmentsFetchFailed())->toBeTrue();
|
|
});
|
|
|
|
test('assignmentsFetchFailed returns false when flag is not set', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [],
|
|
]);
|
|
|
|
expect($backupItem->assignmentsFetchFailed())->toBeFalse();
|
|
});
|
|
|
|
test('snapshotSource prefers snapshot source metadata when present', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [
|
|
'source' => 'policy_version',
|
|
'snapshot_source' => 'metadata_only',
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->snapshotSource())->toBe('metadata_only');
|
|
});
|
|
|
|
test('assignmentCaptureReason returns trimmed capture reason metadata', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [
|
|
'assignment_capture_reason' => ' separate_role_assignments ',
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->assignmentCaptureReason())->toBe('separate_role_assignments');
|
|
});
|
|
|
|
test('warningMessages filters non-string and empty warning entries', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [
|
|
'warnings' => [' metadata fallback ', '', null, 123],
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->warningMessages())->toBe(['metadata fallback']);
|
|
});
|
|
|
|
test('integrityWarning returns trimmed integrity metadata', function () {
|
|
$backupItem = BackupItem::factory()->create([
|
|
'metadata' => [
|
|
'integrity_warning' => ' Protected values are intentionally hidden. ',
|
|
],
|
|
]);
|
|
|
|
expect($backupItem->integrityWarning())->toBe('Protected values are intentionally hidden.');
|
|
});
|
|
|
|
test('hasCapturedPayload reflects whether payload data exists', function () {
|
|
$emptyPayload = BackupItem::factory()->create([
|
|
'payload' => [],
|
|
]);
|
|
|
|
$capturedPayload = BackupItem::factory()->create([
|
|
'payload' => ['id' => 'policy-1'],
|
|
]);
|
|
|
|
expect($emptyPayload->hasCapturedPayload())->toBeFalse()
|
|
->and($capturedPayload->hasCapturedPayload())->toBeTrue();
|
|
});
|
|
|
|
test('scopeWithAssignments filters items with assignments', function () {
|
|
BackupItem::factory()->create(['assignments' => null]);
|
|
BackupItem::factory()->create(['assignments' => []]);
|
|
$withAssignments = BackupItem::factory()->create([
|
|
'assignments' => [
|
|
['id' => 'abc-123', 'target' => ['groupId' => 'group-1']],
|
|
],
|
|
]);
|
|
|
|
$result = BackupItem::withAssignments()->get();
|
|
|
|
expect($result)->toHaveCount(1)
|
|
->and($result->first()->id)->toBe($withAssignments->id);
|
|
});
|