function JavaScriptTest::testAttributes

Tests adding JavaScript files with additional attributes.

File

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

Class

JavaScriptTest
Tests the JavaScript system.

Namespace

Drupal\system\Tests\Common

Code

function testAttributes() {
  $default_query_string = variable_get('css_js_query_string', '0');
  drupal_add_library('system', 'drupal');
  drupal_add_js('http://example.com/script.js', array(
    'attributes' => array(
      'defer' => 'defer',
    ),
  ));
  drupal_add_js('core/misc/collapse.js', array(
    'attributes' => array(
      'defer' => 'defer',
    ),
  ));
  $javascript = drupal_get_js();
  $expected_1 = '<script src="http://example.com/script.js?' . $default_query_string . '" defer="defer"></script>';
  $expected_2 = '<script src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '" defer="defer"></script>';
  $this
    ->assertTrue(strpos($javascript, $expected_1) > 0, 'Rendered external JavaScript with correct defer attribute.');
  $this
    ->assertTrue(strpos($javascript, $expected_2) > 0, 'Rendered internal JavaScript with correct defer attribute.');
}