From 5e3eb4322cc487d540796317e813ac2ffe076646 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Tue, 7 Jul 2026 12:35:03 +0200 Subject: [PATCH] Test that updating pivot columns does not re-create the synced tenant resource Regression test for https://github.com/archtechx/tenancy/issues/1467 --- tests/ResourceSyncingTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 11a172c5..e971b6b5 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -274,6 +274,32 @@ test('attaching central resources to tenants or vice versa creates synced tenant }); }); +test('updating pivot column does not re-create the synced tenant resource', function () { + // Add an extra pivot column so we can update it + Schema::table('tenant_users', fn (Blueprint $table) => $table->string('note')->nullable()); + + $centralUser = CentralUser::create([ + 'global_id' => 'acme', + 'name' => 'John Doe', + 'email' => 'john@localhost', + 'password' => 'secret', + 'role' => 'commenter', + ]); + + $tenant = Tenant::create(); + migrateUsersTableForTenants(); + + // Attaching creates the tenant resource + $centralUser->tenants()->attach($tenant); + $tenant->run(fn () => expect(TenantUser::count())->toBe(1)); + + // Updating a pivot column does not re-attach and create the resource again + // (which would throw a duplicate entry error -- regression test for #1467) + $centralUser->tenants()->updateExistingPivot($tenant->getTenantKey(), ['note' => 'foo']); + + $tenant->run(fn () => expect(TenantUser::count())->toBe(1)); +}); + test('detaching central users from tenants or vice versa force deletes the synced tenant resource', function (bool $attachUserToTenant) { $centralUser = CentralUser::create([ 'global_id' => 'acme',