3 namespace Drupal\KernelTests\Core\Theme;
5 use Drupal\KernelTests\KernelTestBase;
8 * Tests Stable's library overrides.
12 class StableLibraryOverrideTest extends KernelTestBase {
17 * @var \Drupal\Core\Theme\ThemeManagerInterface
19 protected $themeManager;
22 * The theme initialization.
24 * @var \Drupal\Core\Theme\ThemeInitializationInterface
26 protected $themeInitialization;
29 * The library discovery service.
31 * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
33 protected $libraryDiscovery;
36 * A list of all core modules.
40 protected $allModules;
43 * A list of libraries to skip checking, in the format extension/library_name.
47 protected $librariesToSkip = [];
52 public static $modules = ['system'];
57 protected function setUp() {
60 $this->container->get('theme_installer')->install(['stable']);
62 // Enable all core modules.
63 $all_modules = system_rebuild_module_data();
64 $all_modules = array_filter($all_modules, function ($module) {
65 // Filter contrib, hidden, experimental, already enabled modules, and
66 // modules in the Testing package.
67 if ($module->origin !== 'core' || !empty($module->info['hidden']) || $module->status == TRUE || $module->info['package'] == 'Testing' || $module->info['package'] == 'Core (Experimental)') {
72 $this->allModules = array_keys($all_modules);
73 $this->allModules[] = 'system';
74 sort($this->allModules);
75 $this->container->get('module_installer')->install($this->allModules);
77 $this->themeManager = $this->container->get('theme.manager');
78 $this->themeInitialization = $this->container->get('theme.initialization');
79 $this->libraryDiscovery = $this->container->get('library.discovery');
83 * Ensures that Stable overrides all relevant core library assets.
85 public function testStableLibraryOverrides() {
86 // First get the clean library definitions with no active theme.
87 $libraries_before = $this->getAllLibraries();
88 $libraries_before = $this->removeVendorAssets($libraries_before);
90 $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName('stable'));
91 $this->libraryDiscovery->clearCachedDefinitions();
93 // Now get the library definitions with Stable as the active theme.
94 $libraries_after = $this->getAllLibraries();
95 $libraries_after = $this->removeVendorAssets($libraries_after);
97 $root = \Drupal::root();
98 foreach ($libraries_before as $extension => $libraries) {
99 foreach ($libraries as $library_name => $library) {
100 // Allow skipping libraries.
101 if (in_array("$extension/$library_name", $this->librariesToSkip)) {
104 $library_after = $libraries_after[$extension][$library_name];
106 // Check that all the CSS assets are overridden.
107 foreach ($library['css'] as $index => $asset) {
108 $clean_path = $asset['data'];
109 $stable_path = $library_after['css'][$index]['data'];
110 // Make core/misc assets look like they are coming from a "core"
113 'core/misc/' => "core/modules/core/css/",
115 $expected_path = strtr($clean_path, $replacements);
117 // Adjust the module asset paths to correspond with the Stable folder
119 $expected_path = str_replace("core/modules/$extension/css/", "core/themes/stable/css/$extension/", $expected_path);
120 $assert_path = str_replace("core/modules/$extension/", '', $clean_path);
122 $this->assertEqual($expected_path, $stable_path, "$assert_path from the $extension/$library_name library is overridden in Stable.");
129 * Removes all vendor libraries and assets from the library definitions.
131 * @param array[] $all_libraries
132 * An associative array of libraries keyed by extension, then by library
136 * The reduced array of libraries.
138 protected function removeVendorAssets($all_libraries) {
139 foreach ($all_libraries as $extension => $libraries) {
140 foreach ($libraries as $library_name => $library) {
141 if (isset($library['remote'])) {
142 unset($all_libraries[$extension][$library_name]);
144 foreach (['css', 'js'] as $asset_type) {
145 foreach ($library[$asset_type] as $index => $asset) {
146 if (strpos($asset['data'], 'core/assets/vendor') !== FALSE) {
147 unset($all_libraries[$extension][$library_name][$asset_type][$index]);
148 // Re-key the array of assets. This is needed because
149 // libraries-override doesn't always preserve the order.
150 if (!empty($all_libraries[$extension][$library_name][$asset_type])) {
151 $all_libraries[$extension][$library_name][$asset_type] = array_values($all_libraries[$extension][$library_name][$asset_type]);
158 return $all_libraries;
162 * Gets all libraries for core and all installed modules.
165 * An associative array of libraries keyed by extension, then by library
168 protected function getAllLibraries() {
169 $modules = \Drupal::moduleHandler()->getModuleList();
170 $module_list = array_keys($modules);
172 $this->assertEqual($this->allModules, $module_list, 'All core modules are installed.');
174 $libraries['core'] = $this->libraryDiscovery->getLibrariesByExtension('core');
176 $root = \Drupal::root();
177 foreach ($modules as $module_name => $module) {
178 $library_file = $module->getPath() . '/' . $module_name . '.libraries.yml';
179 if (is_file($root . '/' . $library_file)) {
180 $libraries[$module_name] = $this->libraryDiscovery->getLibrariesByExtension($module_name);