template(); [$article, $release, $member, $document] = $this->sold($template, [ 'subtitle' => 'Handbuch für Trainerinnen und Trainer', 'edition' => 'Ausgabe 2.1 · Stand Dezember 2026', 'published_on' => '2026-12-01', 'copyright' => 'Alle Rechte beim Herausgeber. Weitergabe nur mit Lizenz.', ]); $pdf = $this->actingAs($member)->get(route('member.document.pdf', ['article' => $article, 'release' => $release, 'path' => '1']))->assertOk()->getContent(); $pages = $this->pdfPages($pdf); $images = $this->pdfPageImages($pdf); // Titelseite: der Titel allein, dann die Angaben der Fassung und der Vorlage, unten der Vermerk. $this->assertStringContainsString('Trainerhandbuch', $pages[0]); $this->assertStringContainsString('Handbuch für Trainerinnen und Trainer', $pages[0]); $this->assertStringContainsString('Ausgabe 2.1 · Stand Dezember 2026', $pages[0]); $this->assertStringContainsString('Veröffentlicht am 01.12.2026', $pages[0]); $this->assertStringContainsString('Herausgeber: Lara & Felix', $pages[0]); $this->assertStringContainsString('Alle Rechte beim Herausgeber.', $pages[0]); $this->assertStringNotContainsString('Inhaltsverzeichnis', $pages[0]); $this->assertStringNotContainsString('Grundlagen', $pages[0], 'Kein Kapitel auf der Titelseite.'); $this->assertStringContainsString('Lizenziert für '.$member->name.' · Lizenzkopie LK-', $pages[0], 'Die Kennzeichnung steht auch auf der Titelseite.'); // Inhaltsverzeichnis folgt auf Seite 2; Kapitel beginnen dahinter und das Verzeichnis nennt ihre Seiten. $this->assertStringContainsString('Inhaltsverzeichnis', $pages[1]); $this->assertStringContainsString('Seite 2 von '.count($pages), $pages[1]); $start = collect($pages)->search(fn (string $page, int $index): bool => $index > 1 && str_contains($page, '1 Grundlagen')); $this->assertMatchesRegularExpression('/1 Grundlagen\\s+\\.{3,}\\s+'.($start + 1).'\\s/', $pages[1]); // Bilder: Titelseite nur mit ihrem Bild, alle anderen Seiten mit Kopf und Fuß. $this->assertSame(1, $images[0], 'Die Titelseite trägt nur das Titelbild.'); $this->assertSame(array_fill(0, count($pages) - 1, 2), array_slice($images, 1), 'Jede weitere Seite trägt Kopf und Fuß.'); $this->assertSame(3, substr_count($pdf, '/Subtype /Image'), 'Drei verschiedene Grafiken, jede einmal im Dokument.'); $this->assertStringContainsString($document->composition->display_title ?? 'Trainerhandbuch', $pages[0]); } public function test_without_a_template_the_pdf_keeps_its_simple_layout_and_a_cover_can_be_switched_off(): void { [$article, $release, $member] = $this->sold(null, ['subtitle' => 'Wird ohne Vorlage nicht gedruckt']); $plain = $this->pdfPages($this->actingAs($member)->get(route('member.document.pdf', ['article' => $article, 'release' => $release, 'path' => '1']))->getContent()); $this->assertStringContainsString('Trainerhandbuch', $plain[0]); $this->assertStringContainsString('Inhaltsverzeichnis', $plain[0], 'Ohne Vorlage steht das Verzeichnis wie bisher auf der ersten Seite.'); $this->assertStringNotContainsString('Wird ohne Vorlage nicht gedruckt', implode("\n", $plain)); $template = $this->template(cover: false); [$article, $release, $member] = $this->sold($template, null); $pdf = $this->actingAs($member)->get(route('member.document.pdf', ['article' => $article, 'release' => $release, 'path' => '1']))->getContent(); $pages = $this->pdfPages($pdf); $this->assertStringContainsString('Inhaltsverzeichnis', $pages[0], 'Ohne Titelseite beginnt das Verzeichnis auf Seite 1.'); $this->assertSame(array_fill(0, count($pages), 2), $this->pdfPageImages($pdf), 'Kopf und Fuß stehen auf jeder Seite.'); } public function test_the_fallback_copyright_of_the_template_is_used_when_the_version_has_none(): void { $template = $this->template(copyright: 'Standardvermerk der Vorlage.'); [$article, $release, $member] = $this->sold($template, ['edition' => 'Ausgabe 1']); $first = $this->pdfPages($this->actingAs($member)->get(route('member.document.pdf', ['article' => $article, 'release' => $release, 'path' => '1']))->getContent())[0]; $this->assertStringContainsString('Standardvermerk der Vorlage.', $first); } public function test_the_details_belong_to_the_version_and_a_later_edit_changes_only_the_next_version(): void { $document = $this->documentComposition(); $one = $this->document(['Grundlagen' => '

Text

'], composition: $document, meta: ['edition' => 'Ausgabe 1', 'unbekannt' => 'wird verworfen', 'published_on' => 'kein Datum']); $two = $this->document(['Grundlagen' => '

Text

'], composition: $document, meta: ['edition' => 'Ausgabe 2']); $this->assertSame(['edition' => 'Ausgabe 1'], $one->fresh()->document_meta); $this->assertSame(['edition' => 'Ausgabe 2'], $two->fresh()->document_meta); $this->assertNotSame(app(ContentService::class)->structureHash($one->fresh()), app(ContentService::class)->structureHash($two->fresh()), 'Die Angaben ändern den Änderungsnachweis.'); $plain = $this->document(['Grundlagen' => '

Text

']); $this->assertNull($plain->fresh()->document_meta); $this->assertSame($plain->structure_hash, app(ContentService::class)->structureHash($plain->fresh()), 'Ohne Angaben bleibt der Nachweis wie vor den Vorlagen.'); $this->assertNull(DocumentMeta::normalize(['subtitle' => ' '])); } public function test_the_editor_saves_the_details_in_the_draft_and_binds_them_with_the_document_version(): void { $template = $this->template(); $document = app(EditorialCatalog::class)->create('Trainerhandbuch', CompositionKind::Document); $component = Livewire::actingAs($this->editor(), 'staff')->test(EditDocument::class, ['record' => $document->getKey()]) ->call('addChapter', 1)->call('selectChapter', 0)->set('partTitle', 'Grundlagen')->set('rich.partBody', ['type' => 'doc', 'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => 'Text']]]]]) ->set('docSubtitle', 'Für Trainer')->set('docEdition', 'Ausgabe 1')->set('docPublished', '2026-12-01')->set('docCopyright', 'Vermerk') ->call('saveDraft'); $this->assertSame(['subtitle' => 'Für Trainer', 'edition' => 'Ausgabe 1', 'published_on' => '2026-12-01', 'copyright' => 'Vermerk'], $document->fresh()->working_draft['document_meta']); $component->call('armRelease')->set('releaseConsent', true)->call('publishRevision'); $version = $document->versions()->firstOrFail(); $this->assertSame('Ausgabe 1', $version->document_meta['edition']); // Eine Änderung der Ausgabe allein verlangt eine neue Fassung. $component->set('docEdition', 'Ausgabe 2')->call('armRelease')->assertSet('releaseArmed', true); $component->set('releaseConsent', true)->call('publishRevision'); $this->assertSame(['Ausgabe 1', 'Ausgabe 2'], $document->versions()->orderBy('number')->get()->map(fn ($v) => $v->document_meta['edition'])->all()); // Die Vorlage wird sofort zugeordnet und zeigt sich beim erneuten Öffnen. $component->set('pdfTemplateId', (string) $template->id)->call('savePdfTemplate'); $this->assertSame($template->id, $document->fresh()->pdf_template_id); $reopened = Livewire::actingAs($this->editor(), 'staff')->test(EditDocument::class, ['record' => $document->getKey()]); $this->assertSame((string) $template->id, $reopened->get('pdfTemplateId')); $this->assertSame('Ausgabe 2', $reopened->get('docEdition')); $component->set('pdfTemplateId', '')->call('savePdfTemplate'); $this->assertNull($document->fresh()->pdf_template_id); } public function test_the_template_page_creates_changes_and_deletes_templates_with_images_from_the_picker(): void { $header = $this->picture('Kopf', 2480, 300, [20, 80, 60]); $draftImage = app(ContentService::class)->draftElementVersion(app(ContentService::class)->createElement('bild', 'bild', 'Entwurf'), null, ['file' => $this->store('inhalte/entwurf.png', 1200, 200, [90, 40, 40])]); $component = Livewire::actingAs($this->editor(), 'staff')->test(PdfVorlagen::class)->assertOk(); $this->assertSame([], $component->instance()->templates()); $component->set('name', 'Handbuch')->set('publisher', 'Lara & Felix')->set('copyright', '© Lara & Felix') ->call('openImagePicker', 'header')->assertSet('pickerPurpose', 'vorlagenbild'); $this->assertSame(['bild' => 'Bilder'], $component->instance()->pickerKinds()); $this->assertContains('Kopf', array_column($component->instance()->pickerResult()['items'], 'title')); // Gestaltung braucht keine freigegebene Fassung: auch ein Bild im Entwurf lässt sich wählen. $component->call('markPicker', 'element:'.$draftImage->id, true)->call('confirmPicker')->assertSet('bilder.header', 'element:'.$draftImage->id); $component->call('openImagePicker', 'header')->call('markPicker', 'element:'.$header->id, true)->call('confirmPicker')->assertSet('bilder.header', 'element:'.$header->id)->assertSet('pickerOpen', false); $this->assertTrue($component->instance()->changed()); $component->call('save'); $template = PdfTemplate::query()->firstOrFail(); $this->assertSame(['Handbuch', $header->id, true], [$template->name, $template->header_version_id, $template->show_cover]); $this->assertFalse($component->instance()->changed()); $component->set('showCover', false)->call('clearImage', 'header')->call('save'); $this->assertNull($template->fresh()->header_version_id); $this->assertFalse($template->fresh()->show_cover); $this->actingAs($this->editor(), 'staff')->get(route('editorial.pdf-template-preview', $template))->assertOk()->assertHeader('Content-Type', 'application/pdf'); $component->call('askConfirmation', 'delete')->call('confirmConfirmation'); $this->assertSame(0, PdfTemplate::query()->count()); } public function test_images_without_a_content_version_work_too_presentation_media_and_images_released_for_the_website(): void { $presentation = $this->mediumWithoutVersion('Präsentationsbild', [200, 60, 60], released: false); $web = $this->mediumWithoutVersion('Website-Bild', [60, 60, 200], released: true); $component = Livewire::actingAs($this->editor(), 'staff')->test(PdfVorlagen::class)->set('name', 'Ohne Fassungen')->call('openImagePicker', 'header'); $titles = array_column($component->instance()->pickerResult()['items'], 'title'); $this->assertContains('Präsentationsbild', $titles, 'Der Picker bietet Bilder ohne Inhaltsfassung an.'); $this->assertContains('Website-Bild', $titles); $component->call('markPicker', 'medium:'.$presentation->id, true)->call('confirmPicker')->assertSet('bilder.header', 'medium:'.$presentation->id); $component->call('openImagePicker', 'footer')->call('markPicker', 'medium:'.$web->id, true)->call('confirmPicker')->assertSet('bilder.footer', 'medium:'.$web->id); $component->call('save'); $template = PdfTemplate::query()->firstOrFail(); $this->assertSame([null, $presentation->id, null, $web->id], [$template->header_version_id, $template->header_element_id, $template->footer_version_id, $template->footer_element_id]); $this->assertSame(['medium:'.$presentation->id, 'medium:'.$web->id], [$template->target('header'), $template->target('footer')]); // Beide Bilder stehen im PDF. [$article, $release, $member] = $this->sold($template, null); $pdf = $this->actingAs($member)->get(route('member.document.pdf', ['article' => $article, 'release' => $release, 'path' => '1']))->getContent(); $this->assertSame(2, substr_count($pdf, '/Subtype /Image')); $this->assertNotNull($component->instance()->imageUrl('medium:'.$presentation->id)); $this->assertNotNull($component->instance()->imageUrl('medium:'.$web->id)); } public function test_a_blocked_image_a_missing_file_and_something_that_is_no_image_are_refused(): void { $blocked = $this->picture('Gesperrt', 800, 200); app(ContentService::class)->block($blocked, 'Rechte'); $noFile = $this->mediumWithoutVersion('Ohne Datei', [1, 1, 1], released: false, file: false); $film = $this->film('Ein Film'); $component = Livewire::actingAs($this->editor(), 'staff')->test(PdfVorlagen::class)->call('openImagePicker', 'cover'); foreach (['element:'.$blocked->id, 'medium:'.$noFile->id, 'element:'.$film->id] as $target) { $component->call('markPicker', $target, true)->call('confirmPicker')->assertSet('bilder.cover', null); $component->call('openImagePicker', 'cover'); } } public function test_deleting_a_template_returns_its_documents_to_the_simple_layout(): void { $template = $this->template(); $document = $this->documentComposition(); $document->forceFill(['pdf_template_id' => $template->id])->save(); $template->delete(); $this->assertNull($document->fresh()->pdf_template_id); } public function test_the_staff_preview_of_a_document_uses_its_template_and_the_draft_details(): void { $template = $this->template(); $document = $this->documentComposition(); $document->forceFill(['pdf_template_id' => $template->id, 'working_draft' => ['members' => [], 'document_meta' => ['edition' => 'Entwurfsausgabe 7']]])->save(); $this->document(['Grundlagen' => '

Text

'], composition: $document); $document->forceFill(['working_draft' => ['members' => [['role' => 'Kapitel', 'target' => 'element:'.ContentElementVersion::query()->firstOrFail()->id, 'note' => '']], 'document_meta' => ['edition' => 'Entwurfsausgabe 7']]])->save(); $pdf = $this->actingAs($this->editor(), 'staff')->get(route('editorial.document-preview.pdf', $document))->assertOk()->getContent(); $this->assertStringContainsString('Entwurfsausgabe 7', $this->pdfPages($pdf)[0]); } /** Legt eine Bilddatei im abgeleiteten Bestand ab und liefert ihren Pfad. */ private function store(string $path, int $width, int $height, array $rgb): string { $image = imagecreatetruecolor($width, $height); imagefill($image, 0, 0, (int) imagecolorallocate($image, ...$rgb)); ob_start(); imagepng($image); Storage::disk('media_derived')->put($path, (string) ob_get_clean()); return $path; } /** * Ein Bild ohne Inhaltsfassung: ein Präsentationsmedium (Datei unter `vorschau/`) oder ein für die Website freigegebenes Bild. */ private function mediumWithoutVersion(string $title, array $rgb, bool $released, bool $file = true): ContentElement { $element = app(ContentService::class)->createElement('bild', 'bild', $title); if ($file) { $path = $this->store('vorschau/'.$element->id.'/datei.png', 2480, 240, $rgb); $element->thumbnail_path = $path; if ($released) { Storage::disk('media_public')->put('website/'.$element->id.'/datei.png', (string) Storage::disk('media_derived')->get($path)); $element->website_public_path = 'website/'.$element->id.'/datei.png'; $element->website_released_at = now(); } } $element->save(); return $element; } /** * Eine Vorlage mit Kopf-, Fuß- und Titelbild (drei verschiedene Grafiken). */ private function template(bool $cover = true, ?string $copyright = null): PdfTemplate { return PdfTemplate::query()->create([ 'name' => 'Handbuch', 'kind' => PdfTemplate::KIND_DOCUMENT, 'show_cover' => $cover, 'header_version_id' => $this->picture('Kopf '.uniqid(), 2480, 300, [20, 80, 60])->id, 'footer_version_id' => $this->picture('Fuß '.uniqid(), 2480, 200, [200, 160, 40])->id, 'cover_version_id' => $this->picture('Titel '.uniqid(), 1600, 900, [60, 60, 160])->id, 'publisher' => 'Lara & Felix', 'copyright' => $copyright, ]); } /** * Ein Handbuch in einem verkauften Modul, optional mit Vorlage und Angaben. * * @param array|null $meta * @return array{0: LicenseArticle, 1: ArticleRelease, 2: User, 3: CompositionVersion} */ private function sold(?PdfTemplate $template, ?array $meta): array { $composition = $this->documentComposition(); $composition->forceFill(['pdf_template_id' => $template?->id])->save(); $document = $this->document([ 'Grundlagen' => '

'.str_repeat('Haltung zeigen und Abstand halten. ', 40).'

', 'Haltung' => '

'.str_repeat('Klar sprechen. ', 50).'

', ], composition: $composition, meta: $meta); $module = $this->moduleWith('Modul Handbuch', $document); [$article, $release] = $this->publishFor('handbuch-'.uniqid(), $module); return [$article, $release, $this->memberWith($this->fullRights(), [$article]), $document]; } private function editor(): User { $editor = User::factory()->redakteur()->create(); $editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); return $editor; } }