function AllowedValuesConstraintValidatorTest::testValidationCallback
Same name in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php \Drupal\KernelTests\Core\TypedData\AllowedValuesConstraintValidatorTest::testValidationCallback()
- 10 core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php \Drupal\KernelTests\Core\TypedData\AllowedValuesConstraintValidatorTest::testValidationCallback()
- 11.x core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php \Drupal\KernelTests\Core\TypedData\AllowedValuesConstraintValidatorTest::testValidationCallback()
Tests the AllowedValuesConstraintValidator with callbacks.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ TypedData/ AllowedValuesConstraintValidatorTest.php, line 66
Class
- AllowedValuesConstraintValidatorTest
- Tests AllowedValues validation constraint with both valid and invalid values.
Namespace
Drupal\KernelTests\Core\TypedDataCode
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->assertEquals(0, $violations->count(), 'Validation passed for correct value.');
$typed_data = $this->typedData
->create($definition, 1);
$violations = $typed_data->validate();
$this->assertEquals(0, $violations->count(), 'Validation passed for value that will be cast to the correct type.');
$typed_data = $this->typedData
->create($definition, 2);
$violations = $typed_data->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed for incorrect value.');
$typed_data = $this->typedData
->create($definition, 'd');
$violations = $typed_data->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed for incorrect value.');
$typed_data = $this->typedData
->create($definition, 0);
$violations = $typed_data->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed for incorrect value.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.