Commit 7587186f authored by Dries's avatar Dries

- Patch #1691394 by pwolanin, tim.plunkett: added current Drupal path to drupal JS settings.

parent 3e7b3770
......@@ -3817,6 +3817,7 @@ function drupal_add_js($data = NULL, $options = NULL) {
array('basePath' => base_path()),
array('scriptPath' => $scriptPath),
array('pathPrefix' => $pathPrefix),
array('currentPath' => current_path()),
),
'type' => 'setting',
'scope' => 'header',
......
......@@ -28,7 +28,7 @@ public static function getInfo() {
function setUp() {
// Enable Language and SimpleTest in the test environment.
parent::setUp('language', 'simpletest', 'common_test');
parent::setUp('language', 'simpletest', 'common_test', 'path');
// Disable preprocessing
$config = config('system.performance');
......@@ -74,9 +74,15 @@ function testAddFile() {
* Test adding settings.
*/
function testAddSetting() {
// Add a file in order to test default settings.
$javascript = drupal_add_js('core/misc/collapse.js');
$last_settings = end($javascript['settings']['data']);
$this->assertTrue($last_settings['currentPath'], 'The current path JavaScript setting is set correctly.');
$javascript = drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting');
$this->assertEqual(280342800, $javascript['settings']['data'][3]['dries'], t('JavaScript setting is set correctly.'));
$this->assertEqual('rocks', $javascript['settings']['data'][3]['drupal'], t('The other JavaScript setting is set correctly.'));
$last_settings = end($javascript['settings']['data']);
$this->assertEqual(280342800, $last_settings['dries'], 'JavaScript setting is set correctly.');
$this->assertEqual('rocks', $last_settings['drupal'], 'The other JavaScript setting is set correctly.');
}
/**
......@@ -138,10 +144,11 @@ function testHeaderSetting() {
drupal_add_js(array('commonTestArray' => array('key' => 'commonTestNewValue')), 'setting');
$javascript = drupal_get_js('header');
$this->assertTrue(strpos($javascript, 'basePath') > 0, t('Rendered JavaScript header returns basePath setting.'));
$this->assertTrue(strpos($javascript, 'scriptPath') > 0, t('Rendered JavaScript header returns scriptPath setting.'));
$this->assertTrue(strpos($javascript, 'pathPrefix') > 0, t('Rendered JavaScript header returns pathPrefix setting.'));
$this->assertTrue(strpos($javascript, 'core/misc/jquery.js') > 0, t('Rendered JavaScript header includes jQuery.'));
$this->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.');
$this->assertTrue(strpos($javascript, 'scriptPath') > 0, 'Rendered JavaScript header returns scriptPath setting.');
$this->assertTrue(strpos($javascript, 'pathPrefix') > 0, 'Rendered JavaScript header returns pathPrefix setting.');
$this->assertTrue(strpos($javascript, 'currentPath') > 0, 'Rendered JavaScript header returns currentPath setting.');
$this->assertTrue(strpos($javascript, 'core/misc/jquery.js') > 0, 'Rendered JavaScript header includes jQuery.');
// Test whether drupal_add_js can be used to override a previous setting.
$this->assertTrue(strpos($javascript, 'commonTestShouldAppear') > 0, t('Rendered JavaScript header returns custom setting.'));
......@@ -156,6 +163,13 @@ function testHeaderSetting() {
// existing key in an associative array.
$associative_array_override = strpos($javascript, 'commonTestNewValue') > 0 && strpos($javascript, 'commonTestOldValue') === FALSE;
$this->assertTrue($associative_array_override, t('drupal_add_js() correctly overrides settings within an associative array.'));
// Check in a rendered page.
$this->drupalGet('common-test/query-string');
$this->assertPattern('@<script type="text/javascript">.+Drupal\.settings.+"currentPath":"common-test\\\/query-string"@s', 'currentPath is in the JS settings');
$path = array('source' => 'common-test/query-string', 'alias' => 'common-test/currentpath-check');
path_save($path);
$this->drupalGet('common-test/currentpath-check');
$this->assertPattern('@<script type="text/javascript">.+Drupal\.settings.+"currentPath":"common-test\\\/query-string"@s', 'currentPath is in the JS settings for an aliased path');
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment