ldc-cocotte/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php

278 lines
11 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Controllers\Fo\Pages;
use App\Models\Discount;
2026-06-14 21:32:07 +02:00
use App\Models\Page;
use App\Models\Product;
use App\Models\Range;
use App\Models\Slide;
use Carbon\CarbonImmutable;
2026-06-14 21:32:07 +02:00
use Database\Seeders\ContentSeeder;
use Domain\Enums\DiscountType;
use Domain\Enums\ProductBrand;
use Domain\Enums\SellUnit;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\InvoicingUnit;
use Domain\Models\ValueObjects\DiscountValue;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\View;
use Money\Money;
use Shared\Helpers\UuidHelper;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
2026-06-14 21:32:07 +02:00
describe('HomeController', function () {
beforeEach(function (): void {
$now = CarbonImmutable::parse('2026-06-15 10:00:00', 'Europe/Paris');
Date::setTestNow($now);
CarbonImmutable::setTestNow($now);
Carbon::setTestNow($now);
});
afterEach(function (): void {
Date::setTestNow();
CarbonImmutable::setTestNow();
Carbon::setTestNow();
});
2026-06-14 21:32:07 +02:00
it('can access home page', function () {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
});
it('displays star products, slides and ranges', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
$range = new Range();
$range->name = 'Boucherie';
$range->slug = 'boucherie';
$range->picture = new StorageFilePath('ranges/boucherie.jpg');
$range->picture_alt = 'Boucherie';
$range->picture_title = 'Boucherie';
$range->status = true;
$range->order = 1;
$range->save();
$slide = new Slide();
$slide->title = 'Super Promo';
$slide->status = true;
$slide->order = 1;
$slide->connected = false;
$slide->link = 'https://example.com';
$slide->date_start = Carbon::yesterday();
$slide->date_end = Carbon::tomorrow();
$slide->save();
$product = new Product();
$product->id = (string) UuidHelper::v7();
$product->name = 'Poulet Fermier';
$product->brand = ProductBrand::loue;
$product->slug = 'poulet-fermier';
$product->description = 'Délicieux poulet fermier';
$product->picture_alt = 'Poulet Fermier';
$product->picture_title = 'Poulet Fermier';
$product->picture_large_alt = 'Poulet Fermier';
$product->picture_large_title = 'Poulet Fermier';
$product->price = Money::EUR(1500);
$product->invoicing_unit = InvoicingUnit::KGM;
$product->sell_unit = SellUnit::KGM;
$product->tax_rate = TaxRate::TVA_5_5;
$product->stock = 10;
$product->range_id = $range->id;
$product->order = 1;
$product->star = true;
$product->status = true;
$product->save();
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
$response->assertSee('id="hero-carousel"', false);
$response->assertSee('Poulet Fermier');
$response->assertSee('Super Promo');
$response->assertSee('Boucherie');
});
it('does not display slider section when no slides are published', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
$expiredSlide = new Slide();
$expiredSlide->title = 'Expired Promo';
$expiredSlide->status = true;
$expiredSlide->order = 1;
$expiredSlide->connected = false;
$expiredSlide->link = 'https://example.com';
$expiredSlide->date_start = Carbon::now()->subDays(5);
$expiredSlide->date_end = Carbon::now()->subDays(2);
$expiredSlide->save();
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
$response->assertDontSee('id="hero-carousel"', false);
$response->assertDontSee('Expired Promo');
});
it('displays promo prices in red for discounted star products on homepage', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
$range = new Range();
$range->name = 'Boucherie';
$range->slug = 'boucherie';
$range->picture = new StorageFilePath('ranges/boucherie.jpg');
$range->picture_alt = 'Boucherie';
$range->picture_title = 'Boucherie';
$range->status = true;
$range->order = 1;
$range->save();
$discount = new Discount();
$discount->type = DiscountType::PERCENT;
$discount->value = DiscountValue::fromPercentage(10);
$discount->is_old_price_displayed = false;
$discount->date_start = Carbon::yesterday();
$discount->date_end = Carbon::tomorrow();
$discount->save();
$promoProduct = new Product();
$promoProduct->id = (string) UuidHelper::v7();
$promoProduct->name = 'Foie Gras Promo';
$promoProduct->brand = ProductBrand::loue;
$promoProduct->slug = 'foie-gras-promo';
$promoProduct->description = 'Foie Gras en promo';
$promoProduct->picture_alt = 'Foie Gras Promo';
$promoProduct->picture_title = 'Foie Gras Promo';
$promoProduct->picture_large_alt = 'Foie Gras Promo';
$promoProduct->picture_large_title = 'Foie Gras Promo';
$promoProduct->price = Money::EUR(2000);
$promoProduct->invoicing_unit = InvoicingUnit::PCE;
$promoProduct->sell_unit = SellUnit::PCE;
$promoProduct->tax_rate = TaxRate::TVA_5_5;
$promoProduct->stock = 10;
$promoProduct->range_id = $range->id;
$promoProduct->order = 1;
$promoProduct->star = true;
$promoProduct->status = true;
$promoProduct->discount_id = $discount->id;
$promoProduct->save();
$regularProduct = new Product();
$regularProduct->id = (string) UuidHelper::v7();
$regularProduct->name = 'Poulet Standard';
$regularProduct->brand = ProductBrand::loue;
$regularProduct->slug = 'poulet-standard';
$regularProduct->description = 'Poulet standard sans promo';
$regularProduct->picture_alt = 'Poulet Standard';
$regularProduct->picture_title = 'Poulet Standard';
$regularProduct->picture_large_alt = 'Poulet Standard';
$regularProduct->picture_large_title = 'Poulet Standard';
$regularProduct->price = Money::EUR(1500);
$regularProduct->invoicing_unit = InvoicingUnit::PCE;
$regularProduct->sell_unit = SellUnit::PCE;
$regularProduct->tax_rate = TaxRate::TVA_5_5;
$regularProduct->stock = 10;
$regularProduct->range_id = $range->id;
$regularProduct->order = 2;
$regularProduct->star = true;
$regularProduct->status = true;
$regularProduct->discount_id = null;
$regularProduct->save();
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
$content = $response->getContent();
expect($content)->toContain('Foie Gras Promo')
->and($content)->toContain('Poulet Standard');
// Promo product price span has text-danger class
expect($content)->toMatch('/Foie Gras Promo.*?<span class="fw-semibold h4\s+text-danger\s*">/s')
// Regular product price span does NOT have text-danger class
->and($content)->toMatch('/Poulet Standard.*?<span class="fw-semibold h4\s*">/s');
});
it('escapes decoded HTML payloads in featured product slider description', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()->mapWithKeys(fn ($page) => [$page->slug->value() => $page]));
$range = new Range();
$range->name = 'Volaille';
$range->slug = 'volaille';
$range->picture = new StorageFilePath('ranges/volaille.jpg');
$range->picture_alt = 'Volaille';
$range->picture_title = 'Volaille';
$range->status = true;
$range->order = 1;
$range->save();
$product = new Product();
$product->id = (string) UuidHelper::v7();
$product->name = 'Poulet Sûr';
$product->brand = ProductBrand::loue;
$product->slug = 'poulet-sur';
$product->description = '<p>R&ocirc;ti &lt;img src=x onerror=alert(1)&gt;</p>';
$product->picture_alt = 'alt';
$product->picture_title = 'title';
$product->picture_large_alt = 'alt';
$product->picture_large_title = 'title';
$product->price = Money::EUR(1500);
$product->invoicing_unit = InvoicingUnit::PCE;
$product->sell_unit = SellUnit::PCE;
$product->tax_rate = TaxRate::TVA_5_5;
$product->stock = 10;
$product->range_id = $range->id;
$product->order = 1;
$product->star = true;
$product->status = true;
$product->save();
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
$response->assertDontSee('<img src=x onerror=alert(1)>', false);
$response->assertSee('Rôti &lt;img src=x onerror=alert(1)&gt;', false);
});
2026-06-14 21:32:07 +02:00
});