Files
laraundfelix/tests/Feature/ShopSalesAdminTest.php
T
LarsandClaude Sonnet 5.5 42e0a6bb93
CI Feature / test (push) Failing after 3m2s
Mache den Download bei reiner Download-Lizenz erreichbar und zeige den Stand zum Verkaufsstart
Die Artikelseite zeigte bei einer Lizenz nur zum Herunterladen (kein Streamen oder
Ansehen) "Nicht enthalten"; jetzt bietet sie den Download. Tests belegen Einzelfilm
und Begleit-PDF aus der Bibliothek mit Dateiname und Zugriffsgrenzen. Neue
Verwaltungsseite Verkaufsstart nennt fehlende Rechtstexte und den Zustand der
Zahlungseinrichtung, ohne Schlüssel zu zeigen.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-22 19:58:22 +01:00

303 lines
14 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Commerce\Checkout;
use App\Domain\Commerce\Discount\DiscountCode;
use App\Domain\Commerce\Discount\DiscountRelease;
use App\Domain\Commerce\Discount\DiscountService;
use App\Domain\Entitlement\Grant;
use App\Domain\Entitlement\GrantStatus;
use App\Filament\Resources\DiscountCodes\Pages\ListDiscountCodes;
use App\Filament\Resources\DiscountCodes\Pages\ViewDiscountCode;
use App\Filament\Resources\Offers\Pages\ViewOffer;
use App\Filament\Resources\Orders\Pages\ListOrders;
use App\Filament\Resources\Orders\Pages\ViewOrder;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
use Tests\Support\BuildsShop;
use Tests\TestCase;
/**
* Verwaltung des Verkaufsstarts: Rabattcodes mit Freigabe einzelner Konten, Bestellverwaltung mit Zahlungseingang
* auf Rechnung und Erstattung, Nutzerkreis in der Angebotsmaske.
*/
class ShopSalesAdminTest extends TestCase
{
use BuildsShop;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seedShop();
$this->travelTo('2026-11-24 10:00:00');
}
private function staff(string $state = 'katalogverwalter'): User
{
$user = User::factory()->{$state}()->create();
$user->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $user;
}
public function test_the_operator_creates_a_code_in_the_mask_and_edits_it_with_the_same_mask(): void
{
$keeper = $this->staff();
$offer = $this->privateKonflikte();
Livewire::actingAs($keeper, 'staff')
->test(ListDiscountCodes::class)
->assertSee('Noch keine Rabattcodes')
->mountAction('create')
->fillForm([
'code' => 'zert 2026', 'label' => 'Zertifizierungscode', 'kind' => 'prozent', 'percent' => 15,
'scope' => 'auswahl', 'offer_ids' => [$offer->id], 'eligibility' => 'freigabe',
'valid_from' => '2026-11-01', 'valid_until' => '2026-12-31', 'active' => true,
])
->callMountedAction()
->assertHasNoActionErrors();
$code = DiscountCode::query()->firstOrFail();
$this->assertSame('ZERT2026', $code->code);
$this->assertSame('auswahl', $code->scope);
$this->assertSame('freigabe', $code->eligibility, 'Geltungsbereich und Einlösung sind getrennte Einstellungen.');
$this->assertSame([$offer->id], $code->targets->pluck('target_id')->map(fn ($id): int => (int) $id)->all());
$this->assertSame('2026-12-31 23:59:59', $code->valid_until->timezone(config('app.timezone'))->format('Y-m-d H:i:s'), 'Das Ende gilt einschließlich des ganzen Tages.');
$component = Livewire::actingAs($keeper, 'staff')
->test(ViewDiscountCode::class, ['record' => $code->getKey()])
->assertSee('ZERT2026')
->assertSee('nur freigeschaltete Konten')
->assertSee('Angebot „Konflikte im Training: Privatlizenz“')
->mountAction('edit');
$data = $component->get('mountedActions.0.data');
$this->assertSame('ZERT2026', $data['code']);
$this->assertSame(15, (int) $data['percent']);
$component->fillForm(['percent' => 25, 'scope' => 'warenkorb'])->callMountedAction()->assertHasNoActionErrors();
$code->refresh();
$this->assertSame(25, $code->percent);
$this->assertSame('warenkorb', $code->scope);
$this->assertSame(0, $code->targets()->count());
}
public function test_a_code_can_be_deactivated_and_reactivated_with_confirmation(): void
{
$code = app(DiscountService::class)->save(['code' => 'SOMMER10', 'label' => 'Sommer', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'allgemein'], []);
$component = Livewire::actingAs($this->staff(), 'staff')
->test(ViewDiscountCode::class, ['record' => $code->getKey()])
->assertSee('Gerade gültig')
->mountAction('toggle')
->callMountedAction();
$this->assertFalse($code->fresh()->active);
$component->assertSee('Deaktiviert');
$component->mountAction('toggle')->callMountedAction();
$this->assertTrue($code->fresh()->active);
}
public function test_individual_accounts_are_released_and_revoked_for_exactly_this_code(): void
{
$service = app(DiscountService::class);
$x = $service->save(['code' => 'CODEX', 'label' => 'X', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'freigabe'], []);
$y = $service->save(['code' => 'CODEY', 'label' => 'Y', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'freigabe'], []);
$keeper = $this->staff();
$tina = $this->tina();
$component = Livewire::actingAs($keeper, 'staff')
->test(ViewDiscountCode::class, ['record' => $x->getKey()])
->assertSee('Noch kein Konto freigeschaltet')
->mountAction('release')
->fillForm(['user_id' => $tina->id, 'reason' => 'Zertifikat geprüft'])
->callMountedAction()
->assertHasNoActionErrors()
->assertSee('Tina Trainerin')
->assertSee('Zertifikat geprüft')
->assertSee('Entziehen');
$release = DiscountRelease::query()->firstOrFail();
$this->assertSame($x->id, $release->discount_code_id);
$this->assertSame($keeper->id, $release->granted_by);
$this->assertNotNull($service->activeRelease($x, $tina));
$this->assertNull($service->activeRelease($y, $tina), 'Eine Freigabe für Code X berechtigt nicht zu Code Y.');
$component->mountAction('revoke', ['release' => $release->id])->fillForm(['reason' => 'Zertifikat abgelaufen'])->callMountedAction();
$this->assertNotNull($release->fresh()->revoked_at);
$this->assertNull($service->activeRelease($x, $tina));
$component->assertSee('Entzogene Freigaben')->assertSee('Zertifikat abgelaufen');
}
public function test_an_open_code_offers_no_release_action(): void
{
$code = app(DiscountService::class)->save(['code' => 'OFFEN', 'label' => 'Offen', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'allgemein'], []);
Livewire::actingAs($this->staff(), 'staff')
->test(ViewDiscountCode::class, ['record' => $code->getKey()])
->assertSee('Keine Freigaben nötig')
->assertActionHidden('release');
}
public function test_the_list_shows_state_and_value_and_filters(): void
{
$service = app(DiscountService::class);
$service->save(['code' => 'GUELTIG', 'label' => 'Gültig', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'allgemein'], []);
$service->save(['code' => 'ALT', 'label' => 'Alt', 'kind' => 'betrag', 'amount_euro' => '5,00', 'scope' => 'warenkorb', 'eligibility' => 'allgemein', 'valid_until' => '2026-11-01'], []);
Livewire::actingAs($this->staff(), 'staff')
->test(ListDiscountCodes::class)
->assertSee('GUELTIG')->assertSee('Gültig')->assertSee('10 %')
->assertSee('ALT')->assertSee('Abgelaufen')->assertSee('5,00 €')
->set('zustand', 'aktiv')
->assertSee('GUELTIG')->assertDontSee('ALT');
}
public function test_only_catalog_keepers_and_customer_service_may_manage_codes_and_orders(): void
{
$code = app(DiscountService::class)->save(['code' => 'SOMMER10', 'label' => 'Sommer', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'allgemein'], []);
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
foreach (['katalogverwalter', 'kundenbetreuung'] as $role) {
$this->actingAs($this->staff($role), 'staff')->get('/verwaltung/discount-codes')->assertOk();
$this->get('/verwaltung/orders')->assertOk();
$this->get('/verwaltung/orders/'.$order->id)->assertOk();
$this->get('/verwaltung/discount-codes/'.$code->id)->assertOk();
}
$this->actingAs($this->staff('redakteur'), 'staff')->get('/verwaltung/discount-codes')->assertForbidden();
$this->get('/verwaltung/orders')->assertForbidden();
}
public function test_the_operator_confirms_the_payment_of_an_invoice_order(): void
{
$vera = $this->vera();
$order = $this->orderOf($vera, [$this->clubKonflikte()], $this->club()->id);
$order->update(['payment_method' => 'rechnung']);
$staff = $this->staff('kundenbetreuung');
Livewire::actingAs($staff, 'staff')
->test(ListOrders::class)
->assertSee('Bestellung '.$order->id)
->assertSee('Offen: wartet auf Zahlungseingang')
->set('zustand', 'rechnung_offen')
->assertSee('Bestellung '.$order->id);
Livewire::actingAs($staff, 'staff')
->test(ViewOrder::class, ['record' => $order->getKey()])
->assertSee('Testverein Musterstadt e. V.')
->assertSee('Mehrfachlizenz · bis zu 5 Trainer')
->assertSee('Offen: Rechnung erstellen und Zahlungseingang bestätigen.')
->assertActionHidden('refund')
->mountAction('confirmInvoice')
->fillForm(['reference' => ''])
->callMountedAction()
->assertHasActionErrors(['reference' => 'required']);
$this->assertSame('offen', $order->fresh()->status);
Livewire::actingAs($staff, 'staff')
->test(ViewOrder::class, ['record' => $order->getKey()])
->mountAction('confirmInvoice')
->fillForm(['reference' => 'RE-2026-014'])
->callMountedAction()
->assertHasNoActionErrors()
->assertSee('Bezahlt (Überweisung auf Rechnung (manuell))');
$this->assertSame('bezahlt', $order->fresh()->status);
$this->assertTrue(Grant::query()->usableBy($vera)->exists());
$this->assertFalse(Grant::query()->usableBy($this->max())->exists());
$this->assertSame('rechnung-RE-2026-014', $order->payments()->firstOrFail()->provider_reference);
}
public function test_the_operator_refunds_a_paid_order_completely_with_a_reason(): void
{
$tina = $this->tina();
$order = $this->orderOf($tina, [$this->privateKonflikte()]);
app(Checkout::class)->confirmTestPayment($order, 'test-r');
$order->forceFill(['withdrawal_requested_at' => now(), 'withdrawal_note' => 'Passt nicht'])->save();
$component = Livewire::actingAs($this->staff('kundenbetreuung'), 'staff')
->test(ViewOrder::class, ['record' => $order->getKey()])
->assertSee('Widerruf erklärt am')
->assertSee('Passt nicht')
->mountAction('refund')
->fillForm(['reason' => ''])
->callMountedAction()
->assertHasActionErrors(['reason' => 'required']);
$this->assertSame('bezahlt', $order->fresh()->status);
$component->fillForm(['reason' => 'Widerruf geprüft'])->callMountedAction()->assertHasNoActionErrors();
$this->assertSame('erstattet', $order->fresh()->status);
$this->assertSame(0, Grant::query()->where('status', GrantStatus::Aktiv)->count());
$this->assertSame('Widerruf geprüft', Grant::query()->firstOrFail()->status_reason);
}
public function test_the_offer_mask_asks_for_the_scope_and_stores_trainer_count_with_the_version(): void
{
$offer = $this->clubKonflikte();
$component = Livewire::actingAs($this->staff(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->assertSee('Mehrfachlizenz · bis zu 5 Trainer')
->mountAction('activate');
$data = $component->get('mountedActions.0.data');
$this->assertSame('mehrfach', $data['licence_scope']);
$this->assertSame(5, (int) $data['trainer_count']);
$component->fillForm(['trainer_count' => 12])->callMountedAction()->assertHasNoActionErrors();
$version = $offer->versions()->orderByDesc('number')->firstOrFail();
$this->assertSame('mehrfach', $version->licence_scope);
$this->assertSame(12, $version->trainer_count);
$this->assertSame(5, $offer->versions()->orderBy('number')->firstOrFail()->trainer_count, 'Die frühere Fassung bleibt unverändert.');
Livewire::actingAs($this->staff(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->mountAction('activate')
->fillForm(['licence_scope' => 'mehrfach', 'trainer_count' => 1])
->callMountedAction()
->assertHasActionErrors(['trainer_count']);
}
public function test_the_offer_page_flags_a_version_without_scope(): void
{
$offer = $this->privateGrenzen();
// Eine Fassung vor der Einführung des Nutzerkreises: aktive Fassungen sind sonst unveränderlich.
DB::table('offer_versions')->where('offer_id', $offer->id)->update(['licence_scope' => null, 'trainer_count' => null]);
Livewire::actingAs($this->staff(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->assertSee('Nutzerkreis nicht festgelegt')
->assertSee('Diese Fassung hat keinen festgelegten Nutzerkreis.')
->mountAction('activate')
->fillForm(['price_euro' => '31,00'])
->callMountedAction()
->assertHasActionErrors(['licence_scope' => 'required']);
$this->assertSame(1, $offer->versions()->count(), 'Ohne Nutzerkreis entsteht keine neue Fassung.');
}
public function test_the_sales_start_page_names_what_is_missing_without_showing_secrets(): void
{
config(['services.stripe.secret' => 'sk_test_geheim_nicht_zeigen', 'services.stripe.webhook_secret' => 'whsec_geheim']);
$this->actingAs($this->staff('kundenbetreuung'), 'staff')->get('/verwaltung/verkaufsstart')
->assertOk()
->assertSee('Allgemeine Geschäftsbedingungen')
->assertSee('Widerrufsbelehrung')
->assertSee('Fehlt')
->assertSee('Produktivfreigabe (SHOP_LIVE_RELEASE): nicht gesetzt')
->assertSee('Stripe-Schlüssel: Testmodus')
->assertSee('Steuerstatus und Belegangaben sind noch nicht entschieden')
->assertDontSee('sk_test_geheim_nicht_zeigen')
->assertDontSee('whsec_geheim');
$this->actingAs($this->staff('redakteur'), 'staff')->get('/verwaltung/verkaufsstart')->assertForbidden();
}
}