mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-29 18:14:59 +00:00
Tickets: edit comments and mark them as deleted
This commit is contained in:
@@ -25,10 +25,13 @@ use Chill\TicketBundle\Entity\PersonHistory;
|
||||
use Chill\TicketBundle\Entity\StateEnum;
|
||||
use Chill\TicketBundle\Entity\StateHistory;
|
||||
use Chill\TicketBundle\Entity\Ticket;
|
||||
use Chill\TicketBundle\Security\Voter\CommentVoter;
|
||||
use Chill\TicketBundle\Serializer\Normalizer\TicketNormalizer;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
@@ -40,6 +43,13 @@ class TicketNormalizerTest extends KernelTestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private ObjectProphecy $security;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->security = $this->prophesize(Security::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideTickets
|
||||
*/
|
||||
@@ -192,6 +202,35 @@ class TicketNormalizerTest extends KernelTestCase
|
||||
];
|
||||
}
|
||||
|
||||
public function testNormalizeTicketWithCommentNotAllowed(): void
|
||||
{
|
||||
$ticket = new Ticket();
|
||||
$comment = new Comment('Test comment', $ticket);
|
||||
$comment->setCreatedAt(new \DateTimeImmutable('2024-04-01T12:04:00'));
|
||||
$comment->setCreatedBy(new User());
|
||||
|
||||
$this->security->isGranted(CommentVoter::READ, $comment)->willReturn(false)->shouldBeCalled();
|
||||
|
||||
$actual = $this->buildNormalizer()->normalize($ticket, 'json', ['groups' => ['read']]);
|
||||
|
||||
self::assertEmpty($actual['history']);
|
||||
}
|
||||
|
||||
public function testNormalizeTicketWithCommentAllowed(): void
|
||||
{
|
||||
$ticket = new Ticket();
|
||||
$comment = new Comment('Test comment', $ticket);
|
||||
$comment->setCreatedAt(new \DateTimeImmutable('2024-04-01T12:04:00'));
|
||||
$comment->setCreatedBy(new User());
|
||||
|
||||
$this->security->isGranted(CommentVoter::READ, $comment)->willReturn(true)->shouldBeCalled();
|
||||
|
||||
$actual = $this->buildNormalizer()->normalize($ticket, 'json', ['groups' => ['read']]);
|
||||
|
||||
self::assertCount(1, $actual['history']);
|
||||
self::assertEquals('add_comment', $actual['history'][0]['event_type']);
|
||||
}
|
||||
|
||||
public function testNormalizeReadSimple(): void
|
||||
{
|
||||
// Create a ticket with some data
|
||||
@@ -274,6 +313,10 @@ class TicketNormalizerTest extends KernelTestCase
|
||||
|
||||
private function buildNormalizer(): TicketNormalizer
|
||||
{
|
||||
$this->security->isGranted(CommentVoter::READ, Argument::type(Comment::class))
|
||||
->willReturn(true);
|
||||
|
||||
|
||||
$normalizer = $this->prophesize(NormalizerInterface::class);
|
||||
|
||||
// empty array
|
||||
@@ -285,14 +328,14 @@ class TicketNormalizerTest extends KernelTestCase
|
||||
|
||||
// array of mixed objects
|
||||
$normalizer->normalize(
|
||||
Argument::that(fn ($arg) => is_array($arg) && 0 < count($arg) && is_object($arg[0])),
|
||||
Argument::that(fn ($arg) => is_array($arg) && 0 < count($arg) && is_object($arg[0] ?? null)),
|
||||
'json',
|
||||
Argument::type('array')
|
||||
)->will(fn ($args) => array_fill(0, count($args[0]), 'embedded'));
|
||||
|
||||
// array of event type
|
||||
$normalizer->normalize(
|
||||
Argument::that(fn ($arg) => is_array($arg) && 0 < count($arg) && is_array($arg[0]) && array_key_exists('event_type', $arg[0])),
|
||||
Argument::that(fn ($arg) => is_array($arg) && 0 < count($arg) && is_array($arg[0] ?? null) && array_key_exists('event_type', $arg[0] ?? null)),
|
||||
'json',
|
||||
Argument::type('array')
|
||||
)->will(function ($args): array {
|
||||
@@ -357,7 +400,7 @@ class TicketNormalizerTest extends KernelTestCase
|
||||
// null values
|
||||
$normalizer->normalize(null, 'json', Argument::type('array'))->willReturn(null);
|
||||
|
||||
$ticketNormalizer = new TicketNormalizer();
|
||||
$ticketNormalizer = new TicketNormalizer($this->security->reveal());
|
||||
$ticketNormalizer->setNormalizer($normalizer->reveal());
|
||||
|
||||
return $ticketNormalizer;
|
||||
|
Reference in New Issue
Block a user