3 namespace Drupal\KernelTests\Core\Asset;
5 use Drupal\Core\Asset\Exception\InvalidLibrariesExtendSpecificationException;
6 use Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException;
7 use Drupal\KernelTests\KernelTestBase;
10 * Tests the library discovery and library discovery parser.
14 class LibraryDiscoveryIntegrationTest extends KernelTestBase {
17 * The library discovery service.
19 * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
21 protected $libraryDiscovery;
26 protected function setUp() {
29 $this->container->get('theme_installer')->install(['test_theme', 'classy']);
30 $this->libraryDiscovery = $this->container->get('library.discovery');
34 * Tests that hook_library_info is invoked and the cache is cleared.
36 public function testHookLibraryInfoByTheme() {
37 // Activate test_theme and verify that the library 'kitten' is added using
38 // hook_library_info_alter().
39 $this->activateTheme('test_theme');
40 $this->assertTrue($this->libraryDiscovery->getLibraryByName('test_theme', 'kitten'));
42 // Now make classy the active theme and assert that library is not added.
43 $this->activateTheme('classy');
44 $this->assertFalse($this->libraryDiscovery->getLibraryByName('test_theme', 'kitten'));
48 * Tests that libraries-override are applied to library definitions.
50 public function testLibrariesOverride() {
51 // Assert some classy libraries that will be overridden or removed.
52 $this->activateTheme('classy');
53 $this->assertAssetInLibrary('core/themes/classy/css/components/button.css', 'classy', 'base', 'css');
54 $this->assertAssetInLibrary('core/themes/classy/css/components/collapse-processed.css', 'classy', 'base', 'css');
55 $this->assertAssetInLibrary('core/themes/classy/css/components/container-inline.css', 'classy', 'base', 'css');
56 $this->assertAssetInLibrary('core/themes/classy/css/components/details.css', 'classy', 'base', 'css');
57 $this->assertAssetInLibrary('core/themes/classy/css/components/dialog.css', 'classy', 'dialog', 'css');
59 // Confirmatory assert on core library to be removed.
60 $this->assertTrue($this->libraryDiscovery->getLibraryByName('core', 'drupal.progress'), 'Confirmatory test on "core/drupal.progress"');
62 // Activate test theme that defines libraries overrides.
63 $this->activateTheme('test_theme');
65 // Assert that entire library was correctly overridden.
66 $this->assertEqual($this->libraryDiscovery->getLibraryByName('core', 'drupal.collapse'), $this->libraryDiscovery->getLibraryByName('test_theme', 'collapse'), 'Entire library correctly overridden.');
68 // Assert that classy library assets were correctly overridden or removed.
69 $this->assertNoAssetInLibrary('core/themes/classy/css/components/button.css', 'classy', 'base', 'css');
70 $this->assertNoAssetInLibrary('core/themes/classy/css/components/collapse-processed.css', 'classy', 'base', 'css');
71 $this->assertNoAssetInLibrary('core/themes/classy/css/components/container-inline.css', 'classy', 'base', 'css');
72 $this->assertNoAssetInLibrary('core/themes/classy/css/components/details.css', 'classy', 'base', 'css');
73 $this->assertNoAssetInLibrary('core/themes/classy/css/components/dialog.css', 'classy', 'dialog', 'css');
75 $this->assertAssetInLibrary('core/modules/system/tests/themes/test_theme/css/my-button.css', 'classy', 'base', 'css');
76 $this->assertAssetInLibrary('core/modules/system/tests/themes/test_theme/css/my-collapse-processed.css', 'classy', 'base', 'css');
77 $this->assertAssetInLibrary('themes/my_theme/css/my-container-inline.css', 'classy', 'base', 'css');
78 $this->assertAssetInLibrary('themes/my_theme/css/my-details.css', 'classy', 'base', 'css');
80 // Assert that entire library was correctly removed.
81 $this->assertFalse($this->libraryDiscovery->getLibraryByName('core', 'drupal.progress'), 'Entire library correctly removed.');
83 // Assert that overridden library asset still retains attributes.
84 $library = $this->libraryDiscovery->getLibraryByName('core', 'jquery');
85 foreach ($library['js'] as $definition) {
86 if ($definition['data'] == 'core/modules/system/tests/themes/test_theme/js/collapse.js') {
87 $this->assertTrue($definition['minified'] && $definition['weight'] == -20, 'Previous attributes retained');
94 * Tests libraries-override on drupalSettings.
96 public function testLibrariesOverrideDrupalSettings() {
97 // Activate test theme that attempts to override drupalSettings.
98 $this->activateTheme('test_theme_libraries_override_with_drupal_settings');
100 // Assert that drupalSettings cannot be overridden and throws an exception.
102 $this->libraryDiscovery->getLibraryByName('core', 'drupal.ajax');
103 $this->fail('Throw Exception when trying to override drupalSettings');
105 catch (InvalidLibrariesOverrideSpecificationException $e) {
106 $expected_message = 'drupalSettings may not be overridden in libraries-override. Trying to override core/drupal.ajax/drupalSettings. Use hook_library_info_alter() instead.';
107 $this->assertEqual($e->getMessage(), $expected_message, 'Throw Exception when trying to override drupalSettings');
112 * Tests libraries-override on malformed assets.
114 public function testLibrariesOverrideMalformedAsset() {
115 // Activate test theme that overrides with a malformed asset.
116 $this->activateTheme('test_theme_libraries_override_with_invalid_asset');
118 // Assert that improperly formed asset "specs" throw an exception.
120 $this->libraryDiscovery->getLibraryByName('core', 'drupal.dialog');
121 $this->fail('Throw Exception when specifying invalid override');
123 catch (InvalidLibrariesOverrideSpecificationException $e) {
124 $expected_message = 'Library asset core/drupal.dialog/css is not correctly specified. It should be in the form "extension/library_name/sub_key/path/to/asset.js".';
125 $this->assertEqual($e->getMessage(), $expected_message, 'Throw Exception when specifying invalid override');
130 * Tests library assets with other ways for specifying paths.
132 public function testLibrariesOverrideOtherAssetLibraryNames() {
133 // Activate a test theme that defines libraries overrides on other types of
135 $this->activateTheme('test_theme');
137 // Assert Drupal-relative paths.
138 $this->assertAssetInLibrary('themes/my_theme/css/dropbutton.css', 'core', 'drupal.dropbutton', 'css');
140 // Assert stream wrapper paths.
141 $this->assertAssetInLibrary('public://my_css/vertical-tabs.css', 'core', 'drupal.vertical-tabs', 'css');
143 // Assert a protocol-relative URI.
144 $this->assertAssetInLibrary('//my-server/my_theme/css/jquery_ui.css', 'core', 'jquery.ui', 'css');
146 // Assert an absolute URI.
147 $this->assertAssetInLibrary('http://example.com/my_theme/css/farbtastic.css', 'core', 'jquery.farbtastic', 'css');
151 * Tests that base theme libraries-override still apply in sub themes.
153 public function testBaseThemeLibrariesOverrideInSubTheme() {
154 // Activate a test theme that has subthemes.
155 $this->activateTheme('test_subtheme');
157 // Assert that libraries-override specified in the base theme still applies
159 $this->assertNoAssetInLibrary('core/misc/dialog/dialog.js', 'core', 'drupal.dialog', 'js');
160 $this->assertAssetInLibrary('core/modules/system/tests/themes/test_basetheme/css/farbtastic.css', 'core', 'jquery.farbtastic', 'css');
164 * Tests libraries-extend.
166 public function testLibrariesExtend() {
167 // Activate classy themes and verify the libraries are not extended.
168 $this->activateTheme('classy');
169 $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_1.css', 'classy', 'book-navigation', 'css');
170 $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/js/extend_1.js', 'classy', 'book-navigation', 'js');
171 $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_2.css', 'classy', 'book-navigation', 'css');
173 // Activate the theme that extends the book-navigation library in classy.
174 $this->activateTheme('test_theme_libraries_extend');
175 $this->assertAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_1.css', 'classy', 'book-navigation', 'css');
176 $this->assertAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/js/extend_1.js', 'classy', 'book-navigation', 'js');
177 $this->assertAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_2.css', 'classy', 'book-navigation', 'css');
179 // Activate a sub theme and confirm that it inherits the library assets
180 // extended in the base theme as well as its own.
181 $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_basetheme/css/base-libraries-extend.css', 'classy', 'base', 'css');
182 $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_subtheme/css/sub-libraries-extend.css', 'classy', 'base', 'css');
183 $this->activateTheme('test_subtheme');
184 $this->assertAssetInLibrary('core/modules/system/tests/themes/test_basetheme/css/base-libraries-extend.css', 'classy', 'base', 'css');
185 $this->assertAssetInLibrary('core/modules/system/tests/themes/test_subtheme/css/sub-libraries-extend.css', 'classy', 'base', 'css');
187 // Activate test theme that extends with a non-existent library. An
188 // exception should be thrown.
189 $this->activateTheme('test_theme_libraries_extend');
191 $this->libraryDiscovery->getLibraryByName('core', 'drupal.dialog');
192 $this->fail('Throw Exception when specifying non-existent libraries-extend.');
194 catch (InvalidLibrariesExtendSpecificationException $e) {
195 $expected_message = 'The specified library "test_theme_libraries_extend/non_existent_library" does not exist.';
196 $this->assertEqual($e->getMessage(), $expected_message, 'Throw Exception when specifying non-existent libraries-extend.');
199 // Also, test non-string libraries-extend. An exception should be thrown.
200 $this->container->get('theme_installer')->install(['test_theme']);
202 $this->libraryDiscovery->getLibraryByName('test_theme', 'collapse');
203 $this->fail('Throw Exception when specifying non-string libraries-extend.');
205 catch (InvalidLibrariesExtendSpecificationException $e) {
206 $expected_message = 'The libraries-extend specification for each library must be a list of strings.';
207 $this->assertEqual($e->getMessage(), $expected_message, 'Throw Exception when specifying non-string libraries-extend.');
212 * Activates a specified theme.
214 * Installs the theme if not already installed and makes it the active theme.
216 * @param string $theme_name
217 * The name of the theme to be activated.
219 protected function activateTheme($theme_name) {
220 $this->container->get('theme_installer')->install([$theme_name]);
222 /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */
223 $theme_initializer = $this->container->get('theme.initialization');
225 /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */
226 $theme_manager = $this->container->get('theme.manager');
228 $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName($theme_name));
230 $this->libraryDiscovery->clearCachedDefinitions();
233 $this->pass(sprintf('Activated theme "%s"', $theme_name));
237 * Asserts that the specified asset is in the given library.
239 * @param string $asset
240 * The asset file with the path for the file.
241 * @param string $extension
242 * The extension in which the $library is defined.
243 * @param string $library_name
244 * Name of the library.
245 * @param mixed $sub_key
246 * The library sub key where the given asset is defined.
247 * @param string $message
248 * (optional) A message to display with the assertion.
251 * TRUE if the specified asset is found in the library.
253 protected function assertAssetInLibrary($asset, $extension, $library_name, $sub_key, $message = NULL) {
254 if (!isset($message)) {
255 $message = sprintf('Asset %s found in library "%s/%s"', $asset, $extension, $library_name);
257 $library = $this->libraryDiscovery->getLibraryByName($extension, $library_name);
258 foreach ($library[$sub_key] as $definition) {
259 if ($asset == $definition['data']) {
260 return $this->pass($message);
263 return $this->fail($message);
267 * Asserts that the specified asset is not in the given library.
269 * @param string $asset
270 * The asset file with the path for the file.
271 * @param string $extension
272 * The extension in which the $library_name is defined.
273 * @param string $library_name
274 * Name of the library.
275 * @param mixed $sub_key
276 * The library sub key where the given asset is defined.
277 * @param string $message
278 * (optional) A message to display with the assertion.
281 * TRUE if the specified asset is not found in the library.
283 protected function assertNoAssetInLibrary($asset, $extension, $library_name, $sub_key, $message = NULL) {
284 if (!isset($message)) {
285 $message = sprintf('Asset %s not found in library "%s/%s"', $asset, $extension, $library_name);
287 $library = $this->libraryDiscovery->getLibraryByName($extension, $library_name);
288 foreach ($library[$sub_key] as $definition) {
289 if ($asset == $definition['data']) {
290 return $this->fail($message);
293 return $this->pass($message);