From 80ce7f0bf15e2f881187d1f8b9925decc037de6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 13 Mar 2025 15:29:49 +0100 Subject: [PATCH] Implement ExportGenerationController with waiting and status routes Added a new controller to handle export generation wait views and status responses. Updated ExportController to redirect to the 'wait' route after export generation. Introduced tests to validate object status handling for pending and ready states. --- .../Controller/ExportController.php | 2 +- .../Controller/ExportGenerationController.php | 65 +++++++++++++++++++ .../ExportGenerationControllerTest.php | 54 +++++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/Bundle/ChillMainBundle/Controller/ExportGenerationController.php create mode 100644 src/Bundle/ChillMainBundle/Tests/Controller/ExportGenerationControllerTest.php diff --git a/src/Bundle/ChillMainBundle/Controller/ExportController.php b/src/Bundle/ChillMainBundle/Controller/ExportController.php index 42ac80de7..772dcf408 100644 --- a/src/Bundle/ChillMainBundle/Controller/ExportController.php +++ b/src/Bundle/ChillMainBundle/Controller/ExportController.php @@ -503,7 +503,7 @@ class ExportController extends AbstractController $this->session->remove('formatter_step_raw'); $this->session->remove('formatter_step'); - return new Response('ok: '.$exportGeneration->getId()); + return $this->redirectToRoute('chill_main_export-generation_wait', ['id' => $exportGeneration->getId()]); } /** diff --git a/src/Bundle/ChillMainBundle/Controller/ExportGenerationController.php b/src/Bundle/ChillMainBundle/Controller/ExportGenerationController.php new file mode 100644 index 000000000..aea4049c9 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Controller/ExportGenerationController.php @@ -0,0 +1,65 @@ +security->isGranted('ROLE_USER')) { + throw new AccessDeniedHttpException('Only users can download an export'); + } + + return new Response( + $this->twig->render('@ChillMain/ExportGeneration/wait.html.twig', ['exportGeneration' => $exportGeneration]), + ); + } + + #[Route('/api/1.0/main/export-generation/{id}/object', methods: ['GET'])] + public function objectStatus(ExportGeneration $exportGeneration): JsonResponse + { + if (!$this->security->isGranted('ROLE_USER')) { + throw new AccessDeniedHttpException('Only users can download an export'); + } + + if (StoredObject::STATUS_PENDING === $exportGeneration->getStoredObject()->getStatus()) { + return new JsonResponse(['status' => 'pending']); + } + + return new JsonResponse( + $this->serializer->serialize( + ['status' => $exportGeneration->getStoredObject()->getStatus(), 'stored_object' => $exportGeneration->getStoredObject()], + 'json', + [AbstractNormalizer::GROUPS => ['read']], + ), + json: true, + ); + } +} diff --git a/src/Bundle/ChillMainBundle/Tests/Controller/ExportGenerationControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Controller/ExportGenerationControllerTest.php new file mode 100644 index 000000000..a6b8668f2 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Controller/ExportGenerationControllerTest.php @@ -0,0 +1,54 @@ +prophesize(Security::class); + $security->isGranted('ROLE_USER')->willReturn(true); + $environment = $this->prophesize(Environment::class); + $serializer = $this->prophesize(SerializerInterface::class); + $serializer->serialize(Argument::any(), 'json', ['groups' => ['read']])->willReturn('{}'); + + $pending = new ExportGeneration('dummy', []); + + $controller = new ExportGenerationController($security->reveal(), $environment->reveal(), $serializer->reveal()); + + $actual = $controller->objectStatus($pending); + self::assertEquals(json_encode(['status' => 'pending'], JSON_THROW_ON_ERROR), $actual->getContent()); + + $generated = new ExportGeneration('dummy', []); + $generated->getStoredObject()->setStatus(StoredObject::STATUS_READY); + + self:assertEquals('{}', $controller->objectStatus($generated)->getContent()); + } +}