3 namespace Drupal\KernelTests\Core\Entity;
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;
12 * Tests validation constraints for FieldWidgetConstraintValidatorTest.
16 class FieldWidgetConstraintValidatorTest extends KernelTestBase {
18 public static $modules = ['entity_test', 'field', 'user', 'system'];
23 protected function setUp() {
26 $this->installSchema('system', ['key_value']);
27 $this->container->get('router.builder')->rebuild();
29 $this->installEntitySchema('user');
30 $this->installEntitySchema('entity_test_composite_constraint');
34 * Tests widget constraint validation.
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');
43 $form_state = new FormState();
44 $display->buildForm($entity, $form, $form_state);
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);
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);
56 $errors = $form_state->getErrors();
57 $this->assertEqual($errors['name'], 'Widget constraint has failed.', 'Constraint violation is generated correctly');
61 * Gets the form errors for a given entity.
63 * @param \Drupal\Core\Entity\EntityInterface $entity
65 * @param array $hidden_fields
66 * (optional) A list of hidden fields.
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');
75 foreach ($hidden_fields as $hidden_field) {
76 $display->removeComponent($hidden_field);
80 $form_state = new FormState();
81 $display->buildForm($entity, $form, $form_state);
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);
87 // Validate the field constraint.
88 /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
89 $form_object = $form_state->getFormObject();
92 ->setFormDisplay($display, $form_state)
93 ->validateForm($form, $form_state);
95 return $form_state->getErrors();
99 * Tests widget constraint validation with composite constraints.
101 public function testValidationWithCompositeConstraint() {
102 // First provide a valid value, this should cause no validation.
103 $entity = EntityTestCompositeConstraint::create([
104 'name' => 'valid-value',
108 $errors = $this->getErrorsForEntity($entity);
109 $this->assertFalse(isset($errors['name']));
110 $this->assertFalse(isset($errors['type']));
112 // Provide an invalid value for the name field.
113 $entity = EntityTestCompositeConstraint::create([
114 'name' => 'failure-field-name',
116 $errors = $this->getErrorsForEntity($entity);
117 $this->assertTrue(isset($errors['name']));
118 $this->assertFalse(isset($errors['type']));
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',
125 $errors = $this->getErrorsForEntity($entity, ['type']);
126 $this->assertTrue(isset($errors['name']));
127 $this->assertFalse(isset($errors['type']));
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',
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']));
142 * Tests entity level constraint validation.
144 public function testEntityLevelConstraintValidation() {
145 $entity = EntityTestCompositeConstraint::create([
146 'name' => 'entity-level-violation'
150 $errors = $this->getErrorsForEntity($entity);
151 $this->assertEqual($errors[''], 'Entity level validation');