mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:04:02 +00:00
Syncing: SyncedResourceDeleted event and DeleteResourceMapping listener
Also move pivot record deletion to that listener and improve tests The 'tenant pivot records are deleted along with the tenants to which they belong to' test is failing in this commit -- the listener for deleting mappings when a *tenant* is deleted is only implemented in the next commit. The only change done here is to re-add FKs (necessary for passing *in this commit* in that specific dataset variant) that were removed from the default test migration as we now have the DeleteResourceMapping listener that's enabled by default.
This commit is contained in:
parent
45cf7029af
commit
44e8ec8abf
10 changed files with 160 additions and 20 deletions
|
|
@ -46,6 +46,10 @@ use Stancl\Tenancy\ResourceSyncing\Events\SyncedResourceSavedInForeignDatabase;
|
|||
use Illuminate\Database\Eloquent\Scope;
|
||||
use Illuminate\Database\QueryException;
|
||||
use function Stancl\Tenancy\Tests\pest;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\SyncedResourceDeleted;
|
||||
use Stancl\Tenancy\ResourceSyncing\Listeners\DeleteResourceMapping;
|
||||
|
||||
beforeEach(function () {
|
||||
config(['tenancy.bootstrappers' => [
|
||||
|
|
@ -92,6 +96,7 @@ beforeEach(function () {
|
|||
CentralUser::$creationAttributes = $creationAttributes;
|
||||
|
||||
Event::listen(SyncedResourceSaved::class, UpdateOrCreateSyncedResource::class);
|
||||
Event::listen(SyncedResourceDeleted::class, DeleteResourceMapping::class);
|
||||
Event::listen(SyncMasterDeleted::class, DeleteResourcesInTenants::class);
|
||||
Event::listen(SyncMasterRestored::class, RestoreResourcesInTenants::class);
|
||||
Event::listen(CentralResourceAttachedToTenant::class, CreateTenantResource::class);
|
||||
|
|
@ -890,9 +895,13 @@ test('deleting SyncMaster automatically deletes its Syncables', function (bool $
|
|||
'basic pivot' => false,
|
||||
]);
|
||||
|
||||
test('tenant pivot records are deleted along with the tenants to which they belong to', function() {
|
||||
test('tenant pivot records are deleted along with the tenants to which they belong to', function(bool $dbLevelOnCascadeDelete) {
|
||||
[$tenant] = createTenantsAndRunMigrations();
|
||||
|
||||
if ($dbLevelOnCascadeDelete) {
|
||||
addFkConstraintsToTenantUsersPivot();
|
||||
}
|
||||
|
||||
$syncMaster = CentralUser::create([
|
||||
'global_id' => 'cascade_user',
|
||||
'name' => 'Central user',
|
||||
|
|
@ -907,6 +916,54 @@ test('tenant pivot records are deleted along with the tenants to which they belo
|
|||
|
||||
// Deleting tenant deletes its pivot records
|
||||
expect(DB::select("SELECT * FROM tenant_users WHERE tenant_id = ?", [$tenant->getTenantKey()]))->toHaveCount(0);
|
||||
})->with([
|
||||
'db level on cascade delete' => true,
|
||||
'event-based on cascade delete' => false,
|
||||
]);
|
||||
|
||||
test('pivot record is automatically deleted with the tenant resource', function() {
|
||||
[$tenant] = createTenantsAndRunMigrations();
|
||||
|
||||
$syncMaster = CentralUser::create([
|
||||
'global_id' => 'cascade_user',
|
||||
'name' => 'Central user',
|
||||
'email' => 'central@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'cascade_user',
|
||||
]);
|
||||
|
||||
$syncMaster->tenants()->attach($tenant);
|
||||
|
||||
expect(DB::select("SELECT * FROM tenant_users WHERE tenant_id = ?", [$tenant->id]))->toHaveCount(1);
|
||||
|
||||
$tenant->run(function () {
|
||||
TenantUser::firstWhere('global_id', 'cascade_user')->delete();
|
||||
});
|
||||
|
||||
// Deleting tenant resource deletes its pivot record
|
||||
expect(DB::select("SELECT * FROM tenant_users WHERE tenant_id = ?", [$tenant->id]))->toHaveCount(0);
|
||||
|
||||
// The same works with forceDelete
|
||||
addExtraColumns(true);
|
||||
|
||||
$syncMaster = CentralUserWithSoftDeletes::create([
|
||||
'global_id' => 'force_cascade_user',
|
||||
'name' => 'Central user',
|
||||
'email' => 'central2@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'force_cascade_user',
|
||||
'foo' => 'bar',
|
||||
]);
|
||||
|
||||
$syncMaster->tenants()->attach($tenant);
|
||||
|
||||
expect(DB::select("SELECT * FROM tenant_users WHERE tenant_id = ?", [$tenant->id]))->toHaveCount(1);
|
||||
|
||||
$tenant->run(function () {
|
||||
TenantUserWithSoftDeletes::firstWhere('global_id', 'force_cascade_user')->forceDelete();
|
||||
});
|
||||
|
||||
expect(DB::select("SELECT * FROM tenant_users WHERE tenant_id = ?", [$tenant->id]))->toHaveCount(0);
|
||||
});
|
||||
|
||||
test('trashed resources are synced correctly', function () {
|
||||
|
|
@ -1265,6 +1322,14 @@ test('global scopes on syncable models can break resource syncing', function ()
|
|||
expect($tenant1->run(fn () => TenantUser::first()->name))->toBe('tenant2 user');
|
||||
});
|
||||
|
||||
function addFkConstraintsToTenantUsersPivot(): void
|
||||
{
|
||||
Schema::table('tenant_users', function (Blueprint $table) {
|
||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
|
||||
$table->foreign('global_user_id')->references('global_id')->on('users')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create two tenants and run migrations for those tenants.
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue