Führe PDF-Vorlagen für Dokumente ein: Titelseite, grafischer Kopf und Fuß, Angaben je Fassung (ADR-51, Nachtrag)
Eine Vorlage ist reines Layout (Titelseite mit allein stehendem Titel, Kopf- und Fußgrafik aus dem Medienbestand, Herausgeber und Standardvermerk) und wird dem Dokument zugeordnet. Untertitel, Ausgabe, Veröffentlichungsdatum und Copyright-Vermerk gehören zur Dokumentfassung (document_meta). Ohne Vorlage bleibt das PDF unverändert. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
81037b9868
commit
765b8e779e
31 files changed
+1272
-33
No files matched your search
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Domain\Catalog\ArticleRelease;
|
||||
use App\Domain\Catalog\CompositionKind;
|
||||
use App\Domain\Catalog\CompositionVersion;
|
||||
use App\Domain\Catalog\ContentElementVersion;
|
||||
use App\Domain\Catalog\ContentService;
|
||||
use App\Domain\Catalog\DocumentMeta;
|
||||
use App\Domain\Catalog\LicenseArticle;
|
||||
use App\Domain\Catalog\PdfTemplate;
|
||||
use App\Filament\Pages\PdfVorlagen;
|
||||
use App\Filament\Resources\Documents\Pages\EditDocument;
|
||||
use App\Filament\Support\EditorialCatalog;
|
||||
use App\Models\User;
|
||||
use Filament\Facades\Filament;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Livewire\Livewire;
|
||||
use Tests\Support\BuildsDocuments;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* PDF-Vorlagen für Dokumente (ADR-51, Nachtrag): Titelseite mit Ausgabe, Datum und Copyright-Vermerk, grafischer Kopf und Fuß,
|
||||
* Angaben als Teil der Dokumentfassung und eine Verwaltungsseite für die Vorlagen.
|
||||
*/
|
||||
class DokumentVorlageTest extends TestCase
|
||||
{
|
||||
use BuildsDocuments;
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Storage::fake('media_derived');
|
||||
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
|
||||
}
|
||||
|
||||
public function test_a_template_gives_the_title_its_own_cover_page_and_the_other_pages_header_and_footer_graphics(): void
|
||||
{
|
||||
$template = $this->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' => '<p>Text</p>'], composition: $document, meta: ['edition' => 'Ausgabe 1', 'unbekannt' => 'wird verworfen', 'published_on' => 'kein Datum']);
|
||||
$two = $this->document(['Grundlagen' => '<p>Text</p>'], 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' => '<p>Text</p>']);
|
||||
$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' => 'inhalte/entwurf.png']);
|
||||
|
||||
$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'));
|
||||
|
||||
// Eine nicht freigegebene Fassung wird abgelehnt.
|
||||
$component->call('markPicker', 'element:'.$draftImage->id, true)->call('confirmPicker')->assertSet('headerId', null);
|
||||
$component->call('markPicker', 'element:'.$header->id, true)->call('confirmPicker')->assertSet('headerId', $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_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' => '<p>Text</p>'], 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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<string, string>|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' => '<p>'.str_repeat('Haltung zeigen und Abstand halten. ', 40).'</p>',
|
||||
'Haltung' => '<p>'.str_repeat('Klar sprechen. ', 50).'</p>',
|
||||
], 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;
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ protected function chapter(Composition $document, string $title, string $body):
|
||||
*
|
||||
* @param array<string, string> $chapters
|
||||
*/
|
||||
protected function document(array $chapters, string $title = 'Trainerhandbuch', ?Composition $composition = null): CompositionVersion
|
||||
protected function document(array $chapters, string $title = 'Trainerhandbuch', ?Composition $composition = null, ?array $meta = null): CompositionVersion
|
||||
{
|
||||
$composition ??= $this->documentComposition($title);
|
||||
$members = [];
|
||||
@@ -60,16 +60,16 @@ protected function document(array $chapters, string $title = 'Trainerhandbuch',
|
||||
$members[] = ['role' => $sub ? 'Unterkapitel' : 'Kapitel', 'target' => $this->chapter($composition, $name, $body)];
|
||||
}
|
||||
|
||||
return app(ContentService::class)->publishComposition($composition, $members);
|
||||
return app(ContentService::class)->publishComposition($composition, $members, null, null, $meta === null ? [] : ['document_meta' => $meta]);
|
||||
}
|
||||
|
||||
/** Ein Bild im Medienbestand mit echter Datei im abgeleiteten Bestand. */
|
||||
protected function picture(string $title, int $width = 640, int $height = 360): ContentElementVersion
|
||||
protected function picture(string $title, int $width = 640, int $height = 360, array $rgb = [40, 120, 90]): ContentElementVersion
|
||||
{
|
||||
$content = app(ContentService::class);
|
||||
$file = 'inhalte/'.str($title)->slug().'.png';
|
||||
$image = imagecreatetruecolor($width, $height);
|
||||
imagefill($image, 0, 0, (int) imagecolorallocate($image, 40, 120, 90));
|
||||
imagefill($image, 0, 0, (int) imagecolorallocate($image, ...$rgb));
|
||||
ob_start();
|
||||
imagepng($image);
|
||||
Storage::disk('media_derived')->put($file, (string) ob_get_clean());
|
||||
@@ -124,6 +124,18 @@ protected function pdfText(string $pdf): string
|
||||
return implode("\n", $this->pdfPages($pdf));
|
||||
}
|
||||
|
||||
/**
|
||||
* Anzahl der Bilder je Seite (aus den Betriebsmitteln der Seitenobjekte).
|
||||
*
|
||||
* @return list<int>
|
||||
*/
|
||||
protected function pdfPageImages(string $pdf): array
|
||||
{
|
||||
preg_match_all('/\/Type \/Page \/Parent 2 0 R(.*?)\/Contents \d+ 0 R/s', $pdf, $pages);
|
||||
|
||||
return array_map(fn (string $page): int => preg_match_all('/\/Im\d+ \d+ 0 R/', $page), $pages[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Text je Seite. Die Seiteninhalte stehen in der Reihenfolge der Seiten im Dokument.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user