Biete Tabellen und Bilder im gemeinsamen Texteditor an und setze sie in Ansicht und PDF
Tabellen und Bilder gehören zur festen Auswahl von FormattedText. Ein Bild ist ein Verweis auf eine freigegebene Bildfassung und kommt per Picker aus dem Medienbestand; der Stand schreibt die Bildfassung mit fest, die Rechte entscheidet DecideAccess. Der PDF-Satz (PdfFlow) ist für Übung und kommende Dokumente gemeinsam; PdfDocument kann zusätzlich Bilder, Flächen, Verweise und Lesezeichen, Belege bleiben byte-gleich. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
219962daff
commit
c57a506f85
27 files changed
+1729
-219
No files matched your search
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Domain\Catalog\CompositionKind;
|
||||
use App\Domain\Catalog\ContentService;
|
||||
use App\Domain\Catalog\FormattedText;
|
||||
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\TestCase;
|
||||
|
||||
/**
|
||||
* Derselbe Texteditor für Kapitel und Textelemente (ADR-51): Tabellen überall, Bilder aus dem Medienbestand in Texten.
|
||||
* Der Editor von Filament schreibt kanonisches HTML in die Eigenschaft und bekommt es unverändert zurück; ein Bild bleibt
|
||||
* dabei ein Verweis auf eine Bildfassung.
|
||||
*/
|
||||
class TextEditorWerkzeugeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
|
||||
}
|
||||
|
||||
public function test_a_table_made_in_the_editor_is_stored_as_canonical_html_and_comes_back_unchanged(): void
|
||||
{
|
||||
$practice = app(EditorialCatalog::class)->create('Stopp sagen', CompositionKind::Exercise);
|
||||
$cell = fn (string $type, string $text, array $attrs = []): array => ['type' => $type, 'attrs' => $attrs + ['colspan' => 1, 'rowspan' => 1, 'colwidth' => null], 'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => $text]]]]];
|
||||
|
||||
$component = Livewire::actingAs($this->editor(), 'staff')
|
||||
->test(EditPractice::class, ['record' => $practice->getKey()])
|
||||
->set('rich.partBody', ['type' => 'doc', 'content' => [
|
||||
['type' => 'table', 'content' => [
|
||||
['type' => 'tableRow', 'content' => [$cell('tableHeader', 'Zeit'), $cell('tableHeader', 'Inhalt')]],
|
||||
['type' => 'tableRow', 'content' => [$cell('tableCell', '10 Min.'), $cell('tableCell', 'Begrüßung')]],
|
||||
]],
|
||||
]]);
|
||||
|
||||
$stored = '<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>';
|
||||
$this->assertSame($stored, $component->get('partBody'));
|
||||
|
||||
// Zurück in den Editor und wieder heraus: kein Verlust, keine Scheinänderung.
|
||||
$component->set('partBody', $stored)->assertOk();
|
||||
$this->assertSame('table', $component->get('rich.partBody.content.0.type'));
|
||||
$this->assertSame('Begrüßung', $component->get('rich.partBody.content.0.content.1.content.1.content.0.content.0.text'));
|
||||
$this->assertSame($stored, FormattedText::canonical($component->get('partBody')));
|
||||
}
|
||||
|
||||
public function test_an_image_stays_a_reference_to_its_version_and_the_editor_gets_a_staff_address(): void
|
||||
{
|
||||
$image = $this->image('Stellung');
|
||||
$practice = app(EditorialCatalog::class)->create('Stopp sagen', CompositionKind::Exercise);
|
||||
$stored = '<p>Davor</p><img data-id="'.$image->id.'" alt="Stellung"><p>Danach</p>';
|
||||
|
||||
$component = Livewire::actingAs($this->editor(), 'staff')
|
||||
->test(EditPractice::class, ['record' => $practice->getKey()])
|
||||
->set('partBody', $stored)
|
||||
->assertOk();
|
||||
|
||||
$node = $component->get('rich.partBody.content.1');
|
||||
$this->assertSame('image', $node['type']);
|
||||
$this->assertSame((string) $image->id, (string) $node['attrs']['id']);
|
||||
$this->assertSame(route('content.file', ['version' => $image->id], false), $node['attrs']['src']);
|
||||
|
||||
// Der Editor gibt die Adresse mit zurück; gespeichert wird sie nie.
|
||||
$component->set('rich.partBody', ['type' => 'doc', 'content' => [$node]]);
|
||||
$this->assertSame('<img data-id="'.$image->id.'" alt="Stellung">', $component->get('partBody'));
|
||||
}
|
||||
|
||||
public function test_the_image_tool_exists_only_in_text_editors_with_the_picker(): void
|
||||
{
|
||||
$practice = app(EditorialCatalog::class)->create('Stopp sagen', CompositionKind::Exercise);
|
||||
|
||||
$component = Livewire::actingAs($this->editor(), 'staff')
|
||||
->test(EditPractice::class, ['record' => $practice->getKey()]);
|
||||
$page = $component->instance();
|
||||
|
||||
$this->assertContains('bild', collect($page->richKomponente('partBody')->getToolbarButtons())->flatten()->all());
|
||||
$this->assertContains('bild', collect($page->richKomponente('newTextBody')->getToolbarButtons())->flatten()->all());
|
||||
$this->assertNotContains('bild', collect($page->richKomponente('summary')->getToolbarButtons())->flatten()->all(), 'Kurzangaben nehmen keine Bilder auf.');
|
||||
|
||||
foreach (['partBody', 'summary', 'learningGoal'] as $field) {
|
||||
$this->assertContains('table', collect($page->richKomponente($field)->getToolbarButtons())->flatten()->all(), $field.' hat dieselben Tabellenwerkzeuge.');
|
||||
}
|
||||
}
|
||||
|
||||
public function test_the_picker_offers_released_images_and_inserts_the_chosen_version_at_the_cursor(): void
|
||||
{
|
||||
$image = $this->image('Stellung');
|
||||
$draft = app(ContentService::class)->draftElementVersion(app(ContentService::class)->createElement('bild', 'bild', 'Entwurfsbild'), null, ['file' => 'inhalte/entwurf.jpg']);
|
||||
$practice = app(EditorialCatalog::class)->create('Stopp sagen', CompositionKind::Exercise);
|
||||
|
||||
$component = Livewire::actingAs($this->editor(), 'staff')
|
||||
->test(EditPractice::class, ['record' => $practice->getKey()])
|
||||
->call('openTextImagePicker', 'partBody', ['type' => 'text', 'anchor' => 1, 'head' => 1])
|
||||
->assertSet('pickerOpen', true)
|
||||
->assertSet('pickerPurpose', 'textbild');
|
||||
|
||||
$this->assertSame('Bild einfügen', $component->instance()->pickerConfirmLabel());
|
||||
$this->assertSame(['bild' => 'Bilder'], $component->instance()->pickerKinds());
|
||||
|
||||
$component->call('markPicker', 'element:'.$image->id, true)->call('confirmPicker');
|
||||
$component->assertSet('pickerOpen', false)->assertDispatched('run-rich-editor-commands');
|
||||
|
||||
// Eine noch nicht freigegebene Fassung lässt sich nicht in einen Text einfügen.
|
||||
$component->call('openTextImagePicker', 'partBody', [])->call('markPicker', 'element:'.$draft->id, true)->call('confirmPicker');
|
||||
$this->assertStringContainsString('noch nicht freigegeben', (string) $component->get('deskFeedback'));
|
||||
}
|
||||
|
||||
public function test_a_text_with_an_unreleased_image_cannot_be_frozen(): void
|
||||
{
|
||||
$content = app(ContentService::class);
|
||||
$draftImage = $content->draftElementVersion($content->createElement('bild', 'bild', 'Entwurfsbild'), null, ['file' => 'inhalte/entwurf.jpg']);
|
||||
$text = $content->draftElementVersion($content->createElement('text', 'text', 'Kapitel'), '<p>Text</p><img data-id="'.$draftImage->id.'" alt="">');
|
||||
|
||||
$this->assertStringContainsString('noch nicht freigegeben', (string) $content->elementReleaseProblem($text));
|
||||
|
||||
$released = $content->releaseElementVersion($draftImage);
|
||||
$this->assertNull($content->elementReleaseProblem($text->refresh()));
|
||||
$this->assertSame($released->id, $draftImage->id);
|
||||
|
||||
$content->block($released, 'Rechte');
|
||||
$this->assertStringContainsString('deaktiviert', (string) $content->imageProblem($text->body));
|
||||
}
|
||||
|
||||
private function image(string $title)
|
||||
{
|
||||
$content = app(ContentService::class);
|
||||
|
||||
return $content->publishElementVersion($content->createElement('bild', 'bild', $title), null, ['file' => 'inhalte/'.str($title)->slug().'.jpg']);
|
||||
}
|
||||
|
||||
private function editor(): User
|
||||
{
|
||||
$editor = User::factory()->redakteur()->create();
|
||||
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
|
||||
|
||||
return $editor;
|
||||
}
|
||||
}
|
||||
@@ -101,4 +101,57 @@ public function test_blocks_carry_the_emphasis_for_the_pdf(): void
|
||||
$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 im Absatz</p>', $html);
|
||||
$this->assertSame([12], FormattedText::imageIds($html));
|
||||
$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_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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user