TenantAtlas/apps/platform/tests/Feature/Filament/WorkspaceContextTopbarAndTenantSelectionTest.php
ahmido e0c2cdb1f4 feat: enforce workspace and environment scope contract (Spec 338) (#409)
## Summary
- enforce the canonical workspace/environment scope contract for workspace hubs and environment-owned surfaces
- replace first-party Operations deep links that leaked Filament `tableFilters[...]` internals with stable product-level query behavior
- add the sidebar scope indicator and split environment-page navigation into explicit `Workspace-wide` and `Workspace admin` groups
- remove redundant tenantless `All environments` scope badges from workspace-wide pages while preserving explicit environment filter affordances
- include the Spec 338 artifacts, guard tests, and browser smoke coverage for the new contract

## Validation
- `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Feature/Navigation/Spec338EnvironmentSidebarSeparationTest.php tests/Feature/Navigation/Spec338OperationRunLinksQueryContractTest.php tests/Feature/Navigation/Spec338SidebarScopeIndicatorTest.php tests/Feature/Filament/PanelNavigationSegregationTest.php`
- `cd apps/platform && ./vendor/bin/sail php vendor/bin/pest tests/Browser/Spec338ScopeContractSmokeTest.php --compact`

## Notes
- Livewire v4 compliance unchanged
- Filament provider registration remains in `bootstrap/providers.php`
- no destructive action behavior changed
- no migrations, env var changes, or new Filament asset registration

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #409
2026-05-31 01:36:08 +00:00

104 lines
3.8 KiB
PHP

<?php
use App\Models\ManagedEnvironment;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\ManagedEnvironmentLinks;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('default Filament tenant selection respects current workspace context', function () {
$user = User::factory()->create();
$workspaceA = Workspace::factory()->create(['name' => 'Workspace A']);
$workspaceB = Workspace::factory()->create(['name' => 'Workspace B']);
WorkspaceMembership::factory()->create([
'workspace_id' => $workspaceA->getKey(),
'user_id' => $user->getKey(),
'role' => 'owner',
]);
WorkspaceMembership::factory()->create([
'workspace_id' => $workspaceB->getKey(),
'user_id' => $user->getKey(),
'role' => 'owner',
]);
$tenantA = ManagedEnvironment::factory()->create([
'workspace_id' => $workspaceA->getKey(),
'status' => 'active',
]);
$tenantB = ManagedEnvironment::factory()->create([
'workspace_id' => $workspaceB->getKey(),
'status' => 'active',
]);
$user->tenants()->syncWithoutDetaching([
$tenantA->getKey() => ['role' => 'owner'],
$tenantB->getKey() => ['role' => 'owner'],
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $workspaceA->getKey());
$user->forceFill(['last_workspace_id' => (int) $workspaceA->getKey()])->save();
$this->actingAs($user)
->get(route('filament.admin.pages.choose-environment'))
->assertOk()
->assertSee($tenantA->name)
->assertDontSee($tenantB->name);
});
test('user menu does not render a workspace switcher (topbar context bar is the single entry point)', function () {
[$user, $tenant] = createUserWithTenant();
$workspace = Workspace::query()->whereKey($tenant->workspace_id)->firstOrFail();
$this->actingAs($user)
->get(ManagedEnvironmentLinks::indexUrl($tenant))
->assertOk()
->assertSee($workspace->name)
->assertDontSee('name="workspace_id"', escape: false);
});
test('workspace-wide operations keep shell scope tenantless when a valid tenant query filter is present', function () {
$rememberedEnvironment = ManagedEnvironment::factory()->create([
'workspace_id' => null,
'status' => 'active',
'name' => 'Remembered Topbar ManagedEnvironment',
]);
[$user, $rememberedEnvironment] = createUserWithTenant(tenant: $rememberedEnvironment, role: 'owner');
$hintedTenant = ManagedEnvironment::factory()->create([
'workspace_id' => (int) $rememberedEnvironment->workspace_id,
'status' => 'active',
'name' => 'Hinted Topbar ManagedEnvironment',
]);
createUserWithTenant(tenant: $hintedTenant, user: $user, role: 'owner');
Filament::setTenant(null, true);
$workspaceId = (int) $rememberedEnvironment->workspace_id;
$this->actingAs($user)
->withSession([
WorkspaceContext::SESSION_KEY => $workspaceId,
WorkspaceContext::LAST_ENVIRONMENT_IDS_SESSION_KEY => [
(string) $workspaceId => (int) $rememberedEnvironment->getKey(),
],
])
->get(route('admin.operations.index', ['workspace' => $workspaceId, 'environment_id' => (int) $hintedTenant->getKey()]))
->assertOk()
->assertSee(__('localization.shell.choose_environment'))
->assertDontSee(__('localization.shell.no_environment_selected'))
->assertDontSee(__('localization.shell.all_environments'))
->assertDontSee(__('localization.shell.environment_scope').': Hinted Topbar ManagedEnvironment')
->assertDontSee(__('localization.shell.environment_scope').': Remembered Topbar ManagedEnvironment');
});