You are here

public function ConfigSchemaTest::testConfigSchemaInfoAlter in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php \Drupal\KernelTests\Core\Config\ConfigSchemaTest::testConfigSchemaInfoAlter()

Tests hook_config_schema_info_alter().

File

core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php, line 557

Class

ConfigSchemaTest
Tests schema for configuration objects.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testConfigSchemaInfoAlter() {

  /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */
  $typed_config = \Drupal::service('config.typed');
  $typed_config
    ->clearCachedDefinitions();

  // Ensure that keys can not be added or removed by
  // hook_config_schema_info_alter().
  \Drupal::state()
    ->set('config_schema_test_exception_remove', TRUE);
  try {
    $typed_config
      ->getDefinitions();
    $this
      ->fail('Expected ConfigSchemaAlterException thrown.');
  } catch (ConfigSchemaAlterException $e) {
    $this
      ->assertEquals('Invoking hook_config_schema_info_alter() has removed (config_schema_test.hook) schema definitions', $e
      ->getMessage());
  }
  \Drupal::state()
    ->set('config_schema_test_exception_add', TRUE);
  try {
    $typed_config
      ->getDefinitions();
    $this
      ->fail('Expected ConfigSchemaAlterException thrown.');
  } catch (ConfigSchemaAlterException $e) {
    $this
      ->assertEquals('Invoking hook_config_schema_info_alter() has added (config_schema_test.hook_added_definition) and removed (config_schema_test.hook) schema definitions', $e
      ->getMessage());
  }
  \Drupal::state()
    ->set('config_schema_test_exception_remove', FALSE);
  try {
    $typed_config
      ->getDefinitions();
    $this
      ->fail('Expected ConfigSchemaAlterException thrown.');
  } catch (ConfigSchemaAlterException $e) {
    $this
      ->assertEquals('Invoking hook_config_schema_info_alter() has added (config_schema_test.hook_added_definition) schema definitions', $e
      ->getMessage());
  }

  // Tests that hook_config_schema_info_alter() can add additional metadata to
  // existing configuration schema.
  \Drupal::state()
    ->set('config_schema_test_exception_add', FALSE);
  $definitions = $typed_config
    ->getDefinitions();
  $this
    ->assertEquals('new schema info', $definitions['config_schema_test.hook']['additional_metadata']);
}