Updated to Drupal 8.5. Core Media not yet in use.
[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     // Remove demo_umami_content module as its install hook creates content
112     // that relies on the presence of entity tables and various other elements
113     // not present in a kernel test.
114     unset($all_modules['demo_umami_content']);
115     $this->allModules = array_keys($all_modules);
116     $this->allModules[] = 'system';
117     sort($this->allModules);
118     $this->container->get('module_installer')->install($this->allModules);
119
120     $this->themeHandler = $this->container->get('theme_handler');
121     $this->themeInitialization = $this->container->get('theme.initialization');
122     $this->themeManager = $this->container->get('theme.manager');
123     $this->libraryDiscovery = $this->container->get('library.discovery');
124   }
125
126   /**
127    * Ensures that all core module and theme library files exist.
128    */
129   public function testCoreLibraryCompleteness() {
130     // First verify all libraries with no active theme.
131     $this->verifyLibraryFilesExist($this->getAllLibraries());
132
133     // Then verify all libraries for each core theme. This may seem like
134     // overkill but themes can override and extend other extensions' libraries
135     // and these changes are only applied for the active theme.
136     foreach ($this->allThemes as $theme) {
137       $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName($theme));
138       $this->libraryDiscovery->clearCachedDefinitions();
139
140       $this->verifyLibraryFilesExist($this->getAllLibraries());
141     }
142   }
143
144   /**
145    * Checks that all the library files exist.
146    *
147    * @param array[] $library_definitions
148    *   An array of library definitions, keyed by extension, then by library, and
149    *   so on.
150    */
151   protected function verifyLibraryFilesExist($library_definitions) {
152     $root = \Drupal::root();
153     foreach ($library_definitions as $extension => $libraries) {
154       foreach ($libraries as $library_name => $library) {
155         if (in_array("$extension/$library_name", $this->librariesToSkip)) {
156           continue;
157         }
158
159         // Check that all the assets exist.
160         foreach (['css', 'js'] as $asset_type) {
161           foreach ($library[$asset_type] as $asset) {
162             $file = $asset['data'];
163             $path = $root . '/' . $file;
164             // Only check and assert each file path once.
165             if (!isset($this->pathsChecked[$path])) {
166               $this->assertTrue(is_file($path), "$file file referenced from the $extension/$library_name library exists.");
167               $this->pathsChecked[$path] = TRUE;
168             }
169           }
170         }
171       }
172     }
173   }
174
175   /**
176    * Gets all libraries for core and all installed modules.
177    *
178    * @return \Drupal\Core\Extension\Extension[]
179    */
180   protected function getAllLibraries() {
181     $modules = \Drupal::moduleHandler()->getModuleList();
182     $extensions = $modules;
183     $module_list = array_keys($modules);
184     sort($module_list);
185     $this->assertEqual($this->allModules, $module_list, 'All core modules are installed.');
186
187     $themes = $this->themeHandler->listInfo();
188     $extensions += $themes;
189     $theme_list = array_keys($themes);
190     sort($theme_list);
191     $this->assertEqual($this->allThemes, $theme_list, 'All core themes are installed.');
192
193     $libraries['core'] = $this->libraryDiscovery->getLibrariesByExtension('core');
194
195     $root = \Drupal::root();
196     foreach ($extensions as $extension_name => $extension) {
197       $library_file = $extension->getPath() . '/' . $extension_name . '.libraries.yml';
198       if (is_file($root . '/' . $library_file)) {
199         $libraries[$extension_name] = $this->libraryDiscovery->getLibrariesByExtension($extension_name);
200       }
201     }
202     return $libraries;
203   }
204
205 }