function FieldWidgetConstraintValidatorTest::testValidation

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\FieldWidgetConstraintValidatorTest::testValidation()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\FieldWidgetConstraintValidatorTest::testValidation()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\FieldWidgetConstraintValidatorTest::testValidation()

Tests widget constraint validation.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php, line 44

Class

FieldWidgetConstraintValidatorTest
Tests validation constraints for FieldWidgetConstraintValidatorTest.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testValidation() : void {
    $entity_type = 'entity_test_constraint_violation';
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create([
        'id' => 1,
        'revision_id' => 1,
    ]);
    $display = \Drupal::service('entity_display.repository')->getFormDisplay($entity_type, $entity_type);
    $form = [];
    $form_state = new FormState();
    $display->buildForm($entity, $form, $form_state);
    // Pretend the form has been built.
    $form_state->setFormObject(\Drupal::entityTypeManager()->getFormObject($entity_type, 'default'));
    \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form, $form_state);
    \Drupal::formBuilder()->processForm('field_test_entity_form', $form, $form_state);
    // Validate the field constraint.
    $form_state->getFormObject()
        ->setEntity($entity)
        ->setFormDisplay($display, $form_state);
    $entity = $form_state->getFormObject()
        ->buildEntity($form, $form_state);
    $display->validateFormValues($entity, $form, $form_state);
    $errors = $form_state->getErrors();
    $this->assertEquals('Widget constraint has failed.', $errors['name'], 'Constraint violation at the field items list level is generated correctly');
    $this->assertEquals('Widget constraint has failed.', $errors['test_field'], 'Constraint violation at the field items list level is generated correctly for an advanced widget');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.