You are here

public function EntityValidationTest::testCompositeConstraintValidation in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::testCompositeConstraintValidation()

Tests composite constraints.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php, line 195

Class

EntityValidationTest
Tests the Entity Validation API.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testCompositeConstraintValidation() {
  $entity = $this
    ->createTestEntity('entity_test_composite_constraint');
  $violations = $entity
    ->validate();
  $this
    ->assertEquals(0, $violations
    ->count());

  // Trigger violation condition.
  $entity->name->value = 'test';
  $entity->type->value = 'test2';
  $violations = $entity
    ->validate();
  $this
    ->assertEquals(1, $violations
    ->count());

  // Make sure we can determine this is composite constraint.
  $constraint = $violations[0]
    ->getConstraint();
  $this
    ->assertInstanceOf(CompositeConstraintBase::class, $constraint);
  $this
    ->assertEquals('type', $violations[0]
    ->getPropertyPath());

  /** @var \Drupal\Core\Entity\Plugin\Validation\Constraint\CompositeConstraintBase $constraint */
  $this
    ->assertEquals([
    'name',
    'type',
  ], $constraint
    ->coversFields(), 'Information about covered fields can be retrieved.');
}