Files
laraundfelix/tests/Feature/OrganizationPurchaseTest.php
T
Lars 4e0760db1e
Deploy Test / test (push) Successful in 1m38s
Deploy Test / deploy (push) Successful in 31s
Add feature tests for shop functionality and organization purchases
- Implement OfferOptionAdminTest to validate offer option management, including price display, activation, and cancellation.
- Create OrganizationPurchaseTest to ensure correct handling of organization purchases, including access control and order processing.
- Add ShopProductPageTest to verify shop product listings, search functionality, and product page details.
- Introduce BuildsShop trait for seeding test data related to shop functionality.
- Implement MoneyTest to validate money parsing and formatting, ensuring correct handling of euro amounts.
2026-11-21 12:06:24 +01:00

300 lines
15 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleRelease;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\OfferVersion;
use App\Domain\Commerce\Cart;
use App\Domain\Commerce\CartLine;
use App\Domain\Commerce\Checkout;
use App\Domain\Commerce\Order;
use App\Domain\Entitlement\Acquisition;
use App\Domain\Entitlement\DecideAccess;
use App\Domain\Entitlement\Grant;
use App\Domain\Entitlement\GrantStatus;
use App\Domain\Identity\Membership;
use App\Http\SignedLink;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\Support\BuildsShop;
use Tests\TestCase;
/**
* Etappe 1, Shop-Abnahme: Mehrfachlizenz für einen vorbereiteten Verein. Der Verein ist Lizenzinhaber, das
* bestellende Konto wird getrennt festgehalten, und nur dieses Konto lädt aus der Bestellung herunter.
*/
class OrganizationPurchaseTest extends TestCase
{
use BuildsShop;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seedShop();
}
public function test_the_club_is_license_holder_and_the_ordering_account_is_recorded_separately(): void
{
$vera = $this->vera();
$order = $this->orderOf($vera, [$this->clubKonflikte()]);
$this->assertSame($vera->id, $order->user_id, 'Handelndes Konto');
$this->assertSame($this->club()->id, $order->party_id, 'Lizenzinhaber');
$this->assertNotSame($vera->party_id, $order->party_id);
$this->assertSame('organisation', $order->holder_kind);
$line = $order->lines->first();
$this->assertSame($this->club()->id, $line->holder_party_id);
$this->assertSame('organisation', $line->holder_kind);
$this->assertSame(14900, $line->price_cents);
$this->assertSame('institutionell', $line->scopeItems->first()->rights_snapshot['purpose']);
app(Checkout::class)->confirmTestPayment($order, 'test-verein');
$acquisition = Acquisition::query()->firstOrFail();
$this->assertSame($this->club()->id, $acquisition->party_id);
$this->assertSame($vera->id, $acquisition->acting_user_id);
$this->assertSame(1, Grant::query()->where('status', GrantStatus::Aktiv)->count());
$this->assertSame(0, Acquisition::query()->where('party_id', $vera->party_id)->count(), 'Nichts geht an die Person.');
}
public function test_only_the_ordering_account_downloads_from_the_club_purchase(): void
{
$vera = $this->vera();
$order = $this->orderOf($vera, [$this->clubKonflikte()]);
[$article, $release, $film, $sheet] = $this->parts();
$this->actingAs($vera)->get($this->mediaUrl($vera, $article, $release, $film))->assertForbidden();
app(Checkout::class)->confirmTestPayment($order, 'test-verein');
$this->actingAs($vera)->get($this->mediaUrl($vera, $article, $release, $film))->assertOk();
$this->actingAs($vera)->get($this->mediaUrl($vera, $article, $release, $sheet))->assertOk();
// Zweites Konto desselben Vereins: auch der direkte Abruf aus dieser Bestellung ist nicht möglich.
$max = $this->max();
$this->actingAs($max)->get($this->mediaUrl($max, $article, $release, $film))->assertForbidden();
$this->actingAs($max)->get($this->mediaUrl($vera, $article, $release, $film))->assertForbidden();
$this->actingAs($max)->get(route('member.order', $order))->assertForbidden();
$this->actingAs($max)->get(route('member.library'))->assertOk()->assertDontSee('Konflikte im Training');
$this->assertFalse(app(DecideAccess::class)($max, $article, $release, $film, 'laden')->allowed);
// Ein fremdes Konto ohne jede Verbindung.
$tina = $this->tina();
$this->actingAs($tina)->get($this->mediaUrl($tina, $article, $release, $film))->assertForbidden();
}
public function test_a_second_ordering_member_of_the_same_club_gets_nothing_from_the_order_of_the_first(): void
{
$anna = $this->secondOrderer();
$vera = $this->vera();
$order = $this->orderOf($vera, [$this->clubKonflikte()]);
app(Checkout::class)->confirmTestPayment($order, 'test-verein');
[$article, $release, $film, $sheet] = $this->parts();
$this->assertTrue(app(DecideAccess::class)($vera, $article, $release, $film, 'laden')->allowed);
foreach ([$film, $sheet] as $version) {
$this->assertFalse(app(DecideAccess::class)($anna, $article, $release, $version, 'laden')->allowed);
$this->actingAs($anna)->get($this->mediaUrl($anna, $article, $release, $version))->assertForbidden();
$this->actingAs($anna)->get($this->mediaUrl($vera, $article, $release, $version))->assertForbidden();
}
$this->actingAs($anna)->get(route('member.order', $order))->assertForbidden();
$this->actingAs($anna)->get(route('member.library'))->assertOk()->assertDontSee('Konflikte im Training');
$this->actingAs($anna)->get(route('member.program', $this->program('Konflikte im Training')))->assertOk()->assertDontSee('>Laden</a>', false);
$program = $this->program('Konflikte im Training');
$program->forceFill(['thumbnail_path' => 'vorschau/konflikte.jpg'])->save();
Storage::disk('media_derived')->put('vorschau/konflikte.jpg', 'bild');
$thumbnail = route('member.thumbnail', ['kind' => 'komposition', 'subject' => $program->id]);
$this->actingAs($anna)->get($thumbnail)->assertForbidden();
$this->actingAs($vera)->get($thumbnail)->assertOk();
}
public function test_membership_boundaries_decide_the_club_access(): void
{
$vera = $this->vera();
app(Checkout::class)->confirmTestPayment($this->orderOf($vera, [$this->clubKonflikte()]), 'test-verein');
[$article, $release, $film] = $this->parts();
$membership = fn () => Membership::query()->where(['user_id' => $vera->id, 'party_id' => $this->club()->id]);
$allowed = fn (): bool => app(DecideAccess::class)($vera, $article, $release, $film, 'laden')->allowed;
$this->travelTo(now()->startOfDay()->addHours(12));
$membership()->update(['valid_until' => today()]);
$this->assertTrue($allowed(), 'Der letzte Tag gilt noch.');
$membership()->update(['valid_until' => today()->subDay()]);
$this->assertFalse($allowed(), 'Gestern beendet.');
$membership()->update(['valid_until' => null, 'valid_from' => today()]);
$this->assertTrue($allowed(), 'Der erste Tag gilt schon.');
$membership()->update(['valid_from' => today()->addDay()]);
$this->assertFalse($allowed(), 'Beginnt erst morgen.');
// Neue Regel (Entscheidung 6): Ohne Bestellberechtigung entfällt auch der Abruf.
$membership()->update(['valid_from' => null, 'may_order' => false]);
$this->assertFalse($allowed());
$membership()->update(['may_order' => true]);
$this->assertTrue($allowed());
}
public function test_an_owned_club_license_is_marked_for_a_second_orderer_who_cannot_download_it(): void
{
$anna = $this->secondOrderer();
app(Checkout::class)->confirmTestPayment($this->orderOf($this->vera(), [$this->clubKonflikte()]), 'test-verein');
$this->actingAs($anna)->post(route('cart.add', $this->clubKonflikte()));
$this->get(route('cart'))
->assertSee('Bereits erworben · Wird nicht mitbestellt')
->assertSee('gehört dem Verein bereits')
->assertSee('Herunterladen kann nur das Konto, das die Lizenz bestellt hat')
->assertDontSee('Zur Bibliothek');
}
public function test_the_library_of_the_ordering_account_lists_the_club_purchase_with_downloads(): void
{
$vera = $this->vera();
app(Checkout::class)->confirmTestPayment($this->orderOf($vera, [$this->clubKonflikte()]), 'test-verein');
$page = $this->actingAs($vera)->get(route('member.program', $this->program('Konflikte im Training')))->assertOk();
$this->assertSame(2, substr_count($page->getContent(), '>Laden</a>'));
}
public function test_the_access_ends_with_the_membership(): void
{
$vera = $this->vera();
app(Checkout::class)->confirmTestPayment($this->orderOf($vera, [$this->clubKonflikte()]), 'test-verein');
[$article, $release, $film] = $this->parts();
$this->assertTrue(app(DecideAccess::class)($vera, $article, $release, $film, 'laden')->allowed);
Membership::query()->where(['user_id' => $vera->id, 'party_id' => $this->club()->id])->update(['valid_until' => now()->subDay()]);
$this->assertFalse(app(DecideAccess::class)($vera, $article, $release, $film, 'laden')->allowed);
$this->actingAs($vera)->get($this->mediaUrl($vera, $article, $release, $film))->assertForbidden();
}
public function test_only_an_ordering_member_can_order_for_the_club(): void
{
foreach ([$this->tina(), $this->max()] as $user) {
$cart = app(Cart::class);
$cart->clear();
$cart->add($this->clubKonflikte());
$digest = $cart->review($user)->digest();
try {
app(Checkout::class)->placeFromCart($user, $cart, $this->club()->id, $digest);
$this->fail($user->name.' darf nicht für den Verein bestellen.');
} catch (DomainRuleException $exception) {
$this->assertStringContainsString('nicht bestellberechtigt', $exception->getMessage());
}
}
$this->assertSame(0, Order::query()->count());
}
public function test_an_ended_membership_cannot_order_for_the_club_any_more(): void
{
Membership::query()->where('user_id', $this->vera()->id)->update(['valid_until' => now()->subDay()]);
$cart = app(Cart::class);
$cart->add($this->clubKonflikte());
$this->assertNull($cart->review($this->vera(), $this->club()->id)->holder);
}
public function test_options_for_the_wrong_holder_kind_are_blocked_and_never_shown_as_the_other_kind(): void
{
$vera = $this->vera();
$cart = app(Cart::class);
$cart->add($this->clubKonflikte());
$cart->add($this->privateGrenzen());
$asClub = $cart->review($vera, $this->club()->id);
$states = collect($asClub->lines)->mapWithKeys(fn (CartLine $line): array => [$line->offer->name => $line->state]);
$this->assertSame(CartLine::OK, $states['Konflikte im Training: Vereinslizenz']);
$this->assertSame(CartLine::WRONG_HOLDER, $states['Grenzen setzen: Privatlizenz']);
$this->assertTrue($asClub->blocked());
$asPerson = $cart->review($vera, $vera->party_id);
$states = collect($asPerson->lines)->mapWithKeys(fn (CartLine $line): array => [$line->offer->name => $line->state]);
$this->assertSame(CartLine::WRONG_HOLDER, $states['Konflikte im Training: Vereinslizenz']);
$this->assertSame(CartLine::OK, $states['Grenzen setzen: Privatlizenz']);
$this->expectException(DomainRuleException::class);
app(Checkout::class)->placeFromCart($vera, $cart, $this->club()->id, $asClub->digest());
}
public function test_the_checkout_offers_the_choice_between_person_and_club_and_names_the_holder(): void
{
$vera = $this->vera();
$this->actingAs($vera)->post(route('cart.add', $this->clubKonflikte()));
$this->get(route('checkout'))
->assertOk()
->assertSee('Testverein Musterstadt e. V.')
->assertSee('Verein oder Organisation')
->assertSee('Der Verein ist Lizenzinhaber')
->assertSee('149,00 €');
$this->get(route('checkout', ['bestellen_als' => $vera->party_id]))
->assertOk()
->assertSee('Passt nicht zum Lizenzinhaber')
->assertSee('Diese Option ist für Organisationen');
}
public function test_the_refund_of_the_club_order_revokes_only_that_purchase(): void
{
$vera = $this->vera();
app(Checkout::class)->confirmTestPayment($this->orderOf($vera, [$this->privateGrenzen()], $vera->party_id), 'test-privat');
$order = $this->orderOf($vera, [$this->clubKonflikte()]);
app(Checkout::class)->confirmTestPayment($order, 'test-verein');
app(Checkout::class)->refund($order->fresh(), 'Widerruf im Test');
$this->assertSame(0, Grant::query()->whereHas('acquisition', fn ($query) => $query->where('party_id', $this->club()->id))->where('status', GrantStatus::Aktiv)->count());
$this->assertSame(1, Grant::query()->whereHas('acquisition', fn ($query) => $query->where('party_id', $vera->party_id))->where('status', GrantStatus::Aktiv)->count());
}
public function test_the_seeded_offers_pair_holder_kind_and_rights_purpose(): void
{
foreach (OfferVersion::query()->with('resolvedScopes.rights')->get() as $version) {
$purposes = $version->resolvedScopes->pluck('rights.purpose')->unique()->values()->all();
$this->assertSame(
$version->holder_kind === 'organisation' ? ['institutionell'] : ['ehrenamtlich'],
$purposes,
'Die Testangebote koppeln Inhaberart und Zweck (das System erzwingt das nicht).',
);
}
}
/** Zweites bestellberechtigtes Mitglied desselben Vereins. */
private function secondOrderer(): User
{
$anna = User::factory()->create(['name' => 'Anna Zweitbestellerin']);
Membership::query()->create(['user_id' => $anna->id, 'party_id' => $this->club()->id, 'role' => 'org_admin', 'may_order' => true]);
return $anna;
}
/**
* @return array{0: LicenseArticle, 1: ArticleRelease, 2: ContentElementVersion, 3: ContentElementVersion}
*/
private function parts(): array
{
$article = $this->article('shop-konflikte');
$release = $article->currentRelease()->with('contents.elementVersion.element')->firstOrFail();
$film = $release->contents->first(fn ($content) => $content->elementVersion->element->output_class === 'film')->elementVersion;
$sheet = $release->contents->first(fn ($content) => $content->elementVersion->element->output_class === 'pdf')->elementVersion;
return [$article, $release, $film, $sheet];
}
private function mediaUrl(User $reader, LicenseArticle $article, ArticleRelease $release, ContentElementVersion $version): string
{
return SignedLink::temporary('member.media', now()->addMinutes(5), [
'article' => $article->id,
'release' => $release->id,
'version' => $version->id,
'action' => 'laden',
'reader' => $reader->id,
]);
}
}