Commit 65edcac3 authored by alexpott's avatar alexpott

Issue #2413587 by Wim Leers, willzyx: Aggregate JavaScript files no longer working

parent 6ab60f77
......@@ -285,6 +285,12 @@ public function getJsAssets(AttachedAssetsInterface $assets, $optimize) {
}
}
if ($optimize) {
$collection_optimizer = \Drupal::service('asset.js.collection_optimizer');
$js_assets_header = $collection_optimizer->optimize($js_assets_header);
$js_assets_footer = $collection_optimizer->optimize($js_assets_footer);
}
// If the core/drupalSettings library is being loaded or is already loaded,
// get the JavaScript settings assets, and convert them into a single
// "regular" JavaScript asset.
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Ajax\AddCssCommand;
use Drupal\Core\Ajax\AfterCommand;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\BeforeCommand;
......@@ -22,6 +23,7 @@
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\RestripeCommand;
use Drupal\Core\Ajax\SettingsCommand;
use Symfony\Component\HttpFoundation\Request;
/**
* Performs tests on AJAX framework commands.
......@@ -120,4 +122,31 @@ function testAjaxCommands() {
$this->assertCommand($commands, $expected->render(), "'settings' AJAX command issued with correct data.");
}
/**
* Regression test: Settings command exists regardless of JS aggregation.
*/
public function testAttachedSettings() {
$assert = function($message) {
$response = new AjaxResponse();
$response->setAttachments([
'library' => ['core/drupalSettings'],
'drupalSettings' => ['foo' => 'bar'],
]);
$response->prepare(new Request());
$expected = [
'command' => 'settings',
];
$this->assertCommand($response->getCommands(), $expected, $message);
};
$config = $this->config('system.performance');
$config->set('js.preprocess', FALSE)->save();
$assert('Settings command exists when JS aggregation is disabled.');
$config->set('js.preprocess', TRUE)->save();
$assert('Settings command exists when JS aggregation is enabled.');
}
}
......@@ -168,6 +168,27 @@ function testAggregatedAttributes() {
$this->assertNotIdentical(strpos($rendered_js, $expected_2), FALSE, 'Rendered internal JavaScript with correct defer and random attributes.');
}
/**
* Integration test for CSS/JS aggregation.
*/
function testAggregation() {
$build['#attached']['library'][] = 'core/drupal.timezone';
$build['#attached']['library'][] = 'core/drupal.vertical-tabs';
$assets = AttachedAssets::createFromRenderArray($build);
$this->assertEqual(1, count($this->assetResolver->getCssAssets($assets, TRUE)), 'There is a sole aggregated CSS asset.');
list($header_js, $footer_js) = $this->assetResolver->getJsAssets($assets, TRUE);
$this->assertEqual([], \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->assertTrue(
count($rendered_footer_js) == 2
&& substr($rendered_footer_js[0]['#value'], 0, 20) === 'var drupalSettings ='
&& substr($rendered_footer_js[1]['#attributes']['src'], 0, 7) === 'http://',
'There are 2 JavaScript assets in the footer: one with drupalSettings, one with the sole aggregated JavaScript asset.'
);
}
/**
* Tests JavaScript settings.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment