Commit 7ed96147 authored by catch's avatar catch

Issue #1691394 by pwolanin, nod_, tim.plunkett: Fixed Drupal settings gets broken by AJAX requests.

parent 75644c15
...@@ -133,6 +133,15 @@ protected function ajaxRender(Request $request) { ...@@ -133,6 +133,15 @@ protected function ajaxRender(Request $request) {
$scripts = drupal_add_js(); $scripts = drupal_add_js();
if (!empty($scripts['settings'])) { if (!empty($scripts['settings'])) {
$settings = drupal_merge_js_settings($scripts['settings']['data']); $settings = drupal_merge_js_settings($scripts['settings']['data']);
// During Ajax requests basic path-specific settings are excluded from
// new drupalSettings 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', 'scriptPath', 'pathPrefix') as $item) {
unset($settings[$item]);
}
$this->addCommand(new SettingsCommand($settings, TRUE), TRUE); $this->addCommand(new SettingsCommand($settings, TRUE), TRUE);
} }
......
...@@ -29,7 +29,7 @@ public static function getInfo() { ...@@ -29,7 +29,7 @@ public static function getInfo() {
/** /**
* Ensures ajax_render() returns JavaScript settings from the page request. * Ensures ajax_render() returns JavaScript settings from the page request.
*/ */
function testAJAXRender() { public function testAJAXRender() {
// Verify that settings command is generated when JavaScript settings are // Verify that settings command is generated when JavaScript settings are
// set via drupal_add_js(). // set via drupal_add_js().
$commands = $this->drupalGetAJAX('ajax-test/render'); $commands = $this->drupalGetAJAX('ajax-test/render');
...@@ -40,7 +40,7 @@ function testAJAXRender() { ...@@ -40,7 +40,7 @@ function testAJAXRender() {
/** /**
* Tests AjaxResponse::prepare() AJAX commands ordering. * Tests AjaxResponse::prepare() AJAX commands ordering.
*/ */
function testOrder() { public function testOrder() {
$path = drupal_get_path('module', 'system'); $path = drupal_get_path('module', 'system');
$expected_commands = array(); $expected_commands = array();
...@@ -82,7 +82,7 @@ function testOrder() { ...@@ -82,7 +82,7 @@ function testOrder() {
/** /**
* Tests behavior of ajax_render_error(). * Tests behavior of ajax_render_error().
*/ */
function testAJAXRenderError() { public function testAJAXRenderError() {
// Verify custom error message. // Verify custom error message.
$edit = array( $edit = array(
'message' => 'Custom error message.', 'message' => 'Custom error message.',
...@@ -95,7 +95,7 @@ function testAJAXRenderError() { ...@@ -95,7 +95,7 @@ function testAJAXRenderError() {
/** /**
* Tests that new JavaScript and CSS files are lazy-loaded on an AJAX request. * Tests that new JavaScript and CSS files are lazy-loaded on an AJAX request.
*/ */
function testLazyLoad() { public function testLazyLoad() {
$expected = array( $expected = array(
'setting_name' => 'ajax_forms_test_lazy_load_form_submit', 'setting_name' => 'ajax_forms_test_lazy_load_form_submit',
'setting_value' => 'executed', 'setting_value' => 'executed',
...@@ -182,10 +182,22 @@ function testLazyLoad() { ...@@ -182,10 +182,22 @@ function testLazyLoad() {
$this->assertCommand(array_slice($commands, 2, 1), array('data' => $expected_js_html), format_string('Page now has the %js file.', array('%js' => $expected['js']))); $this->assertCommand(array_slice($commands, 2, 1), array('data' => $expected_js_html), format_string('Page now has the %js file.', array('%js' => $expected['js'])));
} }
/**
* Tests that drupalSettings.currentPath is not updated on AJAX requests.
*/
public 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. * Tests that overridden CSS files are not added during lazy load.
*/ */
function testLazyLoadOverriddenCSS() { public function testLazyLoadOverriddenCSS() {
// The test theme overrides system.module.css without an implementation, // The test theme overrides system.module.css without an implementation,
// thereby removing it. // thereby removing it.
theme_enable(array('test_theme')); theme_enable(array('test_theme'));
......
...@@ -534,6 +534,13 @@ function ajax_forms_test_validation_number_form_callback($form, $form_state) { ...@@ -534,6 +534,13 @@ function ajax_forms_test_validation_number_form_callback($form, $form_state) {
* Form builder: Builds a form that triggers a simple AJAX callback. * Form builder: Builds a form that triggers a simple AJAX callback.
*/ */
function ajax_forms_test_lazy_load_form($form, &$form_state) { 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( $form['add_files'] = array(
'#type' => 'checkbox', '#type' => 'checkbox',
'#default_value' => FALSE, '#default_value' => FALSE,
......
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