Ajout d'une liste de tickets

This commit is contained in:
2025-07-04 09:00:12 +00:00
parent 70955573e8
commit 11698a52e3
11 changed files with 628 additions and 9 deletions

View File

@@ -0,0 +1,213 @@
<?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\TicketBundle\Tests\Controller;
use Chill\MainBundle\Pagination\PaginatorFactoryInterface;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Serializer\Model\Collection;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\TicketBundle\Controller\TicketListApiController;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Repository\TicketACLAwareRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @internal
*
* @covers \Chill\TicketBundle\Controller\TicketListApiController
*/
final class TicketListApiControllerTest extends TestCase
{
use ProphecyTrait;
public function testListTicketNoParameter(): void
{
// Mock dependencies
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$ticketRepository = $this->prophesize(TicketACLAwareRepositoryInterface::class);
$tickets = [new Ticket(), new Ticket()];
$ticketRepository->countTickets([])->willReturn(2);
$ticketRepository->findTickets([], 0, 10)->willReturn($tickets);
$paginator = $this->prophesize(PaginatorInterface::class);
$paginator->getCurrentPageFirstItemNumber()->willReturn(0);
$paginator->getItemsPerPage()->willReturn(10);
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
$paginatorFactory->create(2)->willReturn($paginator->reveal());
$serializer = $this->prophesize(SerializerInterface::class);
$serializer->serialize(
Argument::that(fn(Collection $collection) => $collection->getItems() === $tickets),
'json',
['groups' => 'read']
)->willReturn('{"items":[{},{}],"pagination":{}}');
$personRepository = $this->prophesize(PersonRepository::class);
// Create controller
$controller = new TicketListApiController(
$security->reveal(),
$ticketRepository->reveal(),
$paginatorFactory->reveal(),
$serializer->reveal(),
$personRepository->reveal()
);
// Create request
$request = new Request();
// Call controller method
$response = $controller->listTicket($request);
// Assert response
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals('{"items":[{},{}],"pagination":{}}', $response->getContent());
}
public function testListTicketWithPersonFilter(): void
{
// Mock dependencies
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$person = new Person();
$security->isGranted(PersonVoter::SEE, $person)->willReturn(true);
$ticketRepository = $this->prophesize(TicketACLAwareRepositoryInterface::class);
$tickets = [new Ticket(), new Ticket()];
$ticketRepository->countTickets(Argument::that(fn($params) => isset($params['byPerson']) && in_array($person, $params['byPerson'])))->willReturn(2);
$ticketRepository->findTickets(
Argument::that(fn($params) => isset($params['byPerson']) && in_array($person, $params['byPerson'])),
0,
10
)->willReturn($tickets);
$paginator = $this->prophesize(PaginatorInterface::class);
$paginator->getCurrentPageFirstItemNumber()->willReturn(0);
$paginator->getItemsPerPage()->willReturn(10);
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
$paginatorFactory->create(2)->willReturn($paginator->reveal());
$serializer = $this->prophesize(SerializerInterface::class);
$serializer->serialize(
Argument::that(fn(Collection $collection) => $collection->getItems() === $tickets),
'json',
['groups' => 'read']
)->willReturn('{"items":[{},{}],"pagination":{}}');
$personRepository = $this->prophesize(PersonRepository::class);
$personRepository->find(123)->willReturn($person);
// Create controller
$controller = new TicketListApiController(
$security->reveal(),
$ticketRepository->reveal(),
$paginatorFactory->reveal(),
$serializer->reveal(),
$personRepository->reveal()
);
// Create request with person filter
$request = new Request(
query: ['byPerson' => '123']
);
// Call controller method
$response = $controller->listTicket($request);
// Assert response
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals('{"items":[{},{}],"pagination":{}}', $response->getContent());
}
public function testListTicketWithoutUserRole(): void
{
// Mock dependencies
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(false);
$ticketRepository = $this->prophesize(TicketACLAwareRepositoryInterface::class);
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
$serializer = $this->prophesize(SerializerInterface::class);
$personRepository = $this->prophesize(PersonRepository::class);
// Create controller
$controller = new TicketListApiController(
$security->reveal(),
$ticketRepository->reveal(),
$paginatorFactory->reveal(),
$serializer->reveal(),
$personRepository->reveal()
);
// Create request
$request = new Request();
// Expect exception
$this->expectException(AccessDeniedHttpException::class);
$this->expectExceptionMessage('Only users are allowed to list tickets.');
// Call controller method
$controller->listTicket($request);
}
public function testListTicketWithPersonWithoutAccess(): void
{
// Mock dependencies
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$person = new Person();
$security->isGranted(PersonVoter::SEE, $person)->willReturn(false);
$ticketRepository = $this->prophesize(TicketACLAwareRepositoryInterface::class);
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
$serializer = $this->prophesize(SerializerInterface::class);
$personRepository = $this->prophesize(PersonRepository::class);
$personRepository->find(123)->willReturn($person);
// Create controller
$controller = new TicketListApiController(
$security->reveal(),
$ticketRepository->reveal(),
$paginatorFactory->reveal(),
$serializer->reveal(),
$personRepository->reveal()
);
// Create request with person filter
$request = new Request(
query: ['byPerson' => '123']
);
// Expect exception
$this->expectException(AccessDeniedHttpException::class);
$this->expectExceptionMessage('Not allowed to see a person with id 123');
// Call controller method
$controller->listTicket($request);
}
}

View File

@@ -0,0 +1,66 @@
<?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\TicketBundle\Tests\Repository;
use Chill\PersonBundle\DataFixtures\Helper\RandomPersonHelperTrait;
use Chill\TicketBundle\Repository\TicketACLAwareRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
*
* @coversNothing
*/
class TicketACLAwareRepositoryTest extends KernelTestCase
{
use RandomPersonHelperTrait;
private TicketACLAwareRepository $repository;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->repository = new TicketACLAwareRepository($this->entityManager);
}
public function testFindNoParameters(): void
{
// Test the findTickets method with byPerson parameter
$actual = $this->repository->findTickets([]);
// Only verify that the query executes successfully without checking results
self::assertIsList($actual);
}
public function testFindTicketByPerson(): void
{
$person = $this->getRandomPerson($this->entityManager);
// Test the findTickets method with byPerson parameter
$actual = $this->repository->findTickets(['byPerson' => [$person]]);
// Only verify that the query executes successfully without checking results
self::assertIsList($actual);
}
public function testCountTicketsByPerson(): void
{
$person = $this->getRandomPerson($this->entityManager);
$result = $this->repository->countTickets(['byPerson' => [$person]]);
self::assertIsInt($result);
}
}

View File

@@ -81,6 +81,7 @@ class TicketNormalizerTest extends KernelTestCase
$t,
[
'type' => 'ticket_ticket',
'type_extended' => 'ticket_ticket:extended',
'createdAt' => $t->getCreatedAt()?->getTimestamp(),
'createdBy' => ['user'],
'id' => null,
@@ -131,6 +132,7 @@ class TicketNormalizerTest extends KernelTestCase
$ticket,
[
'type' => 'ticket_ticket',
'type_extended' => 'ticket_ticket:extended',
'createdAt' => $ticket->getCreatedAt()?->getTimestamp(),
'createdBy' => ['user'],
'id' => null,
@@ -169,6 +171,7 @@ class TicketNormalizerTest extends KernelTestCase
$ticket,
[
'type' => 'ticket_ticket',
'type_extended' => 'ticket_ticket:extended',
'createdAt' => $ticket->getCreatedAt()?->getTimestamp(),
'createdBy' => null,
'id' => null,
@@ -189,6 +192,86 @@ class TicketNormalizerTest extends KernelTestCase
];
}
public function testNormalizeReadSimple(): void
{
// Create a ticket with some data
$ticket = new Ticket();
$ticket->setExternalRef('TEST-123');
// Add state history
new StateHistory(StateEnum::OPEN, $ticket, new \DateTimeImmutable('2024-06-16T00:00:00Z'));
// Add emergency status
new EmergencyStatusHistory(EmergencyStatusEnum::YES, $ticket, new \DateTimeImmutable('2024-06-16T00:00:10Z'));
// Add person
$personHistory = new PersonHistory(new Person(), $ticket, new \DateTimeImmutable('2024-04-01T12:00:00'));
// Add motive
$motiveHistory = new MotiveHistory(new Motive(), $ticket, new \DateTimeImmutable('2024-04-01T12:02:00'));
// Add comment
$comment = new Comment('Test comment', $ticket);
$comment->setCreatedAt(new \DateTimeImmutable('2024-04-01T12:04:00'));
$comment->setCreatedBy(new User());
// Add addressee
new AddresseeHistory(new User(), new \DateTimeImmutable('2024-04-01T12:05:00'), $ticket);
// Add caller
new CallerHistory(new Person(), $ticket, new \DateTimeImmutable('2024-04-01T12:00:00'));
// Set created/updated metadata
$ticket->setCreatedAt(new \DateTimeImmutable('2024-06-16T00:00:00Z'));
$ticket->setCreatedBy(new User());
$ticket->setUpdatedAt(new \DateTimeImmutable('2024-06-16T00:00:00Z'));
$ticket->setUpdatedBy(new User());
// Normalize with read:simple group
$actual = $this->buildNormalizer()->normalize($ticket, 'json', ['groups' => 'read:simple']);
// Expected keys in read:simple normalization
$expectedKeys = [
'type',
'type_extended',
'id',
'externalRef',
'currentPersons',
'currentAddressees',
'currentInputs',
'currentMotive',
'currentState',
'emergency',
'caller',
];
// Keys that should not be present in read:simple normalization
$unexpectedKeys = [
'history',
'createdAt',
'updatedAt',
'updatedBy',
'createdBy',
];
// Assert that all expected keys are present
foreach ($expectedKeys as $key) {
self::assertArrayHasKey($key, $actual, "Expected key '{$key}' is missing");
}
// Assert that none of the unexpected keys are present
foreach ($unexpectedKeys as $key) {
self::assertArrayNotHasKey($key, $actual, "Unexpected key '{$key}' is present");
}
// Assert specific values
self::assertEquals('ticket_ticket', $actual['type']);
self::assertEquals('ticket_ticket:simple', $actual['type_extended']);
self::assertEquals('TEST-123', $actual['externalRef']);
self::assertEquals('open', $actual['currentState']);
self::assertEquals('yes', $actual['emergency']);
}
private function buildNormalizer(): TicketNormalizer
{
$normalizer = $this->prophesize(NormalizerInterface::class);