You are here

public function EntityValidationTest::testCompositeConstraintValidation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityValidationTest.php \Drupal\system\Tests\Entity\EntityValidationTest::testCompositeConstraintValidation()

Tests composite constraints.

File

core/modules/system/src/Tests/Entity/EntityValidationTest.php, line 186
Contains \Drupal\system\Tests\Entity\EntityValidationTest.

Class

EntityValidationTest
Tests the Entity Validation API.

Namespace

Drupal\system\Tests\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
    ->assertTrue($constraint instanceof CompositeConstraintBase, 'Constraint is composite constraint.');
  $this
    ->assertEqual('type', $violations[0]
    ->getPropertyPath());

  /** @var CompositeConstraintBase $constraint */
  $this
    ->assertEqual($constraint
    ->coversFields(), [
    'name',
    'type',
  ], 'Information about covered fields can be retrieved.');
}