function DefaultConfigTest::assertExtensionConfig

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Config/DefaultConfigTest.php \Drupal\KernelTests\Config\DefaultConfigTest::assertExtensionConfig()
  2. 10 core/tests/Drupal/KernelTests/Config/DefaultConfigTest.php \Drupal\KernelTests\Config\DefaultConfigTest::assertExtensionConfig()

Tests that the config provided by the extension is correct.

@internal

Parameters

string $name: Extension name.

string $type: Extension type, either 'module' or 'theme'.

2 calls to DefaultConfigTest::assertExtensionConfig()
DefaultConfigTest::testModuleConfig in core/tests/Drupal/KernelTests/Config/DefaultConfigTest.php
Tests if installed config is equal to the exported config.
DefaultConfigTest::testThemeConfig in core/tests/Drupal/KernelTests/Config/DefaultConfigTest.php
Tests if installed config is equal to the exported config.

File

core/tests/Drupal/KernelTests/Config/DefaultConfigTest.php, line 77

Class

DefaultConfigTest
Tests that the installed config matches the default config.

Namespace

Drupal\KernelTests\Config

Code

protected function assertExtensionConfig(string $name, string $type) : void {
    // Parse .info.yml file for module/theme $name. Since it's not installed at
    // this point we can't retrieve it from the 'module_handler' service.
    switch ($name) {
        case 'test_deprecated_theme':
            $file_name = DRUPAL_ROOT . '/core/modules/system/tests/themes/' . $name . '/' . $name . '.info.yml';
            break;
        case 'deprecated_module':
            $file_name = DRUPAL_ROOT . '/core/modules/system/tests/modules/' . $name . '/' . $name . '.info.yml';
            break;
        default:
            $file_name = DRUPAL_ROOT . '/core/' . $type . 's/' . $name . '/' . $name . '.info.yml';
    }
    $info = \Drupal::service('info_parser')->parse($file_name);
    // Test we have a parsed info.yml file.
    $this->assertNotEmpty($info);
    // Skip deprecated extensions.
    if (isset($info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER]) && $info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::DEPRECATED) {
        $this->markTestSkipped("The {$type} '{$name}' is deprecated.");
    }
    // System and user are required in order to be able to install some of the
    // other modules. Therefore they are put into static::$modules, which though
    // doesn't install config files, so import those config files explicitly. Do
    // this for all tests in case optional configuration depends on it.
    $this->installConfig([
        'system',
        'user',
    ]);
    $extension_path = \Drupal::service('extension.path.resolver')->getPath($type, $name) . '/';
    $extension_config_storage = new FileStorage($extension_path . InstallStorage::CONFIG_INSTALL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
    $optional_config_storage = new FileStorage($extension_path . InstallStorage::CONFIG_OPTIONAL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
    if (empty($optional_config_storage->listAll()) && empty($extension_config_storage->listAll())) {
        $this->markTestSkipped("{$name} has no configuration to test");
    }
    // Work out any additional modules and themes that need installing to create
    // an optional config.
    $modules_to_install = $type !== 'theme' ? [
        $name,
    ] : [];
    $themes_to_install = $type === 'theme' ? [
        $name,
    ] : [];
    foreach ($optional_config_storage->listAll() as $config_name) {
        $data = $optional_config_storage->read($config_name);
        $dependency = new ConfigEntityDependency($config_name, $data);
        $modules_to_install = array_merge($modules_to_install, $dependency->getDependencies('module'));
        $themes_to_install = array_merge($themes_to_install, $dependency->getDependencies('theme'));
    }
    // Remove core and standard because they cannot be installed.
    $modules_to_install = array_diff(array_unique($modules_to_install), [
        'core',
        'standard',
    ]);
    $this->container
        ->get('module_installer')
        ->install($modules_to_install);
    $this->container
        ->get('theme_installer')
        ->install(array_unique($themes_to_install));
    // Test configuration in the extension's config/install directory.
    $this->doTestsOnConfigStorage($extension_config_storage, $name, $type);
    // Test configuration in the extension's config/optional directory.
    $this->doTestsOnConfigStorage($optional_config_storage, $name, $type);
}

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