3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\Core\TypedData\DataDefinition;
8 * Tests validation constraints for EntityTypeConstraintValidator.
12 class EntityTypeConstraintValidatorTest extends EntityKernelTestBase {
15 * The typed data manager to use.
17 * @var \Drupal\Core\TypedData\TypedDataManager
21 public static $modules = ['node', 'field', 'user'];
23 protected function setUp() {
25 $this->typedData = $this->container->get('typed_data_manager');
29 * Tests the EntityTypeConstraintValidator.
31 public function testValidation() {
32 // Create a typed data definition with an EntityType constraint.
33 $entity_type = 'node';
34 $definition = DataDefinition::create('entity_reference')
36 'EntityType' => $entity_type,
40 // Test the validation.
41 $node = $this->container->get('entity.manager')->getStorage('node')->create(['type' => 'page']);
42 $typed_data = $this->typedData->create($definition, $node);
43 $violations = $typed_data->validate();
44 $this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
46 // Test the validation when an invalid value (in this case a user entity)
48 $account = $this->createUser();
50 $typed_data = $this->typedData->create($definition, $account);
51 $violations = $typed_data->validate();
52 $this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.');
54 // Make sure the information provided by a violation is correct.
55 $violation = $violations[0];
56 $this->assertEqual($violation->getMessage(), t('The entity must be of type %type.', ['%type' => $entity_type]), 'The message for invalid value is correct.');
57 $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
58 $this->assertEqual($violation->getInvalidValue(), $account, 'The invalid value is set correctly in the violation.');