public function EntityTypeConstraintValidatorTest::testValidation in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Entity/EntityTypeConstraintValidatorTest.php \Drupal\system\Tests\Entity\EntityTypeConstraintValidatorTest::testValidation()
Tests the EntityTypeConstraintValidator.
File
- core/
modules/ system/ src/ Tests/ Entity/ EntityTypeConstraintValidatorTest.php, line 37 - Contains \Drupal\system\Tests\Entity\EntityTypeConstraintValidatorTest.
Class
- EntityTypeConstraintValidatorTest
- Tests validation constraints for EntityTypeConstraintValidator.
Namespace
Drupal\system\Tests\EntityCode
public function testValidation() {
// Create a typed data definition with an EntityType constraint.
$entity_type = 'node';
$definition = DataDefinition::create('entity_reference')
->setConstraints(array(
'EntityType' => $entity_type,
));
// Test the validation.
$node = $this->container
->get('entity.manager')
->getStorage('node')
->create(array(
'type' => 'page',
));
$typed_data = $this->typedData
->create($definition, $node);
$violations = $typed_data
->validate();
$this
->assertEqual($violations
->count(), 0, 'Validation passed for correct value.');
// Test the validation when an invalid value (in this case a user entity)
// is passed.
$account = $this
->createUser();
$typed_data = $this->typedData
->create($definition, $account);
$violations = $typed_data
->validate();
$this
->assertEqual($violations
->count(), 1, 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this
->assertEqual($violation
->getMessage(), t('The entity must be of type %type.', array(
'%type' => $entity_type,
)), 'The message for invalid value is correct.');
$this
->assertEqual($violation
->getRoot(), $typed_data, 'Violation root is correct.');
$this
->assertEqual($violation
->getInvalidValue(), $account, 'The invalid value is set correctly in the violation.');
}