diff --git a/includes/ajax.inc b/includes/ajax.inc index 0cd6f63e698f7fa8debb4e6cd67f648e00e81fa2..2701cb0ab1e887aeb3640264e3eb3b8b214a122a 100644 --- a/includes/ajax.inc +++ b/includes/ajax.inc @@ -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, ); } diff --git a/modules/simpletest/simpletest.info b/modules/simpletest/simpletest.info index 5e49bfb966f33d331e4e1c18c0eb1fda90ba15ab..1eb85d98a2b7e68f79f366c2ed75f1cbfc46c825 100644 --- a/modules/simpletest/simpletest.info +++ b/modules/simpletest/simpletest.info @@ -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 diff --git a/modules/simpletest/tests/ajax.test b/modules/simpletest/tests/ajax.test new file mode 100644 index 0000000000000000000000000000000000000000..3a55dbb971451ab3ccc6ea5ded00cc4ab49787ff --- /dev/null +++ b/modules/simpletest/tests/ajax.test @@ -0,0 +1,80 @@ +<?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.')); + } +} + diff --git a/modules/simpletest/tests/ajax_test.info b/modules/simpletest/tests/ajax_test.info new file mode 100644 index 0000000000000000000000000000000000000000..0d9c32a465907c6aab4a1bbdabdd464fe70a6f3f --- /dev/null +++ b/modules/simpletest/tests/ajax_test.info @@ -0,0 +1,8 @@ +; $Id$ +name = AJAX Test +description = Support module for AJAX framework tests. +package = Testing +version = VERSION +core = 7.x +files[] = ajax_test.module +hidden = TRUE diff --git a/modules/simpletest/tests/ajax_test.module b/modules/simpletest/tests/ajax_test.module new file mode 100644 index 0000000000000000000000000000000000000000..27bf3bf169183d62607b3a7ededfcecd8063b678 --- /dev/null +++ b/modules/simpletest/tests/ajax_test.module @@ -0,0 +1,59 @@ +<?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); +} +