feat: Configuration du mode de notation par établissement

Les établissements scolaires utilisent des systèmes d'évaluation variés
(notes /20, /10, lettres, compétences, sans notes). Jusqu'ici l'application
imposait implicitement le mode notes /20, ce qui ne correspondait pas
à la réalité pédagogique de nombreuses écoles.

Cette configuration permet à chaque établissement de choisir son mode
de notation par année scolaire, avec verrouillage automatique dès que
des notes ont été saisies pour éviter les incohérences. Le Score Sérénité
adapte ses pondérations selon le mode choisi (les compétences sont
converties via un mapping, le mode sans notes exclut la composante notes).
This commit is contained in:
2026-02-07 01:06:55 +01:00
parent f19d0ae3ef
commit ff18850a43
51 changed files with 3963 additions and 79 deletions

View File

@@ -0,0 +1,254 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Administration\Infrastructure\Api\Processor;
use ApiPlatform\Metadata\Put;
use App\Administration\Application\Command\ConfigureGradingMode\ConfigureGradingModeHandler;
use App\Administration\Application\Port\GradeExistenceChecker;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Infrastructure\Api\Processor\ConfigureGradingModeProcessor;
use App\Administration\Infrastructure\Api\Resource\GradingModeResource;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryGradingConfigurationRepository;
use App\Administration\Infrastructure\School\SchoolIdResolver;
use App\Administration\Infrastructure\Service\CurrentAcademicYearResolver;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Tenant\TenantConfig;
use App\Shared\Infrastructure\Tenant\TenantContext;
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
use DateTimeImmutable;
use Override;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
final class ConfigureGradingModeProcessorTest extends TestCase
{
private const string TENANT_UUID = '550e8400-e29b-41d4-a716-446655440001';
private InMemoryGradingConfigurationRepository $repository;
private TenantContext $tenantContext;
private Clock $clock;
protected function setUp(): void
{
$this->repository = new InMemoryGradingConfigurationRepository();
$this->tenantContext = new TenantContext();
$this->clock = new class implements Clock {
#[Override]
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2025-10-15 10:00:00');
}
};
}
#[Test]
public function itRejectsUnauthorizedAccess(): void
{
$processor = $this->createProcessor(granted: false);
$this->setTenant();
$data = new GradingModeResource();
$data->mode = 'numeric_20';
$this->expectException(AccessDeniedHttpException::class);
$processor->process($data, new Put(), ['academicYearId' => 'current']);
}
#[Test]
public function itRejectsRequestWithoutTenant(): void
{
$processor = $this->createProcessor(granted: true);
$data = new GradingModeResource();
$data->mode = 'numeric_20';
$this->expectException(UnauthorizedHttpException::class);
$processor->process($data, new Put(), ['academicYearId' => 'current']);
}
#[Test]
public function itRejectsInvalidAcademicYearId(): void
{
$processor = $this->createProcessor(granted: true);
$this->setTenant();
$data = new GradingModeResource();
$data->mode = 'numeric_20';
$this->expectException(NotFoundHttpException::class);
$processor->process($data, new Put(), ['academicYearId' => 'invalid']);
}
#[Test]
public function itConfiguresGradingMode(): void
{
$processor = $this->createProcessor(granted: true);
$this->setTenant();
$data = new GradingModeResource();
$data->mode = 'competencies';
$result = $processor->process($data, new Put(), ['academicYearId' => 'current']);
self::assertInstanceOf(GradingModeResource::class, $result);
self::assertSame('competencies', $result->mode);
self::assertSame('Compétences', $result->label);
self::assertFalse($result->hasExistingGrades);
}
#[Test]
public function itSetsAvailableModesOnResult(): void
{
$processor = $this->createProcessor(granted: true);
$this->setTenant();
$data = new GradingModeResource();
$data->mode = 'numeric_20';
$result = $processor->process($data, new Put(), ['academicYearId' => 'current']);
self::assertNotNull($result->availableModes);
self::assertCount(5, $result->availableModes);
}
#[Test]
public function itThrowsConflictWhenGradesExistAndModeChanges(): void
{
// First configure with numeric_20 (no grades)
$processor = $this->createProcessor(granted: true, hasGrades: false);
$this->setTenant();
$data = new GradingModeResource();
$data->mode = 'numeric_20';
$processor->process($data, new Put(), ['academicYearId' => 'current']);
// Now try to change mode with grades existing
$processorWithGrades = $this->createProcessor(granted: true, hasGrades: true);
$this->setTenant();
$data = new GradingModeResource();
$data->mode = 'competencies';
$this->expectException(ConflictHttpException::class);
$processorWithGrades->process($data, new Put(), ['academicYearId' => 'current']);
}
#[Test]
public function itChangesExistingModeWhenNoGradesExist(): void
{
$processor = $this->createProcessor(granted: true);
$this->setTenant();
$data = new GradingModeResource();
$data->mode = 'numeric_20';
$processor->process($data, new Put(), ['academicYearId' => 'current']);
$data = new GradingModeResource();
$data->mode = 'letters';
$result = $processor->process($data, new Put(), ['academicYearId' => 'current']);
self::assertSame('letters', $result->mode);
}
#[Test]
public function itResolvesNextAcademicYear(): void
{
$processor = $this->createProcessor(granted: true);
$this->setTenant();
$data = new GradingModeResource();
$data->mode = 'no_grades';
$result = $processor->process($data, new Put(), ['academicYearId' => 'next']);
self::assertSame('no_grades', $result->mode);
}
#[Test]
public function itRejectsInvalidGradingModeWithBadRequest(): void
{
$processor = $this->createProcessor(granted: true);
$this->setTenant();
$data = new GradingModeResource();
$data->mode = 'invalid_mode';
$this->expectException(BadRequestHttpException::class);
$processor->process($data, new Put(), ['academicYearId' => 'current']);
}
private function setTenant(): void
{
$this->tenantContext->setCurrentTenant(new TenantConfig(
tenantId: InfraTenantId::fromString(self::TENANT_UUID),
subdomain: 'test',
databaseUrl: 'sqlite:///:memory:',
));
}
private function createProcessor(bool $granted, bool $hasGrades = false): ConfigureGradingModeProcessor
{
$authChecker = new class($granted) implements AuthorizationCheckerInterface {
public function __construct(private readonly bool $granted)
{
}
public function isGranted(mixed $attribute, mixed $subject = null, ?AccessDecision $accessDecision = null): bool
{
return $this->granted;
}
};
$eventBus = new class implements MessageBusInterface {
public function dispatch(object $message, array $stamps = []): Envelope
{
return new Envelope($message);
}
};
$gradeChecker = new class($hasGrades) implements GradeExistenceChecker {
public function __construct(private readonly bool $hasGrades)
{
}
#[Override]
public function hasGradesInPeriod(TenantId $tenantId, AcademicYearId $academicYearId, int $periodSequence): bool
{
return $this->hasGrades;
}
#[Override]
public function hasGradesForYear(TenantId $tenantId, SchoolId $schoolId, AcademicYearId $academicYearId): bool
{
return $this->hasGrades;
}
};
$handler = new ConfigureGradingModeHandler($this->repository, $gradeChecker, $this->clock);
$resolver = new CurrentAcademicYearResolver($this->tenantContext, $this->clock);
$schoolIdResolver = new SchoolIdResolver();
return new ConfigureGradingModeProcessor(
$handler,
$gradeChecker,
$this->tenantContext,
$eventBus,
$authChecker,
$resolver,
$schoolIdResolver,
);
}
}

View File

@@ -0,0 +1,226 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Administration\Infrastructure\Api\Provider;
use ApiPlatform\Metadata\Get;
use App\Administration\Application\Port\GradeExistenceChecker;
use App\Administration\Domain\Model\GradingConfiguration\GradingMode;
use App\Administration\Domain\Model\GradingConfiguration\SchoolGradingConfiguration;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Infrastructure\Api\Provider\GradingModeProvider;
use App\Administration\Infrastructure\Api\Resource\GradingModeResource;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryGradingConfigurationRepository;
use App\Administration\Infrastructure\School\SchoolIdResolver;
use App\Administration\Infrastructure\Service\CurrentAcademicYearResolver;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Tenant\TenantConfig;
use App\Shared\Infrastructure\Tenant\TenantContext;
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
use DateTimeImmutable;
use Override;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
final class GradingModeProviderTest extends TestCase
{
private const string TENANT_UUID = '550e8400-e29b-41d4-a716-446655440001';
private InMemoryGradingConfigurationRepository $repository;
private TenantContext $tenantContext;
private Clock $clock;
protected function setUp(): void
{
$this->repository = new InMemoryGradingConfigurationRepository();
$this->tenantContext = new TenantContext();
$this->clock = new class implements Clock {
#[Override]
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2025-10-15 10:00:00');
}
};
}
#[Test]
public function itRejectsUnauthorizedAccess(): void
{
$provider = $this->createProvider(granted: false);
$this->setTenant();
$this->expectException(AccessDeniedHttpException::class);
$provider->provide(new Get(), ['academicYearId' => 'current']);
}
#[Test]
public function itRejectsRequestWithoutTenant(): void
{
$provider = $this->createProvider(granted: true);
$this->expectException(UnauthorizedHttpException::class);
$provider->provide(new Get(), ['academicYearId' => 'current']);
}
#[Test]
public function itRejectsInvalidAcademicYearId(): void
{
$provider = $this->createProvider(granted: true);
$this->setTenant();
$this->expectException(NotFoundHttpException::class);
$provider->provide(new Get(), ['academicYearId' => 'invalid']);
}
#[Test]
public function itReturnsDefaultModeWhenNoConfigurationExists(): void
{
$provider = $this->createProvider(granted: true);
$this->setTenant();
$result = $provider->provide(new Get(), ['academicYearId' => 'current']);
self::assertInstanceOf(GradingModeResource::class, $result);
self::assertSame('numeric_20', $result->mode);
self::assertSame('Notes /20', $result->label);
self::assertFalse($result->hasExistingGrades);
}
#[Test]
public function itReturnsExistingConfigurationWhenPresent(): void
{
$provider = $this->createProvider(granted: true);
$this->setTenant();
$resolver = new CurrentAcademicYearResolver($this->tenantContext, $this->clock);
$academicYearId = $resolver->resolve('current');
self::assertNotNull($academicYearId);
$schoolIdResolver = new SchoolIdResolver();
$schoolId = $schoolIdResolver->resolveForTenant(self::TENANT_UUID);
$config = SchoolGradingConfiguration::configurer(
tenantId: TenantId::fromString(self::TENANT_UUID),
schoolId: SchoolId::fromString($schoolId),
academicYearId: AcademicYearId::fromString($academicYearId),
mode: GradingMode::COMPETENCIES,
hasExistingGrades: false,
configuredAt: new DateTimeImmutable(),
);
$this->repository->save($config);
$result = $provider->provide(new Get(), ['academicYearId' => 'current']);
self::assertInstanceOf(GradingModeResource::class, $result);
self::assertSame('competencies', $result->mode);
self::assertSame('Compétences', $result->label);
}
#[Test]
public function itSetsAvailableModesOnResult(): void
{
$provider = $this->createProvider(granted: true);
$this->setTenant();
$result = $provider->provide(new Get(), ['academicYearId' => 'current']);
self::assertNotNull($result->availableModes);
self::assertCount(5, $result->availableModes);
}
#[Test]
public function itSetsHasExistingGradesFlag(): void
{
$provider = $this->createProvider(granted: true, hasGrades: true);
$this->setTenant();
$result = $provider->provide(new Get(), ['academicYearId' => 'current']);
self::assertTrue($result->hasExistingGrades);
}
#[Test]
public function itResolvesNextAcademicYear(): void
{
$provider = $this->createProvider(granted: true);
$this->setTenant();
$result = $provider->provide(new Get(), ['academicYearId' => 'next']);
self::assertInstanceOf(GradingModeResource::class, $result);
self::assertSame('numeric_20', $result->mode);
}
#[Test]
public function itResolvesPreviousAcademicYear(): void
{
$provider = $this->createProvider(granted: true);
$this->setTenant();
$result = $provider->provide(new Get(), ['academicYearId' => 'previous']);
self::assertInstanceOf(GradingModeResource::class, $result);
self::assertSame('numeric_20', $result->mode);
}
private function setTenant(): void
{
$this->tenantContext->setCurrentTenant(new TenantConfig(
tenantId: InfraTenantId::fromString(self::TENANT_UUID),
subdomain: 'test',
databaseUrl: 'sqlite:///:memory:',
));
}
private function createProvider(bool $granted, bool $hasGrades = false): GradingModeProvider
{
$authChecker = new class($granted) implements AuthorizationCheckerInterface {
public function __construct(private readonly bool $granted)
{
}
public function isGranted(mixed $attribute, mixed $subject = null, ?AccessDecision $accessDecision = null): bool
{
return $this->granted;
}
};
$gradeChecker = new class($hasGrades) implements GradeExistenceChecker {
public function __construct(private readonly bool $hasGrades)
{
}
#[Override]
public function hasGradesInPeriod(TenantId $tenantId, AcademicYearId $academicYearId, int $periodSequence): bool
{
return $this->hasGrades;
}
#[Override]
public function hasGradesForYear(TenantId $tenantId, SchoolId $schoolId, AcademicYearId $academicYearId): bool
{
return $this->hasGrades;
}
};
$resolver = new CurrentAcademicYearResolver($this->tenantContext, $this->clock);
$schoolIdResolver = new SchoolIdResolver();
return new GradingModeProvider(
$this->repository,
$gradeChecker,
$this->tenantContext,
$authChecker,
$resolver,
$schoolIdResolver,
);
}
}

View File

@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Administration\Infrastructure\Api\Resource;
use App\Administration\Domain\Model\GradingConfiguration\GradingMode;
use App\Administration\Domain\Model\GradingConfiguration\SchoolGradingConfiguration;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Infrastructure\Api\Resource\GradingModeResource;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class GradingModeResourceTest extends TestCase
{
#[Test]
public function fromDomainMapsAllFields(): void
{
$config = SchoolGradingConfiguration::configurer(
tenantId: TenantId::generate(),
schoolId: SchoolId::generate(),
academicYearId: AcademicYearId::generate(),
mode: GradingMode::NUMERIC_20,
hasExistingGrades: false,
configuredAt: new DateTimeImmutable(),
);
$resource = GradingModeResource::fromDomain($config);
self::assertSame('numeric_20', $resource->mode);
self::assertSame('Notes /20', $resource->label);
self::assertSame(20, $resource->scaleMax);
self::assertTrue($resource->isNumeric);
self::assertTrue($resource->calculatesAverage);
self::assertSame((string) $config->academicYearId, $resource->academicYearId);
}
#[Test]
public function fromDomainMapsCompetenciesMode(): void
{
$config = SchoolGradingConfiguration::configurer(
tenantId: TenantId::generate(),
schoolId: SchoolId::generate(),
academicYearId: AcademicYearId::generate(),
mode: GradingMode::COMPETENCIES,
hasExistingGrades: false,
configuredAt: new DateTimeImmutable(),
);
$resource = GradingModeResource::fromDomain($config);
self::assertSame('competencies', $resource->mode);
self::assertSame('Compétences', $resource->label);
self::assertNull($resource->scaleMax);
self::assertFalse($resource->isNumeric);
self::assertFalse($resource->calculatesAverage);
}
#[Test]
public function fromDomainMapsNoGradesMode(): void
{
$config = SchoolGradingConfiguration::configurer(
tenantId: TenantId::generate(),
schoolId: SchoolId::generate(),
academicYearId: AcademicYearId::generate(),
mode: GradingMode::NO_GRADES,
hasExistingGrades: false,
configuredAt: new DateTimeImmutable(),
);
$resource = GradingModeResource::fromDomain($config);
self::assertSame('no_grades', $resource->mode);
self::assertSame('Sans notes', $resource->label);
self::assertNull($resource->scaleMax);
self::assertFalse($resource->isNumeric);
self::assertFalse($resource->calculatesAverage);
}
#[Test]
public function defaultForYearUsesNumeric20(): void
{
$yearId = 'some-year-id';
$resource = GradingModeResource::defaultForYear($yearId);
self::assertSame($yearId, $resource->academicYearId);
self::assertSame('numeric_20', $resource->mode);
self::assertSame('Notes /20', $resource->label);
self::assertSame(20, $resource->scaleMax);
self::assertTrue($resource->isNumeric);
self::assertTrue($resource->calculatesAverage);
self::assertFalse($resource->hasExistingGrades);
}
#[Test]
public function allAvailableModesReturnsAllFiveModes(): void
{
$modes = GradingModeResource::allAvailableModes();
self::assertCount(5, $modes);
$values = array_column($modes, 'value');
self::assertContains('numeric_20', $values);
self::assertContains('numeric_10', $values);
self::assertContains('letters', $values);
self::assertContains('competencies', $values);
self::assertContains('no_grades', $values);
}
#[Test]
public function allAvailableModesContainsLabels(): void
{
$modes = GradingModeResource::allAvailableModes();
foreach ($modes as $mode) {
self::assertArrayHasKey('value', $mode);
self::assertArrayHasKey('label', $mode);
self::assertNotEmpty($mode['label']);
}
}
}