Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityTypeConstraintValidatorTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\Core\TypedData\DataDefinition;
6
7 /**
8  * Tests validation constraints for EntityTypeConstraintValidator.
9  *
10  * @group Entity
11  */
12 class EntityTypeConstraintValidatorTest extends EntityKernelTestBase {
13
14   /**
15    * The typed data manager to use.
16    *
17    * @var \Drupal\Core\TypedData\TypedDataManager
18    */
19   protected $typedData;
20
21   public static $modules = ['node', 'field', 'user'];
22
23   protected function setUp() {
24     parent::setUp();
25     $this->typedData = $this->container->get('typed_data_manager');
26   }
27
28   /**
29    * Tests the EntityTypeConstraintValidator.
30    */
31   public function testValidation() {
32     // Create a typed data definition with an EntityType constraint.
33     $entity_type = 'node';
34     $definition = DataDefinition::create('entity_reference')
35       ->setConstraints([
36         'EntityType' => $entity_type,
37       ]
38     );
39
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.');
45
46     // Test the validation when an invalid value (in this case a user entity)
47     // is passed.
48     $account = $this->createUser();
49
50     $typed_data = $this->typedData->create($definition, $account);
51     $violations = $typed_data->validate();
52     $this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.');
53
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.');
59   }
60
61 }