**Phase 4 – was im Hintergrund lief, kannte keinen Mandanten.** `App\Tenancy\TenantRunner` führt jeden Lauf einmal je aktivem Mandanten aus, mit gesetztem Kontext. Ohne ihn sah der Scheduler bei zwei Mandanten gar keinen: Abfragen filterten nicht mehr und der Absender fiel auf die .env zurück – eine Erinnerung des einen Vermieters wäre unter der Adresse des anderen verschickt worden. Seit gestern gibt es zwei Mandanten; nur weil der Cronjob auf Strato noch nicht eingerichtet ist, ist das bisher niemandem passiert. Betroffen: Vormerkungen, Portal-Abgleich, Portal-Postfach, Überwachung, Erinnerungen, Bewertungsbitten, Anonymisierung. Die Datenbank-Sicherung bleibt global – eine Datenbank, ein Dump. Ein Mandant, dessen Lauf scheitert, hält die anderen nicht auf. Das Postfach gehört jetzt dem Mandanten (`mailbox_*`, Passwort verschlüsselt, leerer Server = aus). Es steht im Mandanten-Admin und nur, wenn der Betreiber den Dienst freigeschaltet hat. Auch die Zustands- Schlüssel im Cache tragen den Mandanten – sonst meldeten zwei Mandanten einander ihre letzte erfolgreiche Abholung. **Phase 5 – Kündigung.** `App\Tenancy\TenantArchive` mit zwei Befehlen des Betreibers: `tenants:export <slug>` schreibt alle Zeilen und Dateien in ein Zip, `tenants:delete <slug>` exportiert erst, fragt nach und löscht dann in der Reihenfolge, die die Fremdschlüssel erlauben, Medien eingeschlossen. Konten, die nur zu diesem Mandanten gehörten, gehen mit – ein Konto, das nirgends mehr hineinkommt, ist personenbezogenes Datum ohne Zweck. Wer noch für einen anderen Mandanten arbeitet, und der Betreiber, bleiben. Der Export nennt die Konten mit Rolle, aber ohne Passwort-Hashes und ohne 2FA-Geheimnisse: er wird herausgegeben. Löschen ist bewusst keine Schaltfläche im Panel. `TenantSeparationTest` füllt jeden Bereich aus A1 für zwei Mandanten, exportiert und löscht einen und prüft, dass der andere Zeile für Zeile unverändert ist – auch die Hosts, die über die Website am Mandanten hängen und keine eigene tenant_id haben. Dieser Test merkt künftig, wenn eine neue Tabelle ihre tenant_id vergisst. 262 Feature-Tests grün. Der Export einmal auf echten Daten ausgeführt (2,3 MB mit Fotos); das Löschen nicht, weil es Dateien aus dem echten Speicher entfernt. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
221 lines
8.4 KiB
PHP
221 lines
8.4 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature;
|
||
|
||
use App\Enums\UserRole;
|
||
use App\Models\Amenity;
|
||
use App\Models\BlockedPeriod;
|
||
use App\Models\Booking;
|
||
use App\Models\Channel;
|
||
use App\Models\ContentBlock;
|
||
use App\Models\Extra;
|
||
use App\Models\Faq;
|
||
use App\Models\Guest;
|
||
use App\Models\Notice;
|
||
use App\Models\Page;
|
||
use App\Models\Payment;
|
||
use App\Models\Photo;
|
||
use App\Models\Property;
|
||
use App\Models\Review;
|
||
use App\Models\Season;
|
||
use App\Models\Site;
|
||
use App\Models\SpecialOffer;
|
||
use App\Models\Tenant;
|
||
use App\Models\User;
|
||
use App\Tenancy\TenantArchive;
|
||
use App\Tenancy\TenantContext;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\File;
|
||
use Tests\TestCase;
|
||
use ZipArchive;
|
||
|
||
/**
|
||
* Phase 5: the acceptance of the separation. Every area from A1 is filled for two landlords; then one is
|
||
* exported and deleted, and the other has to be complete – rows, files and booking numbers.
|
||
*
|
||
* This is the test that would notice if a new table ever forgot its tenant_id: such a table would neither
|
||
* be exported nor deleted, and its rows would outlive the landlord they belong to.
|
||
*/
|
||
class TenantSeparationTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
public function test_every_area_of_a_landlord_is_exported_and_deleted_while_the_other_stays_whole(): void
|
||
{
|
||
$gone = $this->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<string, int>
|
||
*/
|
||
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;
|
||
}
|
||
}
|