From 968bf0addb5473ee33346b67780b210613f6c3de Mon Sep 17 00:00:00 2001 From: Mark Carver Date: Sat, 15 Feb 2014 13:34:31 -0600 Subject: [PATCH] Issue #865536 by Mark Carver, ericduran, effulgentsia, tim.plunkett, cosmicdreams, David_Rothstein, scor, aspilicious, Jacine, jessebeach, sreynen | altrugon: Drupal_add_js() is missing the 'browsers' option. --- modules/simpletest/tests/common.test | 135 +++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 44eecdc..64131f3 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -1414,6 +1414,141 @@ class JavaScriptTestCase extends DrupalWebTestCase { } /** + * Test adding JavaScript within conditional comments. + * + * @see drupal_pre_render_conditional_comments() + */ + function testBrowserConditionalComments() { + $default_query_string = variable_get('css_js_query_string', '0'); + + drupal_add_js('misc/collapse.js', array('browsers' => array('IE' => 'lte IE 8', '!IE' => FALSE))); + drupal_add_js('jQuery(function () { });', array('type' => 'inline', 'browsers' => array('IE' => FALSE))); + $javascript = drupal_get_js(); + + $expected_1 = ""; + $expected_2 = "\n" . '' . "\n"; + + $this->assertTrue(strpos($javascript, $expected_1) > 0, t('Rendered JavaScript within downlevel-hidden conditional comments.')); + $this->assertTrue(strpos($javascript, $expected_2) > 0, t('Rendered JavaScript within downlevel-revealed conditional comments.')); + } + + /** + * Test JavaScript versioning. + */ + function testVersionQueryString() { + drupal_add_js('misc/collapse.js', array('version' => 'foo')); + drupal_add_js('misc/ajax.js', array('version' => 'bar')); + $javascript = drupal_get_js(); + $this->assertTrue(strpos($javascript, 'misc/collapse.js?v=foo') > 0 && strpos($javascript, 'misc/ajax.js?v=bar') > 0 , t('JavaScript version identifiers correctly appended to URLs')); + } + + /** + * Test JavaScript grouping and aggregation. + */ + function testAggregation() { + $default_query_string = variable_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. + drupal_add_js('misc/ajax.js'); + drupal_add_js('misc/authorize.js', array('every_page' => TRUE)); + drupal_add_js('misc/autocomplete.js'); + drupal_add_js('misc/batch.js', array('every_page' => TRUE)); + drupal_add_js('misc/collapse.js', array('browsers' => array( + 'IE' => 'lt IE 8', + '!IE' => FALSE, + ))); + drupal_add_js('misc/states.js', array('browsers' => array( + 'IE' => 'lte IE 6', + '!IE' => FALSE, + ))); + drupal_add_js('misc/textarea.js', array('browsers' => array( + 'IE' => 'gt IE 7', + '!IE' => TRUE, + ))); + drupal_add_js('misc/progress.js', array('browsers' => array( + 'IE' => 'lt IE 8', + '!IE' => FALSE, + ))); + $javascript = drupal_get_js(); + $expected = implode("\n", array( + '', + '', + '', + '', + "\n\n", + "\n", + "\n", + "", + '', + "", + )); + $this->assertTrue(strpos($javascript, $expected) !== FALSE, t('Unaggregated JavaScript is added in the expected group order.')); + + // Enable aggregation. + variable_set('preprocess_js', 1); + + // Ensure a file is made for the 'every_page' files, one for different + // browser conditions and another for the rest. + drupal_static_reset('drupal_add_js'); + + // drupal_add_js() adds jquery.js, jquery.once.js and drupal.js if it's + // array is empty. This affects expect aggregation values, inject a fake + // settings array so they don't get added. + $settings = drupal_js_defaults(array()); + $settings['type'] = 'setting'; + $settings['group'] = JS_LIBRARY; + $drupal_add_js = &drupal_static('drupal_add_js', array()); + $drupal_add_js['settings'] = $settings; + + // Duplicate order and options above, but check for aggregated values. + drupal_add_js('misc/ajax.js'); + drupal_add_js('misc/authorize.js', array('every_page' => TRUE)); + drupal_add_js('misc/autocomplete.js'); + drupal_add_js('misc/batch.js', array('every_page' => TRUE)); + drupal_add_js('misc/collapse.js', array('browsers' => array( + 'IE' => 'lt IE 8', + '!IE' => FALSE, + ))); + drupal_add_js('misc/states.js', array('browsers' => array( + 'IE' => 'lte IE 6', + '!IE' => FALSE, + ))); + drupal_add_js('misc/textarea.js', array('browsers' => array( + 'IE' => 'gt IE 7', + '!IE' => TRUE, + ))); + drupal_add_js('misc/progress.js', array('browsers' => array( + 'IE' => 'lt IE 8', + '!IE' => FALSE, + ))); + $js_items = drupal_add_js(); + $javascript = drupal_get_js(); + $expected = implode("\n", array( + '', + '', + "\n\n", + "\n", + "", + '', + "", + )); + $this->assertTrue(strpos($javascript, $expected) !== FALSE, t('JavaScript is aggregated in the expected groups and order.')); + } + + /** * Test JavaScript ordering. */ function testRenderOrder() { -- 1.8.5.3