1 common.test CommonJavaScriptTestCase::testAggregation()

Test JavaScript grouping and aggregation.

File

core/modules/simpletest/tests/common.test, line 1514
Tests for common.inc functionality.

Class

CommonJavaScriptTestCase
Tests for the JavaScript system.

Code

function testAggregation() {
  $default_query_string = state_get('css_js_query_string', '0');

  // To optimize aggregation, items with the 'every_page' option are ordered
  // ahead of ones without. The order of JavaScript execution must be the
  // same regardless of whether aggregation is enabled, so ensure this
  // expected order, first with aggregation off.
  backdrop_add_js('core/misc/ajax.js');
  backdrop_add_js('core/misc/collapse.js', array('every_page' => TRUE));
  backdrop_add_js('core/misc/autocomplete.js');
  backdrop_add_js('core/misc/batch.js', array('every_page' => TRUE));
  $javascript = backdrop_get_js();
  $expected = implode("\n", array(
    '<script src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '"></script>',
    '<script src="' . file_create_url('core/misc/batch.js') . '?' . $default_query_string . '"></script>',
    '<script src="' . file_create_url('core/misc/ajax.js') . '?' . $default_query_string . '"></script>',
    '<script src="' . file_create_url('core/misc/autocomplete.js') . '?' . $default_query_string . '"></script>',
  ));
  $this->assertTrue(strpos($javascript, $expected) > 0, t('Unaggregated JavaScript is added in the expected group order.'));

  // Now ensure that with aggregation on, one file is made for the
  // 'every_page' files, and one file is made for the others.
  backdrop_static_reset('backdrop_add_js');
  config_set('system.core', 'preprocess_js', 1);
  backdrop_add_js('core/misc/ajax.js');
  backdrop_add_js('core/misc/collapse.js', array('every_page' => TRUE));
  backdrop_add_js('core/misc/autocomplete.js');
  backdrop_add_js('core/misc/batch.js', array('every_page' => TRUE));
  $js_items = backdrop_add_js();
  $javascript = backdrop_get_js();
  $expected = implode("\n", array(
    '<script src="' . file_create_url(backdrop_build_js_cache(array('core/misc/collapse.js' => $js_items['core/misc/collapse.js'], 'core/misc/batch.js' => $js_items['core/misc/batch.js']))) . '"></script>',
    '<script src="' . file_create_url(backdrop_build_js_cache(array('core/misc/ajax.js' => $js_items['core/misc/ajax.js'], 'core/misc/autocomplete.js' => $js_items['core/misc/autocomplete.js']))) . '"></script>',
  ));
  $this->assertTrue(strpos($javascript, $expected) > 0, t('JavaScript is aggregated in the expected groups and order.'));
}