Files
laraundfelix/tests/Feature/CmsEditorTest.php
T
LarsandClaude Opus 5.5 2dc479a8a4
CI Feature / test (push) Successful in 29s
Deploy Test / test (push) Successful in 26s
Deploy Test / deploy (push) Successful in 31s
Prüfe Altseiten im echten Altformat und zeige Speicherfehler am Abschnitt.
- Der Altseiten-Test schreibt die Seederdaten vor dem Vergleich im flachen
  Format zurück, wie sie auf Test und Produktion liegen (die Migration
  speichert sie inzwischen über die Registry); zusätzlich ein Rundlauf
  Editor -> Speichern -> Veröffentlichen.
- Fehler, die erst die Registry erkennt, erscheinen als Meldung mit
  Abschnittsnummer und Art (Felder liegen im Dialog).
- Seite anlegen übernimmt die gewählten Säulen (wurden bisher verworfen).
- Nach dem Speichern wird der Editor neu befüllt, damit duplizierte
  Abschnitte ihre Kennung behalten.
- Entwicklerdokumentation der Registry inkl. Rollback-Hinweis.

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

324 lines
14 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\PillarService;
use App\Domain\Cms\NavigationItem;
use App\Domain\Cms\Page;
use App\Filament\Resources\NavigationItems\Pages\ManageNavigationItems;
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 Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Livewire\Livewire;
use Tests\Support\BuildsCatalog;
use Tests\TestCase;
class CmsEditorTest extends TestCase
{
use BuildsCatalog;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
}
public function test_editor_creates_a_page_from_registered_widgets_without_publishing_it(): void
{
$program = $this->composition('Programm Abstand', 'programm');
$editor = $this->editor();
Livewire::actingAs($editor, 'staff')
->test(CreatePage::class)
->fillForm([
'path' => '/themen-test',
'seo_title' => 'Themenseite',
'blocks' => $this->keyed([
['type' => 'hero', 'data' => ['variant' => 'highlight', 'data' => ['heading' => 'Stark im Alltag', 'text' => 'Einstieg']]],
['type' => 'image_text', 'data' => ['variant' => 'image_right', 'data' => ['title' => 'Worum es geht', 'body' => '<p>Text mit <strong>Betonung</strong></p>'], 'refs' => []]],
['type' => 'cards', 'data' => ['variant' => 'compact', 'data' => ['items' => $this->keyed([['title' => 'Prävention', 'url' => '/praevention']])]]],
['type' => 'catalog_cards', 'data' => ['variant' => 'standard', 'data' => ['heading' => 'Programme'], 'refs' => ['items' => $this->keyed([['type' => 'composition', 'id' => $program->composition_id]])]]],
]),
])
->call('create')
->assertHasNoFormErrors();
$page = Page::query()->where('path', '/themen-test')->firstOrFail();
$blocks = $page->latestRevision->blocks;
$this->assertNull($page->published_revision_id);
$this->assertSame(['hero', 'image_text', 'cards', 'catalog_cards'], array_column($blocks, 'type'));
$this->assertSame(['highlight', 'image_right', 'compact', 'standard'], array_column($blocks, 'variant'));
$this->assertSame([2], array_values(array_unique([$blocks[1]['v']])));
$this->assertStringContainsString('<strong>Betonung</strong>', $blocks[1]['data']['body']);
$this->assertSame([['title' => 'Prävention', 'text' => null, 'url' => '/praevention', 'image' => null]], $blocks[2]['data']['items']);
$this->assertSame([['type' => 'composition', 'id' => $program->composition_id]], $blocks[3]['refs']['items']);
$this->get('/themen-test')->assertNotFound();
}
public function test_existing_page_opens_in_the_editor_and_saves_without_changing_the_live_page(): void
{
$page = Page::query()->where('path', '/')->firstOrFail();
$live = $page->published_revision_id;
$before = $this->mainText($this->get('/')->getContent());
$component = Livewire::actingAs($this->editor(), 'staff')
->test(EditPage::class, ['record' => $page->id]);
$state = $component->get('data.blocks');
$this->assertSame(['heading', 'prose', 'cards', 'call_to_action'], array_column(array_values($state), 'type'));
$component->call('save')->assertHasNoFormErrors();
$page->refresh();
$this->assertSame($live, $page->published_revision_id);
$this->assertNotSame($live, $page->latestRevision->id);
$this->assertSame(['heading', 'prose', 'cards', 'call_to_action'], array_column($page->latestRevision->blocks, 'type'));
$this->assertSame($before, $this->mainText($this->get('/')->getContent()));
Livewire::actingAs($this->editor(), 'staff')
->test(EditPage::class, ['record' => $page->id])
->callAction('publish');
$this->assertSame(
trim(file_get_contents(base_path('tests/Fixtures/legacy-pages/home.txt'))),
$this->mainText($this->get('/')->getContent()),
'Nach dem Speichern im neuen Format zeigt die Seite denselben Text.',
);
}
public function test_editor_reorders_and_duplicates_sections(): void
{
$page = Page::query()->where('path', '/')->firstOrFail();
$component = Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]);
$state = $component->get('data.blocks');
$keys = array_keys($state);
$reordered = [$keys[3] => $state[$keys[3]], $keys[0] => $state[$keys[0]], $keys[1] => $state[$keys[1]], $keys[2] => $state[$keys[2]]];
$copy = $state[$keys[1]];
$reordered[(string) Str::uuid()] = $copy;
$component->set('data.blocks', $reordered)->call('save')->assertHasNoFormErrors();
$blocks = $page->fresh()->latestRevision->blocks;
$this->assertSame(['call_to_action', 'heading', 'prose', 'cards', 'prose'], array_column($blocks, 'type'));
$this->assertCount(5, array_unique(array_column($blocks, 'id')), 'Ein dupliziertes Widget erhält eine eigene Kennung.');
}
public function test_unsafe_link_is_reported_at_the_section_and_nothing_is_saved(): void
{
$page = Page::query()->where('path', '/')->firstOrFail();
$revisions = $page->revisions()->count();
$component = Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]);
$key = array_keys($component->get('data.blocks'))[3];
$component->set("data.blocks.$key.data.data.url", 'javascript:alert(1)')
->call('save')
->assertHasErrors(["data.blocks.$key.data.data.url"]);
$this->assertSame($revisions, $page->revisions()->count());
}
public function test_errors_found_only_by_the_registry_are_announced_with_the_section(): void
{
Storage::fake('media_public');
Storage::fake('cms_incoming');
Storage::disk('media_public')->put('cms/bild.jpg', 'bild');
$page = Page::query()->where('path', '/')->firstOrFail();
$revisions = $page->revisions()->count();
$component = Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]);
$state = $component->get('data.blocks');
$state[(string) Str::uuid()] = ['type' => 'hero', 'data' => ['variant' => 'standard', 'data' => ['heading' => 'Mit Bild'], 'refs' => ['image' => 'cms/bild.jpg']]];
$component->set('data.blocks', $state)
->call('save')
->assertNotified('Nicht gespeichert');
$this->assertSame($revisions, $page->revisions()->count());
}
public function test_new_page_keeps_the_selected_pillars(): void
{
$pillar = app(PillarService::class)->create('Grenzen');
Livewire::actingAs($this->editor(), 'staff')
->test(CreatePage::class)
->fillForm(['path' => '/grenzen-thema', 'pillars' => [$pillar->id], 'blocks' => []])
->call('create')
->assertHasNoFormErrors();
$this->assertSame([$pillar->id], Page::query()->where('path', '/grenzen-thema')->firstOrFail()->pillars()->pluck('pillars.id')->all());
}
public function test_duplicated_section_keeps_its_new_id_across_saves(): void
{
$page = Page::query()->where('path', '/')->firstOrFail();
$component = Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]);
$state = $component->get('data.blocks');
$state[(string) Str::uuid()] = $state[array_key_first($state)];
$component->set('data.blocks', $state)->call('save')->assertHasNoFormErrors();
$first = array_column($page->fresh()->latestRevision->blocks, 'id');
$component->call('save')->assertHasNoFormErrors();
$second = array_column($page->fresh()->latestRevision->blocks, 'id');
$this->assertCount(5, array_unique($first));
$this->assertSame($first, $second);
}
public function test_invalid_or_taken_paths_are_shown_at_the_path_field(): void
{
Livewire::actingAs($this->editor(), 'staff')
->test(CreatePage::class)
->fillForm(['path' => '/praevention', 'blocks' => []])
->call('create')
->assertHasErrors(['data.path']);
Livewire::actingAs($this->editor(), 'staff')
->test(CreatePage::class)
->fillForm(['path' => 'C:/Program Files/x', 'blocks' => []])
->call('create')
->assertHasErrors(['data.path']);
$this->assertSame(1, Page::query()->where('path', '/praevention')->count());
}
public function test_block_preview_uses_the_public_widget_view_with_unsaved_state(): void
{
$html = WidgetEditor::renderPreview('image_text', [
'variant' => 'image_right',
'data' => [
'title' => 'Ungespeichert',
'body' => ['type' => 'doc', 'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => 'Vorschautext']]]]],
],
'refs' => ['image' => null, '_upload' => []],
]);
$this->assertStringContainsString('image-text image-text-right', $html);
$this->assertStringContainsString('<h2>Ungespeichert</h2>', $html);
$this->assertStringContainsString('Vorschautext', $html);
$cards = WidgetEditor::renderPreview('cards', [
'variant' => 'standard',
'data' => ['items' => [(string) Str::uuid() => ['title' => 'Karte A'], (string) Str::uuid() => ['title' => 'Karte B']]],
]);
$this->assertStringContainsString('Karte A', $cards);
$this->assertStringContainsString('Karte B', $cards);
}
public function test_editor_offers_every_registered_widget_with_description(): void
{
$page = Page::query()->where('path', '/')->firstOrFail();
Livewire::actingAs($this->editor(), 'staff')
->test(EditPage::class, ['record' => $page->id])
->assertSee('Einstieg (Hero)')
->assertSee('Programm- und Modulkarten')
->assertSee('Titel und Beschreibung kommen automatisch aus den Fachdaten')
->assertSee('Entwurf speichern');
}
public function test_preview_with_device_widths_is_only_available_to_staff(): void
{
$revision = Page::query()->where('path', '/')->firstOrFail()->latestRevision;
$this->get(route('pages.preview.devices', $revision))->assertRedirect();
$this->actingAs(User::factory()->create())->get(route('pages.preview', $revision))->assertRedirect();
$this->actingAs(User::factory()->create(), 'staff')->get(route('pages.preview', $revision))->assertForbidden();
auth('web')->logout();
$this->actingAs($this->editor(), 'staff')
->get(route('pages.preview.devices', $revision))
->assertOk()
->assertSee('data-width="390"', false)
->assertSee('data-width="768"', false)
->assertSee('data-width="1440"', false)
->assertSee(route('pages.preview', $revision), false);
$this->actingAs($this->editor(), 'staff')
->get(route('pages.preview', $revision))
->assertOk()
->assertSee('Vorschau einer unveröffentlichten Revision')
->assertSee('Registrieren')
->assertDontSee('Mitgliederbereich</a>', false);
}
public function test_navigation_items_can_be_edited_hidden_reordered_and_removed(): void
{
$editor = $this->editor();
$start = NavigationItem::query()->where('label', 'Start')->firstOrFail();
$about = NavigationItem::query()->where('label', 'Über das Projekt')->firstOrFail();
Livewire::actingAs($editor, 'staff')
->test(ManageNavigationItems::class)
->assertCanSeeTableRecords([$start, $about])
->callAction(TestAction::make('edit')->table($about), ['label' => 'Über uns'])
->assertHasNoFormErrors()
->callAction(TestAction::make('toggleVisibility')->table($start));
$this->assertSame('Über uns', $about->fresh()->label);
$this->assertFalse($start->fresh()->is_visible);
$this->get('/praevention')->assertSee('Über uns')->assertDontSee('>Start<', false);
$order = NavigationItem::query()->orderByDesc('sort_order')->pluck('id')->map(fn ($id) => (string) $id)->all();
Livewire::actingAs($editor, 'staff')->test(ManageNavigationItems::class)->call('reorderTable', $order);
$this->assertSame((int) $order[0], NavigationItem::query()->orderBy('sort_order')->first()->id);
Livewire::actingAs($editor, 'staff')
->test(ManageNavigationItems::class)
->callAction(TestAction::make('delete')->table($about));
$this->assertNull($about->fresh());
$this->assertNotNull(Page::query()->where('path', '/ueber')->first(), 'Die Seite bleibt bestehen.');
}
public function test_navigation_rejects_script_addresses(): void
{
Livewire::actingAs($this->editor(), 'staff')
->test(ManageNavigationItems::class)
->callAction('create', ['label' => 'Böse', 'placement' => 'header', 'url' => 'javascript:alert(1)', 'sort_order' => 9, 'is_visible' => true])
->assertHasFormErrors(['url']);
NavigationItem::query()->create(['label' => 'Alt', 'url' => 'javascript:alert(1)', 'placement' => 'footer', 'sort_order' => 99]);
$this->get('/praevention')->assertDontSee('javascript:alert(1)', false);
}
private function editor(): User
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $editor;
}
/**
* @param list<array<string, mixed>> $items
* @return array<string, array<string, mixed>>
*/
private function keyed(array $items): array
{
$keyed = [];
foreach ($items as $item) {
$keyed[(string) Str::uuid()] = $item;
}
return $keyed;
}
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));
}
}