diff --git a/assets/tools/FlowReserve.js b/assets/tools/FlowReserve.js index d33474d..ff745e3 100644 --- a/assets/tools/FlowReserve.js +++ b/assets/tools/FlowReserve.js @@ -94,15 +94,15 @@ export class FlowReserve extends HTMLAnchorElement { ensureSidebarExists() { if (document.getElementById(this.sidebarId)) return; - + const template = `
Une erreur inattendue est survenue.
"; + $html = "Une erreur inattendue est survenue.
"; } $response = new Response($html, $statusCode); diff --git a/tests/Security/ErrorListenerTest.php b/tests/Security/ErrorListenerTest.php index 6d39337..0b4a544 100644 --- a/tests/Security/ErrorListenerTest.php +++ b/tests/Security/ErrorListenerTest.php @@ -3,6 +3,7 @@ namespace App\Tests\Security; use App\Security\ErrorListener; +use App\Service\Mailer\Mailer; use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\JsonResponse; @@ -17,12 +18,14 @@ use Twig\Environment; class ErrorListenerTest extends TestCase { private $twig; + private $mailer; private $listener; protected function setUp(): void { $this->twig = $this->createMock(Environment::class); - $this->listener = new ErrorListener($this->twig); + $this->mailer = $this->createMock(Mailer::class); + $this->listener = new ErrorListener($this->mailer, $this->twig); } public function testOnKernelExceptionInDevModeDoesNothing() @@ -32,11 +35,50 @@ class ErrorListenerTest extends TestCase $request = new Request(); $event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, new \Exception()); + // Le mailer ne doit jamais être appelé en dev + $this->mailer->expects($this->never())->method('send'); + $this->listener->onKernelException($event); $this->assertNull($event->getResponse()); - - unset($_ENV['APP_ENV']); // Cleanup + + unset($_ENV['APP_ENV']); + } + + public function testOnKernelExceptionSendsEmailFor404() + { + $_ENV['APP_ENV'] = 'prod'; + $kernel = $this->createMock(HttpKernelInterface::class); + $request = Request::create('/une-page-existante'); + + $exception = new NotFoundHttpException('Not found'); + $event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception); + + // On vérifie que le mail est bien envoyé pour une 404 standard + $this->mailer->expects($this->once()) + ->method('send') + ->with($this->anything(), $this->anything(), $this->stringContains('404 Page introuvable')); + + $this->listener->onKernelException($event); + + unset($_ENV['APP_ENV']); + } + + public function testOnKernelExceptionDoesNotSendEmailForBotTarget() + { + $_ENV['APP_ENV'] = 'prod'; + $kernel = $this->createMock(HttpKernelInterface::class); + $request = Request::create('/.env'); // URL typique de bot + + $exception = new NotFoundHttpException('Not found'); + $event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception); + + // Le mailer ne doit PAS être appelé car c'est une cible de bot + $this->mailer->expects($this->never())->method('send'); + + $this->listener->onKernelException($event); + + unset($_ENV['APP_ENV']); } public function testOnKernelExceptionJsonRequest() @@ -45,7 +87,7 @@ class ErrorListenerTest extends TestCase $kernel = $this->createMock(HttpKernelInterface::class); $request = new Request(); $request->headers->set('Accept', 'application/json'); - + $exception = new NotFoundHttpException('Not found'); $event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception); @@ -54,11 +96,7 @@ class ErrorListenerTest extends TestCase $response = $event->getResponse(); $this->assertInstanceOf(JsonResponse::class, $response); $this->assertEquals(404, $response->getStatusCode()); - - $content = json_decode($response->getContent(), true); - $this->assertEquals('error', $content['status']); - $this->assertEquals('Resource not found', $content['message']); - + unset($_ENV['APP_ENV']); } @@ -67,8 +105,8 @@ class ErrorListenerTest extends TestCase $_ENV['APP_ENV'] = 'prod'; $kernel = $this->createMock(HttpKernelInterface::class); $request = new Request(); - - $exception = new \Exception('Error'); + + $exception = new \Exception('500 Error'); $event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception); $this->twig->expects($this->once()) @@ -79,10 +117,8 @@ class ErrorListenerTest extends TestCase $this->listener->onKernelException($event); $response = $event->getResponse(); - $this->assertInstanceOf(Response::class, $response); $this->assertEquals(500, $response->getStatusCode()); - $this->assertEquals('Error', $response->getContent()); - + unset($_ENV['APP_ENV']); } }