feat: Tracer automatiquement les événements notes et évaluations dans l'audit trail
Story 1-7 avait posé les fondations d'audit trail mais laissé en dehors du
périmètre initial les événements notes/évaluations, qui étaient alors non
couverts par les domaines. Avec la clôture des epics notation, ces actions
sensibles (création/modification/suppression d'évaluation, saisie/modification
de note, publication) doivent maintenant être tracées pour répondre aux
exigences RGPD et faciliter la résolution des litiges parent/enseignant.
Les événements de domaine existants ne transportaient pas tous les champs
nécessaires à l'audit (ancien/nouveau titre, description, barème, coefficient,
date, studentId). L'enrichissement de leur payload permet aux handlers d'audit
de journaliser les diffs complets via AuditLogger, sans que les autres
consommateurs (recalcul de moyennes) n'aient besoin de changer leur logique.
Au passage, le test E2E student-grades AC5 ("Nouveau" badge) visait
séquentiellement '.grade-card' puis '.badge-new' : la fenêtre de 3 s avant
markGradesSeen pouvait se refermer entre les deux attentes sur Firefox CI.
Un seul expect combiné '.grade-card .badge-new' élimine cette course.
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Functional\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Scolarite\Domain\Event\EvaluationCreee;
|
||||
use App\Scolarite\Domain\Event\EvaluationModifiee;
|
||||
use App\Scolarite\Domain\Event\EvaluationSupprimee;
|
||||
use App\Scolarite\Domain\Event\NotesPubliees;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Infrastructure\EventHandler\AuditEvaluationEventsHandler;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
use const JSON_THROW_ON_ERROR;
|
||||
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* Vérifie le round-trip DB de l'audit des événements Evaluation : le handler,
|
||||
* connecté à l'AuditLogger réel, persiste `old_values`/`new_values` complets
|
||||
* (title, description, coefficient, evaluation_date, grade_scale) dans `audit_log`.
|
||||
*/
|
||||
final class AuditEvaluationEventsHandlerFunctionalTest extends KernelTestCase
|
||||
{
|
||||
private Connection $connection;
|
||||
private AuditEvaluationEventsHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
/** @var Connection $connection */
|
||||
$connection = static::getContainer()->get(Connection::class);
|
||||
$this->connection = $connection;
|
||||
|
||||
/** @var AuditEvaluationEventsHandler $handler */
|
||||
$handler = static::getContainer()->get(AuditEvaluationEventsHandler::class);
|
||||
$this->handler = $handler;
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handleEvaluationCreeeWritesAuditEntryToDatabase(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
$classId = Uuid::uuid4()->toString();
|
||||
$subjectId = Uuid::uuid4()->toString();
|
||||
$teacherId = Uuid::uuid4()->toString();
|
||||
|
||||
$event = new EvaluationCreee(
|
||||
evaluationId: $evaluationId,
|
||||
classId: $classId,
|
||||
subjectId: $subjectId,
|
||||
teacherId: $teacherId,
|
||||
title: 'Contrôle chapitre 3',
|
||||
description: 'Sur les parties 1 et 2',
|
||||
evaluationDate: new DateTimeImmutable('2026-04-15'),
|
||||
gradeScale: 20,
|
||||
coefficient: 2.0,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->handler->handleEvaluationCreee($event);
|
||||
|
||||
$entry = $this->connection->fetchAssociative(
|
||||
'SELECT * FROM audit_log WHERE aggregate_id = ? AND event_type = ? ORDER BY occurred_at DESC LIMIT 1',
|
||||
[$evaluationId->value->toString(), 'EvaluationCreee'],
|
||||
);
|
||||
|
||||
self::assertNotFalse($entry, 'Audit log entry should exist after EvaluationCreee');
|
||||
self::assertSame('Evaluation', $entry['aggregate_type']);
|
||||
|
||||
$payload = self::decodePayload($entry['payload']);
|
||||
self::assertSame([], $payload['old_values']);
|
||||
self::assertSame('Contrôle chapitre 3', $payload['new_values']['title']);
|
||||
self::assertSame('Sur les parties 1 et 2', $payload['new_values']['description']);
|
||||
self::assertSame($classId, $payload['new_values']['class_id']);
|
||||
self::assertSame($subjectId, $payload['new_values']['subject_id']);
|
||||
self::assertSame($teacherId, $payload['new_values']['teacher_id']);
|
||||
self::assertSame('2026-04-15', $payload['new_values']['evaluation_date']);
|
||||
self::assertSame(20, $payload['new_values']['grade_scale']);
|
||||
self::assertSame(2.0, $payload['new_values']['coefficient']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handleEvaluationModifieeWritesAuditEntryWithDiff(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
|
||||
$event = new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
oldTitle: 'Ancien titre',
|
||||
newTitle: 'Nouveau titre',
|
||||
oldDescription: null,
|
||||
newDescription: 'Description ajoutée',
|
||||
oldCoefficient: 1.0,
|
||||
newCoefficient: 3.0,
|
||||
oldEvaluationDate: new DateTimeImmutable('2026-04-15'),
|
||||
newEvaluationDate: new DateTimeImmutable('2026-05-02'),
|
||||
oldGradeScale: 20,
|
||||
newGradeScale: 100,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->handler->handleEvaluationModifiee($event);
|
||||
|
||||
$entry = $this->connection->fetchAssociative(
|
||||
'SELECT * FROM audit_log WHERE aggregate_id = ? AND event_type = ? ORDER BY occurred_at DESC LIMIT 1',
|
||||
[$evaluationId->value->toString(), 'EvaluationModifiee'],
|
||||
);
|
||||
|
||||
self::assertNotFalse($entry, 'Audit log entry should exist after EvaluationModifiee');
|
||||
self::assertSame('Evaluation', $entry['aggregate_type']);
|
||||
|
||||
$payload = self::decodePayload($entry['payload']);
|
||||
self::assertSame('Ancien titre', $payload['old_values']['title']);
|
||||
self::assertNull($payload['old_values']['description']);
|
||||
self::assertSame(1.0, $payload['old_values']['coefficient']);
|
||||
self::assertSame('2026-04-15', $payload['old_values']['evaluation_date']);
|
||||
self::assertSame(20, $payload['old_values']['grade_scale']);
|
||||
|
||||
self::assertSame('Nouveau titre', $payload['new_values']['title']);
|
||||
self::assertSame('Description ajoutée', $payload['new_values']['description']);
|
||||
self::assertSame(3.0, $payload['new_values']['coefficient']);
|
||||
self::assertSame('2026-05-02', $payload['new_values']['evaluation_date']);
|
||||
self::assertSame(100, $payload['new_values']['grade_scale']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handleNotesPublieesWritesAuditEntryToDatabase(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
|
||||
$event = new NotesPubliees(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->handler->handleNotesPubliees($event);
|
||||
|
||||
$entry = $this->connection->fetchAssociative(
|
||||
'SELECT * FROM audit_log WHERE aggregate_id = ? AND event_type = ? ORDER BY occurred_at DESC LIMIT 1',
|
||||
[$evaluationId->value->toString(), 'NotesPubliees'],
|
||||
);
|
||||
|
||||
self::assertNotFalse($entry, 'Audit log entry should exist after NotesPubliees');
|
||||
self::assertSame('Evaluation', $entry['aggregate_type']);
|
||||
self::assertSame($evaluationId->value->toString(), $entry['aggregate_id']);
|
||||
|
||||
$payload = self::decodePayload($entry['payload']);
|
||||
self::assertSame([], $payload['old_values']);
|
||||
self::assertSame([], $payload['new_values']);
|
||||
|
||||
$metadata = self::decodePayload($entry['metadata']);
|
||||
self::assertArrayHasKey('correlation_id', $metadata);
|
||||
self::assertArrayHasKey('occurred_at', $metadata);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handleEvaluationSupprimeeWritesAuditEntryToDatabase(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
|
||||
$event = new EvaluationSupprimee(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->handler->handleEvaluationSupprimee($event);
|
||||
|
||||
$entry = $this->connection->fetchAssociative(
|
||||
'SELECT * FROM audit_log WHERE aggregate_id = ? AND event_type = ? ORDER BY occurred_at DESC LIMIT 1',
|
||||
[$evaluationId->value->toString(), 'EvaluationSupprimee'],
|
||||
);
|
||||
|
||||
self::assertNotFalse($entry, 'Audit log entry should exist after EvaluationSupprimee');
|
||||
self::assertSame('Evaluation', $entry['aggregate_type']);
|
||||
self::assertSame($evaluationId->value->toString(), $entry['aggregate_id']);
|
||||
|
||||
$payload = self::decodePayload($entry['payload']);
|
||||
self::assertSame([], $payload['old_values']);
|
||||
self::assertSame([], $payload['new_values']);
|
||||
|
||||
$metadata = self::decodePayload($entry['metadata']);
|
||||
self::assertArrayHasKey('correlation_id', $metadata);
|
||||
self::assertArrayHasKey('occurred_at', $metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function decodePayload(mixed $raw): array
|
||||
{
|
||||
self::assertIsString($raw);
|
||||
|
||||
/** @var array<string, mixed> $decoded */
|
||||
$decoded = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Functional\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Scolarite\Domain\Event\NoteModifiee;
|
||||
use App\Scolarite\Domain\Event\NoteSaisie;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeId;
|
||||
use App\Scolarite\Infrastructure\EventHandler\AuditGradeEventsHandler;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
use const JSON_THROW_ON_ERROR;
|
||||
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* Vérifie le bout-en-bout de l'audit des événements Grade : le handler,
|
||||
* connecté à l'AuditLogger réel et à la base, écrit une ligne immuable
|
||||
* dans `audit_log` avec le bon payload (création / diff).
|
||||
*/
|
||||
final class AuditGradeEventsHandlerFunctionalTest extends KernelTestCase
|
||||
{
|
||||
private Connection $connection;
|
||||
private AuditGradeEventsHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
/** @var Connection $connection */
|
||||
$connection = static::getContainer()->get(Connection::class);
|
||||
$this->connection = $connection;
|
||||
|
||||
/** @var AuditGradeEventsHandler $handler */
|
||||
$handler = static::getContainer()->get(AuditGradeEventsHandler::class);
|
||||
$this->handler = $handler;
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
// audit_log est append-only : pas de DELETE possible, on filtre par UUID unique dans chaque test
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handleNoteSaisieWritesAuditEntryToDatabase(): void
|
||||
{
|
||||
$gradeId = GradeId::generate();
|
||||
$evaluationId = Uuid::uuid4()->toString();
|
||||
$studentId = Uuid::uuid4()->toString();
|
||||
$createdBy = Uuid::uuid4()->toString();
|
||||
|
||||
$event = new NoteSaisie(
|
||||
gradeId: $gradeId,
|
||||
evaluationId: $evaluationId,
|
||||
studentId: $studentId,
|
||||
value: 15.5,
|
||||
status: 'draft',
|
||||
createdBy: $createdBy,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->handler->handleNoteSaisie($event);
|
||||
|
||||
$entry = $this->connection->fetchAssociative(
|
||||
'SELECT * FROM audit_log WHERE aggregate_id = ? AND event_type = ? ORDER BY occurred_at DESC LIMIT 1',
|
||||
[$gradeId->value->toString(), 'NoteSaisie'],
|
||||
);
|
||||
|
||||
self::assertNotFalse($entry, 'Audit log entry should exist after NoteSaisie');
|
||||
self::assertSame('Grade', $entry['aggregate_type']);
|
||||
|
||||
$payload = self::decodePayload($entry['payload']);
|
||||
self::assertSame([], $payload['old_values']);
|
||||
self::assertSame($evaluationId, $payload['new_values']['evaluation_id']);
|
||||
self::assertSame($studentId, $payload['new_values']['student_id']);
|
||||
self::assertSame(15.5, $payload['new_values']['value']);
|
||||
self::assertSame('draft', $payload['new_values']['status']);
|
||||
self::assertSame($createdBy, $payload['new_values']['created_by']);
|
||||
|
||||
self::assertArrayHasKey('metadata', $entry);
|
||||
$metadata = self::decodePayload($entry['metadata']);
|
||||
self::assertArrayHasKey('correlation_id', $metadata);
|
||||
self::assertArrayHasKey('occurred_at', $metadata);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handleNoteModifieeWritesAuditEntryWithDiff(): void
|
||||
{
|
||||
$gradeId = GradeId::generate();
|
||||
$evaluationId = Uuid::uuid4()->toString();
|
||||
$studentId = Uuid::uuid4()->toString();
|
||||
$modifiedBy = Uuid::uuid4()->toString();
|
||||
|
||||
$event = new NoteModifiee(
|
||||
gradeId: $gradeId,
|
||||
evaluationId: $evaluationId,
|
||||
studentId: $studentId,
|
||||
oldValue: 12.0,
|
||||
newValue: 14.5,
|
||||
oldStatus: 'draft',
|
||||
newStatus: 'published',
|
||||
modifiedBy: $modifiedBy,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->handler->handleNoteModifiee($event);
|
||||
|
||||
$entry = $this->connection->fetchAssociative(
|
||||
'SELECT * FROM audit_log WHERE aggregate_id = ? AND event_type = ? ORDER BY occurred_at DESC LIMIT 1',
|
||||
[$gradeId->value->toString(), 'NoteModifiee'],
|
||||
);
|
||||
|
||||
self::assertNotFalse($entry, 'Audit log entry should exist after NoteModifiee');
|
||||
self::assertSame('Grade', $entry['aggregate_type']);
|
||||
|
||||
$payload = self::decodePayload($entry['payload']);
|
||||
self::assertSame(['value' => 12.0, 'status' => 'draft'], $payload['old_values']);
|
||||
self::assertSame(14.5, $payload['new_values']['value']);
|
||||
self::assertSame('published', $payload['new_values']['status']);
|
||||
self::assertSame($modifiedBy, $payload['new_values']['modified_by']);
|
||||
self::assertSame($evaluationId, $payload['new_values']['evaluation_id']);
|
||||
self::assertSame($studentId, $payload['new_values']['student_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function decodePayload(mixed $raw): array
|
||||
{
|
||||
self::assertIsString($raw);
|
||||
|
||||
/** @var array<string, mixed> $decoded */
|
||||
$decoded = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,10 @@ final class EvaluationTest extends TestCase
|
||||
self::assertCount(1, $events);
|
||||
self::assertInstanceOf(EvaluationCreee::class, $events[0]);
|
||||
self::assertSame($evaluation->id, $events[0]->evaluationId);
|
||||
self::assertSame($evaluation->title, $events[0]->title);
|
||||
self::assertSame($evaluation->description, $events[0]->description);
|
||||
self::assertSame($evaluation->gradeScale->maxValue, $events[0]->gradeScale);
|
||||
self::assertSame($evaluation->coefficient->value, $events[0]->coefficient);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
@@ -136,6 +140,16 @@ final class EvaluationTest extends TestCase
|
||||
self::assertCount(1, $events);
|
||||
self::assertInstanceOf(EvaluationModifiee::class, $events[0]);
|
||||
self::assertSame($evaluation->id, $events[0]->evaluationId);
|
||||
self::assertSame('Contrôle chapitre 5', $events[0]->oldTitle);
|
||||
self::assertSame('Titre modifié', $events[0]->newTitle);
|
||||
self::assertSame('Évaluation sur les fonctions', $events[0]->oldDescription);
|
||||
self::assertSame('Nouvelle description', $events[0]->newDescription);
|
||||
self::assertSame(1.0, $events[0]->oldCoefficient);
|
||||
self::assertSame(2.0, $events[0]->newCoefficient);
|
||||
self::assertEquals(new DateTimeImmutable('2026-04-15'), $events[0]->oldEvaluationDate);
|
||||
self::assertEquals($newDate, $events[0]->newEvaluationDate);
|
||||
self::assertSame(20, $events[0]->oldGradeScale);
|
||||
self::assertSame(20, $events[0]->newGradeScale);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
|
||||
@@ -179,6 +179,7 @@ final class GradeTest extends TestCase
|
||||
$events = $grade->pullDomainEvents();
|
||||
self::assertCount(1, $events);
|
||||
self::assertInstanceOf(NoteModifiee::class, $events[0]);
|
||||
self::assertSame(self::STUDENT_ID, $events[0]->studentId);
|
||||
self::assertSame(15.5, $events[0]->oldValue);
|
||||
self::assertSame(18.0, $events[0]->newValue);
|
||||
self::assertSame('graded', $events[0]->oldStatus);
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Scolarite\Domain\Event\EvaluationCreee;
|
||||
use App\Scolarite\Domain\Event\EvaluationModifiee;
|
||||
use App\Scolarite\Domain\Event\EvaluationSupprimee;
|
||||
use App\Scolarite\Domain\Event\NotesPubliees;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Infrastructure\EventHandler\AuditEvaluationEventsHandler;
|
||||
use App\Shared\Application\Port\AuditLogger;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
final class AuditEvaluationEventsHandlerTest extends TestCase
|
||||
{
|
||||
private AuditLogger&MockObject $auditLogger;
|
||||
private AuditEvaluationEventsHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->auditLogger = $this->createMock(AuditLogger::class);
|
||||
$this->handler = new AuditEvaluationEventsHandler($this->auditLogger);
|
||||
}
|
||||
|
||||
public function testHandleEvaluationCreeeLogsAuditEntryWithFullPayload(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
$classId = Uuid::uuid4()->toString();
|
||||
$subjectId = Uuid::uuid4()->toString();
|
||||
$teacherId = Uuid::uuid4()->toString();
|
||||
$evaluationDate = new DateTimeImmutable('2026-04-15');
|
||||
|
||||
$event = new EvaluationCreee(
|
||||
evaluationId: $evaluationId,
|
||||
classId: $classId,
|
||||
subjectId: $subjectId,
|
||||
teacherId: $teacherId,
|
||||
title: 'Contrôle chapitre 3',
|
||||
description: 'Sur le chapitre 3 uniquement',
|
||||
evaluationDate: $evaluationDate,
|
||||
gradeScale: 20,
|
||||
coefficient: 2.0,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->auditLogger->expects($this->once())
|
||||
->method('logDataChange')
|
||||
->with(
|
||||
$this->equalTo('Evaluation'),
|
||||
$this->callback(static fn ($uuid) => $uuid->toString() === $evaluationId->value->toString()),
|
||||
$this->equalTo('EvaluationCreee'),
|
||||
$this->equalTo([]),
|
||||
$this->callback(static fn ($new) => $new['title'] === 'Contrôle chapitre 3'
|
||||
&& $new['description'] === 'Sur le chapitre 3 uniquement'
|
||||
&& $new['class_id'] === $classId
|
||||
&& $new['subject_id'] === $subjectId
|
||||
&& $new['teacher_id'] === $teacherId
|
||||
&& $new['evaluation_date'] === '2026-04-15'
|
||||
&& $new['grade_scale'] === 20
|
||||
&& $new['coefficient'] === 2.0
|
||||
),
|
||||
);
|
||||
|
||||
$this->handler->handleEvaluationCreee($event);
|
||||
}
|
||||
|
||||
public function testHandleEvaluationModifieeLogsAuditEntryWithDiff(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
|
||||
$event = new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
oldTitle: 'Ancien titre',
|
||||
newTitle: 'Nouveau titre',
|
||||
oldDescription: 'Ancienne description',
|
||||
newDescription: 'Nouvelle description',
|
||||
oldCoefficient: 1.0,
|
||||
newCoefficient: 2.0,
|
||||
oldEvaluationDate: new DateTimeImmutable('2026-04-15'),
|
||||
newEvaluationDate: new DateTimeImmutable('2026-05-01'),
|
||||
oldGradeScale: 20,
|
||||
newGradeScale: 100,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->auditLogger->expects($this->once())
|
||||
->method('logDataChange')
|
||||
->with(
|
||||
$this->equalTo('Evaluation'),
|
||||
$this->callback(static fn ($uuid) => $uuid->toString() === $evaluationId->value->toString()),
|
||||
$this->equalTo('EvaluationModifiee'),
|
||||
$this->callback(static fn ($old) => $old['title'] === 'Ancien titre'
|
||||
&& $old['description'] === 'Ancienne description'
|
||||
&& $old['coefficient'] === 1.0
|
||||
&& $old['evaluation_date'] === '2026-04-15'
|
||||
&& $old['grade_scale'] === 20
|
||||
),
|
||||
$this->callback(static fn ($new) => $new['title'] === 'Nouveau titre'
|
||||
&& $new['description'] === 'Nouvelle description'
|
||||
&& $new['coefficient'] === 2.0
|
||||
&& $new['evaluation_date'] === '2026-05-01'
|
||||
&& $new['grade_scale'] === 100
|
||||
),
|
||||
);
|
||||
|
||||
$this->handler->handleEvaluationModifiee($event);
|
||||
}
|
||||
|
||||
public function testHandleEvaluationSupprimeeLogsAuditEntry(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
|
||||
$event = new EvaluationSupprimee(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->auditLogger->expects($this->once())
|
||||
->method('logDataChange')
|
||||
->with(
|
||||
$this->equalTo('Evaluation'),
|
||||
$this->callback(static fn ($uuid) => $uuid->toString() === $evaluationId->value->toString()),
|
||||
$this->equalTo('EvaluationSupprimee'),
|
||||
$this->equalTo([]),
|
||||
$this->equalTo([]),
|
||||
);
|
||||
|
||||
$this->handler->handleEvaluationSupprimee($event);
|
||||
}
|
||||
|
||||
public function testHandleNotesPublieesLogsAuditEntry(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
|
||||
$event = new NotesPubliees(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->auditLogger->expects($this->once())
|
||||
->method('logDataChange')
|
||||
->with(
|
||||
$this->equalTo('Evaluation'),
|
||||
$this->callback(static fn ($uuid) => $uuid->toString() === $evaluationId->value->toString()),
|
||||
$this->equalTo('NotesPubliees'),
|
||||
$this->equalTo([]),
|
||||
$this->equalTo([]),
|
||||
);
|
||||
|
||||
$this->handler->handleNotesPubliees($event);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Scolarite\Domain\Event\NoteModifiee;
|
||||
use App\Scolarite\Domain\Event\NoteSaisie;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeId;
|
||||
use App\Scolarite\Infrastructure\EventHandler\AuditGradeEventsHandler;
|
||||
use App\Shared\Application\Port\AuditLogger;
|
||||
|
||||
use function array_key_exists;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
final class AuditGradeEventsHandlerTest extends TestCase
|
||||
{
|
||||
private AuditLogger&MockObject $auditLogger;
|
||||
private AuditGradeEventsHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->auditLogger = $this->createMock(AuditLogger::class);
|
||||
$this->handler = new AuditGradeEventsHandler($this->auditLogger);
|
||||
}
|
||||
|
||||
public function testHandleNoteSaisieLogsAuditEntryWithCreationPayload(): void
|
||||
{
|
||||
$gradeId = GradeId::generate();
|
||||
$evaluationId = Uuid::uuid4()->toString();
|
||||
$studentId = Uuid::uuid4()->toString();
|
||||
$createdBy = Uuid::uuid4()->toString();
|
||||
|
||||
$event = new NoteSaisie(
|
||||
gradeId: $gradeId,
|
||||
evaluationId: $evaluationId,
|
||||
studentId: $studentId,
|
||||
value: 15.5,
|
||||
status: 'draft',
|
||||
createdBy: $createdBy,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->auditLogger->expects($this->once())
|
||||
->method('logDataChange')
|
||||
->with(
|
||||
$this->equalTo('Grade'),
|
||||
$this->callback(static fn ($uuid) => $uuid->toString() === $gradeId->value->toString()),
|
||||
$this->equalTo('NoteSaisie'),
|
||||
$this->equalTo([]),
|
||||
$this->callback(static fn ($new) => $new['evaluation_id'] === $evaluationId
|
||||
&& $new['student_id'] === $studentId
|
||||
&& $new['value'] === 15.5
|
||||
&& $new['status'] === 'draft'
|
||||
&& $new['created_by'] === $createdBy
|
||||
),
|
||||
);
|
||||
|
||||
$this->handler->handleNoteSaisie($event);
|
||||
}
|
||||
|
||||
public function testHandleNoteSaisieSupportsNullValue(): void
|
||||
{
|
||||
$event = new NoteSaisie(
|
||||
gradeId: GradeId::generate(),
|
||||
evaluationId: Uuid::uuid4()->toString(),
|
||||
studentId: Uuid::uuid4()->toString(),
|
||||
value: null,
|
||||
status: 'absent',
|
||||
createdBy: Uuid::uuid4()->toString(),
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->auditLogger->expects($this->once())
|
||||
->method('logDataChange')
|
||||
->with(
|
||||
$this->equalTo('Grade'),
|
||||
$this->anything(),
|
||||
$this->equalTo('NoteSaisie'),
|
||||
$this->equalTo([]),
|
||||
$this->callback(static fn ($new) => $new['value'] === null
|
||||
&& $new['status'] === 'absent'
|
||||
),
|
||||
);
|
||||
|
||||
$this->handler->handleNoteSaisie($event);
|
||||
}
|
||||
|
||||
public function testHandleNoteModifieeLogsAuditEntryWithDiff(): void
|
||||
{
|
||||
$gradeId = GradeId::generate();
|
||||
$evaluationId = Uuid::uuid4()->toString();
|
||||
$studentId = Uuid::uuid4()->toString();
|
||||
$modifiedBy = Uuid::uuid4()->toString();
|
||||
|
||||
$event = new NoteModifiee(
|
||||
gradeId: $gradeId,
|
||||
evaluationId: $evaluationId,
|
||||
studentId: $studentId,
|
||||
oldValue: 12.0,
|
||||
newValue: 14.5,
|
||||
oldStatus: 'draft',
|
||||
newStatus: 'published',
|
||||
modifiedBy: $modifiedBy,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->auditLogger->expects($this->once())
|
||||
->method('logDataChange')
|
||||
->with(
|
||||
$this->equalTo('Grade'),
|
||||
$this->callback(static fn ($uuid) => $uuid->toString() === $gradeId->value->toString()),
|
||||
$this->equalTo('NoteModifiee'),
|
||||
$this->callback(static fn ($old) => $old['value'] === 12.0
|
||||
&& $old['status'] === 'draft'
|
||||
),
|
||||
$this->callback(static fn ($new) => $new['value'] === 14.5
|
||||
&& $new['status'] === 'published'
|
||||
&& $new['modified_by'] === $modifiedBy
|
||||
&& $new['evaluation_id'] === $evaluationId
|
||||
&& $new['student_id'] === $studentId
|
||||
),
|
||||
);
|
||||
|
||||
$this->handler->handleNoteModifiee($event);
|
||||
}
|
||||
|
||||
public function testHandleNoteModifieeSupportsNullValues(): void
|
||||
{
|
||||
$event = new NoteModifiee(
|
||||
gradeId: GradeId::generate(),
|
||||
evaluationId: Uuid::uuid4()->toString(),
|
||||
studentId: Uuid::uuid4()->toString(),
|
||||
oldValue: 10.0,
|
||||
newValue: null,
|
||||
oldStatus: 'published',
|
||||
newStatus: 'absent',
|
||||
modifiedBy: Uuid::uuid4()->toString(),
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->auditLogger->expects($this->once())
|
||||
->method('logDataChange')
|
||||
->with(
|
||||
$this->equalTo('Grade'),
|
||||
$this->anything(),
|
||||
$this->equalTo('NoteModifiee'),
|
||||
$this->callback(static fn ($old) => $old['value'] === 10.0),
|
||||
$this->callback(static fn ($new) => $new['value'] === null
|
||||
&& $new['status'] === 'absent'
|
||||
),
|
||||
);
|
||||
|
||||
$this->handler->handleNoteModifiee($event);
|
||||
}
|
||||
|
||||
public function testHandleNoteSaisieSupportsZeroValue(): void
|
||||
{
|
||||
$event = new NoteSaisie(
|
||||
gradeId: GradeId::generate(),
|
||||
evaluationId: Uuid::uuid4()->toString(),
|
||||
studentId: Uuid::uuid4()->toString(),
|
||||
value: 0.0,
|
||||
status: 'published',
|
||||
createdBy: Uuid::uuid4()->toString(),
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
);
|
||||
|
||||
$this->auditLogger->expects($this->once())
|
||||
->method('logDataChange')
|
||||
->with(
|
||||
$this->equalTo('Grade'),
|
||||
$this->anything(),
|
||||
$this->equalTo('NoteSaisie'),
|
||||
$this->equalTo([]),
|
||||
$this->callback(static fn ($new) => array_key_exists('value', $new)
|
||||
&& $new['value'] === 0.0
|
||||
&& $new['status'] === 'published'
|
||||
),
|
||||
);
|
||||
|
||||
$this->handler->handleNoteSaisie($event);
|
||||
}
|
||||
}
|
||||
@@ -100,8 +100,16 @@ final class RecalculerMoyennesOnEvaluationModifieeHandlerTest extends TestCase
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldTitle: 'Test Evaluation',
|
||||
newTitle: 'Titre modifié',
|
||||
oldDescription: null,
|
||||
newDescription: null,
|
||||
oldCoefficient: 1.0,
|
||||
newCoefficient: 1.0,
|
||||
oldEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
newEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldGradeScale: 20,
|
||||
newGradeScale: 20,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
@@ -126,8 +134,16 @@ final class RecalculerMoyennesOnEvaluationModifieeHandlerTest extends TestCase
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldTitle: 'Test Evaluation',
|
||||
newTitle: 'Titre modifié',
|
||||
oldDescription: null,
|
||||
newDescription: null,
|
||||
oldCoefficient: 1.0,
|
||||
newCoefficient: 1.0,
|
||||
oldEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
newEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldGradeScale: 20,
|
||||
newGradeScale: 20,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
@@ -163,8 +179,16 @@ final class RecalculerMoyennesOnEvaluationModifieeHandlerTest extends TestCase
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldTitle: 'Test Evaluation',
|
||||
newTitle: 'Titre modifié',
|
||||
oldDescription: null,
|
||||
newDescription: null,
|
||||
oldCoefficient: 1.0,
|
||||
newCoefficient: 1.0,
|
||||
oldEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
newEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldGradeScale: 20,
|
||||
newGradeScale: 20,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
@@ -188,8 +212,16 @@ final class RecalculerMoyennesOnEvaluationModifieeHandlerTest extends TestCase
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldTitle: 'Test Evaluation',
|
||||
newTitle: 'Titre modifié',
|
||||
oldDescription: null,
|
||||
newDescription: null,
|
||||
oldCoefficient: 1.0,
|
||||
newCoefficient: 1.0,
|
||||
oldEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
newEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldGradeScale: 20,
|
||||
newGradeScale: 20,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
@@ -219,8 +251,16 @@ final class RecalculerMoyennesOnEvaluationModifieeHandlerTest extends TestCase
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldTitle: 'Test Evaluation',
|
||||
newTitle: 'Titre modifié',
|
||||
oldDescription: null,
|
||||
newDescription: null,
|
||||
oldCoefficient: 1.0,
|
||||
newCoefficient: 1.0,
|
||||
oldEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
newEvaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
oldGradeScale: 20,
|
||||
newGradeScale: 20,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
|
||||
@@ -118,6 +118,7 @@ final class RecalculerMoyennesOnNoteModifieeHandlerTest extends TestCase
|
||||
$event = new NoteModifiee(
|
||||
gradeId: $grade1->id,
|
||||
evaluationId: (string) $evaluation->id,
|
||||
studentId: self::STUDENT_ID,
|
||||
oldValue: 14.0,
|
||||
newValue: 18.0,
|
||||
oldStatus: 'graded',
|
||||
@@ -174,6 +175,7 @@ final class RecalculerMoyennesOnNoteModifieeHandlerTest extends TestCase
|
||||
$event = new NoteModifiee(
|
||||
gradeId: $grade->id,
|
||||
evaluationId: (string) $evaluation->id,
|
||||
studentId: self::STUDENT_ID,
|
||||
oldValue: 10.0,
|
||||
newValue: 14.0,
|
||||
oldStatus: 'graded',
|
||||
@@ -233,6 +235,7 @@ final class RecalculerMoyennesOnNoteModifieeHandlerTest extends TestCase
|
||||
$event = new NoteModifiee(
|
||||
gradeId: GradeId::generate(),
|
||||
evaluationId: (string) $evaluation->id,
|
||||
studentId: self::STUDENT_ID,
|
||||
oldValue: 10.0,
|
||||
newValue: 14.0,
|
||||
oldStatus: 'graded',
|
||||
@@ -264,6 +267,7 @@ final class RecalculerMoyennesOnNoteModifieeHandlerTest extends TestCase
|
||||
$event = new NoteModifiee(
|
||||
gradeId: GradeId::generate(),
|
||||
evaluationId: (string) $unknownEvalId,
|
||||
studentId: self::STUDENT_ID,
|
||||
oldValue: 10.0,
|
||||
newValue: 14.0,
|
||||
oldStatus: 'graded',
|
||||
|
||||
Reference in New Issue
Block a user