seedShop(); } private function publishLegalPages(): void { foreach (['/agb', '/lizenzbedingungen', '/widerruf', '/impressum', '/datenschutz'] as $path) { $page = Page::query()->firstOrCreate(['path' => $path]); $revision = PageRevision::query()->create(['page_id' => $page->id, 'blocks' => [], 'sections' => [], 'seo_title' => ltrim($path, '/')]); $page->published_revision_id = $revision->id; $page->save(); } } /** Produktion mit Stripe-Live-Schlüssel, aber ohne Freigabe: beide Schalter aus. */ private function production(bool $public = false, bool $release = false): void { $this->app['env'] = 'production'; config([ 'services.payment.driver' => 'stripe', 'services.stripe.secret' => 'sk_live_nur_fuer_tests', 'shop.public' => $public, 'shop.live_release' => $release, ]); if ($release) { $this->publishLegalPages(); } } /** In Produktion prüft die Anwendung das CSRF-Token (im Testbetrieb wird es sonst übersprungen). */ private function postProduction(string $uri, array $data = [], ?User $user = null) { $request = $user === null ? $this : $this->actingAs($user); return $request->withSession(['_token' => 'produktion'])->post($uri, $data + ['_token' => 'produktion']); } private function articleUrl(): string { return route('shop.article', $this->article('shop-konflikte')); } // ---- Der alte Einzelkauf ---------------------------------------------------------------------------------------- public function test_production_without_release_creates_no_order_through_the_removed_direct_purchase(): void { $this->production(); $offer = $this->privateKonflikte(); foreach (['/bereich/angebote/'.$offer->id.'/kaufen', '/angebote/'.$offer->id.'/kaufen'] as $path) { $this->postProduction($path, [], $this->tina())->assertStatus(404); } $this->assertSame(0, Order::query()->count()); $this->assertFalse(Route::has('member.offers.buy'), 'Die Route des Einzelkaufs ist entfernt.'); } public function test_the_direct_purchase_is_gone_in_test_mode_too_and_checkout_has_no_place_method(): void { $offer = $this->privateKonflikte(); $this->actingAs($this->tina())->post('/bereich/angebote/'.$offer->id.'/kaufen')->assertStatus(404); $this->assertSame(0, Order::query()->count()); $this->assertFalse(method_exists(Checkout::class, 'place'), 'Es gibt nur noch die Bestellung aus dem Warenkorb mit Pflichtangaben.'); } // ---- Die Schalter ----------------------------------------------------------------------------------------------- public function test_in_production_both_switches_are_off_by_default_and_nothing_of_the_shop_is_reachable(): void { $this->production(); $this->assertFalse(Shop::visible()); $this->assertFalse(Shop::checkoutOpen()); $this->get(route('shop'))->assertNotFound(); $this->get($this->articleUrl())->assertNotFound(); $this->get(route('public.package', Bundle::query()->firstOrFail()))->assertNotFound(); $this->get(route('cart'))->assertNotFound(); $this->get(route('checkout'))->assertNotFound(); $this->get(route('shop.closed'))->assertNotFound(); $this->postProduction(route('cart.add', $this->privateKonflikte()))->assertNotFound(); $this->postProduction(route('cart.buy', $this->privateKonflikte()))->assertNotFound(); } public function test_with_the_shop_visible_but_the_checkout_closed_the_shop_can_be_viewed_but_not_bought_from(): void { $this->production(public: true); $this->assertTrue(Shop::visible()); $this->assertFalse(Shop::checkoutOpen()); $this->get(route('shop')) ->assertOk() ->assertSee('Konflikte im Training') ->assertSee('', false); $page = $this->get($this->articleUrl())->assertOk(); $page->assertSee('Privatlizenz') ->assertSee('49,00 €') ->assertSee('Kaufen') ->assertSee(route('shop.closed'), false) ->assertDontSee('In den Warenkorb') ->assertDontSee('Jetzt kaufen') ->assertSee('noindex', false); $this->get(route('shop.closed')) ->assertOk() ->assertSee('noch nicht freigeschaltet') ->assertSee('Verkaufsstart') ->assertSee('noindex', false) ->assertSee(route('shop'), false); } public function test_with_the_checkout_closed_cart_checkout_and_buy_now_lead_to_the_notice_and_change_nothing(): void { $this->production(public: true); $offer = $this->privateKonflikte(); $member = $this->tina(); $this->postProduction(route('cart.add', $offer))->assertRedirect(route('shop.closed')); $this->postProduction(route('cart.buy', $offer))->assertRedirect(route('shop.closed')); $this->get(route('cart'))->assertRedirect(route('shop.closed')); $this->get(route('checkout'))->assertRedirect(route('shop.closed')); $this->actingAs($member)->get(route('checkout'))->assertRedirect(route('shop.closed')); $this->postProduction(route('checkout.place'), $this->consentFields() + ['digest' => str_repeat('a', 64)], $member)->assertRedirect(route('shop.closed')); $this->assertSame([], app(Cart::class)->items()); $this->assertSame(0, Order::query()->count()); $this->assertStringNotContainsString('href="'.route('cart').'"', $this->get(route('shop'))->getContent(), 'Das Warenkorb-Symbol bleibt aus.'); } public function test_with_both_switches_on_the_shop_works_as_before(): void { $this->production(public: true, release: true); $this->assertTrue(Shop::visible()); $this->assertTrue(Shop::checkoutOpen()); $this->get($this->articleUrl())->assertOk()->assertSee('In den Warenkorb')->assertSee('Jetzt kaufen')->assertDontSee('noindex', false); $this->postProduction(route('cart.add', $this->privateKonflikte()))->assertRedirect(); $this->get(route('cart'))->assertOk()->assertSee('Privatlizenz'); $this->get(route('shop.closed'))->assertRedirect(route('shop')); } public function test_a_release_alone_makes_the_shop_visible_without_a_second_setting(): void { $this->production(public: false, release: true); $this->assertTrue(Shop::visible(), 'Wer den Checkout öffnet, hat den Shop damit sichtbar gemacht.'); $this->get(route('shop'))->assertOk(); } public function test_a_test_or_local_instance_has_both_switches_on_with_the_test_driver_and_off_without_a_driver(): void { $this->assertTrue(Shop::visible()); $this->assertTrue(Shop::checkoutOpen()); config(['services.payment.driver' => 'aus']); $this->assertFalse(Shop::visible()); $this->assertFalse(Shop::checkoutOpen()); $this->get(route('shop'))->assertNotFound(); } public function test_the_stripe_webhook_and_gateway_stay_bound_to_the_live_release_not_to_visibility(): void { $this->production(public: true); $this->assertFalse(Shop::stripe()); $this->assertFalse(Shop::live()); $this->postProduction(route('stripe.webhook'))->assertNotFound(); } // ---- Menü und alte Adresse -------------------------------------------------------------------------------------- public function test_the_old_public_offers_address_redirects_to_the_shop_when_the_shop_is_visible(): void { $this->get('/angebote')->assertRedirect(route('shop')); } public function test_while_the_shop_is_hidden_an_existing_cms_page_stays_and_without_one_the_address_is_gone(): void { $page = Page::query()->create(['path' => '/angebote']); $revision = PageRevision::query()->create(['page_id' => $page->id, 'blocks' => [], 'sections' => [], 'seo_title' => 'Angebote']); $page->published_revision_id = $revision->id; $page->save(); $this->production(); $this->get('/angebote')->assertOk(); $page->delete(); $this->get('/angebote')->assertNotFound(); } public function test_menu_entries_pointing_into_the_shop_are_hidden_while_the_shop_is_not_visible(): void { NavigationItem::query()->create(['label' => 'Shopartikel', 'url' => '/shop/4', 'placement' => 'footer', 'sort_order' => 91, 'is_visible' => true]); NavigationItem::query()->create(['label' => 'Warenkorbweg', 'url' => '/warenkorb', 'placement' => 'footer', 'sort_order' => 92, 'is_visible' => true]); $startseite = Page::query()->firstOrCreate(['path' => '/']); $revision = PageRevision::query()->create(['page_id' => $startseite->id, 'blocks' => [], 'sections' => [], 'seo_title' => 'Start']); $startseite->published_revision_id = $revision->id; $startseite->save(); $this->get('/')->assertOk()->assertSee('href="'.url('/shop').'"', false); $this->production(); $this->get('/')->assertOk() ->assertDontSee('href="'.url('/shop').'"', false) ->assertDontSee('href="'.url('/shop/4').'"', false) ->assertDontSee('href="'.url('/warenkorb').'"', false); $this->production(public: true); $this->get('/')->assertOk()->assertSee('href="'.url('/shop').'"', false); } // ---- „Jetzt kaufen“ --------------------------------------------------------------------------------------------- public function test_buy_now_puts_the_option_in_the_cart_and_opens_the_order_overview(): void { $offer = $this->privateKonflikte(); $this->actingAs($this->tina())->post(route('cart.buy', $offer))->assertRedirect(route('checkout')); $this->assertSame([$offer->id], array_keys(app(Cart::class)->items())); $this->assertSame(0, Order::query()->count(), 'Erst der Knopf „Zahlungspflichtig bestellen“ legt eine Bestellung an.'); $this->actingAs($this->tina())->get(route('checkout')) ->assertOk() ->assertSee('Zahlungspflichtig bestellen') ->assertSee('Privatlizenz'); } public function test_buy_now_replaces_another_option_of_the_same_product_without_asking_and_keeps_other_products(): void { $private = $this->privateKonflikte(); $club = $this->clubKonflikte(); $other = $this->privateGrenzen(); $cart = app(Cart::class); $cart->add($private); $cart->add($other); $this->actingAs($this->tina())->post(route('cart.buy', $club))->assertRedirect(route('checkout')); $ids = array_keys(app(Cart::class)->items()); sort($ids); $expected = [$club->id, $other->id]; sort($expected); $this->assertSame($expected, $ids); } public function test_a_guest_buying_now_is_sent_to_the_login_and_keeps_the_cart(): void { $offer = $this->privateKonflikte(); $this->post(route('cart.buy', $offer))->assertRedirect(route('checkout')); $this->get(route('checkout'))->assertRedirect(route('login')); $this->assertSame([$offer->id], array_keys(app(Cart::class)->items())); $this->post('/anmelden', ['email' => $this->tina()->email, 'password' => ShopDemoSeeder::PASSWORD])->assertRedirect(); $this->assertAuthenticatedAs($this->tina()); $this->assertSame([$offer->id], array_keys(app(Cart::class)->items()), 'Der Warenkorb übersteht die Anmeldung.'); } public function test_buy_now_refuses_an_option_that_is_not_for_sale_and_a_missing_one(): void { $offer = $this->privateKonflikte(); app(OfferService::class)->end($offer); $this->actingAs($this->tina())->post(route('cart.buy', $offer))->assertRedirect()->assertSessionHasErrors('shop'); $this->assertSame([], app(Cart::class)->items()); $this->actingAs($this->tina())->post('/warenkorb/999999/jetzt-kaufen')->assertNotFound(); } public function test_buy_now_is_not_offered_for_an_option_the_account_already_owns_and_does_not_add_it(): void { $offer = $this->privateKonflikte(); $order = $this->orderOf($this->tina(), [$offer]); app(Checkout::class)->confirmTestPayment($order, 'test-eigen'); Mail::fake(); $page = $this->actingAs($this->tina())->get($this->articleUrl())->assertOk(); $page->assertSee('Bereits erworben'); $this->actingAs($this->tina())->post(route('cart.buy', $offer))->assertRedirect()->assertSessionHas('status'); $this->assertSame([], app(Cart::class)->items()); } // ---- Mitgliederseite „Angebote“ --------------------------------------------------------------------------------- private function freeOffer(): Offer { $article = app(ArticleService::class)->create('gratis-artikel'); app(ArticleService::class)->publishRelease($article, $this->article('shop-grenzen')->currentRelease->rootCompositionVersion, 'Erster Stand.'); $service = app(OfferService::class); $offer = $service->create('Gratis zum Start'); $rights = RightsProfileVersion::query()->whereHas('profile', fn ($query) => $query->where('name', 'Privatlizenz ehrenamtlich'))->firstOrFail(); $service->activate($offer, [['target' => $article->fresh(), 'rights' => $rights]], ['grant_kind' => 'auf_abruf', 'holder_kind' => 'person']); return $offer; } public function test_the_member_offers_page_offers_only_the_free_claim_and_a_link_to_the_shop_but_no_purchase(): void { $free = $this->freeOffer(); $page = $this->actingAs($this->tina())->get(route('member.offers'))->assertOk(); $page->assertSee('Gratis zum Start') ->assertSee('In die Bibliothek') ->assertSee(route('shop'), false) ->assertDontSee('Testzahlung') ->assertDontSee('Kaufen') ->assertDontSee('Konflikte im Training: Privatlizenz'); $page->assertSee(route('member.offers.claim', $free), false); } public function test_the_member_offers_page_hides_the_shop_link_while_the_shop_is_not_visible_and_claiming_still_works(): void { $free = $this->freeOffer(); $this->production(); $page = $this->actingAs($this->tina())->get(route('member.offers'))->assertOk(); $page->assertSee('Gratis zum Start')->assertDontSee(route('shop'), false); $this->postProduction(route('member.offers.claim', $free), [], $this->tina())->assertRedirect(route('member.library')); $this->assertSame(1, Acquisition::query()->where('party_id', $this->tina()->party_id)->count()); } public function test_the_member_offers_page_requires_a_login(): void { $this->get(route('member.offers'))->assertRedirect(); } }