Handle identifier uniqueness validation for same Person in UniqueIdentifierConstraintValidator

- Add logic to skip validation errors for duplicate identifiers belonging to the same Person.
- Update test case to verify no violation is raised for such duplicates.
- Refactor repository query logic to support the new validation scenario.
This commit is contained in:
2025-09-26 13:55:40 +02:00
parent bfbde078b7
commit ad2b6d63ac
2 changed files with 36 additions and 5 deletions

View File

@@ -39,12 +39,15 @@ class UniqueIdentifierConstraintValidator extends ConstraintValidator
$identifiers = $this->personIdentifierRepository->findByDefinitionAndCanonical($value->getDefinition(), $value->getValue());
if (count($identifiers) > 0) {
$persons = array_map(fn (PersonIdentifier $idf): string => $this->personRender->renderString($idf->getPerson(), []), $identifiers);
if (count($identifiers) > 1 || $identifiers[0]->getPerson() !== $value->getPerson()) {
$persons = array_map(fn (PersonIdentifier $idf): string => $this->personRender->renderString($idf->getPerson(), []), $identifiers);
$this->context->buildViolation($constraint->message)
->setParameter('{{ persons }}', implode(', ', $persons))
->setParameter('definition_id', (string) $value->getDefinition()->getId())
->addViolation();
$this->context->buildViolation($constraint->message)
->setParameter('{{ persons }}', implode(', ', $persons))
->setParameter('definition_id', (string) $value->getDefinition()->getId())
->addViolation();
}
}
}
}

View File

@@ -127,4 +127,32 @@ final class UniqueIdentifierConstraintValidatorTest extends ConstraintValidatorT
->setParameter('definition_id', '1')
->assertRaised();
}
public function testViolationWhenDuplicateFoundButForSamePerson(): void
{
$definition = new PersonIdentifierDefinition(['en' => 'SSN'], 'string');
$reflectionClass = new \ReflectionClass($definition);
$reflectionId = $reflectionClass->getProperty('id');
$reflectionId->setValue($definition, 1);
$personA = new Person();
$personA->setFirstName('Alice')->setLastName('Anderson');
$dup1 = new PersonIdentifier($definition);
$dup1->setPerson($personA);
$dup1->setValue(['value' => '123']);
// Repository returns duplicates
$this->repository->findByDefinitionAndCanonical($definition, ['value' => '123'])->willReturn([$dup1]);
$identifier = new PersonIdentifier($definition);
$identifier->setPerson($personA);
$identifier->setValue(['value' => '123']);
$constraint = new UniqueIdentifierConstraint();
$this->validator->validate($identifier, $constraint);
$this->assertNoViolation();
}
}