$style * @return array */ 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 $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); } }