Files
laraundfelix/tests/Feature/OfferOptionAdminTest.php
T
Lars 4e0760db1e
Deploy Test / test (push) Successful in 1m38s
Deploy Test / deploy (push) Successful in 31s
Add feature tests for shop functionality and organization purchases
- Implement OfferOptionAdminTest to validate offer option management, including price display, activation, and cancellation.
- Create OrganizationPurchaseTest to ensure correct handling of organization purchases, including access control and order processing.
- Add ShopProductPageTest to verify shop product listings, search functionality, and product page details.
- Introduce BuildsShop trait for seeding test data related to shop functionality.
- Implement MoneyTest to validate money parsing and formatting, ensuring correct handling of euro amounts.
2026-11-21 12:06:24 +01:00

231 lines
10 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\OfferStatus;
use App\Domain\Commerce\Cart;
use App\Domain\Commerce\CartLine;
use App\Filament\Resources\Offers\Pages\ListOffers;
use App\Filament\Resources\Offers\Pages\ViewOffer;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\Support\BuildsShop;
use Tests\TestCase;
/**
* Etappe 1, Shop-Abnahme: „Lizenzoption im bestehenden Arbeitsplatz bearbeiten“. Die Maske ist vorbelegt, der Preis
* erscheint in Euro, Abbrechen verwirft die Eingaben, erst die Aktivierung wirkt, und andere Optionen bleiben kaufbar.
*/
class OfferOptionAdminTest extends TestCase
{
use BuildsShop;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seedShop();
}
public function test_the_option_page_shows_price_in_euro_suitability_rights_content_and_status(): void
{
$offer = $this->privateKonflikte();
Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->assertSee('Die Option auf einen Blick')
->assertSee('Preis: 49,00 €')
->assertSee('Geeignet für: Privatpersonen')
->assertSee('Ehrenamtlich: Film ansehen, Film herunterladen, PDF ansehen, PDF herunterladen')
->assertSee('Zugrunde liegender Inhalt: Konflikte im Training')
->assertSee('Status: Öffentlich kaufbar')
->assertSee('Produktseite: Konflikte im Training')
->assertSee('Geänderte Fassung aktivieren')
->assertSee('Aus dem Verkauf nehmen');
Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $this->clubKonflikte()->getKey()])
->assertSee('Preis: 149,00 €')
->assertSee('Geeignet für: Organisationen und Vereine')
->assertSee('Institutionell:');
}
public function test_the_list_shows_price_and_suitability_per_option(): void
{
Livewire::actingAs($this->keeper(), 'staff')
->test(ListOffers::class)
->assertSee('Kauf · 49,00 €')
->assertSee('Für Privatpersonen')
->assertSee('Kauf · 149,00 €')
->assertSee('Für Organisationen');
}
public function test_the_mask_is_prefilled_names_the_option_and_states_the_effect(): void
{
$offer = $this->privateKonflikte();
$component = Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->mountAction('activate');
$action = $component->instance()->getMountedAction();
$data = $component->get('mountedActions.0.data');
$this->assertSame('Option „Privatlizenz“ ändern: Fassung 2 aktivieren', $action->getModalHeading());
$description = (string) $action->getModalDescription();
$this->assertStringContainsString('Betroffen ist nur die Option „Privatlizenz“ der Produktseite „Konflikte im Training“', $description);
$this->assertStringContainsString('Andere Optionen derselben Produktseite bleiben unverändert kaufbar', $description);
$this->assertStringContainsString('„Abbrechen“ verwirft die Eingaben', $description);
$this->assertSame('Fassung 2 aktivieren', $action->getModalSubmitActionLabel());
$this->assertSame('Privatlizenz', $data['option_label']);
$this->assertSame((string) $offer->composition_id, (string) $data['composition_id']);
$this->assertSame('person', $data['holder_kind']);
$this->assertSame('kauf', $data['grant_kind']);
$this->assertSame('49,00', $data['price_euro'], 'Der Preis steht in Euro, nicht in Cent.');
$this->assertCount(1, $data['lines']);
}
public function test_cancelling_discards_the_open_input_and_creates_no_version(): void
{
$offer = $this->privateKonflikte();
Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->mountAction('activate')
->fillForm(['price_euro' => '1,00', 'option_label' => 'Geändert'])
->unmountAction();
$this->assertSame(1, $offer->versions()->count());
$this->assertSame(4900, $offer->versions()->first()->price_cents);
$this->assertSame('Privatlizenz', $offer->fresh()->option_label);
}
public function test_activating_changes_only_this_option_and_leaves_the_others_purchasable(): void
{
$offer = $this->privateKonflikte();
$club = $this->clubKonflikte();
$other = $this->privateGrenzen();
Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->mountAction('activate')
->fillForm(['price_euro' => '39,00', 'option_label' => 'Privatlizenz (neu)'])
->callMountedAction()
->assertHasNoFormErrors();
$versions = $offer->versions()->orderBy('number')->get();
$this->assertCount(2, $versions);
$this->assertSame(OfferStatus::Beendet, $versions[0]->status);
$this->assertSame(4900, $versions[0]->price_cents, 'Die frühere Fassung bleibt unverändert gespeichert.');
$this->assertSame(OfferStatus::Aktiv, $versions[1]->status);
$this->assertSame(3900, $versions[1]->price_cents);
$this->assertSame('Privatlizenz (neu)', $offer->fresh()->option_label);
foreach ([$club, $other] as $untouched) {
$this->assertSame(1, $untouched->versions()->count());
$this->assertNotNull(app(Cart::class)->purchasable($untouched));
}
$this->get(route('public.program', $offer->composition_id))
->assertSee('39,00 €')
->assertSee('Privatlizenz (neu)')
->assertSee('149,00 €');
}
public function test_an_open_cart_shows_the_activated_price_change(): void
{
$offer = $this->privateKonflikte();
$cart = app(Cart::class);
$cart->add($offer);
Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->mountAction('activate')
->fillForm(['price_euro' => '39,00'])
->callMountedAction();
$line = $cart->review($this->tina())->lines[0];
$this->assertSame(CartLine::PRICE_CHANGED, $line->state);
$this->assertSame([4900, 3900], [$line->seenPriceCents, $line->currentPriceCents]);
}
public function test_the_price_is_entered_in_euro_and_an_invalid_amount_changes_nothing(): void
{
$offer = $this->privateKonflikte();
$component = Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->mountAction('activate')
->fillForm(['price_euro' => 'zwölf Euro'])
->callMountedAction();
$this->assertSame(1, $offer->versions()->count());
$component->mountAction('activate')->fillForm(['price_euro' => '1.299,5'])->callMountedAction();
$this->assertSame(2, $offer->versions()->count());
$this->assertSame(129950, $offer->versions()->orderByDesc('number')->first()->price_cents);
}
public function test_a_purchase_needs_a_price_above_zero_and_below_the_limit(): void
{
$offer = $this->privateKonflikte();
$component = Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()]);
foreach (['0', '0,00', '', '1.000.001', '99999999999'] as $price) {
$component->mountAction('activate')->fillForm(['price_euro' => $price])->callMountedAction();
}
$this->assertSame(1, $offer->versions()->count());
}
public function test_the_holder_kind_is_chosen_in_the_mask_and_kept_by_a_change(): void
{
$club = $this->clubKonflikte();
$component = Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $club->getKey()])
->mountAction('activate');
$this->assertSame('organisation', $component->get('mountedActions.0.data.holder_kind'));
$this->assertSame('149,00', $component->get('mountedActions.0.data.price_euro'));
$component->fillForm(['price_euro' => '159,00'])->callMountedAction();
$this->assertSame('organisation', $club->versions()->orderByDesc('number')->first()->holder_kind);
}
public function test_withdrawing_an_option_ends_only_that_option_and_blocks_open_carts(): void
{
$offer = $this->privateKonflikte();
$club = $this->clubKonflikte();
$cart = app(Cart::class);
$cart->add($offer);
$component = Livewire::actingAs($this->keeper(), 'staff')
->test(ViewOffer::class, ['record' => $offer->getKey()])
->mountAction('end');
$description = (string) $component->instance()->getMountedAction()->getModalDescription();
$this->assertStringContainsString('Betroffen ist nur die Option „Privatlizenz“', $description);
$this->assertStringContainsString('blockiert den Abschluss', $description);
$component->callMountedAction();
$this->assertNull($offer->versions()->where('status', OfferStatus::Aktiv)->first());
$this->assertSame(OfferStatus::Beendet, $offer->versions()->first()->status);
$this->assertNotNull(app(Cart::class)->purchasable($club));
$this->assertSame(CartLine::WITHDRAWN, $cart->review($this->tina())->lines[0]->state);
}
public function test_a_redaktion_account_without_catalog_role_has_no_access_to_the_option(): void
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
$this->actingAs($editor, 'staff')->get('/verwaltung/offers/'.$this->privateKonflikte()->getKey())->assertForbidden();
}
private function keeper(): User
{
$keeper = User::factory()->katalogverwalter()->create();
$keeper->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $keeper;
}
}