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

57 lines
1.7 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;
it('scopes TenantResource global search query to the current workspace', function () {
$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([
'name' => 'Tenant Alpha',
'workspace_id' => $workspaceA->getKey(),
'is_current' => true,
]);
$tenantB = Tenant::factory()->create([
'name' => 'Tenant Gamma',
'workspace_id' => $workspaceB->getKey(),
'is_current' => false,
]);
$this->actingAs($user);
session([WorkspaceContext::SESSION_KEY => $workspaceA->getKey()]);
$tenantIds = TenantResource::getGlobalSearchEloquentQuery()->pluck('id')->all();
expect($tenantIds)
->toContain($tenantA->getKey())
->not()->toContain($tenantB->getKey());
});
it('returns no global search results when no workspace is selected', function () {
$user = User::factory()->create();
$workspace = Workspace::factory()->create();
WorkspaceMembership::factory()->for($workspace)->for($user)->create(['role' => 'owner']);
Tenant::factory()->create(['workspace_id' => $workspace->getKey()]);
$this->actingAs($user);
$tenantIds = TenantResource::getGlobalSearchEloquentQuery()->pluck('id')->all();
expect($tenantIds)->toBeEmpty();
});