diff --git a/core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php b/core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php index 05a36ba2e9..e52fd21deb 100644 --- a/core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php @@ -4,6 +4,7 @@ use Drupal\Core\TypedData\DataDefinition; use Drupal\KernelTests\KernelTestBase; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; /** * Tests AllowedValues validation constraint with both valid and invalid values. @@ -49,6 +50,61 @@ public function testValidation() { $this->assertEqual($violation->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.'); $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.'); $this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.'); + + // Test the validation when a value of an incorrect type is passed. + $typed_data = $this->typedData->create($definition, '1'); + $violations = $typed_data->validate(); + $this->assertEqual($violations->count(), 0, 'Value is coerced to the correct type and is valid.'); + } + + /** + * Tests the AllowedValuesConstraintValidator with callbacks. + */ + public function testValidationCallback() { + // Create a definition that specifies some AllowedValues and a callback. + // This tests that callbacks have a higher priority than a supplied list of + // values and can be used to coerce the value to the correct type. + $definition = DataDefinition::create('string') + ->addConstraint('AllowedValues', ['choices' => [1, 2, 3], 'callback' => [static::class, 'allowedValueCallback']]); + $typed_data = $this->typedData->create($definition, 'a'); + $violations = $typed_data->validate(); + $this->assertEqual($violations->count(), 0, 'Validation passed for correct value.'); + + $typed_data = $this->typedData->create($definition, 1); + $violations = $typed_data->validate(); + $this->assertEqual($violations->count(), 0, 'Validation passed for value that will be cast to the correct type.'); + + $typed_data = $this->typedData->create($definition, 2); + $violations = $typed_data->validate(); + $this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.'); + + $typed_data = $this->typedData->create($definition, 'd'); + $violations = $typed_data->validate(); + $this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.'); + } + + /** + * An AllowedValueConstraint callback. + * + * @return array + */ + public static function allowedValueCallback() { + return ['a', 'b', 'c', '1']; + } + + /** + * Tests the AllowedValuesConstraintValidator with an invalid callback. + */ + public function testValidationCallbackException() { + // Create a definition that specifies some AllowedValues and a callback. + // This tests that callbacks have a higher priority than a supplied list of + // values and can be used to coerce the value to the correct type. + $definition = DataDefinition::create('string') + ->addConstraint('AllowedValues', ['choices' => [1, 2, 3], 'callback' => [static::class, 'doesNotExist']]); + $typed_data = $this->typedData->create($definition, 1); + + $this->setExpectedException(ConstraintDefinitionException::class, 'The AllowedValuesConstraint constraint expects a valid callback'); + $typed_data->validate(); } }