function ConfigExistsConstraintValidatorTest::testValidation

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Config/ConfigExistsConstraintValidatorTest.php \Drupal\KernelTests\Core\Config\ConfigExistsConstraintValidatorTest::testValidation()

Tests the ConfigExists constraint validator.

@testWith [{}, "system.site", "system.site"]
[{"prefix": "system."}, "site", "system.site"]

File

core/tests/Drupal/KernelTests/Core/Config/ConfigExistsConstraintValidatorTest.php, line 32

Class

ConfigExistsConstraintValidatorTest
Tests the ConfigExists constraint validator.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testValidation(array $constraint_options, string $value, string $expected_config_name) : void {
  // Create a data definition that specifies the value must be a string with
  // the name of an existing piece of config.
  $definition = DataDefinition::create('string')->addConstraint('ConfigExists', $constraint_options);
  /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data */
  $typed_data = $this->container
    ->get('typed_data_manager');
  $data = $typed_data->create($definition, $value);
  $violations = $data->validate();
  $this->assertCount(1, $violations);
  $this->assertSame("The '{$expected_config_name}' config does not exist.", (string) $violations->get(0)
    ->getMessage());
  $this->installConfig('system');
  $this->assertCount(0, $data->validate());
  // NULL should not trigger a validation error: a value may be nullable.
  $data->setValue(NULL);
  $this->assertCount(0, $data->validate());
}

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