function JavaScriptTest::testBrowserConditionalComments

Tests adding JavaScript within conditional comments.

See also

drupal_pre_render_conditional_comments()

File

drupal/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php, line 260
Definition of Drupal\system\Tests\Common\JavaScriptTest.

Class

JavaScriptTest
Tests the JavaScript system.

Namespace

Drupal\system\Tests\Common

Code

function testBrowserConditionalComments() {
  $default_query_string = variable_get('css_js_query_string', '0');
  drupal_add_library('system', 'drupal');
  drupal_add_js('core/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 = "<!--[if lte IE 8]>\n" . '<script src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '"></script>' . "\n<![endif]-->";
  $expected_2 = "<!--[if !IE]><!-->\n" . '<script>' . "\n<!--//--><![CDATA[//><!--\n" . 'jQuery(function () { });' . "\n//--><!]]>\n" . '</script>' . "\n<!--<![endif]-->";
  $this
    ->assertTrue(strpos($javascript, $expected_1) > 0, 'Rendered JavaScript within downlevel-hidden conditional comments.');
  $this
    ->assertTrue(strpos($javascript, $expected_2) > 0, 'Rendered JavaScript within downlevel-revealed conditional comments.');
}