TenantAtlas/tests/Feature/Rbac/AdminGlobalSearchContextSafetyTest.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

48 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Filament\Resources\OperationRunResource;
use App\Filament\Resources\TenantResource;
use App\Models\Tenant;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
function adminGlobalSearchTitles($results): array
{
return collect($results)->map(fn ($result): string => (string) $result->title)->all();
}
it('does not leak non-selectable tenant results from remembered tenant context', function (): void {
$activeTenant = Tenant::factory()->active()->create(['name' => 'Search Safety Active']);
[$user, $activeTenant] = createUserWithTenant(tenant: $activeTenant, role: 'owner');
$onboardingTenant = Tenant::factory()->onboarding()->create([
'workspace_id' => (int) $activeTenant->workspace_id,
'name' => 'Search Safety Onboarding',
]);
createUserWithTenant(tenant: $onboardingTenant, user: $user, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
$this->actingAs($user);
Filament::setCurrentPanel('admin');
Filament::setTenant(null, true);
Filament::bootCurrentPanel();
session()->put(WorkspaceContext::SESSION_KEY, (int) $activeTenant->workspace_id);
session()->put(WorkspaceContext::LAST_TENANT_IDS_SESSION_KEY, [
(string) $activeTenant->workspace_id => (int) $onboardingTenant->getKey(),
]);
expect(adminGlobalSearchTitles(TenantResource::getGlobalSearchResults('Search Safety')))
->toBe(['Search Safety Active']);
});
it('keeps operation runs out of admin global search regardless of remembered context state', function (): void {
expect(OperationRunResource::canGloballySearch())->toBeFalse();
});