2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-09-30 15:49:20 +02:00
|
|
|
namespace Tests\Feature\App\Http\Controllers\Fo\Pages;
|
|
|
|
|
|
|
|
|
|
use App\Dto\ShopDto;
|
2026-11-14 22:41:40 +01:00
|
|
|
use App\Jobs\SendContactMailJob;
|
2026-11-14 16:06:12 +01:00
|
|
|
use App\Lib\PrivateS3Disk;
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Models\Page;
|
2026-09-30 15:49:20 +02:00
|
|
|
use App\Models\Shop;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Database\Seeders\ContentSeeder;
|
2026-11-14 23:15:10 +01:00
|
|
|
use Domain\Models\ValueObjects\OrderReference;
|
2026-11-14 16:06:12 +01:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2026-09-30 15:49:20 +02:00
|
|
|
use Illuminate\Support\Facades\Config;
|
2026-11-14 22:41:40 +01:00
|
|
|
use Illuminate\Support\Facades\Queue;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Illuminate\Support\Facades\View;
|
2026-11-14 23:15:10 +01:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2026-11-14 16:06:12 +01:00
|
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
|
|
|
use Uwil\Storage\Lib\ValueObjects\DiskName;
|
|
|
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-09-30 15:49:20 +02:00
|
|
|
describe('ContactController', function (): void {
|
|
|
|
|
beforeEach(function (): void {
|
|
|
|
|
Config::set('mail.to.contact', 'test@example.com');
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-09-30 15:49:20 +02:00
|
|
|
it('returns 404 when no default shop is configured', function (): void {
|
|
|
|
|
/** @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,
|
|
|
|
|
]));
|
|
|
|
|
Cache::flush();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = $this->get('/contactez-nous');
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$response->assertStatus(404);
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-30 15:49:20 +02:00
|
|
|
it('displays contact page with default shop and page content', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
$this->seed(ContentSeeder::class);
|
|
|
|
|
View::share('PageList', Page::getCached()
|
|
|
|
|
->mapWithKeys(fn ($page) => [
|
|
|
|
|
$page->slug->value() => $page,
|
|
|
|
|
]));
|
|
|
|
|
Cache::flush();
|
|
|
|
|
|
|
|
|
|
$shop = new Shop();
|
|
|
|
|
$shop->name = 'Boutique Cocotte';
|
|
|
|
|
$shop->address = '1 rue des Poules';
|
|
|
|
|
$shop->postcode = '75000';
|
|
|
|
|
$shop->city = 'Paris';
|
|
|
|
|
$shop->default = true;
|
|
|
|
|
$shop->save();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = $this->get('/contactez-nous');
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertViewIs('fo.pages.contact');
|
|
|
|
|
$response->assertViewHas('magasin', fn ($magasin) => $magasin instanceof ShopDto && $magasin->name === 'Boutique Cocotte');
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-14 22:41:40 +01:00
|
|
|
it('submits contact form and dispatches contact mail job', function (): void {
|
2026-09-30 15:49:20 +02:00
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
2026-11-14 22:41:40 +01:00
|
|
|
Queue::fake();
|
2026-09-30 15:49:20 +02:00
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'first_name' => 'Jean',
|
|
|
|
|
'last_name' => 'Dupont',
|
|
|
|
|
'email' => 'jean.dupont@gmail.com',
|
|
|
|
|
'subject' => 'Question',
|
2026-11-14 23:15:10 +01:00
|
|
|
'order_ref' => (string) new OrderReference(Uuid::uuid7()),
|
2026-09-30 15:49:20 +02:00
|
|
|
'message' => 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
|
|
|
|
'ok_cgv' => '1',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = $this->from('/contactez-nous')->post('/contactez-nous', $data);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$response->assertRedirect('/contactez-nous');
|
|
|
|
|
$response->assertSessionHas('status', true);
|
2026-11-14 22:41:40 +01:00
|
|
|
Queue::assertPushed(
|
|
|
|
|
SendContactMailJob::class,
|
|
|
|
|
fn (SendContactMailJob $job): bool => $job->recipientEmail === 'test@example.com'
|
|
|
|
|
&& $job->data->email === 'jean.dupont@gmail.com'
|
|
|
|
|
);
|
2026-09-30 15:49:20 +02:00
|
|
|
});
|
|
|
|
|
|
2026-11-14 16:06:12 +01:00
|
|
|
it('submits contact form with an attachment uploaded via StorageService', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
2026-11-14 22:41:40 +01:00
|
|
|
Queue::fake();
|
2026-11-14 16:06:12 +01:00
|
|
|
$file = UploadedFile::fake()->create('piece.pdf', 100, 'application/pdf');
|
|
|
|
|
$this->mock(StorageService::class, function ($mock): void {
|
|
|
|
|
$mock->shouldReceive('storeUploadedOntoDisk')
|
|
|
|
|
->once()
|
|
|
|
|
->with(
|
|
|
|
|
\Mockery::type(UploadedFile::class),
|
|
|
|
|
\Mockery::on(fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
|
|
|
|
)
|
|
|
|
|
->andReturn(new StorageFilePath('contact-attachments/piece.pdf'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'first_name' => 'Jean',
|
|
|
|
|
'last_name' => 'Dupont',
|
|
|
|
|
'email' => 'jean.dupont@gmail.com',
|
|
|
|
|
'subject' => 'Question avec PJ',
|
|
|
|
|
'message' => 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
|
|
|
|
'file' => $file,
|
|
|
|
|
'ok_cgv' => '1',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = $this->from('/contactez-nous')->post('/contactez-nous', $data);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$response->assertRedirect('/contactez-nous');
|
|
|
|
|
$response->assertSessionHas('status', true);
|
2026-11-14 22:41:40 +01:00
|
|
|
Queue::assertPushed(
|
|
|
|
|
SendContactMailJob::class,
|
|
|
|
|
fn (SendContactMailJob $job): bool => $job->data->file?->path === 'contact-attachments/piece.pdf'
|
2026-11-14 16:06:12 +01:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-14 22:41:40 +01:00
|
|
|
it('throttles contact form submissions after 5 attempts within 2 minutes', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
Queue::fake();
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'first_name' => 'Jean',
|
|
|
|
|
'last_name' => 'Dupont',
|
|
|
|
|
'email' => 'jean.dupont@gmail.com',
|
|
|
|
|
'subject' => 'Question',
|
|
|
|
|
'message' => 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
|
|
|
|
'ok_cgv' => '1',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
for ($i = 0; $i < 5; $i++) {
|
|
|
|
|
$this->from('/contactez-nous')
|
|
|
|
|
->post('/contactez-nous', $data)
|
|
|
|
|
->assertStatus(302);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$throttledResponse = $this->from('/contactez-nous')->post('/contactez-nous', $data);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$throttledResponse->assertStatus(429);
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-30 15:49:20 +02:00
|
|
|
it('redirects with validation errors when form data is invalid', function (): void {
|
|
|
|
|
/** @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,
|
|
|
|
|
]));
|
|
|
|
|
Cache::flush();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
2026-09-30 15:49:20 +02:00
|
|
|
$response = $this->from('/contactez-nous')->post('/contactez-nous', []);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$response->assertStatus(302);
|
2026-09-30 15:49:20 +02:00
|
|
|
$response->assertSessionHasErrors(['first_name', 'last_name', 'email', 'subject', 'message', 'ok_cgv']);
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
});
|