You are here

public function ModuleTestBase::assertModuleConfig in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Module/ModuleTestBase.php \Drupal\Tests\system\Functional\Module\ModuleTestBase::assertModuleConfig()

Asserts that the default configuration of a module has been installed.

Parameters

string $module: The name of the module.

2 calls to ModuleTestBase::assertModuleConfig()
ConfigImportAllTest::testInstallUninstall in core/modules/config/tests/src/Functional/ConfigImportAllTest.php
Tests that a fixed set of modules can be installed and uninstalled.
InstallUninstallTest::assertModuleSuccessfullyInstalled in core/modules/system/tests/src/Functional/Module/InstallUninstallTest.php
Asserts that a module was successfully installed.

File

core/modules/system/tests/src/Functional/Module/ModuleTestBase.php, line 79

Class

ModuleTestBase
Helper class for module test cases.

Namespace

Drupal\Tests\system\Functional\Module

Code

public function assertModuleConfig($module) {
  $module_config_dir = $this
    ->getModulePath($module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
  if (!is_dir($module_config_dir)) {
    return;
  }
  $module_file_storage = new FileStorage($module_config_dir);

  // Verify that the module's default config directory is not empty and
  // contains default configuration files (instead of something else).
  $all_names = $module_file_storage
    ->listAll();
  if (empty($all_names)) {

    // Module has an empty config directory. For example it might contain a
    // schema directory.
    return;
  }
  $this
    ->assertNotEmpty($all_names);
  $module_config_dependencies = \Drupal::service('config.manager')
    ->findConfigEntityDependencies('module', [
    $module,
  ]);

  // Look up each default configuration object name in the active
  // configuration, and if it exists, remove it from the stack.
  $names = $module_file_storage
    ->listAll();
  foreach ($names as $key => $name) {
    if ($this
      ->config($name)
      ->get()) {
      unset($names[$key]);
    }

    // All configuration in a module's config/install directory should depend
    // on the module as it must be removed on uninstall or the module will not
    // be re-installable.
    $this
      ->assertTrue(strpos($name, $module . '.') === 0 || isset($module_config_dependencies[$name]), "Configuration {$name} provided by {$module} in its config/install directory does not depend on it.");
  }

  // Verify that all configuration has been installed (which means that $names
  // is empty).
  $this
    ->assertEmpty($names, new FormattableMarkup('All default configuration of @module module found.', [
    '@module' => $module,
  ]));
}