Files
laraundfelix/tests/Feature/CmsSectionCardsTest.php
T
LarsandClaude Sonnet 5.5 62f3a33541
CI Feature / test (push) Successful in 8m2s
Deploy Test / test (push) Successful in 8m1s
Deploy Test / deploy (push) Successful in 51s
Richte Karten im Abschnitt an der größten Karte aus und setze Kartenlinks an den unteren Rand
Zwei Gestaltungswerte am Abschnitt (Höhe der Karten, Link in der Karte), gespeichert nur bei Abweichung
von der Vorgabe. Karten nebeneinander füllen die Zeilenhöhe (CSS); ein kleines Skript gleicht Zeilen
untereinander an. Links im Fließtext bleiben unberührt (ADR-66).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-29 17:33:27 +01:00

150 lines
7.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Sections\PageSections;
use App\Domain\Cms\Sections\SectionStyle;
use App\Domain\Cms\Widgets\Types\CardsWidget;
use App\Domain\Cms\Widgets\Types\CatalogCardsWidget;
use App\Domain\Cms\Widgets\Types\PremiumCardWidget;
use App\Domain\Cms\Widgets\WidgetRegistry;
use App\Filament\Resources\Pages\Pages\EditPage;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Validation\ValidationException;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Karten im Abschnitt (ADR-66): „Höhe der Karten“ (alle so hoch wie die größte) und „Link in der Karte“ (am unteren Rand).
* Beides sind Gestaltungswerte des Abschnitts; Abschnitte ohne diese Werte bleiben in Format und Ausgabe unverändert.
*/
class CmsSectionCardsTest extends TestCase
{
use RefreshDatabase;
/**
* @param array<string, mixed> $style
* @return array<string, mixed>
*/
private function cardsSection(array $style = []): array
{
return ['layout' => 'u4', 'style' => $style, 'slots' => [['type' => 'cards', 'v' => 2, 'variant' => 'standard', 'settings' => ['link_style' => 'text'], 'data' => ['items' => [
['title' => 'Eins', 'url' => '/a', 'link_label' => 'Mehr'],
['title' => 'Zwei', 'url' => '/b'],
]]]]];
}
/**
* @param array<string, mixed> $style
*/
private function publish(string $path, array $style): string
{
$page = Page::query()->create(['path' => $path]);
$service = app(PageService::class);
$service->publish($page, $service->saveRevision($page, [$this->cardsSection($style)], null, null, null));
return $this->get($path)->assertOk()->getContent();
}
public function test_an_existing_section_keeps_its_stored_format_and_its_output(): void
{
$html = $this->publish('/ohne-karteneinstellung', []);
$stored = Page::query()->where('path', '/ohne-karteneinstellung')->firstOrFail()->latestRevision->sections[0]['style'];
$this->assertArrayNotHasKey('cards_height', $stored, 'Vorgaben stehen nicht im gespeicherten Stand.');
$this->assertArrayNotHasKey('cards_links', $stored);
$this->assertStringNotContainsString('section-cards-', $html);
$defaults = SectionStyle::fromStored(['background' => 'light']);
$this->assertSame(['auto', 'text'], [$defaults['cards_height'], $defaults['cards_links']]);
}
public function test_each_setting_adds_its_own_class_to_the_section_and_is_stored(): void
{
$equal = $this->publish('/gleich-hoch', ['cards_height' => 'equal']);
$this->assertStringContainsString('section-cards-equal', $equal);
$this->assertStringNotContainsString('section-cards-links-bottom', $equal);
$bottom = $this->publish('/link-unten', ['cards_links' => 'bottom']);
$this->assertStringContainsString('section-cards-links-bottom', $bottom);
$this->assertStringNotContainsString('section-cards-equal', $bottom);
$both = $this->publish('/beides', ['cards_height' => 'equal', 'cards_links' => 'bottom']);
$this->assertStringContainsString('section-cards-equal', $both);
$this->assertStringContainsString('section-cards-links-bottom', $both);
$stored = Page::query()->where('path', '/beides')->firstOrFail()->latestRevision->sections[0]['style'];
$this->assertSame(['equal', 'bottom'], [$stored['cards_height'], $stored['cards_links']]);
}
public function test_explicit_defaults_are_not_stored_and_invalid_values_are_refused(): void
{
$sections = app(PageSections::class)->normalize([$this->cardsSection(['cards_height' => 'auto', 'cards_links' => 'text'])]);
$this->assertArrayNotHasKey('cards_height', $sections[0]['style']);
$this->assertArrayNotHasKey('cards_links', $sections[0]['style']);
try {
app(PageSections::class)->normalize([$this->cardsSection(['cards_height' => 'riesig'])]);
$this->fail('Ein unbekannter Wert muss abgelehnt werden.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.style.cards_height', $exception->errors());
}
$this->assertSame('auto', SectionStyle::fromStored(['cards_height' => 'riesig'])['cards_height'], 'Beim Lesen fällt Unbekanntes auf die Vorgabe zurück.');
}
public function test_the_summary_names_the_settings_only_when_they_differ(): void
{
$this->assertSame('Standard (ohne eigene Fläche)', SectionStyle::summary([]));
$this->assertStringContainsString('Karten gleich hoch', SectionStyle::summary(['cards_height' => 'equal']));
$this->assertStringContainsString('Kartenlinks unten', SectionStyle::summary(['cards_links' => 'bottom']));
}
public function test_the_editor_saves_both_settings_and_an_untouched_page_stays_unchanged(): void
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
$page = Page::query()->create(['path' => '/editor']);
app(PageService::class)->saveRevision($page, [$this->cardsSection()], null, null, null);
$revisions = $page->revisions()->count();
$component = Livewire::actingAs($editor, 'staff')->test(EditPage::class, ['record' => $page->id]);
$key = array_key_first($component->get('data.sections'));
$component->assertSet("data.sections.{$key}.style.cards_height", 'auto')->assertSet("data.sections.{$key}.style.cards_links", 'text');
$component->call('save')->assertHasNoFormErrors();
$this->assertSame($revisions, $page->revisions()->count(), 'Speichern ohne Änderung legt keine Revision an.');
$key = array_key_first($component->get('data.sections'));
$component->set("data.sections.{$key}.style.cards_height", 'equal')->set("data.sections.{$key}.style.cards_links", 'bottom')->call('save')->assertHasNoFormErrors();
$style = $page->fresh()->latestRevision->sections[0]['style'];
$this->assertSame(['equal', 'bottom'], [$style['cards_height'], $style['cards_links']]);
$key = array_key_first($component->get('data.sections'));
$component->set("data.sections.{$key}.style.cards_height", 'auto')->set("data.sections.{$key}.style.cards_links", 'text')->call('save')->assertHasNoFormErrors();
$style = $page->fresh()->latestRevision->sections[0]['style'];
$this->assertArrayNotHasKey('cards_height', $style, 'Zurück auf die Vorgabe entfernt den Wert wieder.');
$this->assertArrayNotHasKey('cards_links', $style);
}
public function test_the_stylesheet_covers_every_card_widget(): void
{
$css = (string) file_get_contents(resource_path('css/widgets.css'));
$covered = 0;
foreach (app(WidgetRegistry::class)->all() as $type) {
if (! $type instanceof CardsWidget && ! $type instanceof CatalogCardsWidget && ! $type instanceof PremiumCardWidget) {
continue;
}
$covered++;
$this->assertStringContainsString('.widget-'.str_replace('_', '-', $type->key()), $css, 'Das Kartenwidget „'.$type->key().'“ fehlt in den Regeln für Karten im Abschnitt.');
}
$this->assertGreaterThanOrEqual(5, $covered);
}
}