Files
laraundfelix/tests/Feature/CmsWidgetRenderingTest.php
T
LarsandClaude Opus 5.5 0bfb53463a
CI Feature / test (push) Successful in 41s
Deploy Test / test (push) Successful in 37s
Deploy Test / deploy (push) Successful in 33s
Führe einen gemeinsamen Layoutvertrag ein und integriere die Premium-Widgets.
Layoutvertrag (ADR-38) in der bestehenden Registry: Jede Widget-Art
erklärt zulässige Rasterbreiten (supportedUnits), Höhen aus einem
gemeinsamen Vokabular (heights, gespeichert als settings.height),
Darstellungsmodi und Verhalten bei wenig Platz. Unzulässige Breiten
werden beim Speichern verständlich abgewiesen; Höhe „fill“ streckt
den Handlungsaufruf auf die Abschnittszeile.

Premium-Widgets nach Brand-UI-Kit v1.6:
- hero Version 2: photo_split/editorial_solid, Höhen compact/normal/
  large, Label, Überschriftenebene, Akzent, Bildfokus in Prozent, zwei
  Schaltflächen; nur 3 oder 4 Einheiten. Upgrade aus Version 1.
- image_text Version 3: Fläche cream/clear/navy, Höhen compact/normal/
  spacious, Label, zwei Schaltflächen, Bildfokus; 1 bis 4 Einheiten.
  Upgrade aus Version 2 (format entfällt, Fokus in Prozent).
- Kit-CSS ersetzt das bisherige Hero- und Bild-und-Text-CSS; der
  Randlos-Mechanismus der Designiteration 1 entfällt.

Editor: Reiter Inhalt/Medien/Darstellung aus der Registry, Auswahl-
knöpfe mit Symbolen, Schieberegler für den Bildfokus und eine Live-
Vorschau im Dialog mit demselben Renderer in wählbarer Platzbreite.

Tests bewusst angepasst: Einstieg braucht jetzt 3 oder 4 Einheiten
(vorher Mindestbreite 2), image_text ist Version 3, Klassen der neuen
Ansichten; die Hero-/Bild-Tests der Designiteration 1 sind durch
CmsPremiumWidgetsTest ersetzt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:20:09 +01:00

290 lines
14 KiB
PHP
Raw 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\Domain\Catalog\CompositionDisplayRelease;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Filament\Resources\Pages\Pages\EditPage;
use App\Jobs\PublishPublicMedia;
use App\Models\User;
use Database\Seeders\CmsSeeder;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Illuminate\Validation\ValidationException;
use Livewire\Livewire;
use ReflectionMethod;
use Tests\Support\BuildsCatalog;
use Tests\TestCase;
class CmsWidgetRenderingTest extends TestCase
{
use BuildsCatalog;
use RefreshDatabase;
/**
* Die Textfolge jeder Bestandsseite entspricht der Darstellung vor Einführung der Registry
* (tests/Fixtures/legacy-pages, erzeugt aus dem Stand develop@cfdd6de).
*/
public function test_existing_pages_render_the_same_text_as_before_the_registry(): void
{
$this->storeSeededPagesInTheLegacyFormat();
$this->assertSame('heading', Page::query()->where('path', '/')->first()->publishedRevision->blocks[0]['type']);
$this->assertArrayNotHasKey('v', Page::query()->where('path', '/')->first()->publishedRevision->blocks[0]);
foreach (self::LEGACY_PAGES as $path => $fixture) {
$this->assertSame($this->fixture($fixture), $this->mainText($this->get($path)->assertOk()->getContent()), "Abweichung auf $path");
}
}
/**
* Rundlauf: eine Seite im Altformat im Editor öffnen, speichern, veröffentlichen – der Text bleibt gleich.
*/
public function test_legacy_page_survives_a_round_trip_through_the_editor(): void
{
$this->storeSeededPagesInTheLegacyFormat();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
foreach (['/' => 'home', '/praevention' => 'praevention'] as $path => $fixture) {
$page = Page::query()->where('path', $path)->firstOrFail();
Livewire::actingAs($editor, 'staff')
->test(EditPage::class, ['record' => $page->id])
->call('save')
->assertHasNoErrors()
->callAction('publish');
$live = $page->fresh()->publishedRevision;
$this->assertArrayHasKey('v', $live->blocks[0], 'Die neue Revision ist im Hüllenformat gespeichert.');
$this->assertSame($this->fixture($fixture), $this->mainText($this->get($path)->getContent()), "Abweichung nach Rundlauf auf $path");
}
}
private const LEGACY_PAGES = ['/' => 'home', '/praevention' => 'praevention', '/materialien' => 'materialien', '/angebote' => 'angebote', '/trainer' => 'trainer', '/ueber' => 'ueber', '/impressum' => 'impressum', '/datenschutz' => 'datenschutz'];
/**
* Auf Test und Produktion liegen die Seiten im flachen Format vor der Registry. Die Migration ruft den
* Seeder heute über die Registry auf; deshalb werden die veröffentlichten Revisionen hier mit den
* unveränderten Seederdaten im Altformat überschrieben.
*/
private function storeSeededPagesInTheLegacyFormat(): void
{
$method = new ReflectionMethod(CmsSeeder::class, 'pages');
$pages = $method->invoke(new CmsSeeder);
foreach ($pages as $path => $page) {
$revisionId = Page::query()->where('path', $path)->value('published_revision_id');
DB::table('page_revisions')->where('id', $revisionId)->update(['blocks' => json_encode($page['blocks'])]);
}
}
private function fixture(string $name): string
{
return trim(file_get_contents(base_path("tests/Fixtures/legacy-pages/$name.txt")));
}
public function test_legacy_revisions_stored_in_the_flat_format_still_render(): void
{
$page = Page::query()->create(['path' => '/alt']);
$revision = $page->revisions()->create([
'blocks' => [
['type' => 'heading', 'heading' => 'Alte Seite', 'intro' => 'Einleitung'],
['type' => 'image_text', 'image' => 'cms/alt.jpg', 'alt' => 'Altes Bild', 'body' => "Absatz eins.\n\nAbsatz <zwei>."],
['type' => 'faq', 'items' => [['question' => 'Frage?', 'answer' => 'Antwort.']]],
['type' => 'cards', 'items' => [['title' => 'Karte', 'text' => 'Text', 'url' => '/praevention']]],
['type' => 'call_to_action', 'heading' => 'Los', 'text' => null, 'label' => 'Anmelden', 'url' => '/anmelden'],
],
]);
app(PageService::class)->publish($page, $revision);
$this->get('/alt')
->assertOk()
->assertSee('<title>Alte Seite</title>', false)
->assertSee('<h1>Alte Seite</h1>', false)
->assertSee('alt="Altes Bild"', false)
->assertSee('<p>Absatz eins.</p><p>Absatz &lt;zwei&gt;.</p>', false)
->assertSee('<dt>Frage?</dt>', false)
->assertSee('href="/praevention"', false)
->assertSee('href="/anmelden"', false);
$this->assertSame('heading', $revision->fresh()->blocks[0]['type']);
$this->assertArrayNotHasKey('v', $revision->fresh()->blocks[0], 'Die gespeicherte Altrevision bleibt unverändert.');
}
public function test_restoring_a_legacy_revision_keeps_its_stored_blocks(): void
{
$page = Page::query()->where('path', '/')->firstOrFail();
$legacy = $page->publishedRevision;
$service = app(PageService::class);
$service->saveRevision($page, [['type' => 'heading', 'v' => 1, 'data' => ['heading' => 'Neu']]], null, null, null);
$restored = $service->restore($page, $legacy, null);
$this->assertSame($legacy->blocks, $restored->blocks);
}
public function test_new_revisions_are_stored_as_widget_envelopes(): void
{
$page = Page::query()->create(['path' => '/neu']);
$revision = app(PageService::class)->saveRevision($page, [
['type' => 'prose', 'body' => 'Altes Eingabeformat'],
['type' => 'hero', 'v' => 1, 'variant' => 'highlight', 'data' => ['heading' => 'Einstieg']],
], null, null, null);
$this->assertSame(['id', 'type', 'v', 'variant', 'settings', 'data', 'refs'], array_keys($revision->blocks[0]));
$this->assertSame('prose', $revision->blocks[0]['type']);
$this->assertSame(['body' => 'Altes Eingabeformat'], $revision->blocks[0]['data']);
// Gespeichert wird immer die aktuelle Version: hero v1 „highlight“ wird zu v2 „editorial_solid“.
$this->assertSame([2, 'editorial_solid'], [$revision->blocks[1]['v'], $revision->blocks[1]['variant']]);
$this->assertNull($page->fresh()->published_revision_id, 'Speichern veröffentlicht nicht.');
}
public function test_reference_widgets_render_their_variants(): void
{
$page = Page::query()->create(['path' => '/varianten']);
$revision = app(PageService::class)->saveRevision($page, [
['type' => 'hero', 'v' => 1, 'variant' => 'highlight', 'data' => ['heading' => 'Stark im Alltag', 'text' => 'Einstieg', 'cta_label' => 'Mehr', 'cta_url' => '/praevention']],
['type' => 'image_text', 'v' => 2, 'variant' => 'image_right', 'data' => ['title' => 'Bild rechts', 'body' => '<p>Text <em>betont</em></p>', 'alt' => 'Beschreibung'], 'refs' => ['image' => 'cms/beispiel.jpg']],
['type' => 'cards', 'v' => 1, 'variant' => 'compact', 'data' => ['items' => [['title' => 'Eins', 'url' => '/a'], ['title' => 'Zwei']]]],
], null, null, null);
app(PageService::class)->publish($page, $revision);
$this->get('/varianten')
->assertOk()
->assertSee('lf-hero lf-hero--editorial-solid lf-hero--height-normal', false)
->assertSee('<h1 class="lf-hero__title">Stark im Alltag</h1>', false)
->assertSee('class="lf-hero__button lf-hero__button--primary" href="/praevention"', false)
->assertSee('lf-bt lf-bt--cream lf-bt--image-right lf-bt--height-normal', false)
->assertSee('<h2 class="lf-bt__headline">Bild rechts</h2>', false)
->assertSee('<em>betont</em>', false)
->assertSee(route('media.public', ['path' => 'cms/beispiel.jpg']), false)
->assertSee('cards cards-compact', false);
}
public function test_media_of_any_widget_are_published_through_the_queue(): void
{
Queue::fake();
$page = Page::query()->create(['path' => '/medien']);
app(PageService::class)->saveRevision($page, [
['type' => 'hero', 'v' => 1, 'data' => ['heading' => 'A', 'alt' => 'Bild'], 'refs' => ['image' => 'cms/hero.jpg']],
['type' => 'cards', 'v' => 1, 'data' => ['items' => [['title' => 'K', 'image' => 'cms/karte.jpg']]]],
], null, null, null);
Queue::assertPushed(PublishPublicMedia::class, fn ($job) => $job->path === 'cms/hero.jpg');
Queue::assertPushed(PublishPublicMedia::class, fn ($job) => $job->path === 'cms/karte.jpg');
}
public function test_catalog_cards_show_released_programs_from_the_master_data_only(): void
{
$program = $this->composition('Programm Abstand', 'programm', summary: 'Kurzbeschreibung aus den Fachdaten');
$module = $this->composition('Modul Nein sagen', 'modul');
$page = Page::query()->create(['path' => '/thema']);
$revision = app(PageService::class)->saveRevision($page, [[
'type' => 'catalog_cards',
'v' => 1,
'data' => ['heading' => 'Passende Angebote'],
'refs' => ['items' => [
['type' => 'composition', 'id' => $module->composition_id],
['type' => 'composition', 'id' => $program->composition_id],
]],
]], null, null, null);
app(PageService::class)->publish($page, $revision);
$this->assertSame(
['items' => [['type' => 'composition', 'id' => $module->composition_id], ['type' => 'composition', 'id' => $program->composition_id]]],
$revision->blocks[0]['refs'],
);
$this->assertStringNotContainsString('Programm Abstand', json_encode($revision->blocks), 'Keine Stammdaten im CMS.');
$this->get('/thema')
->assertOk()
->assertSee('Passende Angebote')
->assertSee('Programm Abstand')
->assertSee('Kurzbeschreibung aus den Fachdaten')
->assertSee(route('public.program', $program->composition_id), false)
->assertDontSee('Modul Nein sagen')
->assertDontSee('Redaktionsvorschau');
$program->composition->update(['display_title' => 'Programm „Abstand halten“']);
$this->get('/thema')->assertSee('Programm „Abstand halten“');
}
public function test_catalog_cards_show_modules_only_in_the_protected_preview(): void
{
$module = $this->composition('Modul Nein sagen', 'modul');
$page = Page::query()->create(['path' => '/modulseite']);
$revision = app(PageService::class)->saveRevision($page, [
['type' => 'heading', 'v' => 1, 'data' => ['heading' => 'Module']],
['type' => 'catalog_cards', 'v' => 1, 'data' => [], 'refs' => ['items' => [['type' => 'composition', 'id' => $module->composition_id]]]],
], null, null, null);
app(PageService::class)->publish($page, $revision);
$this->get('/modulseite')->assertOk()->assertDontSee('Modul Nein sagen')->assertDontSee('catalog-cards');
$editor = User::factory()->redakteur()->create();
$this->actingAs($editor, 'staff')
->get(route('pages.preview', $revision))
->assertOk()
->assertSee('Modul Nein sagen')
->assertSee('Nur in der Redaktionsvorschau sichtbar');
}
public function test_revoking_the_display_release_removes_the_card_without_editing_the_page(): void
{
$program = $this->composition('Programm Rückzug', 'programm');
$page = Page::query()->create(['path' => '/rueckzug']);
$revision = app(PageService::class)->saveRevision($page, [
['type' => 'heading', 'v' => 1, 'data' => ['heading' => 'Rückzug']],
['type' => 'catalog_cards', 'v' => 1, 'refs' => ['items' => [['type' => 'composition', 'id' => $program->composition_id]]]],
], null, null, null);
app(PageService::class)->publish($page, $revision);
$this->get('/rueckzug')->assertSee('Programm Rückzug');
$program->displayEvents()->update(['occurred_at' => now()->subMinute()]);
app(CompositionDisplayRelease::class)->revoke($program, User::factory()->create());
$this->get('/rueckzug')->assertOk()->assertDontSee('Programm Rückzug');
}
public function test_unreleased_or_foreign_objects_cannot_be_referenced(): void
{
$draft = $this->composition('Programm Entwurf', 'programm', released: false);
$page = Page::query()->create(['path' => '/entwurf']);
$this->expectException(ValidationException::class);
app(PageService::class)->saveRevision($page, [
['type' => 'catalog_cards', 'v' => 1, 'refs' => ['items' => [['type' => 'composition', 'id' => $draft->composition_id]]]],
], null, null, null);
}
public function test_public_program_list_uses_the_same_public_rule(): void
{
$this->composition('Programm Öffentlich', 'programm');
$this->composition('Programm Intern', 'programm', released: false);
$this->composition('Modul Freigegeben', 'modul');
$this->get('/programme')
->assertOk()
->assertSee('Programm Öffentlich')
->assertDontSee('Programm Intern')
->assertDontSee('Modul Freigegeben');
}
private function mainText(string $html): string
{
preg_match('#<main[^>]*>(.*)</main>#s', $html, $match);
$text = html_entity_decode(strip_tags(str_replace('<', ' <', $match[1] ?? '')), ENT_QUOTES | ENT_HTML5, 'UTF-8');
return trim(preg_replace('/\s+/u', ' ', $text));
}
}