3 namespace Drupal\KernelTests\Core\Asset;
5 use Drupal\KernelTests\KernelTestBase;
8 * Tests that the asset files for all core libraries exist.
10 * This test also changes the active theme to each core theme to verify
11 * the libraries after theme-level libraries-override and libraries-extend are
16 class ResolvedLibraryDefinitionsFilesMatchTest extends KernelTestBase {
21 * @var \Drupal\Core\Extension\ThemeHandlerInterface
23 protected $themeHandler;
26 * The theme initialization.
28 * @var \Drupal\Core\Theme\ThemeInitializationInterface
30 protected $themeInitialization;
35 * @var \Drupal\Core\Theme\ThemeManagerInterface
37 protected $themeManager;
40 * The library discovery service.
42 * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
44 protected $libraryDiscovery;
47 * A list of all core modules.
51 protected $allModules;
54 * A list of all core themes.
56 * We hardcode this because test themes don't use a 'package' or 'hidden' key
57 * so we don't have a good way of filtering to only get "real" themes.
61 protected $allThemes = [
70 * A list of libraries to skip checking, in the format extension/library_name.
74 protected $librariesToSkip = [
75 // Locale has a "dummy" library that does not actually exist.
76 'locale/translations',
80 * A list of all paths that have been checked.
84 protected $pathsChecked;
89 public static $modules = ['system'];
94 protected function setUp() {
97 // Install all core themes.
98 sort($this->allThemes);
99 $this->container->get('theme_installer')->install($this->allThemes);
101 // Enable all core modules.
102 $all_modules = system_rebuild_module_data();
103 $all_modules = array_filter($all_modules, function ($module) {
104 // Filter contrib, hidden, already enabled modules and modules in the
106 if ($module->origin !== 'core' || !empty($module->info['hidden']) || $module->status == TRUE || $module->info['package'] == 'Testing') {
111 $this->allModules = array_keys($all_modules);
112 $this->allModules[] = 'system';
113 sort($this->allModules);
114 $this->container->get('module_installer')->install($this->allModules);
116 $this->themeHandler = $this->container->get('theme_handler');
117 $this->themeInitialization = $this->container->get('theme.initialization');
118 $this->themeManager = $this->container->get('theme.manager');
119 $this->libraryDiscovery = $this->container->get('library.discovery');
123 * Ensures that all core module and theme library files exist.
125 public function testCoreLibraryCompleteness() {
126 // First verify all libraries with no active theme.
127 $this->verifyLibraryFilesExist($this->getAllLibraries());
129 // Then verify all libraries for each core theme. This may seem like
130 // overkill but themes can override and extend other extensions' libraries
131 // and these changes are only applied for the active theme.
132 foreach ($this->allThemes as $theme) {
133 $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName($theme));
134 $this->libraryDiscovery->clearCachedDefinitions();
136 $this->verifyLibraryFilesExist($this->getAllLibraries());
141 * Checks that all the library files exist.
143 * @param array[] $library_definitions
144 * An array of library definitions, keyed by extension, then by library, and
147 protected function verifyLibraryFilesExist($library_definitions) {
148 $root = \Drupal::root();
149 foreach ($library_definitions as $extension => $libraries) {
150 foreach ($libraries as $library_name => $library) {
151 if (in_array("$extension/$library_name", $this->librariesToSkip)) {
155 // Check that all the assets exist.
156 foreach (['css', 'js'] as $asset_type) {
157 foreach ($library[$asset_type] as $asset) {
158 $file = $asset['data'];
159 $path = $root . '/' . $file;
160 // Only check and assert each file path once.
161 if (!isset($this->pathsChecked[$path])) {
162 $this->assertTrue(is_file($path), "$file file referenced from the $extension/$library_name library exists.");
163 $this->pathsChecked[$path] = TRUE;
172 * Gets all libraries for core and all installed modules.
174 * @return \Drupal\Core\Extension\Extension[]
176 protected function getAllLibraries() {
177 $modules = \Drupal::moduleHandler()->getModuleList();
178 $extensions = $modules;
179 $module_list = array_keys($modules);
181 $this->assertEqual($this->allModules, $module_list, 'All core modules are installed.');
183 $themes = $this->themeHandler->listInfo();
184 $extensions += $themes;
185 $theme_list = array_keys($themes);
187 $this->assertEqual($this->allThemes, $theme_list, 'All core themes are installed.');
189 $libraries['core'] = $this->libraryDiscovery->getLibrariesByExtension('core');
191 $root = \Drupal::root();
192 foreach ($extensions as $extension_name => $extension) {
193 $library_file = $extension->getPath() . '/' . $extension_name . '.libraries.yml';
194 if (is_file($root . '/' . $library_file)) {
195 $libraries[$extension_name] = $this->libraryDiscovery->getLibrariesByExtension($extension_name);