Files
laraundfelix/tests/Feature/OfferClaimRulesTest.php
T
LarsandClaude Sonnet 5.5 95a888d1ca
CI Feature / test (push) Successful in 5m24s
Deploy Test / test (push) Successful in 5m56s
Deploy Test / deploy (push) Successful in 32s
Fuehre Regeln fuer freie Artikel und Fuer dich im Mitgliederbereich ein
- Regel je Angebot auf Abruf: alle Mitglieder, nur freigeschaltete Konten, wer einen Artikel besitzt
- Freigaben je Angebot und Konto (offer_releases), nie geloescht, nur entzogen; Pflege im Angebot
- Pruefung serverseitig in GrantIssuer::claim; Angebote bei Registrierung bleiben regelfrei
- Mitgliederseite Angebote: Neu fuer dich (neue Staende besessener Artikel), Zum Uebernehmen, Shop
- Migration 2026_12_01_110000, ADR-56, DEPLOYMENT.md

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-26 08:22:43 +01:00

392 lines
18 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\Offer;
use App\Domain\Catalog\OfferRelease;
use App\Domain\Catalog\OfferService;
use App\Domain\Catalog\RightsProfileVersion;
use App\Domain\Commerce\Checkout;
use App\Domain\Entitlement\Acquisition;
use App\Domain\Entitlement\Grant;
use App\Domain\Entitlement\GrantIssuer;
use App\Domain\Entitlement\GrantStatus;
use App\Domain\Entitlement\NewStands;
use App\Domain\Entitlement\OfferAccess;
use App\Filament\Resources\Offers\Pages\ViewOffer;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\Support\BuildsShop;
use Tests\TestCase;
/**
* Regeln für freie Artikel (ADR-56): Wer ein Angebot „auf Abruf“ übernehmen darf (alle, freigeschaltete Konten, wer einen
* Artikel besitzt), serverseitig beim Abruf geprüft; Freigaben je Angebot und Konto, nie gelöscht; „Neu für dich“.
*/
class OfferClaimRulesTest extends TestCase
{
use BuildsShop;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
$this->seedShop();
}
private function access(): OfferAccess
{
return app(OfferAccess::class);
}
private function rights(): RightsProfileVersion
{
return RightsProfileVersion::query()->whereHas('profile', fn ($query) => $query->where('name', 'Privatlizenz ehrenamtlich'))->firstOrFail();
}
/** Ein freies Angebot auf Abruf mit eigenem Artikel. */
private function freeOffer(string $name = 'Gratis zum Start', string $kind = 'auf_abruf'): Offer
{
$article = app(ArticleService::class)->create('gratis-'.str($name)->slug());
app(ArticleService::class)->publishRelease($article, $this->article('shop-grenzen')->currentRelease->rootCompositionVersion, 'Erster Stand.');
$service = app(OfferService::class);
$offer = $service->create($name);
$service->activate($offer, [['target' => $article->fresh(), 'rights' => $this->rights()]], ['grant_kind' => $kind, 'holder_kind' => 'person']);
return $offer->fresh();
}
private function keeper(): User
{
$keeper = User::factory()->katalogverwalter()->create();
$keeper->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $keeper;
}
private function userWithoutProfile(): User
{
$user = User::factory()->create();
$user->forceFill(['party_id' => null])->save();
return $user->fresh();
}
private function claim(User $user, Offer $offer)
{
return $this->actingAs($user)->post(route('member.offers.claim', $offer));
}
private function taken(User $user): int
{
return Acquisition::query()->where('party_id', $user->party_id)->count();
}
// ---- Regel „alle“ -------------------------------------------------------------------------------------------------
public function test_by_default_every_member_sees_and_claims_a_free_offer_exactly_once(): void
{
$offer = $this->freeOffer();
$tina = $this->tina();
$this->assertSame('alle', $offer->claim_access);
$this->assertTrue($this->access()->mayClaim($tina, $offer));
$this->actingAs($tina)->get(route('member.offers'))->assertOk()->assertSee('Gratis zum Start');
$this->claim($tina, $offer)->assertRedirect(route('member.library'));
$this->claim($tina, $offer)->assertRedirect();
$this->assertSame(1, $this->taken($tina), 'Ein zweiter Abruf legt nichts doppelt an.');
$this->actingAs($tina)->get(route('member.offers'))->assertOk()->assertDontSee('In die Bibliothek');
}
// ---- Regel „nur freigeschaltete Konten“ --------------------------------------------------------------------------
public function test_a_released_only_offer_is_hidden_and_refused_for_everyone_else_on_the_server(): void
{
$offer = $this->freeOffer();
$this->access()->setRule($offer, OfferAccess::RELEASED);
$tina = $this->tina();
$max = $this->max();
$this->access()->release($offer->fresh(), $tina, $this->keeper());
$this->actingAs($tina)->get(route('member.offers'))->assertOk()->assertSee('Gratis zum Start');
$this->actingAs($max)->get(route('member.offers'))->assertOk()->assertDontSee('Gratis zum Start');
$this->claim($max, $offer)->assertRedirect()->assertSessionHasErrors('offer');
$this->assertSame(0, $this->taken($max), 'Ein nicht freigeschaltetes Konto bekommt nichts, auch mit der Adresse.');
$this->claim($tina, $offer)->assertRedirect(route('member.library'));
$this->assertSame(1, $this->taken($tina));
}
public function test_a_release_counts_only_for_its_own_offer_and_account(): void
{
$first = $this->freeOffer('Erstes Angebot');
$second = $this->freeOffer('Zweites Angebot');
$this->access()->setRule($first, OfferAccess::RELEASED);
$this->access()->setRule($second, OfferAccess::RELEASED);
$this->access()->release($first->fresh(), $this->tina(), $this->keeper());
$this->assertTrue($this->access()->mayClaim($this->tina(), $first->fresh()));
$this->assertFalse($this->access()->mayClaim($this->tina(), $second->fresh()));
$this->assertFalse($this->access()->mayClaim($this->max(), $first->fresh()));
}
public function test_a_revoked_or_expired_release_no_longer_allows_the_claim_but_keeps_what_was_taken(): void
{
$offer = $this->freeOffer();
$this->access()->setRule($offer, OfferAccess::RELEASED);
$keeper = $this->keeper();
$tina = $this->tina();
$max = $this->max();
$releaseTina = $this->access()->release($offer->fresh(), $tina, $keeper, now()->addDays(2));
$releaseMax = $this->access()->release($offer->fresh(), $max, $keeper);
$this->claim($max, $offer)->assertRedirect(route('member.library'));
$this->access()->revoke($releaseMax, $keeper, 'Irrtum');
$this->assertSame(1, $this->taken($max), 'Ein bereits übernommenes Angebot bleibt nach dem Entzug.');
$this->assertNotNull(OfferRelease::query()->find($releaseMax->id)->revoked_at, 'Die Freigabe bleibt mit dem Entzug gespeichert.');
$this->travel(3)->days();
$this->assertFalse($this->access()->mayClaim($tina, $offer->fresh()), 'Nach dem Ende der Freigabe ist der Abruf gesperrt.');
$this->claim($tina, $offer)->assertRedirect()->assertSessionHasErrors('offer');
$this->assertSame(0, $this->taken($tina));
$this->assertSame(2, OfferRelease::query()->count(), 'Freigaben werden nie gelöscht.');
$this->assertNotNull($releaseTina->fresh());
}
public function test_releases_are_validated(): void
{
$offer = $this->freeOffer();
$keeper = $this->keeper();
try {
$this->access()->release($offer, $this->tina(), $keeper);
$this->fail('Bei der Regel „alle“ gibt es keine Freigaben.');
} catch (DomainRuleException $exception) {
$this->assertStringContainsString('nur freigeschaltete Konten', $exception->getMessage());
}
$this->access()->setRule($offer, OfferAccess::RELEASED);
$offer = $offer->fresh();
$this->access()->release($offer, $this->tina(), $keeper);
foreach ([
'doppelt' => fn () => $this->access()->release($offer, $this->tina(), $keeper),
'Vergangenheit' => fn () => $this->access()->release($offer, $this->max(), $keeper, now()->subDay()),
'ohne Profil' => fn () => $this->access()->release($offer, $this->userWithoutProfile(), $keeper),
] as $name => $attempt) {
try {
$attempt();
$this->fail('Diese Freigabe muss abgelehnt werden: '.$name);
} catch (DomainRuleException) {
$this->assertTrue(true);
}
}
$this->assertSame(1, OfferRelease::query()->count());
}
// ---- Regel „besitzt einen Artikel“ -------------------------------------------------------------------------------
private function buy(User $user, Offer $offer): void
{
$order = $this->orderOf($user, [$offer]);
app(Checkout::class)->confirmTestPayment($order, 'test-'.$order->id);
}
public function test_an_owner_rule_opens_the_offer_only_for_accounts_with_a_valid_grant_on_the_article(): void
{
$offer = $this->freeOffer();
$article = $this->article('shop-konflikte');
$this->access()->setRule($offer, OfferAccess::OWNERS, $article->id);
$offer = $offer->fresh();
$tina = $this->tina();
$this->assertFalse($this->access()->mayClaim($tina, $offer));
$this->claim($tina, $offer)->assertRedirect()->assertSessionHasErrors('offer');
$this->assertSame(0, $this->taken($tina));
$this->buy($tina, $this->privateKonflikte());
$this->assertTrue($this->access()->mayClaim($tina, $offer));
$this->actingAs($tina)->get(route('member.offers'))->assertOk()->assertSee('Gratis zum Start');
$this->assertFalse($this->access()->mayClaim($this->max(), $offer), 'Der Besitz gilt je Konto.');
Grant::query()->where('license_article_id', $article->id)->update(['status' => GrantStatus::Widerrufen]);
$this->assertFalse($this->access()->mayClaim($tina, $offer), 'Eine widerrufene Berechtigung zählt nicht als Besitz.');
}
public function test_the_rule_itself_is_validated_and_clears_a_stale_article(): void
{
$offer = $this->freeOffer();
$article = $this->article('shop-konflikte');
foreach ([['unbekannt', null], [OfferAccess::OWNERS, null], [OfferAccess::OWNERS, 999999]] as [$access, $articleId]) {
try {
$this->access()->setRule($offer, $access, $articleId);
$this->fail('Diese Regel muss abgelehnt werden.');
} catch (DomainRuleException) {
$this->assertSame('alle', $offer->fresh()->claim_access);
}
}
$article->forceFill(['archived_at' => now()])->save();
try {
$this->access()->setRule($offer, OfferAccess::OWNERS, $article->id);
$this->fail('Ein archivierter Artikel ist keine Voraussetzung.');
} catch (DomainRuleException) {
$this->assertNull($offer->fresh()->claim_requires_article_id);
}
$article->forceFill(['archived_at' => null])->save();
$this->access()->setRule($offer, OfferAccess::OWNERS, $article->id);
$this->assertSame($article->id, $offer->fresh()->claim_requires_article_id);
$this->access()->setRule($offer->fresh(), OfferAccess::ALL, $article->id);
$this->assertNull($offer->fresh()->claim_requires_article_id, 'Eine andere Regel löscht den Artikel.');
}
// ---- Was die Regel nicht betrifft --------------------------------------------------------------------------------
public function test_on_registration_offers_ignore_the_rule_and_stay_listed(): void
{
$offer = $this->freeOffer('Zur Registrierung', 'bei_registrierung');
$this->access()->setRule($offer, OfferAccess::RELEASED);
$max = $this->max();
$this->assertTrue($this->access()->mayClaim($max, $offer->fresh()));
$this->assertTrue($this->access()->claimable($max)->contains('id', $offer->id));
}
public function test_a_purchase_offer_or_an_ended_offer_cannot_be_claimed_for_free(): void
{
$purchase = $this->privateKonflikte();
$tina = $this->tina();
$this->assertFalse($this->access()->mayClaim($tina, $purchase));
$this->claim($tina, $purchase)->assertRedirect()->assertSessionHasErrors('offer');
$offer = $this->freeOffer();
app(OfferService::class)->end($offer);
$this->assertFalse($this->access()->mayClaim($tina, $offer->fresh()));
$this->assertSame(0, $this->taken($tina));
}
public function test_the_issuer_enforces_the_rule_even_without_the_page(): void
{
$offer = $this->freeOffer();
$this->access()->setRule($offer, OfferAccess::RELEASED);
$this->expectException(DomainRuleException::class);
app(GrantIssuer::class)->claim($this->max(), $offer->fresh());
}
// ---- Verwaltung --------------------------------------------------------------------------------------------------
public function test_the_keeper_sets_the_rule_releases_and_revokes_on_the_offer_page(): void
{
$offer = $this->freeOffer();
$tina = $this->tina();
$page = Livewire::actingAs($this->keeper(), 'staff')->test(ViewOffer::class, ['record' => $offer->getKey()]);
$page->assertSee('Wer darf abrufen')->assertSee('Alle angemeldeten Mitglieder');
$page->callAction('claimRule', ['claim_access' => OfferAccess::RELEASED, 'claim_requires_article_id' => null])->assertHasNoActionErrors();
$this->assertSame('konten', $offer->fresh()->claim_access);
$page->assertSee('Freigeschaltete Konten')->callAction('release', ['user_id' => $tina->id, 'valid_until' => null, 'reason' => 'Seminar am 12.11.2026'])->assertHasNoActionErrors();
$release = OfferRelease::query()->firstOrFail();
$this->assertSame($tina->id, $release->user_id);
$this->assertTrue($this->access()->mayClaim($tina, $offer->fresh()));
$page->assertSee('Seminar am 12.11.2026');
$page->callAction('revoke', ['reason' => 'Irrtum'], ['release' => $release->id])->assertHasNoActionErrors();
$this->assertNotNull($release->fresh()->revoked_at);
$this->assertFalse($this->access()->mayClaim($tina, $offer->fresh()));
}
public function test_the_rule_actions_exist_only_for_offers_on_demand_and_only_for_the_keeper(): void
{
$purchase = $this->privateKonflikte();
Livewire::actingAs($this->keeper(), 'staff')->test(ViewOffer::class, ['record' => $purchase->getKey()])
->assertDontSee('Wer darf abrufen')
->assertActionHidden('claimRule');
$offer = $this->freeOffer();
$redakteur = User::factory()->redakteur()->create();
$redakteur->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
Livewire::actingAs($redakteur, 'staff')->test(ViewOffer::class, ['record' => $offer->getKey()])->assertForbidden();
$this->assertSame('alle', $offer->fresh()->claim_access);
}
public function test_a_release_of_another_offer_cannot_be_revoked_through_this_offer(): void
{
$first = $this->freeOffer('Erstes');
$second = $this->freeOffer('Zweites');
$this->access()->setRule($first, OfferAccess::RELEASED);
$this->access()->setRule($second, OfferAccess::RELEASED);
$foreign = $this->access()->release($first->fresh(), $this->tina(), $this->keeper());
try {
Livewire::actingAs($this->keeper(), 'staff')->test(ViewOffer::class, ['record' => $second->getKey()])
->callAction('revoke', ['reason' => 'x'], ['release' => $foreign->id]);
} catch (\Throwable) {
// Die Freigabe eines anderen Angebots wird nicht gefunden.
}
$this->assertNull($foreign->fresh()->revoked_at);
}
// ---- Neu für dich ------------------------------------------------------------------------------------------------
public function test_a_new_stand_of_an_owned_article_is_listed_for_ninety_days_and_only_for_the_owner(): void
{
$tina = $this->tina();
$article = $this->article('shop-konflikte');
$this->buy($tina, $this->privateKonflikte());
$this->assertCount(0, app(NewStands::class)->forUser($tina), 'Ohne neuen Stand gibt es nichts zu zeigen.');
app(ArticleService::class)->publishRelease($article, $article->fresh()->currentRelease->rootCompositionVersion, 'Neuer Stand mit Ergänzungen.');
$list = app(NewStands::class)->forUser($tina);
$this->assertCount(1, $list);
$this->assertSame(2, $list[0]['number']);
$this->assertCount(0, app(NewStands::class)->forUser($this->max()), 'Wer den Artikel nicht besitzt, sieht keinen neuen Stand.');
$this->actingAs($tina)->get(route('member.offers'))->assertOk()->assertSee('Neu für dich')->assertSee('Stand 2')->assertSee(route('member.grant', $list[0]['grant']), false);
$this->travel(91)->days();
$this->assertCount(0, app(NewStands::class)->forUser($tina), 'Nach 90 Tagen verschwindet der Hinweis.');
}
public function test_a_withdrawn_grant_shows_no_new_stand(): void
{
$tina = $this->tina();
$article = $this->article('shop-konflikte');
$this->buy($tina, $this->privateKonflikte());
app(ArticleService::class)->publishRelease($article, $article->fresh()->currentRelease->rootCompositionVersion, 'Neuer Stand.');
Grant::query()->where('license_article_id', $article->id)->update(['status' => GrantStatus::Widerrufen]);
$this->assertCount(0, app(NewStands::class)->forUser($tina));
}
public function test_the_member_offers_page_shows_an_honest_empty_state(): void
{
$this->actingAs($this->max())->get(route('member.offers'))->assertOk()->assertSee('Gerade liegt nichts Neues für dich bereit');
}
public function test_the_article_title_helper_is_the_label_in_the_rule_form(): void
{
$offer = $this->freeOffer();
$this->access()->setRule($offer, OfferAccess::OWNERS, $this->article('shop-konflikte')->id);
Livewire::actingAs($this->keeper(), 'staff')->test(ViewOffer::class, ['record' => $offer->getKey()])
->assertSee('Mitglieder, die einen bestimmten Artikel besitzen')
->assertSee('Konflikte im Training');
$this->assertInstanceOf(LicenseArticle::class, $this->article('shop-konflikte'));
}
}