Commit 91b465bd authored by webchick's avatar webchick
Browse files

Issue #2171071 by jessebeach, Wim Leers: Remove drupal_add_library().

parent 1f9a06c6
...@@ -2474,7 +2474,7 @@ function drupal_pre_render_scripts($elements) { ...@@ -2474,7 +2474,7 @@ function drupal_pre_render_scripts($elements) {
* FALSE if there were any missing library dependencies; TRUE if all library * FALSE if there were any missing library dependencies; TRUE if all library
* dependencies were met. * dependencies were met.
* *
* @see drupal_add_library() * @see _drupal_add_library()
* @see _drupal_add_js() * @see _drupal_add_js()
* @see _drupal_add_css() * @see _drupal_add_css()
* @see drupal_render() * @see drupal_render()
...@@ -2490,7 +2490,7 @@ function drupal_process_attached($elements, $dependency_check = FALSE) { ...@@ -2490,7 +2490,7 @@ function drupal_process_attached($elements, $dependency_check = FALSE) {
// Add the libraries first. // Add the libraries first.
$success = TRUE; $success = TRUE;
foreach ($elements['#attached']['library'] as $library) { foreach ($elements['#attached']['library'] as $library) {
if (drupal_add_library($library[0], $library[1]) === FALSE) { if (_drupal_add_library($library[0], $library[1]) === FALSE) {
$success = FALSE; $success = FALSE;
// Exit if the dependency is missing. // Exit if the dependency is missing.
if ($dependency_check) { if ($dependency_check) {
...@@ -2692,7 +2692,7 @@ function drupal_process_states(&$elements) { ...@@ -2692,7 +2692,7 @@ function drupal_process_states(&$elements) {
* @see hook_library_info() * @see hook_library_info()
* @see hook_library_info_alter() * @see hook_library_info_alter()
*/ */
function drupal_add_library($module, $name, $every_page = NULL) { function _drupal_add_library($module, $name, $every_page = NULL) {
$added = &drupal_static(__FUNCTION__, array()); $added = &drupal_static(__FUNCTION__, array());
// Only process the library if it exists and it was not added already. // Only process the library if it exists and it was not added already.
...@@ -2752,7 +2752,7 @@ function drupal_add_library($module, $name, $every_page = NULL) { ...@@ -2752,7 +2752,7 @@ function drupal_add_library($module, $name, $every_page = NULL) {
* or FALSE if it does not exist. If no $name was passed, an associative array * or FALSE if it does not exist. If no $name was passed, an associative array
* of libraries registered by $module is returned (which may be empty). * of libraries registered by $module is returned (which may be empty).
* *
* @see drupal_add_library() * @see _drupal_add_library()
* @see hook_library_info() * @see hook_library_info()
* @see hook_library_info_alter() * @see hook_library_info_alter()
* *
......
...@@ -1566,7 +1566,8 @@ function theme_tableselect($variables) { ...@@ -1566,7 +1566,8 @@ function theme_tableselect($variables) {
// checkboxes/radios in the first table column. // checkboxes/radios in the first table column.
if ($element['#js_select']) { if ($element['#js_select']) {
// Add a "Select all" checkbox. // Add a "Select all" checkbox.
drupal_add_library('system', 'drupal.tableselect'); $attached['#attached']['library'][] = array('system', 'drupal.tableselect');
drupal_render($attached);
array_unshift($header, array('class' => array('select-all'))); array_unshift($header, array('class' => array('select-all')));
} }
else { else {
......
...@@ -1558,7 +1558,8 @@ function theme_table($variables) { ...@@ -1558,7 +1558,8 @@ function theme_table($variables) {
// Add sticky headers, if applicable. // Add sticky headers, if applicable.
if (count($header) && $sticky) { if (count($header) && $sticky) {
drupal_add_library('system', 'drupal.tableheader'); $attached['#attached']['library'][] = array('system', 'drupal.tableheader');
drupal_render($attached);
// Add 'sticky-enabled' class to the table to identify it for JS. // Add 'sticky-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function. // This is needed to target tables constructed by this function.
$attributes['class'][] = 'sticky-enabled'; $attributes['class'][] = 'sticky-enabled';
...@@ -1567,7 +1568,8 @@ function theme_table($variables) { ...@@ -1567,7 +1568,8 @@ function theme_table($variables) {
// with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM // with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM
// and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors. // and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors.
if (count($header) && $responsive) { if (count($header) && $responsive) {
drupal_add_library('system', 'drupal.tableresponsive'); $attached['#attached']['library'][] = array('system', 'drupal.tableresponsive');
drupal_render($attached);
// Add 'responsive-enabled' class to the table to identify it for JS. // Add 'responsive-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function. // This is needed to target tables constructed by this function.
$attributes['class'][] = 'responsive-enabled'; $attributes['class'][] = 'responsive-enabled';
...@@ -2160,7 +2162,8 @@ function template_preprocess_html(&$variables) { ...@@ -2160,7 +2162,8 @@ function template_preprocess_html(&$variables) {
drupal_add_html_head($element, $name); drupal_add_html_head($element, $name);
} }
drupal_add_library('system', 'html5shiv', TRUE); $attached['#attached']['library'][] = array('system', 'html5shiv');
drupal_render($attached);
$variables['page_top'][] = array('#markup' => $page->getBodyTop()); $variables['page_top'][] = array('#markup' => $page->getBodyTop());
$variables['page_bottom'][] = array('#markup' => $page->getBodyBottom()); $variables['page_bottom'][] = array('#markup' => $page->getBodyBottom());
......
...@@ -124,7 +124,9 @@ public function setDialogTitle($title) { ...@@ -124,7 +124,9 @@ public function setDialogTitle($title) {
*/ */
public function render() { public function render() {
// Add the library for handling the dialog in the response. // Add the library for handling the dialog in the response.
drupal_add_library('system', 'drupal.dialog.ajax'); $attached['#attached']['library'][] = array('system', 'drupal.dialog.ajax');
drupal_render($attached);
// For consistency ensure the modal option is set to TRUE or FALSE. // For consistency ensure the modal option is set to TRUE or FALSE.
$this->dialogOptions['modal'] = isset($this->dialogOptions['modal']) && $this->dialogOptions['modal']; $this->dialogOptions['modal'] = isset($this->dialogOptions['modal']) && $this->dialogOptions['modal'];
......
...@@ -121,9 +121,9 @@ function testExportTranslation() { ...@@ -121,9 +121,9 @@ function testExportTranslation() {
* Test exportation of translation template file. * Test exportation of translation template file.
*/ */
function testExportTranslationTemplateFile() { function testExportTranslationTemplateFile() {
// Load an admin page with JavaScript so drupal_add_library() fires at least // Load an admin page with JavaScript so _drupal_add_library() fires at
// once and _locale_parse_js_file() gets to run at least once so that the // least once and _locale_parse_js_file() gets to run at least once so that
// locales_source table gets populated with something. // the locales_source table gets populated with something.
$this->drupalGet('admin/config/regional/language'); $this->drupalGet('admin/config/regional/language');
// Get the translation template file. // Get the translation template file.
$this->drupalPostForm('admin/config/regional/translate/export', array(), t('Export')); $this->drupalPostForm('admin/config/regional/translate/export', array(), t('Export'));
......
...@@ -36,7 +36,8 @@ public static function getInfo() { ...@@ -36,7 +36,8 @@ public static function getInfo() {
* @see locale_library_info_alter() * @see locale_library_info_alter()
*/ */
public function testLibraryInfoAlter() { public function testLibraryInfoAlter() {
drupal_add_library('system', 'jquery.ui.datepicker'); $attached['#attached']['library'][] = array('system', 'jquery.ui.datepicker');
drupal_render($attached);
$scripts = drupal_get_js(); $scripts = drupal_get_js();
$this->assertTrue(strpos($scripts, 'locale.datepicker.js'), 'locale.datepicker.js added to scripts.'); $this->assertTrue(strpos($scripts, 'locale.datepicker.js'), 'locale.datepicker.js added to scripts.');
} }
......
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,6 @@
function theme_simpletest_test_table($variables) { function theme_simpletest_test_table($variables) {
$table = $variables['table']; $table = $variables['table'];
drupal_add_library('simpletest', 'drupal.simpletest');
// Create header for test selection table. // Create header for test selection table.
$header = array( $header = array(
array('class' => array('select-all')), array('class' => array('select-all')),
...@@ -133,6 +131,7 @@ function theme_simpletest_test_table($variables) { ...@@ -133,6 +131,7 @@ function theme_simpletest_test_table($variables) {
// Add js array of settings. // Add js array of settings.
$attached = array(); $attached = array();
$attached['#attached']['library'][] = array('simpletest', 'drupal.simpletest');
$attached['#attached']['js'][] = array( $attached['#attached']['js'][] = array(
'data' => array('simpleTest' => $js), 'data' => array('simpleTest' => $js),
'type' => 'setting', 'type' => 'setting',
......
...@@ -47,9 +47,8 @@ function setUp() { ...@@ -47,9 +47,8 @@ function setUp() {
$config->set('js.preprocess', 0); $config->set('js.preprocess', 0);
$config->save(); $config->save();
// Reset _drupal_add_js() and drupal_add_library() statics before each test. // Reset _drupal_add_js() statics before each test.
drupal_static_reset('_drupal_add_js'); drupal_static_reset('_drupal_add_js');
drupal_static_reset('drupal_add_library');
} }
function tearDown() { function tearDown() {
...@@ -71,7 +70,9 @@ function testDefault() { ...@@ -71,7 +70,9 @@ function testDefault() {
* Tests adding a JavaScript file. * Tests adding a JavaScript file.
*/ */
function testAddFile() { function testAddFile() {
$javascript = _drupal_add_js('core/misc/collapse.js'); $attached['#attached']['js']['core/misc/collapse.js'] = array();
drupal_render($attached);
$javascript = _drupal_add_js();
$this->assertTrue(array_key_exists('core/misc/collapse.js', $javascript), 'JavaScript files are correctly added.'); $this->assertTrue(array_key_exists('core/misc/collapse.js', $javascript), 'JavaScript files are correctly added.');
} }
...@@ -80,7 +81,8 @@ function testAddFile() { ...@@ -80,7 +81,8 @@ function testAddFile() {
*/ */
function testAddSetting() { function testAddSetting() {
// Add a file in order to test default settings. // Add a file in order to test default settings.
drupal_add_library('system', 'drupalSettings'); $attached['#attached']['library'][] = array('system', 'drupalSettings');
drupal_render($attached);
$javascript = _drupal_add_js(); $javascript = _drupal_add_js();
$last_settings = reset($javascript['settings']['data']); $last_settings = reset($javascript['settings']['data']);
$this->assertTrue(array_key_exists('currentPath', $last_settings), 'The current path JavaScript setting is set correctly.'); $this->assertTrue(array_key_exists('currentPath', $last_settings), 'The current path JavaScript setting is set correctly.');
...@@ -95,8 +97,9 @@ function testAddSetting() { ...@@ -95,8 +97,9 @@ function testAddSetting() {
* Tests adding an external JavaScript File. * Tests adding an external JavaScript File.
*/ */
function testAddExternal() { function testAddExternal() {
$path = 'http://example.com/script.js'; $attached['#attached']['js']['http://example.com/script.js'] = array('type' => 'external');
$javascript = _drupal_add_js($path, 'external'); drupal_render($attached);
$javascript = _drupal_add_js();
$this->assertTrue(array_key_exists('http://example.com/script.js', $javascript), 'Added an external JavaScript file.'); $this->assertTrue(array_key_exists('http://example.com/script.js', $javascript), 'Added an external JavaScript file.');
} }
...@@ -106,12 +109,18 @@ function testAddExternal() { ...@@ -106,12 +109,18 @@ function testAddExternal() {
function testAttributes() { function testAttributes() {
$default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0'; $default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
drupal_add_library('system', 'drupal'); $attached['#attached']['library'][] = array('system', 'drupal');
_drupal_add_js('http://example.com/script.js', array('attributes' => array('defer' => 'defer'))); $attached['#attached']['js']['http://example.com/script.js'] = array(
_drupal_add_js('core/misc/collapse.js', array('attributes' => array('defer' => 'defer'))); 'type' => 'external',
'attributes' => array('defer' => 'defer'),
);
$attached['#attached']['js']['core/misc/collapse.js'] = array(
'attributes' => array('defer' => 'defer'),
);
drupal_render($attached);
$javascript = drupal_get_js(); $javascript = drupal_get_js();
$expected_1 = '<script src="http://example.com/script.js?' . $default_query_string . '" defer="defer"></script>'; $expected_1 = '<script src="http://example.com/script.js" defer="defer"></script>';
$expected_2 = '<script src="' . file_create_url('core/misc/collapse.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_1) > 0, 'Rendered external JavaScript with correct defer attribute.');
...@@ -127,12 +136,18 @@ function testAggregatedAttributes() { ...@@ -127,12 +136,18 @@ function testAggregatedAttributes() {
$default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0'; $default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
drupal_add_library('system', 'drupal'); $attached['#attached']['library'][] = array('system', 'drupal');
_drupal_add_js('http://example.com/script.js', array('attributes' => array('defer' => 'defer'))); $attached['#attached']['js']['http://example.com/script.js'] = array(
_drupal_add_js('core/misc/collapse.js', array('attributes' => array('defer' => 'defer'))); 'type' => 'external',
'attributes' => array('defer' => 'defer'),
);
$attached['#attached']['js']['core/misc/collapse.js'] = array(
'attributes' => array('defer' => 'defer'),
);
drupal_render($attached);
$javascript = drupal_get_js(); $javascript = drupal_get_js();
$expected_1 = '<script src="http://example.com/script.js?' . $default_query_string . '" defer="defer"></script>'; $expected_1 = '<script src="http://example.com/script.js" defer="defer"></script>';
$expected_2 = '<script src="' . file_create_url('core/misc/collapse.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 with aggregation enabled.'); $this->assertTrue(strpos($javascript, $expected_1) > 0, 'Rendered external JavaScript with correct defer attribute with aggregation enabled.');
...@@ -143,7 +158,9 @@ function testAggregatedAttributes() { ...@@ -143,7 +158,9 @@ function testAggregatedAttributes() {
* Tests drupal_get_js() for JavaScript settings. * Tests drupal_get_js() for JavaScript settings.
*/ */
function testHeaderSetting() { function testHeaderSetting() {
drupal_add_library('system', 'drupalSettings'); $attached = array();
$attached['#attached']['library'][] = array('system', 'drupalSettings');
drupal_render($attached);
$javascript = drupal_get_js('header'); $javascript = drupal_get_js('header');
$this->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.'); $this->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.');
...@@ -152,25 +169,57 @@ function testHeaderSetting() { ...@@ -152,25 +169,57 @@ function testHeaderSetting() {
$this->assertTrue(strpos($javascript, 'currentPath') > 0, 'Rendered JavaScript header returns currentPath setting.'); $this->assertTrue(strpos($javascript, 'currentPath') > 0, 'Rendered JavaScript header returns currentPath setting.');
// Only the second of these two entries should appear in drupalSettings. // Only the second of these two entries should appear in drupalSettings.
_drupal_add_js(array('commonTest' => 'commonTestShouldNotAppear'), 'setting'); $attached = array();
_drupal_add_js(array('commonTest' => 'commonTestShouldAppear'), 'setting'); $attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTest' => 'commonTestShouldNotAppear'),
);
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTest' => 'commonTestShouldAppear'),
);
// Only the second of these entries should appear in drupalSettings. // Only the second of these entries should appear in drupalSettings.
_drupal_add_js(array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralOldValue')), 'setting'); $attached['#attached']['js'][] = array(
_drupal_add_js(array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralNewValue')), 'setting'); 'type' => 'setting',
'data' => array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralOldValue')),
);
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralNewValue')),
);
// Only the second of these two entries should appear in drupalSettings. // Only the second of these two entries should appear in drupalSettings.
_drupal_add_js(array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralOldValue')), 'setting'); $attached['#attached']['js'][] = array(
_drupal_add_js(array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralNewValue')), 'setting'); 'type' => 'setting',
'data' => array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralOldValue')),
);
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralNewValue')),
);
// Real world test case: multiple elements in a render array are adding the // Real world test case: multiple elements in a render array are adding the
// same (or nearly the same) JavaScript settings. When merged, they should // same (or nearly the same) JavaScript settings. When merged, they should
// contain all settings and not duplicate some settings. // contain all settings and not duplicate some settings.
$settings_one = array('moduleName' => array('ui' => array('button A', 'button B'), 'magical flag' => 3.14159265359)); $settings_one = array('moduleName' => array('ui' => array('button A', 'button B'), 'magical flag' => 3.14159265359));
_drupal_add_js(array('commonTestRealWorldIdentical' => $settings_one), 'setting'); $attached['#attached']['js'][] = array(
_drupal_add_js(array('commonTestRealWorldIdentical' => $settings_one), 'setting'); 'type' => 'setting',
'data' => array('commonTestRealWorldIdentical' => $settings_one),
);
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestRealWorldIdentical' => $settings_one),
);
$settings_two = array('moduleName' => array('ui' => array('button A', 'button B'), 'magical flag' => 3.14159265359, 'thingiesOnPage' => array('id1' => array()))); $settings_two = array('moduleName' => array('ui' => array('button A', 'button B'), 'magical flag' => 3.14159265359, 'thingiesOnPage' => array('id1' => array())));
_drupal_add_js(array('commonTestRealWorldAlmostIdentical' => $settings_two), 'setting'); $attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestRealWorldAlmostIdentical' => $settings_two),
);
$settings_two = array('moduleName' => array('ui' => array('button C', 'button D'), 'magical flag' => 3.14, 'thingiesOnPage' => array('id2' => array()))); $settings_two = array('moduleName' => array('ui' => array('button C', 'button D'), 'magical flag' => 3.14, 'thingiesOnPage' => array('id2' => array())));
_drupal_add_js(array('commonTestRealWorldAlmostIdentical' => $settings_two), 'setting'); $attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestRealWorldAlmostIdentical' => $settings_two),
);
drupal_render($attached);
$javascript = drupal_get_js('header'); $javascript = drupal_get_js('header');
// Test whether _drupal_add_js can be used to override a previous setting. // Test whether _drupal_add_js can be used to override a previous setting.
...@@ -205,8 +254,9 @@ function testHeaderSetting() { ...@@ -205,8 +254,9 @@ function testHeaderSetting() {
* Tests to see if resetting the JavaScript empties the cache. * Tests to see if resetting the JavaScript empties the cache.
*/ */
function testReset() { function testReset() {
drupal_add_library('system', 'drupal'); $attached['#attached']['library'][] = array('system', 'drupal');
_drupal_add_js('core/misc/collapse.js'); $attached['#attached']['js']['core/misc/collapse.js'] = array();
drupal_render($attached);
drupal_static_reset('_drupal_add_js'); drupal_static_reset('_drupal_add_js');
$this->assertEqual(array(), _drupal_add_js(), 'Resetting the JavaScript correctly empties the cache.'); $this->assertEqual(array(), _drupal_add_js(), 'Resetting the JavaScript correctly empties the cache.');
} }
...@@ -215,9 +265,15 @@ function testReset() { ...@@ -215,9 +265,15 @@ function testReset() {
* Tests adding inline scripts. * Tests adding inline scripts.
*/ */
function testAddInline() { function testAddInline() {
drupal_add_library('system', 'jquery');
$inline = 'jQuery(function () { });'; $inline = 'jQuery(function () { });';
$javascript = _drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer')); $attached['#attached']['library'][] = array('system', 'jquery');
$attached['#attached']['js'][] = array(
'type' => 'inline',
'data' => $inline,
'attributes' => array('defer' => 'defer'),
);
drupal_render($attached);
$javascript = _drupal_add_js();
$this->assertTrue(array_key_exists('core/assets/vendor/jquery/jquery.js', $javascript), 'jQuery is added when inline scripts are added.'); $this->assertTrue(array_key_exists('core/assets/vendor/jquery/jquery.js', $javascript), 'jQuery is added when inline scripts are added.');
$data = end($javascript); $data = end($javascript);
$this->assertEqual($inline, $data['data'], 'Inline JavaScript is correctly added to the footer.'); $this->assertEqual($inline, $data['data'], 'Inline JavaScript is correctly added to the footer.');
...@@ -227,9 +283,14 @@ function testAddInline() { ...@@ -227,9 +283,14 @@ function testAddInline() {
* Tests rendering an external JavaScript file. * Tests rendering an external JavaScript file.
*/ */
function testRenderExternal() { function testRenderExternal() {
drupal_add_library('system', 'drupal');
$external = 'http://example.com/example.js'; $external = 'http://example.com/example.js';
_drupal_add_js($external, 'external'); $attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js'][] = array(
'type' => 'external',
'data' => $external,
);
drupal_render($attached);
$javascript = drupal_get_js(); $javascript = drupal_get_js();
// Local files have a base_path() prefix, external files should not. // Local files have a base_path() prefix, external files should not.
$this->assertTrue(strpos($javascript, 'src="' . $external) > 0, 'Rendering an external JavaScript file.'); $this->assertTrue(strpos($javascript, 'src="' . $external) > 0, 'Rendering an external JavaScript file.');
...@@ -239,9 +300,16 @@ function testRenderExternal() { ...@@ -239,9 +300,16 @@ function testRenderExternal() {
* Tests drupal_get_js() with a footer scope. * Tests drupal_get_js() with a footer scope.
*/ */
function testFooterHTML() { function testFooterHTML() {
drupal_add_library('system', 'drupal');
$inline = 'jQuery(function () { });'; $inline = 'jQuery(function () { });';
_drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer')); $attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js'][] = array(
'type' => 'inline',
'data' => $inline,
'scope' => 'footer',
'attributes' => array('defer' => 'defer'),
);
drupal_render($attached);
$javascript = drupal_get_js('footer'); $javascript = drupal_get_js('footer');
$this->assertTrue(strpos($javascript, $inline) > 0, 'Rendered JavaScript footer returns the inline code.'); $this->assertTrue(strpos($javascript, $inline) > 0, 'Rendered JavaScript footer returns the inline code.');
} }
...@@ -250,8 +318,10 @@ function testFooterHTML() { ...@@ -250,8 +318,10 @@ function testFooterHTML() {
* Tests _drupal_add_js() sets preproccess to FALSE when cache is also FALSE. * Tests _drupal_add_js() sets preproccess to FALSE when cache is also FALSE.
*/ */
function testNoCache() { function testNoCache() {
drupal_add_library('system', 'drupal'); $attached['#attached']['library'][] = array('system', 'drupal');
$javascript = _drupal_add_js('core/misc/collapse.js', array('cache' => FALSE)); $attached['#attached']['js']['core/misc/collapse.js'] = array('cache' => FALSE);
drupal_render($attached);
$javascript = _drupal_add_js();
$this->assertFalse($javascript['core/misc/collapse.js']['preprocess'], 'Setting cache to FALSE sets proprocess to FALSE when adding JavaScript.'); $this->assertFalse($javascript['core/misc/collapse.js']['preprocess'], 'Setting cache to FALSE sets proprocess to FALSE when adding JavaScript.');
} }
...@@ -259,8 +329,10 @@ function testNoCache() { ...@@ -259,8 +329,10 @@ function testNoCache() {
* Tests adding a JavaScript file with a different group. * Tests adding a JavaScript file with a different group.
*/ */
function testDifferentGroup() { function testDifferentGroup() {
drupal_add_library('system', 'drupal'); $attached['#attached']['library'][] = array('system', 'drupal');
$javascript = _drupal_add_js('core/misc/collapse.js', array('group' => JS_THEME)); $attached['#attached']['js']['core/misc/collapse.js'] = array('group' => JS_THEME);
drupal_render($attached);
$javascript = _drupal_add_js();
$this->assertEqual($javascript['core/misc/collapse.js']['group'], JS_THEME, 'Adding a JavaScript file with a different group caches the given group.'); $this->assertEqual($javascript['core/misc/collapse.js']['group'], JS_THEME, 'Adding a JavaScript file with a different group caches the given group.');
} }
...@@ -268,7 +340,9 @@ function testDifferentGroup() { ...@@ -268,7 +340,9 @@ function testDifferentGroup() {
* Tests adding a JavaScript file with a different weight. * Tests adding a JavaScript file with a different weight.
*/ */
function testDifferentWeight() { function testDifferentWeight() {
$javascript = _drupal_add_js('core/misc/collapse.js', array('weight' => 2)); $attached['#attached']['js']['core/misc/collapse.js'] = array('weight' => 2);
drupal_render($attached);
$javascript = _drupal_add_js();
$this->assertEqual($javascript['core/misc/collapse.js']['weight'], 2, 'Adding a JavaScript file with a different weight caches the given weight.'); $this->assertEqual($javascript['core/misc/collapse.js']['weight'], 2, 'Adding a JavaScript file with a different weight caches the given weight.');
} }
...@@ -280,9 +354,16 @@ function testDifferentWeight() { ...@@ -280,9 +354,16 @@ function testDifferentWeight() {
function testBrowserConditionalComments() { function testBrowserConditionalComments() {
$default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0'; $default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
drupal_add_library('system', 'drupal'); $attached['#attached']['library'][] = array('system', 'drupal');
_drupal_add_js('core/misc/collapse.js', array('browsers' => array('IE' => 'lte IE 8', '!IE' => FALSE))); $attached['#attached']['js']['core/misc/collapse.js'] = array(
_drupal_add_js('jQuery(function () { });', array('type' => 'inline', 'browsers' => array('IE' => FALSE))); 'browsers' => array('IE' => 'lte IE 8', '!IE' => FALSE),
);
$attached['#attached']['js'][] = array(
'type' => 'inline',
'data' => 'jQuery(function () { });',
'browsers' => array('IE' => FALSE),
);
drupal_render($attached);
$javascript = drupal_get_js(); $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_1 = "<!--[if lte IE 8]>\n" . '<script src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '"></script>' . "\n<![endif]-->";
...@@ -296,9 +377,10 @@ function testBrowserConditionalComments() { ...@@ -296,9 +377,10 @@ function testBrowserConditionalComments() {
* Tests JavaScript versioning. * Tests JavaScript versioning.
*/ */
function testVersionQueryString() { function testVersionQueryString() {
drupal_add_library('system', 'drupal'); $attached['#attached']['library'][] = array('system', 'drupal');
_drupal_add_js('core/misc/collapse.js', array('version' => 'foo')); $attached['#attached']['js']['core/misc/collapse.js'] = array('version' => 'foo');
_drupal_add_js('core/misc/ajax.js', array('version' => 'bar')); $attached['#attached']['js']['core/misc/ajax.js'] = array('version' => 'bar');
drupal_render($attached);
$javascript = drupal_get_js(); $javascript = drupal_get_js();
$this->assertTrue(strpos($javascript, 'core/misc/collapse.js?v=foo') > 0 && strpos($javascript, 'core/misc/ajax.js?v=bar') > 0 , 'JavaScript version identifiers correctly appended to URLs'); $this->assertTrue(strpos($javascript, 'core/misc/collapse.js?v=foo') > 0 && strpos($javascript, 'core/misc/ajax.js?v=bar') > 0 , 'JavaScript version identifiers correctly appended to URLs');