function ThemeUiTest::testThemeInstallWithModuleDependencies

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Theme/ThemeUiTest.php \Drupal\Tests\system\Functional\Theme\ThemeUiTest::testThemeInstallWithModuleDependencies()
  2. 10 core/modules/system/tests/src/Functional/Theme/ThemeUiTest.php \Drupal\Tests\system\Functional\Theme\ThemeUiTest::testThemeInstallWithModuleDependencies()
  3. 11.x core/modules/system/tests/src/Functional/Theme/ThemeUiTest.php \Drupal\Tests\system\Functional\Theme\ThemeUiTest::testThemeInstallWithModuleDependencies()

Tests installing a theme with module dependencies.

@dataProvider providerTestThemeInstallWithModuleDependencies

Parameters

string $theme_name: The name of the theme being tested.

string[] $first_modules: Machine names of first modules to enable.

string[] $second_modules: Machine names of second modules to enable.

string[] $required_by_messages: Expected messages when attempting to uninstall $module_names.

string $base_theme_to_uninstall: The name of the theme $theme_name has set as a base theme.

string[] $base_theme_module_names: Machine names of the modules required by $base_theme_to_uninstall.

File

core/modules/system/tests/src/Functional/Theme/ThemeUiTest.php, line 87

Class

ThemeUiTest
Tests the theme UI.

Namespace

Drupal\Tests\system\Functional\Theme

Code

public function testThemeInstallWithModuleDependencies($theme_name, array $first_modules, array $second_modules, array $required_by_messages, $base_theme_to_uninstall, array $base_theme_module_names) {
    $assert_session = $this->assertSession();
    $page = $this->getSession()
        ->getPage();
    $all_dependent_modules = array_merge($first_modules, $second_modules);
    $this->drupalGet('admin/appearance');
    $assert_module_enabled_message = function ($enabled_modules) {
        $count = count($enabled_modules);
        $module_enabled_text = $count === 1 ? "{$this->testModules[$enabled_modules[0]]} has been enabled." : $count . " modules have been enabled:";
        $this->assertSession()
            ->pageTextContains($module_enabled_text);
    };
    // All the modules should be listed as disabled.
    foreach ($all_dependent_modules as $module) {
        $expected_required_list_items[$module] = $this->testModules[$module] . " (disabled)";
    }
    $this->assertUninstallableTheme($expected_required_list_items, $theme_name);
    // Enable the first group of dependee modules.
    $first_module_form_post = [];
    foreach ($first_modules as $module) {
        $first_module_form_post["modules[{$module}][enable]"] = 1;
    }
    $this->drupalGet('admin/modules');
    $this->submitForm($first_module_form_post, 'Install');
    $assert_module_enabled_message($first_modules);
    $this->drupalGet('admin/appearance');
    // Confirm the theme is still uninstallable due to a remaining module
    // dependency.
    // The modules that have already been enabled will no longer be listed as
    // disabled.
    foreach ($first_modules as $module) {
        $expected_required_list_items[$module] = $this->testModules[$module];
    }
    $this->assertUninstallableTheme($expected_required_list_items, $theme_name);
    // Enable the second group of dependee modules.
    $second_module_form_post = [];
    foreach ($second_modules as $module) {
        $second_module_form_post["modules[{$module}][enable]"] = 1;
    }
    $this->drupalGet('admin/modules');
    $this->submitForm($second_module_form_post, 'Install');
    $assert_module_enabled_message($second_modules);
    // The theme should now be installable, so install it.
    $this->drupalGet('admin/appearance');
    $page->clickLink("Install {$theme_name} theme");
    $assert_session->addressEquals('admin/appearance');
    $assert_session->pageTextContains("The {$theme_name} theme has been installed");
    // Confirm that the dependee modules can't be uninstalled because an enabled
    // theme depends on them.
    $this->drupalGet('admin/modules/uninstall');
    foreach ($all_dependent_modules as $attribute) {
        $assert_session->elementExists('css', "[name=\"uninstall[{$attribute}]\"][disabled]");
    }
    foreach ($required_by_messages as $selector => $message) {
        $assert_session->elementTextContains('css', $selector, $message);
    }
    // Uninstall the theme that depends on the modules, and confirm the modules
    // can now be uninstalled.
    $this->uninstallTheme($theme_name);
    $this->drupalGet('admin/modules/uninstall');
    // Only attempt to uninstall modules not required by the base theme.
    $modules_to_uninstall = array_diff($all_dependent_modules, $base_theme_module_names);
    $this->uninstallModules($modules_to_uninstall);
    if (!empty($base_theme_to_uninstall)) {
        $this->uninstallTheme($base_theme_to_uninstall);
        $this->drupalGet('admin/modules/uninstall');
        $this->uninstallModules($base_theme_module_names);
    }
}

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