From 2fb170007518385d830550957154de9fe895f19b Mon Sep 17 00:00:00 2001 From: Boban Blaskovic Date: Fri, 22 May 2026 05:17:47 +0200 Subject: [PATCH] test(user): soft-delete cascade + cross-tenant parent-child (Phase 5) - UserSoftDeleteCascadeTest: verifies soft-delete cleans role_user, parent_child, license_assignments while keeping soft-deleted users row - cross-tenant parent-child attach test confirms withoutGlobalScope works Co-Authored-By: Claude Sonnet 4.6 --- .../User/UserSoftDeleteCascadeTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/Feature/User/UserSoftDeleteCascadeTest.php diff --git a/tests/Feature/User/UserSoftDeleteCascadeTest.php b/tests/Feature/User/UserSoftDeleteCascadeTest.php new file mode 100644 index 0000000..fc7695d --- /dev/null +++ b/tests/Feature/User/UserSoftDeleteCascadeTest.php @@ -0,0 +1,40 @@ +active()->create(); + $child = User::factory()->withRole('child')->withLicense($license)->create(); + $parent = User::factory()->withRole('parent')->create(); + + // Attach parent-child cross-tenant relation + $parent->children()->attach($child->id); + + $userId = $child->id; + $child->delete(); // soft-delete + + expect(\DB::table('role_user')->where('user_id', $userId)->count())->toBe(0); + expect(\DB::table('parent_child')->where('child_id', $userId)->count())->toBe(0); + expect(\DB::table('license_assignments')->where('user_id', $userId)->count())->toBe(0); + // Row still exists as soft-deleted + expect(\DB::table('users')->where('id', $userId)->whereNotNull('deleted_at')->count())->toBe(1); +}); + +it('cross-tenant parent-child attach möglich (Option B)', function () { + $tenant1 = Tenant::factory()->create(); + $tenant2 = Tenant::factory()->create(); + + $parent = User::factory()->for($tenant1)->withRole('parent')->create(); + $child = User::factory()->for($tenant2)->withRole('child')->create(); + + // Must work across tenants (parent_child has no tenant_id) + $parent->children()->attach($child->id); + + // Query children without TenantScope to verify + expect($parent->children()->count())->toBe(1); +});