Default von billing.enforce_booking auf true gedreht: ein in Produktion vergessener Env-Key haette das Gate sonst still geoeffnet (jeder haette ohne Zahlung veroeffentlichen koennen). Tests ergaenzt: Grandfathered gueltig/abgelaufen/NULL (= abgelaufen, Entscheidung 19.06.), Active ausserhalb der Periode, Service-Level-Submit mit Legacy-Buchung und BookingRequiredException ohne Buchung. Scope-Logik selbst war korrekt. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
134 lines
4.5 KiB
PHP
134 lines
4.5 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 does not count as booking', function () {
|
|
// NULL = abgelaufen/ungültig (User-Entscheidung 19.06.2026).
|
|
$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,
|
|
'stripe_subscription_id' => null,
|
|
]);
|
|
|
|
expect($user->hasActiveBooking())->toBeFalse();
|
|
});
|