TenantAtlas/tests/Feature/Rbac/TenantActionSurfaceConsistencyTest.php
2026-03-17 12:47:16 +01:00

259 lines
10 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\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 and preserves the tenant row overflow 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(2)
->and((string) ($declaration->slot(ActionSurfaceSlot::ListRowMoreMenu)?->details ?? ''))->toContain('two');
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
Filament::setTenant(null, true);
Livewire::actingAs($user)
->test(ListTenants::class)
->assertTableActionVisible('view', $tenant)
->assertTableActionVisible('archive', $tenant)
->assertTableActionHidden('related_onboarding', $tenant)
->assertTableActionVisible('related_onboarding_overflow', $tenant)
->assertTableActionHidden('restore', $tenant);
});