StableLibraryOverrideTest.php in Drupal 9
File
core/tests/Drupal/KernelTests/Core/Theme/StableLibraryOverrideTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Theme;
use Drupal\Core\Extension\ExtensionLifecycle;
use Drupal\KernelTests\KernelTestBase;
class StableLibraryOverrideTest extends KernelTestBase {
protected $themeManager;
protected $themeInitialization;
protected $libraryDiscovery;
protected $allModules;
protected $librariesToSkip = [];
protected static $modules = [
'system',
'user',
'path_alias',
];
protected function setUp() : void {
parent::setUp();
$this->container
->get('theme_installer')
->install([
'stable',
]);
$all_modules = $this->container
->get('extension.list.module')
->getList();
$all_modules = array_filter($all_modules, function ($module) {
if ($module->origin !== 'core' || !empty($module->info['hidden']) || $module->status == TRUE || $module->info['package'] == 'Testing' || $module->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::EXPERIMENTAL) {
return FALSE;
}
return TRUE;
});
$this->allModules = array_keys($all_modules);
$this->allModules[] = 'system';
$this->allModules[] = 'user';
$this->allModules[] = 'path_alias';
sort($this->allModules);
$this->container
->get('module_installer')
->install($this->allModules);
$this->themeManager = $this->container
->get('theme.manager');
$this->themeInitialization = $this->container
->get('theme.initialization');
$this->libraryDiscovery = $this->container
->get('library.discovery');
}
public function testStableLibraryOverrides() {
$libraries_before = $this
->getAllLibraries();
$libraries_before = $this
->removeVendorAssets($libraries_before);
$this->themeManager
->setActiveTheme($this->themeInitialization
->getActiveThemeByName('stable'));
$this->libraryDiscovery
->clearCachedDefinitions();
$libraries_after = $this
->getAllLibraries();
$libraries_after = $this
->removeVendorAssets($libraries_after);
foreach ($libraries_before as $extension => $libraries) {
foreach ($libraries as $library_name => $library) {
if (in_array("{$extension}/{$library_name}", $this->librariesToSkip)) {
continue;
}
$library_after = $libraries_after[$extension][$library_name];
foreach ($library['css'] as $index => $asset) {
$clean_path = $asset['data'];
$stable_path = $library_after['css'][$index]['data'];
$replacements = [
'core/misc/' => "core/modules/core/css/",
];
$expected_path = strtr($clean_path, $replacements);
$expected_path = str_replace("core/modules/{$extension}/css/", "core/themes/stable/css/{$extension}/", $expected_path);
$assert_path = str_replace("core/modules/{$extension}/", '', $clean_path);
$this
->assertEquals($expected_path, $stable_path, "{$assert_path} from the {$extension}/{$library_name} library is overridden in Stable.");
}
}
}
}
protected function removeVendorAssets($all_libraries) {
foreach ($all_libraries as $extension => $libraries) {
foreach ($libraries as $library_name => $library) {
if (isset($library['remote'])) {
unset($all_libraries[$extension][$library_name]);
}
foreach ([
'css',
'js',
] as $asset_type) {
foreach ($library[$asset_type] as $index => $asset) {
if (strpos($asset['data'], 'core/assets/vendor') !== FALSE) {
unset($all_libraries[$extension][$library_name][$asset_type][$index]);
if (!empty($all_libraries[$extension][$library_name][$asset_type])) {
$all_libraries[$extension][$library_name][$asset_type] = array_values($all_libraries[$extension][$library_name][$asset_type]);
}
}
}
}
}
}
return $all_libraries;
}
protected function getAllLibraries() {
$modules = \Drupal::moduleHandler()
->getModuleList();
$module_list = array_keys($modules);
sort($module_list);
$this
->assertEquals($this->allModules, $module_list, 'All core modules are installed.');
$libraries['core'] = $this->libraryDiscovery
->getLibrariesByExtension('core');
foreach ($modules as $module_name => $module) {
$library_file = $module
->getPath() . '/' . $module_name . '.libraries.yml';
if (is_file($this->root . '/' . $library_file)) {
$libraries[$module_name] = $this->libraryDiscovery
->getLibrariesByExtension($module_name);
}
}
return $libraries;
}
}