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);
});
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace MeghdadFadaee\NovaTenancy\Tests\Fixtures;
use Illuminate\Database\Eloquent\Model;
use MeghdadFadaee\NovaTenancy\Concerns\HasNovaTenantPresentation;
use MeghdadFadaee\NovaTenancy\Contracts\NovaTenant;
class Tenant extends Model implements NovaTenant
{
use HasNovaTenantPresentation;
protected $guarded = [];
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace MeghdadFadaee\NovaTenancy\Tests\Fixtures;
use Illuminate\Database\Eloquent\Model;
class TenantRecord extends Model
{
protected $guarded = [];
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace MeghdadFadaee\NovaTenancy\Tests\Fixtures;
use Laravel\Nova\Fields\FieldCollection;
use Laravel\Nova\Http\Requests\NovaRequest;
use MeghdadFadaee\NovaTenancy\Nova\TenantResource;
class TenantRecordResource extends TenantResource
{
public static $model = TenantRecord::class;
public function fields(NovaRequest $request): array
{
return [];
}
public function creationFields(NovaRequest $request): FieldCollection
{
return new FieldCollection;
}
public function updateFields(NovaRequest $request): FieldCollection
{
return new FieldCollection;
}
}
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace MeghdadFadaee\NovaTenancy\Tests\Fixtures;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Http\Request;
use MeghdadFadaee\NovaTenancy\Contracts\ProvidesNovaTenants;
class User extends Authenticatable implements ProvidesNovaTenants
{
protected $guarded = [];
public function tenants(): HasMany
{
return $this->hasMany(Tenant::class);
}
public function novaTenantQuery(Request $request): Builder
{
return $this->tenants()->getQuery()->orderBy('title');
}
}
+5
View File
@@ -0,0 +1,5 @@
<?php
use MeghdadFadaee\NovaTenancy\Tests\TestCase;
uses(TestCase::class)->in('Feature');
+88
View File
@@ -0,0 +1,88 @@
<?php
namespace MeghdadFadaee\NovaTenancy\Tests;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
use Laravel\Nova\NovaCoreServiceProvider;
use MeghdadFadaee\NovaTenancy\Http\Controllers\CurrentTenantController;
use MeghdadFadaee\NovaTenancy\Http\Controllers\CurrentTenantLogoController;
use MeghdadFadaee\NovaTenancy\Http\Controllers\TenantIndexController;
use MeghdadFadaee\NovaTenancy\NovaTenancy;
use MeghdadFadaee\NovaTenancy\NovaTenancyServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
abstract class TestCase extends Orchestra
{
protected function getPackageProviders($app): array
{
return [
NovaCoreServiceProvider::class,
NovaTenancyServiceProvider::class,
];
}
protected function defineEnvironment($app): void
{
$app['config']->set('app.key', 'base64:'.base64_encode(str_repeat('n', 32)));
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
$app['config']->set('auth.providers.users.model', Fixtures\User::class);
$app['config']->set('nova-tenancy.model.title_attribute', 'title');
$app['config']->set('nova-tenancy.model.logo_attribute', 'logo_url');
$app['config']->set('nova-tenancy.model.description_attribute', 'description');
$app['config']->set('nova-tenancy.model.accent_attribute', 'accent');
$app['config']->set('nova-tenancy.model.search_columns', ['title']);
$app['config']->set('nova-tenancy.resources.tenant_column', 'tenant_id');
}
protected function defineRoutes($router): void
{
Route::middleware('web')->group(function (): void {
Route::get('/testing/tenants', TenantIndexController::class);
Route::get('/testing/current', [CurrentTenantController::class, 'show']);
Route::post('/testing/current', [CurrentTenantController::class, 'store']);
Route::delete('/testing/current', [CurrentTenantController::class, 'destroy']);
Route::get('/testing/current/logo', CurrentTenantLogoController::class);
});
}
protected function setUp(): void
{
parent::setUp();
NovaTenancy::flushState();
Schema::create('users', function (Blueprint $table): void {
$table->id();
$table->string('name');
$table->rememberToken();
$table->timestamps();
});
Schema::create('tenants', function (Blueprint $table): void {
$table->id();
$table->foreignId('user_id');
$table->string('title');
$table->string('logo_url')->nullable();
$table->string('description')->nullable();
$table->string('accent')->nullable();
$table->timestamps();
});
Schema::create('tenant_records', function (Blueprint $table): void {
$table->id();
$table->foreignId('tenant_id');
$table->string('name');
$table->timestamps();
});
}
protected function tearDown(): void
{
NovaTenancy::flushState();
parent::tearDown();
}
}