feat: make email optional for under-16 participants except applicant

This commit is contained in:
fromme committed 2026-10-16 17:49:04 +02:00
1 parent 55a9189284
commit 1ec9c972cd
18 files changed
+435 -336

No files matched your search

+91
View File
@@ -429,6 +429,97 @@ class BookingDtoTest extends TestCase
$this->assertSame(['participants[0].address'], $this->collectFirstParticipantAddressPaths($dto));
}
public function testChildApplicantWithoutEmailIsReportedOnce(): void
{
$dto = $this->createCreateModeDto();
$dto->participants[0] = $this->createParticipantWithoutEmail(10);
$this->assertSame(
['participants[0].email'],
$this->collectFirstParticipantEmailPaths($dto),
'The applicant is the booking contact and needs an email at any age'
);
}
public function testChildAtIndexZeroOfASplitBookingMayLeaveTheEmailBlank(): void
{
$dto = $this->createCreateModeDto();
$dto->applicantTravels = false;
$dto->participants[0] = $this->createParticipantWithoutEmail(10);
$this->assertSame([], $this->collectFirstParticipantEmailPaths($dto));
}
public function testAdultAtIndexZeroOfASplitBookingStillNeedsAnEmail(): void
{
$dto = $this->createCreateModeDto();
$dto->applicantTravels = false;
$dto->participants[0] = $this->createParticipantWithoutEmail(30);
$this->assertSame(['participants[0].email'], $this->collectFirstParticipantEmailPaths($dto));
}
public function testApplicantEmailComesFromParticipantZeroWhenTheApplicantTravels(): void
{
$dto = $this->createCreateModeDto();
$dto->participants[0]->email = ' [email protected] ';
$dto->applicant = new ParticipantDto();
$dto->applicant->email = '[email protected]';
$this->assertSame('[email protected]', $dto->getApplicantEmail());
}
public function testApplicantEmailComesFromTheApplicantBlockOnACreateModeSplit(): void
{
$dto = $this->createCreateModeDto();
$dto->participants[0]->email = '[email protected]';
$dto->applicantTravels = false;
$dto->applicant = new ParticipantDto();
$dto->applicant->email = '[email protected]';
$this->assertSame('[email protected]', $dto->getApplicantEmail());
}
public function testApplicantEmailComesFromTheAnmelderOnAnEditModeSplit(): void
{
$dto = $this->createEditModeDto(100, 200);
$dto->participants[0]->email = '[email protected]';
$dto->booking->applicant->communication->email = '[email protected]';
$this->assertSame('[email protected]', $dto->getApplicantEmail());
}
/**
* Ages relative to today, since isChild() measures against the current date.
*/
private function createParticipantWithoutEmail(int $years): ParticipantDto
{
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = (new \DateTimeImmutable('today'))->modify(sprintf('-%d years', $years));
$participant->address = $this->createAddress();
return $participant;
}
/**
* @return list<string>
*/
private function collectFirstParticipantEmailPaths(BookingDto $dto): array
{
$validator = Validation::createValidatorBuilder()->enableAttributeMapping()->getValidator();
$violations = $validator->validate($dto, null, ['booking_create', 'strict_required']);
$paths = [];
foreach ($violations as $violation) {
if ('participants[0].email' === $violation->getPropertyPath()) {
$paths[] = $violation->getPropertyPath();
}
}
return $paths;
}
/**
* @return list<string>
*/
+100 -11
View File
@@ -909,7 +909,7 @@ 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 testChildWithoutEmailFailsStrictValidation(): void
public function testChildWithoutEmailPassesStrictValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
@@ -918,14 +918,10 @@ class ParticipantEditDtoTest extends TestCase
$violations = $this->validateStrict($bookingDto, 1);
$this->assertSame(
['Bitte angeben'],
$this->emailRequiredMessages($violations),
'BusPro requires a Mail-Adresse for every participant, children included'
);
$this->assertSame([], $this->emailRequiredMessages($violations));
}
public function testChildWithNullEmailFailsStrictValidation(): void
public function testChildWithNullEmailPassesStrictValidation(): void
{
$child = $this->createChildParticipant('');
$child->email = null;
@@ -937,7 +933,7 @@ class ParticipantEditDtoTest extends TestCase
$violations = $this->validateStrict($bookingDto, 1);
$this->assertSame(['Bitte angeben'], $this->emailRequiredMessages($violations));
$this->assertSame([], $this->emailRequiredMessages($violations));
}
public function testChildSharingTheApplicantsEmailPassesStrictValidation(): void
@@ -952,7 +948,7 @@ class ParticipantEditDtoTest extends TestCase
$this->assertSame(
[],
$this->emailRequiredMessages($violations),
'A child inherits the applicant address, which BusPro accepts'
'A child may use the applicant\'s address instead of leaving it blank'
);
}
@@ -1021,6 +1017,99 @@ class ParticipantEditDtoTest extends TestCase
$this->assertSame(['Bitte angeben'], $this->emailRequiredMessages($violations));
}
public function testChildAtIndexZeroOfASplitBookingMayLeaveTheEmailBlank(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createChildParticipant(''),
]);
$bookingDto->applicantTravels = false;
$violations = $this->validateStrict($bookingDto, 0);
$this->assertSame(
[],
$this->emailRequiredMessages($violations),
'Where the applicant does not travel, participant[0] is an ordinary traveller'
);
}
public function testAdultAtIndexZeroOfASplitBookingStillNeedsAnEmail(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant(''),
]);
$bookingDto->applicantTravels = false;
$violations = $this->validateStrict($bookingDto, 0);
$this->assertSame(['Bitte angeben'], $this->emailRequiredMessages($violations));
}
public function testAdultTravellerMayNotReuseTheNonTravellingApplicantsEmail(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
]);
$bookingDto->applicantTravels = false;
$bookingDto->applicant = $this->createValidParticipant(new \DateTimeImmutable('1980-01-01'), '[email protected]');
$violations = $this->validator->validate(
new ParticipantEditDto(participant: $bookingDto->participants[0], bookingContext: $bookingDto),
null,
['booking_create']
);
$this->assertCount(1, $violations);
$this->assertSame('participant.email', $violations->get(0)->getPropertyPath());
$this->assertSame(
'Diese E-Mail Adresse wird bereits von der anmeldenden Person verwendet',
$violations->get(0)->getMessage()
);
}
public function testChildTravellerMayReuseTheNonTravellingApplicantsEmail(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createChildParticipant('[email protected]'),
]);
$bookingDto->applicantTravels = false;
$bookingDto->applicant = $this->createValidParticipant(new \DateTimeImmutable('1980-01-01'), '[email protected]');
$violations = $this->validator->validate(
new ParticipantEditDto(participant: $bookingDto->participants[0], bookingContext: $bookingDto),
null,
['booking_create']
);
$this->assertCount(0, $violations);
}
public function testAdultTravellerMayNotReuseTheAnmelderEmailOfASplitBookingInEditMode(): void
{
$applicant = new PersonalData();
$applicant->personId = 100;
$applicant->communication->email = '[email protected]';
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
]);
$bookingDto->booking = new Booking();
$bookingDto->booking->applicant = $applicant;
$bookingDto->participants[0]->personId = 200;
$violations = $this->validator->validate(
new ParticipantEditDto(participant: $bookingDto->participants[0], bookingContext: $bookingDto),
null,
['booking_edit']
);
$this->assertCount(1, $violations);
$this->assertSame(
'Diese E-Mail Adresse wird bereits von der anmeldenden Person verwendet',
$violations->get(0)->getMessage()
);
}
public function testDependentTurningExactlySixteenRequiresEmail(): void
{
$participant = $this->createAdultParticipant('');
@@ -1036,7 +1125,7 @@ class ParticipantEditDtoTest extends TestCase
$this->assertSame(['Bitte angeben'], $this->emailRequiredMessages($violations));
}
public function testDependentOneDayShortOfSixteenStillRequiresEmail(): void
public function testDependentOneDayShortOfSixteenDoesNotRequireEmail(): void
{
$participant = $this->createAdultParticipant('');
$participant->dateOfBirth = (new \DateTimeImmutable('today'))->modify('-16 years')->modify('+1 day');
@@ -1048,7 +1137,7 @@ class ParticipantEditDtoTest extends TestCase
$violations = $this->validateStrict($bookingDto, 1);
$this->assertSame(['Bitte angeben'], $this->emailRequiredMessages($violations));
$this->assertSame([], $this->emailRequiredMessages($violations));
}
/**
@@ -74,14 +74,69 @@ class CreateFieldStateProviderTest extends TestCase
);
}
public function testEmailIsRequiredForEveryParticipantIncludingChildren(): void
public function testEmailIsNotRequiredForNonApplicantChild(): void
{
$bookingDto = $this->createBookingDtoWithApplicantAndDependent(new \DateTimeImmutable('2015-01-01'));
$this->assertArrayNotHasKey(
'required',
$this->provider->getFieldState('email', $bookingDto, 1),
'Requiredness is the form type base option now that it is unconditional'
$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 testEmailIsNotRequiredForAChildAtIndexZeroWhenTheApplicantDoesNotTravel(): void
{
$bookingDto = $this->createBookingDtoWithApplicantAndDependent(new \DateTimeImmutable('2015-01-01'));
$bookingDto->participants[0]->dateOfBirth = new \DateTimeImmutable('2015-01-01');
$bookingDto->applicantTravels = false;
$this->assertFalse(
$this->provider->getFieldState('email', $bookingDto, 0)['required'],
'On a split booking participant[0] is an ordinary traveller'
);
}
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'
);
}
@@ -31,14 +31,11 @@ class EditFieldStateProviderTest extends TestCase
$this->assertTrue($this->provider->getFieldState('email', $bookingDto, 1)['required']);
}
public function testEmailIsRequiredForNonApplicantChild(): void
public function testEmailIsNotRequiredForNonApplicantChild(): void
{
$bookingDto = $this->createBookingDto(new \DateTimeImmutable('2015-01-01'), false);
$this->assertTrue(
$this->provider->getFieldState('email', $bookingDto, 1)['required'],
'BusPro requires a Mail-Adresse for every participant, children included'
);
$this->assertFalse($this->provider->getFieldState('email', $bookingDto, 1)['required']);
}
public function testEmailIsNotRequiredForInternalAgencyBookingsEvenForAdults(): void
@@ -1,162 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form\Service;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Travel;
use App\BusProNet\XmlLoader\AgencyLoader;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\ParticipantDateOfBirthFieldHandler;
use App\Form\Service\ParticipantEmailFieldHandler;
use App\Form\Service\ParticipantFieldHandlerRegistry;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
class ParticipantEmailFieldHandlerTest extends TestCase
{
private ParticipantEmailFieldHandler $handler;
protected function setUp(): void
{
$this->handler = new ParticipantEmailFieldHandler();
}
public function testChildWithoutEmailInheritsTheApplicants(): void
{
$bookingDto = $this->createBookingDto();
$this->handler->processField(['email' => ''], $bookingDto, 1);
$this->assertSame('[email protected]', $bookingDto->participants[1]->email);
}
public function testChildKeepsAnEmailThatWasTypedIn(): void
{
$bookingDto = $this->createBookingDto();
$this->handler->processField(['email' => '[email protected]'], $bookingDto, 1);
$this->assertSame(
'[email protected]',
$bookingDto->participants[1]->email,
'The pre-filled address stays overridable'
);
}
public function testAdultWithoutEmailDoesNotInherit(): void
{
$bookingDto = $this->createBookingDto();
$bookingDto->participants[1]->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$this->handler->processField(['email' => ''], $bookingDto, 1);
$this->assertNull(
$bookingDto->participants[1]->email,
'An adult supplies their own address; NotBlank reports the gap'
);
}
public function testApplicantDoesNotInheritFromThemselves(): void
{
$bookingDto = $this->createBookingDto();
$bookingDto->participants[0]->dateOfBirth = new \DateTimeImmutable('2015-01-01');
$this->handler->processField(['email' => ''], $bookingDto, 0);
$this->assertNull($bookingDto->participants[0]->email);
}
public function testChildAtIndexZeroInheritsWhenTheApplicantDoesNotTravel(): void
{
$bookingDto = $this->createBookingDto();
$bookingDto->applicantTravels = false;
$bookingDto->applicant = $this->createParticipant('[email protected]', '1981-03-16');
$bookingDto->participants[0]->dateOfBirth = new \DateTimeImmutable('2015-01-01');
$this->handler->processField(['email' => ''], $bookingDto, 0);
$this->assertSame('[email protected]', $bookingDto->participants[0]->email);
}
public function testEditModeNeverPreFills(): void
{
$bookingDto = $this->createBookingDto();
$bookingDto->booking = new Booking();
$this->handler->processField(['email' => ''], $bookingDto, 1);
$this->assertNull(
$bookingDto->participants[1]->email,
'participant[0] is not reliably the applicant in edit mode, and a blank may be intended'
);
}
public function testEditModeLeavesAnInternalAgencyChildWithoutEmail(): void
{
$bookingDto = $this->createBookingDto();
$bookingDto->booking = new Booking();
$bookingDto->agencyCode = AgencyLoader::INTERNAL_AGENCY_CODE;
$this->handler->processField(['email' => ''], $bookingDto, 1);
$this->assertNull($bookingDto->participants[1]->email);
}
public function testHandlerRunsAfterDateOfBirth(): void
{
$this->assertSame(
['dateOfBirth'],
$this->handler->getDependencies(),
'Whether someone counts as a child is only known once their DOB is processed'
);
}
/**
* The handler runs in PRE_SUBMIT and writes to the DTO, but the form then binds the submitted
* data over it. Only fields the registry syncs back survive, so the pre-filled address has to
* reappear in the returned data - otherwise the blank wins and the field renders empty.
*/
public function testPreFilledEmailIsSyncedBackIntoTheSubmittedData(): void
{
$bookingDto = $this->createBookingDto();
$registry = new ParticipantFieldHandlerRegistry([
new ParticipantDateOfBirthFieldHandler(new NullLogger()),
new ParticipantEmailFieldHandler(),
]);
$synced = $registry->processFieldsForParticipantAndSync(['email' => ''], $bookingDto, 1);
$this->assertSame('[email protected]', $bookingDto->participants[1]->email);
$this->assertSame(
'[email protected]',
$synced['email'],
'Without the sync the form binds the blank back over the pre-filled address'
);
}
private function createBookingDto(): BookingDto
{
$bookingDto = new BookingDto(new Travel(), 1);
$bookingDto->participants = [
$this->createParticipant('[email protected]', '1980-01-01'),
$this->createParticipant('', '2015-01-01'),
];
$bookingDto->participants[0]->index = 0;
$bookingDto->participants[1]->index = 1;
$bookingDto->participants[1]->email = null;
return $bookingDto;
}
private function createParticipant(string $email, string $dateOfBirth): ParticipantDto
{
$participant = new ParticipantDto();
$participant->email = '' !== $email ? $email : null;
$participant->dateOfBirth = new \DateTimeImmutable($dateOfBirth);
return $participant;
}
}