View source
<?php
namespace Drupal\system\Tests\Asset;
use Drupal\Core\Asset\Exception\InvalidLibrariesExtendSpecificationException;
use Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException;
use Drupal\simpletest\KernelTestBase;
class LibraryDiscoveryIntegrationTest extends KernelTestBase {
protected $libraryDiscovery;
protected function setUp() {
parent::setUp();
$this->container
->get('theme_installer')
->install([
'test_theme',
'classy',
]);
$this->libraryDiscovery = $this->container
->get('library.discovery');
}
public function testHookLibraryInfoByTheme() {
$this
->activateTheme('test_theme');
$this
->assertTrue($this->libraryDiscovery
->getLibraryByName('test_theme', 'kitten'));
$this
->activateTheme('classy');
$this
->assertFalse($this->libraryDiscovery
->getLibraryByName('test_theme', 'kitten'));
}
public function testLibrariesOverride() {
$this
->activateTheme('classy');
$this
->assertAssetInLibrary('core/themes/classy/css/components/button.css', 'classy', 'base', 'css');
$this
->assertAssetInLibrary('core/themes/classy/css/components/collapse-processed.css', 'classy', 'base', 'css');
$this
->assertAssetInLibrary('core/themes/classy/css/components/container-inline.css', 'classy', 'base', 'css');
$this
->assertAssetInLibrary('core/themes/classy/css/components/details.css', 'classy', 'base', 'css');
$this
->assertAssetInLibrary('core/themes/classy/css/components/dialog.css', 'classy', 'dialog', 'css');
$this
->assertTrue($this->libraryDiscovery
->getLibraryByName('core', 'drupal.progress'), 'Confirmatory test on "core/drupal.progress"');
$this
->activateTheme('test_theme');
$this
->assertEqual($this->libraryDiscovery
->getLibraryByName('core', 'drupal.collapse'), $this->libraryDiscovery
->getLibraryByName('test_theme', 'collapse'), 'Entire library correctly overridden.');
$this
->assertNoAssetInLibrary('core/themes/classy/css/components/button.css', 'classy', 'base', 'css');
$this
->assertNoAssetInLibrary('core/themes/classy/css/components/collapse-processed.css', 'classy', 'base', 'css');
$this
->assertNoAssetInLibrary('core/themes/classy/css/components/container-inline.css', 'classy', 'base', 'css');
$this
->assertNoAssetInLibrary('core/themes/classy/css/components/details.css', 'classy', 'base', 'css');
$this
->assertNoAssetInLibrary('core/themes/classy/css/components/dialog.css', 'classy', 'dialog', 'css');
$this
->assertAssetInLibrary('core/modules/system/tests/themes/test_theme/css/my-button.css', 'classy', 'base', 'css');
$this
->assertAssetInLibrary('core/modules/system/tests/themes/test_theme/css/my-collapse-processed.css', 'classy', 'base', 'css');
$this
->assertAssetInLibrary('themes/my_theme/css/my-container-inline.css', 'classy', 'base', 'css');
$this
->assertAssetInLibrary('themes/my_theme/css/my-details.css', 'classy', 'base', 'css');
$this
->assertFalse($this->libraryDiscovery
->getLibraryByName('core', 'drupal.progress'), 'Entire library correctly removed.');
$library = $this->libraryDiscovery
->getLibraryByName('core', 'jquery');
foreach ($library['js'] as $definition) {
if ($definition['data'] == 'core/modules/system/tests/themes/test_theme/js/collapse.js') {
$this
->assertTrue($definition['minified'] && $definition['weight'] == -20, 'Previous attributes retained');
break;
}
}
}
public function testLibrariesOverrideDrupalSettings() {
$this
->activateTheme('test_theme_libraries_override_with_drupal_settings');
try {
$this->libraryDiscovery
->getLibraryByName('core', 'drupal.ajax');
$this
->fail('Throw Exception when trying to override drupalSettings');
} catch (InvalidLibrariesOverrideSpecificationException $e) {
$expected_message = 'drupalSettings may not be overridden in libraries-override. Trying to override core/drupal.ajax/drupalSettings. Use hook_library_info_alter() instead.';
$this
->assertEqual($e
->getMessage(), $expected_message, 'Throw Exception when trying to override drupalSettings');
}
}
public function testLibrariesOverrideMalformedAsset() {
$this
->activateTheme('test_theme_libraries_override_with_invalid_asset');
try {
$this->libraryDiscovery
->getLibraryByName('core', 'drupal.dialog');
$this
->fail('Throw Exception when specifying invalid override');
} catch (InvalidLibrariesOverrideSpecificationException $e) {
$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".';
$this
->assertEqual($e
->getMessage(), $expected_message, 'Throw Exception when specifying invalid override');
}
}
public function testLibrariesOverrideOtherAssetLibraryNames() {
$this
->activateTheme('test_theme');
$this
->assertAssetInLibrary('themes/my_theme/css/dropbutton.css', 'core', 'drupal.dropbutton', 'css');
$this
->assertAssetInLibrary('public://my_css/vertical-tabs.css', 'core', 'drupal.vertical-tabs', 'css');
$this
->assertAssetInLibrary('//my-server/my_theme/css/jquery_ui.css', 'core', 'jquery.ui', 'css');
$this
->assertAssetInLibrary('http://example.com/my_theme/css/farbtastic.css', 'core', 'jquery.farbtastic', 'css');
}
public function testBaseThemeLibrariesOverrideInSubTheme() {
$this
->activateTheme('test_subtheme');
$this
->assertNoAssetInLibrary('core/misc/dialog/dialog.js', 'core', 'drupal.dialog', 'js');
$this
->assertAssetInLibrary('core/modules/system/tests/themes/test_basetheme/css/farbtastic.css', 'core', 'jquery.farbtastic', 'css');
}
public function testLibrariesExtend() {
$this
->activateTheme('classy');
$this
->assertNoAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_1.css', 'classy', 'book-navigation', 'css');
$this
->assertNoAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/js/extend_1.js', 'classy', 'book-navigation', 'js');
$this
->assertNoAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_2.css', 'classy', 'book-navigation', 'css');
$this
->activateTheme('test_theme_libraries_extend');
$this
->assertAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_1.css', 'classy', 'book-navigation', 'css');
$this
->assertAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/js/extend_1.js', 'classy', 'book-navigation', 'js');
$this
->assertAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_2.css', 'classy', 'book-navigation', 'css');
$this
->assertNoAssetInLibrary('core/modules/system/tests/themes/test_basetheme/css/base-libraries-extend.css', 'classy', 'base', 'css');
$this
->assertNoAssetInLibrary('core/modules/system/tests/themes/test_subtheme/css/sub-libraries-extend.css', 'classy', 'base', 'css');
$this
->activateTheme('test_subtheme');
$this
->assertAssetInLibrary('core/modules/system/tests/themes/test_basetheme/css/base-libraries-extend.css', 'classy', 'base', 'css');
$this
->assertAssetInLibrary('core/modules/system/tests/themes/test_subtheme/css/sub-libraries-extend.css', 'classy', 'base', 'css');
$this
->activateTheme('test_theme_libraries_extend');
try {
$this->libraryDiscovery
->getLibraryByName('core', 'drupal.dialog');
$this
->fail('Throw Exception when specifying non-existent libraries-extend.');
} catch (InvalidLibrariesExtendSpecificationException $e) {
$expected_message = 'The specified library "test_theme_libraries_extend/non_existent_library" does not exist.';
$this
->assertEqual($e
->getMessage(), $expected_message, 'Throw Exception when specifying non-existent libraries-extend.');
}
$this->container
->get('theme_installer')
->install([
'test_theme',
]);
try {
$this->libraryDiscovery
->getLibraryByName('test_theme', 'collapse');
$this
->fail('Throw Exception when specifying non-string libraries-extend.');
} catch (InvalidLibrariesExtendSpecificationException $e) {
$expected_message = 'The libraries-extend specification for each library must be a list of strings.';
$this
->assertEqual($e
->getMessage(), $expected_message, 'Throw Exception when specifying non-string libraries-extend.');
}
}
protected function activateTheme($theme_name) {
$this->container
->get('theme_installer')
->install([
$theme_name,
]);
$theme_initializer = $this->container
->get('theme.initialization');
$theme_manager = $this->container
->get('theme.manager');
$theme_manager
->setActiveTheme($theme_initializer
->getActiveThemeByName($theme_name));
$this->libraryDiscovery
->clearCachedDefinitions();
$this
->pass(sprintf('Activated theme "%s"', $theme_name));
}
protected function assertAssetInLibrary($asset, $extension, $library_name, $sub_key, $message = NULL) {
if (!isset($message)) {
$message = sprintf('Asset %s found in library "%s/%s"', $asset, $extension, $library_name);
}
$library = $this->libraryDiscovery
->getLibraryByName($extension, $library_name);
foreach ($library[$sub_key] as $definition) {
if ($asset == $definition['data']) {
return $this
->pass($message);
}
}
return $this
->fail($message);
}
protected function assertNoAssetInLibrary($asset, $extension, $library_name, $sub_key, $message = NULL) {
if (!isset($message)) {
$message = sprintf('Asset %s not found in library "%s/%s"', $asset, $extension, $library_name);
}
$library = $this->libraryDiscovery
->getLibraryByName($extension, $library_name);
foreach ($library[$sub_key] as $definition) {
if ($asset == $definition['data']) {
return $this
->fail($message);
}
}
return $this
->pass($message);
}
}