function EntityValidationTest::testCompositeConstraintValidation

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::testCompositeConstraintValidation()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::testCompositeConstraintValidation()
  3. 11.x 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 200

Class

EntityValidationTest
Tests the Entity Validation API.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testCompositeConstraintValidation() : void {
    $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.');
}

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