wip: email uniqueness validation

This commit is contained in:
Björn Fromme
2026-03-16 11:59:11 +01:00
parent 54a3145710
commit 89979d6ce4
11 changed files with 549 additions and 783 deletions
+407
View File
@@ -0,0 +1,407 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form\Model;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Model\ParticipantEditDto;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Tests for ParticipantEditDto email uniqueness validation.
*
* This test suite validates the wrapper DTO pattern used for participant email
* uniqueness validation during form submission. The wrapper enables clean validation
* without requiring back-references or form coupling.
*/
class ParticipantEditDtoTest extends TestCase
{
private ValidatorInterface $validator;
protected function setUp(): void
{
$this->validator = Validation::createValidatorBuilder()
->enableAttributeMapping()
->getValidator();
}
public function testAdultWithUniqueEmailPassesValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createValidAdultParticipant('[email protected]'),
$this->createValidAdultParticipant('[email protected]'),
]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(0, $violations);
}
public function testAdultWithDuplicateEmailFailsValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(1, $violations);
$this->assertSame('participant.email', $violations->get(0)->getPropertyPath());
$this->assertSame(
'Diese E-Mail Adresse wird bereits von einem anderen Teilnehmer verwendet',
$violations->get(0)->getMessage()
);
}
public function testChildWithDuplicateEmailPassesValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createChildParticipant('[email protected]'),
]);
// Validate the child participant (index 1)
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(0, $violations);
}
public function testEmptyEmailPassesValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant(''),
$this->createAdultParticipant(''),
]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
// Should not trigger uniqueness validation (handled by @Email and @NotBlank constraints)
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
// May have other violations, but not uniqueness
$hasUniquenessViolation = false;
foreach ($violations as $violation) {
if (str_contains($violation->getMessage(), 'wird bereits von einem anderen Teilnehmer verwendet')) {
$hasUniquenessViolation = true;
break;
}
}
$this->assertFalse($hasUniquenessViolation, 'Should not have uniqueness violation for empty emails');
}
public function testNullEmailPassesValidation(): void
{
$participant1 = $this->createAdultParticipant('[email protected]');
$participant1->email = null;
$participant2 = $this->createAdultParticipant('[email protected]');
$participant2->email = null;
$bookingDto = $this->createBookingDtoWithParticipants([$participant1, $participant2]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
// Should not trigger uniqueness validation (handled by @Email and @NotBlank constraints)
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
// May have other violations, but not uniqueness
$hasUniquenessViolation = false;
foreach ($violations as $violation) {
if (str_contains($violation->getMessage(), 'wird bereits von einem anderen Teilnehmer verwendet')) {
$hasUniquenessViolation = true;
break;
}
}
$this->assertFalse($hasUniquenessViolation, 'Should not have uniqueness violation for null emails');
}
public function testCaseInsensitiveEmailComparison(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
// Should have uniqueness violation (case-insensitive comparison)
$uniquenessViolations = array_filter(
iterator_to_array($violations),
fn ($v) => str_contains($v->getMessage(), 'wird bereits von einem anderen Teilnehmer verwendet')
);
$this->assertCount(1, $uniquenessViolations);
}
public function testWhitespaceNormalizationInEmailComparison(): void
{
$participant1 = $this->createAdultParticipant('[email protected]');
$participant1->email = ' [email protected] ';
$participant2 = $this->createAdultParticipant('[email protected]');
$bookingDto = $this->createBookingDtoWithParticipants([$participant1, $participant2]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
// Should have uniqueness violation (whitespace trimmed during comparison)
$uniquenessViolations = array_filter(
iterator_to_array($violations),
fn ($v) => str_contains($v->getMessage(), 'wird bereits von einem anderen Teilnehmer verwendet')
);
$this->assertCount(1, $uniquenessViolations);
}
public function testMultipleDuplicatesDetectedCorrectly(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
]);
// Validate first participant - should fail
$wrapper1 = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations1 = $this->validator->validate($wrapper1, null, ['booking_create']);
$this->assertCount(1, $violations1);
// Validate second participant - should fail
$wrapper2 = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations2 = $this->validator->validate($wrapper2, null, ['booking_create']);
$this->assertCount(1, $violations2);
// Validate third participant - should fail
$wrapper3 = new ParticipantEditDto(
participant: $bookingDto->participants[2],
bookingContext: $bookingDto,
);
$violations3 = $this->validator->validate($wrapper3, null, ['booking_create']);
$this->assertCount(1, $violations3);
}
public function testSelfComparisonSkipped(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
]);
// Participant should not validate against itself
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(0, $violations);
}
public function testValidationInEditMode(): void
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
$bookingDto = new BookingDto($travel, 1); // hotelId must be int
$participant1 = $this->createAdultParticipant('[email protected]');
$participant1->index = 0;
$participant2 = $this->createAdultParticipant('[email protected]');
$participant2->index = 1;
$bookingDto->participants = [$participant1, $participant2];
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_edit']);
// Should have uniqueness violation in edit mode as well
$this->assertCount(1, $violations);
}
public function testMixedScenario(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'), // Unique - OK
$this->createAdultParticipant('[email protected]'), // Duplicate - ERROR
$this->createChildParticipant('[email protected]'), // Child with duplicate - OK
$this->createAdultParticipant('[email protected]'), // Duplicate - ERROR
$this->createChildParticipant('[email protected]'), // Unique child - OK
$this->createAdultParticipant('[email protected]'), // Unique - OK
]);
// Validate participant at index 0 - should pass (unique)
$wrapper0 = new ParticipantEditDto($bookingDto->participants[0], $bookingDto);
$violations0 = $this->validator->validate($wrapper0, null, ['booking_create']);
$this->assertCount(0, $violations0);
// Validate participant at index 1 - should fail (duplicate)
$wrapper1 = new ParticipantEditDto($bookingDto->participants[1], $bookingDto);
$violations1 = $this->validator->validate($wrapper1, null, ['booking_create']);
$this->assertCount(1, $violations1);
// Validate participant at index 2 - should pass (child exempt)
$wrapper2 = new ParticipantEditDto($bookingDto->participants[2], $bookingDto);
$violations2 = $this->validator->validate($wrapper2, null, ['booking_create']);
$this->assertCount(0, $violations2);
// Validate participant at index 3 - should fail (duplicate)
$wrapper3 = new ParticipantEditDto($bookingDto->participants[3], $bookingDto);
$violations3 = $this->validator->validate($wrapper3, null, ['booking_create']);
$this->assertCount(1, $violations3);
// Validate participant at index 4 - should pass (unique child)
$wrapper4 = new ParticipantEditDto($bookingDto->participants[4], $bookingDto);
$violations4 = $this->validator->validate($wrapper4, null, ['booking_create']);
$this->assertCount(0, $violations4);
// Validate participant at index 5 - should pass (unique)
$wrapper5 = new ParticipantEditDto($bookingDto->participants[5], $bookingDto);
$violations5 = $this->validator->validate($wrapper5, null, ['booking_create']);
$this->assertCount(0, $violations5);
}
private function createBookingDtoWithParticipants(array $participants): BookingDto
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
$bookingDto = new BookingDto($travel, 1); // hotelId must be int
// Set index for each participant
foreach ($participants as $index => $participant) {
$participant->index = $index;
// First participant (applicant) needs address and mobile
if (0 === $index) {
$participant->mobile = '+49 123 456789';
if (null === $participant->address) {
$participant->address = new \App\Form\Model\AddressDto();
}
$participant->address->street = 'Test Street 1';
$participant->address->postCode = '12345';
$participant->address->city = 'Test City';
$participant->address->country = 'DE';
}
}
$bookingDto->participants = $participants;
return $bookingDto;
}
private function createAdultParticipant(string $email): ParticipantDto
{
$participant = $this->createValidParticipant(
new \DateTimeImmutable('1990-01-01'), // Adult (over 18)
$email
);
// Add required fields for full validation
$participant->assignedRoomId = 1;
$participant->skiPass = $this->createMockService();
$participant->transportationOutbound = $this->createMockService();
$participant->transportationInbound = $this->createMockService();
return $participant;
}
private function createValidAdultParticipant(string $email): ParticipantDto
{
// Same as createAdultParticipant - all adults need full validation
return $this->createAdultParticipant($email);
}
private function createChildParticipant(string $email): ParticipantDto
{
$participant = $this->createValidParticipant(
new \DateTimeImmutable('2015-01-01'), // Child (under 16)
$email
);
// Children also need transportation (required for all participants)
$participant->assignedRoomId = 1;
$participant->skiPass = $this->createMockService();
$participant->transportationOutbound = $this->createMockService();
$participant->transportationInbound = $this->createMockService();
return $participant;
}
private function createValidParticipant(\DateTimeImmutable $dateOfBirth, string $email): ParticipantDto
{
$participant = new ParticipantDto();
$participant->firstName = 'John';
$participant->lastName = 'Doe';
$participant->dateOfBirth = $dateOfBirth;
$participant->email = $email;
return $participant;
}
private function createMockService(): \App\BusProNet\Model\Service
{
$service = new \App\BusProNet\Model\Service();
$service->id = 1;
$service->label = 'Test Service';
$service->subType = 'TEST';
$service->price = 0.0;
return $service;
}
}