Speichere formatierte Texte als bereinigtes HTML mit einem Editor (ADR-50)

Ein Baustein (FormattedText) bereinigt, bringt Werte in die kanonische Form und
wandelt Klartext des Altbestands; RichField ist der eine Editor, ManagesRichFields
verbindet ihn mit den Arbeitsplätzen. Formatiert sind Texte der Übungen und
Textbausteine, Kurzbeschreibung und Beschreibung, Lernziel und Ausstattung.
Mitgliederansicht und PDF zeigen fett, kursiv, Listen, Überschrift, Zitat und Link
(PDF: schräge Schnitte, nur wenn gebraucht). Das Livewire-Limit für verschachtelte
Pfade steigt auf 30, weil Aufzählungen sonst abgelehnt werden.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-23 23:45:54 +01:00
1 parent be24f3bf64
commit 9fbd933e1e
50 files changed
+1461 -181

No files matched your search

+2 -2
View File
@@ -273,7 +273,7 @@ public function test_a_license_article_overrides_only_its_own_presentation(): vo
$article->refresh();
$release->refresh();
$this->assertSame('Eigene Artikelbeschreibung', $article->summary);
$this->assertSame('<p>Eigene Artikelbeschreibung</p>', $article->summary);
$this->assertNull($article->teaser);
$again = app(EditorialProfileReader::class)->read($article, 'artikel');
$this->assertSame('eigen', $again->origin('summary'));
@@ -288,7 +288,7 @@ public function test_a_license_article_overrides_only_its_own_presentation(): vo
$cover = $this->pictureWithoutVersion('Artikelbild ohne Fassung');
Livewire::test(ViewLicenseArticle::class, ['record' => $article->getKey()])
->assertSet('summary', 'Eigene Artikelbeschreibung')
->assertSet('summary', '<p>Eigene Artikelbeschreibung</p>')
->call('openRedaktion')
->set('descriptionText', 'Nur dieser Artikel')
->call('openPresentationPicker', 'titelbild')
+12 -12
View File
@@ -84,7 +84,7 @@ public function test_display_data_does_not_create_a_version_and_a_module_works_w
$this->assertSame('Sichtbar ohne Film', $module->display_title);
$this->assertSame(1, $module->versions()->count());
$this->assertSame('Den Abstand erklären', $module->versions()->first()->learning_goal);
$this->assertSame('<p>Den Abstand erklären</p>', $module->versions()->first()->learning_goal);
$this->assertSame(1, $module->versions()->first()->members()->count());
}
@@ -259,18 +259,18 @@ public function test_saving_a_draft_ten_times_creates_one_version_only_when_rele
$module->refresh();
$this->assertSame(0, $module->versions()->count());
$this->assertSame('Durchgang 10', $module->working_draft['learning_goal']);
$this->assertSame('<p>Durchgang 10</p>', $module->working_draft['learning_goal']);
Livewire::actingAs($editor, 'staff')
->test(EditExercise::class, ['record' => $module->id])
->assertSet('learningGoal', 'Durchgang 10')
->assertSet('learningGoal', '<p>Durchgang 10</p>')
->set('releaseConsent', true)
->call('publishRevision')
->assertSet('feedback', 'Fassung 1 ist freigegeben. Der Entwurf ist geschlossen und die bisherige Fassung bleibt erhalten. Die Mitgliederbibliothek ändert sich dadurch nicht.');
$module->refresh();
$this->assertSame(1, $module->versions()->count());
$this->assertSame('Durchgang 10', $module->versions()->first()->learning_goal);
$this->assertSame('<p>Durchgang 10</p>', $module->versions()->first()->learning_goal);
$this->assertNull($module->working_draft);
Livewire::actingAs($editor, 'staff')
@@ -280,8 +280,8 @@ public function test_saving_a_draft_ten_times_creates_one_version_only_when_rele
$module->refresh();
$this->assertSame(1, $module->versions()->count());
$this->assertSame('Durchgang 10', $module->versions()->first()->fresh()->learning_goal);
$this->assertSame('Nur Entwurf', $module->working_draft['learning_goal']);
$this->assertSame('<p>Durchgang 10</p>', $module->versions()->first()->fresh()->learning_goal);
$this->assertSame('<p>Nur Entwurf</p>', $module->working_draft['learning_goal']);
}
public function test_the_picker_finds_a_film_by_title(): void
@@ -393,7 +393,7 @@ public function test_a_chosen_picture_and_description_do_not_create_a_version():
$this->assertSame(1, $composition->versions()->count());
$this->assertSame('vorschau/komposition-'.$composition->id.'.jpg', $composition->thumbnail_path);
$this->assertSame('element:'.$first->element->id, $composition->thumbnail_source);
$this->assertSame('Eine fachliche Beschreibung für die Bibliothek.', $composition->description);
$this->assertSame('<p>Eine fachliche Beschreibung für die Bibliothek.</p>', $composition->description);
Storage::disk('media_derived')->assertExists($composition->thumbnail_path);
$this->get('/medien/'.$composition->thumbnail_path)->assertNotFound();
}
@@ -429,7 +429,7 @@ public function test_texts_save_in_the_redaktion_and_the_dialog_keeps_didactics_
$composition = $version->composition->fresh();
$this->assertSame(1, $composition->versions()->count());
$this->assertSame('Ausführlicher Dialogtext', $composition->description);
$this->assertSame('<p>Ausführlicher Dialogtext</p>', $composition->description);
$this->assertNull($composition->working_draft);
Livewire::actingAs($editor, 'staff')
@@ -441,7 +441,7 @@ public function test_texts_save_in_the_redaktion_and_the_dialog_keeps_didactics_
$composition->refresh();
$this->assertSame(1, $composition->versions()->count());
$this->assertSame(ContentVersionStatus::Bereit, $composition->versions()->first()->status);
$this->assertSame('Nur im Entwurf', $composition->working_draft['learning_goal']);
$this->assertSame('<p>Nur im Entwurf</p>', $composition->working_draft['learning_goal']);
$this->assertSame(9, $composition->working_draft['duration_minutes']);
}
@@ -621,7 +621,7 @@ public function test_a_module_text_stays_editable_until_it_is_released_and_used(
$version = $element->versions()->sole();
$this->assertSame(ContentVersionStatus::Entwurf, $version->status);
$this->assertSame('Geänderter Wortlaut', $version->body);
$this->assertSame('<p>Geänderter Wortlaut</p>', $version->body);
$this->assertSame(1, $element->versions()->count());
$this->actingAs($editor, 'staff')
@@ -643,7 +643,7 @@ public function test_a_module_text_stays_editable_until_it_is_released_and_used(
->call('saveDraft');
$this->assertSame(2, $element->fresh()->versions()->count());
$this->assertSame('Geänderter Wortlaut', $version->fresh()->body);
$this->assertSame('<p>Geänderter Wortlaut</p>', $version->fresh()->body);
$this->assertSame($version->id, $module->fresh()->versions()->first()->members()->first()->content_element_version_id);
$this->assertSame('element:'.$element->versions()->orderByDesc('number')->first()->id, $module->fresh()->working_draft['members'][0]['target']);
}
@@ -685,7 +685,7 @@ public function test_a_file_can_be_created_inside_the_module(): void
$version = $element->versions()->sole();
$this->assertSame('document', $element->output_class);
$this->assertSame('Für die Gruppe', $element->description);
$this->assertSame('<p>Für die Gruppe</p>', $element->description);
$this->assertSame('Vorher lesen', $element->usage_note);
$this->assertSame(ContentVersionStatus::Entwurf, $version->status);
$this->assertSame('eigen', $element->fresh()->thumbnail_source);
@@ -0,0 +1,182 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\FormattedText;
use App\Domain\Catalog\IdentityChange;
use App\Filament\Resources\ContentElements\Pages\ViewContentElement;
use App\Filament\Resources\Practices\Pages\EditPractice;
use App\Filament\Support\EditorialCatalog;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\Support\BuildsExercises;
use Tests\TestCase;
/**
* Formatierte Textfelder im Arbeitsplatz (ADR-50): Der Editor schreibt kanonisches HTML in die Eigenschaft, Klartext des
* Altbestands erscheint formatiert, ohne dass das Öffnen etwas als geändert markiert.
*/
class FormatierteTexteEditorTest extends TestCase
{
use BuildsExercises;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
}
public function test_the_editor_state_reaches_the_property_as_canonical_html_and_back(): void
{
$practice = app(EditorialCatalog::class)->create('Stopp sagen', CompositionKind::Exercise);
$component = Livewire::actingAs($this->editor(), 'staff')
->test(EditPractice::class, ['record' => $practice->getKey()])
->assertOk();
$component->set('rich.summary', [
'type' => 'doc',
'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => 'Fester ', 'marks' => []], ['type' => 'text', 'text' => 'Stand', 'marks' => [['type' => 'bold']]]]]],
]);
$this->assertSame('<p>Fester <strong>Stand</strong></p>', $component->get('summary'));
$component->set('summary', '<p>Vom Server</p>')->assertOk();
$this->assertSame('doc', $component->get('rich.summary.type'));
$this->assertSame('Vom Server', $component->get('rich.summary.content.0.content.0.text'));
}
public function test_legacy_plain_text_appears_formatted_but_opening_it_changes_nothing(): void
{
$practice = app(EditorialCatalog::class)->create('Stopp sagen', CompositionKind::Exercise);
$practice->forceFill(['summary' => "Erste Zeile.\n\n- Punkt A\n- Punkt B"])->save();
$component = Livewire::actingAs($this->editor(), 'staff')
->test(EditPractice::class, ['record' => $practice->getKey()])
->assertOk();
$this->assertSame("Erste Zeile.\n\n- Punkt A\n- Punkt B", $component->get('summary'), 'Die Eigenschaft bleibt, was gespeichert ist.');
$this->assertSame('bulletList', $component->get('rich.summary.content.1.type'), 'Der Editor zeigt die Liste.');
$this->assertFalse($component->instance()->textFieldsChanged(), 'Öffnen markiert nichts als geändert.');
$this->assertSame("Erste Zeile.\n\n- Punkt A\n- Punkt B", $practice->fresh()->summary);
}
public function test_saving_stores_canonical_html_and_an_untouched_legacy_field_is_not_reported_as_changed(): void
{
$practice = app(EditorialCatalog::class)->create('Stopp sagen', CompositionKind::Exercise);
$component = Livewire::actingAs($this->editor(), 'staff')
->test(EditPractice::class, ['record' => $practice->getKey()])
->set('summary', '<p>Kurz <b>fett</b></p>')
->set('description', '<p>Lang</p><script>x()</script>');
$this->assertTrue($component->instance()->textFieldsChanged());
$component->call('saveRedaktion');
$practice->refresh();
$this->assertSame(FormattedText::canonicalHtml('<p>Kurz <strong>fett</strong></p>'), $practice->summary);
$this->assertSame('<p>Lang</p>', $practice->description);
}
public function test_a_list_and_a_nested_list_pass_the_payload_depth_guard(): void
{
$practice = app(EditorialCatalog::class)->create('Stopp sagen', CompositionKind::Exercise);
$item = fn (string $text, array $more = []): array => ['type' => 'listItem', 'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => $text]]], ...$more]];
$component = Livewire::actingAs($this->editor(), 'staff')
->test(EditPractice::class, ['record' => $practice->getKey()])
->set('rich.summary', ['type' => 'doc', 'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => 'Davor']]], ['type' => 'bulletList', 'content' => [$item('Eins', [['type' => 'bulletList', 'content' => [$item('Eins eins')]]]), $item('Zwei')]]]])
->assertHasNoErrors();
$this->assertSame('<p>Davor</p><ul><li><p>Eins</p><ul><li><p>Eins eins</p></li></ul></li><li><p>Zwei</p></li></ul>', $component->get('summary'));
$component->set('rich.summary.content.1.content.0.content.0.content.0.text', 'Geändert');
$this->assertStringContainsString('Geändert', $component->get('summary'));
}
public function test_opening_a_legacy_exercise_reports_no_unsaved_changes(): void
{
$exercise = $this->exercise('Altbestand');
$composition = $exercise['version']->composition;
$component = Livewire::actingAs($this->editor(), 'staff')
->test(EditPractice::class, ['record' => $composition->getKey()])
->assertOk();
$this->assertStringNotContainsString('ungespeicherte', $component->instance()->editorStatus());
$this->assertSame('Nein sagen und Abstand halten', $component->get('rich.learningGoal.content.0.content.0.text'), 'Das Lernziel steht im Editor.');
$this->assertFalse($component->instance()->textFieldsChanged());
}
public function test_an_untouched_legacy_exercise_creates_no_new_version_when_released_again(): void
{
$exercise = $this->exercise('Altbestand');
$composition = $exercise['version']->composition;
Livewire::actingAs($this->editor(), 'staff')
->test(EditPractice::class, ['record' => $composition->getKey()])
->call('saveDraft')
->call('armRelease')
->set('releaseConsent', true)
->call('publishRevision');
$this->assertSame(1, $composition->versions()->count());
$this->assertSame('Nein sagen und Abstand halten', $composition->versions()->first()->learning_goal, 'Der Altbestand bleibt Klartext.');
}
public function test_formatting_the_learning_goal_is_a_real_change_and_creates_a_version(): void
{
$exercise = $this->exercise('Altbestand');
$composition = $exercise['version']->composition;
Livewire::actingAs($this->editor(), 'staff')
->test(EditPractice::class, ['record' => $composition->getKey()])
->set('learningGoal', '<p>Nein sagen und <strong>Abstand</strong> halten</p>')
->call('saveDraft')
->call('armRelease')
->set('releaseConsent', true)
->call('publishRevision');
$this->assertSame(2, $composition->versions()->count());
$this->assertSame('<p>Nein sagen und <strong>Abstand</strong> halten</p>', $composition->versions()->orderByDesc('number')->first()->learning_goal);
$this->assertSame('Nein sagen und Abstand halten', $composition->versions()->orderBy('number')->first()->learning_goal, 'Die frühere Fassung bleibt unverändert.');
}
public function test_a_medium_keeps_its_plain_description_unless_it_is_edited_and_stores_edits_as_clean_html(): void
{
$content = app(ContentService::class);
$element = $content->createElement('text', 'text', 'Trainerunterlage');
$content->publishElementVersion($element, 'Der Wortlaut');
$content->describe($element, 'Trainerunterlage', 'Alt und schlicht.
- Punkt');
$before = IdentityChange::query()->count();
$component = Livewire::actingAs($this->editor(), 'staff')
->test(ViewContentElement::class, ['record' => $element->getKey()])
->call('openRedaktion')
->call('saveRedaktion');
$this->assertSame('Alt und schlicht.
- Punkt', $element->fresh()->description, 'Speichern ohne Änderung lässt den Altbestand, wie er ist.');
$this->assertSame($before, IdentityChange::query()->count());
$component->set('descriptionText', '<p>Neu <b>fett</b></p><script>x()</script>')->call('saveRedaktion');
$this->assertSame('<p>Neu <strong>fett</strong></p>', $element->fresh()->description);
}
private function editor(): User
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $editor;
}
}
+3 -3
View File
@@ -61,7 +61,7 @@ public function test_a_medium_keeps_the_old_version_when_a_new_one_is_saved(): v
$this->assertCount(2, $versions);
$this->assertSame('Erste Fassung', $versions[0]->body);
$this->assertSame('Zweite Fassung', $versions[1]->body);
$this->assertSame('<p>Zweite Fassung</p>', $versions[1]->body);
$this->assertSame(ContentVersionStatus::Bereit, $versions[0]->status);
}
@@ -152,7 +152,7 @@ public function test_an_exercise_can_gain_and_drop_a_medium_without_losing_the_o
->call('publishRevision')
->assertSet('feedback', 'Fassung 3 ist freigegeben. Der Entwurf ist geschlossen und die bisherige Fassung bleibt erhalten. Die Mitgliederbibliothek ändert sich dadurch nicht.');
$this->assertSame('Den Abstand üben', $exercise->composition->versions()->orderByDesc('number')->first()->learning_goal);
$this->assertSame('<p>Den Abstand üben</p>', $exercise->composition->versions()->orderByDesc('number')->first()->learning_goal);
$this->assertSame(3, $exercise->composition->versions()->count());
}
@@ -232,7 +232,7 @@ public function test_a_description_does_not_create_a_version_and_an_older_versio
->assertSee('Es gibt keine neue Fassung.');
$this->assertSame(2, $element->versions()->count());
$this->assertSame('Für die Gruppe', $element->fresh()->description);
$this->assertSame('<p>Für die Gruppe</p>', $element->fresh()->description);
Livewire::test(ViewContentElement::class, ['record' => $element->getKey()])
->set('bereich', 'inhalt')
+2 -2
View File
@@ -49,7 +49,7 @@ public function test_editorial_text_is_saved_without_a_new_version_and_previewed
$this->assertSame(1, $element->versions()->count());
$this->assertSame($version->id, $element->versions()->first()->id);
$this->assertSame('Lara im Park, neu', $element->display_title);
$this->assertSame('Ein kurzer Teaser für die Karte', $element->summary);
$this->assertSame('<p>Ein kurzer Teaser für die Karte</p>', $element->summary);
$this->assertTrue(
IdentityChange::query()->where('subject_id', $element->id)->where('field', 'summary')->exists()
);
@@ -58,7 +58,7 @@ public function test_editorial_text_is_saved_without_a_new_version_and_previewed
'summary' => 'Noch nicht gespeichert',
]);
$this->assertSame('Noch nicht gespeichert', $draft->summary);
$this->assertSame('Ein kurzer Teaser für die Karte', $element->fresh()->summary);
$this->assertSame('<p>Ein kurzer Teaser für die Karte</p>', $element->fresh()->summary);
Livewire::test(ViewContentElement::class, ['record' => $element->getKey()])
->set('bereich', 'vorschau')
+54
View File
@@ -258,6 +258,60 @@ public function test_the_path_finds_the_place_even_when_the_exercise_sits_deeper
$this->assertNull($guide->pathTo($outer, 999999));
}
public function test_formatted_text_appears_formatted_in_the_guide_and_the_pdf_and_never_carries_markup_that_could_run(): void
{
$content = app(ContentService::class);
$text = $this->section('Formatiert', '<p>Erster Absatz mit <strong>fettem</strong> und <em>schrägem</em> Text.</p><h3>Ablauf</h3><ol><li><p>Erst <strong>aufwärmen</strong></p></li><li><p>Dann üben</p></li></ol><ul><li><p>Matten</p></li></ul><blockquote><p>Ein Zitat</p></blockquote><p><a href="https://example.org/regeln">Regeln</a><script>alert(1)</script></p>');
$composition = $this->exerciseComposition('Formatierte Übung');
$version = $content->publishComposition($composition, [['role' => 'Durchführung', 'target' => $text]], null, null, [
'learning_goal' => '<p>Das <strong>Ziel</strong>:</p><ul><li><p>Stand</p></li><li><p>Stimme</p></li></ul>',
'material_note' => '<p>Matten und <em>Pratzen</em></p>',
]);
$module = $this->moduleWith('Modul formatiert', $version);
$articles = app(ArticleService::class);
$article = $articles->create('formatiert');
$release = $articles->publishRelease($article, $module, 'Formatiert.');
$member = $this->grantTo($this->full(), [$article]);
$page = $this->actingAs($member)->get($this->page($article, $release));
$page->assertOk()
->assertSee('<strong>fettem</strong>', false)
->assertSee('<em>schrägem</em>', false)
->assertSee('<h3>Ablauf</h3>', false)
->assertSee('<li><p>Erst <strong>aufwärmen</strong></p></li>', false)
->assertSee('<blockquote>', false)
->assertSee('<strong>Ziel</strong>', false)
->assertSee('<em>Pratzen</em>', false)
->assertSee('rel="noopener noreferrer"', false)
->assertDontSee('alert(1)', false);
$response = $this->actingAs($member)->get($this->pdf($article, $release, null, $member));
$response->assertOk();
$pdf = $response->getContent();
$text = $this->pdfText($pdf);
$this->assertStringContainsString('/BaseFont /Helvetica-Oblique', $pdf, 'Kursiv braucht die schräge Schrift.');
$this->assertMatchesRegularExpression('/\/F2 [0-9.]+ Tf [0-9. ]+ Td \( ?fettem\)/', $pdf, 'Fettes Wort in fetter Schrift.');
$this->assertMatchesRegularExpression('/\/F3 [0-9.]+ Tf [0-9. ]+ Td \( ?schr\xE4gem\)/', $pdf, 'Kursives Wort in schräger Schrift.');
$this->assertStringContainsString('Ablauf', $text);
$this->assertStringContainsString('Erst', $text);
$this->assertStringContainsString('Ein Zitat', $text);
$this->assertStringContainsString('(https://example.org/regeln)', $text, 'Der Link erscheint auf Papier mit Adresse.');
$this->assertStringNotContainsString('alert(1)', $text);
$this->assertStringContainsString('Lernziel:', $text);
$this->assertStringContainsString('Ausstattung:', $text);
$this->assertStringNotContainsString('<strong>', $text, 'Im PDF steht keine Auszeichnung als Text.');
}
public function test_legacy_plain_text_reads_exactly_as_before(): void
{
$this->actingAs($this->member)
->get($this->page($this->moduleArticle, $this->moduleRelease))
->assertOk()
->assertSee('<ul><li><p>Sicher stehen</p></li><li><p>Deutlich sprechen</p></li></ul>', false)
->assertSee('Nein sagen und Abstand halten');
}
private function page(LicenseArticle $article, ArticleRelease $release): string
{
return route('member.exercise', ['article' => $article, 'release' => $release, 'path' => '1']);
+2 -2
View File
@@ -656,7 +656,7 @@ public function test_saving_a_released_text_selects_the_new_version(): void
->assertSee('Fassung 3 ist angelegt. Fassung 1 bleibt erhalten.');
$third = $text->versions()->where('number', 3)->firstOrFail();
$component->assertSet('fassung', $third->id)->assertSet('bodyDraft', 'Dritter Wortlaut');
$component->assertSet('fassung', $third->id)->assertSet('bodyDraft', '<p>Dritter Wortlaut</p>');
$this->assertSame('Erster Wortlaut', $first->fresh()->body);
}
@@ -739,7 +739,7 @@ public function test_a_new_version_from_the_dialog_is_shown_with_its_own_text():
$second = $text->versions()->where('number', 2)->firstOrFail();
$component->assertSet('fassung', $second->id)
->assertSet('bodyDraft', 'Neuer Wortlaut')
->assertSet('bodyDraft', '<p>Neuer Wortlaut</p>')
->assertDontSee('Text der Fassung nicht gespeichert');
}