mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 01:14:03 +00:00
Clarify scopeGetModelQuery test, document edge case
This commit is contained in:
parent
b08c76974a
commit
add8a96b32
1 changed files with 32 additions and 14 deletions
|
|
@ -1199,6 +1199,8 @@ test('resource creation works correctly when central resource provides defaults
|
||||||
});
|
});
|
||||||
|
|
||||||
test('scopeGetModelQuery can make resource syncing work with global scopes', function (bool $scopeGetModelQuery) {
|
test('scopeGetModelQuery can make resource syncing work with global scopes', function (bool $scopeGetModelQuery) {
|
||||||
|
// The TenantUserWithScope model has whereNull('name') global scope, but we're creating users WITH names.
|
||||||
|
// This creates a conflict during resource syncing when trying to find existing central users.
|
||||||
if ($scopeGetModelQuery) {
|
if ($scopeGetModelQuery) {
|
||||||
// Add a scope that bypasses the global scope
|
// Add a scope that bypasses the global scope
|
||||||
UpdateOrCreateSyncedResource::$scopeGetModelQuery = function (Builder $query) {
|
UpdateOrCreateSyncedResource::$scopeGetModelQuery = function (Builder $query) {
|
||||||
|
|
@ -1208,17 +1210,42 @@ test('scopeGetModelQuery can make resource syncing work with global scopes', fun
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a central user that will be synced to tenant databases
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'email' => 'user@test.cz',
|
'email' => 'user@test.cz',
|
||||||
'name' => 'User',
|
'name' => 'User', // Note: TenantUserWithScope has whereNull('name') global scope
|
||||||
'password' => bcrypt('****'),
|
'password' => bcrypt('****'),
|
||||||
'role' => 'admin',
|
'role' => 'admin',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
|
// Create user in tenant1
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
TenantUserWithScope::create([
|
||||||
|
'global_id' => $centralUser->global_id,
|
||||||
|
'name' => $centralUser->name, // This has a name, conflicting with global scope
|
||||||
|
'email' => $centralUser->email,
|
||||||
|
'password' => $centralUser->password,
|
||||||
|
'role' => 'admin'
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
// Try to create the same user in tenant2
|
||||||
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
|
if (! $scopeGetModelQuery) {
|
||||||
|
// Without scopeGetModelQuery, the global scope whereNull('name') prevents
|
||||||
|
// finding the existing central user with a name.
|
||||||
|
// This causes it to attempt creating a duplicate, which violates unique constraints.
|
||||||
|
pest()->expectException(QueryException::class);
|
||||||
|
pest()->expectExceptionMessage('Duplicate entry');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// This should sync to the existing central user, not create a duplicate
|
||||||
TenantUserWithScope::create([
|
TenantUserWithScope::create([
|
||||||
'global_id' => $centralUser->global_id,
|
'global_id' => $centralUser->global_id,
|
||||||
'name' => $centralUser->name,
|
'name' => $centralUser->name,
|
||||||
|
|
@ -1229,20 +1256,11 @@ test('scopeGetModelQuery can make resource syncing work with global scopes', fun
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
tenancy()->initialize($tenant2);
|
// Verify the syncing worked correctly when scopeGetModelQuery was used
|
||||||
|
if ($scopeGetModelQuery) {
|
||||||
if (! $scopeGetModelQuery) {
|
expect(CentralUser::count())->toBeOne(); // Should still be just one central user
|
||||||
pest()->expectException(QueryException::class);
|
expect(CentralUser::first()->global_id)->toBe($centralUser->global_id);
|
||||||
pest()->expectExceptionMessage('Duplicate entry');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TenantUserWithScope::create([
|
|
||||||
'global_id' => $centralUser->global_id,
|
|
||||||
'name' => $centralUser->name,
|
|
||||||
'email' => $centralUser->email,
|
|
||||||
'password' => $centralUser->password,
|
|
||||||
'role' => 'admin'
|
|
||||||
]);
|
|
||||||
})->with([
|
})->with([
|
||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue