landlordWithEverything('Geht'); $stays = $this->landlordWithEverything('Bleibt'); $before = $this->census($stays); $this->assertNotEmpty(array_filter($before), 'Der Aufbau muss jeden Bereich füllen.'); $archive = app(TenantArchive::class); $directory = storage_path('framework/testing/tenant-export'); $path = $archive->export($gone, $directory); $this->assertFileExists($path); $this->assertExportHasOnly($path, $gone, $stays); $archive->delete($gone); $this->assertDatabaseMissing('tenants', ['id' => $gone->id]); foreach ($archive->tables() as $table) { $this->assertSame( 0, DB::table($table)->where('tenant_id', $gone->id)->count(), "In {$table} sind Zeilen des gelöschten Mandanten übrig.", ); } $this->assertSame($before, $this->census($stays), 'Der verbliebene Mandant hat sich verändert.'); // site_domains trägt keine tenant_id; die Hosts hängen über die Website am Mandanten. Bleibt hier // eine Zeile übrig, zeigt ein Host auf eine gelöschte Website – schlimmstenfalls auf einen fremden. $this->assertSame(0, DB::table('site_domains')->where('host', 'geht.test')->count(), 'Verwaister Host nach der Löschung.'); $this->assertSame(1, DB::table('site_domains')->where('host', 'bleibt.test')->count()); File::deleteDirectory($directory); } public function test_a_deleted_landlord_frees_their_booking_numbers_without_touching_the_others(): void { $gone = $this->landlordWithEverything('Geht'); $stays = $this->landlordWithEverything('Bleibt'); $reference = app(TenantContext::class)->run($stays, fn (): string => Booking::query()->value('reference')); app(TenantArchive::class)->delete($gone); app(TenantContext::class)->run($stays, function () use ($reference): void { $this->assertSame($reference, Booking::query()->value('reference')); $this->assertSame(1, Booking::query()->count()); }); } public function test_accounts_of_the_deleted_landlord_go_with_them_unless_they_serve_somebody_else(): void { $gone = $this->landlordWithEverything('Geht'); $stays = $this->landlordWithEverything('Bleibt'); $onlyTheirs = User::factory()->create(['email' => 'nur-dort@example.com']); $onlyTheirs->tenants()->sync([$gone->id => ['role' => UserRole::Admin->value]]); $shared = User::factory()->create(['email' => 'beide@example.com']); $shared->tenants()->sync([ $gone->id => ['role' => UserRole::Rentals->value], $stays->id => ['role' => UserRole::Rentals->value], ]); $operator = User::factory()->create(['email' => 'betrieb@example.com']); $operator->tenants()->sync([$gone->id => ['role' => UserRole::Admin->value]]); $operator->forceFill(['is_operator' => true])->saveQuietly(); app(TenantArchive::class)->delete($gone); $this->assertDatabaseMissing('users', ['email' => 'nur-dort@example.com']); $this->assertDatabaseHas('users', ['email' => 'beide@example.com']); $this->assertDatabaseHas('users', ['email' => 'betrieb@example.com']); $this->assertSame([$stays->id], $shared->fresh()->tenants()->pluck('tenants.id')->all()); } public function test_the_export_names_the_accounts_but_not_their_passwords(): void { $tenant = $this->landlordWithEverything('Geht'); $user = User::factory()->create(['email' => 'wirt@example.com', 'name' => 'Wirt']); $user->tenants()->sync([$tenant->id => ['role' => UserRole::Admin->value]]); $exported = app(TenantArchive::class)->usersOf($tenant); $this->assertSame(['wirt@example.com'], array_column($exported, 'email')); $this->assertSame('admin', $exported[0]['role']); $this->assertArrayNotHasKey('password', $exported[0]); $this->assertStringNotContainsString($user->password, (string) json_encode($exported)); } /** * The archive of one landlord must not carry a single row of the other. */ private function assertExportHasOnly(string $path, Tenant $exported, Tenant $other): void { $zip = new ZipArchive; $this->assertTrue($zip->open($path) === true); $seen = 0; for ($i = 0; $i < $zip->numFiles; $i++) { $name = (string) $zip->getNameIndex($i); if (! str_starts_with($name, 'daten/')) { continue; } $rows = json_decode((string) $zip->getFromIndex($i), true); $this->assertIsArray($rows); foreach ($rows as $row) { $seen++; $this->assertSame($exported->id, $row['tenant_id'], "Fremde Zeile in {$name}."); $this->assertNotSame($other->id, $row['tenant_id']); } } $zip->close(); $this->assertGreaterThan(0, $seen, 'Das Archiv enthält keine Daten.'); } /** * How many rows this landlord has per table – the yardstick for "unchanged". * * @return array */ private function census(Tenant $tenant): array { $counts = []; foreach (app(TenantArchive::class)->tables() as $table) { $counts[$table] = DB::table($table)->where('tenant_id', $tenant->id)->count(); } return $counts; } /** * One landlord with at least one row in every area of A1. */ private function landlordWithEverything(string $name): Tenant { $tenant = Tenant::factory()->create(['name' => $name]); app(TenantContext::class)->run($tenant, function () use ($name): void { $site = Site::factory()->create([ 'name' => $name, 'domain' => mb_strtolower($name).'.test', 'is_default' => false, ]); $property = Property::factory()->create(['site_id' => $site->id]); Page::factory()->create(['site_id' => $site->id, 'slug' => Page::HOME, 'sections' => []]); Season::factory()->create(['property_id' => $property->id]); Extra::factory()->create(['property_id' => $property->id]); Amenity::factory()->create(['property_id' => $property->id]); SpecialOffer::factory()->create(['property_id' => $property->id]); Photo::factory()->create(['property_id' => $property->id]); Faq::factory()->create(); Notice::factory()->create(); ContentBlock::query()->create(['key' => 'legal.imprint', 'value' => 'Impressum '.$name]); $guest = Guest::factory()->create(); $booking = Booking::factory()->for($property)->create(['guest_id' => $guest->id]); Payment::factory()->create(['booking_id' => $booking->id]); Review::factory()->create(['booking_id' => $booking->id, 'property_id' => $property->id]); $channel = Channel::factory()->create(['property_id' => $property->id]); BlockedPeriod::factory()->create(['property_id' => $property->id, 'channel_id' => $channel->id]); }); return $tenant; } }