Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Asset / ResolvedLibraryDefinitionsFilesMatchTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Asset;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Tests that the asset files for all core libraries exist.
9  *
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
12  * applied.
13  *
14  * @group Asset
15  */
16 class ResolvedLibraryDefinitionsFilesMatchTest extends KernelTestBase {
17
18   /**
19    * The theme handler.
20    *
21    * @var \Drupal\Core\Extension\ThemeHandlerInterface
22    */
23   protected $themeHandler;
24
25   /**
26    * The theme initialization.
27    *
28    * @var \Drupal\Core\Theme\ThemeInitializationInterface
29    */
30   protected $themeInitialization;
31
32   /**
33    * The theme manager.
34    *
35    * @var \Drupal\Core\Theme\ThemeManagerInterface
36    */
37   protected $themeManager;
38
39   /**
40    * The library discovery service.
41    *
42    * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
43    */
44   protected $libraryDiscovery;
45
46   /**
47    * A list of all core modules.
48    *
49    * @var string[]
50    */
51   protected $allModules;
52
53   /**
54    * A list of all core themes.
55    *
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.
58    *
59    * @var string[]
60    */
61   protected $allThemes = [
62     'bartik',
63     'classy',
64     'seven',
65     'stable',
66     'stark',
67   ];
68
69   /**
70    * A list of libraries to skip checking, in the format extension/library_name.
71    *
72    * @var string[]
73    */
74   protected $librariesToSkip = [
75     // Locale has a "dummy" library that does not actually exist.
76     'locale/translations',
77   ];
78
79   /**
80    * A list of all paths that have been checked.
81    *
82    * @var array[]
83    */
84   protected $pathsChecked;
85
86   /**
87    * {@inheritdoc}
88    */
89   public static $modules = ['system'];
90
91   /**
92    * {@inheritdoc}
93    */
94   protected function setUp() {
95     parent::setUp();
96
97     // Install all core themes.
98     sort($this->allThemes);
99     $this->container->get('theme_installer')->install($this->allThemes);
100
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
105       // Testing package.
106       if ($module->origin !== 'core' || !empty($module->info['hidden']) || $module->status == TRUE || $module->info['package'] == 'Testing') {
107         return FALSE;
108       }
109       return TRUE;
110     });
111     $this->allModules = array_keys($all_modules);
112     $this->allModules[] = 'system';
113     sort($this->allModules);
114     $this->container->get('module_installer')->install($this->allModules);
115
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');
120   }
121
122   /**
123    * Ensures that all core module and theme library files exist.
124    */
125   public function testCoreLibraryCompleteness() {
126     // First verify all libraries with no active theme.
127     $this->verifyLibraryFilesExist($this->getAllLibraries());
128
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();
135
136       $this->verifyLibraryFilesExist($this->getAllLibraries());
137     }
138   }
139
140   /**
141    * Checks that all the library files exist.
142    *
143    * @param array[] $library_definitions
144    *   An array of library definitions, keyed by extension, then by library, and
145    *   so on.
146    */
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)) {
152           continue;
153         }
154
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;
164             }
165           }
166         }
167       }
168     }
169   }
170
171   /**
172    * Gets all libraries for core and all installed modules.
173    *
174    * @return \Drupal\Core\Extension\Extension[]
175    */
176   protected function getAllLibraries() {
177     $modules = \Drupal::moduleHandler()->getModuleList();
178     $extensions = $modules;
179     $module_list = array_keys($modules);
180     sort($module_list);
181     $this->assertEqual($this->allModules, $module_list, 'All core modules are installed.');
182
183     $themes = $this->themeHandler->listInfo();
184     $extensions += $themes;
185     $theme_list = array_keys($themes);
186     sort($theme_list);
187     $this->assertEqual($this->allThemes, $theme_list, 'All core themes are installed.');
188
189     $libraries['core'] = $this->libraryDiscovery->getLibrariesByExtension('core');
190
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);
196       }
197     }
198     return $libraries;
199   }
200
201 }