You are here

public function RecursiveContextualValidatorTest::testBasicValidateWithConstraint in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php \Drupal\Tests\Core\TypedData\RecursiveContextualValidatorTest::testBasicValidateWithConstraint()

@covers ::validate

File

core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php, line 128
Contains \Drupal\Tests\Core\TypedData\RecursiveContextualValidatorTest.

Class

RecursiveContextualValidatorTest
@coversDefaultClass \Drupal\Core\TypedData\Validation\RecursiveContextualValidator @group typedData

Namespace

Drupal\Tests\Core\TypedData

Code

public function testBasicValidateWithConstraint() {
  $typed_data = $this->typedDataManager
    ->create(DataDefinition::create('string')
    ->addConstraint('Callback', [
    'callback' => function ($value, ExecutionContextInterface $context) {
      $context
        ->addViolation('test violation: ' . $value);
    },
  ]));
  $typed_data
    ->setValue('foo');
  $violations = $this->recursiveValidator
    ->validate($typed_data);
  $this
    ->assertCount(1, $violations);

  // Ensure that the right value is passed into the validator.
  $this
    ->assertEquals('test violation: foo', $violations
    ->get(0)
    ->getMessage());
}