You are here

public function ConfigInstallTest::testDependencyChecking in Drupal 9

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

Tests the configuration with unmet dependencies is not installed.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php, line 190

Class

ConfigInstallTest
Tests installation of configuration objects in installation functionality.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testDependencyChecking() {
  $this
    ->installModules([
    'config_test',
  ]);
  try {
    $this
      ->installModules([
      'config_install_dependency_test',
    ]);
    $this
      ->fail('Expected UnmetDependenciesException not thrown.');
  } catch (UnmetDependenciesException $e) {
    $this
      ->assertEquals('config_install_dependency_test', $e
      ->getExtension());
    $this
      ->assertEquals([
      'config_test.dynamic.other_module_test_with_dependency' => [
        'config_other_module_config_test',
        'config_test.dynamic.dotted.english',
      ],
    ], $e
      ->getConfigObjects());
    $this
      ->assertEquals('Configuration objects provided by <em class="placeholder">config_install_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test, config_test.dynamic.dotted.english)</em>', $e
      ->getMessage());
  }
  try {
    $this
      ->installModules([
      'config_install_double_dependency_test',
    ]);
    $this
      ->fail('Expected UnmetDependenciesException not thrown.');
  } catch (UnmetDependenciesException $e) {
    $this
      ->assertEquals('config_install_double_dependency_test', $e
      ->getExtension());
    $this
      ->assertEquals([
      'config_test.dynamic.other_module_test_with_dependency' => [
        'config_other_module_config_test',
        'config_test.dynamic.dotted.english',
      ],
    ], $e
      ->getConfigObjects());
    $this
      ->assertEquals('Configuration objects provided by <em class="placeholder">config_install_double_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test, config_test.dynamic.dotted.english)</em>', $e
      ->getMessage());
  }
  $this
    ->installModules([
    'config_test_language',
  ]);
  try {
    $this
      ->installModules([
      'config_install_dependency_test',
    ]);
    $this
      ->fail('Expected UnmetDependenciesException not thrown.');
  } catch (UnmetDependenciesException $e) {
    $this
      ->assertEquals('config_install_dependency_test', $e
      ->getExtension());
    $this
      ->assertEquals([
      'config_test.dynamic.other_module_test_with_dependency' => [
        'config_other_module_config_test',
      ],
    ], $e
      ->getConfigObjects());
    $this
      ->assertEquals('Configuration objects provided by <em class="placeholder">config_install_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test)</em>', $e
      ->getMessage());
  }
  $this
    ->installModules([
    'config_other_module_config_test',
  ]);
  $this
    ->installModules([
    'config_install_dependency_test',
  ]);
  $entity = \Drupal::entityTypeManager()
    ->getStorage('config_test')
    ->load('other_module_test_with_dependency');
  $this
    ->assertNotEmpty($entity, 'The config_test.dynamic.other_module_test_with_dependency configuration has been created during install.');

  // Ensure that dependencies can be added during module installation by
  // hooks.
  $this
    ->assertSame('config_install_dependency_test', $entity
    ->getDependencies()['module'][0]);
}