Files
fewo-stommer/tests/Feature/ThemeSystemTest.php
LarsandClaude Opus 5.5 791786ed3e Saisonfotos am Foto statt am Erscheinungsbild
Ein Foto gehoert zu keinem (ganzjaehrig) oder mehreren Erscheinungsbildern und
erscheint nur dort, vor den ganzjaehrigen Fotos. Bausteine waehlen "automatisch"
oder eine eigene Auswahl; Titelbereich und Abschluss zeigen das erste passende
Foto ihrer Liste. Titelbild und "Bilder je Baustein" des Erscheinungsbilds werden
in die Bausteine uebernommen und entfallen dort.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 18:53:56 +01:00

342 lines
15 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Feature;
use App\Enums\ReviewStatus;
use App\Enums\UserRole;
use App\Filament\Resources\Appearances\AppearanceResource;
use App\Filament\Resources\Appearances\Pages\CreateAppearance;
use App\Filament\Resources\Pages\PageResource;
use App\Filament\Resources\Pages\Pages\EditPage;
use App\Filament\Resources\Photos\Pages\EditPhoto;
use App\Filament\Resources\Photos\Pages\ListPhotos;
use App\Filament\Resources\Sites\SiteResource;
use App\Models\Appearance;
use App\Models\Page;
use App\Models\Photo;
use App\Models\Property;
use App\Models\Review;
use App\Models\Site;
use App\Models\User;
use App\Themes\SectionRegistry;
use App\Themes\Theme;
use App\Themes\ThemeFactory;
use App\Themes\ThemeRegistry;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Database\Seeders\WebsiteContentSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Livewire\Livewire;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
class ThemeSystemTest extends TestCase
{
use RefreshDatabase;
private Site $site;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed([PropertySeeder::class, WebsiteContentSeeder::class, SiteSeeder::class]);
$this->site = Site::sole();
$this->withoutVite();
}
public function test_base_theme_is_discovered_and_provides_a_view_for_every_section(): void
{
$theme = app(ThemeRegistry::class)->get('ostsee-licht');
$this->assertSame('Ostsee-Licht', $theme->name);
$this->assertArrayHasKey('accent', $theme->tokens);
foreach (array_keys(app(SectionRegistry::class)->all()) as $section) {
$this->assertFileExists($theme->viewsPath()."/sections/{$section}.blade.php", "Section {$section} needs a view in the base theme");
}
}
/**
* @return array<string, array{string}>
*/
public static function pageProvider(): array
{
return array_map(fn (string $slug): array => [$slug], array_combine(
['Start', 'Wohnung', 'Kühlungsborn', 'Buchen', 'Kontakt', 'Impressum', 'Datenschutz'],
['/', '/wohnung', '/kuehlungsborn', '/buchen', '/kontakt', '/impressum', '/datenschutz'],
));
}
#[DataProvider('pageProvider')]
public function test_every_seeded_page_renders(string $path): void
{
$this->get($path)->assertOk()->assertSee('Zum Inhalt springen')->assertSee('Preise &amp; Buchung', false);
}
public function test_home_page_shows_content_from_the_admin(): void
{
$photo = Photo::factory()->create(['alt_text' => 'Terrasse im Sommer']);
Review::factory()->for(Property::sole())->create(['status' => ReviewStatus::Approved, 'display_name' => 'Familie M.', 'body' => 'Wunderbar ruhig!']);
$this->get('/')
->assertOk()
->assertSee('Ihr Zuhause zwischen <em>Garten</em> und Meer', false)
->assertSee('53 m²')
->assertSee('Wunderbar ruhig!')
->assertSee('"@type":"VacationRental"', false)
->assertSee('--accent:#2C5F7C', false);
$this->assertNotNull($photo);
}
public function test_unknown_and_unpublished_pages_are_not_found(): void
{
Page::factory()->for($this->site)->create(['slug' => 'entwurf', 'is_published' => false]);
$this->get('/gibt-es-nicht')->assertNotFound();
$this->get('/entwurf')->assertNotFound();
}
/**
* @return array<string, array{string, string}>
*/
public static function seasonProvider(): array
{
return [
'Herbst → Standard' => ['2026-10-22', 'Standard'],
'Sommer' => ['2026-07-15', 'Sommer'],
'Winter (über den Jahreswechsel)' => ['2027-02-10', 'Winter'],
'Weihnachten schlägt Winter' => ['2026-12-20', 'Weihnachten'],
'Silvester schlägt Winter, vor Neujahr' => ['2026-12-31', 'Silvester'],
'Silvester, nach Neujahr' => ['2027-01-02', 'Silvester'],
];
}
#[DataProvider('seasonProvider')]
public function test_appearance_follows_the_calendar_and_priority(string $date, string $expected): void
{
$this->assertSame($expected, $this->site->appearanceOn(CarbonImmutable::parse($date))?->name);
}
public function test_seasonal_appearance_changes_texts_colours_and_banner(): void
{
$this->travelTo(CarbonImmutable::parse('2026-12-20 10:00'));
$this->get('/')
->assertOk()
->assertSee('data-appearance="weihnachten"', false)
->assertSee('Besinnliche Tage <em>am Meer</em>', false)
->assertSee('Weihnachten an der Ostsee: 19.–26.12.')
->assertSee('--accent:#8C2F39', false);
}
public function test_signed_preview_shows_another_appearance_and_is_not_indexed(): void
{
$silvester = $this->site->appearances()->where('name', 'Silvester')->sole();
$this->get(URL::temporarySignedRoute('home', now()->addHour(), ['vorschau' => $silvester->id]))
->assertOk()
->assertSee('Ins neue Jahr <em>mit Meerblick</em>', false)
->assertSee('<meta name="robots" content="noindex">', false);
$this->get('/?vorschau='.$silvester->id)->assertOk()->assertDontSee('Ins neue Jahr');
}
public function test_child_theme_overrides_only_what_it_provides(): void
{
$path = storage_path('framework/testing/themes');
@mkdir($path.'/kind/views/sections', 0777, true);
file_put_contents($path.'/kind/theme.json', json_encode(['key' => 'kind', 'name' => 'Kind', 'parent' => 'ostsee-licht', 'tokens' => ['accent' => ['label' => 'Akzent', 'default' => '#FF0000']]]));
file_put_contents($path.'/kind/views/sections/facts.blade.php', '<p>Eckdaten im Kind-Theme</p>');
config(['themes.paths' => [resource_path('themes'), $path]]);
app()->forgetInstance(ThemeRegistry::class);
$this->site->update(['theme' => 'kind']);
$theme = app(ThemeFactory::class)->forSite($this->site->fresh())->activate();
app()->instance(Theme::class, $theme);
$this->assertSame(['kind', 'ostsee-licht'], array_map(fn ($manifest) => $manifest->key, $theme->chain()));
$this->assertSame('#FF0000', $theme->tokens()['accent']);
$this->assertSame('#F6F2EA', $theme->tokens()['ground']);
$this->assertStringContainsString('Eckdaten im Kind-Theme', view('theme::sections.facts')->render());
$this->assertTrue(view()->exists('theme::sections.hero'), 'falls back to the base theme');
}
public function test_sitemap_and_robots(): void
{
$this->get('/sitemap.xml')->assertOk()->assertHeader('Content-Type', 'application/xml')->assertSee(url('/wohnung'), false);
$this->get('/robots.txt')->assertOk()->assertSee('Disallow: /admin')->assertSee('Sitemap: '.url('/sitemap.xml'));
}
public function test_availability_search_prefills_the_booking_page(): void
{
$this->get('/buchen?anreise=2026-11-07&abreise=2026-11-10&personen=3')
->assertOk()
->assertSee('Gesamtpreis');
}
public function test_content_editor_manages_pages_and_appearances(): void
{
$this->actingAs(User::factory()->role(UserRole::Content)->create());
$this->get(PageResource::getUrl('index'))->assertOk()->assertSee('Die Wohnung');
$this->get(AppearanceResource::getUrl('index'))->assertOk()->assertSee('Weihnachten');
$this->get(SiteResource::getUrl('index'))->assertForbidden();
$page = $this->site->pages()->where('slug', 'kontakt')->sole();
$this->get(PageResource::getUrl('edit', ['record' => $page]))->assertOk();
Livewire::test(EditPage::class, ['record' => $page->getRouteKey()])
->set('data.title', 'Kontakt & Anfahrt')
->call('save')
->assertHasNoFormErrors();
$this->assertSame('Kontakt & Anfahrt', $page->fresh()->title);
Livewire::test(CreateAppearance::class)
->fillForm([
'name' => 'Ostern',
'site_id' => $this->site->id,
'starts_md' => '28.3.',
'ends_md' => '12.04.',
'priority' => 15,
'is_active' => true,
'overrides' => ['hero' => ['title' => 'Frühling an der Ostsee']],
'tokens' => ['accent' => '#5A7D3A'],
])
->call('create')
->assertHasNoFormErrors();
$easter = Appearance::where('name', 'Ostern')->sole();
$this->assertSame(['03-28', '04-12', 'Frühling an der Ostsee', '#5A7D3A'], [$easter->starts_md, $easter->ends_md, $easter->override('hero.title'), $easter->tokens['accent']]);
$this->assertSame('Ostern', $this->site->appearanceOn(CarbonImmutable::parse('2027-04-01'))->name);
}
public function test_placed_sections_get_stable_ids_and_copies_get_new_ones(): void
{
$page = $this->site->pages()->where('slug', 'start')->sole();
$ids = array_map(fn (array $section): string => $section['data']['id'], $page->sections);
$this->assertCount(count($page->sections), array_unique($ids));
$page->update(['sections' => [...$page->sections, $page->sections[0]]]);
$this->assertSame($ids, array_slice(array_map(fn (array $section): string => $section['data']['id'], $page->fresh()->sections), 0, count($ids)));
$this->assertNotSame($ids[0], last($page->fresh()->sections)['data']['id']);
}
public function test_section_can_be_limited_to_an_appearance(): void
{
$christmas = $this->site->appearances()->where('name', 'Weihnachten')->sole();
$page = $this->site->pages()->where('slug', 'start')->sole();
$page->update(['sections' => [...$page->sections, ['type' => 'text_image', 'data' => ['heading' => 'Adventsmarkt am Hafen', 'appearances' => [(string) $christmas->id]]]]]);
$this->get('/')->assertOk()->assertDontSee('Adventsmarkt am Hafen');
$this->travelTo(CarbonImmutable::parse('2026-12-20 10:00'));
$this->get('/')->assertOk()->assertSee('Adventsmarkt am Hafen');
}
public function test_seasonal_photos_appear_only_in_their_appearances_and_come_first(): void
{
Storage::fake('public');
$winter = $this->site->appearances()->where('name', 'Winter')->sole();
$christmas = $this->site->appearances()->where('name', 'Weihnachten')->sole();
$allYear = $this->photoWithFile(['alt_text' => 'Garten im Licht', 'sort_order' => 1]);
$snow = $this->photoWithFile(['alt_text' => 'Raue See im Winter', 'sort_order' => 99, 'appearance_ids' => [$winter->id, $christmas->id]]);
$page = $this->site->pages()->where('slug', 'start')->sole();
$page->update(['sections' => collect($page->sections)->map(fn (array $section): array => $section['type'] === 'cta'
? [...$section, 'data' => [...$section['data'], 'photo_ids' => [$snow->id, $allYear->id]]]
: $section)->all()]);
$apartmentGallery = fn () => $this->get('/wohnung')->assertOk();
$this->get('/')->assertOk()->assertSee('Garten im Licht')->assertDontSee('Raue See im Winter');
$apartmentGallery()->assertDontSee('Raue See im Winter');
$this->travelTo(CarbonImmutable::parse('2026-11-15 10:00'));
$this->get('/')->assertOk()->assertSee('Raue See im Winter')->assertDontSee('Garten im Licht');
$apartmentGallery()->assertSeeInOrder(['Raue See im Winter', 'Garten im Licht']);
$this->travelTo(CarbonImmutable::parse('2026-12-20 10:00'));
$this->get('/')->assertOk()->assertSee('Raue See im Winter');
}
public function test_photo_belongs_to_several_appearances_and_the_appearance_counts_its_photos(): void
{
$this->actingAs(User::factory()->role(UserRole::Content)->create());
$winter = $this->site->appearances()->where('name', 'Winter')->sole();
$christmas = $this->site->appearances()->where('name', 'Weihnachten')->sole();
Storage::fake('public');
$photo = $this->photoWithFile(['alt_text' => 'Strand im Schnee']);
Livewire::test(EditPhoto::class, ['record' => $photo->getRouteKey()])
->fillForm(['appearance_ids' => [(string) $winter->id, (string) $christmas->id]])
->call('save')
->assertHasNoFormErrors();
$this->assertSame([$winter->id, $christmas->id], $photo->fresh()->appearance_ids);
$this->assertSame(['Winter', 'Weihnachten'], $photo->fresh()->appearanceNames());
$this->get(AppearanceResource::getUrl('edit', ['record' => $winter]))
->assertOk()
->assertSee('1 Foto ist diesem Erscheinungsbild zugeordnet.')
->assertDontSee('Bilder je Baustein');
Livewire::test(ListPhotos::class)
->callTableBulkAction('seasons', [$photo], ['appearance_ids' => []])
->assertHasNoTableBulkActionErrors();
$this->assertNull($photo->fresh()->appearance_ids);
}
public function test_appearance_photos_move_to_the_photos_and_sections(): void
{
$migration = require database_path('migrations/2026_10_29_184538_give_photos_their_seasons.php');
$migration->down();
$standard = $this->site->appearances()->where('is_default', true)->sole();
$winter = $this->site->appearances()->where('name', 'Winter')->sole();
$title = Photo::factory()->create();
$snow = Photo::factory()->create();
$closing = Photo::factory()->create();
$page = $this->site->pages()->where('slug', 'start')->sole();
$cta = collect($page->sections)->firstWhere('type', 'cta');
$page->update(['sections' => collect($page->sections)->map(fn (array $section): array => $section['type'] === 'cta'
? [...$section, 'data' => [...$section['data'], 'photo_id' => $closing->id]]
: $section)->all()]);
DB::table('appearances')->where('id', $standard->id)->update(['hero_photo_id' => $title->id]);
DB::table('appearances')->where('id', $winter->id)->update(['section_photos' => json_encode([$cta['data']['id'] => ['photo_id' => $snow->id]])]);
$migration->up();
$sections = collect($page->fresh()->sections);
$this->assertSame([$title->id], $sections->firstWhere('type', 'hero')['data']['photo_ids']);
$this->assertSame([$snow->id, $closing->id], $sections->firstWhere('type', 'cta')['data']['photo_ids']);
$this->assertArrayNotHasKey('photo_id', $sections->firstWhere('type', 'cta')['data']);
$this->assertNull($title->fresh()->appearance_ids);
$this->assertSame([$winter->id], $snow->fresh()->appearance_ids);
}
/**
* @param array<string, mixed> $attributes
*/
private function photoWithFile(array $attributes): Photo
{
$photo = Photo::factory()->create($attributes);
$photo->addMedia(UploadedFile::fake()->image('foto.jpg', 1600, 1000))->toMediaCollection(Photo::COLLECTION);
return $photo;
}
public function test_invalid_day_is_rejected(): void
{
$this->actingAs(User::factory()->create());
Livewire::test(CreateAppearance::class)
->fillForm(['name' => 'Kaputt', 'site_id' => $this->site->id, 'starts_md' => '31.02.', 'ends_md' => '01.03.'])
->call('create')
->assertHasFormErrors(['starts_md']);
}
}