Domäne: Rabattcodes mit Geltungsbereich (Warenkorb oder gewählte Ziele) und Einlösungsberechtigung (allgemein oder freigeschaltete Konten) unabhängig voneinander; Prüfung bei Eingabe, in jeder Übersicht und unter Sperre vor Bestellabschluss (Prüfsumme der Übersicht enthält den Rabatt). Rabatt je Position, Zwischensumme und Zahlbetrag in der Bestellung; Stripe erhält den Preis nach Rabatt. Nutzerkreis (Einzel-/Mehrfachlizenz, Trainerzahl) an der Angebotsfassung, kopiert in Bestellposition und Erwerb, Teil der Prüfung "Bereits erworben". OfferService::activate lehnt Angebote ohne veröffentlichten Artikelstand ab. Kasse: Zustimmungen als Pflichtangabe mit Nachweis, "Zahlungspflichtig bestellen", Rechnungsweg für Organisationen, Erstattung über Stripe, Widerruf erklären. Produktivfreigabe SHOP_LIVE_RELEASE standardmäßig aus. Bestandstests geändert (fachlich überholt, Assertions nicht abgeschwächt): die Schaltfläche heißt "Zahlungspflichtig bestellen" (§ 312j BGB), die Summe heißt "Zu zahlen", der Vereinshinweis steht als Rollenliste; Bestellungen über die Kasse senden jetzt die Zustimmungen. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
367 lines
17 KiB
PHP
367 lines
17 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature;
|
||
|
||
use App\Domain\Catalog\Bundle;
|
||
use App\Domain\Catalog\DomainRuleException;
|
||
use App\Domain\Commerce\Cart;
|
||
use App\Domain\Commerce\Checkout;
|
||
use App\Domain\Commerce\Discount\DiscountCode;
|
||
use App\Domain\Commerce\Discount\DiscountRelease;
|
||
use App\Domain\Commerce\Discount\DiscountResult;
|
||
use App\Domain\Commerce\Discount\DiscountService;
|
||
use App\Domain\Commerce\Order;
|
||
use App\Domain\Commerce\StaleOverview;
|
||
use App\Models\User;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Tests\Support\BuildsShop;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* Rabattcodes (PAY-008, FACH-14): Geltungsbereich und Einlösungsberechtigung sind unabhängig, die Prüfung läuft
|
||
* serverseitig bei der Einlösung und erneut vor Bestellabschluss, der Rabatt bleibt in der Bestellung nachvollziehbar.
|
||
*/
|
||
class DiscountCodeTest extends TestCase
|
||
{
|
||
use BuildsShop;
|
||
use RefreshDatabase;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
$this->seedShop();
|
||
$this->travelTo('2026-11-24 10:00:00');
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @param list<array{type: string, id: int}> $targets
|
||
*/
|
||
private function code(string $code, array $data = [], array $targets = []): DiscountCode
|
||
{
|
||
return app(DiscountService::class)->save([
|
||
'code' => $code,
|
||
'label' => 'Test '.$code,
|
||
'kind' => DiscountCode::PERCENT,
|
||
'percent' => 10,
|
||
'scope' => DiscountCode::SCOPE_CART,
|
||
'eligibility' => DiscountCode::OPEN,
|
||
'active' => true,
|
||
...$data,
|
||
], $targets);
|
||
}
|
||
|
||
private function release(DiscountCode $code, User $member): DiscountRelease
|
||
{
|
||
return app(DiscountService::class)->release($code, $member, $this->vera(), null, 'Zertifikat geprüft');
|
||
}
|
||
|
||
private function cartFor(User $user, array $offers, ?string $code = null): Cart
|
||
{
|
||
$cart = app(Cart::class);
|
||
$cart->clear();
|
||
|
||
foreach ($offers as $offer) {
|
||
$cart->add($offer);
|
||
}
|
||
|
||
if ($code !== null) {
|
||
$cart->applyCode($code, $user);
|
||
}
|
||
|
||
return $cart;
|
||
}
|
||
|
||
public function test_a_general_code_reduces_the_whole_cart(): void
|
||
{
|
||
$this->code('SOMMER10');
|
||
$tina = $this->tina();
|
||
$cart = $this->cartFor($tina, [$this->privateKonflikte(), $this->privateGrenzen()], 'sommer10');
|
||
|
||
$review = $cart->review($tina);
|
||
|
||
$this->assertSame(7800, $review->subtotalCents());
|
||
$this->assertSame(780, $review->discountCents());
|
||
$this->assertSame(7020, $review->totalCents());
|
||
|
||
$order = app(Checkout::class)->placeFromCart($tina, $cart, null, $review->digest());
|
||
|
||
$this->assertSame(7800, $order->subtotal_cents);
|
||
$this->assertSame(780, $order->discount_cents);
|
||
$this->assertSame(7020, $order->total_cents);
|
||
$this->assertSame('SOMMER10', $order->discount_snapshot['code']);
|
||
$this->assertSame([490, 290], $order->lines->pluck('discount_cents')->all());
|
||
$this->assertSame([4900, 2900], $order->lines->pluck('price_cents')->all(), 'Die Listenpreise bleiben unverändert gespeichert.');
|
||
$this->assertSame(0, app(Cart::class)->count());
|
||
$this->assertNull(session(Cart::DISCOUNT_KEY), 'Der Code gehört zur Bestellung und ist danach aus dem Warenkorb entfernt.');
|
||
}
|
||
|
||
public function test_an_item_specific_code_reduces_only_the_selected_position_in_a_mixed_cart(): void
|
||
{
|
||
$konflikte = $this->privateKonflikte();
|
||
$this->code('NURKONFLIKT', targets: [['type' => DiscountCode::TARGET_OFFER, 'id' => $konflikte->id]], data: ['scope' => DiscountCode::SCOPE_SELECTED]);
|
||
$tina = $this->tina();
|
||
$cart = $this->cartFor($tina, [$konflikte, $this->privateGrenzen()], 'NURKONFLIKT');
|
||
|
||
$review = $cart->review($tina);
|
||
|
||
$this->assertSame(490, $review->discountCents());
|
||
$this->assertSame(490, $review->discountFor($review->lines[0]));
|
||
$this->assertSame(0, $review->discountFor($review->lines[1]), 'Die nicht gewählte Position bleibt zum Listenpreis.');
|
||
$this->assertSame(7310, $review->totalCents());
|
||
}
|
||
|
||
public function test_a_license_article_target_does_not_make_a_bundle_containing_it_discountable_and_vice_versa(): void
|
||
{
|
||
$article = $this->article('shop-konflikte');
|
||
$bundle = Bundle::query()->where('name', 'Starterpaket')->firstOrFail();
|
||
$tina = $this->tina();
|
||
|
||
$this->code('ARTIKEL', ['scope' => DiscountCode::SCOPE_SELECTED], [['type' => DiscountCode::TARGET_ARTICLE, 'id' => $article->id]]);
|
||
$cart = $this->cartFor($tina, [$this->privateKonflikte(), $this->starter()], 'ARTIKEL');
|
||
$review = $cart->review($tina);
|
||
|
||
$this->assertSame(490, $review->discountCents(), 'Zehn Prozent nur auf das Angebot, dessen Position der Artikel selbst ist.');
|
||
$this->assertSame(0, $review->discountFor($review->lines[1]), 'Das Starterpaket enthält den Artikel, ist aber nicht als Ziel gewählt.');
|
||
|
||
$this->code('PAKET', ['scope' => DiscountCode::SCOPE_SELECTED], [['type' => DiscountCode::TARGET_BUNDLE, 'id' => $bundle->id]]);
|
||
$cart = $this->cartFor($tina, [$this->privateKonflikte(), $this->starter()], 'PAKET');
|
||
$review = $cart->review($tina);
|
||
|
||
$this->assertSame(0, $review->discountFor($review->lines[0]), 'Der enthaltene Artikel wird durch ein Paketziel nicht rabattfähig.');
|
||
$this->assertSame(590, $review->discountFor($review->lines[1]));
|
||
}
|
||
|
||
public function test_a_restricted_code_works_repeatedly_for_a_released_account_only(): void
|
||
{
|
||
$x = $this->code('ZERT', ['eligibility' => DiscountCode::RESTRICTED, 'percent' => 20]);
|
||
$this->release($x, $this->tina());
|
||
|
||
$first = $this->orderWithCode($this->tina(), 'ZERT');
|
||
$second = $this->orderWithCode($this->tina(), 'ZERT');
|
||
|
||
$this->assertSame(980, $first->discount_cents);
|
||
$this->assertSame(980, $second->discount_cents, 'Das freigeschaltete Konto löst den Code bei späteren Bestellungen erneut ein.');
|
||
|
||
$result = $this->cartFor($this->max(), [$this->privateKonflikte()])->applyCode('ZERT', $this->max());
|
||
$this->assertSame(DiscountResult::NO_RELEASE, $result->state);
|
||
$this->assertStringContainsString('nicht freigeschaltet', (string) $result->message);
|
||
$this->assertSame(0, app(Cart::class)->review($this->max())->discountCents(), 'Wer den Code kennt, aber keine Freigabe hat, erhält keinen Rabatt.');
|
||
}
|
||
|
||
public function test_a_release_for_one_code_does_not_entitle_to_another_code(): void
|
||
{
|
||
$x = $this->code('CODEX', ['eligibility' => DiscountCode::RESTRICTED]);
|
||
$y = $this->code('CODEY', ['eligibility' => DiscountCode::RESTRICTED]);
|
||
$tina = $this->tina();
|
||
$this->release($x, $tina);
|
||
|
||
$this->cartFor($tina, [$this->privateKonflikte()]);
|
||
$this->assertTrue(app(Cart::class)->applyCode('CODEX', $tina)->applied());
|
||
$blocked = app(Cart::class)->applyCode('CODEY', $tina);
|
||
|
||
$this->assertSame(DiscountResult::NO_RELEASE, $blocked->state);
|
||
$this->assertSame($x->id, app(Cart::class)->discountCode()?->id, 'Der abgelehnte zweite Code ändert den bisherigen nicht.');
|
||
|
||
$this->release($y, $tina);
|
||
$this->assertTrue(app(Cart::class)->applyCode('CODEY', $tina)->applied());
|
||
$this->assertSame($y->id, app(Cart::class)->discountCode()?->id, 'Pro Bestellung gilt höchstens ein Code: der zweite ersetzt den ersten.');
|
||
}
|
||
|
||
public function test_a_revoked_release_stops_the_discount_and_keeps_the_history(): void
|
||
{
|
||
$code = $this->code('ZERT', ['eligibility' => DiscountCode::RESTRICTED]);
|
||
$release = $this->release($code, $this->tina());
|
||
$this->assertTrue($this->cartFor($this->tina(), [$this->privateKonflikte()])->applyCode('ZERT', $this->tina())->applied());
|
||
|
||
app(DiscountService::class)->revoke($release, $this->vera(), 'Zertifikat abgelaufen');
|
||
|
||
$review = app(Cart::class)->review($this->tina());
|
||
$this->assertSame(DiscountResult::NO_RELEASE, $review->discount?->state);
|
||
$this->assertSame(0, $review->discountCents());
|
||
$this->assertTrue($review->discountBlocks());
|
||
$this->assertFalse($review->canOrder(), 'Ein vorgemerkter Code, der nicht mehr gilt, hält die Bestellung an.');
|
||
$this->assertNotNull($release->fresh()->revoked_at);
|
||
$this->assertSame($this->vera()->id, $release->fresh()->revoked_by);
|
||
$this->assertSame(1, DiscountRelease::query()->count(), 'Freigaben werden nie gelöscht.');
|
||
$this->assertSame(DiscountResult::NO_RELEASE, app(Cart::class)->applyCode('ZERT', $this->tina())->state);
|
||
}
|
||
|
||
public function test_a_release_can_have_an_end_date_and_cannot_be_granted_twice_or_for_an_open_code(): void
|
||
{
|
||
$code = $this->code('BEFRISTET', ['eligibility' => DiscountCode::RESTRICTED]);
|
||
$service = app(DiscountService::class);
|
||
$service->release($code, $this->tina(), $this->vera(), now()->addDays(2));
|
||
|
||
$this->assertTrue($this->cartFor($this->tina(), [$this->privateKonflikte()])->applyCode('BEFRISTET', $this->tina())->applied());
|
||
$this->travelTo(now()->addDays(3));
|
||
$this->assertSame(DiscountResult::NO_RELEASE, app(Cart::class)->review($this->tina())->discount?->state);
|
||
|
||
$this->expectException(DomainRuleException::class);
|
||
$service->release($this->code('OFFEN'), $this->tina(), $this->vera());
|
||
}
|
||
|
||
public function test_an_expired_deactivated_or_not_yet_valid_code_explains_itself(): void
|
||
{
|
||
$tina = $this->tina();
|
||
$this->code('ALT', ['valid_until' => '2026-11-20']);
|
||
$this->code('AUS', ['active' => false]);
|
||
$this->code('SPAETER', ['valid_from' => '2026-12-01']);
|
||
$cart = $this->cartFor($tina, [$this->privateKonflikte()]);
|
||
|
||
$expired = $cart->applyCode('ALT', $tina);
|
||
$this->assertSame(DiscountResult::EXPIRED, $expired->state);
|
||
$this->assertStringContainsString('abgelaufen', (string) $expired->message);
|
||
$this->assertStringContainsString('20.11.2026', (string) $expired->message);
|
||
$this->assertSame(DiscountResult::INACTIVE, $cart->applyCode('AUS', $tina)->state);
|
||
$this->assertStringContainsString('01.12.2026', (string) $cart->applyCode('SPAETER', $tina)->message);
|
||
$this->assertSame(DiscountResult::UNKNOWN, $cart->applyCode('GIBTESNICHT', $tina)->state);
|
||
$this->assertNull($cart->discountCode());
|
||
}
|
||
|
||
public function test_a_code_that_matches_no_position_says_so(): void
|
||
{
|
||
$this->code('NURGRENZEN', ['scope' => DiscountCode::SCOPE_SELECTED], [['type' => DiscountCode::TARGET_OFFER, 'id' => $this->privateGrenzen()->id]]);
|
||
$tina = $this->tina();
|
||
$cart = $this->cartFor($tina, [$this->privateKonflikte()]);
|
||
|
||
$result = $cart->applyCode('NURGRENZEN', $tina);
|
||
|
||
$this->assertSame(DiscountResult::NO_MATCH, $result->state);
|
||
$this->assertStringContainsString('keines davon liegt in deiner Bestellung', (string) $result->message);
|
||
}
|
||
|
||
public function test_an_owned_position_is_not_discounted_and_does_not_count_as_a_match(): void
|
||
{
|
||
$tina = $this->tina();
|
||
$order = $this->orderOf($tina, [$this->privateKonflikte()]);
|
||
app(Checkout::class)->confirmTestPayment($order, 'test-own');
|
||
$this->code('NURKONFLIKT', ['scope' => DiscountCode::SCOPE_SELECTED], [['type' => DiscountCode::TARGET_OFFER, 'id' => $this->privateKonflikte()->id]]);
|
||
|
||
$result = $this->cartFor($tina, [$this->privateKonflikte(), $this->privateGrenzen()])->applyCode('NURKONFLIKT', $tina);
|
||
|
||
$this->assertSame(DiscountResult::NO_MATCH, $result->state, 'Die bereits erworbene Position geht nicht in die Bestellung ein.');
|
||
}
|
||
|
||
public function test_a_revoked_release_between_overview_and_order_creates_no_order(): void
|
||
{
|
||
$code = $this->code('ZERT', ['eligibility' => DiscountCode::RESTRICTED]);
|
||
$release = $this->release($code, $this->tina());
|
||
$tina = $this->tina();
|
||
$cart = $this->cartFor($tina, [$this->privateKonflikte()], 'ZERT');
|
||
$shown = $cart->review($tina)->digest();
|
||
|
||
app(DiscountService::class)->revoke($release, $this->vera());
|
||
|
||
try {
|
||
app(Checkout::class)->placeFromCart($tina, $cart, null, $shown);
|
||
$this->fail('Die Bestellung zum rabattierten Preis darf nicht entstehen.');
|
||
} catch (StaleOverview) {
|
||
$this->assertSame(0, Order::query()->count());
|
||
}
|
||
}
|
||
|
||
public function test_a_code_deactivated_between_overview_and_order_creates_no_order(): void
|
||
{
|
||
$code = $this->code('SOMMER10');
|
||
$tina = $this->tina();
|
||
$cart = $this->cartFor($tina, [$this->privateKonflikte()], 'SOMMER10');
|
||
$shown = $cart->review($tina)->digest();
|
||
|
||
app(DiscountService::class)->setActive($code, false);
|
||
|
||
$this->expectException(StaleOverview::class);
|
||
app(Checkout::class)->placeFromCart($tina, $cart, null, $shown);
|
||
}
|
||
|
||
public function test_a_blocked_code_must_be_removed_before_ordering_at_full_price(): void
|
||
{
|
||
$code = $this->code('SOMMER10');
|
||
$tina = $this->tina();
|
||
$cart = $this->cartFor($tina, [$this->privateKonflikte()], 'SOMMER10');
|
||
app(DiscountService::class)->setActive($code, false);
|
||
$review = $cart->review($tina);
|
||
|
||
$this->assertFalse($review->canOrder());
|
||
$this->expectException(DomainRuleException::class);
|
||
app(Checkout::class)->placeFromCart($tina, $cart, null, $review->digest());
|
||
}
|
||
|
||
public function test_the_discount_may_not_push_the_payable_amount_below_the_payment_minimum(): void
|
||
{
|
||
$this->code('GRATIS', ['percent' => 100]);
|
||
$tina = $this->tina();
|
||
|
||
$result = $this->cartFor($tina, [$this->privateGrenzen()])->applyCode('GRATIS', $tina);
|
||
|
||
$this->assertSame(DiscountResult::TOO_HIGH, $result->state);
|
||
$this->assertStringContainsString('0,50 €', (string) $result->message);
|
||
}
|
||
|
||
public function test_the_checkout_page_applies_and_removes_a_code_and_shows_the_discount_and_payable_amount(): void
|
||
{
|
||
$this->code('SOMMER10');
|
||
$tina = $this->tina();
|
||
$this->actingAs($tina)->post(route('cart.add', $this->privateKonflikte()));
|
||
|
||
$this->post(route('checkout.code'), ['code' => 'sommer10'])->assertRedirect(route('checkout'));
|
||
$this->get(route('checkout'))
|
||
->assertOk()
|
||
->assertSee('SOMMER10')
|
||
->assertSee('Zwischensumme')
|
||
->assertSee('−4,90 €')
|
||
->assertSee('44,10 €')
|
||
->assertSee('Code entfernen');
|
||
|
||
$this->post(route('checkout.code.remove'))->assertRedirect(route('checkout'));
|
||
$this->get(route('checkout'))->assertOk()->assertDontSee('Zwischensumme')->assertSee('49,00 €');
|
||
|
||
$this->post(route('checkout.code'), ['code' => 'GIBTESNICHT'])->assertSessionHasErrors('code');
|
||
$this->post(route('checkout.code'), ['code' => ''])->assertSessionHasErrors('code');
|
||
}
|
||
|
||
public function test_the_checkout_page_explains_a_missing_release_in_plain_words(): void
|
||
{
|
||
$this->code('ZERT', ['eligibility' => DiscountCode::RESTRICTED]);
|
||
$this->actingAs($this->max())->post(route('cart.add', $this->privateKonflikte()));
|
||
|
||
$response = $this->post(route('checkout.code'), ['code' => 'ZERT'])->assertSessionHasErrors('code');
|
||
$this->assertStringContainsString('nicht freigeschaltet', $response->getSession()->get('errors')->first('code'));
|
||
$this->get(route('checkout'))->assertOk()->assertSee('49,00 €')->assertDontSee('Zwischensumme');
|
||
}
|
||
|
||
public function test_codes_are_validated_when_saved(): void
|
||
{
|
||
$service = app(DiscountService::class);
|
||
$base = ['label' => 'x', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'allgemein'];
|
||
|
||
foreach ([
|
||
['code' => 'a b'],
|
||
['code' => 'OK1', 'label' => ''],
|
||
['code' => 'OK1', 'percent' => 0],
|
||
['code' => 'OK1', 'percent' => 101],
|
||
['code' => 'OK1', 'kind' => 'betrag', 'amount_euro' => '0'],
|
||
['code' => 'OK1', 'scope' => 'auswahl'],
|
||
['code' => 'OK1', 'valid_from' => '2026-12-01', 'valid_until' => '2026-11-01'],
|
||
] as $override) {
|
||
try {
|
||
$service->save([...$base, ...$override], []);
|
||
$this->fail('Ungültige Angaben müssen abgewiesen werden: '.json_encode($override));
|
||
} catch (DomainRuleException) {
|
||
$this->assertSame(0, DiscountCode::query()->count());
|
||
}
|
||
}
|
||
|
||
$service->save([...$base, 'code' => 'ok1'], []);
|
||
$this->expectException(DomainRuleException::class);
|
||
$service->save([...$base, 'code' => 'OK1'], []);
|
||
}
|
||
|
||
private function orderWithCode(User $user, string $code): Order
|
||
{
|
||
$cart = $this->cartFor($user, [$this->privateKonflikte()], $code);
|
||
|
||
return app(Checkout::class)->placeFromCart($user, $cart, null, $cart->review($user)->digest());
|
||
}
|
||
}
|