TenantAtlas/tests/Feature/Workspaces/ManagedTenantScopingTest.php
2026-02-01 12:19:57 +01:00

61 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
use App\Filament\Resources\TenantResource;
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('scopes managed tenant listing to the current workspace', function (): void {
$user = User::factory()->create();
$workspaceA = Workspace::factory()->create();
$workspaceB = Workspace::factory()->create();
WorkspaceMembership::factory()->for($workspaceA)->for($user)->create(['role' => 'owner']);
WorkspaceMembership::factory()->for($workspaceB)->for($user)->create(['role' => 'owner']);
$tenantA = Tenant::factory()->create([
'workspace_id' => $workspaceA->getKey(),
'name' => 'Tenant Alpha',
'is_current' => true,
]);
$tenantB = Tenant::factory()->create([
'workspace_id' => $workspaceB->getKey(),
'name' => 'Tenant Beta',
'is_current' => false,
]);
$this->actingAs($user);
session([WorkspaceContext::SESSION_KEY => $workspaceA->getKey()]);
$tenantIds = TenantResource::getEloquentQuery()->pluck('id')->all();
expect($tenantIds)
->toContain($tenantA->getKey())
->not()->toContain($tenantB->getKey());
});
it('returns no managed tenants when no workspace is selected', function (): void {
$user = User::factory()->create();
$workspace = Workspace::factory()->create();
$otherWorkspace = Workspace::factory()->create();
WorkspaceMembership::factory()->for($workspace)->for($user)->create(['role' => 'owner']);
WorkspaceMembership::factory()->for($otherWorkspace)->for($user)->create(['role' => 'owner']);
Tenant::factory()->create(['workspace_id' => $workspace->getKey()]);
$this->actingAs($user);
$tenantIds = TenantResource::getEloquentQuery()->pluck('id')->all();
expect($tenantIds)->toBeEmpty();
});