This commit is contained in:
MeghdadFadaee committed 2026-09-09 16:33:18 +03:30
commit aafddfb146
64 files changed
+17178

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
<?php
use Illuminate\Http\Request;
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
use MeghdadFadaee\NovaTenancy\CurrentTenant;
use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected;
use MeghdadFadaee\NovaTenancy\NovaTenancy;
use MeghdadFadaee\NovaTenancy\Providers\UserTenantProvider;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\User;
it('resolves only an accessible tenant from the cookie', function (): void {
$user = User::query()->create(['name' => 'Ada']);
$otherUser = User::query()->create(['name' => 'Grace']);
$tenant = Tenant::query()->create(['user_id' => $user->id, 'title' => 'North']);
$inaccessible = Tenant::query()->create(['user_id' => $otherUser->id, 'title' => 'South']);
$request = Request::create('/');
$request->setUserResolver(fn () => $user);
$request->cookies->set('nova_tenant', (string) $tenant->id);
$context = new CurrentTenant(new UserTenantProvider, $request);
expect($context->id())->toBe($tenant->id)
->and($context->model()?->is($tenant))->toBeTrue();
$request = Request::create('/');
$request->setUserResolver(fn () => $user);
$request->cookies->set('nova_tenant', (string) $inaccessible->id);
expect((new CurrentTenant(new UserTenantProvider, $request))->model())->toBeNull();
});
it('provides scoped string bindings and strict accessors', function (): void {
expect(app('CurrentTenantId'))->toBeNull()
->and(app('CurrentTenantModel'))->toBeNull()
->and(fn () => app(CurrentTenantContext::class)->requireModel())
->toThrow(TenantNotSelected::class);
});
it('temporarily activates a tenant and restores the previous context', function (): void {
$user = User::query()->create(['name' => 'Ada']);
$first = Tenant::query()->create(['user_id' => $user->id, 'title' => 'First']);
$second = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Second']);
$request = Request::create('/');
$request->setUserResolver(fn () => $user);
$context = new CurrentTenant(new UserTenantProvider, $request);
$context->activate($first);
$inside = $context->run($second, fn () => $context->id());
expect($inside)->toBe($second->id)
->and($context->id())->toBe($first->id);
});
it('builds an official nova user menu item without replacing the menu callback', function (): void {
$item = NovaTenancy::userMenuItem();
expect($item->path)->toBe('/nova-tenancy')
->and((string) $item->name)->toBe('Select tenant');
});
+91
View File
@@ -0,0 +1,91 @@
<?php
use Illuminate\Support\Facades\Event;
use MeghdadFadaee\NovaTenancy\Events\TenantCleared;
use MeghdadFadaee\NovaTenancy\Events\TenantSelected;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\User;
it('searches and paginates only the authenticated users tenants', function (): void {
$user = User::query()->create(['name' => 'Ada']);
$otherUser = User::query()->create(['name' => 'Grace']);
Tenant::query()->create(['user_id' => $user->id, 'title' => 'Alpha']);
Tenant::query()->create(['user_id' => $user->id, 'title' => 'Beta']);
Tenant::query()->create(['user_id' => $otherUser->id, 'title' => 'Alpha Secret']);
$this->actingAs($user)
->getJson('/testing/tenants?search=Alpha&perPage=1')
->assertOk()
->assertJsonPath('meta.total', 1)
->assertJsonPath('data.0.title', 'Alpha');
});
it('selects an accessible tenant and returns a safe spa destination', function (): void {
Event::fake();
$user = User::query()->create(['name' => 'Ada']);
$tenant = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Alpha']);
$this->actingAs($user)
->postJson('/testing/current', ['tenant' => $tenant->id])
->assertOk()
->assertJsonPath('current.key', $tenant->id)
->assertJsonPath('redirectTo', '/')
->assertCookie('nova_tenant', (string) $tenant->id);
Event::assertDispatched(TenantSelected::class, fn (TenantSelected $event): bool => $event->tenant->is($tenant));
});
it('does not reveal or select another users tenant', function (): void {
$user = User::query()->create(['name' => 'Ada']);
$otherUser = User::query()->create(['name' => 'Grace']);
$tenant = Tenant::query()->create(['user_id' => $otherUser->id, 'title' => 'Secret']);
$this->actingAs($user)
->postJson('/testing/current', ['tenant' => (string) $tenant->id])
->assertNotFound()
->assertCookieMissing('nova_tenant');
});
it('clears the selection and dispatches an event', function (): void {
Event::fake();
$user = User::query()->create(['name' => 'Ada']);
$tenant = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Alpha']);
$this->actingAs($user)
->withCookie('nova_tenant', (string) $tenant->id)
->deleteJson('/testing/current')
->assertOk()
->assertCookieExpired('nova_tenant');
Event::assertDispatched(TenantCleared::class);
});
it('redirects safe logos without caching them', function (): void {
$user = User::query()->create(['name' => 'Ada']);
$tenant = Tenant::query()->create([
'user_id' => $user->id,
'title' => 'Alpha',
'logo_url' => 'https://cdn.example.com/alpha.png',
]);
$this->actingAs($user)
->withCookie('nova_tenant', (string) $tenant->id)
->get('/testing/current/logo')
->assertRedirect('https://cdn.example.com/alpha.png')
->assertHeader('Cache-Control', 'max-age=0, no-store, private');
});
it('renders a fallback for unsafe logo schemes', function (): void {
$user = User::query()->create(['name' => 'Ada']);
$tenant = Tenant::query()->create([
'user_id' => $user->id,
'title' => 'Alpha',
'logo_url' => 'javascript:alert(1)',
]);
$this->actingAs($user)
->withCookie('nova_tenant', (string) $tenant->id)
->get('/testing/current/logo')
->assertOk()
->assertHeader('Content-Type', 'image/svg+xml; charset=UTF-8');
});
+27
View File
@@ -0,0 +1,27 @@
<?php
use MeghdadFadaee\NovaTenancy\Support\TenantPayload;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\User;
it('normalizes model presentation and rejects unsafe accent values', function (): void {
$user = User::query()->create(['name' => 'Ada']);
$tenant = Tenant::query()->create([
'user_id' => $user->id,
'title' => 'Alpha',
'description' => 'Primary workspace',
'logo_url' => 'javascript:alert(1)',
'accent' => 'red; background: url(evil)',
]);
expect(TenantPayload::fromModel($tenant))->toMatchArray([
'key' => $tenant->id,
'title' => 'Alpha',
'description' => 'Primary workspace',
'logoUrl' => null,
'accent' => null,
]);
$tenant->accent = '#7c3aed';
expect(TenantPayload::fromModel($tenant)['accent'])->toBe('#7c3aed');
});
+46
View File
@@ -0,0 +1,46 @@
<?php
use Laravel\Nova\Http\Requests\NovaRequest;
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\TenantRecord;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\TenantRecordResource;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\User;
it('scopes all eloquent resource query entry points', function (): void {
$user = User::query()->create(['name' => 'Ada']);
$first = Tenant::query()->create(['user_id' => $user->id, 'title' => 'First']);
$second = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Second']);
$visible = TenantRecord::query()->create(['tenant_id' => $first->id, 'name' => 'Visible']);
TenantRecord::query()->create(['tenant_id' => $second->id, 'name' => 'Hidden']);
app(CurrentTenantContext::class)->activate($first);
$request = NovaRequest::create('/nova-api/tenant-records');
foreach (['indexQuery', 'detailQuery', 'editQuery', 'replicateQuery', 'relatableQuery'] as $method) {
$ids = TenantRecordResource::{$method}($request, TenantRecord::query())->pluck('id')->all();
expect($ids)->toBe([$visible->id]);
}
});
it('stamps the selected tenant when creating and updating', function (): void {
$user = User::query()->create(['name' => 'Ada']);
$first = Tenant::query()->create(['user_id' => $user->id, 'title' => 'First']);
$second = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Second']);
app(CurrentTenantContext::class)->activate($first);
$request = NovaRequest::create('/nova-api/tenant-records', 'POST');
[$newModel] = TenantRecordResource::fill($request, new TenantRecord(['tenant_id' => $second->id]));
expect($newModel->tenant_id)->toBe($first->id);
$existing = new TenantRecord(['tenant_id' => $second->id]);
[$updatedModel] = TenantRecordResource::fillForUpdate($request, $existing);
expect($updatedModel->tenant_id)->toBe($first->id);
});
it('rejects tenant aware resource queries without a selection', function (): void {
$request = NovaRequest::create('/nova-api/tenant-records');
expect(fn () => TenantRecordResource::indexQuery($request, TenantRecord::query()))
->toThrow(TenantNotSelected::class);
});