Files
laraundfelix/tests/Feature/UebungAusgabeTest.php
T
LarsandClaude Sonnet 5.5 ad581cb545
Deploy Test / test (push) Successful in 2m51s
CI Feature / test (push) Successful in 2m49s
Deploy Test / deploy (push) Successful in 35s
Ordne Übungsabschnitte im Medienbestand ein und filtere Materialarten mehrfach
- Medium anlegen: Pflichtfeld Fachliche Materialart (serverseitig geprüft)
- Materialart Übungsabschnitt (automatisch) für Abschnitte im Übungseditor und aus Shinkan, mit Verweis auf die Übung
- Medienmanager: Checkboxen statt Dropdown, Start ohne Übungsabschnitte, persönlicher Standard je Konto
- Zuordnung des Bestands nur bei belegter Herkunft; Übungsabschnitte nie öffentlich

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 07:40:30 +01:00

414 lines
22 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleRelease;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\CompositionDisplayRelease;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\ExerciseGuide;
use App\Domain\Catalog\ExerciseSections;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\OfferService;
use App\Domain\Catalog\RightsProfileVersion;
use App\Domain\Entitlement\GrantIssuer;
use App\Domain\Entitlement\MediaDelivery;
use App\Http\SignedLink;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Support\BuildsExercises;
use Tests\TestCase;
/**
* Durchstich der Ausgabe: Eine Übung steckt in einem verkauften Modul und in einem verkauften Programm. Die berechtigte
* Kursleitung sieht die im Stand gebundene Fassung und lädt das PDF; eine Sperre trifft nur das Medium.
*/
class UebungAusgabeTest extends TestCase
{
use BuildsExercises;
use RefreshDatabase;
/** @var array<string, mixed> */
private array $exercise;
private LicenseArticle $moduleArticle;
private ArticleRelease $moduleRelease;
private LicenseArticle $programArticle;
private ArticleRelease $programRelease;
private User $member;
protected function setUp(): void
{
parent::setUp();
$this->exercise = $this->exercise();
$module = $this->moduleWith('Modul Stopp', $this->exercise['version'], ['a']);
$program = $this->moduleWith('Programm Stopp', $this->exercise['version'], ['b'], CompositionKind::Kurs);
$articles = app(ArticleService::class);
$this->moduleArticle = $articles->create('modul-stopp');
$this->moduleRelease = $articles->publishRelease($this->moduleArticle, $module, 'Modul.');
$this->programArticle = $articles->create('programm-stopp');
$this->programRelease = $articles->publishRelease($this->programArticle, $program, 'Programm.');
$this->member = $this->grantTo($this->full(), [$this->moduleArticle, $this->programArticle]);
}
public function test_the_trainer_reads_the_bound_version_with_the_offered_variant_only(): void
{
$this->actingAs($this->member)
->get($this->page($this->moduleArticle, $this->moduleRelease))
->assertOk()
->assertSee('Stopp sagen')
->assertSee('Nein sagen und Abstand halten')
->assertSee('Sicher stehen')
->assertSee('Variante A: Der Film zeigt die Situation.')
->assertSee('Lizenzkopie LK-')
->assertDontSee('Variante B: Ohne Film mit dem Arbeitsblatt.')
->assertSee('Als PDF herunterladen');
$this->actingAs($this->member)
->get($this->page($this->programArticle, $this->programRelease))
->assertOk()
->assertSee('Variante B: Ohne Film mit dem Arbeitsblatt.')
->assertDontSee('Variante A: Der Film zeigt die Situation.');
}
public function test_the_module_page_leads_to_the_bound_exercise_not_to_the_newest_one(): void
{
$display = app(CompositionDisplayRelease::class);
$display->release($this->moduleRelease->rootCompositionVersion, $this->member);
$display->release($this->programRelease->rootCompositionVersion, $this->member);
$this->actingAs($this->member)
->get(route('member.program', $this->moduleRelease->rootCompositionVersion->composition_id))
->assertOk()
->assertSee(route('member.exercise', ['article' => $this->moduleArticle, 'release' => $this->moduleRelease, 'path' => '1']), false);
$this->actingAs($this->member)
->get(route('member.program', $this->programRelease->rootCompositionVersion->composition_id))
->assertOk()
->assertSee(route('member.exercise', ['article' => $this->programArticle, 'release' => $this->programRelease, 'path' => '1']), false);
}
public function test_the_pdf_carries_the_same_version_the_marking_and_the_delivery_record(): void
{
$response = $this->actingAs($this->member)->get($this->pdf($this->moduleArticle, $this->moduleRelease, 'a'));
$response->assertOk()->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('no-store', (string) $response->headers->get('Cache-Control'));
$text = $this->pdfText($response->getContent());
$this->assertStringContainsString('Stopp sagen', $text);
$this->assertStringContainsString('Variante A: Der Film zeigt die Situation.', $text);
$this->assertStringContainsString('Sicher stehen', $text);
$this->assertStringNotContainsString('Variante B:', $text);
$this->assertStringContainsString('Lizenziert für '.$this->member->name.' · Lizenzkopie LK-', $text);
$this->assertStringContainsString('Seite 1', $text);
$this->assertStringContainsString('Lizenzkopie '.$response->headers->get('X-License-Copy'), $response->getContent(), 'Die Kennung steht auch in den Metadaten.');
$rows = MediaDelivery::query()->where('format', 'pdf')->get();
$this->assertNotEmpty($rows);
$this->assertSame(['k2'], $rows->pluck('marking')->unique()->all());
$this->assertSame(['laden'], $rows->pluck('action')->unique()->all());
$this->assertSame($this->exercise['version']->id, $rows->first()->composition_version_id);
$this->assertSame(hash('sha256', $response->getContent()), $rows->first()->checksum);
}
public function test_the_pdf_runs_over_several_pages_and_keeps_special_characters_readable(): void
{
$content = app(ContentService::class);
$long = implode("\n\n", array_map(fn (int $n): string => "Absatz $n: ".str_repeat('Abstand halten und Stopp sagen. ', 12), range(1, 30)));
$text = $this->section('Lang', "Gewaltprävention → Stopp ≥ 2 m Abstand; Łódź, naïve «Café».\n\n## Ablauf\n\n1. Erst aufwärmen\n2. Dann üben\n\n- Matten\n- Pratzen\n\n".$long);
$composition = $this->exerciseComposition('Lange Übung');
$version = $content->publishComposition($composition, [['role' => 'Durchführung', 'target' => $text]], null, null, ['learning_goal' => 'Lang']);
$module = $this->moduleWith('Modul lang', $version);
$articles = app(ArticleService::class);
$article = $articles->create('lang');
$release = $articles->publishRelease($article, $module, 'Lang.');
$member = $this->grantTo($this->full(), [$article]);
$response = $this->actingAs($member)->get($this->pdf($article, $release, null, $member));
$response->assertOk();
$pdf = $this->pdfText($response->getContent());
$this->assertGreaterThan(2, substr_count($response->getContent(), '/Type /Page /Parent'));
$this->assertStringContainsString('Seite 1', $pdf);
$this->assertStringContainsString('Seite 3', $pdf);
$this->assertStringContainsString('Gewaltprävention -> Stopp >= 2 m Abstand', $pdf);
$this->assertStringContainsString('Lódz', $pdf);
$this->assertStringContainsString('Ablauf', $pdf);
$this->assertStringContainsString('1.
Erst aufwärmen', $pdf);
$this->assertStringContainsString('•', $pdf);
$this->assertSame(substr_count($response->getContent(), '/Type /Page /Parent'), preg_match_all('/Lizenzkopie LK-[0-9A-F]{12}/', $pdf), 'Jede Seite trägt die Fußzeile.');
}
public function test_an_account_without_a_license_gets_neither_the_guide_nor_the_pdf(): void
{
$stranger = User::factory()->create();
$this->actingAs($stranger)->get($this->page($this->moduleArticle, $this->moduleRelease))->assertForbidden();
$this->actingAs($stranger)->get($this->pdf($this->moduleArticle, $this->moduleRelease, 'a', $stranger))->assertForbidden();
$this->actingAs($this->member)->get(route('member.exercise.pdf', ['article' => $this->moduleArticle, 'release' => $this->moduleRelease, 'path' => '1']))->assertForbidden();
}
public function test_without_the_download_right_the_guide_is_readable_but_the_pdf_is_refused(): void
{
$readOnly = $this->grantTo($this->profile(['film' => ['streamen'], 'text' => ['ansehen'], 'document' => ['ansehen']]), [$this->moduleArticle]);
$this->actingAs($readOnly)
->get($this->page($this->moduleArticle, $this->moduleRelease))
->assertOk()
->assertSee('Variante A: Der Film zeigt die Situation.')
->assertDontSee('Als PDF herunterladen');
$this->actingAs($readOnly)->get($this->pdf($this->moduleArticle, $this->moduleRelease, 'a', $readOnly))->assertForbidden();
}
public function test_a_supplementary_medium_that_is_blocked_leaves_the_guide_and_the_pdf_usable(): void
{
app(ContentService::class)->block($this->exercise['sheetB'], 'Rechte zurückgezogen.');
$this->actingAs($this->member)
->get($this->page($this->programArticle, $this->programRelease))
->assertOk()
->assertSee('Variante B: Ohne Film mit dem Arbeitsblatt.')
->assertSee('Dieses ergänzende Material ist derzeit nicht verfügbar')
->assertDontSee('derzeit nicht vollständig durchführbar');
$pdf = $this->pdfText($this->actingAs($this->member)->get($this->pdf($this->programArticle, $this->programRelease, 'b'))->assertOk()->getContent());
$this->assertStringContainsString('Variante B: Ohne Film mit dem Arbeitsblatt.', $pdf);
$this->assertStringNotContainsString('Arbeitsblatt B', $pdf);
$direct = SignedLink::temporary('member.media', now()->addMinutes(10), [
'article' => $this->programArticle->id,
'release' => $this->programRelease->id,
'version' => $this->exercise['sheetB']->id,
'action' => 'ansehen',
'reader' => $this->member->id,
]);
$this->actingAs($this->member)->get($direct)->assertForbidden();
}
public function test_a_required_medium_that_is_blocked_marks_only_its_variant_and_keeps_the_instructions(): void
{
$everything = $this->moduleWith('Modul alle', $this->exercise['version']);
$articles = app(ArticleService::class);
$article = $articles->create('alle');
$release = $articles->publishRelease($article, $everything, 'Alle Varianten.');
$member = $this->grantTo($this->full(), [$article]);
app(ContentService::class)->block($this->exercise['filmA'], 'Rechte zurückgezogen.');
$this->actingAs($member)
->get($this->page($article, $release))
->assertOk()
->assertSee('Variante A: Der Film zeigt die Situation.')
->assertSee('Diese Variante ist derzeit nicht vollständig durchführbar')
->assertSee('Stopp sagen · Film A ist nicht verfügbar');
$this->actingAs($member)
->get($this->page($article, $release).'?variante=b')
->assertOk()
->assertSee('Variante B: Ohne Film mit dem Arbeitsblatt.')
->assertDontSee('Diese Variante ist derzeit nicht vollständig durchführbar');
$this->actingAs($member)->get($this->pdf($article, $release, 'a', $member))->assertOk();
$direct = SignedLink::temporary('member.media', now()->addMinutes(10), [
'article' => $article->id,
'release' => $release->id,
'version' => $this->exercise['filmA']->id,
'action' => 'streamen',
'reader' => $member->id,
]);
$this->actingAs($member)->get($direct)->assertForbidden();
}
public function test_the_stand_keeps_what_was_chosen_when_a_newer_exercise_version_appears(): void
{
$content = app(ContentService::class);
$revised = $content->publishElementVersion($this->exercise['textA']->element, 'Variante A, später überarbeitet.');
$content->publishComposition($this->exercise['version']->composition, [
['role' => 'Durchführung A', 'target' => $revised, 'variant' => 'a'],
], null, null, ['learning_goal' => 'Neu'], [['key' => 'a', 'name' => 'Mit Film']]);
$this->actingAs($this->member)
->get($this->page($this->moduleArticle, $this->moduleRelease))
->assertOk()
->assertSee('Variante A: Der Film zeigt die Situation.')
->assertDontSee('später überarbeitet')
->assertSee('Nein sagen und Abstand halten');
}
public function test_two_product_stands_show_their_own_text_versions_and_an_exercise_of_sections_only_gives_a_pdf(): void
{
$content = app(ContentService::class);
$articles = app(ArticleService::class);
$exercise = $this->exerciseComposition('Nur Abschnitte');
$section = $this->section('Ablauf', '<p>Stand eins des Abschnitts.</p>');
app(ExerciseSections::class)->mark($section->element, $exercise);
$first = $content->publishComposition($exercise, [['role' => 'Durchführung', 'target' => $section]]);
$moduleOne = $this->moduleWith('Modul eins', $first);
$articleOne = $articles->create('nur-abschnitte-eins');
$releaseOne = $articles->publishRelease($articleOne, $moduleOne, 'Eins.');
$revised = $content->publishElementVersion($section->element, '<p>Stand zwei des Abschnitts.</p>');
$second = $content->publishComposition($exercise, [['role' => 'Durchführung', 'target' => $revised]]);
$moduleTwo = $this->moduleWith('Modul zwei', $second);
$articleTwo = $articles->create('nur-abschnitte-zwei');
$releaseTwo = $articles->publishRelease($articleTwo, $moduleTwo, 'Zwei.');
// Der Abschnitt wird außerdem direkt in einem Modul verwendet und bleibt dort erreichbar.
$direct = $this->moduleWith('Modul direkt', $second, null, CompositionKind::Uebung, $revised);
$articleDirect = $articles->create('nur-abschnitte-direkt');
$articles->publishRelease($articleDirect, $direct, 'Direkt.');
$member = $this->grantTo($this->full(), [$articleOne, $articleTwo, $articleDirect]);
$this->actingAs($member)->get($this->page($articleOne, $releaseOne))->assertOk()
->assertSee('Stand eins des Abschnitts.')->assertDontSee('Stand zwei des Abschnitts.');
$this->actingAs($member)->get($this->page($articleTwo, $releaseTwo))->assertOk()
->assertSee('Stand zwei des Abschnitts.')->assertDontSee('Stand eins des Abschnitts.');
$pdfOne = $this->actingAs($member)->get($this->pdf($articleOne, $releaseOne, null, $member));
$pdfOne->assertOk()->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('Stand eins des Abschnitts.', $this->pdfText($pdfOne->getContent()));
$this->assertStringNotContainsString('Stand zwei', $this->pdfText($pdfOne->getContent()));
$this->assertStringContainsString('Stand zwei des Abschnitts.', $this->pdfText($this->actingAs($member)->get($this->pdf($articleTwo, $releaseTwo, null, $member))->getContent()));
app(CompositionDisplayRelease::class)->release($direct, $member);
$page = $this->actingAs($member)
->get(route('member.content', ['composition' => $direct->composition_id, 'version' => $revised->id]))
->assertOk()
->assertSee('Ablauf');
$this->assertSame(1, preg_match('#href="([^"]+/ansehen\?[^"]+)"#', $page->getContent(), $link), 'Die Seite bietet „Ansehen“ an.');
$this->actingAs($member)->get(html_entity_decode($link[1]))->assertOk()->assertSee('Stand zwei des Abschnitts.');
}
public function test_a_wrong_path_is_not_found(): void
{
$this->actingAs($this->member)->get(route('member.exercise', ['article' => $this->moduleArticle, 'release' => $this->moduleRelease, 'path' => '7']))->assertNotFound();
$this->actingAs($this->member)->get(route('member.exercise', ['article' => $this->moduleArticle, 'release' => $this->programRelease, 'path' => '1']))->assertForbidden();
}
public function test_the_path_finds_the_place_even_when_the_exercise_sits_deeper(): void
{
$inner = $this->moduleWith('Innen', $this->exercise['version'], ['a']);
$outer = $this->moduleWith('Außen', $inner, null, CompositionKind::Kurs);
$guide = app(ExerciseGuide::class);
$this->assertSame('1-1', $guide->pathTo($outer, $this->exercise['version']->id));
$this->assertNull($guide->pathTo($outer, 999999));
}
public function test_formatted_text_appears_formatted_in_the_guide_and_the_pdf_and_never_carries_markup_that_could_run(): void
{
$content = app(ContentService::class);
$text = $this->section('Formatiert', '<p>Erster Absatz mit <strong>fettem</strong> und <em>schrägem</em> Text.</p><h3>Ablauf</h3><ol><li><p>Erst <strong>aufwärmen</strong></p></li><li><p>Dann üben</p></li></ol><ul><li><p>Matten</p></li></ul><blockquote><p>Ein Zitat</p></blockquote><p><a href="https://example.org/regeln">Regeln</a><script>alert(1)</script></p>');
$composition = $this->exerciseComposition('Formatierte Übung');
$version = $content->publishComposition($composition, [['role' => 'Durchführung', 'target' => $text]], null, null, [
'learning_goal' => '<p>Das <strong>Ziel</strong>:</p><ul><li><p>Stand</p></li><li><p>Stimme</p></li></ul>',
'material_note' => '<p>Matten und <em>Pratzen</em></p>',
]);
$module = $this->moduleWith('Modul formatiert', $version);
$articles = app(ArticleService::class);
$article = $articles->create('formatiert');
$release = $articles->publishRelease($article, $module, 'Formatiert.');
$member = $this->grantTo($this->full(), [$article]);
$page = $this->actingAs($member)->get($this->page($article, $release));
$page->assertOk()
->assertSee('<strong>fettem</strong>', false)
->assertSee('<em>schrägem</em>', false)
->assertSee('<h3>Ablauf</h3>', false)
->assertSee('<li><p>Erst <strong>aufwärmen</strong></p></li>', false)
->assertSee('<blockquote>', false)
->assertSee('<strong>Ziel</strong>', false)
->assertSee('<em>Pratzen</em>', false)
->assertSee('rel="noopener noreferrer"', false)
->assertDontSee('alert(1)', false);
$response = $this->actingAs($member)->get($this->pdf($article, $release, null, $member));
$response->assertOk();
$pdf = $response->getContent();
$text = $this->pdfText($pdf);
$this->assertStringContainsString('/BaseFont /Helvetica-Oblique', $pdf, 'Kursiv braucht die schräge Schrift.');
$this->assertMatchesRegularExpression('/\/F2 [0-9.]+ Tf [0-9. ]+ Td \( ?fettem\)/', $pdf, 'Fettes Wort in fetter Schrift.');
$this->assertMatchesRegularExpression('/\/F3 [0-9.]+ Tf [0-9. ]+ Td \( ?schr\xE4gem\)/', $pdf, 'Kursives Wort in schräger Schrift.');
$this->assertStringContainsString('Ablauf', $text);
$this->assertStringContainsString('Erst', $text);
$this->assertStringContainsString('Ein Zitat', $text);
$this->assertStringContainsString('(https://example.org/regeln)', $text, 'Der Link erscheint auf Papier mit Adresse.');
$this->assertStringNotContainsString('alert(1)', $text);
$this->assertStringContainsString('Lernziel:', $text);
$this->assertStringContainsString('Ausstattung:', $text);
$this->assertStringNotContainsString('<strong>', $text, 'Im PDF steht keine Auszeichnung als Text.');
}
public function test_legacy_plain_text_reads_exactly_as_before(): void
{
$this->actingAs($this->member)
->get($this->page($this->moduleArticle, $this->moduleRelease))
->assertOk()
->assertSee('<ul><li><p>Sicher stehen</p></li><li><p>Deutlich sprechen</p></li></ul>', false)
->assertSee('Nein sagen und Abstand halten');
}
private function page(LicenseArticle $article, ArticleRelease $release): string
{
return route('member.exercise', ['article' => $article, 'release' => $release, 'path' => '1']);
}
private function pdf(LicenseArticle $article, ArticleRelease $release, ?string $variant, ?User $reader = null): string
{
return SignedLink::temporary('member.exercise.pdf', now()->addMinutes(10), [
'article' => $article->id,
'release' => $release->id,
'path' => '1',
'variante' => $variant,
'reader' => ($reader ?? $this->member)->id,
]);
}
private function full(): RightsProfileVersion
{
return $this->profile(['film' => ['streamen', 'laden'], 'text' => ['ansehen', 'laden'], 'document' => ['ansehen', 'laden']]);
}
/**
* @param array<string, list<string>> $capabilities
*/
private function profile(array $capabilities): RightsProfileVersion
{
return app(OfferService::class)->createRightsProfile('Profil '.md5(json_encode($capabilities)), 'ehrenamtlich', $capabilities);
}
/**
* @param list<LicenseArticle> $articles
*/
private function grantTo(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;
}
/** Text der Seiten aus dem unkomprimierten Inhalt; Windows-1252 wird nach UTF-8 gewandelt. */
private function pdfText(string $pdf): string
{
preg_match_all('/Td \((.*)\) Tj ET$/m', $pdf, $matches);
return implode("\n", array_map(
fn (string $line): string => mb_convert_encoding(str_replace(['\\(', '\\)', '\\\\'], ['(', ')', '\\'], $line), 'UTF-8', 'Windows-1252'),
$matches[1],
));
}
}