Files
laraundfelix/tests/Feature/DokumentEditorTest.php
T
LarsandClaude Sonnet 5.5 81037b9868
Deploy Test / test (push) Successful in 3m0s
Deploy Test / deploy (push) Successful in 25s
Lasse Bilder im Text den Rechten des Textes folgen, gib Bild- und PDF-Adressen ohne Ablauf aus und belege die Abnahme
- Bilder im Text folgen dem Recht am Text; eine eigene Fähigkeit für Bilder ist nicht nötig (ADR-51, fachliche Entscheidung zur Bestätigung).
- Bildadresse und PDF-Adresse des Dokuments laufen nicht ab; jeder Abruf entscheidet neu.
- Die Fähigkeit text:laden ist im Rechteprofil wählbar.
- Dokumentkapitel in der Medienansicht verweisen auf ihr Dokument; die Pille nennt jetzt auch Dokumentkapitel (MedienFilterDialogTest bewusst angepasst).
- Abnahmeprotokoll mit Bildern unter docs/ux/verwaltung/abnahme/dokumente.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 12:55:21 +01:00

261 lines
14 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\CompositionVersion;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\EditorialContentKind;
use App\Filament\Resources\Documents\Pages\EditDocument;
use App\Filament\Resources\Exercises\Pages\EditExercise as EditModule;
use App\Filament\Support\EditorialCatalog;
use App\Filament\Support\MediaLibrary;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Dokumente bearbeiten (ADR-51): Kapitel und Unterkapitel anlegen, ordnen und schreiben, ohne dass offene Eingaben beim
* Wechsel verloren gehen; speichern als Entwurf; bereitstellen als zusammenhängende Dokumentfassung.
*/
class DokumentEditorTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
}
public function test_a_document_is_created_under_inhalte_and_opens_the_chapter_workplace(): void
{
$document = app(EditorialCatalog::class)->create('Trainerhandbuch', CompositionKind::Document);
$this->assertSame(CompositionKind::Document, $document->kind);
$this->assertSame('dokument', $document->editorialContentKind?->slug);
$this->assertStringEndsWith('/dokumente/'.$document->id, EditorialCatalog::editUrl($document));
$this->actingAs($this->editor(), 'staff')->get('/verwaltung/dokumente')->assertOk()->assertSee('Dokument anlegen')->assertSee('Trainerhandbuch');
$this->actingAs($this->editor(), 'staff')->get('/verwaltung/dokumente/'.$document->id)->assertOk()->assertSee('Kapitelübersicht')->assertSee('Erstes Kapitel anlegen');
}
public function test_chapters_and_subchapters_get_numbers_from_their_order_and_are_marked_as_chapters_of_the_document(): void
{
[$component, $document] = $this->open();
$component->call('addChapter', 1)->call('addChapter', 2)->call('addChapter', 2)->call('addChapter', 1);
$this->assertSame(['1', '1.1', '1.2', '2'], array_column($component->instance()->chapterRows(), 'number'));
$this->assertSame([1, 2, 2, 1], array_column($component->instance()->chapterRows(), 'level'));
$element = ContentElementVersion::query()->with('element.editorialContentKind')->get()->first()->element;
$this->assertSame('dokumentkapitel', $element->editorialContentKind->slug);
$this->assertSame($document->id, $element->origin_composition_id);
}
public function test_switching_chapters_keeps_open_input_and_the_outline_and_nothing_is_written_before_saving(): void
{
[$component, $document] = $this->open();
$component->call('addChapter', 1)->call('addChapter', 1)->call('addChapter', 1);
$first = $component->get('members.0.target');
$component->call('selectChapter', 0)
->set('partTitle', 'Grundlagen')
->set('rich.partBody', $this->doc('Erster Text'))
->call('selectChapter', 1)
->set('partTitle', 'Aufbau')
->set('rich.partBody', $this->doc('Zweiter Text'))
->call('selectChapter', 2)
->call('selectChapter', 0);
$this->assertSame('Grundlagen', $component->get('partTitle'), 'Titel des ersten Kapitels ist wieder da.');
$this->assertSame('<p>Erster Text</p>', $component->get('partBody'));
$this->assertCount(2, $component->get('chapterEdits'), 'Beide offenen Kapitel sind im Arbeitsstand.');
$this->assertSame(['Grundlagen', 'Aufbau', 'Neues Kapitel'], array_column($component->instance()->chapterRows(), 'title'));
$this->assertStringContainsString('ungespeicherte Änderungen', $component->instance()->editorStatus());
$this->assertSame('<p></p>', ContentElementVersion::query()->find((int) substr($first, 8))->body, 'Gespeichert ist noch nichts.');
}
public function test_saving_writes_every_chapter_as_a_draft_and_the_draft_survives_reopening(): void
{
[$component, $document] = $this->open();
$component->call('addChapter', 1)->call('addChapter', 2)
->call('selectChapter', 0)->set('partTitle', 'Grundlagen')->set('rich.partBody', $this->doc('Erster Text'))
->call('selectChapter', 1)->set('partTitle', 'Haltung')->set('rich.partBody', $this->doc('Zweiter Text'))
->call('saveDraft');
$this->assertSame([], $component->get('chapterEdits'));
$this->assertStringNotContainsString('ungespeicherte', $component->instance()->editorStatus());
$this->assertSame(0, $document->versions()->count(), 'Es entsteht keine Dokumentfassung.');
$versions = ContentElementVersion::query()->with('element')->orderBy('id')->get();
$this->assertSame(['Grundlagen', 'Haltung'], $versions->pluck('element.working_title')->all());
$this->assertSame('<p>Erster Text</p>', $versions[0]->body);
$this->assertSame(ContentVersionStatus::Entwurf, $versions[0]->status);
$reopened = Livewire::actingAs($this->editor(), 'staff')->test(EditDocument::class, ['record' => $document->getKey()]);
$this->assertSame(['Grundlagen', 'Haltung'], array_column($reopened->instance()->chapterRows(), 'title'));
$this->assertSame(['Kapitel', 'Unterkapitel'], array_column($reopened->get('members'), 'role'));
}
public function test_reordering_moves_a_chapter_with_its_subchapters_and_keeps_the_open_text(): void
{
[$component] = $this->open();
$component->call('addChapter', 1)->call('addChapter', 2)->call('addChapter', 1)->call('addChapter', 2)->call('addChapter', 2);
$titles = ['A', 'A1', 'B', 'B1', 'B2'];
foreach ($titles as $index => $title) {
$component->call('selectChapter', $index)->set('partTitle', $title);
}
$component->call('selectChapter', 2)->set('rich.partBody', $this->doc('Text von B'));
$component->call('movePart', 2, -1);
$this->assertSame(['B', 'B1', 'B2', 'A', 'A1'], array_column($component->instance()->chapterRows(), 'title'));
$this->assertSame(['1', '1.1', '1.2', '2', '2.1'], array_column($component->instance()->chapterRows(), 'number'));
$this->assertSame(0, $component->get('selected'), 'Das gewählte Kapitel bleibt gewählt.');
$this->assertSame('<p>Text von B</p>', $component->get('partBody'), 'Der offene Text ging nicht verloren.');
// Ziehen: das zweite Kapitel hinter das erste.
$component->call('moveTo', 3, 0);
$this->assertSame(['A', 'A1', 'B', 'B1', 'B2'], array_column($component->instance()->chapterRows(), 'title'));
// Unterkapitel wandern innerhalb des Kapitels und über die Kapitelgrenze.
$component->call('movePart', 4, -1);
$this->assertSame(['A', 'A1', 'B', 'B2', 'B1'], array_column($component->instance()->chapterRows(), 'title'));
$component->call('movePart', 3, -1);
$this->assertSame(['A', 'A1', 'B2', 'B', 'B1'], array_column($component->instance()->chapterRows(), 'title'));
}
public function test_the_level_changes_and_the_first_chapter_stays_a_chapter(): void
{
[$component] = $this->open();
$component->call('addChapter', 1)->call('addChapter', 1);
$component->call('setChapterLevel', 0, 2);
$this->assertSame('Kapitel', $component->get('members.0.role'));
$component->call('setChapterLevel', 1, 2);
$this->assertSame(['1', '1.1'], array_column($component->instance()->chapterRows(), 'number'));
$component->call('setChapterLevel', 1, 1);
$this->assertSame(['1', '2'], array_column($component->instance()->chapterRows(), 'number'));
}
public function test_providing_the_document_creates_one_version_that_binds_the_chapter_versions_in_order(): void
{
[$component, $document] = $this->open();
$component->call('addChapter', 1)->call('addChapter', 2)
->call('selectChapter', 0)->set('partTitle', 'Grundlagen')->set('rich.partBody', $this->doc('Erster Text'))
->call('selectChapter', 1)->set('partTitle', 'Haltung')->set('rich.partBody', $this->doc('Zweiter Text'))
->call('armRelease')
->assertSet('releaseArmed', true);
$this->assertSame([], $component->get('chapterEdits'), 'Die Texte wurden vor der Bereitstellung gespeichert.');
$component->set('releaseConsent', true)->call('publishRevision');
$version = $document->versions()->with('members.contentElementVersion')->first();
$this->assertSame(1, $version->number);
$this->assertSame(ContentVersionStatus::Bereit, $version->status);
$this->assertSame(['Kapitel', 'Unterkapitel'], $version->members->pluck('role')->all());
$this->assertSame(['<p>Erster Text</p>', '<p>Zweiter Text</p>'], $version->members->map(fn ($member) => $member->contentElementVersion->body)->all());
$this->assertTrue($version->members->every(fn ($member) => $member->contentElementVersion->status === ContentVersionStatus::Bereit));
// Eine spätere Bearbeitung ändert die bereitgestellte Fassung nicht: Sie erzeugt Entwürfe und erst mit der Bereitstellung eine zweite Fassung.
$component->call('selectChapter', 0)->set('rich.partBody', $this->doc('Geänderter Text'))->call('saveDraft');
$this->assertSame(1, $document->versions()->count());
$this->assertSame('<p>Erster Text</p>', $version->members()->first()->contentElementVersion()->first()->body);
$this->assertSame(ContentVersionStatus::Entwurf, ContentElementVersion::query()->latest('id')->first()->status);
$component->call('armRelease')->set('releaseConsent', true)->call('publishRevision');
$second = $document->versions()->where('number', 2)->with('members.contentElementVersion')->first();
$this->assertSame('<p>Geänderter Text</p>', $second->members->first()->contentElementVersion->body);
$this->assertSame('<p>Zweiter Text</p>', $second->members->last()->contentElementVersion->body);
}
public function test_a_module_offers_only_provided_document_versions_through_the_shared_picker(): void
{
$provided = $this->documentVersion('Handbuch fertig', 'Kapitel eins');
$draftOnly = app(EditorialCatalog::class)->create('Handbuch im Entwurf', CompositionKind::Document);
$draftOnly->forceFill(['working_draft' => ['members' => [['role' => 'Kapitel', 'target' => 'element:1', 'note' => '']]]])->save();
$module = app(EditorialCatalog::class)->create('Modul', CompositionKind::Uebung);
$component = Livewire::actingAs($this->editor(), 'staff')->test(EditModule::class, ['record' => $module->getKey()])->call('openPicker');
$titles = fn (string $kind): array => array_column($component->set('pickerKind', $kind)->instance()->pickerResult()['items'], 'title');
$this->assertArrayHasKey('dokumentfassung', $component->instance()->pickerKinds());
$this->assertArrayHasKey('dokument', $component->instance()->pickerKinds(), 'Der bisherige Filter für Dokumentdateien bleibt.');
$this->assertSame(['Handbuch fertig'], $titles('dokumentfassung'));
$this->assertContains('Handbuch fertig', $titles('alle'));
$this->assertNotContains('Handbuch im Entwurf', $titles('alle'), 'Ein Dokument ohne bereitgestellte Fassung lässt sich nicht einbinden.');
$this->assertNotContains('Handbuch fertig', $titles('modul'));
$component->call('markPicker', 'composition:'.$provided->id, true)->call('confirmPicker');
$this->assertSame('composition:'.$provided->id, $component->get('members.0.target'));
}
private function documentVersion(string $title, string $chapterTitle): CompositionVersion
{
$content = app(ContentService::class);
$document = app(EditorialCatalog::class)->create($title, CompositionKind::Document);
$chapter = $content->publishElementVersion($content->createElement('text', 'text', $chapterTitle), '<p>Text</p>');
return $content->publishComposition($document, [['role' => 'Kapitel', 'target' => $chapter]]);
}
public function test_a_chapter_shows_in_the_media_library_with_a_link_to_its_document_and_stays_out_of_the_default_list(): void
{
[$component, $document] = $this->open();
$component->call('addChapter', 1)->call('selectChapter', 0)->set('partTitle', 'Grundlagen')->call('saveDraft');
$element = ContentElement::query()->where('working_title', 'Grundlagen')->firstOrFail();
$library = app(MediaLibrary::class);
$card = $library->cardFor($element->fresh(), true);
$this->assertSame('Dokumentkapitel', $card['editorialKind']);
$this->assertSame('Trainerhandbuch', $card['exercise']['title']);
$this->assertSame('Dokument', $card['exercise']['kind']);
$this->assertStringEndsWith('/dokumente/'.$document->getKey(), (string) $card['exercise']['url']);
$this->assertTrue(EditorialContentKind::query()->where('slug', 'dokumentkapitel')->firstOrFail()->isAutomatic(), 'Wie die Übungsabschnitte nur automatisch gesetzt und standardmäßig ausgeblendet.');
}
public function test_a_chapter_cannot_be_saved_without_a_title(): void
{
[$component] = $this->open();
$component->call('addChapter', 1)->call('selectChapter', 0)->set('partTitle', ' ')->call('saveDraft');
$this->assertStringContainsString('braucht einen Titel', (string) $component->get('feedback'));
$this->assertNull(Composition::query()->first()->working_draft);
}
/**
* @return array{0: Testable, 1: Composition}
*/
private function open(): array
{
$document = app(EditorialCatalog::class)->create('Trainerhandbuch', CompositionKind::Document);
return [Livewire::actingAs($this->editor(), 'staff')->test(EditDocument::class, ['record' => $document->getKey()]), $document];
}
/** @return array<string, mixed> */
private function doc(string $text): array
{
return ['type' => 'doc', 'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => $text]]]]];
}
private function editor(): User
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $editor;
}
}