TenantAtlas/tests/Feature/Filament/CreateCtaPlacementTest.php
ahmido 440e63edff feat: implement tenant action taxonomy lifecycle visibility (#174)
## Summary

Implements Spec 145 for tenant action taxonomy and lifecycle-safe visibility.

This PR:
- adds a central tenant action policy surface and supporting value objects
- aligns tenant list, detail, edit, onboarding, and widget surfaces around lifecycle-safe actions
- standardizes operator-facing lifecycle wording around View, Resume onboarding, Archive, Restore, and Complete onboarding
- tightens onboarding and tenant lifecycle authorization semantics, including honest 404 vs 403 behavior
- updates related regression coverage and spec artifacts for Spec 145
- fixes follow-on full-suite regressions uncovered during validation, including onboarding browser flows, provider consent fixtures, workspace redirect DI expectations, and critical table/action/UI expectation drift

## Validation

Executed and passed:
- vendor/bin/sail bin pint --dirty --format agent
- vendor/bin/sail artisan test --compact

Result:
- 2581 passed
- 8 skipped
- 13534 assertions

## Notes

- Base branch: dev
- Feature branch commit: a33a41b
- Filament v5 / Livewire v4 compliance preserved
- No panel provider registration changes; Laravel 12 provider registration remains in bootstrap/providers.php
- No new globally searchable resource behavior added in this slice
- Destructive lifecycle actions remain confirmation-gated and authorization-protected

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #174
2026-03-16 00:57:17 +00:00

419 lines
14 KiB
PHP

<?php
declare(strict_types=1);
use App\Filament\Resources\BackupScheduleResource\Pages\ListBackupSchedules;
use App\Filament\Resources\BackupSetResource\Pages\ListBackupSets;
use App\Filament\Resources\ProviderConnectionResource\Pages\ListProviderConnections;
use App\Filament\Resources\RestoreRunResource\Pages\ListRestoreRuns;
use App\Filament\Resources\TenantResource\Pages\ListTenants;
use App\Filament\Resources\Workspaces\Pages\ListWorkspaces;
use App\Models\BackupSchedule;
use App\Models\BackupSet;
use App\Models\ProviderConnection;
use App\Models\RestoreRun;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
uses(RefreshDatabase::class);
function getHeaderAction(Testable $component, string $name): ?Action
{
$instance = $component->instance();
$instance->cacheInteractsWithHeaderActions();
foreach ($instance->getCachedHeaderActions() as $action) {
if ($action instanceof Action && $action->getName() === $name) {
return $action;
}
}
return null;
}
function getPlacementEmptyStateAction(Testable $component, string $name): ?Action
{
foreach ($component->instance()->getTable()->getEmptyStateActions() as $action) {
if ($action instanceof Action && $action->getName() === $name) {
return $action;
}
}
return null;
}
it('shows create only in empty state when workspaces table is empty', function (): void {
$workspace = Workspace::factory()->create([
'archived_at' => now(),
]);
$user = User::factory()->create();
WorkspaceMembership::factory()->create([
'workspace_id' => (int) $workspace->getKey(),
'user_id' => (int) $user->getKey(),
'role' => 'owner',
]);
$user->forceFill([
'last_workspace_id' => (int) $workspace->getKey(),
])->save();
$this->actingAs($user)
->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);
$component = Livewire::test(ListWorkspaces::class)
->assertTableEmptyStateActionsExistInOrder(['create']);
expect($component->instance()->getTable()->getEmptyStateActions())->toHaveCount(1);
$emptyStateCreate = getPlacementEmptyStateAction($component, 'create');
expect($emptyStateCreate)->not->toBeNull();
expect($emptyStateCreate?->getLabel())->toBe('New workspace');
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeFalse();
});
it('shows create only in header when workspaces table is not empty', function (): void {
$workspace = Workspace::factory()->create([
'archived_at' => null,
]);
$user = User::factory()->create();
WorkspaceMembership::factory()->create([
'workspace_id' => (int) $workspace->getKey(),
'user_id' => (int) $user->getKey(),
'role' => 'owner',
]);
$user->forceFill([
'last_workspace_id' => (int) $workspace->getKey(),
])->save();
$this->actingAs($user)
->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);
$component = Livewire::test(ListWorkspaces::class)
->assertCountTableRecords(1);
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeTrue();
});
it('shows create only in empty state when backup schedules table is empty', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
$this->actingAs($user);
$tenant->makeCurrent();
Filament::setTenant($tenant, true);
$component = Livewire::test(ListBackupSchedules::class)
->assertTableEmptyStateActionsExistInOrder(['create']);
expect($component->instance()->getTable()->getEmptyStateActions())->toHaveCount(1);
$emptyStateCreate = getPlacementEmptyStateAction($component, 'create');
expect($emptyStateCreate)->not->toBeNull();
expect($emptyStateCreate?->getLabel())->toBe('New backup schedule');
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeFalse();
});
it('shows create only in header when backup schedules table is not empty', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
$this->actingAs($user);
$tenant->makeCurrent();
Filament::setTenant($tenant, true);
BackupSchedule::query()->create([
'tenant_id' => (int) $tenant->getKey(),
'name' => 'Daily schedule',
'is_enabled' => true,
'timezone' => 'UTC',
'frequency' => 'daily',
'time_of_day' => '00:00:00',
'days_of_week' => null,
'policy_types' => ['device_config'],
'include_foundations' => true,
'retention_keep_last' => 30,
]);
$component = Livewire::test(ListBackupSchedules::class)
->assertCountTableRecords(1);
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeTrue();
});
it('shows create only in empty state when restore runs table is empty', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
$this->actingAs($user);
$tenant->makeCurrent();
Filament::setTenant($tenant, true);
$component = Livewire::test(ListRestoreRuns::class)
->assertTableEmptyStateActionsExistInOrder(['create']);
expect($component->instance()->getTable()->getEmptyStateActions())->toHaveCount(1);
$emptyStateCreate = getPlacementEmptyStateAction($component, 'create');
expect($emptyStateCreate)->not->toBeNull();
expect($emptyStateCreate?->getLabel())->toBe('New restore run');
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeFalse();
});
it('shows create only in header when restore runs table is not empty', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
$this->actingAs($user);
$tenant->makeCurrent();
Filament::setTenant($tenant, true);
RestoreRun::factory()->create([
'tenant_id' => (int) $tenant->getKey(),
]);
$component = Livewire::test(ListRestoreRuns::class)
->assertCountTableRecords(1);
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeTrue();
});
it('shows create only in empty state when backup sets table is empty', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
$this->actingAs($user);
$tenant->makeCurrent();
Filament::setTenant($tenant, true);
$component = Livewire::test(ListBackupSets::class)
->assertTableEmptyStateActionsExistInOrder(['create']);
expect($component->instance()->getTable()->getEmptyStateActions())->toHaveCount(1);
$emptyStateCreate = getPlacementEmptyStateAction($component, 'create');
expect($emptyStateCreate)->not->toBeNull();
expect($emptyStateCreate?->getLabel())->toBe('Create backup set');
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeFalse();
});
it('shows create only in header when backup sets table is not empty', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
$this->actingAs($user);
$tenant->makeCurrent();
Filament::setTenant($tenant, true);
BackupSet::factory()->create([
'tenant_id' => (int) $tenant->getKey(),
]);
$component = Livewire::test(ListBackupSets::class)
->assertCountTableRecords(1);
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeTrue();
});
it('shows create only in empty state when tenants table is empty', function (): void {
$workspace = Workspace::factory()->create([
'archived_at' => now(),
]);
$user = User::factory()->create();
WorkspaceMembership::factory()->create([
'workspace_id' => (int) $workspace->getKey(),
'user_id' => (int) $user->getKey(),
'role' => 'owner',
]);
$user->forceFill([
'last_workspace_id' => (int) $workspace->getKey(),
])->save();
$this->actingAs($user)
->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);
$component = Livewire::test(ListTenants::class)
->assertTableEmptyStateActionsExistInOrder(['add_tenant']);
$emptyStateCreate = getPlacementEmptyStateAction($component, 'add_tenant');
expect($emptyStateCreate)->not->toBeNull();
expect($emptyStateCreate?->getLabel())->toBe('Add tenant');
$headerCreate = getHeaderAction($component, 'add_tenant');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeFalse();
});
it('shows create only in header when tenants table is not empty', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
$this->actingAs($user);
$component = Livewire::test(ListTenants::class)
->assertCountTableRecords(1);
$headerCreate = getHeaderAction($component, 'add_tenant');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeTrue();
expect($headerCreate?->getLabel())->toBe('Add tenant');
});
it('labels the empty-state tenant action as resume onboarding when one draft exists', function (): void {
$workspace = Workspace::factory()->create([
'archived_at' => now(),
]);
$user = User::factory()->create();
WorkspaceMembership::factory()->create([
'workspace_id' => (int) $workspace->getKey(),
'user_id' => (int) $user->getKey(),
'role' => 'owner',
]);
createOnboardingDraft([
'workspace' => $workspace,
'started_by' => $user,
'updated_by' => $user,
'state' => [
'entra_tenant_id' => 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
'tenant_name' => 'Onboarding Tenant',
],
]);
$user->forceFill([
'last_workspace_id' => (int) $workspace->getKey(),
])->save();
$this->actingAs($user)
->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);
$component = Livewire::test(ListTenants::class)
->assertTableEmptyStateActionsExistInOrder(['add_tenant']);
$emptyStateCreate = getPlacementEmptyStateAction($component, 'add_tenant');
expect($emptyStateCreate)->not->toBeNull();
expect($emptyStateCreate?->getLabel())->toBe('Resume onboarding');
});
it('labels the tenant header action as resume onboarding when one draft exists', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
createOnboardingDraft([
'workspace' => $tenant->workspace,
'started_by' => $user,
'updated_by' => $user,
'state' => [
'entra_tenant_id' => 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb',
'tenant_name' => 'Resumable Draft',
],
]);
$this->actingAs($user);
$component = Livewire::test(ListTenants::class)
->assertCountTableRecords(1);
$headerCreate = getHeaderAction($component, 'add_tenant');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeTrue();
expect($headerCreate?->getLabel())->toBe('Resume onboarding');
});
it('labels the tenant header action as choose onboarding draft when multiple drafts exist', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
createOnboardingDraft([
'workspace' => $tenant->workspace,
'started_by' => $user,
'updated_by' => $user,
'state' => [
'entra_tenant_id' => 'cccccccc-cccc-cccc-cccc-cccccccccccc',
'tenant_name' => 'Draft One',
],
]);
createOnboardingDraft([
'workspace' => $tenant->workspace,
'started_by' => $user,
'updated_by' => $user,
'state' => [
'entra_tenant_id' => 'dddddddd-dddd-dddd-dddd-dddddddddddd',
'tenant_name' => 'Draft Two',
],
]);
$this->actingAs($user);
$component = Livewire::test(ListTenants::class)
->assertCountTableRecords(1);
$headerCreate = getHeaderAction($component, 'add_tenant');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeTrue();
expect($headerCreate?->getLabel())->toBe('Choose onboarding draft');
});
it('shows create only in empty state when provider connections table is empty', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
$this->actingAs($user);
$tenant->makeCurrent();
Filament::setTenant($tenant, true);
$component = Livewire::test(ListProviderConnections::class)
->assertCountTableRecords(0)
->assertTableEmptyStateActionsExistInOrder(['create']);
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeFalse();
});
it('shows create only in header when provider connections table is not empty', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
$this->actingAs($user);
$tenant->makeCurrent();
Filament::setTenant($tenant, true);
ProviderConnection::factory()->create([
'tenant_id' => (int) $tenant->getKey(),
'workspace_id' => (int) $tenant->workspace_id,
]);
$component = Livewire::test(ListProviderConnections::class);
$headerCreate = getHeaderAction($component, 'create');
expect($headerCreate)->not->toBeNull();
expect($headerCreate?->isVisible())->toBeTrue();
});