presseportale/tests/Feature/CustomerStatusSummaryTest.php
Kevin Adametz a82964df2b Optimierung Phase 0: Korrektheit & Security
Konsolidierter Entwicklungsplan aus den zwei Analyse-Dokumenten plus
Umsetzung der Phase-0-Massnahmen (Korrektheit/Security, je mit Tests):

0.1 BasicAuth: timing-sicherer Vergleich (hash_equals, beide Felder ohne
    Short-Circuit); Enabled-Check zur Laufzeit via config('auth.basic.*')
    statt env() im Bootstrap-Closure (greift jetzt unter config:cache);
    Config-Block + .env.example. Fund: auth.basic.* war nie als Config
    definiert, Gate war faktisch unkonfigurierbar.
0.2 Legacy-Invoice-"latest"-Abfrage zentralisiert (LegacyInvoice::
    scopeLatestFirst + User::latestLegacyInvoice); beide Consumer N+1-frei
    umgestellt; unterschiedliches null-Verhalten bewusst erhalten.
0.3 Payment-Option-Eligibility vereinheitlicht (period-aware, strikt):
    UserPaymentOption::scopeActiveNow/grandfatheredNow + User::
    hasActivePaymentOption; hasActiveBooking/hasUnlimitedPressReleaseQuota
    und ApiAccessEligibilityService darauf umgestellt; $hasGrandfathered-
    Mislabel behoben.
0.4 Theme->Portal-Vokabular: totes, projektfremdes ThemeMiddleware
    geloescht; Mapping in Portal::fromTheme() zentralisiert; fremden
    b2in-Default im ThemeServiceProvider auf Portal-Default korrigiert.
0.5 InvoiceNumberGenerator: Erst-Insert race-sicher via insertOrIgnore
    (Unique-Index auf circle existiert bereits). CreditWalletService war
    bereits abgesichert (Unique-Index + createOrFirst).

Tests: 4 neue Dateien, 5 bestehende auf gueltiges grandfathered_until
angepasst. Volle Suite gruen (793 passed, 1 skipped).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 08:09:02 +00:00

102 lines
3.3 KiB
PHP

<?php
use App\Enums\CreditTransactionType;
use App\Enums\Tier;
use App\Enums\UserPaymentOptionStatus;
use App\Models\User;
use App\Models\UserPaymentOption;
use App\Services\Billing\CreditWalletService;
use App\Services\Customer\CustomerStatusSummary;
use Database\Seeders\RolesAndPermissionsSeeder;
use Tests\TestCase;
beforeEach(function (): void {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
});
test('a user without any booking and the gate off counts as unlimited and active', function () {
config()->set('billing.enforce_booking', false);
$user = User::factory()->create();
$status = CustomerStatusSummary::fromUser($user);
expect($status->tier)->toBe(Tier::Einzel);
expect($status->hasActiveBooking)->toBeTrue();
expect($status->unlimitedQuota)->toBeTrue();
expect($status->quotaRemaining)->toBeNull();
expect($status->bookingLabel())->toBe(__('Kein Abo'));
expect($status->badgeLabel())->toContain('∞');
});
test('a user without booking and the gate enforced has no active booking and zero quota', function () {
config()->set('billing.enforce_booking', true);
$user = User::factory()->create();
$status = CustomerStatusSummary::fromUser($user);
expect($status->hasActiveBooking)->toBeFalse();
expect($status->unlimitedQuota)->toBeFalse();
expect($status->quotaRemaining)->toBe(0);
expect($status->quotaTotal)->toBe(0);
});
test('a grandfathered bestandskunde is labelled and counts as active', function () {
config()->set('billing.enforce_booking', true);
$user = User::factory()->create();
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'status' => UserPaymentOptionStatus::Grandfathered->value,
'grandfathered_until' => now()->addYear(),
'stripe_subscription_id' => null,
]);
$status = CustomerStatusSummary::fromUser($user->fresh());
expect($status->hasActiveBooking)->toBeTrue();
expect($status->hasGrandfathered)->toBeTrue();
expect($status->bookingLabel())->toBe(__('Bestandstarif'));
});
test('credit balance is surfaced and shown in the badge', function () {
config()->set('billing.enforce_booking', true);
$user = User::factory()->create();
app(CreditWalletService::class)->credit($user, 27, CreditTransactionType::Topup, 'Paket 25');
$status = CustomerStatusSummary::fromUser($user->fresh());
expect($status->creditBalance)->toBe(27);
expect($status->hasCredits())->toBeTrue();
expect($status->badgeLabel())->toContain('27');
});
test('a user without credits hides the credit segment in the badge', function () {
config()->set('billing.enforce_booking', false);
$user = User::factory()->create();
$status = CustomerStatusSummary::fromUser($user);
expect($status->hasCredits())->toBeFalse();
expect($status->badgeLabel())->not->toContain('Cr');
});
test('the status menu renders inside the customer layout header', function () {
/** @var TestCase $this */
config()->set('billing.enforce_booking', true);
$user = User::factory()->create(['is_active' => true]);
$user->assignRole('customer');
$this->actingAs($user);
$this->get(route('me.dashboard'))
->assertOk()
->assertSee(__('Aktuelle Buchung'))
->assertSee(__('PMs frei'))
->assertSee(route('me.bookings.index'), false);
});