diff options
author | webchick <webchick@24967.no-reply.drupal.org> | 2012-04-30 21:42:08 -0700 |
---|---|---|
committer | webchick <webchick@24967.no-reply.drupal.org> | 2012-04-30 21:42:08 -0700 |
commit | 47f1ff523d738eb7d2e66ee7f689f62c5d6964de (patch) | |
tree | 547598c66b1be740f7c8827c89d19e81bf6b2143 | |
parent | b7ac66710151bfbb197e653c1d03a43d2ccfcbfa (diff) | |
download | brdo-47f1ff523d738eb7d2e66ee7f689f62c5d6964de.tar.gz brdo-47f1ff523d738eb7d2e66ee7f689f62c5d6964de.tar.bz2 |
Issue #1404380 by msonnabaum, xjm, mjpa: Fixed Unnecessary aggregation of CSS/JS.
-rw-r--r-- | includes/common.inc | 16 | ||||
-rw-r--r-- | modules/simpletest/tests/common.test | 42 |
2 files changed, 56 insertions, 2 deletions
diff --git a/includes/common.inc b/includes/common.inc index ff2549317..f3c936e95 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -3455,7 +3455,13 @@ function drupal_build_css_cache($css) { $data = ''; $uri = ''; $map = variable_get('drupal_css_cache_files', array()); - $key = hash('sha256', serialize($css)); + // Create a new array so that only the file names are used to create the hash. + // This prevents new aggregates from being created unnecessarily. + $css_data = array(); + foreach ($css as $css_file) { + $css_data[] = $css_file['data']; + } + $key = hash('sha256', serialize($css_data)); if (isset($map[$key])) { $uri = $map[$key]; } @@ -4816,7 +4822,13 @@ function drupal_build_js_cache($files) { $contents = ''; $uri = ''; $map = variable_get('drupal_js_cache_files', array()); - $key = hash('sha256', serialize($files)); + // Create a new array so that only the file names are used to create the hash. + // This prevents new aggregates from being created unnecessarily. + $js_data = array(); + foreach ($files as $file) { + $js_data[] = $file['data']; + } + $key = hash('sha256', serialize($js_data)); if (isset($map[$key])) { $uri = $map[$key]; } diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 5ce0d95cd..437b67cd1 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -1343,6 +1343,48 @@ class JavaScriptTestCase extends DrupalWebTestCase { } /** + * Tests JavaScript aggregation when files are added to a different scope. + */ + function testAggregationOrder() { + // Enable JavaScript aggregation. + variable_set('preprocess_js', 1); + drupal_static_reset('drupal_add_js'); + + // Add two JavaScript files to the current request and build the cache. + drupal_add_js('misc/ajax.js'); + drupal_add_js('misc/autocomplete.js'); + + $js_items = drupal_add_js(); + drupal_build_js_cache(array( + 'misc/ajax.js' => $js_items['misc/ajax.js'], + 'misc/autocomplete.js' => $js_items['misc/autocomplete.js'] + )); + + // Store the expected key for the first item in the cache. + $cache = array_keys(variable_get('drupal_js_cache_files', array())); + $expected_key = $cache[0]; + + // Reset variables and add a file in a different scope first. + variable_del('drupal_js_cache_files'); + drupal_static_reset('drupal_add_js'); + drupal_add_js('some/custom/javascript_file.js', array('scope' => 'footer')); + drupal_add_js('misc/ajax.js'); + drupal_add_js('misc/autocomplete.js'); + + // Rebuild the cache. + $js_items = drupal_add_js(); + drupal_build_js_cache(array( + 'misc/ajax.js' => $js_items['misc/ajax.js'], + 'misc/autocomplete.js' => $js_items['misc/autocomplete.js'] + )); + + // Compare the expected key for the first file to the current one. + $cache = array_keys(variable_get('drupal_js_cache_files', array())); + $key = $cache[0]; + $this->assertEqual($key, $expected_key, 'JavaScript aggregation is not affected by ordering in different scopes.'); + } + + /** * Test JavaScript ordering. */ function testRenderOrder() { |