You are here

public function FieldWidgetConstraintValidatorTest::testValidation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php \Drupal\system\Tests\Entity\FieldWidgetConstraintValidatorTest::testValidation()

Tests widget constraint validation.

File

core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php, line 42
Contains \Drupal\system\Tests\Entity\FieldWidgetConstraintValidatorTest.

Class

FieldWidgetConstraintValidatorTest
Tests validation constraints for FieldWidgetConstraintValidatorTest.

Namespace

Drupal\system\Tests\Entity

Code

public function testValidation() {
  $entity_type = 'entity_test_constraint_violation';
  $entity = entity_create($entity_type, array(
    'id' => 1,
    'revision_id' => 1,
  ));
  $display = entity_get_form_display($entity_type, $entity_type, 'default');
  $form = array();
  $form_state = new FormState();
  $display
    ->buildForm($entity, $form, $form_state);

  // Pretend the form has been built.
  $form_state
    ->setFormObject(\Drupal::entityManager()
    ->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
    ->assertEqual($errors['name'], 'Widget constraint has failed.', 'Constraint violation is generated correctly');
}