Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / TypedData / ComplexDataConstraintValidatorTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\TypedData;
4
5 use Drupal\Core\TypedData\DataDefinition;
6 use Drupal\Core\TypedData\MapDataDefinition;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Tests ComplexData validation constraint with both valid and invalid values
11  * for a key.
12  *
13  * @group Validation
14  */
15 class ComplexDataConstraintValidatorTest extends KernelTestBase {
16
17   /**
18    * The typed data manager to use.
19    *
20    * @var \Drupal\Core\TypedData\TypedDataManager
21    */
22   protected $typedData;
23
24   protected function setUp() {
25     parent::setUp();
26     $this->typedData = $this->container->get('typed_data_manager');
27   }
28
29   /**
30    * Tests the ComplexData validation constraint validator.
31    *
32    * For testing a map including a constraint on one of its keys is defined.
33    */
34   public function testValidation() {
35     // Create a definition that specifies some ComplexData constraint.
36     $definition = MapDataDefinition::create()
37       ->setPropertyDefinition('key', DataDefinition::create('integer'))
38       ->addConstraint('ComplexData', [
39         'key' => [
40           'AllowedValues' => [1, 2, 3],
41         ],
42       ]);
43
44     // Test the validation.
45     $typed_data = $this->typedData->create($definition, ['key' => 1]);
46     $violations = $typed_data->validate();
47     $this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
48
49     // Test the validation when an invalid value is passed.
50     $typed_data = $this->typedData->create($definition, ['key' => 4]);
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 value you selected is not a valid choice.'), 'The message for invalid value is correct.');
57     $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
58     $this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.');
59
60     // Test using the constraint with a map without the specified key. This
61     // should be ignored as long as there is no NotNull or NotBlank constraint.
62     $typed_data = $this->typedData->create($definition, ['foo' => 'bar']);
63     $violations = $typed_data->validate();
64     $this->assertEqual($violations->count(), 0, 'Constraint on non-existing key is ignored.');
65
66     $definition = MapDataDefinition::create()
67       ->setPropertyDefinition('key', DataDefinition::create('integer'))
68       ->addConstraint('ComplexData', [
69         'key' => [
70           'NotNull' => [],
71         ],
72       ]);
73
74     $typed_data = $this->typedData->create($definition, ['foo' => 'bar']);
75     $violations = $typed_data->validate();
76     $this->assertEqual($violations->count(), 1, 'Key is required.');
77   }
78
79 }