From 62027b5d4d061e564d2d883471d1fd50864c01ed Mon Sep 17 00:00:00 2001 From: Dave Long <dave@longwaveconsulting.com> Date: Thu, 26 Sep 2024 10:34:58 +0200 Subject: [PATCH] Issue #3457863 by grimreaper, catch, pdureau, maboy, longwave: YAML discovery does not take theme inheritance into account (cherry picked from commit 5e0ff5a872f4682bdef93ccb05fd3e4ecb696f46) --- .../Drupal/Core/Extension/ThemeHandler.php | 6 +- .../test_child_theme.info.yml | 4 ++ .../test_child_theme.layouts.yml | 3 + .../test_parent_theme.info.yml | 4 ++ .../test_parent_theme.layouts.yml | 3 + .../Core/Layout/LayoutPluginManagerTest.php | 64 +++++++++++++++++++ .../Core/Theme/ThemeInstallerTest.php | 10 ++- 7 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 core/modules/system/tests/themes/test_child_theme/test_child_theme.info.yml create mode 100644 core/modules/system/tests/themes/test_child_theme/test_child_theme.layouts.yml create mode 100644 core/modules/system/tests/themes/test_parent_theme/test_parent_theme.info.yml create mode 100644 core/modules/system/tests/themes/test_parent_theme/test_parent_theme.layouts.yml create mode 100644 core/tests/Drupal/KernelTests/Core/Layout/LayoutPluginManagerTest.php diff --git a/core/lib/Drupal/Core/Extension/ThemeHandler.php b/core/lib/Drupal/Core/Extension/ThemeHandler.php index 792be25ab6da..6ea873af2b5e 100644 --- a/core/lib/Drupal/Core/Extension/ThemeHandler.php +++ b/core/lib/Drupal/Core/Extension/ThemeHandler.php @@ -69,8 +69,10 @@ public function listInfo() { $this->list = []; $installed_themes = $this->configFactory->get('core.extension')->get('theme'); if (!empty($installed_themes)) { - $installed_themes = array_intersect_key($this->themeList->getList(), $installed_themes); - array_map([$this, 'addTheme'], $installed_themes); + $list = $this->themeList->getList(); + foreach (array_keys($installed_themes) as $theme_name) { + $this->addTheme($list[$theme_name]); + } } } return $this->list; diff --git a/core/modules/system/tests/themes/test_child_theme/test_child_theme.info.yml b/core/modules/system/tests/themes/test_child_theme/test_child_theme.info.yml new file mode 100644 index 000000000000..9f053829cd43 --- /dev/null +++ b/core/modules/system/tests/themes/test_child_theme/test_child_theme.info.yml @@ -0,0 +1,4 @@ +name: 'Theme test child theme' +type: theme +version: VERSION +base theme: test_parent_theme diff --git a/core/modules/system/tests/themes/test_child_theme/test_child_theme.layouts.yml b/core/modules/system/tests/themes/test_child_theme/test_child_theme.layouts.yml new file mode 100644 index 000000000000..7c3861ed394d --- /dev/null +++ b/core/modules/system/tests/themes/test_child_theme/test_child_theme.layouts.yml @@ -0,0 +1,3 @@ +theme_parent_provided_layout: + class: '\Drupal\Core\Layout\LayoutDefault' + label: Child diff --git a/core/modules/system/tests/themes/test_parent_theme/test_parent_theme.info.yml b/core/modules/system/tests/themes/test_parent_theme/test_parent_theme.info.yml new file mode 100644 index 000000000000..6e4d4164b4e2 --- /dev/null +++ b/core/modules/system/tests/themes/test_parent_theme/test_parent_theme.info.yml @@ -0,0 +1,4 @@ +name: 'Theme test parent theme' +type: theme +version: VERSION +base theme: false diff --git a/core/modules/system/tests/themes/test_parent_theme/test_parent_theme.layouts.yml b/core/modules/system/tests/themes/test_parent_theme/test_parent_theme.layouts.yml new file mode 100644 index 000000000000..a07c1be3ce02 --- /dev/null +++ b/core/modules/system/tests/themes/test_parent_theme/test_parent_theme.layouts.yml @@ -0,0 +1,3 @@ +theme_parent_provided_layout: + class: '\Drupal\Core\Layout\LayoutDefault' + label: Parent diff --git a/core/tests/Drupal/KernelTests/Core/Layout/LayoutPluginManagerTest.php b/core/tests/Drupal/KernelTests/Core/Layout/LayoutPluginManagerTest.php new file mode 100644 index 000000000000..050f76567c8e --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Layout/LayoutPluginManagerTest.php @@ -0,0 +1,64 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\KernelTests\Core\Layout; + +use Drupal\Core\Layout\LayoutDefinition; +use Drupal\KernelTests\KernelTestBase; + +/** + * @coversDefaultClass \Drupal\Core\Layout\LayoutPluginManager + * @group Layout + */ +class LayoutPluginManagerTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + protected static $modules = ['layout_discovery']; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + $this->container->get('theme_installer')->install(['test_child_theme']); + $this->activateTheme('test_child_theme'); + } + + /** + * Tests that layout plugins are correctly overridden. + */ + public function testPluginOverride(): void { + /** @var \Drupal\Core\Layout\LayoutPluginManagerInterface $layouts_manager */ + $layouts_manager = $this->container->get('plugin.manager.core.layout'); + $definitions = $layouts_manager->getDefinitions(); + + $this->assertInstanceOf(LayoutDefinition::class, $definitions['theme_parent_provided_layout']); + $this->assertSame('Child', $definitions['theme_parent_provided_layout']->getLabel()->render()); + } + + /** + * Activates a specified theme. + * + * Installs the theme if not already installed and makes it the active theme. + * + * @param string $theme_name + * The name of the theme to be activated. + */ + protected function activateTheme(string $theme_name): void { + $this->container->get('theme_installer')->install([$theme_name]); + + /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */ + $theme_initializer = $this->container->get('theme.initialization'); + + /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */ + $theme_manager = $this->container->get('theme.manager'); + + $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName($theme_name)); + $this->assertSame($theme_name, $theme_manager->getActiveTheme()->getName()); + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Theme/ThemeInstallerTest.php b/core/tests/Drupal/KernelTests/Core/Theme/ThemeInstallerTest.php index 980a500e59f5..71d8cd80e51d 100644 --- a/core/tests/Drupal/KernelTests/Core/Theme/ThemeInstallerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Theme/ThemeInstallerTest.php @@ -88,8 +88,8 @@ public function testInstall(): void { * Tests installing a sub-theme. */ public function testInstallSubTheme(): void { - $name = 'test_subtheme'; - $base_name = 'test_basetheme'; + $name = 'test_child_theme'; + $base_name = 'test_parent_theme'; $themes = $this->themeHandler()->listInfo(); $this->assertEmpty(array_keys($themes)); @@ -100,6 +100,12 @@ public function testInstallSubTheme(): void { $this->assertTrue(isset($themes[$name])); $this->assertTrue(isset($themes[$base_name])); + $expectedOrder = [ + $base_name, + $name, + ]; + $this->assertEquals($expectedOrder, array_keys($themes)); + $this->themeInstaller()->uninstall([$name]); $themes = $this->themeHandler()->listInfo(); -- GitLab