function ComponentValidatorTest::testCustomValidator

Tests we can use a custom validator to validate props.

File

core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php, line 243

Class

ComponentValidatorTest
Unit tests for the component validation.

Namespace

Drupal\Tests\Core\Theme\Component

Code

public function testCustomValidator() : void {
  $translation = $this->getStringTranslationStub();
  $container = new ContainerBuilder();
  $container->set('string_translation', $translation);
  \Drupal::setContainer($container);
  $component = new Component([
    'app_root' => '/fake/path/root',
  ], 'sdc_test:my-cta', static::loadComponentDefinitionFromFs('my-cta'));
  $component_validator = new ComponentValidator();
  // A validator with a constraint factory that uses a custom constraint for
  // checking format.
  $component_validator->setValidator(new Validator((new Factory())->setConstraintClass('format', UrlHelperFormatConstraint::class)));
  self::assertTrue($component_validator->validateProps([
    'text' => 'Can Pica',
    // This is a valid URI but for v5.2 of justinrainbow/json-schema it
    // does not pass validation without a custom constraint for format.
    // We pass a custom factory and it should be used.
'href' => 'entity:node/1',
    'target' => '_blank',
    'attributes' => new Attribute([
      'key' => 'value',
    ]),
  ], $component), 'The valid component props threw an error.');
}

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