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>
161 lines
6.2 KiB
PHP
161 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Support;
|
|
|
|
use App\Domain\Catalog\ArticleRelease;
|
|
use App\Domain\Catalog\ArticleService;
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\CompositionKind;
|
|
use App\Domain\Catalog\CompositionVersion;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ContentService;
|
|
use App\Domain\Catalog\DocumentChapters;
|
|
use App\Domain\Catalog\EditorialContentKind;
|
|
use App\Domain\Catalog\LicenseArticle;
|
|
use App\Domain\Catalog\OfferService;
|
|
use App\Domain\Catalog\RightsProfileVersion;
|
|
use App\Domain\Entitlement\GrantIssuer;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* Baut Dokumente für Tests: ein Handbuch mit Kapiteln, Tabellen und Bildern, Module, die eine Dokumentfassung einbinden,
|
|
* Artikel-Stände und Konten mit Rechten.
|
|
*/
|
|
trait BuildsDocuments
|
|
{
|
|
use BuildsExercises;
|
|
|
|
protected function documentComposition(string $title = 'Trainerhandbuch'): Composition
|
|
{
|
|
$composition = app(ContentService::class)->createComposition($title, CompositionKind::Document);
|
|
$composition->editorial_content_kind_id = EditorialContentKind::query()->where('slug', 'dokument')->value('id');
|
|
$composition->save();
|
|
|
|
return $composition;
|
|
}
|
|
|
|
/** Ein Kapitel als bereitgestellte Textfassung im Medienbestand. */
|
|
protected function chapter(Composition $document, string $title, string $body): ContentElementVersion
|
|
{
|
|
$version = $this->section($title, $body);
|
|
app(DocumentChapters::class)->mark($version->element, $document);
|
|
|
|
return $version;
|
|
}
|
|
|
|
/**
|
|
* Ein Dokument aus Kapiteln (Titel => Text; ein Titel mit führendem „-“ ist ein Unterkapitel).
|
|
*
|
|
* @param array<string, string> $chapters
|
|
*/
|
|
protected function document(array $chapters, string $title = 'Trainerhandbuch', ?Composition $composition = null, ?array $meta = null): CompositionVersion
|
|
{
|
|
$composition ??= $this->documentComposition($title);
|
|
$members = [];
|
|
|
|
foreach ($chapters as $chapterTitle => $body) {
|
|
$sub = str_starts_with((string) $chapterTitle, '-');
|
|
$name = ltrim((string) $chapterTitle, '-');
|
|
$members[] = ['role' => $sub ? 'Unterkapitel' : 'Kapitel', 'target' => $this->chapter($composition, $name, $body)];
|
|
}
|
|
|
|
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, 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, ...$rgb));
|
|
ob_start();
|
|
imagepng($image);
|
|
Storage::disk('media_derived')->put($file, (string) ob_get_clean());
|
|
|
|
return $content->publishElementVersion($content->createElement('bild', 'bild', $title), null, ['file' => $file]);
|
|
}
|
|
|
|
protected function publishFor(string $code, CompositionVersion $root, ?LicenseArticle $article = null): array
|
|
{
|
|
$articles = app(ArticleService::class);
|
|
$article ??= $articles->create($code);
|
|
$release = $articles->publishRelease($article, $root, 'Stand '.$code.'.');
|
|
|
|
return [$article, $release];
|
|
}
|
|
|
|
protected function fullRights(): RightsProfileVersion
|
|
{
|
|
return $this->rights(['film' => ['streamen', 'laden'], 'text' => ['ansehen', 'laden'], 'document' => ['ansehen', 'laden'], 'bild' => ['ansehen', 'laden']]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, list<string>> $capabilities
|
|
*/
|
|
protected function rights(array $capabilities): RightsProfileVersion
|
|
{
|
|
return app(OfferService::class)->createRightsProfile('Profil '.md5(json_encode($capabilities)), 'ehrenamtlich', $capabilities);
|
|
}
|
|
|
|
/**
|
|
* @param list<LicenseArticle> $articles
|
|
*/
|
|
protected function memberWith(RightsProfileVersion $rights, array $articles): User
|
|
{
|
|
$offers = app(OfferService::class);
|
|
$offer = $offers->create('Angebot '.uniqid());
|
|
$offers->activate($offer, array_map(fn (LicenseArticle $article): array => ['target' => $article->fresh(), 'rights' => $rights], $articles), ['grant_kind' => 'auf_abruf']);
|
|
$user = User::factory()->create();
|
|
app(GrantIssuer::class)->claim($user, $offer);
|
|
|
|
return $user;
|
|
}
|
|
|
|
protected function documentPage(LicenseArticle $article, ArticleRelease $release, string $path = '1'): string
|
|
{
|
|
return route('member.document', ['article' => $article, 'release' => $release, 'path' => $path]);
|
|
}
|
|
|
|
/** Text der Seiten aus dem unkomprimierten Inhalt; Windows-1252 wird nach UTF-8 gewandelt. */
|
|
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.
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
protected function pdfPages(string $pdf): array
|
|
{
|
|
preg_match_all('/\/Type \/Page \/Parent 2 0 R.*?\/Contents (\d+) 0 R/s', $pdf, $contents);
|
|
$pages = [];
|
|
|
|
foreach ($contents[1] as $object) {
|
|
preg_match('/\n'.$object.' 0 obj\n<< \/Length \d+ >>\nstream\n(.*?)endstream/s', $pdf, $stream);
|
|
preg_match_all('/Td \((.*)\) Tj ET$/m', $stream[1] ?? '', $matches);
|
|
$pages[] = implode("\n", array_map(
|
|
fn (string $line): string => mb_convert_encoding(str_replace(['\\(', '\\)', '\\\\'], ['(', ')', '\\'], $line), 'UTF-8', 'Windows-1252'),
|
|
$matches[1],
|
|
));
|
|
}
|
|
|
|
return $pages;
|
|
}
|
|
}
|