Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Theme / StableLibraryOverrideTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Theme;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Tests Stable's library overrides.
9  *
10  * @group Theme
11  */
12 class StableLibraryOverrideTest extends KernelTestBase {
13
14   /**
15    * The theme manager.
16    *
17    * @var \Drupal\Core\Theme\ThemeManagerInterface
18    */
19   protected $themeManager;
20
21   /**
22    * The theme initialization.
23    *
24    * @var \Drupal\Core\Theme\ThemeInitializationInterface
25    */
26   protected $themeInitialization;
27
28   /**
29    * The library discovery service.
30    *
31    * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
32    */
33   protected $libraryDiscovery;
34
35   /**
36    * A list of all core modules.
37    *
38    * @var string[]
39    */
40   protected $allModules;
41
42   /**
43    * A list of libraries to skip checking, in the format extension/library_name.
44    *
45    * @var string[]
46    */
47   protected $librariesToSkip = [];
48
49   /**
50    * {@inheritdoc}
51    */
52   public static $modules = ['system'];
53
54   /**
55    * {@inheritdoc}
56    */
57   protected function setUp() {
58     parent::setUp();
59
60     $this->container->get('theme_installer')->install(['stable']);
61
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)') {
68         return FALSE;
69       }
70       return TRUE;
71     });
72     $this->allModules = array_keys($all_modules);
73     $this->allModules[] = 'system';
74     sort($this->allModules);
75     $this->container->get('module_installer')->install($this->allModules);
76
77     $this->themeManager = $this->container->get('theme.manager');
78     $this->themeInitialization = $this->container->get('theme.initialization');
79     $this->libraryDiscovery = $this->container->get('library.discovery');
80   }
81
82   /**
83    * Ensures that Stable overrides all relevant core library assets.
84    */
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);
89
90     $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName('stable'));
91     $this->libraryDiscovery->clearCachedDefinitions();
92
93     // Now get the library definitions with Stable as the active theme.
94     $libraries_after = $this->getAllLibraries();
95     $libraries_after = $this->removeVendorAssets($libraries_after);
96
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)) {
102           continue;
103         }
104         $library_after = $libraries_after[$extension][$library_name];
105
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"
111           // module.
112           $replacements = [
113             'core/misc/' => "core/modules/core/css/",
114           ];
115           $expected_path = strtr($clean_path, $replacements);
116
117           // Adjust the module asset paths to correspond with the Stable folder
118           // structure.
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);
121
122           $this->assertEqual($expected_path, $stable_path, "$assert_path from the $extension/$library_name library is overridden in Stable.");
123         }
124       }
125     }
126   }
127
128   /**
129    * Removes all vendor libraries and assets from the library definitions.
130    *
131    * @param array[] $all_libraries
132    *   An associative array of libraries keyed by extension, then by library
133    *   name, and so on.
134    *
135    * @return array[]
136    *   The reduced array of libraries.
137    */
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]);
143         }
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]);
152               }
153             }
154           }
155         }
156       }
157     }
158     return $all_libraries;
159   }
160
161   /**
162    * Gets all libraries for core and all installed modules.
163    *
164    * @return array[]
165    *   An associative array of libraries keyed by extension, then by library
166    *   name, and so on.
167    */
168   protected function getAllLibraries() {
169     $modules = \Drupal::moduleHandler()->getModuleList();
170     $module_list = array_keys($modules);
171     sort($module_list);
172     $this->assertEqual($this->allModules, $module_list, 'All core modules are installed.');
173
174     $libraries['core'] = $this->libraryDiscovery->getLibrariesByExtension('core');
175
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);
181       }
182     }
183     return $libraries;
184   }
185
186 }