seedShop(); } public function test_two_offers_become_one_order_with_two_positions_that_keep_their_conditions_and_scope(): void { $tina = $this->tina(); $order = $this->orderOf($tina, [$this->privateKonflikte(), $this->privateGrenzen()]); $this->assertSame('offen', $order->status); $this->assertSame($tina->id, $order->user_id, 'Das handelnde Konto steht an der Bestellung.'); $this->assertSame($tina->party_id, $order->party_id, 'Lizenzinhaber ist hier die Person selbst.'); $this->assertSame('person', $order->holder_kind); $this->assertSame(7800, $order->total_cents); $this->assertSame('EUR', $order->currency); $this->assertSame(2, $order->lines->count()); $first = $order->lines->firstWhere('offer_name', 'Konflikte im Training: Privatlizenz'); $this->assertSame(4900, $first->price_cents); $this->assertSame('EUR', $first->currency); $this->assertSame('dauerhaft', $first->term_rule); $this->assertSame('alle_freigegebenen_fassungen', $first->update_promise); $this->assertSame('kauf', $first->grant_kind); $this->assertSame('person', $first->holder_kind); $this->assertSame($tina->party_id, $first->holder_party_id); $this->assertSame($this->privateKonflikte()->versions()->first()->id, $first->offer_version_id); $this->assertSame(1, $first->scopeItems->count()); $this->assertSame('ehrenamtlich', $first->scopeItems->first()->rights_snapshot['purpose']); $this->assertEqualsCanonicalizing(['streamen', 'laden'], $first->scopeItems->first()->rights_snapshot['capabilities']['film']); $this->assertSame(0, Grant::query()->count(), 'Vor der Zahlung gibt es keine Berechtigung.'); } public function test_the_confirmed_test_payment_issues_every_position_exactly_once(): void { $tina = $this->tina(); $order = $this->orderOf($tina, [$this->privateKonflikte(), $this->privateGrenzen(), $this->starter()]); $checkout = app(Checkout::class); $checkout->confirmTestPayment($order, 'test-1'); $checkout->confirmTestPayment($order->fresh(), 'test-2'); $checkout->confirmTestPayment($order->fresh(), 'test-3'); $this->assertSame('bezahlt', $order->fresh()->status); $this->assertSame(1, Payment::query()->count()); $this->assertSame(3, Acquisition::query()->count(), 'Ein Erwerb je Bestellposition.'); // Konflikte 1 + Grenzen 1 + Paket 3 Artikel $this->assertSame(5, Grant::query()->where('status', GrantStatus::Aktiv)->count()); foreach ($order->lines as $line) { $acquisition = Acquisition::query()->where(['reference_type' => 'order_line', 'reference_id' => $line->id])->firstOrFail(); $this->assertSame($tina->party_id, $acquisition->party_id); $this->assertSame($tina->id, $acquisition->acting_user_id); $this->assertSame($line->scopeItems->count(), $acquisition->grants->count()); } Mail::assertSent(OrderConfirmed::class, 1); } public function test_a_failure_while_issuing_leaves_no_payment_and_no_partial_rights_and_a_retry_succeeds(): void { $tina = $this->tina(); $order = $this->orderOf($tina, [$this->privateKonflikte(), $this->privateGrenzen()]); $this->app->bind(GrantIssuer::class, fn () => new class(app(OfferService::class)) extends GrantIssuer { public int $calls = 0; public function issueFromOrderLine(OrderLine $line, Party $holder, User $actor): Acquisition { if (++$this->calls === 2) { throw new RuntimeException('simulierter Ausfall bei der zweiten Position'); } return parent::issueFromOrderLine($line, $holder, $actor); } }); try { app(Checkout::class)->confirmTestPayment($order, 'test-ausfall'); $this->fail('Der simulierte Ausfall wurde nicht geworfen.'); } catch (RuntimeException $exception) { $this->assertStringContainsString('simulierter Ausfall', $exception->getMessage()); } $this->assertSame('offen', $order->fresh()->status); $this->assertSame(0, Payment::query()->count()); $this->assertSame(0, Acquisition::query()->count()); $this->assertSame(0, Grant::query()->count()); Mail::assertNothingSent(); $this->app->bind(GrantIssuer::class, fn () => new GrantIssuer(app(OfferService::class))); app(Checkout::class)->confirmTestPayment($order->fresh(), 'test-ausfall'); $this->assertSame('bezahlt', $order->fresh()->status); $this->assertSame(2, Acquisition::query()->count()); $this->assertSame(2, Grant::query()->count()); } public function test_a_rejected_test_payment_issues_nothing_and_refuses_a_later_confirmation(): void { $order = $this->orderOf($this->tina(), [$this->privateKonflikte(), $this->privateGrenzen()]); $checkout = app(Checkout::class); $checkout->failTestPayment($order, 'test-fail-1'); $this->assertSame('fehlgeschlagen', $order->fresh()->status); $this->assertSame(0, Grant::query()->count()); $this->expectException(DomainRuleException::class); $checkout->confirmTestPayment($order->fresh(), 'test-spaet'); } public function test_an_open_order_without_a_payment_stays_without_rights_and_the_cart_is_emptied(): void { $this->orderOf($this->tina(), [$this->privateKonflikte()]); $this->assertSame(0, Grant::query()->count()); $this->assertSame(0, app(Cart::class)->count(), 'Der Warenkorb ist nach der Bestellung geleert.'); } public function test_an_older_order_keeps_its_price_and_version_after_the_offer_changes(): void { $offer = $this->privateKonflikte(); $order = $this->orderOf($this->tina(), [$offer]); $oldVersion = $order->lines->first()->offer_version_id; $active = app(OfferService::class)->currentActiveVersion($offer); app(OfferService::class)->activate($offer, [['target' => $this->article('shop-konflikte'), 'rights' => $active->lines()->first()->rights]], [ 'grant_kind' => 'kauf', 'price_cents' => 3900, 'holder_kind' => 'person', ]); app(Checkout::class)->confirmTestPayment($order->fresh(), 'test-alt'); $line = $order->fresh()->lines->first(); $this->assertSame(4900, $line->price_cents); $this->assertSame($oldVersion, $line->offer_version_id); $this->assertSame(1, Grant::query()->count()); $this->assertSame($oldVersion, Acquisition::query()->first()->conditions['offer_version_id']); $second = $this->orderOf($this->max(), [$offer]); $this->assertSame(3900, $second->lines->first()->price_cents); } public function test_a_refund_revokes_all_positions_and_keeps_other_acquisition_sources(): void { $tina = $this->tina(); $free = app(OfferService::class)->create('Konflikte gratis testen'); $rights = app(OfferService::class)->createRightsProfile('Vorschau', 'ehrenamtlich', ['pdf' => ['ansehen']]); app(OfferService::class)->activate($free, [['target' => $this->article('shop-konflikte'), 'rights' => $rights]], ['grant_kind' => 'auf_abruf']); $freeAcquisition = app(GrantIssuer::class)->claim($tina, $free); $earlier = $this->orderOf($tina, [$this->privateGrenzen()]); app(Checkout::class)->confirmTestPayment($earlier, 'test-frueher'); $order = $this->orderOf($tina, [$this->privateKonflikte(), $this->starter()]); app(Checkout::class)->confirmTestPayment($order, 'test-neu'); app(Checkout::class)->refund($order->fresh(), 'Widerruf im Test'); $this->assertSame('erstattet', $order->fresh()->status); foreach ($order->lines as $line) { $acquisition = Acquisition::query()->where(['reference_type' => 'order_line', 'reference_id' => $line->id])->firstOrFail(); $this->assertSame(0, $acquisition->grants()->where('status', GrantStatus::Aktiv)->count()); $this->assertSame($acquisition->grants()->count(), $acquisition->grants()->where('status', GrantStatus::Widerrufen)->count()); } $this->assertSame(1, $freeAcquisition->grants()->where('status', GrantStatus::Aktiv)->count(), 'Die kostenlose Vergabe bleibt.'); $earlierAcquisition = Acquisition::query()->where(['reference_type' => 'order_line', 'reference_id' => $earlier->lines->first()->id])->firstOrFail(); $this->assertSame(1, $earlierAcquisition->grants()->where('status', GrantStatus::Aktiv)->count(), 'Der frühere Kauf bleibt.'); } public function test_film_and_companion_material_are_downloadable_only_after_the_test_payment_and_only_by_the_buyer(): void { $tina = $this->tina(); $order = $this->orderOf($tina, [$this->privateKonflikte()]); [$article, $release, $film, $sheet] = $this->parts('shop-konflikte'); // Vor Geldeingang auch der direkte Abruf aus der geprüften Bestellung: gesperrt. $this->actingAs($tina)->get($this->mediaUrl($tina, $article, $release, $film, 'laden'))->assertForbidden(); $this->actingAs($tina)->get($this->mediaUrl($tina, $article, $release, $sheet, 'laden'))->assertForbidden(); app(Checkout::class)->confirmTestPayment($order, 'test-download'); $filmResponse = $this->actingAs($tina)->get($this->mediaUrl($tina, $article, $release, $film, 'laden'))->assertOk(); $this->assertStringContainsString('attachment', $filmResponse->headers->get('Content-Disposition')); $this->assertSame('Platzhalter für den Film Konflikte im Training', $filmResponse->streamedContent()); $this->actingAs($tina)->get($this->mediaUrl($tina, $article, $release, $sheet, 'laden'))->assertOk(); // Ein anderes Konto mit eigener Signatur bleibt ausgeschlossen, ebenso die Signatur der Käuferin. $max = $this->max(); $this->actingAs($max)->get($this->mediaUrl($max, $article, $release, $film, 'laden'))->assertForbidden(); $this->actingAs($max)->get($this->mediaUrl($tina, $article, $release, $film, 'laden'))->assertForbidden(); } public function test_the_library_shows_the_allowed_downloads_after_payment_and_none_before(): void { $tina = $this->tina(); $program = $this->program('Konflikte im Training'); $order = $this->orderOf($tina, [$this->privateKonflikte()]); $this->actingAs($tina)->get(route('member.library'))->assertOk()->assertDontSee('Konflikte im Training'); app(Checkout::class)->confirmTestPayment($order, 'test-bibliothek'); $this->actingAs($tina)->get(route('member.library'))->assertOk()->assertSee('Konflikte im Training'); $page = $this->actingAs($tina)->get(route('member.program', $program))->assertOk(); $this->assertSame(2, substr_count($page->getContent(), '>Laden'), 'Film und Begleitmaterial haben je eine Download-Aktion.'); } public function test_a_purchase_covers_only_its_own_article(): void { $tina = $this->tina(); $order = $this->orderOf($tina, [$this->privateKonflikte()]); app(Checkout::class)->confirmTestPayment($order, 'test-fremd'); [$article, $release, $film] = $this->parts('shop-grenzen'); $this->assertFalse(app(DecideAccess::class)($tina, $article, $release, $film, 'laden')->allowed); $this->actingAs($tina)->get($this->mediaUrl($tina, $article, $release, $film, 'laden'))->assertForbidden(); } public function test_the_order_page_lists_positions_holder_and_total(): void { $tina = $this->tina(); $order = $this->orderOf($tina, [$this->privateKonflikte(), $this->privateGrenzen()]); $this->actingAs($tina)->get(route('member.order', $order)) ->assertOk() ->assertSee('Konflikte im Training: Privatlizenz') ->assertSee('Grenzen setzen: Privatlizenz') ->assertSee('78,00 €') ->assertSee('Tina Trainerin') ->assertSee('Testzahlung bestätigen'); $this->actingAs($this->max())->get(route('member.order', $order))->assertForbidden(); } public function test_the_checkout_page_places_the_order_through_the_http_flow(): void { $tina = $this->tina(); $this->actingAs($tina)->post(route('cart.add', $this->privateKonflikte())); $this->post(route('cart.add', $this->privateGrenzen())); $page = $this->get(route('checkout'))->assertOk()->assertSee('Summe')->assertSee('78,00 €'); preg_match('/name="digest" value="([a-f0-9]{64})"/', $page->getContent(), $match); $this->assertNotEmpty($match); $this->post(route('checkout.place'), ['digest' => $match[1]])->assertRedirect(route('member.order', Order::query()->latest('id')->first() ?? 1)); $order = Order::query()->latest('id')->firstOrFail(); $this->post(route('member.order.confirm', $order))->assertRedirect(route('member.library')); $this->assertSame('bezahlt', $order->fresh()->status); $this->assertSame(2, Acquisition::query()->count()); $this->assertSame(0, app(Cart::class)->count()); } /** * @return array{0: LicenseArticle, 1: ArticleRelease, 2: ContentElementVersion, 3: ContentElementVersion} */ private function parts(string $code): array { $article = $this->article($code); $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 $action): string { return SignedLink::temporary('member.media', now()->addMinutes(5), [ 'article' => $article->id, 'release' => $release->id, 'version' => $version->id, 'action' => $action, 'reader' => $reader->id, ]); } }