Führe Übungen als Fassungen mit Varianten, Verwendung und Mitgliederausgabe ein
CI Feature / test (push) Successful in 3m32s

Neuer kind 'exercise', Varianten je Fassung, Verwendung (erforderlich/ergänzend) und Variantenauswahl am einbindenden
Mitglied; der Stand schreibt nur gewählte Varianten fest. Änderungsnachweis über structure_hash. Formatierte Anleitung
im Mitgliederbereich und PDF (Fußzeile, Metadaten, Ausgabenachweis, keine gespeicherten Kopien); Sperre trifft nur das
Medium.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-23 16:52:01 +01:00
1 parent ef56befc0e
commit d1f97a351a
26 files changed
+1628 -15

No files matched your search

+3 -2
View File
@@ -36,7 +36,7 @@ class RueckfallschutzTest extends TestCase
{
use RefreshDatabase;
private const FOREIGN = 'exercise';
private const FOREIGN = 'art_von_morgen';
private ContentElementVersion $filmA;
@@ -64,7 +64,8 @@ protected function setUp(): void
public function test_the_kind_values_are_the_ones_this_program_state_knows(): void
{
$this->assertSame(['kombination', 'uebung', 'kurs'], CompositionKind::values());
$this->assertSame(['kombination', 'uebung', 'kurs', 'exercise'], CompositionKind::values());
$this->assertNotContains(self::FOREIGN, CompositionKind::values());
}
public function test_a_composition_of_an_unknown_kind_does_not_crash_loading_and_stays_hidden(): void
+296
View File
@@ -0,0 +1,296 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleRelease;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\ExerciseGuide;
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_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_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));
}
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],
));
}
}
+166
View File
@@ -0,0 +1,166 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\ContentResolver;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\DomainRuleException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Support\BuildsExercises;
use Tests\TestCase;
class UebungFassungTest extends TestCase
{
use BuildsExercises;
use RefreshDatabase;
public function test_an_exercise_version_keeps_its_fields_variants_and_placement(): void
{
$exercise = $this->exercise();
$version = $exercise['version']->fresh(['variants', 'members.variant']);
$this->assertSame(CompositionKind::Exercise, $version->composition->kind);
$this->assertSame(['a', 'b'], $version->variants->pluck('key')->all());
$this->assertSame(20, $version->duration_max);
$this->assertSame('Keine', $version->prerequisites);
$this->assertNull($version->members[0]->variant, 'Der Einstieg ist gemeinsam.');
$this->assertSame('a', $version->members[2]->variant->key);
$this->assertSame('ergaenzend', $version->members[4]->requirement);
$this->assertNotNull($version->structure_hash);
$this->assertSame($version->structure_hash, $version->attestations()->first()->structure_hash);
}
public function test_the_selection_decides_which_media_a_stand_freezes(): void
{
$exercise = $this->exercise();
$withFilm = $this->moduleWith('Modul mit Film', $exercise['version'], ['a']);
$withoutFilm = $this->moduleWith('Modul ohne Film', $exercise['version'], ['b']);
$articles = app(ArticleService::class);
$first = $articles->publishRelease($articles->create('mit-film'), $withFilm, 'Filmvariante.');
$second = $articles->publishRelease($articles->create('ohne-film'), $withoutFilm, 'Variante ohne Film.');
$this->assertEqualsCanonicalizing(
[$exercise['intro']->id, $exercise['textA']->id, $exercise['filmA']->id],
$first->contents->pluck('content_element_version_id')->all(),
);
$this->assertEqualsCanonicalizing(
[$exercise['intro']->id, $exercise['textB']->id, $exercise['sheetB']->id],
$second->contents->pluck('content_element_version_id')->all(),
);
$this->assertSame(
['erforderlich', 'ergaenzend'],
$second->contents->whereIn('content_element_version_id', [$exercise['textB']->id, $exercise['sheetB']->id])->sortBy('position')->pluck('requirement')->map(fn ($r, $i) => $i === 0 ? 'erforderlich' : $r)->values()->all(),
);
$this->assertSame('ergaenzend', $second->contents->firstWhere('content_element_version_id', $exercise['sheetB']->id)->requirement);
$this->assertSame('b', $second->contents->firstWhere('content_element_version_id', $exercise['sheetB']->id)->variant_key);
$this->assertNull($second->contents->firstWhere('content_element_version_id', $exercise['intro']->id)->variant_key);
}
public function test_without_a_selection_all_variants_are_offered(): void
{
$exercise = $this->exercise();
$all = $this->moduleWith('Modul mit allen', $exercise['version']);
$this->assertCount(5, app(ContentResolver::class)->expand($all));
}
public function test_the_same_medium_in_the_common_part_and_a_variant_and_directly_in_the_product_is_frozen_each_time(): void
{
$content = app(ContentService::class);
$film = $this->film('Demonstration');
$composition = $this->exerciseComposition('Doppelt');
$exercise = $content->publishComposition($composition, [
['role' => 'Einstieg', 'target' => $film],
['role' => 'Film in A', 'target' => $film, 'variant' => 'a'],
['role' => 'Film in B', 'target' => $film, 'variant' => 'b'],
], null, null, [], [['key' => 'a', 'name' => 'A'], ['key' => 'b', 'name' => 'B']]);
$module = $this->moduleWith('Modul', $exercise, ['a'], CompositionKind::Uebung, $film);
$articles = app(ArticleService::class);
$release = $articles->publishRelease($articles->create('doppelt'), $module, 'Mehrfach.');
$this->assertSame([$film->id, $film->id, $film->id], $release->contents->pluck('content_element_version_id')->all());
$this->assertSame([null, 'a', null], $release->contents->pluck('variant_key')->all());
}
public function test_the_structure_hash_notices_what_the_media_set_does_not(): void
{
$content = app(ContentService::class);
$film = $this->film('Gleicher Film');
$composition = $this->exerciseComposition('Hash');
$variants = [['key' => 'a', 'name' => 'A']];
$first = $content->publishComposition($composition, [['role' => 'Film', 'target' => $film, 'variant' => 'a', 'requirement' => 'erforderlich']], null, null, [], $variants);
$second = $content->publishComposition($composition, [['role' => 'Film', 'target' => $film, 'variant' => 'a', 'requirement' => 'ergaenzend']], null, null, [], $variants);
$third = $content->publishComposition($composition, [['role' => 'Film', 'target' => $film]], null, null, [], $variants);
$this->assertSame($first->resolved_hash, $second->resolved_hash);
$this->assertSame($first->resolved_hash, $third->resolved_hash);
$this->assertCount(3, array_unique([$first->structure_hash, $second->structure_hash, $third->structure_hash]));
}
public function test_two_stands_keep_different_versions_of_the_same_exercise(): void
{
$content = app(ContentService::class);
$exercise = $this->exercise();
$moduleV1 = $this->moduleWith('Modul eins', $exercise['version'], ['a']);
$revisedText = $content->publishElementVersion($exercise['textA']->element, 'Variante A, überarbeitet.');
$secondVersion = $content->publishComposition($exercise['version']->composition, [
['role' => 'Einstieg', 'target' => $exercise['intro']],
['role' => 'Durchführung A', 'target' => $revisedText, 'variant' => 'a'],
['role' => 'Film A', 'target' => $exercise['filmA'], 'variant' => 'a'],
], null, null, ['learning_goal' => 'Neu gefasst'], [['key' => 'a', 'name' => 'Mit Film']]);
$moduleV2 = $this->moduleWith('Modul zwei', $secondVersion, ['a']);
$articles = app(ArticleService::class);
$one = $articles->publishRelease($articles->create('eins'), $moduleV1, 'Alte Übung.');
$two = $articles->publishRelease($articles->create('zwei'), $moduleV2, 'Neue Übung.');
$this->assertContains($exercise['textA']->id, $one->contents->pluck('content_element_version_id')->all());
$this->assertNotContains($revisedText->id, $one->contents->pluck('content_element_version_id')->all());
$this->assertContains($revisedText->id, $two->contents->pluck('content_element_version_id')->all());
$this->assertSame('Nein sagen und Abstand halten', $exercise['version']->fresh()->learning_goal);
$this->assertSame(2, $exercise['version']->composition->versions()->count());
}
public function test_a_published_exercise_version_and_its_variants_stay_unchanged(): void
{
$exercise = $this->exercise();
$this->expectException(DomainRuleException::class);
$exercise['version']->variants()->first()->update(['name' => 'Anders']);
}
public function test_invalid_placements_are_refused(): void
{
$content = app(ContentService::class);
$film = $this->film('Film');
$composition = $this->exerciseComposition('Prüfung');
$variants = [['key' => 'a', 'name' => 'A']];
foreach ([
[['role' => 'x', 'target' => $film, 'variant' => 'zzz'], $variants],
[['role' => 'x', 'target' => $film, 'requirement' => 'vielleicht'], $variants],
[['role' => 'x', 'target' => $film, 'selection' => ['a']], $variants],
] as [$member, $declared]) {
try {
$content->publishComposition($composition, [$member], null, null, [], $declared);
$this->fail('Eine ungültige Zuordnung wurde angenommen.');
} catch (DomainRuleException) {
$this->addToAssertionCount(1);
}
}
$exercise = $this->exercise('Andere');
$module = $this->exerciseComposition('Wirt');
foreach ([['zzz'], []] as $selection) {
try {
$content->publishComposition($module, [['role' => 'Übung', 'target' => $exercise['version'], 'selection' => $selection]]);
$this->fail('Eine ungültige Variantenauswahl wurde angenommen.');
} catch (DomainRuleException) {
$this->addToAssertionCount(1);
}
}
}
}