You are here

public function ConfigSchemaTest::testConfigSchemaInfoAlter in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php \Drupal\KernelTests\Core\Config\ConfigSchemaTest::testConfigSchemaInfoAlter()
  2. 10 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 554

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
      ->assertEqual($e
      ->getMessage(), 'Invoking hook_config_schema_info_alter() has removed (config_schema_test.hook) schema definitions');
  }
  \Drupal::state()
    ->set('config_schema_test_exception_add', TRUE);
  try {
    $typed_config
      ->getDefinitions();
    $this
      ->fail('Expected ConfigSchemaAlterException thrown.');
  } catch (ConfigSchemaAlterException $e) {
    $this
      ->assertEqual($e
      ->getMessage(), 'Invoking hook_config_schema_info_alter() has added (config_schema_test.hook_added_definition) and removed (config_schema_test.hook) schema definitions');
  }
  \Drupal::state()
    ->set('config_schema_test_exception_remove', FALSE);
  try {
    $typed_config
      ->getDefinitions();
    $this
      ->fail('Expected ConfigSchemaAlterException thrown.');
  } catch (ConfigSchemaAlterException $e) {
    $this
      ->assertEqual($e
      ->getMessage(), 'Invoking hook_config_schema_info_alter() has added (config_schema_test.hook_added_definition) schema definitions');
  }

  // 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
    ->assertEqual($definitions['config_schema_test.hook']['additional_metadata'], 'new schema info');
}