Mercurial > hg > isophonics-drupal-site
comparison core/modules/field/tests/src/Kernel/FieldValidationTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\field\Kernel; | |
4 | |
5 /** | |
6 * Tests field validation. | |
7 * | |
8 * @group field | |
9 */ | |
10 class FieldValidationTest extends FieldKernelTestBase { | |
11 | |
12 /** | |
13 * @var string | |
14 */ | |
15 private $entityType; | |
16 | |
17 /** | |
18 * @var string | |
19 */ | |
20 private $bundle; | |
21 | |
22 /** | |
23 * @var \Drupal\Core\Entity\EntityInterface | |
24 */ | |
25 private $entity; | |
26 | |
27 protected function setUp() { | |
28 parent::setUp(); | |
29 | |
30 // Create a field and storage of type 'test_field', on the 'entity_test' | |
31 // entity type. | |
32 $this->entityType = 'entity_test'; | |
33 $this->bundle = 'entity_test'; | |
34 $this->createFieldWithStorage('', $this->entityType, $this->bundle); | |
35 | |
36 // Create an 'entity_test' entity. | |
37 $this->entity = entity_create($this->entityType, [ | |
38 'type' => $this->bundle, | |
39 ]); | |
40 } | |
41 | |
42 /** | |
43 * Tests that the number of values is validated against the field cardinality. | |
44 */ | |
45 public function testCardinalityConstraint() { | |
46 $cardinality = $this->fieldTestData->field_storage->getCardinality(); | |
47 $entity = $this->entity; | |
48 | |
49 for ($delta = 0; $delta < $cardinality + 1; $delta++) { | |
50 $entity->{$this->fieldTestData->field_name}[] = ['value' => 1]; | |
51 } | |
52 | |
53 // Validate the field. | |
54 $violations = $entity->{$this->fieldTestData->field_name}->validate(); | |
55 | |
56 // Check that the expected constraint violations are reported. | |
57 $this->assertEqual(count($violations), 1); | |
58 $this->assertEqual($violations[0]->getPropertyPath(), ''); | |
59 $this->assertEqual($violations[0]->getMessage(), t('%name: this field cannot hold more than @count values.', ['%name' => $this->fieldTestData->field->getLabel(), '@count' => $cardinality])); | |
60 } | |
61 | |
62 /** | |
63 * Tests that constraints defined by the field type are validated. | |
64 */ | |
65 public function testFieldConstraints() { | |
66 $cardinality = $this->fieldTestData->field_storage->getCardinality(); | |
67 $entity = $this->entity; | |
68 | |
69 // The test is only valid if the field cardinality is greater than 2. | |
70 $this->assertTrue($cardinality >= 2); | |
71 | |
72 // Set up values for the field. | |
73 $expected_violations = []; | |
74 for ($delta = 0; $delta < $cardinality; $delta++) { | |
75 // All deltas except '1' have incorrect values. | |
76 if ($delta == 1) { | |
77 $value = 1; | |
78 } | |
79 else { | |
80 $value = -1; | |
81 $expected_violations[$delta . '.value'][] = t('%name does not accept the value -1.', ['%name' => $this->fieldTestData->field->getLabel()]); | |
82 } | |
83 $entity->{$this->fieldTestData->field_name}[] = $value; | |
84 } | |
85 | |
86 // Validate the field. | |
87 $violations = $entity->{$this->fieldTestData->field_name}->validate(); | |
88 | |
89 // Check that the expected constraint violations are reported. | |
90 $violations_by_path = []; | |
91 foreach ($violations as $violation) { | |
92 $violations_by_path[$violation->getPropertyPath()][] = $violation->getMessage(); | |
93 } | |
94 $this->assertEqual($violations_by_path, $expected_violations); | |
95 } | |
96 | |
97 } |