View source
<?php
namespace Drupal\KernelTests\Core\Asset;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Asset\AttachedAssets;
use Drupal\KernelTests\KernelTestBase;
class AttachedAssetsTest extends KernelTestBase {
protected $assetResolver;
protected $renderer;
protected $fileUrlGenerator;
protected static $modules = [
'language',
'common_test',
'system',
];
protected function setUp() : void {
parent::setUp();
$this->assetResolver = $this->container
->get('asset.resolver');
$this->renderer = $this->container
->get('renderer');
$this->fileUrlGenerator = $this->container
->get('file_url_generator');
}
public function testDefault() {
$assets = new AttachedAssets();
$this
->assertEquals([], $this->assetResolver
->getCssAssets($assets, FALSE), 'Default CSS is empty.');
list($js_assets_header, $js_assets_footer) = $this->assetResolver
->getJsAssets($assets, FALSE);
$this
->assertEquals([], $js_assets_header, 'Default header JavaScript is empty.');
$this
->assertEquals([], $js_assets_footer, 'Default footer JavaScript is empty.');
}
public function testLibraryUnknown() {
$build['#attached']['library'][] = 'core/unknown';
$assets = AttachedAssets::createFromRenderArray($build);
$this
->assertSame([], $this->assetResolver
->getJsAssets($assets, FALSE)[0], 'Unknown library was not added to the page.');
}
public function testAddFiles() {
$build['#attached']['library'][] = 'common_test/files';
$assets = AttachedAssets::createFromRenderArray($build);
$css = $this->assetResolver
->getCssAssets($assets, FALSE);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$this
->assertArrayHasKey('core/modules/system/tests/modules/common_test/bar.css', $css);
$this
->assertArrayHasKey('core/modules/system/tests/modules/common_test/foo.js', $js);
$css_render_array = \Drupal::service('asset.css.collection_renderer')
->render($css);
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_css = $this->renderer
->renderPlain($css_render_array);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$query_string = $this->container
->get('state')
->get('system.css_js_query_string') ?: '0';
$this
->assertStringContainsString('<link rel="stylesheet" media="all" href="' . $this->fileUrlGenerator
->generateString('core/modules/system/tests/modules/common_test/bar.css') . '?' . $query_string . '" />', $rendered_css, 'Rendering an external CSS file.');
$this
->assertStringContainsString('<script src="' . $this->fileUrlGenerator
->generateString('core/modules/system/tests/modules/common_test/foo.js') . '?' . $query_string . '"></script>', $rendered_js, 'Rendering an external JavaScript file.');
}
public function testAddJsSettings() {
$build['#attached']['library'][] = 'core/drupalSettings';
$assets = AttachedAssets::createFromRenderArray($build);
$this
->assertEquals([], $assets
->getSettings(), 'JavaScript settings on $assets are empty.');
$javascript = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$this
->assertArrayHasKey('currentPath', $javascript['drupalSettings']['data']['path']);
$this
->assertArrayHasKey('currentPath', $assets
->getSettings()['path']);
$assets
->setSettings([
'drupal' => 'rocks',
'dries' => 280342800,
]);
$javascript = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$this
->assertEquals(280342800, $javascript['drupalSettings']['data']['dries'], 'JavaScript setting is set correctly.');
$this
->assertEquals('rocks', $javascript['drupalSettings']['data']['drupal'], 'The other JavaScript setting is set correctly.');
}
public function testAddExternalFiles() {
$build['#attached']['library'][] = 'common_test/external';
$assets = AttachedAssets::createFromRenderArray($build);
$css = $this->assetResolver
->getCssAssets($assets, FALSE);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$this
->assertArrayHasKey('http://example.com/stylesheet.css', $css);
$this
->assertArrayHasKey('http://example.com/script.js', $js);
$css_render_array = \Drupal::service('asset.css.collection_renderer')
->render($css);
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_css = $this->renderer
->renderPlain($css_render_array);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$this
->assertStringContainsString('<link rel="stylesheet" media="all" href="http://example.com/stylesheet.css" />', $rendered_css, 'Rendering an external CSS file.');
$this
->assertStringContainsString('<script src="http://example.com/script.js"></script>', $rendered_js, 'Rendering an external JavaScript file.');
}
public function testAttributes() {
$build['#attached']['library'][] = 'common_test/js-attributes';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$expected_1 = '<script src="http://example.com/deferred-external.js" foo="bar" defer></script>';
$expected_2 = '<script src="' . $this->fileUrlGenerator
->generateString('core/modules/system/tests/modules/common_test/deferred-internal.js') . '?v=1" defer bar="foo"></script>';
$this
->assertStringContainsString($expected_1, $rendered_js, 'Rendered external JavaScript with correct defer and random attributes.');
$this
->assertStringContainsString($expected_2, $rendered_js, 'Rendered internal JavaScript with correct defer and random attributes.');
}
public function testAggregatedAttributes() {
$build['#attached']['library'][] = 'common_test/js-attributes';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, TRUE)[1];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$expected_1 = '<script src="http://example.com/deferred-external.js" foo="bar" defer></script>';
$expected_2 = '<script src="' . $this->fileUrlGenerator
->generateString('core/modules/system/tests/modules/common_test/deferred-internal.js') . '?v=1" defer bar="foo"></script>';
$this
->assertStringContainsString($expected_1, $rendered_js, 'Rendered external JavaScript with correct defer and random attributes.');
$this
->assertStringContainsString($expected_2, $rendered_js, 'Rendered internal JavaScript with correct defer and random attributes.');
}
public function testAggregation() {
$build['#attached']['library'][] = 'core/drupal.timezone';
$build['#attached']['library'][] = 'core/drupal.vertical-tabs';
$assets = AttachedAssets::createFromRenderArray($build);
$this
->assertCount(1, $this->assetResolver
->getCssAssets($assets, TRUE), 'There is a sole aggregated CSS asset.');
list($header_js, $footer_js) = $this->assetResolver
->getJsAssets($assets, TRUE);
$this
->assertEquals([], \Drupal::service('asset.js.collection_renderer')
->render($header_js), 'There are 0 JavaScript assets in the header.');
$rendered_footer_js = \Drupal::service('asset.js.collection_renderer')
->render($footer_js);
$this
->assertCount(2, $rendered_footer_js, 'There are 2 JavaScript assets in the footer.');
$this
->assertEquals('drupal-settings-json', $rendered_footer_js[0]['#attributes']['data-drupal-selector'], 'The first of the two JavaScript assets in the footer has drupal settings.');
$this
->assertStringStartsWith(base_path(), $rendered_footer_js[1]['#attributes']['src'], 'The second of the two JavaScript assets in the footer has the sole aggregated JavaScript asset.');
}
public function testSettings() {
$build = [];
$build['#attached']['library'][] = 'core/drupalSettings';
$build['#attached']['drupalSettings']['path']['pathPrefix'] = 'yarhar';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = (string) $this->renderer
->renderPlain($js_render_array);
$startToken = '{';
$endToken = '}';
$start = strpos($rendered_js, $startToken);
$end = strrpos($rendered_js, $endToken);
$json = mb_substr($rendered_js, $start, $end - $start + 1);
$parsed_settings = Json::decode($json);
$this
->assertTrue(isset($parsed_settings['path']['baseUrl']), 'drupalSettings.path.baseUrl is present.');
$this
->assertSame('yarhar', $parsed_settings['path']['pathPrefix'], 'drupalSettings.path.pathPrefix is present and has the correct (overridden) value.');
$this
->assertSame('', $parsed_settings['path']['currentPath'], 'drupalSettings.path.currentPath is present and has the correct value.');
$this
->assertFalse($parsed_settings['path']['currentPathIsAdmin'], 'drupalSettings.path.currentPathIsAdmin is present and has the correct value.');
$this
->assertFalse($parsed_settings['path']['isFront'], 'drupalSettings.path.isFront is present and has the correct value.');
$this
->assertSame('en', $parsed_settings['path']['currentLanguage'], 'drupalSettings.path.currentLanguage is present and has the correct value.');
$this
->assertSame('☃', $parsed_settings['pluralDelimiter']);
$this
->assertSame('bar', $parsed_settings['foo']);
}
public function testHeaderHTML() {
$build['#attached']['library'][] = 'common_test/js-header';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[0];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$query_string = $this->container
->get('state')
->get('system.css_js_query_string') ?: '0';
$this
->assertStringContainsString('<script src="' . $this->fileUrlGenerator
->generateString('core/modules/system/tests/modules/common_test/header.js') . '?' . $query_string . '"></script>', $rendered_js, 'The JS asset in common_test/js-header appears in the header.');
$this
->assertStringContainsString('<script src="' . $this->fileUrlGenerator
->generateString('core/misc/drupal.js'), $rendered_js, 'The JS asset of the direct dependency (core/drupal) of common_test/js-header appears in the header.');
$this
->assertStringContainsString('<script src="' . $this->fileUrlGenerator
->generateString('core/misc/drupalSettingsLoader.js'), $rendered_js, 'The JS asset of the indirect dependency (core/drupalSettings) of common_test/js-header appears in the header.');
}
public function testNoCache() {
$build['#attached']['library'][] = 'common_test/no-cache';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$this
->assertFalse($js['core/modules/system/tests/modules/common_test/nocache.js']['preprocess'], 'Setting cache to FALSE sets preprocess to FALSE when adding JavaScript.');
}
public function testBrowserConditionalComments() {
$this
->expectDeprecation('Support for IE Conditional Comments is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. See https://www.drupal.org/node/3102997');
$default_query_string = $this->container
->get('state')
->get('system.css_js_query_string') ?: '0';
$build['#attached']['library'][] = 'common_test/browsers';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$expected_1 = "<!--[if lte IE 8]>\n" . '<script src="' . $this->fileUrlGenerator
->generateString('core/modules/system/tests/modules/common_test/old-ie.js') . '?' . $default_query_string . '"></script>' . "\n<![endif]-->";
$expected_2 = "<!--[if !IE]><!-->\n" . '<script src="' . $this->fileUrlGenerator
->generateString('core/modules/system/tests/modules/common_test/no-ie.js') . '?' . $default_query_string . '"></script>' . "\n<!--<![endif]-->";
$this
->assertStringContainsString($expected_1, $rendered_js, 'Rendered JavaScript within downlevel-hidden conditional comments.');
$this
->assertStringContainsString($expected_2, $rendered_js, 'Rendered JavaScript within downlevel-revealed conditional comments.');
}
public function testVersionQueryString() {
$build['#attached']['library'][] = 'core/backbone';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$this
->assertStringContainsString('core/assets/vendor/backbone/backbone-min.js?v=1.4.0', $rendered_js, 'JavaScript version identifiers correctly appended to URLs');
}
public function testRenderOrder() {
$build['#attached']['library'][] = 'common_test/order';
$assets = AttachedAssets::createFromRenderArray($build);
$expected_order_js = [
"-8_1",
"-8_2",
"-8_3",
"-8_4",
"-5_1",
"-3_1",
"-3_2",
"0_1",
"0_2",
"0_3",
];
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$matches = [];
if (preg_match_all('/weight_([-0-9]+_[0-9]+)/', $rendered_js, $matches)) {
$result = $matches[1];
}
else {
$result = [];
}
$this
->assertSame($expected_order_js, $result, 'JavaScript is added in the expected weight order.');
$expected_order_css = [
'base_weight_-101_1',
'base_weight_-8_1',
'layout_weight_-101_1',
'base_weight_0_1',
'base_weight_0_2',
'layout_weight_-8_1',
'component_weight_-101_1',
'layout_weight_0_1',
'layout_weight_0_2',
'component_weight_-8_1',
'state_weight_-101_1',
'component_weight_0_1',
'component_weight_0_2',
'state_weight_-8_1',
'theme_weight_-101_1',
'state_weight_0_1',
'state_weight_0_2',
'theme_weight_-8_1',
'theme_weight_0_1',
'theme_weight_0_2',
];
$css = $this->assetResolver
->getCssAssets($assets, FALSE);
$css_render_array = \Drupal::service('asset.css.collection_renderer')
->render($css);
$rendered_css = $this->renderer
->renderPlain($css_render_array);
$matches = [];
if (preg_match_all('/([a-z]+)_weight_([-0-9]+_[0-9]+)/', $rendered_css, $matches)) {
$result = $matches[0];
}
else {
$result = [];
}
$this
->assertSame($expected_order_css, $result, 'CSS is added in the expected weight order.');
}
public function testRenderDifferentWeight() {
$build['#attached']['library'][] = 'core/jquery';
$build['#attached']['library'][] = 'common_test/weight';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$this
->assertLessThan(strpos($rendered_js, 'first.js'), strpos($rendered_js, 'lighter.css'));
$this
->assertLessThan(strpos($rendered_js, 'first.js'), strpos($rendered_js, 'lighter.js'));
$this
->assertLessThan(strpos($rendered_js, 'core/assets/vendor/jquery/jquery.min.js'), strpos($rendered_js, 'before-jquery.js'));
}
public function testAlter() {
$build['#attached']['library'][] = 'core/drupal.tableselect';
$build['#attached']['library'][] = 'common_test/hook_js_alter';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$this
->assertLessThan(strpos($rendered_js, 'core/misc/tableselect.js'), strpos($rendered_js, 'alter.js'));
}
public function testLibraryAlter() {
$library_discovery = \Drupal::service('library.discovery');
$library = $library_discovery
->getLibraryByName('core', 'jquery.farbtastic');
$this
->assertEquals('0.0', $library['version'], 'Registered libraries were altered.');
$build['#attached']['library'][] = 'core/jquery.farbtastic';
$assets = AttachedAssets::createFromRenderArray($build);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$this
->assertStringContainsString('core/assets/vendor/jquery-form/jquery.form.min.js', (string) $rendered_js, 'Altered library dependencies are added to the page.');
}
public function testDynamicLibrary() {
$library_discovery = \Drupal::service('library.discovery');
\Drupal::state()
->set('common_test.library_info_build_test', TRUE);
$library_discovery
->clearCachedDefinitions();
$dynamic_library = $library_discovery
->getLibraryByName('common_test', 'dynamic_library');
$this
->assertIsArray($dynamic_library);
$this
->assertArrayHasKey('version', $dynamic_library);
$this
->assertSame('1.0', $dynamic_library['version']);
$this
->assertArrayHasKey('dependencies', $dynamic_library);
$this
->assertSame([
'core/jquery',
], $dynamic_library['dependencies']);
}
public function testLibraryNameConflicts() {
$library_discovery = \Drupal::service('library.discovery');
$farbtastic = $library_discovery
->getLibraryByName('common_test', 'jquery.farbtastic');
$this
->assertEquals('0.1', $farbtastic['version'], 'Alternative libraries can be added to the page.');
}
public function testAddJsFileWithQueryString() {
$build['#attached']['library'][] = 'common_test/querystring';
$assets = AttachedAssets::createFromRenderArray($build);
$css = $this->assetResolver
->getCssAssets($assets, FALSE);
$js = $this->assetResolver
->getJsAssets($assets, FALSE)[1];
$this
->assertArrayHasKey('core/modules/system/tests/modules/common_test/querystring.css?arg1=value1&arg2=value2', $css);
$this
->assertArrayHasKey('core/modules/system/tests/modules/common_test/querystring.js?arg1=value1&arg2=value2', $js);
$css_render_array = \Drupal::service('asset.css.collection_renderer')
->render($css);
$rendered_css = $this->renderer
->renderPlain($css_render_array);
$js_render_array = \Drupal::service('asset.js.collection_renderer')
->render($js);
$rendered_js = $this->renderer
->renderPlain($js_render_array);
$query_string = $this->container
->get('state')
->get('system.css_js_query_string') ?: '0';
$this
->assertStringContainsString('<link rel="stylesheet" media="all" href="' . str_replace('&', '&', $this->fileUrlGenerator
->generateString('core/modules/system/tests/modules/common_test/querystring.css?arg1=value1&arg2=value2')) . '&' . $query_string . '" />', $rendered_css, 'CSS file with query string gets version query string correctly appended..');
$this
->assertStringContainsString('<script src="' . str_replace('&', '&', $this->fileUrlGenerator
->generateString('core/modules/system/tests/modules/common_test/querystring.js?arg1=value1&arg2=value2')) . '&' . $query_string . '"></script>', $rendered_js, 'JavaScript file with query string gets version query string correctly appended.');
}
}