seedShop(); } /** * Eine weitere Option mit denselben Positionen und Rechten wie die Quelle. * * @param array $attributes */ private function variant(Offer $source, string $name, array $attributes): Offer { $active = app(OfferService::class)->currentActiveVersion($source); $lines = $active->lines()->with('article', 'bundleVersion', 'rights')->get()->map(fn ($line): array => [ 'target' => $line->article ?? $line->bundleVersion, 'rights' => $line->rights, ])->all(); $offer = app(OfferService::class)->create($name); app(OfferService::class)->activate($offer, $lines, [ 'grant_kind' => 'kauf', 'price_cents' => 4900, 'holder_kind' => $active->holder_kind, ...$attributes, ]); $offer->update(['composition_id' => $source->composition_id]); return $offer->refresh(); } public function test_the_scope_is_validated_when_a_version_is_activated(): void { $source = $this->privateKonflikte(); foreach ([ ['licence_scope' => 'einzel', 'trainer_count' => 3], ['licence_scope' => 'mehrfach', 'trainer_count' => 1], ['licence_scope' => 'mehrfach', 'trainer_count' => null], ['licence_scope' => 'mehrfach', 'trainer_count' => 100000], ['licence_scope' => null, 'trainer_count' => 4], ['licence_scope' => 'gruppe', 'trainer_count' => 2], ] as $attributes) { try { $this->variant($source, 'Ungültig', $attributes); $this->fail('Ungültiger Nutzerkreis muss abgewiesen werden: '.json_encode($attributes)); } catch (DomainRuleException) { $this->assertNull(Offer::query()->where('name', 'Ungültig')->first()?->versions()->where('status', 'aktiv')->first()); } } } public function test_a_single_license_always_means_exactly_one_trainer(): void { $offer = $this->variant($this->privateKonflikte(), 'Einzel ohne Zahl', ['licence_scope' => 'einzel']); $version = app(OfferService::class)->currentActiveVersion($offer); $this->assertSame('einzel', $version->licence_scope); $this->assertSame(1, $version->trainer_count); } public function test_the_scope_is_not_derived_from_purpose_price_or_discount(): void { $institutional = app(OfferService::class)->currentActiveVersion($this->clubKonflikte())->lines()->first()->rights; $this->assertSame('institutionell', $institutional->purpose); $single = $this->variant($this->clubKonflikte(), 'Institutionell, ein Trainer', ['licence_scope' => 'einzel', 'holder_kind' => 'organisation', 'price_cents' => 99900]); $legacy = $this->variant($this->privateGrenzen(), 'Ohne Angabe', ['price_cents' => 1900]); $this->assertSame('Einzellizenz · 1 Trainer', LicenceScope::label($single->versions()->first()->licence_scope, $single->versions()->first()->trainer_count)); $this->assertNull($legacy->versions()->first()->licence_scope, 'Ohne Angabe bleibt es ohne Angabe, auch bei anderem Preis oder Zweck.'); $this->assertNull($legacy->versions()->first()->trainer_count); } public function test_the_product_page_tells_single_and_multi_licenses_apart(): void { $page = $this->program('Konflikte im Training'); $this->variant($this->privateKonflikte(), 'Ohne Angabe', ['price_cents' => 1900]); $this->get(route('public.program', $page)) ->assertOk() ->assertSee('Nutzerkreis') ->assertSee('Einzellizenz · 1 Trainer') ->assertSee('Mehrfachlizenz · bis zu 5 Trainer') ->assertSee('Die Inhalte dürfen von einem Trainer eingesetzt werden.') ->assertSee('Die Inhalte dürfen von bis zu 5 Trainern eingesetzt werden.') ->assertSee('Nutzerkreis nicht festgelegt'); } public function test_cart_checkout_and_order_show_the_scope_and_keep_the_agreed_one(): void { $tina = $this->tina(); $offer = $this->privateKonflikte(); $this->actingAs($tina)->post(route('cart.add', $offer)); $this->get(route('cart'))->assertSee('Einzellizenz · 1 Trainer'); $this->get(route('checkout'))->assertSee('Einzellizenz · 1 Trainer'); $order = $this->orderOf($tina, [$offer]); $line = $order->lines->first(); $this->assertSame('einzel', $line->licence_scope); $this->assertSame(1, $line->trainer_count); app(Checkout::class)->confirmTestPayment($order, 'test-scope'); $conditions = Acquisition::query()->where('reference_type', 'order_line')->firstOrFail()->conditions; $this->assertSame('einzel', $conditions['licence_scope']); $this->assertSame(1, $conditions['trainer_count']); // Eine spätere Fassung mit anderem Nutzerkreis ändert den gekauften Umfang nicht. $lines = app(OfferService::class)->currentActiveVersion($offer)->lines()->with('article', 'rights')->get()->map(fn ($l): array => ['target' => $l->article, 'rights' => $l->rights])->all(); app(OfferService::class)->activate($offer, $lines, ['grant_kind' => 'kauf', 'price_cents' => 4900, 'licence_scope' => 'mehrfach', 'trainer_count' => 3]); $this->assertSame('einzel', $line->fresh()->licence_scope); $this->assertSame('einzel', Acquisition::query()->where('reference_type', 'order_line')->firstOrFail()->conditions['licence_scope']); $this->get(route('member.order', $order))->assertOk()->assertSee('Einzellizenz · 1 Trainer')->assertDontSee('bis zu 3 Trainer'); } public function test_a_club_purchase_keeps_the_club_as_holder_and_only_the_ordering_account_downloads(): void { $vera = $this->vera(); $order = $this->orderOf($vera, [$this->clubKonflikte()], $this->club()->id); app(Checkout::class)->confirmTestPayment($order, 'test-club'); $order = $order->fresh(['lines']); $this->assertSame($this->club()->id, $order->party_id, 'Lizenzinhaber ist der Verein.'); $this->assertSame($vera->id, $order->user_id, 'Das bestellende Konto bleibt davon getrennt festgehalten.'); $this->assertSame('mehrfach', $order->lines->first()->licence_scope); $this->assertSame(5, $order->lines->first()->trainer_count); $acquisition = Acquisition::query()->where('reference_type', 'order_line')->firstOrFail(); $this->assertSame($this->club()->id, $acquisition->party_id); $this->assertSame($vera->id, $acquisition->acting_user_id); $this->assertSame(5, $acquisition->conditions['trainer_count']); $this->assertSame(1, Acquisition::query()->count(), 'Eine Mehrfachlizenz erzeugt zum Start keine weiteren Downloadkonten.'); $this->assertTrue(Grant::query()->usableBy($vera)->exists()); $this->assertFalse(Grant::query()->usableBy($this->max())->exists()); $this->assertFalse(Grant::query()->usableBy($this->tina())->exists()); $this->actingAs($vera)->get(route('member.order', $order))->assertOk() ->assertSee('Testverein Musterstadt e. V.') ->assertSee('Mehrfachlizenz · bis zu 5 Trainer'); } public function test_an_owned_single_license_does_not_cover_a_multi_license_with_the_same_rights(): void { $tina = $this->tina(); $owned = $this->privateKonflikte(); $order = $this->orderOf($tina, [$owned]); app(Checkout::class)->confirmTestPayment($order, 'test-own'); $multi = $this->variant($owned, 'Konflikte Mehrfach 3', ['licence_scope' => 'mehrfach', 'trainer_count' => 3]); $single = $this->variant($owned, 'Konflikte Einzel nochmal', ['licence_scope' => 'einzel']); $cart = app(Cart::class); $cart->clear(); $cart->add($multi); $this->assertSame(CartLine::OK, $cart->review($tina)->lines[0]->state, 'Eine Einzellizenz macht eine Mehrfachlizenz nicht zur „bereits erworben“.'); $cart->clear(); $cart->add($single); $this->assertSame(CartLine::OWNED, $cart->review($tina)->lines[0]->state); } public function test_an_offer_without_published_content_cannot_be_activated(): void { $empty = app(ArticleService::class)->create('leer-ohne-stand'); $rights = app(OfferService::class)->currentActiveVersion($this->privateKonflikte())->lines()->first()->rights; $offers = app(OfferService::class); $offer = $offers->create('Ohne Inhalt'); try { $offers->activate($offer, [['target' => $empty, 'rights' => $rights]], ['grant_kind' => 'kauf', 'price_cents' => 1000]); $this->fail('Ein Artikel ohne veröffentlichten Stand darf nicht verkauft werden.'); } catch (DomainRuleException $exception) { $this->assertStringContainsString('leer-ohne-stand', $exception->getMessage()); $this->assertStringContainsString('keinen veröffentlichten Stand', $exception->getMessage()); } $bundles = app(BundleService::class); $bundle = $bundles->publish($bundles->create('Leeres Paket'), [$empty]); try { $offers->activate($offer, [['target' => $bundle, 'rights' => $rights]], ['grant_kind' => 'kauf', 'price_cents' => 1000]); $this->fail('Auch ein Paket mit einem Artikel ohne Stand darf nicht verkauft werden.'); } catch (DomainRuleException) { $this->assertSame(0, $offer->versions()->count(), 'Es entsteht keine halbe Fassung.'); } $offers->activate($offer, [['target' => $this->article('shop-konflikte'), 'rights' => $rights]], ['grant_kind' => 'kauf', 'price_cents' => 1000]); $this->assertSame(1, $offer->versions()->count()); } }