function EntityValidationTest::checkValidation
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::checkValidation()
- 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::checkValidation()
- 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::checkValidation()
Executes the validation test set for a defined entity type.
Parameters
string $entity_type: The entity type to run the tests with.
1 call to EntityValidationTest::checkValidation()
- EntityValidationTest::testValidation in core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityValidationTest.php - Tests validating test entity types.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityValidationTest.php, line 133
Class
- EntityValidationTest
- Tests the Entity Validation API.
Namespace
Drupal\KernelTests\Core\EntityCode
protected function checkValidation($entity_type) {
$entity = $this->createTestEntity($entity_type);
$violations = $entity->validate();
$this->assertEquals(0, $violations->count(), 'Validation passes.');
// Test triggering a fail for each of the constraints specified.
$test_entity = clone $entity;
$test_entity->id->value = -1;
$violations = $test_entity->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed.');
$this->assertEquals('ID: The integer must be larger or equal to 0.', $violations[0]->getMessage());
$test_entity = clone $entity;
$test_entity->uuid->value = $this->randomString(129);
$violations = $test_entity->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed.');
$this->assertEquals('UUID: may not be longer than 128 characters.', $violations[0]->getMessage());
$test_entity = clone $entity;
$langcode_key = $this->entityTypeManager
->getDefinition($entity_type)
->getKey('langcode');
$test_entity->{$langcode_key}->value = $this->randomString(13);
$violations = $test_entity->validate();
// This should fail on AllowedValues and Length constraints.
$this->assertEquals(2, $violations->count(), 'Validation failed.');
$this->assertEquals('This value is too long. It should have 12 characters or less.', $violations[0]->getMessage());
$this->assertEquals('The value you selected is not a valid choice.', $violations[1]->getMessage());
$test_entity = clone $entity;
$test_entity->type->value = NULL;
$violations = $test_entity->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed.');
$this->assertEquals('This value should not be null.', $violations[0]->getMessage());
$test_entity = clone $entity;
$test_entity->name->value = $this->randomString(65);
$violations = $test_entity->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed.');
$this->assertEquals('Name: may not be longer than 64 characters.', $violations[0]->getMessage());
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEquals($test_entity, $violation->getRoot()
->getValue(), 'Violation root is entity.');
$this->assertEquals('name.0.value', $violation->getPropertyPath(), 'Violation property path is correct.');
$this->assertEquals($test_entity->name->value, $violation->getInvalidValue(), 'Violation contains invalid value.');
$test_entity = clone $entity;
$test_entity->set('user_id', 9999);
$violations = $test_entity->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed.');
$this->assertEquals('The referenced entity (user: 9999) does not exist.', $violations[0]->getMessage());
$test_entity = clone $entity;
$test_entity->field_test_text->format = $this->randomString(33);
$violations = $test_entity->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed.');
$this->assertEquals('The value you selected is not a valid choice.', $violations[0]->getMessage());
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEquals($test_entity, $violation->getRoot()
->getValue(), 'Violation root is entity.');
$this->assertEquals('field_test_text.0.format', $violation->getPropertyPath(), 'Violation property path is correct.');
$this->assertEquals($test_entity->field_test_text->format, $violation->getInvalidValue(), 'Violation contains invalid value.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.