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:
LarsandClaude Sonnet 5.5 committed 2026-11-24 11:31:03 +01:00
1 parent 219962daff
commit c57a506f85
27 files changed
+1729 -219

No files matched your search

+147
View File
@@ -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;
}
}