|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Illuminate\Support\Facades\DB; |
3 | 4 | use Spatie\Permission\Contracts\Permission as PermissionContract; |
4 | 5 | use Spatie\Permission\Contracts\Role as RoleContract; |
5 | 6 | use Spatie\Permission\Models\Permission as SpatiePermission; |
|
22 | 23 | expect($reflectedProperty->getValue(app(PermissionRegistrar::class)))->toBeNull(); |
23 | 24 | }); |
24 | 25 |
|
| 26 | +it('clears the loaded permissions collection when reinitializing the cache', function () { |
| 27 | + $reflectedClass = new ReflectionClass(app(PermissionRegistrar::class)); |
| 28 | + $reflectedProperty = $reflectedClass->getProperty('permissions'); |
| 29 | + $reflectedProperty->setAccessible(true); |
| 30 | + |
| 31 | + app(PermissionRegistrar::class)->getPermissions(); |
| 32 | + |
| 33 | + expect($reflectedProperty->getValue(app(PermissionRegistrar::class)))->not->toBeNull(); |
| 34 | + |
| 35 | + app(PermissionRegistrar::class)->initializeCache(); |
| 36 | + |
| 37 | + expect($reflectedProperty->getValue(app(PermissionRegistrar::class)))->toBeNull(); |
| 38 | +}); |
| 39 | + |
| 40 | +it('does not leak a previous tenant\'s permissions after switching cache context via initializeCache', function () { |
| 41 | + // Two separate cache "stores" stand in for two tenants' cache namespaces |
| 42 | + // (e.g. distinct cache prefixes/connections in a real multi-tenant app). |
| 43 | + config([ |
| 44 | + 'cache.stores.tenant_a' => ['driver' => 'array'], |
| 45 | + 'cache.stores.tenant_b' => ['driver' => 'array'], |
| 46 | + ]); |
| 47 | + |
| 48 | + // Insert both tenants' rows via the query builder, bypassing Eloquent, |
| 49 | + // so the RefreshesPermissionCache model events don't auto-bust the cache and |
| 50 | + // mask the very staleness this test is meant to catch. |
| 51 | + $tenantAId = DB::table('permissions')->insertGetId([ |
| 52 | + 'name' => 'tenant-permission', |
| 53 | + 'guard_name' => 'web', |
| 54 | + 'created_at' => now(), |
| 55 | + 'updated_at' => now(), |
| 56 | + ]); |
| 57 | + |
| 58 | + config(['permission.cache.store' => 'tenant_a']); |
| 59 | + app(PermissionRegistrar::class)->initializeCache(); |
| 60 | + |
| 61 | + $loaded = app(PermissionRegistrar::class)->getPermissions()->firstWhere('name', 'tenant-permission'); |
| 62 | + expect($loaded->getKey())->toBe($tenantAId); |
| 63 | + |
| 64 | + // Simulate switching to tenant B: its own row for the "same" permission has |
| 65 | + // a different primary key, as it would in a separate tenant database. |
| 66 | + DB::table('permissions')->where('id', $tenantAId)->delete(); |
| 67 | + $tenantBId = DB::table('permissions')->insertGetId([ |
| 68 | + 'name' => 'tenant-permission', |
| 69 | + 'guard_name' => 'web', |
| 70 | + 'created_at' => now(), |
| 71 | + 'updated_at' => now(), |
| 72 | + ]); |
| 73 | + expect($tenantBId)->not->toBe($tenantAId); |
| 74 | + |
| 75 | + config(['permission.cache.store' => 'tenant_b']); |
| 76 | + app(PermissionRegistrar::class)->initializeCache(); |
| 77 | + |
| 78 | + $loaded = app(PermissionRegistrar::class)->getPermissions()->firstWhere('name', 'tenant-permission'); |
| 79 | + expect($loaded->getKey())->toBe($tenantBId); |
| 80 | +}); |
| 81 | + |
25 | 82 | it('can check uids', function () { |
26 | 83 | $uids = [ |
27 | 84 | // UUIDs |
|
0 commit comments