179 lines
8.5 KiB
PHP
179 lines
8.5 KiB
PHP
<?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;
|
|
});
|
|
}
|
|
}
|