Am Lizenzartikel gibt es den Abschnitt "Im Shop verkaufen": Er listet die Optionen mit Preis, Rechten, Nutzerkreis und Zustand. "Option hinzufuegen" fragt nur Rechteprofil, Preis, Eignung, Nutzerkreis und eine Bezeichnung ab; Artikel, Inhalt und Vergabe stehen fest. Aendern und Aus dem Verkauf nehmen laufen am selben Ort. OfferService::offerArticle legt Angebot und erste Fassung in einer Transaktion an, damit eine fehlgeschlagene Pruefung kein leeres Angebot hinterlaesst. reviseArticleOption aktiviert die naechste Fassung und lehnt Optionen anderer Artikel ab. Aus der Angebotsmaske entfaellt die Auswahl "Programmfassung, die abgedeckt sein muss"; die Pruefung bleibt im Dienst. Pakete und der Altbestand behalten die Angebotsmaske.
344 lines
17 KiB
PHP
344 lines
17 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Domain\Catalog\ArticleService;
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Catalog\LicenseArticle;
|
|
use App\Domain\Catalog\Offer;
|
|
use App\Domain\Catalog\OfferService;
|
|
use App\Domain\Catalog\OfferStatus;
|
|
use App\Domain\Catalog\RightsProfileVersion;
|
|
use App\Domain\Commerce\Cart;
|
|
use App\Domain\Commerce\ProductOptions;
|
|
use App\Filament\Resources\LicenseArticles\Pages\ViewLicenseArticle;
|
|
use App\Filament\Resources\Offers\Pages\ViewOffer;
|
|
use App\Filament\Support\PublicationReadiness;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Livewire\Livewire;
|
|
use Tests\Support\BuildsShop;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Optionen am Lizenzartikel: Der Artikel wird direkt in seiner Maske verkauft (Rechteprofil, Preis, Nutzerkreis,
|
|
* Eignung), mehrere Optionen je Artikel sind möglich, und eine fehlgeschlagene Prüfung hinterlässt kein leeres Angebot.
|
|
*/
|
|
class ArticleOptionsTest extends TestCase
|
|
{
|
|
use BuildsShop;
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->seedShop();
|
|
}
|
|
|
|
private function keeper(): User
|
|
{
|
|
$keeper = User::factory()->katalogverwalter()->create();
|
|
$keeper->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
|
|
|
|
return $keeper;
|
|
}
|
|
|
|
/** Ein Artikel mit veröffentlichtem Stand, aber ohne Angebot. */
|
|
private function freshArticle(string $code = 'neu-optionen'): LicenseArticle
|
|
{
|
|
$article = app(ArticleService::class)->create($code);
|
|
app(ArticleService::class)->publishRelease($article, $this->article('shop-grenzen')->currentRelease->rootCompositionVersion, 'Erster Stand dieses Lizenzartikels.');
|
|
|
|
return $article->fresh();
|
|
}
|
|
|
|
private function privateRights(): RightsProfileVersion
|
|
{
|
|
return RightsProfileVersion::query()->whereHas('profile', fn ($query) => $query->where('name', 'Privatlizenz ehrenamtlich'))->firstOrFail();
|
|
}
|
|
|
|
private function clubRights(): RightsProfileVersion
|
|
{
|
|
return RightsProfileVersion::query()->whereHas('profile', fn ($query) => $query->where('name', 'Vereinslizenz institutionell'))->firstOrFail();
|
|
}
|
|
|
|
private function page(LicenseArticle $article)
|
|
{
|
|
return Livewire::actingAs($this->keeper(), 'staff')->test(ViewLicenseArticle::class, ['record' => $article->getKey()]);
|
|
}
|
|
|
|
public function test_the_dialog_asks_only_for_rights_price_scope_suitability_and_label(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
|
|
$component = $this->page($article)->mountAction('option');
|
|
$page = $component->instance();
|
|
$schema = (new \ReflectionMethod($page, 'getMountedActionSchema'))->invoke($page);
|
|
$labels = collect($schema->getFlatComponents())
|
|
->map(fn ($field): ?string => method_exists($field, 'getLabel') ? (string) $field->getLabel() : null)
|
|
->filter()
|
|
->values()
|
|
->all();
|
|
|
|
foreach (['Rechteprofil', 'Preis in Euro', 'Geeignet für', 'Nutzerkreis', 'Bezeichnung der Option'] as $label) {
|
|
$this->assertContains($label, $labels);
|
|
}
|
|
|
|
foreach (['Artikel', 'Ziel', 'Positionen', 'Paketfassung', 'Programmfassung, die abgedeckt sein muss', 'Vergabe'] as $label) {
|
|
$this->assertNotContains($label, $labels, 'Der Artikel steht fest, es gibt keine Auswahl von Artikel, Inhalt oder Vergabe.');
|
|
}
|
|
|
|
$data = $component->get('mountedActions.0.data');
|
|
$this->assertSame('person', $data['holder_kind']);
|
|
$this->assertSame('einzel', $data['licence_scope']);
|
|
}
|
|
|
|
public function test_an_article_gets_two_options_that_share_its_product_page_and_are_alternatives_in_the_cart(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
|
|
$this->page($article)
|
|
->mountAction('option')
|
|
->fillForm(['rights_id' => $this->privateRights()->id, 'price_euro' => '39,00', 'holder_kind' => 'person', 'licence_scope' => 'einzel', 'option_label' => ''])
|
|
->callMountedAction()
|
|
->assertHasNoActionErrors();
|
|
|
|
$this->page($article)
|
|
->mountAction('option')
|
|
->fillForm(['rights_id' => $this->clubRights()->id, 'price_euro' => '149,00', 'holder_kind' => 'organisation', 'licence_scope' => 'mehrfach', 'trainer_count' => 8, 'option_label' => 'Vereinslizenz'])
|
|
->callMountedAction()
|
|
->assertHasNoActionErrors();
|
|
|
|
$offers = app(ProductOptions::class)->offersOfArticle($article);
|
|
$this->assertCount(2, $offers);
|
|
[$private, $club] = $offers;
|
|
$this->assertSame('Einzellizenz für Privatpersonen', $private->optionName(), 'Ohne Eingabe gilt der Vorschlag aus Nutzerkreis und Eignung.');
|
|
$this->assertSame('Vereinslizenz', $club->optionName());
|
|
$this->assertStringStartsWith(app(ProductOptions::class)->articleTitle($article).': ', $private->name);
|
|
|
|
$privateVersion = $private->versions()->firstOrFail();
|
|
$this->assertSame(OfferStatus::Aktiv, $privateVersion->status);
|
|
$this->assertSame(3900, $privateVersion->price_cents);
|
|
$this->assertSame('kauf', $privateVersion->grant_kind);
|
|
$this->assertSame('einzel', $privateVersion->licence_scope);
|
|
$clubVersion = $club->versions()->firstOrFail();
|
|
$this->assertSame(14900, $clubVersion->price_cents);
|
|
$this->assertSame('organisation', $clubVersion->holder_kind);
|
|
$this->assertSame(8, $clubVersion->trainer_count);
|
|
|
|
$this->get('/shop/'.$article->id)
|
|
->assertOk()
|
|
->assertSee('Einzellizenz für Privatpersonen')
|
|
->assertSee('39,00')
|
|
->assertSee('Vereinslizenz')
|
|
->assertSee('149,00');
|
|
$this->get('/shop')->assertOk()->assertSee('/shop/'.$article->id, false);
|
|
|
|
$cart = app(Cart::class);
|
|
$this->assertSame(Cart::ADDED, $cart->add($private));
|
|
$this->assertSame(Cart::ALTERNATIVE, $cart->add($club), 'Zwei Optionen desselben Artikels sind Alternativen im Warenkorb.');
|
|
$this->assertSame(1, $cart->count());
|
|
}
|
|
|
|
public function test_the_article_page_lists_its_options_with_price_rights_and_state(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
$this->page($article)->assertSee('Im Shop verkaufen')->assertSee('Noch keine Option')->assertSee('Option hinzufügen');
|
|
|
|
app(OfferService::class)->offerArticle($article, $this->privateRights(), ['holder_kind' => 'person', 'licence_scope' => 'einzel', 'trainer_count' => 1, 'price_cents' => 3900], 'Neu: Privat', 'Privatlizenz');
|
|
|
|
$this->page($article)
|
|
->assertSee('Privatlizenz')
|
|
->assertSee('39,00 €')
|
|
->assertSee('Einzellizenz · 1 Trainer')
|
|
->assertSee('Rechte: Privatlizenz ehrenamtlich')
|
|
->assertSee('Im Shop')
|
|
->assertSee('Produktseite ansehen')
|
|
->assertSee('Aus dem Verkauf nehmen')
|
|
->assertDontSee('Noch keine Option');
|
|
}
|
|
|
|
public function test_an_article_without_stand_cannot_be_offered_and_the_page_says_so(): void
|
|
{
|
|
$article = app(ArticleService::class)->create('neu-ohne-stand-optionen');
|
|
|
|
$this->page($article)
|
|
->assertSee('Zuerst den Inhalt festlegen')
|
|
->assertDontSee('Noch keine Option');
|
|
|
|
$before = Offer::query()->count();
|
|
$this->page($article)
|
|
->mountAction('option')
|
|
->fillForm(['rights_id' => $this->privateRights()->id, 'price_euro' => '39,00', 'holder_kind' => 'person', 'licence_scope' => 'einzel'])
|
|
->callMountedAction();
|
|
|
|
$this->assertSame($before, Offer::query()->count(), 'Ein Artikel ohne Stand hinterlässt kein Angebot.');
|
|
}
|
|
|
|
public function test_a_failed_check_leaves_no_empty_offer_behind(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
$before = Offer::query()->count();
|
|
|
|
foreach (['0,00', '-3', 'abc'] as $price) {
|
|
$this->page($article)
|
|
->mountAction('option')
|
|
->fillForm(['rights_id' => $this->privateRights()->id, 'price_euro' => $price, 'holder_kind' => 'person', 'licence_scope' => 'einzel'])
|
|
->callMountedAction();
|
|
}
|
|
|
|
$this->assertSame($before, Offer::query()->count(), 'Ein Kauf ohne Preis über 0,00 € legt kein Angebot an.');
|
|
|
|
$service = app(OfferService::class);
|
|
|
|
try {
|
|
$service->offerArticle($article, $this->privateRights(), ['holder_kind' => 'person', 'licence_scope' => 'mehrfach', 'trainer_count' => 1, 'price_cents' => 3900], 'Neu: Fehler');
|
|
$this->fail('Eine Mehrfachlizenz mit einem Trainer wird abgelehnt.');
|
|
} catch (DomainRuleException) {
|
|
$this->assertSame($before, Offer::query()->count());
|
|
}
|
|
|
|
$withoutStand = app(ArticleService::class)->create('neu-ohne-stand-dienst');
|
|
|
|
try {
|
|
$service->offerArticle($withoutStand, $this->privateRights(), ['holder_kind' => 'person', 'licence_scope' => 'einzel', 'trainer_count' => 1, 'price_cents' => 3900], 'Neu: Ohne Stand');
|
|
$this->fail('Ein Artikel ohne Stand lässt sich nicht verkaufen.');
|
|
} catch (DomainRuleException) {
|
|
$this->assertSame($before, Offer::query()->count());
|
|
}
|
|
}
|
|
|
|
public function test_changing_an_option_activates_the_next_version_and_keeps_the_others(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
$service = app(OfferService::class);
|
|
$service->offerArticle($article, $this->privateRights(), ['holder_kind' => 'person', 'licence_scope' => 'einzel', 'trainer_count' => 1, 'price_cents' => 3900], 'Neu: Privat', 'Privatlizenz');
|
|
$service->offerArticle($article, $this->clubRights(), ['holder_kind' => 'organisation', 'licence_scope' => 'mehrfach', 'trainer_count' => 5, 'price_cents' => 14900], 'Neu: Verein', 'Vereinslizenz');
|
|
[$private, $club] = app(ProductOptions::class)->offersOfArticle($article);
|
|
|
|
$component = $this->page($article)->mountAction('option', ['offer' => $private->id]);
|
|
$data = $component->get('mountedActions.0.data');
|
|
$this->assertSame('39,00', $data['price_euro'], 'Die Maske ist mit der letzten Fassung vorbelegt, der Preis steht in Euro.');
|
|
$this->assertSame('Privatlizenz', $data['option_label']);
|
|
$this->assertSame($this->privateRights()->id, (int) $data['rights_id']);
|
|
|
|
$component->fillForm(['price_euro' => '29,00'])->callMountedAction()->assertHasNoActionErrors();
|
|
|
|
$versions = $private->versions()->orderBy('number')->get();
|
|
$this->assertCount(2, $versions);
|
|
$this->assertSame(OfferStatus::Beendet, $versions[0]->status);
|
|
$this->assertSame(3900, $versions[0]->price_cents, 'Die frühere Fassung bleibt unverändert gespeichert.');
|
|
$this->assertSame(OfferStatus::Aktiv, $versions[1]->status);
|
|
$this->assertSame(2900, $versions[1]->price_cents);
|
|
$this->assertSame(1, $club->versions()->count(), 'Die andere Option bleibt unberührt.');
|
|
$this->assertSame(14900, $club->versions()->firstOrFail()->price_cents);
|
|
}
|
|
|
|
public function test_cancelling_the_dialog_changes_nothing(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
app(OfferService::class)->offerArticle($article, $this->privateRights(), ['holder_kind' => 'person', 'licence_scope' => 'einzel', 'trainer_count' => 1, 'price_cents' => 3900], 'Neu: Privat', 'Privatlizenz');
|
|
[$offer] = app(ProductOptions::class)->offersOfArticle($article);
|
|
|
|
$this->page($article)
|
|
->mountAction('option', ['offer' => $offer->id])
|
|
->fillForm(['price_euro' => '1,00', 'option_label' => 'Geändert'])
|
|
->unmountAction();
|
|
|
|
$this->assertSame(1, $offer->versions()->count());
|
|
$this->assertSame(3900, $offer->versions()->firstOrFail()->price_cents);
|
|
$this->assertSame('Privatlizenz', $offer->fresh()->option_label);
|
|
}
|
|
|
|
public function test_ending_one_option_leaves_the_other_purchasable_and_it_can_be_offered_again(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
$service = app(OfferService::class);
|
|
$service->offerArticle($article, $this->privateRights(), ['holder_kind' => 'person', 'licence_scope' => 'einzel', 'trainer_count' => 1, 'price_cents' => 3900], 'Neu: Privat', 'Privatlizenz');
|
|
$service->offerArticle($article, $this->clubRights(), ['holder_kind' => 'organisation', 'licence_scope' => 'mehrfach', 'trainer_count' => 5, 'price_cents' => 14900], 'Neu: Verein', 'Vereinslizenz');
|
|
[$private, $club] = app(ProductOptions::class)->offersOfArticle($article);
|
|
|
|
$this->page($article)->mountAction('optionEnd', ['offer' => $private->id])->callMountedAction();
|
|
|
|
$this->assertNull($service->purchasableVersion($private->fresh()));
|
|
$this->assertNotNull($service->purchasableVersion($club->fresh()));
|
|
$this->get('/shop/'.$article->id)->assertOk()->assertDontSee('Privatlizenz')->assertSee('Vereinslizenz');
|
|
|
|
$this->page($article)
|
|
->assertSee('Nicht im Verkauf')
|
|
->assertSee('Wieder anbieten')
|
|
->mountAction('option', ['offer' => $private->id])
|
|
->callMountedAction()
|
|
->assertHasNoActionErrors();
|
|
|
|
$this->assertNotNull($service->purchasableVersion($private->fresh()));
|
|
$this->assertSame(2, $private->versions()->count());
|
|
|
|
$this->page($article)->mountAction('optionEnd', ['offer' => $private->id])->callMountedAction();
|
|
$this->page($article)->mountAction('optionEnd', ['offer' => $club->id])->callMountedAction();
|
|
$this->get('/shop/'.$article->id)->assertNotFound();
|
|
}
|
|
|
|
public function test_an_option_of_another_article_cannot_be_opened_or_changed(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
$foreign = $this->privateKonflikte();
|
|
|
|
$this->page($article)->call('mountAction', 'option', ['offer' => $foreign->id])->assertStatus(404);
|
|
$this->page($article)->call('mountAction', 'optionEnd', ['offer' => $foreign->id])->assertStatus(404);
|
|
$this->assertSame(1, $foreign->versions()->count());
|
|
$this->assertNotNull(app(OfferService::class)->purchasableVersion($foreign->fresh()), 'Eine fremde Option lässt sich weder öffnen noch beenden.');
|
|
|
|
try {
|
|
app(OfferService::class)->reviseArticleOption($foreign, $article, $this->privateRights(), ['holder_kind' => 'person', 'licence_scope' => 'einzel', 'trainer_count' => 1, 'price_cents' => 100]);
|
|
$this->fail('Der Dienst lehnt eine Option eines anderen Artikels ab.');
|
|
} catch (DomainRuleException $exception) {
|
|
$this->assertStringContainsString('gehört nicht zu diesem Lizenzartikel', $exception->getMessage());
|
|
$this->assertSame(1, $foreign->versions()->count());
|
|
}
|
|
}
|
|
|
|
public function test_the_dialog_neither_saves_nor_resets_open_editorial_input(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
$storedTitle = $article->display_title;
|
|
|
|
$component = $this->page($article)->set('displayTitle', 'Noch nicht gespeichert');
|
|
$component
|
|
->mountAction('option')
|
|
->fillForm(['rights_id' => $this->privateRights()->id, 'price_euro' => '39,00', 'holder_kind' => 'person', 'licence_scope' => 'einzel'])
|
|
->callMountedAction()
|
|
->assertHasNoActionErrors();
|
|
|
|
$component->assertSet('displayTitle', 'Noch nicht gespeichert');
|
|
$this->assertSame($storedTitle, $article->fresh()->display_title, 'Die Option speichert die Redaktion nicht mit.');
|
|
$this->assertCount(1, app(ProductOptions::class)->offersOfArticle($article));
|
|
}
|
|
|
|
public function test_the_next_step_on_the_article_opens_the_option_dialog(): void
|
|
{
|
|
$article = $this->freshArticle();
|
|
$steps = collect(app(PublicationReadiness::class)->forArticle($article)['schritte']);
|
|
$step = $steps->firstWhere('titel', 'Im Shop verkaufen');
|
|
|
|
$this->assertNotNull($step);
|
|
$this->assertSame("mountAction('option')", $step['aktion']['wire']);
|
|
}
|
|
|
|
public function test_the_offer_dialog_no_longer_asks_for_a_program_version_to_cover(): void
|
|
{
|
|
$component = Livewire::actingAs($this->keeper(), 'staff')
|
|
->test(ViewOffer::class, ['record' => $this->privateKonflikte()->getKey()])
|
|
->mountAction('activate');
|
|
$page = $component->instance();
|
|
$schema = (new \ReflectionMethod($page, 'getMountedActionSchema'))->invoke($page);
|
|
$labels = collect($schema->getFlatComponents())
|
|
->map(fn ($field): ?string => method_exists($field, 'getLabel') ? (string) $field->getLabel() : null)
|
|
->filter()
|
|
->all();
|
|
|
|
$this->assertNotContains('Programmfassung, die abgedeckt sein muss', $labels);
|
|
$this->assertContains('Positionen', $labels, 'Pakete und der Altbestand behalten die Positionen.');
|
|
}
|
|
}
|