mivita/tests/Feature/WizardPaymentCartTest.php
2026-04-14 18:07:45 +02:00

169 lines
5 KiB
PHP

<?php
/**
* Wizard-Payment: Da Radio Buttons nur eine Produktauswahl erlauben,
* darf nach dem Absenden des Formulars immer nur exakt ein Produkt
* im Warenkorb liegen — auch wenn der Nutzer per Browser-Zurück-Button
* mehrfach einreicht.
*/
use App\Models\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
beforeEach(function () {
Schema::connection('sqlite')->table('products', function ($table) {
if (! Schema::connection('sqlite')->hasColumn('products', 'slug')) {
$table->string('slug')->nullable();
}
if (! Schema::connection('sqlite')->hasColumn('products', 'free_shipping_consultant')) {
$table->boolean('free_shipping_consultant')->default(false)->nullable();
}
});
});
function makeWizardProduct(string $name, float $price = 69.90, int $pos = 1): Product
{
return Product::forceCreate([
'name' => $name,
'title' => $name,
'price' => $price,
'tax' => 19.00,
'active' => true,
'is_membership_only' => false,
'pos' => $pos,
'show_at' => 3,
'show_on' => '["7","8"]',
'identifier' => 'show_order',
'action' => '["0","1"]',
'no_commission' => false,
'no_free_shipping' => false,
'free_shipping_consultant' => false,
'weight' => 0,
'points' => 0,
'slug' => \Illuminate\Support\Str::slug($name).'-'.uniqid(),
]);
}
it('entfernt vorhandene Produkte aus dem Wizard-Warenkorb wenn ein neues ausgewaehlt wird', function () {
$productA = makeWizardProduct('Starter-Paket A', 49.90, 1);
$productB = makeWizardProduct('Starter-Paket B', 99.90, 2);
$instance = 'shopping';
\Yard::instance($instance)->destroy();
// Produkt A ist bereits im Warenkorb (simuliert den Zustand nach erstem Absenden)
\Yard::instance($instance)->add(
$productA->id,
$productA->name,
1,
$productA->price,
false,
false,
[
'slug' => $productA->slug,
'weight' => 0,
'points' => 0,
'no_commission' => false,
'no_free_shipping' => false,
'free_shipping_consultant' => false,
'show_on' => $productA->show_on,
]
);
expect(\Yard::instance($instance)->count())->toBe(1);
// Nutzer geht zurück und wählt Produkt B — die neue Logik soll Produkt A entfernen
$cartItemB = \Yard::instance($instance)->add(
$productB->id,
$productB->name,
1,
$productB->price,
false,
false,
[
'slug' => $productB->slug,
'weight' => 0,
'points' => 0,
'no_commission' => false,
'no_free_shipping' => false,
'free_shipping_consultant' => false,
'show_on' => $productB->show_on,
]
);
// Schutzlogik aus WizardController::storePayment()
foreach (\Yard::instance($instance)->content() as $existingItem) {
if ($existingItem->rowId !== $cartItemB->rowId) {
\Yard::instance($instance)->remove($existingItem->rowId);
}
}
expect(\Yard::instance($instance)->count())->toBe(1);
expect(\Yard::instance($instance)->content()->first()->id)->toBe($productB->id);
\Yard::instance($instance)->destroy();
});
it('behaelt das Produkt bei qty=1 wenn dasselbe Produkt erneut abgesendet wird', function () {
$product = makeWizardProduct('Starter-Paket A', 49.90, 1);
$instance = 'shopping';
\Yard::instance($instance)->destroy();
\Yard::instance($instance)->add(
$product->id,
$product->name,
1,
$product->price,
false,
false,
[
'slug' => $product->slug,
'weight' => 0,
'points' => 0,
'no_commission' => false,
'no_free_shipping' => false,
'free_shipping_consultant' => false,
'show_on' => $product->show_on,
]
);
// Zweites Absenden desselben Produkts
$cartItemAgain = \Yard::instance($instance)->add(
$product->id,
$product->name,
1,
$product->price,
false,
false,
[
'slug' => $product->slug,
'weight' => 0,
'points' => 0,
'no_commission' => false,
'no_free_shipping' => false,
'free_shipping_consultant' => false,
'show_on' => $product->show_on,
]
);
if ($cartItemAgain->qty > 1) {
\Yard::instance($instance)->update($cartItemAgain->rowId, 1);
}
foreach (\Yard::instance($instance)->content() as $existingItem) {
if ($existingItem->rowId !== $cartItemAgain->rowId) {
\Yard::instance($instance)->remove($existingItem->rowId);
}
}
expect(\Yard::instance($instance)->count())->toBe(1);
expect(\Yard::instance($instance)->get($cartItemAgain->rowId)->qty)->toBe(1);
\Yard::instance($instance)->destroy();
});