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

+178
View File
@@ -0,0 +1,178 @@
<?php
namespace Tests\Feature;
use App\Enums\UserRole;
use App\Filament\Resources\Sites\Pages\EditSite;
use App\ImageWorlds\ImageWorldRegistry;
use App\Mail\BookingConfirmed;
use App\Models\Booking;
use App\Models\Page;
use App\Models\Property;
use App\Models\Site;
use App\Models\Tenant;
use App\Models\User;
use App\Tenancy\TenantContext;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use PHPUnit\Framework\Attributes\TestWith;
use Tests\TestCase;
class TenantBrandRenderingTest extends TestCase
{
use RefreshDatabase;
#[TestWith(['ostsee-licht'])]
#[TestWith(['seebad'])]
public function test_both_themes_show_only_the_current_websites_brand(string $theme): void
{
Storage::fake('public');
$this->withoutVite();
$first = $this->website('Nordhaus', 'nord.test', $theme);
$second = $this->website('Südheim', 'sued.test', $theme);
$first->addMedia(UploadedFile::fake()->image('nord-logo.png'))->toMediaCollection(Site::BRAND_LOGO);
$first->addMedia(UploadedFile::fake()->image('nord-icon.png'))->toMediaCollection(Site::BRAND_FAVICON);
$second->addMedia(UploadedFile::fake()->image('sued-logo.png'))->toMediaCollection(Site::BRAND_LOGO);
$second->addMedia(UploadedFile::fake()->image('sued-icon.png'))->toMediaCollection(Site::BRAND_FAVICON);
$this->get('https://nord.test/')->assertOk()->assertSee('nord-logo.png')->assertSee('nord-icon.png')
->assertDontSee('sued-logo.png')->assertDontSee('sued-icon.png');
$this->get('https://sued.test/')->assertOk()->assertSee('sued-logo.png')->assertSee('sued-icon.png')
->assertDontSee('nord-logo.png')->assertDontSee('nord-icon.png');
}
#[TestWith(['ostsee-licht'])]
#[TestWith(['seebad'])]
public function test_a_website_without_a_brand_does_not_inherit_stommers_identity(string $theme): void
{
$this->withoutVite();
$this->website('Berghaus Alpenblick', 'berge.test', $theme);
$this->get('https://berge.test/')->assertOk()->assertSee('Berghaus Alpenblick')
->assertDontSee('logo-bildmarke')->assertDontSee('brand/favicon')->assertDontSee('img/favicon')
->assertDontSee('Ferienwohnung · Kühlungsborn');
}
#[TestWith(['ostsee-licht'])]
#[TestWith(['seebad'])]
public function test_the_selected_picture_world_supplies_decoration_and_placeholder_in_both_themes(string $theme): void
{
$this->withoutVite();
$directory = storage_path('framework/testing/brand-worlds');
File::ensureDirectoryExists($directory.'/berge/svg');
File::put($directory.'/berge/world.json', json_encode([
'name' => 'Berge', 'slots' => ['divider' => 'berg', 'placeholder' => 'berg'],
]));
File::put($directory.'/berge/svg/berg.svg', '<svg viewBox="0 0 10 10" data-world="berge"><path d="M0 10L5 0L10 10"/></svg>');
config(['image-worlds.paths' => [$directory], 'image-worlds.default' => null]);
app()->forgetInstance(ImageWorldRegistry::class);
$site = $this->website('Berghaus', 'berge.test', $theme);
$site->update(['image_world' => 'berge']);
try {
$this->get('https://berge.test/')->assertOk()->assertSee('data-world="berge"', escape: false)
->assertDontSee('coastal-illustration')->assertDontSee('brand/mark.svg');
} finally {
File::deleteDirectory($directory);
}
}
public function test_booking_mail_embeds_only_the_logo_of_its_website(): void
{
Storage::fake('public');
$first = $this->website('Nordhaus', 'nord.test', 'seebad');
$second = $this->website('Südheim', 'sued.test', 'seebad');
$firstLogo = $first->addMedia(UploadedFile::fake()->image('nord-mail.png', 300, 100))->toMediaCollection(Site::BRAND_DOCUMENT_LOGO);
$secondLogo = $second->addMedia(UploadedFile::fake()->image('sued-mail.png', 400, 150))->toMediaCollection(Site::BRAND_DOCUMENT_LOGO);
app(TenantContext::class)->run($first->tenant, function () use ($first, $firstLogo, $secondLogo): void {
$booking = Booking::factory()->for($first->primaryProperty())->create(['deposit_due_on' => now()->addWeek()]);
$mail = new BookingConfirmed($booking);
$html = $mail->render();
$this->assertSame(1, preg_match('/src="data:image\/png;base64,([^"]+)"/s', $html, $image));
$this->assertSame(File::get($firstLogo->getPath()), base64_decode($image[1], strict: true));
$this->assertNotSame(File::get($secondLogo->getPath()), base64_decode($image[1], strict: true));
$this->assertStringContainsString('Nordhaus', $html);
$this->assertStringNotContainsString('Südheim', $html);
$this->assertStringNotContainsString('Ferienwohnung Stommer', $html);
$mail->assertSeeInText('Nordhaus');
$mail->assertDontSeeInText('Ferienwohnung Stommer');
});
}
public function test_the_legacy_brand_import_preserves_uploads_and_refuses_other_landlords(): void
{
Storage::fake('public');
$site = $this->website('Stommer', 'stommer.test', 'seebad');
$site->tenant->update(['slug' => 'stommer']);
$custom = $site->addMedia(UploadedFile::fake()->image('eigenes-logo.png'))->toMediaCollection(Site::BRAND_LOGO);
$other = $this->website('Anderer Vermieter', 'andere.test', 'seebad');
$this->artisan('sites:import-stommer-brand', ['site' => $site->id])->assertSuccessful();
$this->artisan('sites:import-stommer-brand', ['site' => $site->id])->assertSuccessful();
$this->artisan('sites:import-stommer-brand', ['site' => $other->id])->assertFailed();
$this->assertSame($custom->id, $site->fresh()->getFirstMedia(Site::BRAND_LOGO)->id);
$this->assertCount(3, $site->fresh()->media);
$this->assertCount(0, $other->fresh()->media);
}
#[TestWith(['logo'])]
#[TestWith(['favicon'])]
public function test_the_admin_rejects_unchecked_svg_uploads(string $field): void
{
Storage::fake('public');
$site = $this->website('Nordhaus', 'nord.test', 'seebad');
$user = User::factory()->role(UserRole::Admin)->create();
$user->tenants()->syncWithoutDetaching([$site->tenant_id => ['role' => UserRole::Admin->value]]);
app(TenantContext::class)->set($site->tenant);
$this->actingAs($user);
$upload = UploadedFile::fake()->createWithContent('unsicher.svg', '<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>');
Livewire::test(EditSite::class, ['record' => $site->getRouteKey()])
->fillForm([$field => [$upload]])->call('save')->assertHasFormErrors([$field]);
$this->assertCount(0, $site->fresh()->media);
}
#[TestWith(['logo'])]
#[TestWith(['favicon'])]
#[TestWith(['document_logo'])]
public function test_the_admin_saves_brand_images_on_the_public_disk(string $field): void
{
Storage::fake('public');
$site = $this->website('Nordhaus', 'nord.test', 'seebad');
$user = User::factory()->role(UserRole::Admin)->create();
$user->tenants()->syncWithoutDetaching([$site->tenant_id => ['role' => UserRole::Admin->value]]);
app(TenantContext::class)->set($site->tenant);
$this->actingAs($user);
Livewire::test(EditSite::class, ['record' => $site->getRouteKey()])
->fillForm([$field => [UploadedFile::fake()->image('marke.png')]])->call('save')->assertHasNoFormErrors();
$media = $site->fresh()->getFirstMedia($field);
$this->assertNotNull($media);
$this->assertSame('public', $media->disk);
$this->assertFileExists($media->getPath());
$this->assertStringContainsString('/storage/', $site->fresh()->brandUrl($field));
}
private function website(string $name, string $host, string $theme): Site
{
$tenant = Tenant::factory()->create(['name' => $name, 'owner_name' => $name]);
return app(TenantContext::class)->run($tenant, function () use ($name, $host, $theme): Site {
$site = Site::factory()->create(['name' => $name, 'domain' => $host, 'theme' => $theme, 'is_default' => true]);
Property::factory()->create(['site_id' => $site->id]);
Page::factory()->create(['site_id' => $site->id, 'slug' => Page::HOME, 'sections' => [
['type' => 'hero', 'data' => ['title' => $name]],
]]);
return $site;
});
}
}
+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]);
});
}
}