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

63 lines
2.2 KiB
PHP

<?php
use App\Filament\Pages\ChooseWorkspace;
use App\Filament\Pages\NoAccess;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\Workspaces\WorkspaceContext;
use Livewire\Livewire;
it('allows creating the first workspace from the no-access page', function () {
$user = User::factory()->create();
$this->actingAs($user);
Livewire::test(NoAccess::class)
->mountAction('createWorkspace')
->set('mountedActions.0.data.name', 'Acme')
->set('mountedActions.0.data.slug', 'acme')
->callMountedAction()
->assertHasNoActionErrors();
$workspace = Workspace::query()->where('slug', 'acme')->firstOrFail();
expect(WorkspaceMembership::query()
->where('workspace_id', $workspace->getKey())
->where('user_id', $user->getKey())
->where('role', 'owner')
->exists())->toBeTrue();
expect(session()->get(WorkspaceContext::SESSION_KEY))->toBe($workspace->getKey());
expect($user->fresh()->last_workspace_id)->toBe($workspace->getKey());
});
it('allows creating a new workspace from the choose-workspace page', 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']);
$this->actingAs($user);
Livewire::test(ChooseWorkspace::class)
->mountAction('createWorkspace')
->set('mountedActions.0.data.name', 'New Workspace')
->set('mountedActions.0.data.slug', 'new-workspace')
->callMountedAction()
->assertHasNoActionErrors();
$workspace = Workspace::query()->where('slug', 'new-workspace')->firstOrFail();
expect(WorkspaceMembership::query()
->where('workspace_id', $workspace->getKey())
->where('user_id', $user->getKey())
->where('role', 'owner')
->exists())->toBeTrue();
expect(session()->get(WorkspaceContext::SESSION_KEY))->toBe($workspace->getKey());
expect($user->fresh()->last_workspace_id)->toBe($workspace->getKey());
});