Commit 24844396 authored by webchick's avatar webchick

#595654 by sun: Fixed AJAX command 'settings' (with tests).

parent bba83fc6
......@@ -142,7 +142,7 @@ function ajax_render($commands = array(), $header = TRUE) {
// them the first command.
$scripts = drupal_add_js(NULL, NULL);
if (!empty($scripts['settings'])) {
array_unshift($commands, ajax_command_settings($scripts['settings']['data']));
array_unshift($commands, ajax_command_settings(call_user_func_array('array_merge_recursive', $scripts['settings']['data'])));
}
// Allow modules to alter any AJAX response.
......@@ -156,7 +156,7 @@ function ajax_render($commands = array(), $header = TRUE) {
// http://malsup.com/jquery/form/#code-samples
print '<textarea>' . drupal_json_encode($commands) . '</textarea>';
}
else if ($header) {
elseif ($header) {
drupal_json_output($commands);
}
else {
......@@ -773,7 +773,7 @@ function ajax_command_css($selector, $argument) {
function ajax_command_settings($argument) {
return array(
'command' => 'settings',
'argument' => $argument,
'settings' => $argument,
);
}
......
......@@ -12,6 +12,7 @@ files[] = drupal_web_test_case.php
; Tests in tests directory.
files[] = tests/actions.test
files[] = tests/ajax.test
files[] = tests/batch.test
files[] = tests/bootstrap.test
files[] = tests/browser.test
......
<?php
// $Id$
class AJAXTestCase extends DrupalWebTestCase {
function setUp() {
parent::setUp('ajax_test');
}
function drupalGetAJAX($path, $query = array()) {
$this->drupalGet($path, array('query' => $query));
return json_decode($this->content, TRUE);
}
}
/**
* Tests primary AJAX framework functions.
*/
class AJAXFrameworkTestCase extends AJAXTestCase {
function getInfo() {
return array(
'name' => 'AJAX framework',
'description' => 'Performs tests on AJAX framework functions.',
'group' => 'AJAX',
);
}
/**
* Test proper passing of JavaScript settings via ajax_render().
*/
function testAJAXRender() {
$result = $this->drupalGetAJAX('ajax-test/render');
// Verify that JavaScript settings are contained (always first).
$this->assertIdentical($result[0]['command'], 'settings', t('drupal_add_js() settings are contained first.'));
// Verify that basePath is contained in JavaScript settings.
$this->assertEqual($result[0]['settings']['basePath'], base_path(), t('Base path is contained in JavaScript settings.'));
}
/**
* Test behavior of ajax_render_error().
*/
function testAJAXRenderError() {
$result = $this->drupalGetAJAX('ajax-test/render-error');
// Verify default error message.
$this->assertEqual($result[0]['command'], 'alert', t('ajax_render_error() invokes alert command.'));
$this->assertEqual($result[0]['text'], t('An error occurred while handling the request: The server received invalid input.'), t('Default error message is output.'));
// Verify custom error message.
$edit = array(
'message' => 'Custom error message.',
);
$result = $this->drupalGetAJAX('ajax-test/render-error', $edit);
$this->assertEqual($result[0]['text'], $edit['message'], t('Custom error message is output.'));
}
}
/**
* Tests AJAX framework commands.
*/
class AJAXCommandsTestCase extends AJAXTestCase {
function getInfo() {
return array(
'name' => 'AJAX commands',
'description' => 'Performs tests on AJAX framework commands.',
'group' => 'AJAX',
);
}
/**
* Test ajax_command_settings().
*/
function testAJAXRender() {
$commands = array();
$commands[] = ajax_command_settings(array('foo' => 42));
$result = $this->drupalGetAJAX('ajax-test/render', array('commands' => $commands));
// Verify that JavaScript settings are contained (always first).
$this->assertIdentical($result[0]['command'], 'settings', t('drupal_add_js() settings are contained first.'));
// Verify that the custom setting is contained.
$this->assertEqual($result[1]['settings']['foo'], 42, t('Custom setting is output.'));
}
}
; $Id$
name = AJAX Test
description = Support module for AJAX framework tests.
package = Testing
version = VERSION
core = 7.x
files[] = ajax_test.module
hidden = TRUE
<?php
// $Id$
/**
* @file
* Helper module for AJAX framework tests.
*/
/**
* Implement hook_menu().
*/
function ajax_test_menu() {
$items['ajax-test/render'] = array(
'title' => 'ajax_render',
'page callback' => 'ajax_test_render',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['ajax-test/render-error'] = array(
'title' => 'ajax_render_error',
'page callback' => 'ajax_test_render_error',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu callback; Copies $_GET['commands'] into $commands and ajax_render()s that.
*
* Additionally ensures that ajax_render() incorporates JavaScript settings
* by invoking drupal_add_js() with a dummy setting.
*/
function ajax_test_render() {
// Prepare AJAX commands.
$commands = array();
if (!empty($_GET['commands'])) {
$commands = $_GET['commands'];
}
// Add a dummy JS setting.
drupal_add_js(array('ajax' => 'test'), 'setting');
// Output AJAX commands and end the request.
ajax_render($commands);
}
/**
* Menu callback; Invokes ajax_render_error().
*
* Optionally passes $_GET['message'] to ajax_render_error().
*/
function ajax_test_render_error() {
$message = '';
if (!empty($_GET['message'])) {
$message = $_GET['message'];
}
ajax_render_error($message);
}
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