Files
Classeo/backend/tests/Unit/Administration/Application/Query/CheckImageRights/CheckImageRightsHandlerTest.php
Mathias STRASSER 1b8bd6cd78 feat: Permettre la consultation et gestion des droits à l'image des élèves
Les administrateurs et enseignants ont besoin de consulter et gérer
les autorisations de droit à l'image des élèves pour respecter
la réglementation lors de publications contenant des photos (FR82).

Cette fonctionnalité ajoute une page dédiée avec liste filtrable
par statut, modification individuelle via dropdown, export CSV
avec BOM UTF-8 pour Excel, et préparation du système d'avertissement
avant publication (query/handler prêts, intégration à faire quand
le module publication existera).

Le filtrage par classe (AC2) est bloqué en attente d'une table
d'affectation élève↔classe qui n'existe pas encore.
2026-02-19 14:44:52 +01:00

137 lines
4.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Unit\Administration\Application\Query\CheckImageRights;
use App\Administration\Application\Query\CheckImageRights\CheckImageRightsHandler;
use App\Administration\Application\Query\CheckImageRights\CheckImageRightsQuery;
use App\Administration\Domain\Exception\UserNotFoundException;
use App\Administration\Domain\Model\User\Email;
use App\Administration\Domain\Model\User\ImageRightsStatus;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\User;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryUserRepository;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class CheckImageRightsHandlerTest extends TestCase
{
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
private InMemoryUserRepository $userRepository;
private CheckImageRightsHandler $handler;
protected function setUp(): void
{
$this->userRepository = new InMemoryUserRepository();
$this->handler = new CheckImageRightsHandler($this->userRepository);
}
#[Test]
public function authorizedStudentCanPublish(): void
{
$student = $this->createStudentWithStatus(ImageRightsStatus::AUTHORIZED);
$query = new CheckImageRightsQuery(
studentId: (string) $student->id,
tenantId: self::TENANT_ID,
);
$result = ($this->handler)($query);
self::assertTrue($result->canPublish);
self::assertNull($result->warningMessage);
self::assertSame(ImageRightsStatus::AUTHORIZED, $result->status);
}
#[Test]
public function refusedStudentCannotPublish(): void
{
$student = $this->createStudentWithStatus(ImageRightsStatus::REFUSED);
$query = new CheckImageRightsQuery(
studentId: (string) $student->id,
tenantId: self::TENANT_ID,
);
$result = ($this->handler)($query);
self::assertFalse($result->canPublish);
self::assertNotNull($result->warningMessage);
self::assertStringContainsString('PAS l\'autorisation', $result->warningMessage);
}
#[Test]
public function notSpecifiedStudentCannotPublish(): void
{
$student = $this->createStudentWithStatus(ImageRightsStatus::NOT_SPECIFIED);
$query = new CheckImageRightsQuery(
studentId: (string) $student->id,
tenantId: self::TENANT_ID,
);
$result = ($this->handler)($query);
self::assertFalse($result->canPublish);
self::assertNotNull($result->warningMessage);
self::assertStringContainsString('pas renseigné', $result->warningMessage);
}
#[Test]
public function throwsWhenStudentNotFound(): void
{
$this->expectException(UserNotFoundException::class);
$query = new CheckImageRightsQuery(
studentId: '550e8400-e29b-41d4-a716-446655440000',
tenantId: self::TENANT_ID,
);
($this->handler)($query);
}
#[Test]
public function throwsWhenTenantMismatch(): void
{
$student = $this->createStudentWithStatus(ImageRightsStatus::AUTHORIZED);
$this->expectException(UserNotFoundException::class);
$query = new CheckImageRightsQuery(
studentId: (string) $student->id,
tenantId: '550e8400-e29b-41d4-a716-446655440099',
);
($this->handler)($query);
}
private function createStudentWithStatus(ImageRightsStatus $status): User
{
$student = User::creer(
email: new Email('eleve@example.com'),
role: Role::ELEVE,
tenantId: TenantId::fromString(self::TENANT_ID),
schoolName: 'École Alpha',
dateNaissance: new DateTimeImmutable('2012-06-15'),
createdAt: new DateTimeImmutable('2026-01-15 10:00:00'),
);
if ($status !== ImageRightsStatus::NOT_SPECIFIED) {
$student->modifierDroitImage(
$status,
UserId::fromString('550e8400-e29b-41d4-a716-446655440099'),
new DateTimeImmutable(),
);
}
$this->userRepository->save($student);
return $student;
}
}