Mandantenabschluss: Marken, Vertragsdateien und Archive trennen
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m23s

This commit is contained in:
Lars committed 2026-10-26 17:07:11 +01:00
1 parent 86374b1659
commit 06df80e4db
45 files changed
+592 -99

No files matched your search

+148
View File
@@ -0,0 +1,148 @@
<?php
namespace Tests\Feature;
use App\Documents\RentalContract;
use App\Models\Booking;
use App\Models\Property;
use App\Models\Site;
use App\Models\Tenant;
use App\Tenancy\TenantArchive;
use App\Tenancy\TenantContext;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\TestWith;
use Tests\TestCase;
use ZipArchive;
class TenantFilesTest extends TestCase
{
use RefreshDatabase;
#[TestWith([false])]
#[TestWith([true])]
public function test_equal_booking_numbers_do_not_overwrite_another_tenants_document(bool $draft): void
{
Storage::fake('local');
$first = $this->booking('Nordhaus');
$second = $this->booking('Südheim');
$contracts = app(RentalContract::class);
$firstPath = $contracts->generate($first, $draft);
$firstContent = Storage::disk('local')->get($firstPath);
$secondPath = $contracts->generate($second, $draft);
$this->assertSame($first->reference, $second->reference);
$this->assertNotSame($firstPath, $secondPath);
$this->assertSame($firstContent, Storage::disk('local')->get($firstPath));
Storage::disk('local')->assertExists($secondPath);
}
public function test_export_and_deletion_include_website_media_and_all_document_versions_only_of_that_tenant(): void
{
Storage::fake('public');
Storage::fake('local');
$gone = $this->booking('Geht');
$stays = $this->booking('Bleibt');
$site = $gone->website();
$otherSite = $stays->website();
$logo = $site->addMedia(UploadedFile::fake()->image('geht.png'))->toMediaCollection(Site::BRAND_LOGO);
$otherLogo = $otherSite->addMedia(UploadedFile::fake()->image('bleibt.png'))->toMediaCollection(Site::BRAND_LOGO);
$contracts = app(RentalContract::class);
$oldOffer = $contracts->generate($gone, draft: true);
$gone->update(['offer_version' => 2]);
$newOffer = $contracts->generate($gone, draft: true);
$contract = $contracts->generate($gone);
$otherContract = $contracts->generate($stays);
$otherBytes = Storage::disk('local')->get($otherContract);
$directory = storage_path('framework/testing/tenant-files');
try {
$archive = app(TenantArchive::class);
$path = $archive->export($site->tenant, $directory);
$zip = new ZipArchive;
$this->assertTrue($zip->open($path));
$this->assertNotFalse($zip->locateName('dateien/'.$logo->id.'-geht.png'));
$this->assertFalse($zip->locateName('dateien/'.$otherLogo->id.'-bleibt.png'));
foreach ([$oldOffer, $newOffer, $contract] as $document) {
$this->assertSame(Storage::disk('local')->get($document), $zip->getFromName('dateien/local/'.$document));
}
$this->assertFalse($zip->locateName('dateien/local/'.$otherContract));
$media = json_decode($zip->getFromName('daten/media.json'), true, flags: JSON_THROW_ON_ERROR);
$this->assertSame([$logo->id], array_column($media, 'id'));
$zip->close();
$archive->delete($site->tenant);
$this->assertFileDoesNotExist($logo->getPath());
$this->assertFileExists($otherLogo->getPath());
$this->assertDatabaseMissing('media', ['id' => $logo->id]);
$this->assertDatabaseHas('media', ['id' => $otherLogo->id]);
Storage::disk('local')->assertMissing([$oldOffer, $newOffer, $contract]);
$this->assertSame($otherBytes, Storage::disk('local')->get($otherContract));
} finally {
File::deleteDirectory($directory);
}
}
public function test_existing_document_paths_remain_readable_and_are_exported_and_deleted(): void
{
Storage::fake('local');
$booking = $this->booking('Altbestand');
$path = 'contracts/Mietvertrag-2026-0001.pdf';
Storage::disk('local')->put($path, '%PDF-alter-vertrag');
$booking->update(['contract_path' => $path]);
$directory = storage_path('framework/testing/legacy-tenant-files');
try {
$this->assertSame(Storage::disk('local')->path($path), app(RentalContract::class)->absolutePath($booking));
$archive = app(TenantArchive::class);
$zip = new ZipArchive;
$this->assertTrue($zip->open($archive->export($booking->tenant, $directory)));
$this->assertSame('%PDF-alter-vertrag', $zip->getFromName('dateien/local/'.$path));
$zip->close();
$archive->delete($booking->tenant);
Storage::disk('local')->assertMissing($path);
} finally {
File::deleteDirectory($directory);
}
}
public function test_ambiguous_legacy_documents_prevent_deletion_of_either_tenant(): void
{
Storage::fake('local');
$first = $this->booking('Erster');
$second = $this->booking('Zweiter');
$path = 'contracts/Mietvertrag-2026-0001.pdf';
Storage::disk('local')->put($path, '%PDF-mehrdeutig');
$first->update(['contract_path' => $path]);
$second->update(['contract_path' => $path]);
try {
app(TenantArchive::class)->delete($first->tenant);
$this->fail('Mehrdeutige Altbestände dürfen nicht automatisch gelöscht werden.');
} catch (\RuntimeException $exception) {
$this->assertSame('Vertragsdatei ist mehreren Mandanten zugeordnet: '.$path, $exception->getMessage());
}
$this->assertModelExists($first->tenant);
$this->assertModelExists($second->tenant);
Storage::disk('local')->assertExists($path);
}
private function booking(string $name): Booking
{
$tenant = Tenant::factory()->create(['name' => $name]);
return app(TenantContext::class)->run($tenant, function () use ($name): Booking {
$site = Site::factory()->create(['name' => $name]);
return Booking::factory()->for(Property::factory()->create(['site_id' => $site->id]))
->create(['reference' => '2026-0001', 'offer_version' => 1]);
});
}
}