function FieldWidgetConstraintValidatorTest::testValidationWithCompositeConstraint

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

Tests widget constraint validation with composite constraints.

File

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

Class

FieldWidgetConstraintValidatorTest
Tests validation constraints for FieldWidgetConstraintValidatorTest.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testValidationWithCompositeConstraint() : void {
  // First provide a valid value, this should cause no validation.
  $entity = EntityTestCompositeConstraint::create([
    'name' => 'valid-value',
  ]);
  $entity->save();
  $errors = $this->getErrorsForEntity($entity);
  $this->assertFalse(isset($errors['name']));
  $this->assertFalse(isset($errors['type']));
  // Provide an invalid value for the name field.
  $entity = EntityTestCompositeConstraint::create([
    'name' => 'failure-field-name',
  ]);
  $errors = $this->getErrorsForEntity($entity);
  $this->assertTrue(isset($errors['name']));
  $this->assertFalse(isset($errors['type']));
  // Hide the second field (type) and ensure the validation still happens. The
  // error message appears on the first field (name).
  $entity = EntityTestCompositeConstraint::create([
    'name' => 'failure-field-name',
  ]);
  $errors = $this->getErrorsForEntity($entity, [
    'type',
  ]);
  $this->assertTrue(isset($errors['name']));
  $this->assertFalse(isset($errors['type']));
  // Provide a violation again, but this time hide the first field (name).
  // Ensure that the validation still happens and the error message is moved
  // from the field to the second field and have a custom error message.
  $entity = EntityTestCompositeConstraint::create([
    'name' => 'failure-field-name',
  ]);
  $errors = $this->getErrorsForEntity($entity, [
    'name',
  ]);
  $this->assertFalse(isset($errors['name']));
  $this->assertTrue(isset($errors['type']));
  $this->assertEquals(new FormattableMarkup('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', [
    '%field_name' => 'name',
  ]), $errors['type']);
}

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