summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-01-26 17:12:55 (GMT)
committerAlex Pott2015-01-26 17:12:55 (GMT)
commit65edcac35be5ab55dd9d8490d121ad7b56023453 (patch)
tree461caf6be8b6bb99dbe8f68c0dc2fd3421c8499c
parent6ab60f77c3ef1d5d29fd39897c7af2a4cf0bd0cc (diff)
Issue #2413587 by Wim Leers, willzyx: Aggregate JavaScript files no longer working
-rw-r--r--core/lib/Drupal/Core/Asset/AssetResolver.php6
-rw-r--r--core/modules/system/src/Tests/Ajax/CommandsTest.php29
-rw-r--r--core/modules/system/src/Tests/Common/AttachedAssetsTest.php21
3 files changed, 56 insertions, 0 deletions
diff --git a/core/lib/Drupal/Core/Asset/AssetResolver.php b/core/lib/Drupal/Core/Asset/AssetResolver.php
index 4c845b3..c41e14e 100644
--- a/core/lib/Drupal/Core/Asset/AssetResolver.php
+++ b/core/lib/Drupal/Core/Asset/AssetResolver.php
@@ -285,6 +285,12 @@ class AssetResolver implements AssetResolverInterface {
}
}
+ 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.
diff --git a/core/modules/system/src/Tests/Ajax/CommandsTest.php b/core/modules/system/src/Tests/Ajax/CommandsTest.php
index ec981a7..c645f4f 100644
--- a/core/modules/system/src/Tests/Ajax/CommandsTest.php
+++ b/core/modules/system/src/Tests/Ajax/CommandsTest.php
@@ -9,6 +9,7 @@ namespace Drupal\system\Tests\Ajax;
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\PrependCommand;
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 @@ class CommandsTest extends AjaxTestBase {
$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.');
+ }
+
}
diff --git a/core/modules/system/src/Tests/Common/AttachedAssetsTest.php b/core/modules/system/src/Tests/Common/AttachedAssetsTest.php
index ac7c47b..537d062 100644
--- a/core/modules/system/src/Tests/Common/AttachedAssetsTest.php
+++ b/core/modules/system/src/Tests/Common/AttachedAssetsTest.php
@@ -169,6 +169,27 @@ class AttachedAssetsTest extends KernelTestBase {
}
/**
+ * 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.
*/
function testSettings() {