StableLibraryOverrideTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Drupal\KernelTests\Core\Theme;
  3. use Drupal\KernelTests\KernelTestBase;
  4. /**
  5. * Tests Stable's library overrides.
  6. *
  7. * @group Theme
  8. */
  9. class StableLibraryOverrideTest extends KernelTestBase {
  10. /**
  11. * The theme manager.
  12. *
  13. * @var \Drupal\Core\Theme\ThemeManagerInterface
  14. */
  15. protected $themeManager;
  16. /**
  17. * The theme initialization.
  18. *
  19. * @var \Drupal\Core\Theme\ThemeInitializationInterface
  20. */
  21. protected $themeInitialization;
  22. /**
  23. * The library discovery service.
  24. *
  25. * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
  26. */
  27. protected $libraryDiscovery;
  28. /**
  29. * A list of all core modules.
  30. *
  31. * @var string[]
  32. */
  33. protected $allModules;
  34. /**
  35. * A list of libraries to skip checking, in the format extension/library_name.
  36. *
  37. * @var string[]
  38. */
  39. protected $librariesToSkip = [];
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static $modules = ['system', 'user', 'path_alias'];
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function setUp() {
  48. parent::setUp();
  49. $this->container->get('theme_installer')->install(['stable']);
  50. // Enable all core modules.
  51. $all_modules = $this->container->get('extension.list.module')->getList();
  52. $all_modules = array_filter($all_modules, function ($module) {
  53. // Filter contrib, hidden, experimental, already enabled modules, and
  54. // modules in the Testing package.
  55. if ($module->origin !== 'core' || !empty($module->info['hidden']) || $module->status == TRUE || $module->info['package'] == 'Testing' || $module->info['package'] == 'Core (Experimental)') {
  56. return FALSE;
  57. }
  58. return TRUE;
  59. });
  60. $this->allModules = array_keys($all_modules);
  61. $this->allModules[] = 'system';
  62. $this->allModules[] = 'user';
  63. $this->allModules[] = 'path_alias';
  64. sort($this->allModules);
  65. $this->container->get('module_installer')->install($this->allModules);
  66. $this->themeManager = $this->container->get('theme.manager');
  67. $this->themeInitialization = $this->container->get('theme.initialization');
  68. $this->libraryDiscovery = $this->container->get('library.discovery');
  69. }
  70. /**
  71. * Ensures that Stable overrides all relevant core library assets.
  72. */
  73. public function testStableLibraryOverrides() {
  74. // First get the clean library definitions with no active theme.
  75. $libraries_before = $this->getAllLibraries();
  76. $libraries_before = $this->removeVendorAssets($libraries_before);
  77. $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName('stable'));
  78. $this->libraryDiscovery->clearCachedDefinitions();
  79. // Now get the library definitions with Stable as the active theme.
  80. $libraries_after = $this->getAllLibraries();
  81. $libraries_after = $this->removeVendorAssets($libraries_after);
  82. foreach ($libraries_before as $extension => $libraries) {
  83. foreach ($libraries as $library_name => $library) {
  84. // Allow skipping libraries.
  85. if (in_array("$extension/$library_name", $this->librariesToSkip)) {
  86. continue;
  87. }
  88. $library_after = $libraries_after[$extension][$library_name];
  89. // Check that all the CSS assets are overridden.
  90. foreach ($library['css'] as $index => $asset) {
  91. $clean_path = $asset['data'];
  92. $stable_path = $library_after['css'][$index]['data'];
  93. // Make core/misc assets look like they are coming from a "core"
  94. // module.
  95. $replacements = [
  96. 'core/misc/' => "core/modules/core/css/",
  97. ];
  98. $expected_path = strtr($clean_path, $replacements);
  99. // Adjust the module asset paths to correspond with the Stable folder
  100. // structure.
  101. $expected_path = str_replace("core/modules/$extension/css/", "core/themes/stable/css/$extension/", $expected_path);
  102. $assert_path = str_replace("core/modules/$extension/", '', $clean_path);
  103. $this->assertEqual($expected_path, $stable_path, "$assert_path from the $extension/$library_name library is overridden in Stable.");
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Removes all vendor libraries and assets from the library definitions.
  110. *
  111. * @param array[] $all_libraries
  112. * An associative array of libraries keyed by extension, then by library
  113. * name, and so on.
  114. *
  115. * @return array[]
  116. * The reduced array of libraries.
  117. */
  118. protected function removeVendorAssets($all_libraries) {
  119. foreach ($all_libraries as $extension => $libraries) {
  120. foreach ($libraries as $library_name => $library) {
  121. if (isset($library['remote'])) {
  122. unset($all_libraries[$extension][$library_name]);
  123. }
  124. foreach (['css', 'js'] as $asset_type) {
  125. foreach ($library[$asset_type] as $index => $asset) {
  126. if (strpos($asset['data'], 'core/assets/vendor') !== FALSE) {
  127. unset($all_libraries[$extension][$library_name][$asset_type][$index]);
  128. // Re-key the array of assets. This is needed because
  129. // libraries-override doesn't always preserve the order.
  130. if (!empty($all_libraries[$extension][$library_name][$asset_type])) {
  131. $all_libraries[$extension][$library_name][$asset_type] = array_values($all_libraries[$extension][$library_name][$asset_type]);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. return $all_libraries;
  139. }
  140. /**
  141. * Gets all libraries for core and all installed modules.
  142. *
  143. * @return array[]
  144. * An associative array of libraries keyed by extension, then by library
  145. * name, and so on.
  146. */
  147. protected function getAllLibraries() {
  148. $modules = \Drupal::moduleHandler()->getModuleList();
  149. $module_list = array_keys($modules);
  150. sort($module_list);
  151. $this->assertEqual($this->allModules, $module_list, 'All core modules are installed.');
  152. $libraries['core'] = $this->libraryDiscovery->getLibrariesByExtension('core');
  153. foreach ($modules as $module_name => $module) {
  154. $library_file = $module->getPath() . '/' . $module_name . '.libraries.yml';
  155. if (is_file($this->root . '/' . $library_file)) {
  156. $libraries[$module_name] = $this->libraryDiscovery->getLibrariesByExtension($module_name);
  157. }
  158. }
  159. return $libraries;
  160. }
  161. }