feat: Permettre la génération et l'envoi de codes d'invitation aux parents

Les administrateurs ont besoin d'un moyen simple pour inviter les parents
à rejoindre la plateforme. Cette fonctionnalité permet de générer des codes
d'invitation uniques (8 caractères alphanumériques) avec une validité de
48h, de les envoyer par email, et de les activer via une page publique
dédiée qui crée automatiquement le compte parent.

L'interface d'administration offre l'envoi unitaire et en masse, le renvoi,
le filtrage par statut, ainsi que la visualisation de l'état de chaque
invitation (en attente, activée, expirée).
This commit is contained in:
2026-02-28 00:08:56 +01:00
parent de5880e25e
commit be1b0b60a6
68 changed files with 8787 additions and 1 deletions

View File

@@ -0,0 +1,208 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Administration\Application\Command\ActivateParentInvitation;
use App\Administration\Application\Command\ActivateParentInvitation\ActivateParentInvitationCommand;
use App\Administration\Application\Command\ActivateParentInvitation\ActivateParentInvitationHandler;
use App\Administration\Application\Port\PasswordHasher;
use App\Administration\Domain\Exception\InvitationDejaActiveeException;
use App\Administration\Domain\Exception\InvitationExpireeException;
use App\Administration\Domain\Exception\InvitationNonEnvoyeeException;
use App\Administration\Domain\Exception\ParentInvitationNotFoundException;
use App\Administration\Domain\Model\Invitation\InvitationCode;
use App\Administration\Domain\Model\Invitation\ParentInvitation;
use App\Administration\Domain\Model\User\Email;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryParentInvitationRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class ActivateParentInvitationHandlerTest extends TestCase
{
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
private const string CODE = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
private InMemoryParentInvitationRepository $repository;
private ActivateParentInvitationHandler $handler;
protected function setUp(): void
{
$this->repository = new InMemoryParentInvitationRepository();
$clock = new class implements Clock {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2026-02-08 10:00:00');
}
};
$passwordHasher = new class implements PasswordHasher {
public function hash(string $plainPassword): string
{
return 'hashed_' . $plainPassword;
}
public function verify(string $hashedPassword, string $plainPassword): bool
{
return $hashedPassword === 'hashed_' . $plainPassword;
}
};
$this->handler = new ActivateParentInvitationHandler(
$this->repository,
$passwordHasher,
$clock,
);
}
#[Test]
public function itValidatesAndReturnsActivationResult(): void
{
$invitation = $this->createSentInvitation();
$result = ($this->handler)(new ActivateParentInvitationCommand(
code: self::CODE,
firstName: 'Jean',
lastName: 'Parent',
password: 'SecurePass123!',
));
self::assertSame((string) $invitation->id, $result->invitationId);
self::assertSame((string) $invitation->studentId, $result->studentId);
self::assertSame('parent@example.com', $result->parentEmail);
self::assertSame('hashed_SecurePass123!', $result->hashedPassword);
self::assertSame('Jean', $result->firstName);
self::assertSame('Parent', $result->lastName);
}
#[Test]
public function itThrowsWhenCodeNotFound(): void
{
$this->expectException(ParentInvitationNotFoundException::class);
($this->handler)(new ActivateParentInvitationCommand(
code: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
firstName: 'Jean',
lastName: 'Parent',
password: 'SecurePass123!',
));
}
#[Test]
public function itThrowsWhenInvitationNotSent(): void
{
$this->createPendingInvitation();
$this->expectException(InvitationNonEnvoyeeException::class);
($this->handler)(new ActivateParentInvitationCommand(
code: self::CODE,
firstName: 'Jean',
lastName: 'Parent',
password: 'SecurePass123!',
));
}
#[Test]
public function itThrowsWhenInvitationExpired(): void
{
$this->createExpiredInvitation();
$this->expectException(InvitationExpireeException::class);
($this->handler)(new ActivateParentInvitationCommand(
code: self::CODE,
firstName: 'Jean',
lastName: 'Parent',
password: 'SecurePass123!',
));
}
#[Test]
public function itThrowsWhenInvitationAlreadyActivated(): void
{
$this->createActivatedInvitation();
$this->expectException(InvitationDejaActiveeException::class);
($this->handler)(new ActivateParentInvitationCommand(
code: self::CODE,
firstName: 'Jean',
lastName: 'Parent',
password: 'SecurePass123!',
));
}
private function createSentInvitation(): ParentInvitation
{
$invitation = ParentInvitation::creer(
tenantId: TenantId::fromString(self::TENANT_ID),
studentId: UserId::generate(),
parentEmail: new Email('parent@example.com'),
code: new InvitationCode(self::CODE),
createdAt: new DateTimeImmutable('2026-02-07 10:00:00'),
createdBy: UserId::generate(),
);
$invitation->envoyer(new DateTimeImmutable('2026-02-07 10:00:00'));
$invitation->pullDomainEvents();
$this->repository->save($invitation);
return $invitation;
}
private function createPendingInvitation(): ParentInvitation
{
$invitation = ParentInvitation::creer(
tenantId: TenantId::fromString(self::TENANT_ID),
studentId: UserId::generate(),
parentEmail: new Email('parent@example.com'),
code: new InvitationCode(self::CODE),
createdAt: new DateTimeImmutable('2026-02-07 10:00:00'),
createdBy: UserId::generate(),
);
$invitation->pullDomainEvents();
$this->repository->save($invitation);
return $invitation;
}
private function createExpiredInvitation(): ParentInvitation
{
$invitation = ParentInvitation::creer(
tenantId: TenantId::fromString(self::TENANT_ID),
studentId: UserId::generate(),
parentEmail: new Email('parent@example.com'),
code: new InvitationCode(self::CODE),
createdAt: new DateTimeImmutable('2026-01-01 10:00:00'),
createdBy: UserId::generate(),
);
$invitation->envoyer(new DateTimeImmutable('2026-01-01 10:00:00'));
$invitation->pullDomainEvents();
$this->repository->save($invitation);
return $invitation;
}
private function createActivatedInvitation(): ParentInvitation
{
$invitation = ParentInvitation::creer(
tenantId: TenantId::fromString(self::TENANT_ID),
studentId: UserId::generate(),
parentEmail: new Email('parent@example.com'),
code: new InvitationCode(self::CODE),
createdAt: new DateTimeImmutable('2026-02-07 10:00:00'),
createdBy: UserId::generate(),
);
$invitation->envoyer(new DateTimeImmutable('2026-02-07 10:00:00'));
$invitation->activer(UserId::generate(), new DateTimeImmutable('2026-02-07 12:00:00'));
$invitation->pullDomainEvents();
$this->repository->save($invitation);
return $invitation;
}
}

View File

@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Administration\Application\Command\ResendParentInvitation;
use App\Administration\Application\Command\ResendParentInvitation\ResendParentInvitationCommand;
use App\Administration\Application\Command\ResendParentInvitation\ResendParentInvitationHandler;
use App\Administration\Application\Service\InvitationCodeGenerator;
use App\Administration\Domain\Model\Invitation\InvitationCode;
use App\Administration\Domain\Model\Invitation\InvitationStatus;
use App\Administration\Domain\Model\Invitation\ParentInvitation;
use App\Administration\Domain\Model\User\Email;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryParentInvitationRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class ResendParentInvitationHandlerTest extends TestCase
{
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
private InMemoryParentInvitationRepository $repository;
private ResendParentInvitationHandler $handler;
protected function setUp(): void
{
$this->repository = new InMemoryParentInvitationRepository();
$clock = new class implements Clock {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2026-02-14 10:00:00');
}
};
$this->handler = new ResendParentInvitationHandler(
$this->repository,
new InvitationCodeGenerator(),
$clock,
);
}
#[Test]
public function itResendsInvitationWithNewCode(): void
{
$invitation = $this->createSentInvitation();
$oldCode = (string) $invitation->code;
$result = ($this->handler)(new ResendParentInvitationCommand(
invitationId: (string) $invitation->id,
tenantId: self::TENANT_ID,
));
self::assertSame(InvitationStatus::SENT, $result->status);
self::assertNotSame($oldCode, (string) $result->code);
}
#[Test]
public function itUpdatesExpirationDate(): void
{
$invitation = $this->createSentInvitation();
$oldExpiresAt = $invitation->expiresAt;
$result = ($this->handler)(new ResendParentInvitationCommand(
invitationId: (string) $invitation->id,
tenantId: self::TENANT_ID,
));
self::assertGreaterThan($oldExpiresAt, $result->expiresAt);
}
private function createSentInvitation(): ParentInvitation
{
$invitation = ParentInvitation::creer(
tenantId: TenantId::fromString(self::TENANT_ID),
studentId: UserId::generate(),
parentEmail: new Email('parent@example.com'),
code: new InvitationCode(str_repeat('a', 32)),
createdAt: new DateTimeImmutable('2026-02-07 10:00:00'),
createdBy: UserId::generate(),
);
$invitation->envoyer(new DateTimeImmutable('2026-02-07 10:00:00'));
$invitation->pullDomainEvents();
$this->repository->save($invitation);
return $invitation;
}
}

View File

@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Administration\Application\Command\SendParentInvitation;
use App\Administration\Application\Command\SendParentInvitation\SendParentInvitationCommand;
use App\Administration\Application\Command\SendParentInvitation\SendParentInvitationHandler;
use App\Administration\Application\Service\InvitationCodeGenerator;
use App\Administration\Domain\Event\InvitationParentEnvoyee;
use App\Administration\Domain\Model\Invitation\InvitationStatus;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\StatutCompte;
use App\Administration\Domain\Model\User\User;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryParentInvitationRepository;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryUserRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class SendParentInvitationHandlerTest extends TestCase
{
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
private InMemoryParentInvitationRepository $repository;
private InMemoryUserRepository $userRepository;
private SendParentInvitationHandler $handler;
private string $studentId;
protected function setUp(): void
{
$this->repository = new InMemoryParentInvitationRepository();
$this->userRepository = new InMemoryUserRepository();
$this->studentId = (string) UserId::generate();
$student = User::reconstitute(
id: UserId::fromString($this->studentId),
email: null,
roles: [Role::ELEVE],
tenantId: TenantId::fromString(self::TENANT_ID),
schoolName: 'Test School',
statut: StatutCompte::INSCRIT,
dateNaissance: null,
createdAt: new DateTimeImmutable('2026-01-01'),
hashedPassword: null,
activatedAt: null,
consentementParental: null,
firstName: 'Camille',
lastName: 'Test',
);
$this->userRepository->save($student);
$clock = new class implements Clock {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2026-02-07 10:00:00');
}
};
$this->handler = new SendParentInvitationHandler(
$this->repository,
$this->userRepository,
new InvitationCodeGenerator(),
$clock,
);
}
#[Test]
public function itCreatesAndSendsInvitation(): void
{
$createdBy = (string) UserId::generate();
$invitation = ($this->handler)(new SendParentInvitationCommand(
tenantId: self::TENANT_ID,
studentId: $this->studentId,
parentEmail: 'parent@example.com',
createdBy: $createdBy,
));
self::assertSame(InvitationStatus::SENT, $invitation->status);
self::assertSame('parent@example.com', (string) $invitation->parentEmail);
self::assertSame($this->studentId, (string) $invitation->studentId);
self::assertNotNull($invitation->sentAt);
}
#[Test]
public function itPersistsTheInvitation(): void
{
$createdBy = (string) UserId::generate();
$invitation = ($this->handler)(new SendParentInvitationCommand(
tenantId: self::TENANT_ID,
studentId: $this->studentId,
parentEmail: 'parent@example.com',
createdBy: $createdBy,
));
$found = $this->repository->findById($invitation->id, TenantId::fromString(self::TENANT_ID));
self::assertNotNull($found);
self::assertSame((string) $invitation->id, (string) $found->id);
}
#[Test]
public function itRecordsInvitationSentEvent(): void
{
$createdBy = (string) UserId::generate();
$invitation = ($this->handler)(new SendParentInvitationCommand(
tenantId: self::TENANT_ID,
studentId: $this->studentId,
parentEmail: 'parent@example.com',
createdBy: $createdBy,
));
$events = $invitation->pullDomainEvents();
self::assertCount(1, $events);
self::assertInstanceOf(InvitationParentEnvoyee::class, $events[0]);
}
}