TenantAtlas/tests/Feature/Rbac/TenantActionSurfaceConsistencyTest.php
ahmido 37c6d0622c feat: implement spec 169 action surface contract v1.1 (#200)
## Summary
- implement the Action Surface Contract v1.1 runtime changes for Spec 169
- add the new explicit ActionSurfaceType contract, validator/discovery updates, and enrolled surface declarations
- update Filament action-surface documentation, focused guard tests, and spec artifacts for the completed feature

## Included
- clickable-row vs explicit-inspect enforcement across monitoring, reporting, CRUD, and system reference surfaces
- helper-first, workflow-next, destructive-last overflow ordering checks
- system panel list discovery in the primary action-surface validator
- Spec 169 artifacts: spec, plan, tasks, research, data model, quickstart, and logical contract

## Verification
- focused Pest verification pack completed for:
  - tests/Feature/Guards/ActionSurfaceValidatorTest.php
  - tests/Feature/Guards/ActionSurfaceContractTest.php
  - tests/Feature/Rbac/TenantActionSurfaceConsistencyTest.php
- integrated browser smoke test completed for admin-side reference surfaces:
  - /admin/operations
  - /admin/audit-log
  - /admin/finding-exceptions/queue
  - /admin/reviews
  - /admin/tenants

## Notes
- system panel browser smoke coverage could not be exercised in the same session because /system routes require platform authentication in the integrated browser
- Livewire target remains v4-compliant and no provider registration or asset strategy changes are introduced by this PR

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #200
2026-03-30 09:21:39 +00:00

284 lines
12 KiB
PHP

<?php
declare(strict_types=1);
use App\Filament\Resources\TenantResource;
use App\Filament\Resources\TenantResource\Pages\EditTenant;
use App\Filament\Resources\TenantResource\Pages\ListTenants;
use App\Filament\Resources\TenantResource\Pages\ViewTenant;
use App\Models\Tenant;
use App\Support\Ui\ActionSurface\Enums\ActionSurfaceSlot;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Actions\ActionGroup;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
function tenantActionSurfaceSearchTitles($results): array
{
return collect($results)->map(fn ($result): string => (string) $result->title)->values()->all();
}
it('keeps onboarding lifecycle actions consistent across list, view, and edit surfaces', function (): void {
$tenant = Tenant::factory()->onboarding()->create();
[$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'started_by' => $user,
'updated_by' => $user,
'state' => [
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
],
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
Livewire::actingAs($user)
->test(ListTenants::class)
->assertTableActionVisible('related_onboarding', $tenant)
->assertTableActionHidden('archive', $tenant)
->assertTableActionHidden('restore', $tenant);
Filament::setTenant(null, true);
Livewire::actingAs($user)
->test(ViewTenant::class, ['record' => $tenant->getRouteKey()])
->assertActionVisible('related_onboarding')
->assertActionHidden('archive')
->assertActionHidden('restore');
Livewire::actingAs($user)
->test(EditTenant::class, ['record' => $tenant->getRouteKey()])
->assertActionVisible('related_onboarding')
->assertActionHidden('archive')
->assertActionHidden('restore');
});
it('keeps active lifecycle actions consistent across list, view, and edit surfaces', function (): void {
$tenant = Tenant::factory()->active()->create();
[$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
Livewire::actingAs($user)
->test(ListTenants::class)
->assertTableActionVisible('archive', $tenant)
->assertTableActionHidden('restore', $tenant)
->assertTableActionHidden('related_onboarding', $tenant);
Filament::setTenant(null, true);
Livewire::actingAs($user)
->test(ViewTenant::class, ['record' => $tenant->getRouteKey()])
->assertActionVisible('archive')
->assertActionHidden('restore')
->assertActionHidden('related_onboarding');
Livewire::actingAs($user)
->test(EditTenant::class, ['record' => $tenant->getRouteKey()])
->assertActionVisible('archive')
->assertActionHidden('restore')
->assertActionHidden('related_onboarding');
});
it('keeps draft lifecycle actions consistent across list, view, and edit surfaces', function (): void {
$tenant = Tenant::factory()->draft()->create();
[$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'started_by' => $user,
'updated_by' => $user,
'state' => [
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
],
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
Livewire::actingAs($user)
->test(ListTenants::class)
->assertTableActionVisible('related_onboarding', $tenant)
->assertTableActionHidden('archive', $tenant)
->assertTableActionHidden('restore', $tenant);
Filament::setTenant(null, true);
Livewire::actingAs($user)
->test(ViewTenant::class, ['record' => $tenant->getRouteKey()])
->assertActionVisible('related_onboarding')
->assertActionHidden('archive')
->assertActionHidden('restore');
Livewire::actingAs($user)
->test(EditTenant::class, ['record' => $tenant->getRouteKey()])
->assertActionVisible('related_onboarding')
->assertActionHidden('archive')
->assertActionHidden('restore');
});
it('keeps archived lifecycle actions consistent across list, view, and edit surfaces', function (): void {
$tenant = Tenant::factory()->archived()->create();
[$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
Livewire::actingAs($user)
->test(ListTenants::class)
->assertTableActionVisible('restore', $tenant)
->assertTableActionHidden('archive', $tenant);
Filament::setTenant(null, true);
Livewire::actingAs($user)
->test(ViewTenant::class, ['record' => $tenant->getRouteKey()])
->assertActionVisible('restore')
->assertActionHidden('archive');
Livewire::actingAs($user)
->test(EditTenant::class, ['record' => $tenant->getRouteKey()])
->assertActionVisible('restore')
->assertActionHidden('archive');
});
it('keeps tenant global search aligned with administrative discoverability across lifecycles', function (): void {
$active = Tenant::factory()->active()->create(['name' => 'Surface Search Active']);
[$user, $active] = createUserWithTenant(tenant: $active, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
$draft = Tenant::factory()->draft()->create([
'workspace_id' => (int) $active->workspace_id,
'name' => 'Surface Search Draft',
]);
$onboarding = Tenant::factory()->onboarding()->create([
'workspace_id' => (int) $active->workspace_id,
'name' => 'Surface Search Onboarding',
]);
$archived = Tenant::factory()->archived()->create([
'workspace_id' => (int) $active->workspace_id,
'name' => 'Surface Search Archived',
]);
createUserWithTenant(tenant: $draft, user: $user, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
createUserWithTenant(tenant: $onboarding, user: $user, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
createUserWithTenant(tenant: $archived, user: $user, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
$this->actingAs($user);
Filament::setCurrentPanel('admin');
Filament::setTenant(null, true);
Filament::bootCurrentPanel();
session()->put(WorkspaceContext::SESSION_KEY, (int) $active->workspace_id);
expect(tenantActionSurfaceSearchTitles(TenantResource::getGlobalSearchResults('Surface Search')))
->toEqualCanonicalizing([
'Surface Search Active',
'Surface Search Draft',
'Surface Search Onboarding',
'Surface Search Archived',
]);
});
it('keeps list-row lifecycle actions independent from the selected header tenant context', function (): void {
$selectedTenant = Tenant::factory()->active()->create(['name' => 'Selected Header Tenant']);
[$user, $selectedTenant] = createUserWithTenant(tenant: $selectedTenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
$onboardingTenant = Tenant::factory()->onboarding()->create([
'workspace_id' => (int) $selectedTenant->workspace_id,
'name' => 'Independent Onboarding Tenant',
]);
$archivedTenant = Tenant::factory()->archived()->create([
'workspace_id' => (int) $selectedTenant->workspace_id,
'name' => 'Independent Archived Tenant',
]);
createUserWithTenant(tenant: $onboardingTenant, user: $user, role: 'owner', workspaceRole: 'owner', ensureDefaultMicrosoftProviderConnection: false);
createUserWithTenant(tenant: $archivedTenant, user: $user, role: 'owner', workspaceRole: 'owner', ensureDefaultMicrosoftProviderConnection: false);
createOnboardingDraft([
'workspace' => $onboardingTenant->workspace,
'tenant' => $onboardingTenant,
'started_by' => $user,
'updated_by' => $user,
'state' => [
'entra_tenant_id' => (string) $onboardingTenant->tenant_id,
'tenant_name' => (string) $onboardingTenant->name,
],
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $selectedTenant->workspace_id);
Filament::setTenant($selectedTenant, true);
Livewire::actingAs($user)
->test(ListTenants::class)
->assertTableActionVisible('archive', $selectedTenant)
->assertTableActionVisible('related_onboarding', $onboardingTenant)
->assertTableActionHidden('archive', $onboardingTenant)
->assertTableActionVisible('restore', $archivedTenant)
->assertTableActionHidden('archive', $archivedTenant);
});
it('documents the tenant row-click and More-menu contract for lifecycle actions', function (): void {
$tenant = Tenant::factory()->active()->create();
[$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'started_by' => $user,
'updated_by' => $user,
'status' => 'completed',
'state' => [
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
],
]);
$declaration = TenantResource::actionSurfaceDeclaration();
expect($declaration->listRowPrimaryActionLimit())->toBe(1)
->and((string) ($declaration->slot(ActionSurfaceSlot::ListRowMoreMenu)?->details ?? ''))->toContain('one');
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
Filament::setTenant(null, true);
$component = Livewire::actingAs($user)
->test(ListTenants::class)
->assertTableActionVisible('archive', $tenant)
->assertTableActionHidden('related_onboarding', $tenant)
->assertTableActionVisible('related_onboarding_overflow', $tenant)
->assertTableActionHidden('restore', $tenant);
$table = $component->instance()->getTable();
$rowActions = $table->getActions();
$primaryRowActionNames = collect($rowActions)
->reject(static fn ($action): bool => $action instanceof ActionGroup)
->map(static fn ($action): ?string => $action->getName())
->filter()
->values()
->all();
$moreGroup = collect($rowActions)->first(static fn ($action): bool => $action instanceof ActionGroup);
$moreActionNames = collect($moreGroup?->getActions() ?? [])
->map(static fn ($action): ?string => $action->getName())
->filter()
->values()
->all();
$relatedOnboardingIndex = array_search('related_onboarding_overflow', $moreActionNames, true);
$archiveIndex = array_search('archive', $moreActionNames, true);
expect($primaryRowActionNames)->not->toContain('view')
->and($table->getRecordUrl($tenant))->toBe(TenantResource::getUrl('view', ['record' => $tenant]))
->and($moreActionNames)->toContain('archive')
->and($moreActionNames)->toContain('related_onboarding_overflow')
->and($relatedOnboardingIndex)->toBe(0)
->and($archiveIndex)->toBe(count($moreActionNames) - 1);
});