function ConfigImportThemeInstallTest::testConfigImportWithThemeWithModuleDependencies

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Theme/ConfigImportThemeInstallTest.php \Drupal\KernelTests\Core\Theme\ConfigImportThemeInstallTest::testConfigImportWithThemeWithModuleDependencies()
  2. 11.x core/tests/Drupal/KernelTests/Core/Theme/ConfigImportThemeInstallTest.php \Drupal\KernelTests\Core\Theme\ConfigImportThemeInstallTest::testConfigImportWithThemeWithModuleDependencies()

Tests config imports that install and uninstall a theme with dependencies.

File

core/tests/Drupal/KernelTests/Core/Theme/ConfigImportThemeInstallTest.php, line 33

Class

ConfigImportThemeInstallTest
Tests installing and uninstalling of themes via configuration import.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testConfigImportWithThemeWithModuleDependencies() : void {
  $this->container
    ->get('module_installer')
    ->install([
    'test_module_required_by_theme',
    'test_another_module_required_by_theme',
  ]);
  $this->container
    ->get('theme_installer')
    ->install([
    'test_theme_depending_on_modules',
  ]);
  $this->assertTrue($this->container
    ->get('theme_handler')
    ->themeExists('test_theme_depending_on_modules'), 'test_theme_depending_on_modules theme installed');
  $sync = $this->container
    ->get('config.storage.sync');
  $this->copyConfig($this->container
    ->get('config.storage'), $sync);
  $extensions = $sync->read('core.extension');
  // Remove one of the modules the theme depends on.
  unset($extensions['module']['test_module_required_by_theme']);
  $sync->write('core.extension', $extensions);
  try {
    $this->configImporter()
      ->validate();
    $this->fail('ConfigImporterException not thrown; an invalid import was not stopped due to missing dependencies.');
  } catch (ConfigImporterException $e) {
    $error_message = 'Unable to uninstall the <em class="placeholder">Test Module Required by Theme</em> module because: Required by the theme: Test Theme Depending on Modules.';
    $this->assertStringContainsString($error_message, $e->getMessage(), 'There were errors validating the config synchronization.');
    $error_log = $this->configImporter
      ->getErrors();
    $this->assertSame($error_message, (string) $error_log[0]);
  }
  // Remove the other module and the theme.
  unset($extensions['module']['test_another_module_required_by_theme']);
  unset($extensions['theme']['test_theme_depending_on_modules']);
  $sync->write('core.extension', $extensions);
  $this->configImporter()
    ->import();
  $this->assertFalse($this->container
    ->get('theme_handler')
    ->themeExists('test_theme_depending_on_modules'), 'test_theme_depending_on_modules theme uninstalled by configuration import');
  // Try installing a theme with dependencies via config import.
  $extensions['theme']['test_theme_depending_on_modules'] = 0;
  $extensions['module']['test_another_module_required_by_theme'] = 0;
  $sync->write('core.extension', $extensions);
  try {
    $this->configImporter()
      ->validate();
    $this->fail('ConfigImporterException not thrown; an invalid import was not stopped due to missing dependencies.');
  } catch (ConfigImporterException $e) {
    $error_message = 'Unable to install the <em class="placeholder">Test Theme Depending on Modules</em> theme since it requires the <em class="placeholder">Test Module Required by Theme</em> module.';
    $this->assertStringContainsString($error_message, $e->getMessage(), 'There were errors validating the config synchronization.');
    $error_log = $this->configImporter
      ->getErrors();
    $this->assertSame($error_message, (string) $error_log[0]);
  }
  $extensions['module']['test_module_required_by_theme'] = 0;
  $sync->write('core.extension', $extensions);
  $this->configImporter()
    ->import();
  $this->assertTrue($this->container
    ->get('theme_handler')
    ->themeExists('test_theme_depending_on_modules'), 'test_theme_depending_on_modules theme installed');
}

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