diff --git a/includes/ajax.inc b/includes/ajax.inc index f059209..b076e99 100644 --- a/includes/ajax.inc +++ b/includes/ajax.inc @@ -295,8 +295,17 @@ function ajax_render($commands = array()) { // Now add a command to merge changes and additions to Drupal.settings. $scripts = drupal_add_js(); if (!empty($scripts['settings'])) { - $settings = $scripts['settings']; - array_unshift($commands, ajax_command_settings(drupal_array_merge_deep_array($settings['data']), TRUE)); + $settings = drupal_array_merge_deep_array($scripts['settings']['data']); + // During Ajax requests basic path-specific settings are excluded from + // new Drupal.settings values. The original page where this request comes + // from already has the right values for the keys below. An Ajax request + // would update them with values for the Ajax request and incorrectly + // override the page's values. + // @see drupal_add_js + foreach (array('basePath', 'currentPath', 'pathPrefix') as $item) { + unset($settings[$item]); + } + array_unshift($commands, ajax_command_settings($settings, TRUE)); } // Allow modules to alter any Ajax response. diff --git a/includes/common.inc b/includes/common.inc index c97704c..cbae219 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -4271,6 +4271,7 @@ function drupal_add_js($data = NULL, $options = NULL) { 'data' => array( array('basePath' => base_path()), array('pathPrefix' => empty($prefix) ? '' : $prefix), + array('currentPath' => current_path()), ), 'type' => 'setting', 'scope' => 'header', diff --git a/modules/simpletest/tests/ajax.test b/modules/simpletest/tests/ajax.test index afe0230..3c2a507 100644 --- a/modules/simpletest/tests/ajax.test +++ b/modules/simpletest/tests/ajax.test @@ -197,6 +197,18 @@ class AJAXFrameworkTestCase extends AJAXTestCase { } /** + * Tests that Drupal.settings.currentPath is not updated on AJAX requests. + */ + function testCurrentPathChange() { + $commands = $this->drupalPostAJAX('ajax_forms_test_lazy_load_form', array('add_files' => FALSE), array('op' => t('Submit'))); + foreach ($commands as $command) { + if ($command['command'] == 'settings') { + $this->assertFalse(isset($command['settings']['currentPath']), 'Value of drupalSettings.currentPath is not updated after an AJAX request.'); + } + } + } + + /** * Tests that overridden CSS files are not added during lazy load. */ function testLazyLoadOverriddenCSS() { diff --git a/modules/simpletest/tests/ajax_forms_test.module b/modules/simpletest/tests/ajax_forms_test.module index de2fa0b..f319c2b 100644 --- a/modules/simpletest/tests/ajax_forms_test.module +++ b/modules/simpletest/tests/ajax_forms_test.module @@ -482,6 +482,13 @@ function ajax_forms_test_validation_form_callback($form, $form_state) { * Form builder: Builds a form that triggers a simple AJAX callback. */ function ajax_forms_test_lazy_load_form($form, &$form_state) { + // We attach a JavaScript setting, so that one of the generated AJAX commands + // will be a settings command. We can then check the settings command to + // ensure that the 'currentPath' setting is not part of the Ajax response. + $form['#attached']['js'][] = array( + 'type' => 'setting', + 'data' => array('test' => 'currentPathUpdate'), + ); $form['add_files'] = array( '#type' => 'checkbox', '#default_value' => FALSE, diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 92aefe4..1292de4 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -1377,8 +1377,8 @@ class JavaScriptTestCase extends DrupalWebTestCase { } function setUp() { - // Enable Locale and SimpleTest in the test environment. - parent::setUp('locale', 'simpletest', 'common_test'); + // Enable Locale, Path and SimpleTest in the test environment. + parent::setUp('locale', 'simpletest', 'common_test', 'path'); // Disable preprocessing $this->preprocess_js = variable_get('preprocess_js', 0); @@ -1419,9 +1419,15 @@ class JavaScriptTestCase extends DrupalWebTestCase { * Test adding settings. */ function testAddSetting() { + // Add a file in order to test default settings. + $javascript = drupal_add_js('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'][2]['dries'], 'JavaScript setting is set correctly.'); - $this->assertEqual('rocks', $javascript['settings']['data'][2]['drupal'], '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.'); } /** @@ -1451,6 +1457,7 @@ class JavaScriptTestCase extends DrupalWebTestCase { $javascript = drupal_get_js('header'); $this->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.'); $this->assertTrue(strpos($javascript, 'misc/jquery.js') > 0, 'Rendered JavaScript header includes jQuery.'); + $this->assertTrue(strpos($javascript, 'currentPath') > 0, 'Rendered JavaScript header returns currentPath setting.'); $this->assertTrue(strpos($javascript, 'pathPrefix') > 0, 'Rendered JavaScript header returns pathPrefix setting.'); // Test whether drupal_add_js can be used to override a previous setting. @@ -1466,6 +1473,13 @@ class JavaScriptTestCase extends DrupalWebTestCase { // existing key in an associative array. $associative_array_override = strpos($javascript, 'commonTestNewValue') > 0 && strpos($javascript, 'commonTestOldValue') === FALSE; $this->assertTrue($associative_array_override, 'drupal_add_js() correctly overrides settings within an associative array.'); + // Check in a rendered page. + $this->drupalGet('common-test/query-string'); + $this->assertPattern('@