fix: don't require email address from participants under 16

This commit is contained in:
2026-09-01 15:15:58 +02:00
parent 98e0cfcf6f
commit f595183c05
11 changed files with 566 additions and 36 deletions
+56
View File
@@ -204,4 +204,60 @@ class ParticipantDtoTest extends TestCase
return $insurance;
}
public function testRequiresOwnEmailIsFalseForNonApplicantChild(): void
{
$participant = new ParticipantDto();
$participant->index = 1;
$participant->dateOfBirth = new \DateTimeImmutable('2015-01-01');
$this->assertFalse($participant->requiresOwnEmail());
}
public function testRequiresOwnEmailIsTrueForNonApplicantAdult(): void
{
$participant = new ParticipantDto();
$participant->index = 1;
$participant->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$this->assertTrue($participant->requiresOwnEmail());
}
public function testRequiresOwnEmailIsTrueForTheApplicantEvenAsAChild(): void
{
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = new \DateTimeImmutable('2015-01-01');
$this->assertTrue(
$participant->requiresOwnEmail(),
'The applicant is the booking contact and always has to be reachable'
);
}
public function testRequiresOwnEmailTreatsUnknownAgeAsAdult(): void
{
$participant = new ParticipantDto();
$participant->index = 1;
$participant->dateOfBirth = null;
$this->assertTrue($participant->requiresOwnEmail());
}
public function testRequiresOwnEmailBoundaryAtChildAgeThreshold(): void
{
$exactlyThreshold = new ParticipantDto();
$exactlyThreshold->index = 1;
$exactlyThreshold->dateOfBirth = (new \DateTimeImmutable('today'))
->modify(sprintf('-%d years', ParticipantDto::CHILD_AGE_THRESHOLD));
$oneDayShort = new ParticipantDto();
$oneDayShort->index = 1;
$oneDayShort->dateOfBirth = (new \DateTimeImmutable('today'))
->modify(sprintf('-%d years', ParticipantDto::CHILD_AGE_THRESHOLD))
->modify('+1 day');
$this->assertTrue($exactlyThreshold->requiresOwnEmail());
$this->assertFalse($oneDayShort->requiresOwnEmail());
}
}
+156
View File
@@ -30,6 +30,7 @@ use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\EmailValidator;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -856,6 +857,161 @@ class ParticipantEditDtoTest extends TestCase
* available ski pass. Ski pass validation depends on the travel actually offering
* passes, so a travel without them is a distinct fixture, not the default.
*/
public function testChildWithoutEmailPassesStrictValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createChildParticipant(''),
]);
$violations = $this->validateStrict($bookingDto, 1);
$this->assertSame([], $this->emailRequiredMessages($violations));
}
public function testChildWithNullEmailPassesStrictValidation(): void
{
$child = $this->createChildParticipant('');
$child->email = null;
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$child,
]);
$violations = $this->validateStrict($bookingDto, 1);
$this->assertSame([], $this->emailRequiredMessages($violations));
}
public function testChildWithMalformedEmailStillFailsEmailFormatValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createChildParticipant('not-an-email'),
]);
$violations = $this->validator->validate(
new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
),
null,
['booking_create', 'strict_required']
);
$messages = [];
foreach ($violations as $violation) {
if ('participant.email' === $violation->getPropertyPath()) {
$messages[] = $violation->getMessage();
}
}
$this->assertSame(['Bitte eine gültige E-Mail Adresse angeben'], $messages);
}
public function testAdultDependentWithoutEmailFailsStrictValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant(''),
]);
$violations = $this->validateStrict($bookingDto, 1);
$this->assertSame(['Bitte angeben'], $this->emailRequiredMessages($violations));
}
public function testDependentWithUnknownDateOfBirthAndNoEmailFailsStrictValidation(): void
{
$participant = $this->createAdultParticipant('');
$participant->dateOfBirth = null;
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$participant,
]);
$violations = $this->validateStrict($bookingDto, 1);
$this->assertSame(['Bitte angeben'], $this->emailRequiredMessages($violations));
}
public function testApplicantChildWithoutEmailStillFailsStrictValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createChildParticipant(''),
$this->createAdultParticipant('[email protected]'),
]);
$violations = $this->validateStrict($bookingDto, 0);
$this->assertSame(['Bitte angeben'], $this->emailRequiredMessages($violations));
}
public function testDependentTurningExactlySixteenRequiresEmail(): void
{
$participant = $this->createAdultParticipant('');
$participant->dateOfBirth = (new \DateTimeImmutable('today'))->modify('-16 years');
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$participant,
]);
$violations = $this->validateStrict($bookingDto, 1);
$this->assertSame(['Bitte angeben'], $this->emailRequiredMessages($violations));
}
public function testDependentOneDayShortOfSixteenDoesNotRequireEmail(): void
{
$participant = $this->createAdultParticipant('');
$participant->dateOfBirth = (new \DateTimeImmutable('today'))->modify('-16 years')->modify('+1 day');
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$participant,
]);
$violations = $this->validateStrict($bookingDto, 1);
$this->assertSame([], $this->emailRequiredMessages($violations));
}
/**
* Validates one participant with the strict group active, as the booking create flow does.
*/
private function validateStrict(BookingDto $bookingDto, int $index): ConstraintViolationListInterface
{
return $this->validator->validate(
new ParticipantEditDto(
participant: $bookingDto->participants[$index],
bookingContext: $bookingDto,
),
null,
['booking_create', 'strict_required']
);
}
/**
* Narrows a violation list down to the "email is mandatory" messages.
*
* @return list<string>
*/
private function emailRequiredMessages(ConstraintViolationListInterface $violations): array
{
$messages = [];
foreach ($violations as $violation) {
if ('participant.email' === $violation->getPropertyPath() && 'Bitte angeben' === $violation->getMessage()) {
$messages[] = $violation->getMessage();
}
}
return $messages;
}
private function createTravelOfferingSkiPasses(?int $ageFrom = null): Travel
{
$travel = $this->createTravelWithoutSkiPasses();