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();
@@ -73,4 +73,95 @@ class CreateFieldStateProviderTest extends TestCase
'Dependent insurance field must reappear once the applicant switches away from family insurance'
);
}
public function testEmailIsNotRequiredForNonApplicantChild(): void
{
$bookingDto = $this->createBookingDtoWithApplicantAndDependent(new \DateTimeImmutable('2015-01-01'));
$this->assertFalse(
$this->provider->getFieldState('email', $bookingDto, 1)['required'],
'A child travelling with an applicant does not need an email address of their own'
);
}
public function testEmailIsRequiredForNonApplicantAdult(): void
{
$bookingDto = $this->createBookingDtoWithApplicantAndDependent(new \DateTimeImmutable('1990-01-01'));
$this->assertTrue($this->provider->getFieldState('email', $bookingDto, 1)['required']);
}
public function testEmailIsRequiredForTheApplicantEvenAsAChild(): void
{
$bookingDto = $this->createBookingDtoWithApplicantAndDependent(new \DateTimeImmutable('2015-01-01'));
$bookingDto->participants[0]->dateOfBirth = new \DateTimeImmutable('2015-01-01');
$this->assertTrue(
$this->provider->getFieldState('email', $bookingDto, 0)['required'],
'The applicant is the booking contact and always has to be reachable'
);
}
public function testEmailIsRequiredWhenDateOfBirthIsUnknown(): void
{
$bookingDto = $this->createBookingDtoWithApplicantAndDependent(new \DateTimeImmutable('2015-01-01'));
$bookingDto->participants[1]->dateOfBirth = null;
$this->assertTrue(
$this->provider->getFieldState('email', $bookingDto, 1)['required'],
'An unknown age counts as an adult'
);
}
public function testEmailRequirementFlipsWhenDateOfBirthChanges(): void
{
$bookingDto = $this->createBookingDtoWithApplicantAndDependent(new \DateTimeImmutable('2015-01-01'));
$dependent = $bookingDto->participants[1];
$this->assertFalse($this->provider->getFieldState('email', $bookingDto, 1)['required']);
$dependent->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$this->assertTrue(
$this->provider->getFieldState('email', $bookingDto, 1)['required'],
'Correcting a child date of birth to an adult one must reinstate the requirement'
);
}
/**
* Guards the switch from add-only to toggling 'required' state: the two fields that already
* used a 'required' condition must keep behaving exactly as before, now reporting false
* instead of omitting the key. Both declare 'required' => false as their base in the form
* type, so assigning the condition result either way is equivalent to the old add-only pass.
*/
public function testMobileAndAddressRemainRequiredOnlyForTheFirstParticipant(): void
{
$bookingDto = $this->createBookingDtoWithApplicantAndDependent(new \DateTimeImmutable('1990-01-01'));
$this->assertTrue($this->provider->getFieldState('mobile', $bookingDto, 0)['required']);
$this->assertFalse($this->provider->getFieldState('mobile', $bookingDto, 1)['required']);
$this->assertTrue($this->provider->getFieldState('address', $bookingDto, 0)['required']);
$this->assertFalse($this->provider->getFieldState('address', $bookingDto, 1)['required']);
}
private function createBookingDtoWithApplicantAndDependent(\DateTimeImmutable $dependentDateOfBirth): BookingDto
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
$applicant = new ParticipantDto();
$applicant->index = 0;
$applicant->dateOfBirth = new \DateTimeImmutable('1980-01-01');
$dependent = new ParticipantDto();
$dependent->index = 1;
$dependent->dateOfBirth = $dependentDateOfBirth;
$bookingDto = new BookingDto($travel, 1);
$bookingDto->participants = [$applicant, $dependent];
return $bookingDto;
}
}
@@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form\Service;
use App\BusProNet\Model\Travel;
use App\BusProNet\XmlLoader\AgencyLoader;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\EditFieldStateProvider;
use PHPUnit\Framework\TestCase;
/**
* Covers the edit-flow email requirement, which follows the same age rule as the create flow
* but stands down entirely for internal agency bookings, where staff leave personal data blank.
*/
class EditFieldStateProviderTest extends TestCase
{
private EditFieldStateProvider $provider;
protected function setUp(): void
{
$this->provider = new EditFieldStateProvider();
}
public function testEmailIsRequiredForNonApplicantAdult(): void
{
$bookingDto = $this->createBookingDto(new \DateTimeImmutable('1990-01-01'), false);
$this->assertTrue($this->provider->getFieldState('email', $bookingDto, 1)['required']);
}
public function testEmailIsNotRequiredForNonApplicantChild(): void
{
$bookingDto = $this->createBookingDto(new \DateTimeImmutable('2015-01-01'), false);
$this->assertFalse($this->provider->getFieldState('email', $bookingDto, 1)['required']);
}
public function testEmailIsNotRequiredForInternalAgencyBookingsEvenForAdults(): void
{
$bookingDto = $this->createBookingDto(new \DateTimeImmutable('1990-01-01'), true);
$this->assertFalse(
$this->provider->getFieldState('email', $bookingDto, 1)['required'],
'Internal agency bookings leave personal data optional throughout'
);
}
public function testEmailIsRequiredForTheApplicantOutsideInternalAgencyBookings(): void
{
$bookingDto = $this->createBookingDto(new \DateTimeImmutable('1990-01-01'), false);
$this->assertTrue($this->provider->getFieldState('email', $bookingDto, 0)['required']);
}
private function createBookingDto(\DateTimeImmutable $dependentDateOfBirth, bool $internalAgency): BookingDto
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
$applicant = new ParticipantDto();
$applicant->index = 0;
$applicant->dateOfBirth = new \DateTimeImmutable('1980-01-01');
$dependent = new ParticipantDto();
$dependent->index = 1;
$dependent->dateOfBirth = $dependentDateOfBirth;
$bookingDto = new BookingDto($travel, 1);
$bookingDto->participants = [$applicant, $dependent];
if (true === $internalAgency) {
$bookingDto->agencyCode = AgencyLoader::INTERNAL_AGENCY_CODE;
}
return $bookingDto;
}
}