|
|
|
@ -47,8 +47,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
$config->set('js.preprocess', 0);
|
|
|
|
|
$config->save();
|
|
|
|
|
|
|
|
|
|
// Reset _drupal_add_js() statics before each test.
|
|
|
|
|
// Reset _drupal_add_js() and drupal_add_library() statics before each test.
|
|
|
|
|
drupal_static_reset('_drupal_add_js');
|
|
|
|
|
drupal_static_reset('drupal_add_library');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function tearDown() {
|
|
|
|
@ -70,9 +71,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests adding a JavaScript file.
|
|
|
|
|
*/
|
|
|
|
|
function testAddFile() {
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array();
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
$javascript = _drupal_add_js();
|
|
|
|
|
$javascript = _drupal_add_js('core/misc/collapse.js');
|
|
|
|
|
$this->assertTrue(array_key_exists('core/misc/collapse.js', $javascript), 'JavaScript files are correctly added.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -81,8 +80,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
*/
|
|
|
|
|
function testAddSetting() {
|
|
|
|
|
// Add a file in order to test default settings.
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupalSettings');
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'drupalSettings');
|
|
|
|
|
$javascript = _drupal_add_js();
|
|
|
|
|
$last_settings = reset($javascript['settings']['data']);
|
|
|
|
|
$this->assertTrue(array_key_exists('currentPath', $last_settings), 'The current path JavaScript setting is set correctly.');
|
|
|
|
@ -97,9 +95,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests adding an external JavaScript File.
|
|
|
|
|
*/
|
|
|
|
|
function testAddExternal() {
|
|
|
|
|
$attached['#attached']['js']['http://example.com/script.js'] = array('type' => 'external');
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
$javascript = _drupal_add_js();
|
|
|
|
|
$path = 'http://example.com/script.js';
|
|
|
|
|
$javascript = _drupal_add_js($path, 'external');
|
|
|
|
|
$this->assertTrue(array_key_exists('http://example.com/script.js', $javascript), 'Added an external JavaScript file.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -109,18 +106,12 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
function testAttributes() {
|
|
|
|
|
$default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
|
|
|
|
|
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['http://example.com/script.js'] = array(
|
|
|
|
|
'type' => 'external',
|
|
|
|
|
'attributes' => array('defer' => 'defer'),
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array(
|
|
|
|
|
'attributes' => array('defer' => 'defer'),
|
|
|
|
|
);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
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" defer="defer"></script>';
|
|
|
|
|
$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.');
|
|
|
|
@ -136,18 +127,12 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
|
|
|
|
|
$default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
|
|
|
|
|
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['http://example.com/script.js'] = array(
|
|
|
|
|
'type' => 'external',
|
|
|
|
|
'attributes' => array('defer' => 'defer'),
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array(
|
|
|
|
|
'attributes' => array('defer' => 'defer'),
|
|
|
|
|
);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
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" defer="defer"></script>';
|
|
|
|
|
$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 with aggregation enabled.');
|
|
|
|
@ -158,9 +143,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests drupal_get_js() for JavaScript settings.
|
|
|
|
|
*/
|
|
|
|
|
function testHeaderSetting() {
|
|
|
|
|
$attached = array();
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupalSettings');
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'drupalSettings');
|
|
|
|
|
|
|
|
|
|
$javascript = drupal_get_js('header');
|
|
|
|
|
$this->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.');
|
|
|
|
@ -169,57 +152,25 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
$this->assertTrue(strpos($javascript, 'currentPath') > 0, 'Rendered JavaScript header returns currentPath setting.');
|
|
|
|
|
|
|
|
|
|
// Only the second of these two entries should appear in drupalSettings.
|
|
|
|
|
$attached = array();
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTest' => 'commonTestShouldNotAppear'),
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTest' => 'commonTestShouldAppear'),
|
|
|
|
|
);
|
|
|
|
|
_drupal_add_js(array('commonTest' => 'commonTestShouldNotAppear'), 'setting');
|
|
|
|
|
_drupal_add_js(array('commonTest' => 'commonTestShouldAppear'), 'setting');
|
|
|
|
|
// Only the second of these entries should appear in drupalSettings.
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralOldValue')),
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralNewValue')),
|
|
|
|
|
);
|
|
|
|
|
_drupal_add_js(array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralOldValue')), 'setting');
|
|
|
|
|
_drupal_add_js(array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralNewValue')), 'setting');
|
|
|
|
|
// Only the second of these two entries should appear in drupalSettings.
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralOldValue')),
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralNewValue')),
|
|
|
|
|
);
|
|
|
|
|
_drupal_add_js(array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralOldValue')), 'setting');
|
|
|
|
|
_drupal_add_js(array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralNewValue')), 'setting');
|
|
|
|
|
// Real world test case: multiple elements in a render array are adding the
|
|
|
|
|
// same (or nearly the same) JavaScript settings. When merged, they should
|
|
|
|
|
// contain all settings and not duplicate some settings.
|
|
|
|
|
$settings_one = array('moduleName' => array('ui' => array('button A', 'button B'), 'magical flag' => 3.14159265359));
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTestRealWorldIdentical' => $settings_one),
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTestRealWorldIdentical' => $settings_one),
|
|
|
|
|
);
|
|
|
|
|
_drupal_add_js(array('commonTestRealWorldIdentical' => $settings_one), 'setting');
|
|
|
|
|
_drupal_add_js(array('commonTestRealWorldIdentical' => $settings_one), 'setting');
|
|
|
|
|
$settings_two = array('moduleName' => array('ui' => array('button A', 'button B'), 'magical flag' => 3.14159265359, 'thingiesOnPage' => array('id1' => array())));
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTestRealWorldAlmostIdentical' => $settings_two),
|
|
|
|
|
);
|
|
|
|
|
_drupal_add_js(array('commonTestRealWorldAlmostIdentical' => $settings_two), 'setting');
|
|
|
|
|
$settings_two = array('moduleName' => array('ui' => array('button C', 'button D'), 'magical flag' => 3.14, 'thingiesOnPage' => array('id2' => array())));
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'setting',
|
|
|
|
|
'data' => array('commonTestRealWorldAlmostIdentical' => $settings_two),
|
|
|
|
|
);
|
|
|
|
|
_drupal_add_js(array('commonTestRealWorldAlmostIdentical' => $settings_two), 'setting');
|
|
|
|
|
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
$javascript = drupal_get_js('header');
|
|
|
|
|
|
|
|
|
|
// Test whether _drupal_add_js can be used to override a previous setting.
|
|
|
|
@ -254,9 +205,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests to see if resetting the JavaScript empties the cache.
|
|
|
|
|
*/
|
|
|
|
|
function testReset() {
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array();
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
_drupal_add_js('core/misc/collapse.js');
|
|
|
|
|
drupal_static_reset('_drupal_add_js');
|
|
|
|
|
$this->assertEqual(array(), _drupal_add_js(), 'Resetting the JavaScript correctly empties the cache.');
|
|
|
|
|
}
|
|
|
|
@ -265,15 +215,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests adding inline scripts.
|
|
|
|
|
*/
|
|
|
|
|
function testAddInline() {
|
|
|
|
|
drupal_add_library('system', 'jquery');
|
|
|
|
|
$inline = 'jQuery(function () { });';
|
|
|
|
|
$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();
|
|
|
|
|
$javascript = _drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
|
|
|
|
|
$this->assertTrue(array_key_exists('core/assets/vendor/jquery/jquery.js', $javascript), 'jQuery is added when inline scripts are added.');
|
|
|
|
|
$data = end($javascript);
|
|
|
|
|
$this->assertEqual($inline, $data['data'], 'Inline JavaScript is correctly added to the footer.');
|
|
|
|
@ -283,14 +227,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests rendering an external JavaScript file.
|
|
|
|
|
*/
|
|
|
|
|
function testRenderExternal() {
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
$external = 'http://example.com/example.js';
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'external',
|
|
|
|
|
'data' => $external,
|
|
|
|
|
);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
|
|
|
|
|
_drupal_add_js($external, 'external');
|
|
|
|
|
$javascript = drupal_get_js();
|
|
|
|
|
// Local files have a base_path() prefix, external files should not.
|
|
|
|
|
$this->assertTrue(strpos($javascript, 'src="' . $external) > 0, 'Rendering an external JavaScript file.');
|
|
|
|
@ -300,16 +239,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests drupal_get_js() with a footer scope.
|
|
|
|
|
*/
|
|
|
|
|
function testFooterHTML() {
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
$inline = 'jQuery(function () { });';
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'inline',
|
|
|
|
|
'data' => $inline,
|
|
|
|
|
'scope' => 'footer',
|
|
|
|
|
'attributes' => array('defer' => 'defer'),
|
|
|
|
|
);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
|
|
|
|
|
_drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
|
|
|
|
|
$javascript = drupal_get_js('footer');
|
|
|
|
|
$this->assertTrue(strpos($javascript, $inline) > 0, 'Rendered JavaScript footer returns the inline code.');
|
|
|
|
|
}
|
|
|
|
@ -318,10 +250,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests _drupal_add_js() sets preproccess to FALSE when cache is also FALSE.
|
|
|
|
|
*/
|
|
|
|
|
function testNoCache() {
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array('cache' => FALSE);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
$javascript = _drupal_add_js();
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
$javascript = _drupal_add_js('core/misc/collapse.js', array('cache' => FALSE));
|
|
|
|
|
$this->assertFalse($javascript['core/misc/collapse.js']['preprocess'], 'Setting cache to FALSE sets proprocess to FALSE when adding JavaScript.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -329,10 +259,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests adding a JavaScript file with a different group.
|
|
|
|
|
*/
|
|
|
|
|
function testDifferentGroup() {
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array('group' => JS_THEME);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
$javascript = _drupal_add_js();
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
$javascript = _drupal_add_js('core/misc/collapse.js', array('group' => JS_THEME));
|
|
|
|
|
$this->assertEqual($javascript['core/misc/collapse.js']['group'], JS_THEME, 'Adding a JavaScript file with a different group caches the given group.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -340,9 +268,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests adding a JavaScript file with a different weight.
|
|
|
|
|
*/
|
|
|
|
|
function testDifferentWeight() {
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array('weight' => 2);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
$javascript = _drupal_add_js();
|
|
|
|
|
$javascript = _drupal_add_js('core/misc/collapse.js', array('weight' => 2));
|
|
|
|
|
$this->assertEqual($javascript['core/misc/collapse.js']['weight'], 2, 'Adding a JavaScript file with a different weight caches the given weight.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -354,16 +280,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
function testBrowserConditionalComments() {
|
|
|
|
|
$default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
|
|
|
|
|
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array(
|
|
|
|
|
'browsers' => array('IE' => 'lte IE 8', '!IE' => FALSE),
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = array(
|
|
|
|
|
'type' => 'inline',
|
|
|
|
|
'data' => 'jQuery(function () { });',
|
|
|
|
|
'browsers' => array('IE' => FALSE),
|
|
|
|
|
);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
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]-->";
|
|
|
|
@ -377,10 +296,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests JavaScript versioning.
|
|
|
|
|
*/
|
|
|
|
|
function testVersionQueryString() {
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array('version' => 'foo');
|
|
|
|
|
$attached['#attached']['js']['core/misc/ajax.js'] = array('version' => 'bar');
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
_drupal_add_js('core/misc/collapse.js', array('version' => 'foo'));
|
|
|
|
|
_drupal_add_js('core/misc/ajax.js', array('version' => 'bar'));
|
|
|
|
|
$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');
|
|
|
|
|
}
|
|
|
|
@ -395,13 +313,11 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
// 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.
|
|
|
|
|
$attached = array();
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['core/misc/ajax.js'] = array();
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array('every_page' => TRUE);
|
|
|
|
|
$attached['#attached']['js']['core/misc/autocomplete.js'] = array();
|
|
|
|
|
$attached['#attached']['js']['core/misc/batch.js'] = array('every_page' => TRUE);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
_drupal_add_js('core/misc/ajax.js');
|
|
|
|
|
_drupal_add_js('core/misc/collapse.js', array('every_page' => TRUE));
|
|
|
|
|
_drupal_add_js('core/misc/autocomplete.js');
|
|
|
|
|
_drupal_add_js('core/misc/batch.js', array('every_page' => TRUE));
|
|
|
|
|
$javascript = drupal_get_js();
|
|
|
|
|
$expected = implode("\n", array(
|
|
|
|
|
'<script src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '"></script>',
|
|
|
|
@ -417,13 +333,11 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
$config = \Drupal::config('system.performance');
|
|
|
|
|
$config->set('js.preprocess', 1);
|
|
|
|
|
$config->save();
|
|
|
|
|
$attached = array();
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['core/misc/ajax.js'] = array();
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array('every_page' => TRUE);
|
|
|
|
|
$attached['#attached']['js']['core/misc/autocomplete.js'] = array();
|
|
|
|
|
$attached['#attached']['js']['core/misc/batch.js'] = array('every_page' => TRUE);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
_drupal_add_js('core/misc/ajax.js');
|
|
|
|
|
_drupal_add_js('core/misc/collapse.js', array('every_page' => TRUE));
|
|
|
|
|
_drupal_add_js('core/misc/autocomplete.js');
|
|
|
|
|
_drupal_add_js('core/misc/batch.js', array('every_page' => TRUE));
|
|
|
|
|
$js_items = _drupal_add_js();
|
|
|
|
|
$javascript = drupal_get_js();
|
|
|
|
|
$expected = implode("\n", array(
|
|
|
|
@ -442,11 +356,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
drupal_static_reset('_drupal_add_js');
|
|
|
|
|
|
|
|
|
|
// Add two JavaScript files to the current request and build the cache.
|
|
|
|
|
$attached = array();
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['core/misc/ajax.js'] = array();
|
|
|
|
|
$attached['#attached']['js']['core/misc/autocomplete.js'] = array();
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
_drupal_add_js('core/misc/ajax.js');
|
|
|
|
|
_drupal_add_js('core/misc/autocomplete.js');
|
|
|
|
|
|
|
|
|
|
$js_items = _drupal_add_js();
|
|
|
|
|
$scripts_html = array(
|
|
|
|
@ -465,12 +377,10 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
// Reset variables and add a file in a different scope first.
|
|
|
|
|
\Drupal::state()->delete('system.js_cache_files');
|
|
|
|
|
drupal_static_reset('_drupal_add_js');
|
|
|
|
|
$attached = array();
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'drupal');
|
|
|
|
|
$attached['#attached']['js']['some/custom/javascript_file.js'] = array('scope' => 'footer');
|
|
|
|
|
$attached['#attached']['js']['core/misc/ajax.js'] = array();
|
|
|
|
|
$attached['#attached']['js']['core/misc/autocomplete.js'] = array();
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'drupal');
|
|
|
|
|
_drupal_add_js('some/custom/javascript_file.js', array('scope' => 'footer'));
|
|
|
|
|
_drupal_add_js('core/misc/ajax.js');
|
|
|
|
|
_drupal_add_js('core/misc/autocomplete.js');
|
|
|
|
|
|
|
|
|
|
// Rebuild the cache.
|
|
|
|
|
$js_items = _drupal_add_js();
|
|
|
|
@ -493,50 +403,17 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Tests JavaScript ordering.
|
|
|
|
|
*/
|
|
|
|
|
function testRenderOrder() {
|
|
|
|
|
$shared_options = array(
|
|
|
|
|
'type' => 'inline',
|
|
|
|
|
'scope' => 'footer',
|
|
|
|
|
);
|
|
|
|
|
// Add a bunch of JavaScript in strange ordering.
|
|
|
|
|
$attached['#attached']['js'][] = $shared_options + array(
|
|
|
|
|
'data' => '(function($){alert("Weight 5 #1");})(jQuery);',
|
|
|
|
|
'weight' => 5,
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = $shared_options + array(
|
|
|
|
|
'data' => '(function($){alert("Weight 0 #1");})(jQuery);',
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = $shared_options + array(
|
|
|
|
|
'data' => '(function($){alert("Weight 0 #2");})(jQuery);',
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = $shared_options + array(
|
|
|
|
|
'data' => '(function($){alert("Weight -8 #1");})(jQuery);',
|
|
|
|
|
'weight' => -8,
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = $shared_options + array(
|
|
|
|
|
'data' => '(function($){alert("Weight -8 #2");})(jQuery);',
|
|
|
|
|
'weight' => -8,
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = $shared_options + array(
|
|
|
|
|
'data' => '(function($){alert("Weight -8 #3");})(jQuery);',
|
|
|
|
|
'weight' => -8,
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js']['http://example.com/example.js?Weight -5 #1'] = array(
|
|
|
|
|
'type' => 'external',
|
|
|
|
|
'scope' => 'footer',
|
|
|
|
|
'weight' => -5,
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = $shared_options + array(
|
|
|
|
|
'data' => '(function($){alert("Weight -8 #4");})(jQuery);',
|
|
|
|
|
'weight' => -8,
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = $shared_options + array(
|
|
|
|
|
'data' => '(function($){alert("Weight 5 #2");})(jQuery);',
|
|
|
|
|
'weight' => 5,
|
|
|
|
|
);
|
|
|
|
|
$attached['#attached']['js'][] = $shared_options + array(
|
|
|
|
|
'data' => '(function($){alert("Weight 0 #3");})(jQuery);',
|
|
|
|
|
);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
_drupal_add_js('(function($){alert("Weight 5 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5));
|
|
|
|
|
_drupal_add_js('(function($){alert("Weight 0 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
|
|
|
|
|
_drupal_add_js('(function($){alert("Weight 0 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
|
|
|
|
|
_drupal_add_js('(function($){alert("Weight -8 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
|
|
|
|
|
_drupal_add_js('(function($){alert("Weight -8 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
|
|
|
|
|
_drupal_add_js('(function($){alert("Weight -8 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
|
|
|
|
|
_drupal_add_js('http://example.com/example.js?Weight -5 #1', array('type' => 'external', 'scope' => 'footer', 'weight' => -5));
|
|
|
|
|
_drupal_add_js('(function($){alert("Weight -8 #4");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
|
|
|
|
|
_drupal_add_js('(function($){alert("Weight 5 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5));
|
|
|
|
|
_drupal_add_js('(function($){alert("Weight 0 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
|
|
|
|
|
|
|
|
|
|
// Construct the expected result from the regex.
|
|
|
|
|
$expected = array(
|
|
|
|
@ -571,13 +448,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
// JavaScript files are sorted first by group, then by the 'every_page'
|
|
|
|
|
// flag, then by weight (see drupal_sort_css_js()), so to test the effect of
|
|
|
|
|
// weight, we need the other two options to be the same.
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'jquery');
|
|
|
|
|
$attached['#attached']['js']['core/misc/collapse.js'] = array(
|
|
|
|
|
'group' => JS_LIBRARY,
|
|
|
|
|
'every_page' => TRUE,
|
|
|
|
|
'weight' => -21,
|
|
|
|
|
);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'jquery');
|
|
|
|
|
_drupal_add_js('core/misc/collapse.js', array('group' => JS_LIBRARY, 'every_page' => TRUE, 'weight' => -21));
|
|
|
|
|
$javascript = drupal_get_js();
|
|
|
|
|
$this->assertTrue(strpos($javascript, 'core/misc/collapse.js') < strpos($javascript, 'core/assets/vendor/jquery/jquery.js'), 'Rendering a JavaScript file above jQuery.');
|
|
|
|
|
}
|
|
|
|
@ -589,9 +461,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
*/
|
|
|
|
|
function testAlter() {
|
|
|
|
|
// Add both tableselect.js and simpletest.js, with a larger weight on SimpleTest.
|
|
|
|
|
$attached['#attached']['js']['core/misc/tableselect.js'] = array();
|
|
|
|
|
$attached['#attached']['js'][drupal_get_path('module', 'simpletest') . '/simpletest.js'] = array('weight' => 9999);
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
_drupal_add_js('core/misc/tableselect.js');
|
|
|
|
|
_drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', array('weight' => 9999));
|
|
|
|
|
|
|
|
|
|
// Render the JavaScript, testing if simpletest.js was altered to be before
|
|
|
|
|
// tableselect.js. See simpletest_js_alter() to see where this alteration
|
|
|
|
@ -604,17 +475,14 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
* Adds a library to the page and tests for both its JavaScript and its CSS.
|
|
|
|
|
*/
|
|
|
|
|
function testLibraryRender() {
|
|
|
|
|
$attached = array();
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'jquery.farbtastic');
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
$result = drupal_add_library('system', 'jquery.farbtastic');
|
|
|
|
|
$this->assertTrue($result !== FALSE, 'Library was added without errors.');
|
|
|
|
|
$scripts = drupal_get_js();
|
|
|
|
|
$styles = drupal_get_css();
|
|
|
|
|
$this->assertTrue(strpos($scripts, 'core/assets/vendor/farbtastic/farbtastic.js'), 'JavaScript of library was added to the page.');
|
|
|
|
|
$this->assertTrue(strpos($styles, 'core/assets/vendor/farbtastic/farbtastic.css'), 'Stylesheet of library was added to the page.');
|
|
|
|
|
|
|
|
|
|
$attached = array();
|
|
|
|
|
$attached['#attached']['library'][] = array('common_test', 'shorthand.plugin');
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('common_test', 'shorthand.plugin');
|
|
|
|
|
$path = drupal_get_path('module', 'common_test') . '/js/shorthand.js?v=0.8.3.37';
|
|
|
|
|
$scripts = drupal_get_js();
|
|
|
|
|
$this->assertTrue(strpos($scripts, $path), 'JavaScript specified in hook_library_info() using shorthand format (without any options) was added to the page.');
|
|
|
|
@ -632,8 +500,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
$this->assertEqual($library['title'], 'Farbtastic: Altered Library', 'Registered libraries were altered.');
|
|
|
|
|
|
|
|
|
|
// common_test_library_info_alter() also added a dependency on jQuery Form.
|
|
|
|
|
$attached['#attached']['library'][] = array('system', 'jquery.farbtastic');
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
drupal_add_library('system', 'jquery.farbtastic');
|
|
|
|
|
$scripts = drupal_get_js();
|
|
|
|
|
$this->assertTrue(strpos($scripts, 'core/assets/vendor/jquery-form/jquery.form.js'), 'Altered library dependencies are added to the page.');
|
|
|
|
|
}
|
|
|
|
@ -656,8 +523,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
|
|
|
|
|
$this->assertFalse($result, 'Unknown library returned FALSE.');
|
|
|
|
|
drupal_static_reset('drupal_get_library');
|
|
|
|
|
|
|
|
|
|
$attached['#attached']['library'][] = array('unknown', 'unknown');
|
|
|
|
|
drupal_render($attached);
|
|
|
|
|
$result = drupal_add_library('unknown', 'unknown');
|
|
|
|
|
$this->assertFalse($result, 'Unknown library returned FALSE.');
|
|
|
|
|
$scripts = drupal_get_js();
|
|
|
|
|
$this->assertTrue(strpos($scripts, 'unknown') === FALSE, 'Unknown library was not added to the page.');
|
|
|
|
|
}
|
|
|
|
|