TenantAtlas/tests/Feature/Filament/ManagedTenantsLandingLifecycleTest.php
ahmido 73a879d061 feat: implement spec 147 tenant context enforcement (#176)
## Summary
- implement Spec 147 for workspace-first tenant selector and remembered tenant context enforcement
- harden canonical and tenant-bound route behavior so selected tenant mismatch stays informational
- fix drift finding subject fallback for workspace-safe RBAC identifiers and centralize finding subject resolution

## Testing
- vendor/bin/sail artisan test --compact tests/Feature/Filament/FindingViewRbacEvidenceTest.php tests/Feature/Findings/FindingsListDefaultsTest.php
- vendor/bin/sail bin pint --dirty --format agent

## Notes
- branch pushed at de0679cd8b
- includes the spec artifacts under specs/147-tenant-selector-remembered-context-enforcement/

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #176
2026-03-16 22:52:58 +00:00

89 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Tenant;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('shows onboarding and archived tenants on the managed-tenants landing with lifecycle labels', function (): void {
$workspace = Workspace::factory()->create(['slug' => 'lifecycle-ws']);
$user = User::factory()->create();
WorkspaceMembership::factory()->create([
'workspace_id' => (int) $workspace->getKey(),
'user_id' => (int) $user->getKey(),
'role' => 'owner',
]);
$active = Tenant::factory()->active()->create([
'workspace_id' => (int) $workspace->getKey(),
'name' => 'Active Tenant',
]);
$onboarding = Tenant::factory()->onboarding()->create([
'workspace_id' => (int) $workspace->getKey(),
'name' => 'Onboarding Tenant',
]);
$archived = Tenant::factory()->archived()->create([
'workspace_id' => (int) $workspace->getKey(),
'name' => 'Archived Tenant',
]);
$outsider = Tenant::factory()->active()->create(['name' => 'Other Workspace Tenant']);
$user->tenants()->syncWithoutDetaching([
$active->getKey() => ['role' => 'owner'],
$onboarding->getKey() => ['role' => 'owner'],
$archived->getKey() => ['role' => 'owner'],
$outsider->getKey() => ['role' => 'owner'],
]);
$this->actingAs($user)
->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
->get(route('admin.workspace.managed-tenants.index', ['workspace' => $workspace]))
->assertSuccessful()
->assertSee('Active Tenant')
->assertSee('Onboarding Tenant')
->assertSee('Archived Tenant')
->assertSee('Active')
->assertSee('Onboarding')
->assertSee('Archived')
->assertDontSee('Other Workspace Tenant');
});
it('keeps managed tenants discoverable with no selected tenant context', function (): void {
$workspace = Workspace::factory()->create(['slug' => 'discoverable-managed-tenants']);
$user = User::factory()->create();
WorkspaceMembership::factory()->create([
'workspace_id' => (int) $workspace->getKey(),
'user_id' => (int) $user->getKey(),
'role' => 'owner',
]);
$onboardingTenant = Tenant::factory()->onboarding()->create([
'workspace_id' => (int) $workspace->getKey(),
'name' => 'Discoverable Onboarding Tenant',
]);
$archivedTenant = Tenant::factory()->archived()->create([
'workspace_id' => (int) $workspace->getKey(),
'name' => 'Discoverable Archived Tenant',
]);
$user->tenants()->syncWithoutDetaching([
$onboardingTenant->getKey() => ['role' => 'owner'],
$archivedTenant->getKey() => ['role' => 'owner'],
]);
$this->actingAs($user)
->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
->get(route('admin.workspace.managed-tenants.index', ['workspace' => $workspace]))
->assertSuccessful()
->assertSee('Discoverable Onboarding Tenant')
->assertSee('Discoverable Archived Tenant');
});