TenantAtlas/tests/Feature/Auth/TenantChooserSelectionTest.php
ahmido 641bb4afde feat: implement tenant lifecycle operability semantics (#172)
## Summary
- implement Spec 143 tenant lifecycle, operability, and tenant-context semantics across chooser, tenant management, onboarding, and canonical operation viewers
- add centralized tenant lifecycle and operability support types, audit action coverage, and lifecycle-aware badge and action handling
- add feature and unit coverage for tenant chooser eligibility, global search scoping, canonical operation access, onboarding authorization, and lifecycle presentation

## Testing
- vendor/bin/sail artisan test --compact
- vendor/bin/sail bin pint --dirty --format agent

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #172
2026-03-15 09:08:36 +00:00

84 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
use App\Filament\Pages\ChooseTenant;
use App\Filament\Pages\TenantDashboard;
use App\Models\Tenant;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Livewire\Livewire;
uses(RefreshDatabase::class);
it('shows only active tenants in the standard chooser and persists the last-used tenant', function (): void {
$activeTenant = Tenant::factory()->active()->create(['name' => 'Active Tenant']);
[$user, $activeTenant] = createUserWithTenant(tenant: $activeTenant, role: 'owner');
$this->actingAs($user);
$otherActiveTenant = Tenant::factory()->active()->create([
'workspace_id' => (int) $activeTenant->workspace_id,
'name' => 'Other Active Tenant',
]);
$onboardingTenant = Tenant::factory()->onboarding()->create([
'workspace_id' => (int) $activeTenant->workspace_id,
'name' => 'Onboarding Tenant',
]);
$draftTenant = Tenant::factory()->draft()->create([
'workspace_id' => (int) $activeTenant->workspace_id,
'name' => 'Draft Tenant',
]);
$archivedTenant = Tenant::factory()->archived()->create([
'workspace_id' => (int) $activeTenant->workspace_id,
'name' => 'Archived Tenant',
]);
createUserWithTenant(tenant: $otherActiveTenant, user: $user, role: 'owner');
createUserWithTenant(tenant: $onboardingTenant, user: $user, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
createUserWithTenant(tenant: $draftTenant, user: $user, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
createUserWithTenant(tenant: $archivedTenant, user: $user, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
session()->put(WorkspaceContext::SESSION_KEY, (int) $activeTenant->workspace_id);
$this->get('/admin/choose-tenant')
->assertSuccessful()
->assertSee('Active Tenant')
->assertSee('Other Active Tenant')
->assertDontSee('Onboarding Tenant')
->assertDontSee('Draft Tenant')
->assertDontSee('Archived Tenant');
Livewire::test(ChooseTenant::class)
->call('selectTenant', (int) $activeTenant->getKey())
->assertRedirect(TenantDashboard::getUrl(tenant: $activeTenant));
$user->refresh();
if (Schema::hasColumn('users', 'last_tenant_id')) {
expect($user->last_tenant_id)->toBe($activeTenant->getKey());
return;
}
if (Schema::hasTable('user_tenant_preferences')) {
$preference = $user->tenantPreferences()
->where('tenant_id', $activeTenant->getKey())
->first();
expect($preference)->not->toBeNull();
expect($preference?->last_used_at)->not->toBeNull();
}
});
it('returns 404 when a non-operable tenant is selected through the chooser endpoint', function (): void {
$tenant = Tenant::factory()->onboarding()->create();
[$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner');
$this->actingAs($user)
->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id])
->post(route('admin.select-tenant'), ['tenant_id' => (int) $tenant->getKey()])
->assertNotFound();
});