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. 10 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 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->assertEqual($violations->count(), 0);
    // Trigger violation condition.
    $entity->name->value = 'test';
    $entity->type->value = 'test2';
    $violations = $entity->validate();
    $this->assertEqual($violations->count(), 1);
    // Make sure we can determine this is composite constraint.
    $constraint = $violations[0]->getConstraint();
    $this->assertInstanceOf(CompositeConstraintBase::class, $constraint);
    $this->assertEqual('type', $violations[0]->getPropertyPath());
    
    /** @var \Drupal\Core\Entity\Plugin\Validation\Constraint\CompositeConstraintBase $constraint */
    $this->assertEqual($constraint->coversFields(), [
        'name',
        'type',
    ], '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.