Files
laraundfelix/tests/Feature/CatalogRulesTest.php
T
LarsandCursor fef0ea2ac2
Deploy Test / test (push) Successful in 12s
Deploy Test / deploy (push) Successful in 32s
Halte Auswirkung und Rückzug beim Veröffentlichen eines Artikel-Stands fest.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-15 09:07:14 +01:00

336 lines
13 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\BundleService;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\OfferService;
use App\Domain\Catalog\RightsProfileVersion;
use App\Domain\Entitlement\Acquisition;
use App\Domain\Entitlement\AcquisitionType;
use App\Domain\Entitlement\Grant;
use App\Domain\Entitlement\GrantStatus;
use App\Domain\Identity\Party;
use App\Domain\Identity\PartyType;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CatalogRulesTest extends TestCase
{
use RefreshDatabase;
public function test_a_single_element_becomes_one_license_article(): void
{
$version = $this->element('Arbeitsheft', 'Seite 1');
$release = app(ArticleService::class)->publishRelease(
$this->article('arbeitsheft'),
$version,
'Eigenständiges Arbeitsheft, kein Bestandteil einer Übung.',
);
$this->assertSame([$version->id], $release->contents->pluck('content_element_version_id')->all());
$this->assertSame($version->id, $release->root_element_version_id);
}
public function test_b_composition_fixes_exact_member_versions(): void
{
$film = $this->element('Film', 'Aufnahme');
$sheet = $this->element('Arbeitsblatt', 'Aufgaben');
$composition = app(ContentService::class)->publishComposition(
app(ContentService::class)->createComposition('Übung'),
[
['role' => 'Film', 'target' => $film],
['role' => 'Arbeitsblatt', 'target' => $sheet],
],
);
$release = app(ArticleService::class)->publishRelease(
$this->article('uebung'),
$composition,
'Die Übung wird als ein Lizenzartikel geführt.',
);
$this->assertSame(
[$film->id, $sheet->id],
$release->contents->pluck('content_element_version_id')->all(),
);
$this->assertNotNull($composition->attestations()->first());
}
public function test_c_one_element_version_can_be_reused(): void
{
$shared = $this->element('Merkblatt', 'Hinweise');
$first = app(ContentService::class)->publishComposition(
app(ContentService::class)->createComposition('Übung A'),
[['role' => 'Merkblatt', 'target' => $shared]],
);
$second = app(ContentService::class)->publishComposition(
app(ContentService::class)->createComposition('Übung B'),
[['role' => 'Merkblatt', 'target' => $shared]],
);
$this->assertEqualsCanonicalizing(
[$first->id, $second->id],
app(ContentService::class)->containingCompositionIds($shared),
);
}
public function test_d_nested_composition_resolves_inner_versions_and_rejects_cycles(): void
{
$sheet = $this->element('Arbeitsblatt', 'Aufgaben');
$content = app(ContentService::class);
$inner = $content->publishComposition(
$content->createComposition('Materialsatz'),
[['role' => 'Arbeitsblatt', 'target' => $sheet]],
);
$outer = $content->publishComposition(
$content->createComposition('Übung'),
[['role' => 'Materialsatz', 'target' => $inner]],
);
$release = app(ArticleService::class)->publishRelease(
$this->article('uebung-mit-satz'),
$outer,
'Verschachtelte Übung, derselbe Artikel.',
);
$this->assertSame(['Materialsatz / Arbeitsblatt'], $release->contents->pluck('path')->all());
$this->assertSame([$sheet->id], $release->contents->pluck('content_element_version_id')->all());
$this->expectException(DomainRuleException::class);
$content->publishComposition(
$inner->composition,
[['role' => 'Übung', 'target' => $outer]],
);
}
public function test_e_one_article_can_belong_to_two_bundles(): void
{
$article = $this->publishedArticle('einzel', 'Blatt');
$bundles = app(BundleService::class);
$first = $bundles->publish($bundles->create('Paket eins'), [$article]);
$second = $bundles->publish($bundles->create('Paket zwei'), [$article]);
$this->assertSame([$article->id], $bundles->articleIds($first));
$this->assertSame([$article->id], $bundles->articleIds($second));
}
public function test_f_nested_bundles_reject_cycles_and_deduplicate_articles(): void
{
$article = $this->publishedArticle('gemeinsam', 'Blatt');
$bundles = app(BundleService::class);
$left = $bundles->create('Links');
$right = $bundles->create('Rechts');
$bundles->publish($left, [$article]);
$bundles->publish($right, [$article]);
$outer = $bundles->publish($bundles->create('Außen'), [$left, $right]);
$this->assertSame([$article->id], $bundles->articleIds($outer));
$this->expectException(DomainRuleException::class);
$bundles->publish($left, [$outer->bundle]);
}
public function test_g_a_new_element_version_does_not_change_a_published_composition(): void
{
$content = app(ContentService::class);
$element = $content->createElement('pdf', 'begleitmaterial', 'Anleitung');
$first = $content->publishElementVersion($element, 'Fassung 1');
$composition = $content->publishComposition(
$content->createComposition('Übung'),
[['role' => 'Anleitung', 'target' => $first]],
);
$release = app(ArticleService::class)->publishRelease(
$this->article('anleitung'),
$composition,
'Korrektur bleibt eine Fassung desselben Artikels.',
);
$second = $content->publishElementVersion($element->fresh(), 'Fassung 2');
$composition->refresh()->load('members');
$this->assertSame($first->id, $composition->members->sole()->content_element_version_id);
$this->assertNotSame($second->id, $composition->members->sole()->content_element_version_id);
$this->assertSame(
[$first->id],
$release->fresh()->contents->pluck('content_element_version_id')->all(),
);
$this->expectException(DomainRuleException::class);
$first->body = 'stillschweigend geändert';
$first->save();
}
public function test_h_a_bundle_change_does_not_change_an_activated_offer(): void
{
$first = $this->publishedArticle('grundlage', 'Grundlage');
$second = $this->publishedArticle('erweiterung', 'Erweiterung');
$bundles = app(BundleService::class);
$bundle = $bundles->create('Programm');
$published = $bundles->publish($bundle, [$first]);
$rights = $this->rights();
$offer = app(OfferService::class)->activate(
app(OfferService::class)->create('Programmangebot'),
[['target' => $published, 'rights' => $rights]],
);
$bundles->publish($bundle, [$first, $second]);
$offer->refresh();
$this->assertSame([$first->id], app(OfferService::class)->scopeArticleIds($offer));
$this->assertSame([$first->id], $bundles->articleIds($published->fresh()));
$this->assertNotNull($rights->fresh()->locked_at);
$this->expectException(DomainRuleException::class);
$offer->tax_class = 'unveraendert';
$offer->save();
}
public function test_publication_records_impact_and_withdrawal_restores_the_previous_release(): void
{
$content = app(ContentService::class);
$articles = app(ArticleService::class);
$firstVersion = $this->element('Blatt', 'Fassung 1');
$article = $this->article('blatt');
$first = $articles->publishRelease($article, $firstVersion, 'Erste Einordnung.');
$article = $article->fresh();
$rights = $this->rights();
app(OfferService::class)->activate(
app(OfferService::class)->create('Blattangebot'),
[['target' => $article, 'rights' => $rights]],
);
$party = Party::query()->create([
'type' => PartyType::Person,
'display_name' => 'Mitglied Probe',
]);
$acquisition = Acquisition::query()->create([
'party_id' => $party->id,
'type' => AcquisitionType::FreierAbruf,
'reference_type' => 'offer',
'reference_id' => 1,
]);
Grant::query()->create([
'acquisition_id' => $acquisition->id,
'license_article_id' => $article->id,
'rights_profile_version_id' => $rights->id,
'rights_snapshot' => $rights->capabilities,
'release_at_grant_id' => $first->id,
'status' => GrantStatus::Aktiv,
]);
$secondVersion = $content->publishElementVersion($firstVersion->element->fresh(), 'Fassung 2');
$second = $articles->publishRelease($article->fresh(), $secondVersion, 'Überarbeitung desselben Artikels.');
$this->assertSame(1, $second->impact_report['holders']);
$this->assertSame(['Blattangebot'], $second->impact_report['offers']);
$this->assertSame(1, $second->impact_report['replaces_number']);
$content->block($secondVersion, 'Fehlerhafte Fassung.');
try {
$articles->publishRelease($article->fresh(), $secondVersion, 'Darf nicht erscheinen.');
$this->fail('Eine gesperrte Fassung darf nicht veröffentlicht werden.');
} catch (DomainRuleException) {
}
$this->assertSame($second->id, $article->fresh()->current_release_id);
$result = $articles->withdraw($article->fresh(), 'Die zweite Fassung ist fehlerhaft.');
$this->assertSame(1, $result['fallback_number']);
$this->assertSame(['Mitglied Probe'], $result['holders']);
$this->assertSame($first->id, $article->fresh()->current_release_id);
$this->assertSame('gesperrt', $second->fresh()->status->value);
}
public function test_each_holder_gets_an_own_acquisition_and_one_grant_per_article(): void
{
$article = $this->publishedArticle('frei', 'Kostenloses Blatt');
$rights = $this->rights();
$offer = app(OfferService::class)->create('Kostenlos');
app(OfferService::class)->activate($offer, [
['target' => $article, 'rights' => $rights],
]);
$acquisitions = [];
foreach (['Mitglied Eins', 'Mitglied Zwei'] as $name) {
$party = Party::query()->create([
'type' => PartyType::Person,
'display_name' => $name,
]);
$acquisitions[] = Acquisition::query()->create([
'party_id' => $party->id,
'type' => AcquisitionType::FreierAbruf,
'reference_type' => 'offer',
'reference_id' => $offer->id,
]);
}
$this->assertCount(2, $acquisitions);
try {
Acquisition::query()->create([
'party_id' => $acquisitions[0]->party_id,
'type' => AcquisitionType::FreierAbruf,
'reference_type' => 'offer',
'reference_id' => $offer->id,
]);
$this->fail('Derselbe Inhaber darf dasselbe Angebot nicht ein zweites Mal abrufen.');
} catch (QueryException) {
}
Grant::query()->create([
'acquisition_id' => $acquisitions[0]->id,
'license_article_id' => $article->id,
'rights_profile_version_id' => $rights->id,
'rights_snapshot' => $rights->capabilities,
'release_at_grant_id' => $article->current_release_id,
'status' => GrantStatus::Aktiv,
]);
$this->expectException(QueryException::class);
Grant::query()->create([
'acquisition_id' => $acquisitions[0]->id,
'license_article_id' => $article->id,
'rights_profile_version_id' => $rights->id,
'rights_snapshot' => $rights->capabilities,
'status' => GrantStatus::Aktiv,
]);
}
private function element(string $title, string $body): ContentElementVersion
{
$content = app(ContentService::class);
return $content->publishElementVersion($content->createElement('text', 'text', $title), $body);
}
private function article(string $code): LicenseArticle
{
return app(ArticleService::class)->create($code);
}
private function publishedArticle(string $code, string $title): LicenseArticle
{
$article = $this->article($code);
app(ArticleService::class)->publishRelease(
$article,
$this->element($title, $title),
'Eigenständiger Artikel.',
);
return $article->fresh();
}
private function rights(): RightsProfileVersion
{
return app(OfferService::class)->createRightsProfile('Lesen', 'ehrenamtlich', [
'text' => ['lesen'],
]);
}
}