mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-27 09:05:01 +00:00
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:
@@ -39,12 +39,15 @@ class UniqueIdentifierConstraintValidator extends ConstraintValidator
|
|||||||
$identifiers = $this->personIdentifierRepository->findByDefinitionAndCanonical($value->getDefinition(), $value->getValue());
|
$identifiers = $this->personIdentifierRepository->findByDefinitionAndCanonical($value->getDefinition(), $value->getValue());
|
||||||
|
|
||||||
if (count($identifiers) > 0) {
|
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)
|
$this->context->buildViolation($constraint->message)
|
||||||
->setParameter('{{ persons }}', implode(', ', $persons))
|
->setParameter('{{ persons }}', implode(', ', $persons))
|
||||||
->setParameter('definition_id', (string) $value->getDefinition()->getId())
|
->setParameter('definition_id', (string) $value->getDefinition()->getId())
|
||||||
->addViolation();
|
->addViolation();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -127,4 +127,32 @@ final class UniqueIdentifierConstraintValidatorTest extends ConstraintValidatorT
|
|||||||
->setParameter('definition_id', '1')
|
->setParameter('definition_id', '1')
|
||||||
->assertRaised();
|
->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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user