diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-10-18 05:14:39 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-10-18 05:14:39 +0000 |
commit | 2484439643f86cbc2da3b4f391eb3e23e51fc94d (patch) | |
tree | 97fbddc1ba1f9c1921ea131870acd39ebec7732b /modules/simpletest | |
parent | bba83fc6846b1530be180fb56a060fd31d4d13cc (diff) | |
download | brdo-2484439643f86cbc2da3b4f391eb3e23e51fc94d.tar.gz brdo-2484439643f86cbc2da3b4f391eb3e23e51fc94d.tar.bz2 |
#595654 by sun: Fixed AJAX command 'settings' (with tests).
Diffstat (limited to 'modules/simpletest')
-rw-r--r-- | modules/simpletest/simpletest.info | 1 | ||||
-rw-r--r-- | modules/simpletest/tests/ajax.test | 80 | ||||
-rw-r--r-- | modules/simpletest/tests/ajax_test.info | 8 | ||||
-rw-r--r-- | modules/simpletest/tests/ajax_test.module | 59 |
4 files changed, 148 insertions, 0 deletions
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); +} + |