Files
laraundfelix/tests/Unit/FormattedTextTest.php
T
LarsandClaude Sonnet 5.5 7f9534d289
CI Feature / test (push) Successful in 7m47s
Deploy Test / test (push) Successful in 8m9s
Deploy Test / deploy (push) Successful in 44s
Erkenne Text mit H1, H2 oder div am Anfang als HTML statt als Klartext
Ein Fließtext, dessen HTML mit <h2> begann, wurde als Klartext des Altbestands gelesen
und mit sichtbaren Tags ausgegeben (Website und Vorschau). Die Bereinigung bildet H1/H2
auf H3 ab, kam aber nie zum Zug. isHtml kennt nun h1, h2 und div am Textanfang.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-01 10:43:04 +01:00

189 lines
12 KiB
PHP

<?php
namespace Tests\Unit;
use App\Domain\Catalog\FormattedText;
use Tests\TestCase;
class FormattedTextTest extends TestCase
{
public function test_unsafe_markup_is_removed_and_the_text_stays_readable(): void
{
$html = FormattedText::canonical('<p onclick="x()">Hallo <script>alert(1)</script><strong>Welt</strong></p><p><a href="javascript:alert(1)">Link</a> <a href="https://example.org/a?b=1&c=2">Gut</a> <a href="/intern">Relativ</a></p><iframe src="https://evil.example"></iframe><p><img src="x" onerror="y()">Bild</p>');
$this->assertStringNotContainsString('<script', $html);
$this->assertStringNotContainsString('onclick', $html);
$this->assertStringNotContainsString('onerror', $html);
$this->assertStringNotContainsString('javascript:', $html);
$this->assertStringNotContainsString('<iframe', $html);
$this->assertStringNotContainsString('<img', $html);
$this->assertStringContainsString('<strong>Welt</strong>', $html);
$this->assertStringContainsString('href="https://example.org/a?b=1&amp;c=2"', $html);
$this->assertStringContainsString('rel="noopener noreferrer"', $html);
$this->assertStringContainsString('Bild', $html);
}
public function test_canonical_form_is_stable_and_ignores_spelling_of_the_same_content(): void
{
$samples = [
'<p>Eins</p>'."\n\n".'<p>Zwei <b>fett</b> und <i>schräg</i></p>',
'<div>Lose <strong>Zeile</strong></div><ul><li>Punkt A</li><li><p>Punkt B</p></li></ul>',
'<h2>Titel</h2><ol><li>Erst</li><li>Dann</li></ol><blockquote>Zitat</blockquote>',
'Text <b>ohne</b> Block',
];
foreach ($samples as $sample) {
$once = FormattedText::canonicalHtml($sample);
$this->assertNotSame('', $once);
$this->assertSame($once, FormattedText::canonical($once), 'Zweimal kanonisch ändert nichts: '.$sample);
$this->assertTrue(FormattedText::isHtml($once));
}
$this->assertSame(
FormattedText::canonicalHtml('<p>A <b>B</b></p>'),
FormattedText::canonicalHtml("<p>A <strong>B</strong></p>\n"),
);
$this->assertTrue(FormattedText::same('<p>A</p>', "<p>A</p>\n\n"));
$this->assertSame('', FormattedText::canonical("<p></p>\n<p> </p>"));
$this->assertSame('', FormattedText::canonical(' '));
$this->assertSame('', FormattedText::canonical(null));
}
public function test_list_items_always_carry_their_text_in_a_paragraph_like_the_editor_writes_them(): void
{
$this->assertSame('<ul><li><p>A</p></li><li><p>B</p></li></ul>', FormattedText::canonicalHtml('<ul><li>A</li><li>B</li></ul>'));
$this->assertSame('<ul><li><p>A</p></li></ul>', FormattedText::canonicalHtml('<ul><li><p>A</p></li></ul>'));
}
public function test_legacy_plain_text_keeps_its_paragraphs_lists_and_headings(): void
{
$plain = "Einleitung mit < und & Zeichen.\n\n## Ablauf\n\n1. Stand einnehmen\n2. Arme nach vorn\n\n- Leise ermutigen\n- Laute anhalten\n\nSchluss.";
$this->assertFalse(FormattedText::isHtml($plain));
$html = FormattedText::canonical($plain);
$this->assertSame(
'<p>Einleitung mit &lt; und &amp; Zeichen.</p><h3>Ablauf</h3><ol><li><p>Stand einnehmen</p></li><li><p>Arme nach vorn</p></li></ol><ul><li><p>Leise ermutigen</p></li><li><p>Laute anhalten</p></li></ul><p>Schluss.</p>',
$html,
);
$this->assertSame($html, FormattedText::canonical($html));
$this->assertSame(FormattedText::html($plain), $html);
}
public function test_plain_text_that_only_looks_like_markup_stays_plain(): void
{
$this->assertFalse(FormattedText::isHtml('<3 Training & Spaß'));
$this->assertSame('<p>&lt;3 Training &amp; Spaß</p>', FormattedText::canonical('<3 Training & Spaß'));
$this->assertFalse(FormattedText::isHtml('Preis < 5 und > 3'));
}
public function test_html_that_starts_with_h1_h2_or_div_is_html_and_not_masked_as_plain_text(): void
{
$html = '<h2>Was Kinder üben</h2><p>Text mit <strong>fett</strong>.</p><h3>Unterpunkt</h3><ul><li><p>Eins</p></li></ul>';
$this->assertTrue(FormattedText::isHtml($html));
$this->assertTrue(FormattedText::isHtml('
<h1>Titel</h1>'));
$this->assertTrue(FormattedText::isHtml('<div>Lose Zeile</div>'));
// h1 und h2 gehören der Seite und dem Kapitel; im Text werden sie zur Zwischenüberschrift h3.
$this->assertSame(
'<h3>Was Kinder üben</h3><p>Text mit <strong>fett</strong>.</p><h3>Unterpunkt</h3><ul><li><p>Eins</p></li></ul>',
FormattedText::canonical($html, true),
);
$this->assertStringNotContainsString('&lt;', FormattedText::html($html, null, true));
$this->assertSame('<p>Lose Zeile</p>', FormattedText::canonical('<div>Lose Zeile</div>'));
}
public function test_plain_text_and_excerpt_drop_markup_but_keep_structure(): void
{
$html = '<p>Erster <strong>Absatz</strong><br>mit Umbruch</p><ul><li><p>Eins</p></li><li><p>Zwei</p></li></ul><ol><li><p>A</p></li><li><p>B</p></li></ol>';
$this->assertSame("Erster Absatz\nmit Umbruch\n\n- Eins\n- Zwei\n\n1. A\n2. B", FormattedText::plain($html));
$this->assertSame('Erster Absatz mit Umbruch - Eins - Zwei 1. A 2. B', FormattedText::excerpt($html, 200));
$this->assertSame('Erster Absatz…', FormattedText::excerpt($html, 14));
}
public function test_blocks_carry_the_emphasis_for_the_pdf(): void
{
$blocks = FormattedText::blocks('<h3>Titel</h3><p>Normal <strong>fett</strong> <em>schräg</em> <a href="https://example.org/">Link</a></p><ul><li><p>Eins <strong>fett</strong></p></li></ul>');
$this->assertSame('heading', $blocks[0]['type']);
$this->assertSame('Titel', $blocks[0]['runs'][0]['text']);
$this->assertSame('paragraph', $blocks[1]['type']);
$this->assertSame(
[['Normal ', false, false, null], ['fett', true, false, null], [' ', false, false, null], ['schräg', false, true, null], [' ', false, false, null], ['Link', false, false, 'https://example.org/']],
array_map(fn (array $run): array => [$run['text'], $run['bold'], $run['italic'], $run['href']], $blocks[1]['runs']),
);
$this->assertSame('bullets', $blocks[2]['type']);
$this->assertTrue($blocks[2]['items'][0][1]['bold']);
}
public function test_a_table_keeps_its_structure_and_loses_widths_and_styling(): void
{
$html = FormattedText::canonical('<table style="min-width: 50px"><colgroup><col><col></colgroup><tbody><tr><th colspan="2" colwidth="120"><p>Kopf</p></th></tr><tr><td><p>a</p></td><td rowspan="1">b <strong>x</strong></td></tr><tr><td></td><td><ul><li>eins</li></ul></td></tr></tbody></table>');
$this->assertSame('<table><tbody><tr><th colspan="2"><p>Kopf</p></th></tr><tr><td><p>a</p></td><td><p>b <strong>x</strong></p></td></tr><tr><td><p></p></td><td><ul><li><p>eins</p></li></ul></td></tr></tbody></table>', $html);
$this->assertSame($html, FormattedText::canonical($html), 'Die kanonische Form ist stabil.');
$this->assertTrue(FormattedText::isHtml($html), 'Ein Text, der mit einer Tabelle beginnt, bleibt HTML und wird nicht als Klartext maskiert.');
}
public function test_a_table_reaches_the_pdf_as_rows_and_plain_text_as_lines(): void
{
$html = '<table><tbody><tr><th><p>Zeit</p></th><th><p>Inhalt</p></th></tr><tr><td><p>10 Min.</p></td><td><p>Begrüßung</p></td></tr></tbody></table>';
$blocks = FormattedText::blocks($html);
$this->assertSame('table', $blocks[0]['type']);
$this->assertCount(2, $blocks[0]['rows']);
$this->assertTrue($blocks[0]['rows'][0][0]['header']);
$this->assertSame('Begrüßung', $blocks[0]['rows'][1][1]['blocks'][0]['runs'][0]['text']);
$this->assertSame("Zeit | Inhalt\n10 Min. | Begrüßung", FormattedText::plain($html));
}
public function test_an_image_is_a_reference_to_an_image_version_and_never_an_address(): void
{
$html = FormattedText::canonical('<p>Davor</p><img data-id="12" alt="Ein Bild" src="https://evil.example/x.png" onerror="x()" width="40"><p>Danach</p><img src="y.png"><p>Eingebettet <img data-id="9" alt="x"> im Absatz</p>');
$this->assertSame('<p>Davor</p><img data-id="12" alt="Ein Bild"><p>Danach</p><p>Eingebettet <img data-id="9" alt="x"> im Absatz</p>', $html, 'Der Editor setzt ein Bild mitten in einen Absatz; es bleibt dort.');
$this->assertSame([12, 9], FormattedText::imageIds($html));
$this->assertSame([['type' => 'paragraph', 'text' => 'Eingebettet'], ['type' => 'image', 'id' => 9], ['type' => 'paragraph', 'text' => 'im Absatz']], array_map(fn (array $block): array => $block['type'] === 'image' ? ['type' => 'image', 'id' => $block['id']] : ['type' => $block['type'], 'text' => trim(implode('', array_column($block['runs'], 'text')))], array_slice(FormattedText::blocks($html), 3)), 'Im PDF teilt das Bild den Absatz.');
$this->assertSame('<h3>Kopf</h3><ul><li><p>Punkt</p></li></ul>', FormattedText::canonical('<h3>Kopf<img data-id="5"></h3><ul><li><p>Punkt<img data-id="6"></p></li></ul>'), 'In Überschrift und Liste entfällt ein Bild.');
$this->assertSame($html, FormattedText::canonical($html));
$this->assertSame('<img data-id="3" alt="">', FormattedText::canonical('<img data-id="3" alt="">'), 'Ein Text nur aus einem Bild ist nicht leer.');
}
public function test_an_image_in_a_table_cell_is_kept_because_the_editor_can_put_it_there(): void
{
$html = FormattedText::canonical('<table><tbody><tr><td><p>Stuhlkreis</p><img data-id="7" alt="Aufstellung" src="x.png"></td></tr></tbody></table>');
$this->assertSame('<table><tbody><tr><td><p>Stuhlkreis</p><img data-id="7" alt="Aufstellung"></td></tr></tbody></table>', $html);
$this->assertSame([7], FormattedText::imageIds($html));
$this->assertSame('image', FormattedText::blocks($html)[0]['rows'][0][0]['blocks'][1]['type']);
$this->assertSame($html, FormattedText::canonical($html));
$this->assertSame('<table><tbody><tr><td><img data-id="7" alt=""></td></tr></tbody></table>', FormattedText::canonical('<table><tbody><tr><td><img data-id="7"></td></tr></tbody></table>'), 'Eine Zelle nur mit Bild bleibt erhalten.');
}
public function test_images_appear_only_where_the_reader_resolves_them(): void
{
$html = '<p>Davor</p><img data-id="12" alt="Ein Bild"><img data-id="13" alt="Anderes"><p>Danach</p>';
$this->assertSame('<p>Davor</p><p>Danach</p>', FormattedText::html($html), 'Ohne Auflösung zeigt die Ausgabe kein Bild.');
$shown = FormattedText::html($html, fn (int $id): ?array => $id === 12 ? ['src' => '/b/12?sig=1'] : ['note' => 'Dieses Bild ist derzeit nicht verfügbar.']);
$this->assertStringContainsString('<img alt="Ein Bild" src="/b/12?sig=1" loading="lazy">', $shown);
$this->assertStringNotContainsString('data-id', $shown);
$this->assertStringContainsString('<p class="mb-bildhinweis">Dieses Bild ist derzeit nicht verfügbar.</p>', $shown);
$this->assertSame('Davor'."\n\n".'Danach', FormattedText::plain($html), 'Im Klartext hat ein Bild keinen Platz.');
}
public function test_the_editor_gets_an_address_for_every_image_and_loses_it_again_on_the_way_back(): void
{
$stored = FormattedText::canonical('<p>Text</p><img data-id="12" alt="Ein Bild">');
$forEditor = FormattedText::forEditor($stored, fn (int $id): string => "/redaktion/inhalt/{$id}/datei");
$this->assertStringContainsString('src="/redaktion/inhalt/12/datei"', $forEditor);
$this->assertSame($stored, FormattedText::canonicalHtml($forEditor));
}
}