View source
<?php
namespace Drupal\KernelTests\Core\Theme;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ExtensionNameLengthException;
use Drupal\Core\Extension\MissingDependencyException;
use Drupal\Core\Extension\ModuleUninstallValidatorException;
use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\KernelTests\KernelTestBase;
class ThemeInstallerTest extends KernelTestBase {
public static $modules = [
'system',
];
public function register(ContainerBuilder $container) {
parent::register($container);
$container
->register('router.dumper', 'Drupal\\Core\\Routing\\NullMatcherDumper');
}
protected function setUp() {
parent::setUp();
$this
->installConfig([
'system',
]);
}
public function testEmpty() {
$this
->assertEmpty($this
->extensionConfig()
->get('theme'));
$this
->assertEmpty(array_keys($this
->themeHandler()
->listInfo()));
$this
->assertEmpty(array_keys(\Drupal::service('theme_handler')
->listInfo()));
$this
->assertNotEmpty($this
->themeHandler()
->rebuildThemeData()['stark'], 'ThemeHandler::rebuildThemeData() yields all available themes.');
$this
->assertIdentical(theme_get_setting('features.favicon'), TRUE);
}
public function testInstall() {
$name = 'test_basetheme';
$themes = $this
->themeHandler()
->listInfo();
$this
->assertFalse(isset($themes[$name]));
$this
->themeInstaller()
->install([
$name,
]);
$this
->assertIdentical($this
->extensionConfig()
->get("theme.{$name}"), 0);
$themes = $this
->themeHandler()
->listInfo();
$this
->assertTrue(isset($themes[$name]));
$this
->assertEqual($themes[$name]
->getName(), $name);
$this
->assertIdentical(theme_get_setting('features.favicon', $name), FALSE);
$this
->assertEqual(theme_get_setting('base', $name), 'only');
$this
->assertEqual(theme_get_setting('override', $name), 'base');
}
public function testInstallSubTheme() {
$name = 'test_subtheme';
$base_name = 'test_basetheme';
$themes = $this
->themeHandler()
->listInfo();
$this
->assertEmpty(array_keys($themes));
$this
->themeInstaller()
->install([
$name,
]);
$themes = $this
->themeHandler()
->listInfo();
$this
->assertTrue(isset($themes[$name]));
$this
->assertTrue(isset($themes[$base_name]));
$this
->themeInstaller()
->uninstall([
$name,
]);
$themes = $this
->themeHandler()
->listInfo();
$this
->assertFalse(isset($themes[$name]));
$this
->assertTrue(isset($themes[$base_name]));
}
public function testInstallNonExisting() {
$name = 'non_existing_theme';
$themes = $this
->themeHandler()
->listInfo();
$this
->assertEmpty(array_keys($themes));
try {
$message = 'ThemeInstaller::install() throws UnknownExtensionException upon installing a non-existing theme.';
$this
->themeInstaller()
->install([
$name,
]);
$this
->fail($message);
} catch (\Exception $e) {
$this
->assertInstanceOf(UnknownExtensionException::class, $e);
}
$themes = $this
->themeHandler()
->listInfo();
$this
->assertEmpty(array_keys($themes));
}
public function testInstallNameTooLong() {
$name = 'test_theme_having_veery_long_name_which_is_too_long';
try {
$message = 'ThemeInstaller::install() throws ExtensionNameLengthException upon installing a theme with a too long name.';
$this
->themeInstaller()
->install([
$name,
]);
$this
->fail($message);
} catch (\Exception $e) {
$this
->assertInstanceOf(ExtensionNameLengthException::class, $e);
}
}
public function testInstallThemeWithUnmetModuleDependencies($theme_name, $installed_modules, $message) {
$this->container
->get('module_installer')
->install($installed_modules);
$themes = $this
->themeHandler()
->listInfo();
$this
->assertEmpty($themes);
$this
->expectException(MissingDependencyException::class);
$this
->expectExceptionMessage($message);
$this
->themeInstaller()
->install([
$theme_name,
]);
}
public function providerTestInstallThemeWithUnmetModuleDependencies() {
return [
'theme with uninstalled module dependencies' => [
'test_theme_depending_on_modules',
[],
"Unable to install theme: 'test_theme_depending_on_modules' due to unmet module dependencies: 'test_module_required_by_theme, test_another_module_required_by_theme'.",
],
'theme with a base theme with uninstalled module dependencies' => [
'test_theme_with_a_base_theme_depending_on_modules',
[],
"Unable to install theme: 'test_theme_with_a_base_theme_depending_on_modules' due to unmet module dependencies: 'test_module_required_by_theme, test_another_module_required_by_theme'.",
],
'theme and base theme have uninstalled module dependencies' => [
'test_theme_mixed_module_dependencies',
[],
"Unable to install theme: 'test_theme_mixed_module_dependencies' due to unmet module dependencies: 'help, test_module_required_by_theme, test_another_module_required_by_theme'.",
],
'theme with already installed module dependencies, base theme module dependencies are not installed' => [
'test_theme_mixed_module_dependencies',
[
'help',
],
"Unable to install theme: 'test_theme_mixed_module_dependencies' due to unmet module dependencies: 'test_module_required_by_theme, test_another_module_required_by_theme'.",
],
'theme with module dependencies not installed, base theme module dependencies are already installed, ' => [
'test_theme_mixed_module_dependencies',
[
'test_module_required_by_theme',
'test_another_module_required_by_theme',
],
"Unable to install theme: 'test_theme_mixed_module_dependencies' due to unmet module dependencies: 'help'.",
],
'theme depending on a module that does not exist' => [
'test_theme_depending_on_nonexisting_module',
[],
"Unable to install theme: 'test_theme_depending_on_nonexisting_module' due to unmet module dependencies: 'test_module_non_existing",
],
'theme depending on an installed but incompatible module' => [
'test_theme_depending_on_constrained_modules',
[
'test_module_compatible_constraint',
'test_module_incompatible_constraint',
],
"Unable to install theme: Test Module Theme Depends on with Incompatible Constraint (>=8.x-2.x) (incompatible with version 8.x-1.8)",
],
];
}
public function testInstallThemeWithMetModuleDependencies() {
$name = 'test_theme_depending_on_modules';
$themes = $this
->themeHandler()
->listInfo();
$this
->assertArrayNotHasKey($name, $themes);
$this->container
->get('module_installer')
->install([
'test_module_required_by_theme',
'test_another_module_required_by_theme',
]);
$this
->themeInstaller()
->install([
$name,
]);
$themes = $this
->themeHandler()
->listInfo();
$this
->assertArrayHasKey($name, $themes);
$this
->expectException(ModuleUninstallValidatorException::class);
$this
->expectExceptionMessage('The following reasons prevent the modules from being uninstalled: Required by the theme: Test Theme Depending on Modules');
$this->container
->get('module_installer')
->uninstall([
'test_module_required_by_theme',
]);
}
public function testUninstallDefault() {
$name = 'stark';
$other_name = 'bartik';
$this
->themeInstaller()
->install([
$name,
$other_name,
]);
$this
->config('system.theme')
->set('default', $name)
->save();
$themes = $this
->themeHandler()
->listInfo();
$this
->assertTrue(isset($themes[$name]));
$this
->assertTrue(isset($themes[$other_name]));
try {
$message = 'ThemeInstaller::uninstall() throws InvalidArgumentException upon disabling default theme.';
$this
->themeInstaller()
->uninstall([
$name,
]);
$this
->fail($message);
} catch (\Exception $e) {
$this
->assertInstanceOf(\InvalidArgumentException::class, $e);
}
$themes = $this
->themeHandler()
->listInfo();
$this
->assertTrue(isset($themes[$name]));
$this
->assertTrue(isset($themes[$other_name]));
}
public function testUninstallAdmin() {
$name = 'stark';
$other_name = 'bartik';
$this
->themeInstaller()
->install([
$name,
$other_name,
]);
$this
->config('system.theme')
->set('admin', $name)
->save();
$themes = $this
->themeHandler()
->listInfo();
$this
->assertTrue(isset($themes[$name]));
$this
->assertTrue(isset($themes[$other_name]));
try {
$message = 'ThemeInstaller::uninstall() throws InvalidArgumentException upon disabling admin theme.';
$this
->themeInstaller()
->uninstall([
$name,
]);
$this
->fail($message);
} catch (\Exception $e) {
$this
->assertInstanceOf(\InvalidArgumentException::class, $e);
}
$themes = $this
->themeHandler()
->listInfo();
$this
->assertTrue(isset($themes[$name]));
$this
->assertTrue(isset($themes[$other_name]));
}
public function testUninstallSubTheme() {
$name = 'test_subtheme';
$base_name = 'test_basetheme';
$this
->themeInstaller()
->install([
$name,
]);
$this
->themeInstaller()
->uninstall([
$name,
]);
$themes = $this
->themeHandler()
->listInfo();
$this
->assertFalse(isset($themes[$name]));
$this
->assertTrue(isset($themes[$base_name]));
}
public function testUninstallBaseBeforeSubTheme() {
$name = 'test_basetheme';
$sub_name = 'test_subtheme';
$this
->themeInstaller()
->install([
$sub_name,
]);
try {
$message = 'ThemeInstaller::install() throws InvalidArgumentException upon uninstalling base theme before sub theme.';
$this
->themeInstaller()
->uninstall([
$name,
]);
$this
->fail($message);
} catch (\Exception $e) {
$this
->assertInstanceOf(\InvalidArgumentException::class, $e);
}
$themes = $this
->themeHandler()
->listInfo();
$this
->assertTrue(isset($themes[$name]));
$this
->assertTrue(isset($themes[$sub_name]));
$this
->themeInstaller()
->uninstall([
$name,
$sub_name,
]);
$themes = $this
->themeHandler()
->listInfo();
$this
->assertFalse(isset($themes[$name]));
$this
->assertFalse(isset($themes[$sub_name]));
}
public function testUninstallNonExisting() {
$name = 'non_existing_theme';
$themes = $this
->themeHandler()
->listInfo();
$this
->assertEmpty(array_keys($themes));
try {
$message = 'ThemeInstaller::uninstall() throws UnknownExtensionException upon uninstalling a non-existing theme.';
$this
->themeInstaller()
->uninstall([
$name,
]);
$this
->fail($message);
} catch (\Exception $e) {
$this
->assertInstanceOf(UnknownExtensionException::class, $e);
}
$themes = $this
->themeHandler()
->listInfo();
$this
->assertEmpty(array_keys($themes));
}
public function testUninstall() {
$name = 'test_basetheme';
$this
->themeInstaller()
->install([
$name,
]);
$this
->assertNotEmpty($this
->config("{$name}.settings")
->get());
$this
->themeInstaller()
->uninstall([
$name,
]);
$this
->assertEmpty(array_keys($this
->themeHandler()
->listInfo()));
$this
->assertEmpty($this
->config("{$name}.settings")
->get());
$this
->themeInstaller()
->install([
$name,
]);
$themes = $this
->themeHandler()
->listInfo();
$this
->assertTrue(isset($themes[$name]));
$this
->assertEqual($themes[$name]
->getName(), $name);
$this
->assertNotEmpty($this
->config("{$name}.settings")
->get());
}
public function testUninstallNotInstalled() {
$name = 'test_basetheme';
try {
$message = 'ThemeInstaller::uninstall() throws UnknownExtensionException upon uninstalling a theme that is not installed.';
$this
->themeInstaller()
->uninstall([
$name,
]);
$this
->fail($message);
} catch (\Exception $e) {
$this
->assertInstanceOf(UnknownExtensionException::class, $e);
}
}
public function testThemeInfoAlter() {
$name = 'seven';
$this->container
->get('state')
->set('module_test.hook_system_info_alter', TRUE);
$this
->themeInstaller()
->install([
$name,
]);
$themes = $this
->themeHandler()
->listInfo();
$this
->assertFalse(isset($themes[$name]->info['regions']['test_region']));
$this
->moduleInstaller()
->install([
'module_test',
], FALSE);
$this
->assertTrue($this
->moduleHandler()
->moduleExists('module_test'));
$themes = $this
->themeHandler()
->listInfo();
$this
->assertTrue(isset($themes[$name]->info['regions']['test_region']));
$info = \Drupal::service('extension.list.theme')
->getExtensionInfo($name);
$this
->assertTrue(isset($info['regions']['test_region']));
$regions = system_region_list($name);
$this
->assertTrue(isset($regions['test_region']));
$theme_list = \Drupal::service('theme_handler')
->listInfo();
$this
->assertTrue(isset($theme_list[$name]->info['regions']['test_region']));
$this
->moduleInstaller()
->uninstall([
'module_test',
]);
$this
->assertFalse($this
->moduleHandler()
->moduleExists('module_test'));
$themes = $this
->themeHandler()
->listInfo();
$this
->assertFalse(isset($themes[$name]->info['regions']['test_region']));
$info = \Drupal::service('extension.list.theme')
->getExtensionInfo($name);
$this
->assertFalse(isset($info['regions']['test_region']));
$regions = system_region_list($name);
$this
->assertFalse(isset($regions['test_region']));
$theme_list = \Drupal::service('theme_handler')
->listInfo();
$this
->assertFalse(isset($theme_list[$name]->info['regions']['test_region']));
}
protected function themeHandler() {
return $this->container
->get('theme_handler');
}
protected function themeInstaller() {
return $this->container
->get('theme_installer');
}
protected function extensionConfig() {
return $this
->config('core.extension');
}
protected function moduleHandler() {
return $this->container
->get('module_handler');
}
protected function moduleInstaller() {
return $this->container
->get('module_installer');
}
}