Files
laraundfelix/tests/Feature/CmsPremiumCardsTest.php
T
LarsandClaude Opus 5.5 bc1f84ac89
CI Feature / test (push) Successful in 43s
Deploy Test / test (push) Successful in 40s
Deploy Test / deploy (push) Successful in 34s
Zeige von Medien keine Kurzbeschreibung und begrenze die Kartenbildhöhe.
- Medienkarten nennen öffentlich nur Titel, Art und Programmzugehörigkeit; die Kurzbeschreibung eines
  Elements ist heute nirgends öffentlich (offene Entscheidung, ADR-39).
- PublicMaterialReader je Anfrage einmal (scoped), nicht je Karte.
- Kartenbild höchstens min(70vh, 640 px), damit breite gestapelte Karten nicht übermäßig hoch werden.
- Bericht mit Prüfungen, Grenzen und dem offenen Bedarf einer kaufbaren Angebotskarte.

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

386 lines
22 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\CompositionDisplayRelease;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\EditorialContentKind;
use App\Domain\Catalog\Pillar;
use App\Domain\Catalog\PillarService;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageRevision;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Widgets\WidgetRegistry;
use App\Filament\Resources\Pages\Pages\CreatePage;
use App\Filament\Resources\Pages\Pages\EditPage;
use App\Filament\Support\WidgetEditor;
use App\Models\User;
use Filament\Actions\Testing\TestAction;
use Filament\Facades\Filament;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\ToggleButtons;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Livewire\Livewire;
use Tests\Support\BuildsCatalog;
use Tests\TestCase;
/**
* Premium-Karten v1.7 (Themen-, Medien- und Programmkarte): Fachbezug ohne Kopien, Freigaberegeln,
* keine Bibliotheksbilder, kein Preis und keine Kaufaktion, Bearbeitung über den regulären Seiteneditor.
*/
class CmsPremiumCardsTest extends TestCase
{
use BuildsCatalog;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
}
public function test_the_three_card_types_are_registered_with_the_layout_contract(): void
{
$registry = app(WidgetRegistry::class);
foreach (['topic_card' => 'pillar', 'media_card' => 'element', 'program_card' => 'composition'] as $key => $ref) {
$type = $registry->get($key);
$this->assertSame([1, 2, 3, 4], $type->supportedUnits(), $key);
$this->assertSame(['auto', 'fill'], array_keys($type->heights()), $key);
$this->assertSame(['adaptive', 'stacked'], array_keys($type->variants()), $key);
$this->assertSame('widgets.premium_card', $type->view());
$this->assertArrayHasKey($ref, $type->refRules());
$this->assertArrayNotHasKey('url', $type->dataRules(), 'Kein freies Linkziel.');
$this->assertArrayNotHasKey('title', $type->dataRules(), 'Kein eigener Titel: der Titel kommt aus dem Fachobjekt.');
}
$this->assertArrayNotHasKey('show_kind', $registry->get('program_card')->settingOptions(), 'Programm oder Modul bleibt immer benannt.');
$this->assertArrayNotHasKey('show_summary', $registry->get('media_card')->settingOptions(), 'Medienkarten zeigen keine Kurzbeschreibung.');
}
public function test_topic_card_shows_a_published_theme_from_the_master_data_only(): void
{
$pillar = $this->pillar('Gefühle verstehen', 'gefuehle-verstehen', 'Geschichten und Impulse, die Kinder ins Gespräch bringen.');
$revision = $this->publish('/themenkarte', [$this->section('u2_2', [
$this->card('topic_card', ['pillar' => $pillar->id], settings: ['heading_level' => 'h2', 'show_summary' => 'yes']),
$this->card('topic_card', ['pillar' => $pillar->id], data: ['action_label' => 'Mehr zum Thema'], settings: ['show_summary' => 'no', 'show_kind' => 'no']),
])]);
$this->assertSame(['pillar' => $pillar->id, 'image' => null], $revision->sections[0]['slots'][0]['refs']);
$this->assertStringNotContainsString('Gefühle verstehen', json_encode($revision->sections), 'Keine Kopie des Themennamens im CMS.');
$html = $this->get('/themenkarte')->assertOk()->getContent();
$this->assertStringContainsString('<h2 class="lf-pc__title">Gefühle verstehen</h2>', $html);
$this->assertStringContainsString('<h3 class="lf-pc__title">Gefühle verstehen</h3>', $html);
$this->assertSame(1, substr_count($html, 'Geschichten und Impulse'), 'Kurzbeschreibung nur, wo sie angezeigt werden soll.');
$this->assertStringContainsString('href="'.route('theme', ['slug' => 'gefuehle-verstehen']).'">Thema entdecken</a>', $html);
$this->assertStringContainsString('>Mehr zum Thema</a>', $html);
$this->assertSame(1, substr_count($html, '<p class="lf-pc__kind">Thema</p>'));
$pillar->update(['name' => 'Gefühle verstehen und benennen']);
$this->get('/themenkarte')->assertSee('Gefühle verstehen und benennen');
$pillar->update(['published_at' => null]);
$this->get('/themenkarte')->assertOk()->assertDontSee('lf-pc', false)->assertDontSee('Gefühle verstehen');
}
public function test_program_card_names_its_kind_and_offers_no_price_or_purchase(): void
{
$module = $this->composition('Modul Nein sagen', 'modul', summary: 'Ein Modul');
$program = $this->composition('Programm Selbstvertrauen', 'programm', summary: 'Ein roter Faden für die Kursgestaltung.');
$revision = $this->publish('/programmkarte', [$this->section('u2_2', [
$this->card('program_card', ['composition' => $program->composition_id]),
$this->card('program_card', ['composition' => $module->composition_id]),
])]);
$html = $this->get('/programmkarte')->assertOk()->getContent();
$this->assertStringContainsString('<p class="lf-pc__kind">Programm</p>', $html);
$this->assertStringContainsString('<span class="lf-pc__panel-label">Programm</span>', $html, 'Ohne Bild trägt die Navy-Tafel die Art.');
$this->assertStringContainsString('href="'.route('public.program', $program->composition_id).'">Programm ansehen</a>', $html);
$this->assertStringContainsString('Ein roter Faden', $html);
$this->assertStringNotContainsString('Modul Nein sagen', $html, 'Module erscheinen öffentlich nicht.');
$this->assertStringNotContainsString('Modul / Programm', $html);
foreach (['€', 'EUR', 'Preis', 'kaufen', 'Warenkorb', '/bereich/angebote', 'offers'] as $commerce) {
$this->assertStringNotContainsString($commerce, $this->mainHtml($html), "Keine Kaufaktion: $commerce");
}
$preview = $this->actingAs(User::factory()->redakteur()->create(), 'staff')->get(route('pages.preview', $revision))->assertOk()->getContent();
$this->assertStringContainsString('<p class="lf-pc__kind">Modul</p>', $preview);
$this->assertStringContainsString('Nur in der Redaktionsvorschau sichtbar: Für Module gibt es noch keine öffentliche Seitenart', $preview);
$this->assertStringContainsString('lf-pc--internal', $preview);
}
public function test_program_card_lists_the_number_of_modules_of_the_shown_version(): void
{
$content = app(ContentService::class);
$moduleA = $this->composition('Modul Eins', 'modul');
$moduleB = $this->composition('Modul Zwei', 'modul');
$composition = $content->createComposition('Programm mit Modulen');
$composition->editorial_content_kind_id = EditorialContentKind::query()->where('slug', 'programm')->value('id');
$composition->save();
$version = $content->publishComposition($composition, [
['role' => 'Modul', 'target' => $moduleA],
['role' => 'Modul', 'target' => $moduleB],
]);
app(CompositionDisplayRelease::class)->release($version, User::factory()->create());
$this->publish('/programm-module', [$this->section('u4', [$this->card('program_card', ['composition' => $composition->id])])]);
$this->get('/programm-module')->assertOk()->assertSee('<li>2 Module</li>', false);
}
public function test_media_card_is_public_only_as_part_of_a_public_program_and_never_releases_the_medium(): void
{
$content = app(ContentService::class);
$film = $content->createElement('film', 'film', 'Denkpause Rohschnitt');
$film->update(['display_title' => 'Eine Pause, die stark macht', 'summary' => 'Gemeinsam innehalten und über gute nächste Schritte sprechen.', 'thumbnail_path' => 'vorschau/denkpause.jpg']);
$filmVersion = $content->publishElementVersion($film, 'Filmbeschreibung');
$sheet = $content->createElement('text', 'pdf', 'Arbeitsblatt intern');
$sheet->update(['editorial_content_kind_id' => EditorialContentKind::query()->where('slug', 'merkblatt')->value('id')]);
$content->publishElementVersion($sheet, 'Inhalt');
$composition = $content->createComposition('Programm Denkpause');
$composition->editorial_content_kind_id = EditorialContentKind::query()->where('slug', 'programm')->value('id');
$composition->save();
$version = $content->publishComposition($composition, [['role' => 'Film', 'target' => $filmVersion]]);
app(CompositionDisplayRelease::class)->release($version, User::factory()->create());
$revision = $this->publish('/medienkarte', [$this->section('u2_2', [
$this->card('media_card', ['element' => $film->id], data: ['note' => 'Für die nächste Kursstunde.']),
$this->card('media_card', ['element' => $sheet->id]),
])]);
$html = $this->get('/medienkarte')->assertOk()->getContent();
$main = $this->mainHtml($html);
$this->assertStringContainsString('Eine Pause, die stark macht', $main);
$this->assertStringContainsString('<p class="lf-pc__kind">Film</p>', $main);
$this->assertStringContainsString('<li>Im Programm „Programm Denkpause“</li>', $main);
$this->assertStringContainsString('href="'.route('public.program', $composition->id).'">Im Programm ansehen</a>', $main);
$this->assertStringContainsString('Für die nächste Kursstunde.', $main);
$this->assertStringNotContainsString('Gemeinsam innehalten', $main, 'Die Kurzbeschreibung eines Elements ist nirgends öffentlich und bleibt aus.');
$this->assertStringNotContainsString('Arbeitsblatt intern', $main, 'Nicht Teil eines öffentlichen Programms: öffentlich nicht sichtbar.');
foreach (['vorschau/denkpause.jpg', '/bereich/', '/redaktion/', '/ausgabe/', '<video', 'download', 'Filmbeschreibung'] as $protected) {
$this->assertStringNotContainsString($protected, $main, "Keine geschützte Ausgabe über die Karte: $protected");
}
$preview = $this->actingAs(User::factory()->redakteur()->create(), 'staff')->get(route('pages.preview', $revision))->assertOk()->getContent();
$this->assertStringContainsString('Arbeitsblatt intern', $preview);
$this->assertStringContainsString('<p class="lf-pc__kind">Merkblatt</p>', $preview);
$this->assertStringContainsString('Dieses Medium gehört zu keinem öffentlich gezeigten Programm', $preview);
$film->update(['archived_at' => now()]);
$this->get('/medienkarte')->assertOk()->assertDontSee('Eine Pause, die stark macht');
}
public function test_cards_reject_unpublished_or_unreleased_objects_and_require_alt_text_for_images(): void
{
Storage::fake('media_public');
Storage::disk('media_public')->put('cms/karte.jpg', 'bild');
$hidden = Pillar::query()->create(['name' => 'Entwurfsthema', 'slug' => 'entwurf']);
$draft = $this->composition('Programm Entwurf', 'programm', released: false);
$archived = app(ContentService::class)->createElement('film', 'film', 'Ohne Fassung');
$published = $this->pillar('Grenzen', 'grenzen', null);
$cases = [
[$this->card('topic_card', ['pillar' => $hidden->id]), 'refs.pillar'],
[$this->card('program_card', ['composition' => $draft->composition_id]), 'refs.composition'],
[$this->card('media_card', ['element' => $archived->id]), 'refs.element'],
[$this->card('topic_card', ['pillar' => $published->id, 'image' => 'cms/karte.jpg']), 'data.alt'],
[$this->card('topic_card', ['pillar' => $published->id], settings: ['format' => 'panorama']), 'settings.format'],
[$this->card('topic_card', []), 'refs.pillar'],
];
foreach ($cases as [$card, $key]) {
try {
app(PageService::class)->saveRevision(Page::query()->create(['path' => '/fehler-'.Str::random(6)]), [$this->section('u4', [$card])], null, null, null);
$this->fail("Erwarteter Fehler: $key");
} catch (ValidationException $exception) {
$this->assertArrayHasKey("sections.0.slots.0.$key", $exception->errors(), json_encode($exception->errors()));
}
}
}
public function test_mixed_card_grids_use_the_section_grid_with_equal_height_and_image_options(): void
{
Storage::fake('media_public');
Storage::disk('media_public')->put('cms/karte.jpg', 'bild');
$pillar = $this->pillar('Hilfe holen', 'hilfe-holen', str_repeat('Ein sehr langer Text, der umbrechen muss. ', 12));
$program = $this->composition('Programm Lang', 'programm', summary: 'Kurz');
$this->publish('/kartenraster', [
$this->section('u1_1_2', [
$this->card('topic_card', ['pillar' => $pillar->id, 'image' => 'cms/karte.jpg'], data: ['alt' => 'Lara und Felix im Park', 'focus_x' => 30, 'focus_y' => 70], settings: ['height' => 'fill', 'format' => 'portrait', 'fit' => 'contain']),
$this->card('program_card', ['composition' => $program->composition_id], settings: ['height' => 'fill']),
$this->card('topic_card', ['pillar' => $pillar->id], variant: 'stacked'),
]),
$this->section('u2_2', [
$this->card('program_card', ['composition' => $program->composition_id, 'image' => 'cms/karte.jpg'], data: ['alt' => 'Kursraum']),
$this->card('topic_card', ['pillar' => $pillar->id], settings: ['show_action' => 'no', 'show_meta' => 'no']),
]),
]);
$html = $this->get('/kartenraster')->assertOk()->getContent();
$this->assertStringContainsString('section-layout-u1-1-2', $html);
$this->assertStringContainsString('--section-cols-lg: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 2fr)', $html);
$this->assertStringNotContainsString('lf-pc-grid', $html, 'Kein zweites Kartenraster.');
$this->assertSame(2, substr_count($html, 'widget-height-fill'));
$this->assertStringContainsString('lf-pc lf-pc--editorial lf-pc--portrait lf-pc--contain', $html);
$this->assertStringContainsString('--pc-focus-x: 30%; --pc-focus-y: 70%', $html);
$this->assertStringContainsString('alt="Lara und Felix im Park"', $html);
$this->assertStringContainsString('lf-pc--stacked lf-pc--without-visual', $html);
$this->assertStringContainsString('lf-pc lf-pc--catalog lf-pc--landscape lf-pc--cover"', $html, 'Programmkarte mit Bild zeigt das Bild statt der Tafel.');
$this->assertSame(2, substr_count($html, 'Thema entdecken'), 'Drei Themenkarten, auf einer ist die Aktion ausgeblendet.');
$this->assertStringContainsString(str_repeat('Ein sehr langer Text, der umbrechen muss. ', 11), html_entity_decode($html), 'Lange Texte werden nicht gekürzt.');
}
public function test_editor_inserts_configures_saves_and_reopens_all_three_cards(): void
{
Storage::fake('media_public');
Storage::fake('cms_incoming');
Storage::disk('media_public')->put('cms/karte.jpg', 'bild');
$pillar = $this->pillar('Gefühle', 'gefuehle', 'Kurz');
$program = $this->composition('Programm Abstand', 'programm');
$element = ContentElement::query()->where('working_title', 'Programm Abstand Blatt')->firstOrFail();
Livewire::actingAs($this->editor(), 'staff')
->test(CreatePage::class)
->fillForm([
'path' => '/karten-editor',
'sections' => [
$this->sectionState('u1_1_2', [
['type' => 'topic_card', 'data' => ['variant' => 'adaptive', 'settings' => ['height' => 'fill', 'format' => 'square', 'heading_level' => 'h2'], 'data' => ['action_label' => 'Zum Thema', 'alt' => 'Park', 'focus_x' => 20, 'focus_y' => 80], 'refs' => ['pillar' => $pillar->id, 'image' => 'cms/karte.jpg']]],
['type' => 'media_card', 'data' => ['variant' => 'stacked', 'settings' => ['height' => 'fill', 'show_meta' => 'no'], 'data' => ['note' => 'Hinweis'], 'refs' => ['element' => $element->id]]],
['type' => 'program_card', 'data' => ['variant' => 'adaptive', 'settings' => ['height' => 'fill', 'fit' => 'contain'], 'data' => [], 'refs' => ['composition' => $program->composition_id]]],
]),
],
])
->call('create')
->assertHasNoFormErrors();
$page = Page::query()->where('path', '/karten-editor')->firstOrFail();
$this->assertNull($page->published_revision_id, 'Entwurf, nicht veröffentlicht.');
[$topic, $media, $programCard] = $page->latestRevision->sections[0]['slots'];
$this->assertSame(['topic_card', 'adaptive', ['height' => 'fill', 'format' => 'square', 'heading_level' => 'h2']], [$topic['type'], $topic['variant'], $topic['settings']]);
$this->assertSame(['pillar' => $pillar->id, 'image' => 'cms/karte.jpg'], $topic['refs']);
$this->assertSame(['Zum Thema', 20, 80], [$topic['data']['action_label'], $topic['data']['focus_x'], $topic['data']['focus_y']]);
$this->assertSame(['media_card', 'stacked', $element->id], [$media['type'], $media['variant'], $media['refs']['element']]);
$this->assertSame(['program_card', $program->composition_id], [$programCard['type'], $programCard['refs']['composition']]);
// Wieder öffnen: Dialog zeigt Inhalt (Fachobjekt), Medien (Bild, Fokus) und Darstellung (Variante, Höhe, Optionen).
$component = Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]);
$sectionKey = array_key_first($component->get('data.sections'));
$slotKey = array_key_first($component->get("data.sections.$sectionKey.slots"));
$itemKey = array_key_first($component->get("data.sections.$sectionKey.slots.$slotKey.widget"));
$this->assertSame($pillar->id, (int) $component->get("data.sections.$sectionKey.slots.$slotKey.widget.$itemKey.data.refs.pillar"));
$component->mountAction(TestAction::make('edit')->schemaComponent("sections.$sectionKey.slots.$slotKey.widget")->arguments(['item' => $itemKey]))
->assertFormFieldExists('refs.pillar', fn ($field) => $field instanceof Select && $field->isRequired())
->assertFormFieldExists('data.action_label')
->assertFormFieldExists('data.note')
->assertFormFieldExists('refs.image')
->assertFormFieldExists('data.alt')
->assertFormFieldExists('data.focus_x', fn ($field) => $field instanceof Slider)
->assertFormFieldExists('variant', fn ($field) => $field instanceof ToggleButtons && array_keys($field->getOptions()) === ['adaptive', 'stacked'])
->assertFormFieldExists('settings.height', fn ($field) => $field instanceof ToggleButtons && array_keys($field->getOptions()) === ['auto', 'fill'])
->assertFormFieldExists('settings.format')
->assertFormFieldExists('settings.fit')
->assertFormFieldExists('settings.show_summary')
->assertFormFieldExists('settings.show_action');
// Live-Vorschau des Dialogs mit derselben Ansicht wie die öffentliche Seite.
$preview = WidgetEditor::renderPreview('topic_card', $component->get("data.sections.$sectionKey.slots.$slotKey.widget.$itemKey.data"));
$this->assertStringContainsString('lf-pc lf-pc--editorial lf-pc--square', htmlspecialchars_decode($preview));
// Speichern ohne Änderung bleibt fehlerfrei; Veröffentlichen zeigt die Karten öffentlich.
$component->call('save')->assertHasNoFormErrors();
app(PageService::class)->publish($page->refresh(), $page->latestRevision);
$html = $this->get('/karten-editor')->assertOk()->getContent();
$this->assertStringContainsString('<h2 class="lf-pc__title">Gefühle</h2>', $html);
$this->assertStringContainsString('>Zum Thema</a>', $html);
$this->assertStringContainsString('Programm Abstand', $html);
$this->assertStringContainsString('Programm Abstand Blatt', $html, 'Ein Element eines öffentlichen Programms erscheint öffentlich.');
$this->assertStringContainsString('lf-pc lf-pc--media lf-pc--landscape lf-pc--cover lf-pc--stacked', $html);
$this->assertStringNotContainsString('Im Programm „Programm Abstand“', $html, 'Zusatzangaben ausgeblendet.');
}
private function pillar(string $name, string $slug, ?string $summary): Pillar
{
$pillar = app(PillarService::class)->create($name);
app(PillarService::class)->present($pillar, $name, $slug, $summary, true, 10);
return $pillar->refresh();
}
/**
* @param array<string, mixed> $refs
* @param array<string, mixed> $data
* @param array<string, string> $settings
* @return array<string, mixed>
*/
private function card(string $type, array $refs, array $data = [], array $settings = [], string $variant = 'adaptive'): array
{
return ['type' => $type, 'v' => 1, 'variant' => $variant, 'settings' => $settings, 'data' => $data, 'refs' => $refs];
}
/**
* @param list<array<string, mixed>|null> $slots
* @return array<string, mixed>
*/
private function section(string $layout, array $slots): array
{
return ['layout' => $layout, 'style' => [], 'slots' => $slots];
}
/**
* @param list<array<string, mixed>> $sections
*/
private function publish(string $path, array $sections): PageRevision
{
$page = Page::query()->create(['path' => $path]);
$service = app(PageService::class);
$revision = $service->saveRevision($page, $sections, null, null, null);
$service->publish($page, $revision);
return $revision;
}
private function mainHtml(string $html): string
{
preg_match('#<main[^>]*>(.*)</main>#s', $html, $match);
return $match[1] ?? '';
}
private function editor(): User
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $editor;
}
/**
* @param list<array<string, mixed>|null> $widgets
* @return array<string, mixed>
*/
private function sectionState(string $layout, array $widgets): array
{
$slots = [];
foreach ($widgets as $widget) {
$slots[(string) Str::uuid()] = ['widget' => $widget === null ? [] : [(string) Str::uuid() => $widget]];
}
return ['id' => null, 'layout' => $layout, 'style' => [], 'slots' => $slots];
}
}