lernschiff/tests/Feature/User/UserSoftDeleteCascadeTest.php

41 lines
1.5 KiB
PHP

<?php
use App\Models\License;
use App\Models\Tenant;
use App\Models\User;
use function Pest\Laravel\actingAs;
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
it('soft-delete User bereinigt role_user, parent_child, license_assignments', function () {
$license = License::factory()->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);
});