2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
namespace Tests\Feature\App\Http\Controllers\Bo;
|
|
|
|
|
|
2026-08-26 11:45:57 +02:00
|
|
|
use App\Models\Customer;
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Models\Discount;
|
2026-09-30 12:43:55 +02:00
|
|
|
use App\Models\Product;
|
|
|
|
|
use App\Models\Range;
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Models\User;
|
|
|
|
|
use Domain\Enums\DiscountType;
|
2026-09-30 12:43:55 +02:00
|
|
|
use Domain\Enums\ProductBrand;
|
|
|
|
|
use Domain\Enums\SellUnit;
|
|
|
|
|
use Domain\Enums\TaxRate;
|
|
|
|
|
use Domain\Enums\Units\InvoicingUnit;
|
|
|
|
|
use Domain\Enums\Units\OrderUnit;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Domain\Enums\UserRole;
|
|
|
|
|
use Domain\Models\ValueObjects\DiscountValue;
|
2026-11-16 15:23:40 +01:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2026-09-30 12:43:55 +02:00
|
|
|
use Money\Money;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-16 15:23:40 +01:00
|
|
|
use function Pest\Laravel\from;
|
|
|
|
|
use function Pest\Laravel\get;
|
|
|
|
|
use function Pest\Laravel\post;
|
|
|
|
|
use function Pest\Laravel\postJson;
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
describe('DiscountsController', function (): void {
|
|
|
|
|
beforeEach(function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
2026-06-14 21:32:07 +02:00
|
|
|
$this->user = User::factory()->create(['role' => UserRole::SuperAdmin]);
|
|
|
|
|
$this->actingAs($this->user, 'backend');
|
2026-09-30 12:43:55 +02:00
|
|
|
|
|
|
|
|
$this->range = new Range();
|
|
|
|
|
$this->range->name = 'Gamme ' . uniqid();
|
|
|
|
|
$this->range->status = true;
|
|
|
|
|
$this->range->order = random_int(1, 9999);
|
|
|
|
|
$this->range->picture_alt = 'alt';
|
|
|
|
|
$this->range->picture_title = 'title';
|
|
|
|
|
$this->range->save();
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
$createProduct = function (string $name, Range $range): Product {
|
|
|
|
|
$product = new Product([
|
|
|
|
|
'range_id' => $range->id,
|
|
|
|
|
'name' => $name . ' ' . uniqid(),
|
|
|
|
|
'brand' => ProductBrand::loue,
|
|
|
|
|
'invoicing_unit' => InvoicingUnit::PCE,
|
|
|
|
|
'sell_unit' => SellUnit::PCE,
|
|
|
|
|
'order_unit' => OrderUnit::PCE,
|
|
|
|
|
'tax_rate' => TaxRate::TVA_20,
|
|
|
|
|
'description' => 'Description test',
|
|
|
|
|
'status' => true,
|
|
|
|
|
]);
|
|
|
|
|
$product->order = random_int(1, 999999);
|
|
|
|
|
$product->price = Money::EUR(1000);
|
|
|
|
|
$product->short_description = 'Court';
|
|
|
|
|
$product->picture_alt = 'alt';
|
|
|
|
|
$product->picture_title = 'title';
|
|
|
|
|
$product->picture_large_alt = 'alt';
|
|
|
|
|
$product->picture_large_title = 'title';
|
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
|
|
return $product;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it('can access index', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
2026-10-29 16:09:41 +01:00
|
|
|
$discount = new Discount();
|
|
|
|
|
$discount->type = DiscountType::PERCENT;
|
|
|
|
|
$discount->value = DiscountValue::fromPercentage(20);
|
|
|
|
|
$discount->is_old_price_displayed = true;
|
|
|
|
|
$discount->save();
|
2026-09-30 12:43:55 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$response = $this->get('/bo/discounts');
|
2026-09-30 12:43:55 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-06-14 21:32:07 +02:00
|
|
|
expect($response->status())->toBe(200);
|
|
|
|
|
$response->assertViewIs('bo.discounts.index');
|
2026-10-29 16:09:41 +01:00
|
|
|
$response->assertSee("discount={$discount->id}");
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
it('can access create', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
|
|
|
|
|
// WHEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$response = $this->get('/bo/discounts/create');
|
2026-09-30 12:43:55 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-06-14 21:32:07 +02:00
|
|
|
expect($response->status())->toBe(200);
|
|
|
|
|
$response->assertViewIs('bo.discounts.create');
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
it('can store a discount with associated products', function () use ($createProduct): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
$product1 = $createProduct('Produit 1', $this->range);
|
|
|
|
|
$product2 = $createProduct('Produit 2', $this->range);
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
$data = [
|
|
|
|
|
'type' => DiscountType::PERCENT->value,
|
|
|
|
|
'value' => 15,
|
|
|
|
|
'is_old_price_displayed' => true,
|
|
|
|
|
'date_start' => null,
|
|
|
|
|
'date_end' => null,
|
|
|
|
|
'client_timezone' => 'UTC',
|
2026-09-30 12:43:55 +02:00
|
|
|
'associated_products' => [(string) $product1->id, (string) $product2->id],
|
2026-06-14 21:32:07 +02:00
|
|
|
];
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
// WHEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$response = $this->post('/bo/discounts', $data);
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
// THEN
|
|
|
|
|
$discount = Discount::query()->first();
|
2026-06-14 21:32:07 +02:00
|
|
|
expect($discount)->not->toBeNull()
|
2026-09-30 12:43:55 +02:00
|
|
|
->and($discount?->type)->toBe(DiscountType::PERCENT)
|
|
|
|
|
->and($discount?->value->getPercentage())->toBe(15);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
$freshProduct1 = Product::query()->find($product1->id);
|
|
|
|
|
$freshProduct2 = Product::query()->find($product2->id);
|
|
|
|
|
expect($freshProduct1?->discount_id)->toBe($discount?->id)
|
|
|
|
|
->and($freshProduct2?->discount_id)->toBe($discount?->id);
|
|
|
|
|
|
|
|
|
|
$response->assertRedirect("/bo/discounts/{$discount?->id}/edit");
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
it('can access edit', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$discount = new Discount();
|
|
|
|
|
$discount->type = DiscountType::PERCENT;
|
|
|
|
|
$discount->value = DiscountValue::fromPercentage(10);
|
|
|
|
|
$discount->is_old_price_displayed = true;
|
|
|
|
|
$discount->save();
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
// WHEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$response = $this->get("/bo/discounts/{$discount->id}/edit");
|
2026-09-30 12:43:55 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-06-14 21:32:07 +02:00
|
|
|
expect($response->status())->toBe(200);
|
|
|
|
|
$response->assertViewIs('bo.discounts.edit');
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
it('can update a discount and sync products', function () use ($createProduct): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
$product1 = $createProduct('P1', $this->range);
|
|
|
|
|
$product2 = $createProduct('P2', $this->range);
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
$discount = new Discount();
|
|
|
|
|
$discount->type = DiscountType::PERCENT;
|
|
|
|
|
$discount->value = DiscountValue::fromPercentage(10);
|
|
|
|
|
$discount->is_old_price_displayed = true;
|
|
|
|
|
$discount->save();
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
Product::query()->where('id', $product1->id)->update(['discount_id' => $discount->id]);
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
$data = [
|
|
|
|
|
'type' => DiscountType::PERCENT->value,
|
|
|
|
|
'value' => 20,
|
|
|
|
|
'is_old_price_displayed' => false,
|
|
|
|
|
'date_start' => null,
|
|
|
|
|
'date_end' => null,
|
|
|
|
|
'client_timezone' => 'UTC',
|
2026-09-30 12:43:55 +02:00
|
|
|
'associated_products' => [(string) $product2->id],
|
2026-06-14 21:32:07 +02:00
|
|
|
];
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
// WHEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$response = $this->put("/bo/discounts/{$discount->id}", $data);
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
// THEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$discount->refresh();
|
|
|
|
|
expect($discount->is_old_price_displayed)->toBeFalse()
|
|
|
|
|
->and($discount->value->getPercentage())->toBe(20);
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
$freshP1 = Product::query()->find($product1->id);
|
|
|
|
|
$freshP2 = Product::query()->find($product2->id);
|
|
|
|
|
expect($freshP1?->discount_id)->toBeNull()
|
|
|
|
|
->and($freshP2?->discount_id)->toBe($discount->id);
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
$response->assertRedirect("/bo/discounts/{$discount->id}/edit");
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
it('can delete a discount', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$discount = new Discount();
|
|
|
|
|
$discount->type = DiscountType::PERCENT;
|
|
|
|
|
$discount->value = DiscountValue::fromPercentage(10);
|
|
|
|
|
$discount->is_old_price_displayed = true;
|
|
|
|
|
$discount->save();
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
// WHEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$response = $this->delete("/bo/discounts/{$discount->id}");
|
2026-09-30 12:43:55 +02:00
|
|
|
|
2026-08-26 11:17:17 +02:00
|
|
|
// THEN
|
2026-09-30 12:43:55 +02:00
|
|
|
expect(Discount::query()->find($discount->id))->toBeNull();
|
2026-06-14 21:32:07 +02:00
|
|
|
$response->assertRedirect('/bo/discounts');
|
|
|
|
|
});
|
2026-08-26 11:17:17 +02:00
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
describe('authorization rejections', function (): void {
|
|
|
|
|
it('redirects unauthenticated guest to login', function (): void {
|
2026-08-26 11:52:08 +02:00
|
|
|
/** @var \Tests\TestCase $this */
|
2026-09-30 12:43:55 +02:00
|
|
|
// GIVEN
|
2026-08-26 11:17:17 +02:00
|
|
|
auth('backend')->logout();
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
// WHEN
|
2026-08-26 11:17:17 +02:00
|
|
|
$response = $this->get('/bo/discounts');
|
2026-09-30 12:43:55 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-08-26 11:17:17 +02:00
|
|
|
$response->assertRedirect('/bo/login');
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
it('redirects frontend customer trying to access bo to login', function (): void {
|
2026-08-26 11:52:08 +02:00
|
|
|
/** @var \Tests\TestCase $this */
|
2026-09-30 12:43:55 +02:00
|
|
|
// GIVEN
|
2026-08-26 11:52:08 +02:00
|
|
|
auth('backend')->logout();
|
2026-08-26 11:45:57 +02:00
|
|
|
$customer = new Customer();
|
|
|
|
|
$this->actingAs($customer, 'frontend');
|
2026-08-26 11:17:17 +02:00
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
// WHEN
|
2026-08-26 11:17:17 +02:00
|
|
|
$response = $this->get('/bo/discounts');
|
2026-09-30 12:43:55 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-08-26 11:45:57 +02:00
|
|
|
$response->assertRedirect('/bo/login');
|
2026-08-26 11:17:17 +02:00
|
|
|
});
|
2026-11-16 15:23:40 +01:00
|
|
|
|
|
|
|
|
it('forbids create and store when policy denies access', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$deniedPolicy = new class ()
|
|
|
|
|
{
|
|
|
|
|
public function create(): bool
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Gate::policy(Discount::class, $deniedPolicy::class);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$createResponse = get('/bo/discounts/create');
|
|
|
|
|
$storeResponse = post('/bo/discounts', [
|
|
|
|
|
'type' => DiscountType::PERCENT->value,
|
|
|
|
|
'value' => 10,
|
|
|
|
|
'is_old_price_displayed' => true,
|
|
|
|
|
'client_timezone' => 'Europe/Paris',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$createResponse->assertForbidden();
|
|
|
|
|
$storeResponse->assertForbidden();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('redirects back with session validation errors on invalid web store payload', function (): void {
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = from('/bo/discounts/create')->post('/bo/discounts', [
|
|
|
|
|
'type' => DiscountType::PERCENT->value,
|
|
|
|
|
'value' => 150,
|
|
|
|
|
'client_timezone' => 'Europe/Paris',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$response->assertRedirect('/bo/discounts/create')
|
|
|
|
|
->assertSessionHasErrors('value');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns 422 JSON validation errors on invalid JSON store payload', function (): void {
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = postJson('/bo/discounts', [
|
|
|
|
|
'type' => DiscountType::PERCENT->value,
|
|
|
|
|
'value' => 150,
|
|
|
|
|
'client_timezone' => 'Europe/Paris',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$response->assertUnprocessable()
|
|
|
|
|
->assertJsonValidationErrors('value');
|
2026-08-26 11:17:17 +02:00
|
|
|
});
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|