presseportale/tests/Feature/Billing/HasActiveBookingTest.php
Kevin Adametz 3364fd13b8 Fix: Bestandsschutz ohne explizites Vertragsende zaehlte nie als aktiv
Der grandfatheredNow-Scope verlangte ein gesetztes grandfathered_until —
das ist aber das explizite Legacy-Vertragsende (valid_until_date) und im
Import-Regelfall NULL (offene Dauervereinbarung). Ergebnis: praktisch
kein Legacy-Kunde hatte eine aktive Zahlungsoption; eingeloggte
Bestandskunden sahen "Buchung erforderlich" und konnten nicht einreichen.
Jetzt gilt: grandfathered_until deckelt, wenn gesetzt; sonst zaehlt die
laufende, vom MAN-Rechnungslauf fortgerollte Periode (current_period_end
= next_due_date). Tests decken beide NULL-Faelle und den Deckel ab.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 11:40:39 +00:00

173 lines
6.1 KiB
PHP

<?php
use App\Enums\UserPaymentOptionStatus;
use App\Models\PaymentOption;
use App\Models\SinglePurchase;
use App\Models\User;
use App\Models\UserPaymentOption;
beforeEach(function (): void {
config()->set('billing.enforce_booking', true);
});
test('without the enforce flag everyone counts as booked', function () {
config()->set('billing.enforce_booking', false);
expect(User::factory()->create()->hasActiveBooking())->toBeTrue();
});
test('a user without any booking is rejected when the gate is enforced', function () {
expect(User::factory()->create()->hasActiveBooking())->toBeFalse();
});
test('an active cashier subscription counts as booking', function () {
$user = User::factory()->create();
$user->subscriptions()->create([
'type' => 'default',
'stripe_id' => 'sub_test_123',
'stripe_status' => 'active',
'stripe_price' => 'price_test',
'quantity' => 1,
]);
expect($user->hasActiveBooking())->toBeTrue();
});
test('a paid unconsumed single purchase counts as booking', function () {
$user = User::factory()->create();
SinglePurchase::factory()->paid()->create(['user_id' => $user->id]);
expect($user->hasActiveBooking())->toBeTrue();
});
test('a consumed single purchase does not count as booking', function () {
$user = User::factory()->create();
SinglePurchase::factory()->consumed()->create(['user_id' => $user->id]);
expect($user->hasActiveBooking())->toBeFalse();
});
test('an active legacy payment agreement counts as booking (manual circle)', function () {
$user = User::factory()->create();
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'payment_option_id' => PaymentOption::factory()->create()->id,
'status' => UserPaymentOptionStatus::Active->value,
'stripe_subscription_id' => null,
]);
expect($user->hasActiveBooking())->toBeTrue();
});
test('a cancelled legacy agreement does not count as booking', function () {
$user = User::factory()->create();
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'payment_option_id' => PaymentOption::factory()->create()->id,
'status' => UserPaymentOptionStatus::Cancelled->value,
'stripe_subscription_id' => null,
]);
expect($user->hasActiveBooking())->toBeFalse();
});
test('an active legacy agreement outside its current period does not count as booking', function () {
$user = User::factory()->create();
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'payment_option_id' => PaymentOption::factory()->create()->id,
'status' => UserPaymentOptionStatus::Active->value,
'current_period_start' => now()->subMonths(2)->startOfMonth(),
'current_period_end' => now()->subMonths(2)->endOfMonth(),
'grandfathered_until' => null,
'stripe_subscription_id' => null,
]);
expect($user->hasActiveBooking())->toBeFalse();
});
test('a grandfathered agreement with valid protection counts as booking', function () {
$user = User::factory()->create();
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'payment_option_id' => PaymentOption::factory()->create()->id,
'status' => UserPaymentOptionStatus::Grandfathered->value,
'grandfathered_until' => now()->addYear(),
'stripe_subscription_id' => null,
]);
expect($user->hasActiveBooking())->toBeTrue();
});
test('a grandfathered agreement with expired protection does not count as booking', function () {
$user = User::factory()->create();
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'payment_option_id' => PaymentOption::factory()->create()->id,
'status' => UserPaymentOptionStatus::Grandfathered->value,
'grandfathered_until' => now()->subDay(),
'stripe_subscription_id' => null,
]);
expect($user->hasActiveBooking())->toBeFalse();
});
test('a grandfathered agreement without an end date counts while its period is running', function () {
// Regelfall des Imports (Fix 06.07.2026): offene Dauervereinbarung —
// grandfathered_until NULL, Gültigkeit über die laufende Periode
// (current_period_end = next_due_date, vom MAN-Lauf fortgerollt).
$user = User::factory()->create();
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'payment_option_id' => PaymentOption::factory()->create()->id,
'status' => UserPaymentOptionStatus::Grandfathered->value,
'grandfathered_until' => null,
'current_period_start' => now()->subMonths(3),
'current_period_end' => now()->addMonths(9),
'stripe_subscription_id' => null,
]);
expect($user->hasActiveBooking())->toBeTrue();
});
test('a grandfathered agreement without an end date lapses with its period', function () {
$user = User::factory()->create();
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'payment_option_id' => PaymentOption::factory()->create()->id,
'status' => UserPaymentOptionStatus::Grandfathered->value,
'grandfathered_until' => null,
'current_period_start' => now()->subYears(2),
'current_period_end' => now()->subYear(),
'stripe_subscription_id' => null,
]);
expect($user->hasActiveBooking())->toBeFalse();
});
test('an explicit legacy end date caps a grandfathered agreement despite a running period', function () {
// grandfathered_until = explizites Vertragsende (valid_until_date) —
// wenn gesetzt und überschritten, zählt die Vereinbarung nicht mehr,
// auch wenn die Abrechnungsperiode formal noch läuft.
$user = User::factory()->create();
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'payment_option_id' => PaymentOption::factory()->create()->id,
'status' => UserPaymentOptionStatus::Grandfathered->value,
'grandfathered_until' => now()->subDay(),
'current_period_start' => now()->subMonths(3),
'current_period_end' => now()->addMonths(9),
'stripe_subscription_id' => null,
]);
expect($user->hasActiveBooking())->toBeFalse();
});