- <?php
- * @file
- * Ajax Tests.
- */
-
- * Ajax Test.
- */
- class AJAXTestCase extends BackdropWebTestCase {
- function setUp() {
- $modules = func_get_args();
- if (isset($modules[0]) && is_array($modules[0])) {
- $modules = $modules[0];
- }
- parent::setUp(array_unique(array_merge(array('ajax_test', 'ajax_forms_test'), $modules)));
- }
-
-
- * Assert that a command with the required properties exists within the array
- * of Ajax commands returned by the server.
- *
- * The Ajax framework, via the ajax_deliver() and ajax_render() functions,
- * returns an array of commands. This array sometimes includes commands
- * automatically provided by the framework in addition to commands returned by
- * a particular page callback. During testing, we're usually interested that
- * a particular command is present, and don't care whether other commands
- * precede or follow the one we're interested in. Additionally, the command
- * we're interested in may include additional data that we're not interested
- * in. Therefore, this function asserts that one of the commands in $haystack
- * contains all of the keys and values in $needle. Furthermore, if $needle
- * contains a 'settings' key with an array value, we assert that all keys and
- * values within that array are present in the command we're checking, and do
- * not consider it a failure if the actual command contains additional
- * settings that aren't part of $needle.
- *
- * @param $haystack
- * An array of Ajax commands returned by the server.
- * @param $needle
- * Array of info we're expecting in one of those commands.
- * @param $message
- * An assertion message.
- */
- protected function assertCommand($haystack, $needle, $message) {
- $found = FALSE;
- foreach ($haystack as $command) {
-
-
- if (isset($command['settings']) && is_array($command['settings']) && isset($needle['settings']) && is_array($needle['settings'])) {
- $command['settings'] = array_intersect_key($command['settings'], $needle['settings']);
- }
-
-
-
-
- if (array_intersect_key($command, $needle) == $needle) {
- $found = TRUE;
- break;
- }
- }
- $this->assertTrue($found, $message);
- }
- }
-
- * Tests primary Ajax framework functions.
- */
- class AJAXFrameworkTestCase extends AJAXTestCase {
- protected $profile = 'testing';
-
-
- * Test that ajax_render() returns JavaScript settings generated during the page request.
- *
- * @todo Add tests to ensure that ajax_render() returns commands for new CSS
- * and JavaScript files to be loaded by the page. See
- * http://drupal.org/node/561858.
- */
- function testAJAXRender() {
- $commands = $this->backdropGetAJAX('ajax-test/render');
-
-
-
- $expected = array(
- 'command' => 'settings',
- 'settings' => array('basePath' => base_path(), 'ajax' => 'test'),
- );
- $this->assertCommand($commands, $expected, t('ajax_render() loads settings added with backdrop_add_js().'));
- }
-
-
- * Test behavior of ajax_render_error().
- */
- function testAJAXRenderError() {
-
- $commands = $this->backdropGetAJAX('ajax-test/render-error');
- $expected = array(
- 'command' => 'alert',
- 'text' => t('An error occurred while handling the request: The server received invalid input.'),
- );
- $this->assertCommand($commands, $expected, t('ajax_render_error() invokes alert command.'));
-
-
- $edit = array(
- 'message' => 'Custom error message.',
- );
- $commands = $this->backdropGetAJAX('ajax-test/render-error', array('query' => $edit));
- $expected = array(
- 'command' => 'alert',
- 'text' => $edit['message'],
- );
- $this->assertCommand($commands, $expected, t('Custom error message is output.'));
- }
-
-
- * Test that new JavaScript and CSS files added during an AJAX request are returned.
- */
- function testLazyLoad() {
- $expected = array(
- 'setting_name' => 'ajax_forms_test_lazy_load_form_submit',
- 'setting_value' => 'executed',
- 'css' => backdrop_get_path('module', 'system') . '/css/system.admin.css',
- 'js' => backdrop_get_path('module', 'system') . '/js/system.admin.js',
- );
- $expected_css_html = backdrop_get_css(array($expected['css'] => backdrop_css_defaults($expected['css'])), TRUE);
- $expected_js_html = backdrop_get_js('header', array($expected['js'] => backdrop_js_defaults($expected['js'])), TRUE);
-
-
- $this->backdropGet('ajax_forms_test_lazy_load_form');
- $original_settings = $this->backdropGetSettings();
- $original_css = $original_settings['ajaxPageState']['css'];
- $original_js = $original_settings['ajaxPageState']['js'];
-
-
-
- $this->assertTrue(!isset($original_settings[$expected['setting_name']]), t('Page originally lacks the %setting, as expected.', array('%setting' => $expected['setting_name'])));
- $this->assertTrue(!isset($original_settings[$expected['css']]), t('Page originally lacks the %css file, as expected.', array('%css' => $expected['css'])));
- $this->assertTrue(!isset($original_settings[$expected['js']]), t('Page originally lacks the %js file, as expected.', array('%js' => $expected['js'])));
-
-
- $commands = $this->backdropPostAJAX(NULL, array('add_files' => FALSE), array('op' => t('Submit')));
- $new_settings = $this->backdropGetSettings();
-
-
- $this->assertTrue(!isset($new_settings['setting_name']), t('Page still lacks the %setting, as expected.', array('%setting' => $expected['setting_name'])));
-
-
- $found_settings_command = FALSE;
- $found_markup_command = FALSE;
- foreach ($commands as $command) {
- if ($command['command'] == 'settings' && (array_key_exists('css', $command['settings']['ajaxPageState']) || array_key_exists('js', $command['settings']['ajaxPageState']))) {
- $found_settings_command = TRUE;
- }
- if (isset($command['data']) && ($command['data'] == $expected_js_html || $command['data'] == $expected_css_html)) {
- $found_markup_command = TRUE;
- }
- }
- $this->assertFalse($found_settings_command, t('Page state still lacks the %css and %js files, as expected.', array('%css' => $expected['css'], '%js' => $expected['js'])));
- $this->assertFalse($found_markup_command, t('Page still lacks the %css and %js files, as expected.', array('%css' => $expected['css'], '%js' => $expected['js'])));
-
-
- $commands = $this->backdropPostAJAX(NULL, array('add_files' => TRUE), array('op' => t('Submit')));
- $new_settings = $this->backdropGetSettings();
- $new_css = $new_settings['ajaxPageState']['css'];
- $new_js = $new_settings['ajaxPageState']['js'];
-
-
- $this->assertIdentical($new_settings[$expected['setting_name']], $expected['setting_value'], t('Page now has the %setting.', array('%setting' => $expected['setting_name'])));
-
-
-
- $this->assertEqual($new_css, $original_css + array($expected['css'] => 1), t('Page state now has the %css file.', array('%css' => $expected['css'])));
- $this->assertCommand($commands, array('data' => $expected_css_html), t('Page now has the %css file.', array('%css' => $expected['css'])));
-
-
-
-
-
-
- $this->assertEqual($new_js, $original_js + array($expected['js'] => 1), t('Page state now has the %js file.', array('%js' => $expected['js'])));
- $this->assertCommand($commands, array('data' => $expected_js_html), t('Page now has the %js file.', array('%js' => $expected['js'])));
- }
-
-
- * Tests that overridden CSS files are not added during lazy load.
- */
- function testLazyLoadOverriddenCSS() {
-
-
- theme_enable(array('test_theme'));
- config_set('system.core', 'theme_default', 'test_theme');
-
-
-
- $this->backdropPostAJAX('ajax_forms_test_lazy_load_form', array('add_files' => TRUE), array('op' => t('Submit')));
-
-
-
-
-
-
- $this->assertNoText('system.css?', 'Ajax lazy loading does not add overridden CSS files.');
- }
- }
-
- * Tests Ajax framework commands.
- */
- class AJAXCommandsTestCase extends AJAXTestCase {
-
- * Test the various Ajax Commands.
- */
- function testAJAXCommands() {
- $form_path = 'ajax_forms_test_ajax_commands_form';
- $web_user = $this->backdropCreateUser(array('access content'));
- $this->backdropLogin($web_user);
-
- $edit = array();
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX 'After': Click to put something after the div")));
- $expected = array(
- 'command' => 'insert',
- 'method' => 'after',
- 'data' => 'This will be placed after',
- );
- $this->assertCommand($commands, $expected, "'after' AJAX command issued with correct data");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX 'Alert': Click to alert")));
- $expected = array(
- 'command' => 'alert',
- 'text' => 'Alert',
- );
- $this->assertCommand($commands, $expected, "'alert' AJAX Command issued with correct text");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX 'Append': Click to append something")));
- $expected = array(
- 'command' => 'insert',
- 'method' => 'append',
- 'data' => 'Appended text',
- );
- $this->assertCommand($commands, $expected, "'append' AJAX command issued with correct data");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX 'before': Click to put something before the div")));
- $expected = array(
- 'command' => 'insert',
- 'method' => 'before',
- 'data' => 'Before text',
- );
- $this->assertCommand($commands, $expected, "'before' AJAX command issued with correct data");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX changed: Click to mark div changed.")));
- $expected = array(
- 'command' => 'changed',
- 'selector' => '#changed_div',
- );
- $this->assertCommand($commands, $expected, "'changed' AJAX command issued with correct selector");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX changed: Click to mark div changed with asterisk.")));
- $expected = array(
- 'command' => 'changed',
- 'selector' => '#changed_div',
- 'asterisk' => '#changed_div_mark_this',
- );
- $this->assertCommand($commands, $expected, "'changed' AJAX command (with asterisk) issued with correct selector");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("Set the the '#box' div to be blue.")));
- $expected = array(
- 'command' => 'css',
- 'selector' => '#css_div',
- 'argument' => array('background-color' => 'blue'),
- );
- $this->assertCommand($commands, $expected, "'css' AJAX command issued with correct selector");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX data command: Issue command.")));
- $expected = array(
- 'command' => 'data',
- 'name' => 'test_key',
- 'value' => 'test_value',
- );
- $this->assertCommand($commands, $expected, "'data' AJAX command issued with correct key and value");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX invoke command: Invoke addClass() method.")));
- $expected = array(
- 'command' => 'invoke',
- 'method' => 'addClass',
- 'args' => array('error'),
- );
- $this->assertCommand($commands, $expected, "'invoke' AJAX command issued with correct method and argument");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX html: Replace the HTML in a selector.")));
- $expected = array(
- 'command' => 'insert',
- 'method' => 'html',
- 'data' => 'replacement text',
- );
- $this->assertCommand($commands, $expected, "'html' AJAX command issued with correct data");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX insert: Let client insert based on #ajax['method'].")));
- $expected = array(
- 'command' => 'insert',
- 'data' => 'insert replacement text',
- );
- $this->assertCommand($commands, $expected, "'insert' AJAX command issued with correct data");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX 'prepend': Click to prepend something")));
- $expected = array(
- 'command' => 'insert',
- 'method' => 'prepend',
- 'data' => 'prepended text',
- );
- $this->assertCommand($commands, $expected, "'prepend' AJAX command issued with correct data");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX 'remove': Click to remove text")));
- $expected = array(
- 'command' => 'remove',
- 'selector' => '#remove_text',
- );
- $this->assertCommand($commands, $expected, "'remove' AJAX command issued with correct command and selector");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX 'restripe' command")));
- $expected = array(
- 'command' => 'restripe',
- 'selector' => '#restripe_table',
- );
- $this->assertCommand($commands, $expected, "'restripe' AJAX command issued with correct selector");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX 'settings' command")));
- $expected = array(
- 'command' => 'settings',
- 'settings' => array('ajax_forms_test' => array('foo' => 42)),
- );
- $this->assertCommand($commands, $expected, "'settings' AJAX command issued with correct data");
-
-
- $commands = $this->backdropPostAJAX($form_path, $edit, array('op' => t("AJAX 'addCss' command")));
- $expected = array(
- 'command' => 'addCss',
- 'data' => 'my/file.css',
- );
- $this->assertCommand($commands, $expected, "'addCss' AJAX command issued with correct data");
- }
- }
-
- * Test that $form_state['values'] is properly delivered to $ajax['callback'].
- */
- class AJAXFormValuesTestCase extends AJAXTestCase {
-
-
- * @var User
- */
- protected $web_user;
-
- function setUp() {
- parent::setUp();
-
- $this->web_user = $this->backdropCreateUser(array('access content'));
- $this->backdropLogin($this->web_user);
- }
-
-
- * Create a simple form, then POST to system/ajax to change to it.
- */
- function testSimpleAJAXFormValue() {
-
- foreach (array('red', 'green', 'blue') as $item) {
- $edit = array(
- 'select' => $item,
- );
- $commands = $this->backdropPostAJAX('ajax_forms_test_get_form', $edit, 'select');
- $expected = array(
- 'command' => 'data',
- 'value' => $item,
- );
- $this->assertCommand($commands, $expected, "verification of AJAX form values from a selectbox issued with a correct value");
- }
-
-
- foreach (array(FALSE, TRUE) as $item) {
- $edit = array(
- 'checkbox' => $item,
- );
- $commands = $this->backdropPostAJAX('ajax_forms_test_get_form', $edit, 'checkbox');
- $expected = array(
- 'command' => 'data',
- 'value' => (int) $item,
- );
- $this->assertCommand($commands, $expected, "verification of AJAX form values from a checkbox issued with a correct value");
- }
- }
- }
-
- * Tests that Ajax-enabled forms work when multiple instances of the same form are on a page.
- */
- class AJAXMultiFormTestCase extends AJAXTestCase {
-
-
- * @var User
- */
- protected $web_user;
-
- function setUp() {
- parent::setUp(array('form_test'));
-
-
- $field_name = 'field_ajax_test';
- $field = array(
- 'field_name' => $field_name,
- 'type' => 'text',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- );
- field_create_field($field);
- $instance = array(
- 'field_name' => $field_name,
- 'entity_type' => 'node',
- 'bundle' => 'page',
- );
- field_create_instance($instance);
-
-
- $this->web_user = $this->backdropCreateUser(array('create page content'));
- $this->backdropLogin($this->web_user);
- }
-
-
- * Test that a page with the 'page_node_form' included twice works correctly.
- */
- function testMultiForm() {
-
-
-
- $field_name = 'field_ajax_test';
- $field_xpaths = array(
- 'page-node-form' => '//form[@id="page-node-form"]//div[contains(@class, "field-name-field-ajax-test")]',
- 'page-node-form--2' => '//form[@id="page-node-form--2"]//div[contains(@class, "field-name-field-ajax-test")]',
- );
- $button_name = $field_name . '_add_more';
- $button_value = t('Add another');
- $button_xpath_suffix = '//input[@name="' . $button_name . '"]';
- $field_items_xpath_suffix = '//input[@type="text"]';
-
-
-
-
- $this->backdropGet('form-test/two-instances-of-same-form');
- foreach ($field_xpaths as $form_html_id => $field_xpath) {
- $this->assert(count($this->xpath($field_xpath . $field_items_xpath_suffix)) == 1, t('Found the correct number of field items on the initial page.'));
- $this->assertFieldByXPath($field_xpath . $button_xpath_suffix, NULL, t('Found the "add more" button on the initial page.'));
- }
- $this->assertNoDuplicateIds(t('Initial page contains unique IDs'), 'Other');
-
-
-
- foreach ($field_xpaths as $form_html_id => $field_xpath) {
- for ($i = 0; $i < 2; $i++) {
- $this->backdropPostAJAX(NULL, array(), array($button_name => $button_value), 'system/ajax', array(), array(), $form_html_id);
- $this->assert(count($this->xpath($field_xpath . $field_items_xpath_suffix)) == $i+2, t('Found the correct number of field items after an AJAX submission.'));
- $this->assertFieldByXPath($field_xpath . $button_xpath_suffix, NULL, t('Found the "add more" button after an AJAX submission.'));
- $this->assertNoDuplicateIds(t('Updated page contains unique IDs'), 'Other');
- }
- }
- }
- }
-
- * Test Ajax forms when page caching for anonymous users is turned on.
- */
- class AJAXFormPageCacheTestCase extends AJAXTestCase {
- protected $profile = 'testing';
-
- public function setUp() {
- parent::setUp();
-
- config_set('system.core', 'cache', 1);
- config_set('system.core', 'page_cache_maximum_age', 300);
- }
-
-
- * Return the build id of the current form.
- */
- protected function getFormBuildId() {
- $build_id_fields = $this->xpath('//main//input[@name="form_build_id"]');
- $this->assertEqual(count($build_id_fields), 1, 'One form build id field on the page');
- return (string) $build_id_fields[0]['value'];
- }
-
-
- * Create a simple form, then POST to system/ajax to change to it.
- */
- public function testSimpleAJAXFormValue() {
- $this->backdropGet('ajax_forms_test_get_form');
- $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'MISS', 'Page was not cached.');
- $build_id_initial = $this->getFormBuildId();
-
- $edit = array('select' => 'green');
- $commands = $this->backdropPostAJAX(NULL, $edit, 'select');
- $build_id_first_ajax = $this->getFormBuildId();
- $this->assertNotEqual($build_id_initial, $build_id_first_ajax, 'Build id is changed in the simpletest-DOM on first AJAX submission');
- $expected = array(
- 'command' => 'updateBuildId',
- 'old' => $build_id_initial,
- 'new' => $build_id_first_ajax,
- );
- $this->assertCommand($commands, $expected, 'Build id change command issued on first AJAX submission');
-
- $edit = array('select' => 'red');
- $commands = $this->backdropPostAJAX(NULL, $edit, 'select');
- $build_id_second_ajax = $this->getFormBuildId();
- $this->assertEqual($build_id_first_ajax, $build_id_second_ajax, 'Build id remains the same on subsequent AJAX submissions');
-
-
- $this->backdropGet('ajax_forms_test_get_form');
- $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'HIT', 'Page was cached.');
- $build_id_from_cache_initial = $this->getFormBuildId();
- $this->assertEqual($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
-
- $edit = array('select' => 'green');
- $commands = $this->backdropPostAJAX(NULL, $edit, 'select');
- $build_id_from_cache_first_ajax = $this->getFormBuildId();
- $this->assertNotEqual($build_id_from_cache_initial, $build_id_from_cache_first_ajax, 'Build id is changed in the simpletest-DOM on first AJAX submission');
- $this->assertNotEqual($build_id_first_ajax, $build_id_from_cache_first_ajax, 'Build id from first user is not reused');
- $expected = array(
- 'command' => 'updateBuildId',
- 'old' => $build_id_from_cache_initial,
- 'new' => $build_id_from_cache_first_ajax,
- );
- $this->assertCommand($commands, $expected, 'Build id change command issued on first AJAX submission');
-
- $edit = array('select' => 'red');
- $commands = $this->backdropPostAJAX(NULL, $edit, 'select');
- $build_id_from_cache_second_ajax = $this->getFormBuildId();
- $this->assertEqual($build_id_from_cache_first_ajax, $build_id_from_cache_second_ajax, 'Build id remains the same on subsequent AJAX submissions');
- }
- }
-
-
- * Miscellaneous Ajax tests using ajax_test module.
- */
- class AJAXElementValidation extends AJAXTestCase {
-
- * Try to post an Ajax change to a form that has a validated element.
- *
- * The driver_text field is Ajax-enabled. An additional field is not, but
- * is set to be a required field. In this test the required field is not
- * filled in, and we want to see if the activation of the "driver_text"
- * Ajax-enabled field fails due to the required field being empty.
- */
- function testAJAXElementValidation() {
- $web_user = $this->backdropCreateUser();
- $edit = array('driver_text' => t('some dumb text'));
-
-
- $post_result = $this->backdropPostAJAX('ajax_validation_test', $edit, 'driver_text');
-
- $this->assertNoText(t('Error message'), "No error message in resultant JSON");
- $this->assertText('ajax_forms_test_validation_form_callback invoked', 'The correct callback was invoked');
- }
- }
-
- * Tests use of dialogs as wrappers for Ajax responses.
- */
- class AJAXDialogTest extends AJAXTestCase {
- protected $profile = 'testing';
-
-
- * @var User
- */
- protected $web_user;
-
- function setUp() {
- parent::setUp();
-
- $this->web_user = $this->backdropCreateUser(array('access content'));
- $this->backdropLogin($this->web_user);
- }
-
-
- * Test sending non-JS and AJAX requests to open and manipulate modals.
- */
- function testDialog() {
-
- $this->backdropGet('ajax-test/dialog');
-
-
- $dialog_renderable = ajax_test_dialog_contents();
- $dialog_contents = backdrop_render($dialog_renderable);
- $modal_expected_response = array(
- 'command' => 'openDialog',
- 'selector' => '#backdrop-modal',
- 'data' => $dialog_contents,
- 'dialogOptions' => array(
- 'modal' => true,
- 'title' => 'AJAX Dialog',
- ),
- );
- $normal_expected_response = array(
- 'command' => 'openDialog',
- 'selector' => '#ajax-test-dialog-wrapper-1',
- 'data' => $dialog_contents,
- 'dialogOptions' => array(
- 'modal' => false,
- 'title' => 'AJAX Dialog',
- ),
- );
- $close_expected_response = array(
- 'command' => 'closeDialog',
- 'selector' => '#ajax-test-dialog-wrapper-1',
- );
-
-
- $this->backdropGet('ajax-test/dialog-contents/nojs/1');
- $this->assertRaw($dialog_contents, 'Non-JS modal dialog page present.');
-
-
- $ajax_result = $this->backdropGetAJAX('ajax-test/dialog-contents/ajax/1');
- $this->assertEqual($modal_expected_response, $ajax_result[1], 'Modal dialog JSON response matches.');
-
-
- $this->backdropGet('ajax-test/dialog-contents/nojs');
- $this->assertRaw($dialog_contents, 'Non-JS normal dialog page present.');
-
-
- $ajax_result = $this->backdropGetAJAX('ajax-test/dialog-contents/ajax');
- $this->assertEqual($normal_expected_response, $ajax_result[1], 'Normal dialog JSON response matches.');
-
-
-
- $ajax_result = $this->backdropGetAJAX('ajax-test/dialog-close');
- $this->assertEqual($close_expected_response, $ajax_result[1], 'Close dialog JSON response matches.');
-
-
-
-
- $ajax_result = $this->backdropPostAJAX('ajax-test/dialog', array(), 'button1');
-
-
- $dialog_css_exists = strpos($ajax_result[1]['data'], 'jquery.ui.dialog.css') !== FALSE;
- $this->assertTrue($dialog_css_exists, 'jQuery UI dialog CSS added to the page.');
- $dialog_js_exists = strpos($ajax_result[2]['data'], 'jquery.ui.dialog.min.js') !== FALSE;
- $this->assertTrue($dialog_js_exists, 'jQuery UI dialog JS added to the page.');
- $dialog_js_exists = strpos($ajax_result[2]['data'], 'dialog.ajax.js') !== FALSE;
- $this->assertTrue($dialog_js_exists, 'Backdrop dialog JS added to the page.');
-
-
- $this->assertEqual($modal_expected_response, $ajax_result[3], 'POST request modal dialog JSON response matches.');
-
-
- $ajax_result = $this->backdropPostAJAX('ajax-test/dialog', array(), 'button2');
- $this->assertEqual($normal_expected_response, $ajax_result[3], 'POST request normal dialog JSON response matches.');
- }
-
- }