5ae824b71a2329f099a10b080013075a57e1165a
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / FieldWidgetConstraintValidatorTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormState;
8 use Drupal\entity_test\Entity\EntityTestCompositeConstraint;
9 use Drupal\KernelTests\KernelTestBase;
10
11 /**
12  * Tests validation constraints for FieldWidgetConstraintValidatorTest.
13  *
14  * @group Entity
15  */
16 class FieldWidgetConstraintValidatorTest extends KernelTestBase {
17
18   public static $modules = ['entity_test', 'field', 'user', 'system'];
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function setUp() {
24     parent::setUp();
25
26     $this->installSchema('system', ['key_value']);
27     $this->container->get('router.builder')->rebuild();
28
29     $this->installEntitySchema('user');
30     $this->installEntitySchema('entity_test_composite_constraint');
31   }
32
33   /**
34    * Tests widget constraint validation.
35    */
36   public function testValidation() {
37     $entity_type = 'entity_test_constraint_violation';
38     $entity = $this->container->get('entity_type.manager')
39       ->getStorage($entity_type)
40       ->create(['id' => 1, 'revision_id' => 1]);
41     $display = entity_get_form_display($entity_type, $entity_type, 'default');
42     $form = [];
43     $form_state = new FormState();
44     $display->buildForm($entity, $form, $form_state);
45
46     // Pretend the form has been built.
47     $form_state->setFormObject(\Drupal::entityManager()->getFormObject($entity_type, 'default'));
48     \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form, $form_state);
49     \Drupal::formBuilder()->processForm('field_test_entity_form', $form, $form_state);
50
51     // Validate the field constraint.
52     $form_state->getFormObject()->setEntity($entity)->setFormDisplay($display, $form_state);
53     $entity = $form_state->getFormObject()->buildEntity($form, $form_state);
54     $display->validateFormValues($entity, $form, $form_state);
55
56     $errors = $form_state->getErrors();
57     $this->assertEqual($errors['name'], 'Widget constraint has failed.', 'Constraint violation is generated correctly');
58   }
59
60   /**
61    * Gets the form errors for a given entity.
62    *
63    * @param \Drupal\Core\Entity\EntityInterface $entity
64    *   The entity
65    * @param array $hidden_fields
66    *   (optional) A list of hidden fields.
67    *
68    * @return array
69    *   The form errors.
70    */
71   protected function getErrorsForEntity(EntityInterface $entity, $hidden_fields = []) {
72     $entity_type_id = 'entity_test_composite_constraint';
73     $display = entity_get_form_display($entity_type_id, $entity_type_id, 'default');
74
75     foreach ($hidden_fields as $hidden_field) {
76       $display->removeComponent($hidden_field);
77     }
78
79     $form = [];
80     $form_state = new FormState();
81     $display->buildForm($entity, $form, $form_state);
82
83     $form_state->setFormObject(\Drupal::entityManager()->getFormObject($entity_type_id, 'default'));
84     \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form, $form_state);
85     \Drupal::formBuilder()->processForm('field_test_entity_form', $form, $form_state);
86
87     // Validate the field constraint.
88     /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
89     $form_object = $form_state->getFormObject();
90     $form_object
91       ->setEntity($entity)
92       ->setFormDisplay($display, $form_state)
93       ->validateForm($form, $form_state);
94
95     return $form_state->getErrors();
96   }
97
98   /**
99    * Tests widget constraint validation with composite constraints.
100    */
101   public function testValidationWithCompositeConstraint() {
102     // First provide a valid value, this should cause no validation.
103     $entity = EntityTestCompositeConstraint::create([
104       'name' => 'valid-value',
105     ]);
106     $entity->save();
107
108     $errors = $this->getErrorsForEntity($entity);
109     $this->assertFalse(isset($errors['name']));
110     $this->assertFalse(isset($errors['type']));
111
112     // Provide an invalid value for the name field.
113     $entity = EntityTestCompositeConstraint::create([
114       'name' => 'failure-field-name',
115     ]);
116     $errors = $this->getErrorsForEntity($entity);
117     $this->assertTrue(isset($errors['name']));
118     $this->assertFalse(isset($errors['type']));
119
120     // Hide the second field (type) and ensure the validation still happens. The
121     // error message appears on the first field (name).
122     $entity = EntityTestCompositeConstraint::create([
123       'name' => 'failure-field-name',
124     ]);
125     $errors = $this->getErrorsForEntity($entity, ['type']);
126     $this->assertTrue(isset($errors['name']));
127     $this->assertFalse(isset($errors['type']));
128
129     // Provide a violation again, but this time hide the first field (name).
130     // Ensure that the validation still happens and the error message is moved
131     // from the field to the second field and have a custom error message.
132     $entity = EntityTestCompositeConstraint::create([
133       'name' => 'failure-field-name',
134     ]);
135     $errors = $this->getErrorsForEntity($entity, ['name']);
136     $this->assertFalse(isset($errors['name']));
137     $this->assertTrue(isset($errors['type']));
138     $this->assertEqual($errors['type'], SafeMarkup::format('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', ['%field_name' => 'name']));
139   }
140
141   /**
142    * Tests entity level constraint validation.
143    */
144   public function testEntityLevelConstraintValidation() {
145     $entity = EntityTestCompositeConstraint::create([
146       'name' => 'entity-level-violation'
147     ]);
148     $entity->save();
149
150     $errors = $this->getErrorsForEntity($entity);
151     $this->assertEqual($errors[''], 'Entity level validation');
152   }
153
154 }