Files
laraundfelix/tests/Feature/CmsProseEditorTest.php
LarsandClaude Sonnet 5.5 00b7ce9b3f
CI Feature / test (push) Canceled after 0s
Deploy Test / test (push) Successful in 6m21s
Deploy Test / deploy (push) Successful in 41s
Schreibe die Einleitung der Überschrift im Editor des Fließtexts
Das Widget Überschrift und Einleitung (jetzt Version 2) nimmt die Einleitung als formatierten Text: Absätze, Zeilenumbrüche, Auszeichnungen, Schriftgröße, -farbe und Ausrichtung wie im Fließtext. Die Regeln für .prose gelten auch für die Überschrift. Klartext der Version 1 wird beim Lesen zu Absätzen mit Zeilenumbrüchen angehoben. Drei Tests erwarteten das alte Markup der Einleitung und wurden bewusst angepasst.

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

168 lines
9.5 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\FormattedText;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Widgets\Support\HeadingAccent;
use App\Domain\Cms\Widgets\Types\ProseWidget;
use App\Domain\Cms\Widgets\WidgetRegistry;
use Filament\Forms\Components\RichEditor;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
/**
* Fließtext mit dem Editor der Dokumente (ADR-50): formatierter Text mit Zwischenüberschriften, Listen, Zitat, Tabelle und
* Links, bereinigt beim Speichern und bei der Ausgabe. Dazu die Breite des Texts in Überschrift und Fließtext.
*/
class CmsProseEditorTest extends TestCase
{
use RefreshDatabase;
private const RICH = '<h3>Zwischenüberschrift</h3><p>Ein Absatz mit <strong>fett</strong>, <em>kursiv</em> und einem <a href="/materialien">Link auf eine eigene Seite</a>.</p><ul><li><p>Eins</p></li><li><p>Zwei</p></li></ul><blockquote><p>Ein Zitat</p></blockquote><table><tbody><tr><th><p>Name</p></th><th><p>Wert</p></th></tr><tr><td><p>A</p></td><td><p>1</p></td></tr></tbody></table>';
/**
* @param array<string, mixed> $settings
* @return list<array<string, mixed>>
*/
private function prose(string $body, array $settings = [], int $version = 2): array
{
return [['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'prose', 'v' => $version, 'settings' => $settings, 'data' => ['body' => $body]]]]];
}
/**
* @param list<array<string, mixed>> $sections
*/
private function publish(string $path, array $sections): string
{
$page = Page::query()->create(['path' => $path]);
$service = app(PageService::class);
$service->publish($page, $service->saveRevision($page, $sections, null, null, null));
return $this->get($path)->assertOk()->getContent();
}
public function test_formatted_text_renders_headings_lists_quotes_tables_and_internal_links(): void
{
$html = $this->publish('/fliesstext-reich', $this->prose(self::RICH));
$this->assertStringContainsString('<h3>Zwischenüberschrift</h3>', $html);
$this->assertStringContainsString('<strong>fett</strong>', $html);
$this->assertStringContainsString('<em>kursiv</em>', $html);
$this->assertStringContainsString('<a href="/materialien" rel="noopener noreferrer">Link auf eine eigene Seite</a>', $html, 'Ein Link auf eine eigene Seite bleibt erhalten.');
$this->assertStringContainsString('<ul><li><p>Eins</p></li>', $html);
$this->assertStringContainsString('<blockquote><p>Ein Zitat</p></blockquote>', $html);
$this->assertStringContainsString('<th><p>Name</p></th>', $html);
$this->assertStringContainsString('<div class="prose-table" role="region" tabindex="0" aria-label="Tabelle"><table>', $html, 'Jede Tabelle steht in einem scrollbaren, tastaturerreichbaren Rahmen.');
$this->assertSame(1, substr_count($html, '<div class="prose-table"'));
}
public function test_scripts_styles_events_and_unsafe_links_are_removed_when_saving_and_when_rendering(): void
{
$dirty = '<p onclick="x()">Text<script>alert(1)</script><style>p{color:red}</style> <a href="javascript:alert(1)">Böse</a> <a href="https://example.org/x">Gut</a> <a href="#anker">Anker</a></p>';
$normalized = app(WidgetRegistry::class)->normalize($this->prose($dirty)[0]['slots']);
$stored = $normalized[0]['data']['body'];
$this->assertStringNotContainsString('<script', $stored);
$this->assertStringNotContainsString('onclick', $stored);
$this->assertStringNotContainsString('<style', $stored);
$this->assertStringNotContainsString('javascript:', $stored);
$this->assertStringContainsString('href="https://example.org/x"', $stored);
$this->assertStringContainsString('href="#anker"', $stored);
// Auch eine schon gespeicherte, unsaubere Fassung wird bei der Ausgabe bereinigt.
$this->assertStringNotContainsString('<script', ProseWidget::render($dirty));
$this->assertStringNotContainsString('javascript:', ProseWidget::render($dirty));
}
public function test_the_catalog_keeps_its_stricter_link_rule(): void
{
$html = '<p><a href="/materialien">Intern</a> <a href="https://example.org/">Extern</a></p>';
$this->assertStringNotContainsString('href="/materialien"', FormattedText::canonical($html), 'Verwaltung und PDF kennen nur ausdrückliche Adressen.');
$this->assertStringContainsString('href="/materialien"', FormattedText::canonical($html, true));
$this->assertStringContainsString('href="https://example.org/"', FormattedText::canonical($html));
}
public function test_version_one_plain_text_becomes_paragraphs_and_renders_as_before(): void
{
$plain = "Erster Absatz.\n\nZweiter Absatz mit <b>Zeichen</b> & mehr.\n- Kein Aufzählungspunkt\n# Keine Überschrift";
$revision = app(PageService::class)->saveRevision(Page::query()->create(['path' => '/fliesstext-alt']), $this->prose($plain, [], 1), null, null, null);
$this->assertSame(2, $revision->blocks[0]['v']);
$body = $revision->blocks[0]['data']['body'];
$this->assertStringStartsWith('<p>Erster Absatz.</p><p>Zweiter Absatz mit &lt;b&gt;Zeichen&lt;/b&gt; &amp; mehr.', $body);
$this->assertStringNotContainsString('<ul>', $body, 'Klartext wird nicht nachträglich zu Listen oder Überschriften gedeutet.');
$this->assertStringNotContainsString('<h3>', $body);
$this->assertStringNotContainsString('<b>', $body);
}
public function test_a_blank_text_is_rejected_and_long_formatted_text_is_accepted(): void
{
foreach (['', '<p></p>', '<p> </p>'] as $blank) {
try {
app(WidgetRegistry::class)->normalize($this->prose($blank)[0]['slots']);
$this->fail('Angenommen: leerer Text');
} catch (ValidationException $exception) {
$this->assertNotEmpty($exception->errors(), $blank);
}
}
$long = str_repeat('<p>'.str_repeat('Wort ', 30).'</p>', 250);
$this->assertGreaterThan(20000, strlen($long), 'Formatierter Text braucht mehr als die frühere Grenze für Klartext.');
$normalized = app(WidgetRegistry::class)->normalize($this->prose($long)[0]['slots']);
$this->assertSame(250, substr_count($normalized[0]['data']['body'], '<p>'));
}
public function test_the_summary_shows_text_without_markup(): void
{
$summary = app(WidgetRegistry::class)->get('prose')->summary(['data' => ['body' => self::RICH]]);
$this->assertStringStartsWith('Zwischenüberschrift', $summary);
$this->assertStringNotContainsString('<', $summary);
}
public function test_the_editor_field_is_the_shared_rich_editor(): void
{
$field = app(WidgetRegistry::class)->get('prose')->dataFields()[0];
$this->assertInstanceOf(RichEditor::class, $field);
$this->assertSame('body', $field->getName());
$this->assertContains('table', array_merge(...FormattedText::TOOLBAR), 'Die Symbolleiste der Dokumente bietet Tabellen.');
}
public function test_heading_and_prose_use_the_full_width_unless_the_reading_width_is_chosen(): void
{
$full = $this->publish('/breite-voll', [['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'heading', 'v' => 1, 'data' => ['heading' => 'Eine Überschrift', 'intro' => 'Einleitung']]]], ['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'prose', 'v' => 2, 'data' => ['body' => '<p>Text</p>']]]]]);
$this->assertStringContainsString('<div class="prose lede"><p>Einleitung</p></div>', $full);
$this->assertStringContainsString('<div class="prose"><p>Text</p></div>', $full);
$this->assertStringNotContainsString('lf-reading', $full);
$this->assertStringNotContainsString('prose--reading', $full);
$reading = $this->publish('/breite-lesen', [['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'heading', 'v' => 1, 'settings' => ['text_width' => 'reading'], 'data' => ['heading' => 'Eine Überschrift', 'intro' => 'Einleitung']]]], ['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'prose', 'v' => 2, 'settings' => ['text_width' => 'reading'], 'data' => ['body' => '<p>Text</p>']]]]]);
$this->assertStringContainsString('<h1 class="lf-reading">', $reading, 'Das erste Widget der Seite ist die H1.');
$this->assertStringContainsString('<div class="prose lede lf-reading prose--reading"><p>Einleitung</p></div>', $reading);
$this->assertStringContainsString('<div class="prose prose--reading">', $reading);
}
public function test_the_text_width_setting_takes_only_known_values(): void
{
foreach (['heading' => ['heading' => 'X'], 'prose' => ['body' => '<p>X</p>']] as $type => $data) {
try {
app(WidgetRegistry::class)->normalize([['type' => $type, 'v' => $type === 'prose' ? 2 : 1, 'settings' => ['text_width' => 'breit'], 'data' => $data]]);
$this->fail('Angenommen: '.$type);
} catch (ValidationException $exception) {
$this->assertStringContainsString('nicht freigegeben', implode(' ', array_merge(...array_values($exception->errors()))));
}
}
$this->assertSame('full', HeadingAccent::textWidth([]));
$this->assertSame('full', HeadingAccent::textWidth(['text_width' => ['reading']]));
$this->assertSame('reading', HeadingAccent::textWidth(['text_width' => 'reading']));
}
}