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.
This commit is contained in:
Julien Fastré 2025-03-13 15:29:49 +01:00
parent 1ebf838bde
commit 80ce7f0bf1
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 120 additions and 1 deletions

View File

@ -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()]);
}
/**

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Controller;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Entity\ExportGeneration;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Twig\Environment;
final readonly class ExportGenerationController
{
public function __construct(
private Security $security,
private Environment $twig,
private SerializerInterface $serializer,
) {}
#[Route('/{_locale}/main/export-generation/{id}/wait', methods: ['GET'], name: 'chill_main_export-generation_wait')]
public function wait(ExportGeneration $exportGeneration): Response
{
if (!$this->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,
);
}
}

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Tests\Controller;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Controller\ExportGenerationController;
use Chill\MainBundle\Entity\ExportGeneration;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
use Twig\Environment;
use function PHPUnit\Framework\assertEquals;
/**
* @internal
*
* @coversNothing
*/
class ExportGenerationControllerTest extends TestCase
{
use ProphecyTrait;
public function testObjectStatus(): void
{
$security = $this->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());
}
}