function TypedDataTest::testTypedDataValidation
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataValidation()
- 10 core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataValidation()
- 11.x core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataValidation()
Tests typed data validation.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ TypedData/ TypedDataTest.php, line 576
Class
- TypedDataTest
- Tests the functionality of all core data types.
Namespace
Drupal\KernelTests\Core\TypedDataCode
public function testTypedDataValidation() {
$definition = DataDefinition::create('integer')->setConstraints([
'Range' => [
'min' => 5,
],
]);
$violations = $this->typedDataManager
->create($definition, 10)
->validate();
$this->assertEqual($violations->count(), 0);
$integer = $this->typedDataManager
->create($definition, 1);
$violations = $integer->validate();
$this->assertEqual($violations->count(), 1);
// Test translating violation messages.
$message = t('This value should be %limit or more.', [
'%limit' => 5,
]);
$this->assertEqual($violations[0]->getMessage(), $message, 'Translated violation message retrieved.');
$this->assertEqual($violations[0]->getPropertyPath(), '');
$this->assertIdentical($violations[0]->getRoot(), $integer, 'Root object returned.');
// Test translating violation messages when pluralization is used.
$definition = DataDefinition::create('string')->setConstraints([
'Length' => [
'min' => 10,
],
]);
$violations = $this->typedDataManager
->create($definition, "short")
->validate();
$this->assertEqual($violations->count(), 1);
$message = t('This value is too short. It should have %limit characters or more.', [
'%limit' => 10,
]);
$this->assertEqual($violations[0]->getMessage(), $message, 'Translated violation message retrieved.');
// Test having multiple violations.
$definition = DataDefinition::create('integer')->setConstraints([
'Range' => [
'min' => 5,
],
'Null' => [],
]);
$violations = $this->typedDataManager
->create($definition, 10)
->validate();
$this->assertEqual($violations->count(), 1);
$violations = $this->typedDataManager
->create($definition, 1)
->validate();
$this->assertEqual($violations->count(), 2);
// Test validating property containers and make sure the NotNull and Null
// constraints work with typed data containers.
$definition = BaseFieldDefinition::create('integer')->setConstraints([
'NotNull' => [],
]);
$field_item = $this->typedDataManager
->create($definition, [
'value' => 10,
]);
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 0);
$field_item = $this->typedDataManager
->create($definition, [
'value' => 'no integer',
]);
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 1);
$this->assertEqual($violations[0]->getPropertyPath(), '0.value');
// Test that the field item may not be empty.
$field_item = $this->typedDataManager
->create($definition);
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 1);
// Test the Null constraint with typed data containers.
$definition = BaseFieldDefinition::create('float')->setConstraints([
'Null' => [],
]);
$field_item = $this->typedDataManager
->create($definition, [
'value' => 11.5,
]);
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 1);
$field_item = $this->typedDataManager
->create($definition);
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 0);
// Test getting constraint definitions by type.
$definitions = $this->typedDataManager
->getValidationConstraintManager()
->getDefinitionsByType('entity');
$this->assertTrue(isset($definitions['EntityType']), 'Constraint plugin found for type entity.');
$this->assertTrue(isset($definitions['Null']), 'Constraint plugin found for type entity.');
$this->assertTrue(isset($definitions['NotNull']), 'Constraint plugin found for type entity.');
$definitions = $this->typedDataManager
->getValidationConstraintManager()
->getDefinitionsByType('string');
$this->assertFalse(isset($definitions['EntityType']), 'Constraint plugin not found for type string.');
$this->assertTrue(isset($definitions['Null']), 'Constraint plugin found for type string.');
$this->assertTrue(isset($definitions['NotNull']), 'Constraint plugin found for type string.');
// Test automatic 'required' validation.
$definition = DataDefinition::create('integer')->setRequired(TRUE);
$violations = $this->typedDataManager
->create($definition)
->validate();
$this->assertEqual($violations->count(), 1);
$violations = $this->typedDataManager
->create($definition, 0)
->validate();
$this->assertEqual($violations->count(), 0);
// Test validating a list of a values and make sure property paths starting
// with "0" are created.
$definition = BaseFieldDefinition::create('integer');
$violations = $this->typedDataManager
->create($definition, [
[
'value' => 10,
],
])
->validate();
$this->assertEqual($violations->count(), 0);
$violations = $this->typedDataManager
->create($definition, [
[
'value' => 'string',
],
])
->validate();
$this->assertEqual($violations->count(), 1);
$this->assertEqual($violations[0]->getInvalidValue(), 'string');
$this->assertIdentical($violations[0]->getPropertyPath(), '0.value');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.