You are here

public function FieldWidgetConstraintValidatorTest::testValidationWithCompositeConstraint 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::testValidationWithCompositeConstraint()

Tests widget constraint validation with composite constraints.

File

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

Class

FieldWidgetConstraintValidatorTest
Tests validation constraints for FieldWidgetConstraintValidatorTest.

Namespace

Drupal\system\Tests\Entity

Code

public function testValidationWithCompositeConstraint() {

  // 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
    ->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',
  ]));
}