function TypedConfigTest::testSimpleConfigValidation

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Config/TypedConfigTest.php \Drupal\KernelTests\Config\TypedConfigTest::testSimpleConfigValidation()
  2. 10 core/tests/Drupal/KernelTests/Config/TypedConfigTest.php \Drupal\KernelTests\Config\TypedConfigTest::testSimpleConfigValidation()
  3. 11.x core/tests/Drupal/KernelTests/Config/TypedConfigTest.php \Drupal\KernelTests\Config\TypedConfigTest::testSimpleConfigValidation()

Tests config validation via the Typed Data API.

File

core/tests/Drupal/KernelTests/Config/TypedConfigTest.php, line 95

Class

TypedConfigTest
Tests config validation mechanism.

Namespace

Drupal\KernelTests\Config

Code

public function testSimpleConfigValidation() {
    $config = \Drupal::configFactory()->getEditable('config_test.validation');
    
    /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager */
    $typed_config_manager = \Drupal::service('config.typed');
    
    /** @var \Drupal\Core\Config\Schema\TypedConfigInterface $typed_config */
    $typed_config = $typed_config_manager->get('config_test.validation');
    $result = $typed_config->validate();
    $this->assertInstanceOf(ConstraintViolationListInterface::class, $result);
    $this->assertEmpty($result);
    // Test constraints on primitive types.
    $config->set('llama', 'elephant');
    $config->save();
    $typed_config = $typed_config_manager->get('config_test.validation');
    $result = $typed_config->validate();
    // Its not a valid llama anymore.
    $this->assertCount(1, $result);
    $this->assertEquals('no valid llama', $result->get(0)
        ->getMessage());
    // Test constraints on mapping.
    $config->set('llama', 'llama');
    $config->set('cat.type', 'nyans');
    $config->save();
    $typed_config = $typed_config_manager->get('config_test.validation');
    $result = $typed_config->validate();
    $this->assertEmpty($result);
    // Test constrains on nested mapping.
    $config->set('cat.type', 'miaus');
    $config->save();
    $typed_config = $typed_config_manager->get('config_test.validation');
    $result = $typed_config->validate();
    $this->assertCount(1, $result);
    $this->assertEquals('no valid cat', $result->get(0)
        ->getMessage());
    // Test constrains on sequences elements.
    $config->set('cat.type', 'nyans');
    $config->set('giraffe', [
        'muh',
        'hum2',
    ]);
    $config->save();
    $typed_config = $typed_config_manager->get('config_test.validation');
    $result = $typed_config->validate();
    $this->assertCount(1, $result);
    $this->assertEquals('Giraffes just hum', $result->get(0)
        ->getMessage());
    // Test constrains on the sequence itself.
    $config->set('giraffe', [
        'hum',
        'hum2',
        'invalid-key' => 'hum',
    ]);
    $config->save();
    $typed_config = $typed_config_manager->get('config_test.validation');
    $result = $typed_config->validate();
    $this->assertCount(1, $result);
    $this->assertEquals('giraffe', $result->get(0)
        ->getPropertyPath());
    $this->assertEquals('Invalid giraffe key.', $result->get(0)
        ->getMessage());
    // Validates mapping.
    $typed_config = $typed_config_manager->get('config_test.validation');
    $value = $typed_config->getValue();
    unset($value['giraffe']);
    $value['elephant'] = 'foo';
    $value['zebra'] = 'foo';
    $typed_config->setValue($value);
    $result = $typed_config->validate();
    $this->assertCount(1, $result);
    $this->assertEquals('', $result->get(0)
        ->getPropertyPath());
    $this->assertEquals('Unexpected keys: elephant, zebra', $result->get(0)
        ->getMessage());
}

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