presseportale/tests/Feature/PressReleaseBookingGateTest.php
Kevin Adametz ad2879daa4 R1.1: Buchungs-Gate geprueft + safe-by-default; Legacy-Buchungspfade festgeschrieben
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>
2026-07-03 08:07:01 +00:00

109 lines
3.6 KiB
PHP

<?php
use App\Enums\PressReleaseStatus;
use App\Enums\UserPaymentOptionStatus;
use App\Models\Category;
use App\Models\Company;
use App\Models\PressRelease;
use App\Models\SinglePurchase;
use App\Models\User;
use App\Models\UserPaymentOption;
use App\Services\PressRelease\BookingRequiredException;
use App\Services\PressRelease\PressReleaseService;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Support\Facades\Queue;
use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
beforeEach(function (): void {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
});
function draftForGate(): array
{
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create();
$customer->companies()->attach($company->id, ['role' => 'owner']);
$pr = PressRelease::factory()->create([
'user_id' => $customer->id,
'company_id' => $company->id,
'category_id' => Category::factory()->create()->id,
'portal' => $company->portal->value,
'status' => 'draft',
]);
return compact('customer', 'pr');
}
test('with the gate on a draft without a booking shows the notice and links to bookings', function () {
/** @var TestCase $this */
config()->set('billing.enforce_booking', true);
['customer' => $customer, 'pr' => $pr] = draftForGate();
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.show', ['id' => $pr->id])
->assertSee('Noch keine Buchung')
->assertSee('Buchung wählen')
->assertSee(route('me.bookings.index'), false)
->assertDontSee('Zur Prüfung einreichen');
});
test('with the gate on a paid single purchase unlocks the submission action', function () {
/** @var TestCase $this */
config()->set('billing.enforce_booking', true);
['customer' => $customer, 'pr' => $pr] = draftForGate();
SinglePurchase::factory()->paid()->create(['user_id' => $customer->id]);
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.show', ['id' => $pr->id])
->assertDontSee('Noch keine Buchung')
->assertSee('Zur Prüfung einreichen');
});
test('submitting without a booking is rejected at the service level', function () {
/** @var TestCase $this */
config()->set('billing.enforce_booking', true);
Queue::fake();
['pr' => $pr] = draftForGate();
expect(fn () => app(PressReleaseService::class)->submitForReview($pr))
->toThrow(BookingRequiredException::class);
expect($pr->fresh()->status)->toBe(PressReleaseStatus::Draft);
});
test('a legacy grandfathered booking allows submitting for review', function () {
/** @var TestCase $this */
config()->set('billing.enforce_booking', true);
Queue::fake();
['customer' => $customer, 'pr' => $pr] = draftForGate();
UserPaymentOption::factory()->create([
'user_id' => $customer->id,
'status' => UserPaymentOptionStatus::Grandfathered->value,
'grandfathered_until' => now()->addYear(),
'stripe_subscription_id' => null,
]);
app(PressReleaseService::class)->submitForReview($pr);
expect($pr->fresh()->status)->toBe(PressReleaseStatus::Review);
});
test('with the gate off the notice never shows', function () {
/** @var TestCase $this */
config()->set('billing.enforce_booking', false);
['customer' => $customer, 'pr' => $pr] = draftForGate();
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.show', ['id' => $pr->id])
->assertDontSee('Noch keine Buchung')
->assertSee('Zur Prüfung einreichen');
});