mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-03-13 17:37:45 +00:00
Add AuditEventDumperTest to validate audit event dumping functionality
- Introduced a test case for `AuditEventDumper` to ensure proper CSV generation and storage. - Verified metadata, user, and entity subject data accuracy in the dumped output.
This commit is contained in:
104
src/Bundle/ChillMainBundle/Tests/Audit/AuditEventDumperTest.php
Normal file
104
src/Bundle/ChillMainBundle/Tests/Audit/AuditEventDumperTest.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?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 Audit;
|
||||
|
||||
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
|
||||
use Chill\MainBundle\Audit\AuditEventDumper;
|
||||
use Chill\MainBundle\Audit\SubjectConverterManagerInterface;
|
||||
use Chill\MainBundle\Entity\AuditTrail;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Repository\AuditTrailRepository;
|
||||
use Chill\MainBundle\Templating\Entity\UserRender;
|
||||
use Chill\MainBundle\Test\PrepareCenterTrait;
|
||||
use Chill\MainBundle\Test\PrepareUserTrait;
|
||||
use Chill\PersonBundle\DataFixtures\Helper\RandomPersonHelperTrait;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Ramsey\Uuid\Rfc4122\UuidV7;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class AuditEventDumperTest extends KernelTestCase
|
||||
{
|
||||
use PrepareUserTrait;
|
||||
|
||||
use RandomPersonHelperTrait;
|
||||
|
||||
use PrepareCenterTrait;
|
||||
private StoredObjectManagerInterface $storedObjectManager;
|
||||
private AuditTrailRepository $auditTrailRepository;
|
||||
private TranslatorInterface $translator;
|
||||
private SubjectConverterManagerInterface $subjectConverterManager;
|
||||
private EntityManagerInterface $entityManager;
|
||||
private UserRender $userRender;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
$this->storedObjectManager = self::getContainer()->get(StoredObjectManagerInterface::class);
|
||||
$this->auditTrailRepository = self::getContainer()->get(AuditTrailRepository::class);
|
||||
$this->translator = self::getContainer()->get('translator');
|
||||
$this->subjectConverterManager = self::getContainer()->get(SubjectConverterManagerInterface::class);
|
||||
$this->entityManager = self::getContainer()->get('doctrine.orm.entity_manager');
|
||||
$this->userRender = self::getContainer()->get(UserRender::class);
|
||||
}
|
||||
|
||||
public function testDump(): void
|
||||
{
|
||||
// store an audit
|
||||
|
||||
$user = $this->entityManager->createQuery('SELECT u FROM '.User::class.' u')
|
||||
->setMaxResults(1)
|
||||
->getOneOrNullResult();
|
||||
$person = $this->getRandomPerson($this->entityManager);
|
||||
$subjetBag = $this->subjectConverterManager->getSubjectsForEntity($person);
|
||||
|
||||
$audit = new AuditTrail(
|
||||
UuidV7::uuid7(),
|
||||
AuditTrail::AUDIT_VIEW,
|
||||
$at = new \DateTimeImmutable('2026-02-14T14:00:00', new \DateTimeZone('Europe/Brussels')),
|
||||
$user,
|
||||
'Some description',
|
||||
$subjetBag->subject->asArray(),
|
||||
[],
|
||||
['some-metadata' => 1],
|
||||
);
|
||||
|
||||
$this->entityManager->persist($audit);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$criteria = ['from' => $at->sub(new \DateInterval('PT30S')), 'to' => $at->add(new \DateInterval('PT30S'))];
|
||||
|
||||
$dumper = new AuditEventDumper(
|
||||
$this->storedObjectManager,
|
||||
$this->auditTrailRepository,
|
||||
$this->translator,
|
||||
$this->subjectConverterManager,
|
||||
$this->entityManager,
|
||||
$this->userRender,
|
||||
);
|
||||
|
||||
$storedObject = $dumper->dump($criteria, 'fr');
|
||||
|
||||
$read = $this->storedObjectManager->read($storedObject);
|
||||
|
||||
self::assertStringContainsString('14/02/2026 14:00:00', $read);
|
||||
self::assertStringContainsString((string) $user->getId(), $read);
|
||||
self::assertStringContainsString('Some description', $read);
|
||||
self::assertStringContainsString($person->getFirstName(), $read);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user