summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2009-10-18 05:14:39 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2009-10-18 05:14:39 +0000
commit2484439643f86cbc2da3b4f391eb3e23e51fc94d (patch)
tree97fbddc1ba1f9c1921ea131870acd39ebec7732b
parentbba83fc6846b1530be180fb56a060fd31d4d13cc (diff)
downloadbrdo-2484439643f86cbc2da3b4f391eb3e23e51fc94d.tar.gz
brdo-2484439643f86cbc2da3b4f391eb3e23e51fc94d.tar.bz2
#595654 by sun: Fixed AJAX command 'settings' (with tests).
-rw-r--r--includes/ajax.inc6
-rw-r--r--modules/simpletest/simpletest.info1
-rw-r--r--modules/simpletest/tests/ajax.test80
-rw-r--r--modules/simpletest/tests/ajax_test.info8
-rw-r--r--modules/simpletest/tests/ajax_test.module59
5 files changed, 151 insertions, 3 deletions
diff --git a/includes/ajax.inc b/includes/ajax.inc
index 0cd6f63e6..2701cb0ab 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 5e49bfb96..1eb85d98a 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 000000000..3a55dbb97
--- /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 000000000..0d9c32a46
--- /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 000000000..27bf3bf16
--- /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);
+}
+