37 lines
1.1 KiB
PHP
37 lines
1.1 KiB
PHP
<?php
|
|
|
|
use App\Models\Tenant;
|
|
use App\Support\Auth\Capabilities;
|
|
use App\Support\Auth\UiEnforcement;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
it('preflights bulk selections with a set-based tenant_memberships query (no N+1)', function () {
|
|
$tenants = Tenant::factory()->count(25)->create();
|
|
[$user] = createUserWithTenant($tenants->first(), role: 'owner');
|
|
|
|
foreach ($tenants->slice(1) as $tenant) {
|
|
$user->tenants()->syncWithoutDetaching([
|
|
$tenant->getKey() => ['role' => 'owner'],
|
|
]);
|
|
}
|
|
|
|
$enforcement = UiEnforcement::for(Capabilities::TENANT_SYNC)
|
|
->tenantFromRecord()
|
|
->preflightByCapability();
|
|
|
|
$membershipQueries = 0;
|
|
|
|
DB::listen(function ($query) use (&$membershipQueries): void {
|
|
if (str_contains($query->sql, 'tenant_memberships')) {
|
|
$membershipQueries++;
|
|
}
|
|
});
|
|
|
|
expect($enforcement->bulkSelectionIsAuthorized($user, $tenants))->toBeTrue();
|
|
expect($membershipQueries)->toBe(1);
|
|
});
|
|
|