Issue #1848880 by Wim Leers, effulgentsia: Test order of AJAX commands: add Ajax\FrameworkTest::testOrder(), strengthen testLazyLoad(), fix JS settings not being prepended.

8.0.x
webchick 2013-03-04 20:16:44 -05:00
parent a7a746a69a
commit 570433156d
6 changed files with 188 additions and 164 deletions

View File

@ -51,8 +51,8 @@ class AjaxResponse extends JsonResponse {
* @param Request $request
* A request object.
*
* @return
* Response The current response.
* @return Response
* The current response.
*/
public function prepare(Request $request) {
$this->setData($this->ajaxRender($request));
@ -133,7 +133,7 @@ class AjaxResponse extends JsonResponse {
$scripts = drupal_add_js();
if (!empty($scripts['settings'])) {
$settings = drupal_merge_js_settings($scripts['settings']['data']);
$this->addCommand(new SettingsCommand($settings, TRUE));
$this->addCommand(new SettingsCommand($settings, TRUE), TRUE);
}
$commands = $this->commands;

View File

@ -2,11 +2,27 @@
/**
* @file
* Definition of Drupal\system\Tests\Ajax\CommandsTest.
* Contains \Drupal\system\Tests\Ajax\CommandsTest.
*/
namespace Drupal\system\Tests\Ajax;
use Drupal\Core\Ajax\AddCssCommand;
use Drupal\Core\Ajax\AfterCommand;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\BeforeCommand;
use Drupal\Core\Ajax\ChangedCommand;
use Drupal\Core\Ajax\CssCommand;
use Drupal\Core\Ajax\DataCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\InsertCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\RestripeCommand;
use Drupal\Core\Ajax\SettingsCommand;
/**
* Tests Ajax framework commands.
*/
@ -29,149 +45,89 @@ class CommandsTest extends AjaxTestBase {
$edit = array();
// Tests the 'add_css' command.
$commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'add_css' command")));
$expected = new AddCssCommand('my/file.css');
$this->assertCommand($commands, $expected->render(), "'add_css' AJAX command issued with correct data.");
// Tests the 'after' command.
$commands = $this->drupalPostAJAX($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");
$expected = new AfterCommand('#after_div', 'This will be placed after');
$this->assertCommand($commands, $expected->render(), "'after' AJAX command issued with correct data.");
// Tests the 'alert' command.
$commands = $this->drupalPostAJAX($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");
$expected = new AlertCommand(t('Alert'));
$this->assertCommand($commands, $expected->render(), "'alert' AJAX Command issued with correct text.");
// Tests the 'append' command.
$commands = $this->drupalPostAJAX($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");
$expected = new AppendCommand('#append_div', 'Appended text');
$this->assertCommand($commands, $expected->render(), "'append' AJAX command issued with correct data.");
// Tests the 'before' command.
$commands = $this->drupalPostAJAX($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");
$expected = new BeforeCommand('#before_div', 'Before text');
$this->assertCommand($commands, $expected->render(), "'before' AJAX command issued with correct data.");
// Tests the 'changed' command.
$commands = $this->drupalPostAJAX($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");
$expected = new ChangedCommand('#changed_div');
$this->assertCommand($commands, $expected->render(), "'changed' AJAX command issued with correct selector.");
// Tests the 'changed' command using the second argument.
$commands = $this->drupalPostAJAX($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");
$expected = new ChangedCommand('#changed_div', '#changed_div_mark_this');
$this->assertCommand($commands, $expected->render(), "'changed' AJAX command (with asterisk) issued with correct selector.");
// Tests the 'css' command.
$commands = $this->drupalPostAJAX($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");
$expected = new CssCommand('#css_div', array('background-color' => 'blue'));
$this->assertCommand($commands, $expected->render(), "'css' AJAX command issued with correct selector.");
// Tests the 'data' command.
$commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX data command: Issue command.")));
$expected = array(
'command' => 'data',
'name' => 'testkey',
'value' => 'testvalue',
);
$this->assertCommand($commands, $expected, "'data' AJAX command issued with correct key and value");
// Tests the 'invoke' command.
$commands = $this->drupalPostAJAX($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");
$expected = new DataCommand('#data_div', 'testkey', 'testvalue');
$this->assertCommand($commands, $expected->render(), "'data' AJAX command issued with correct key and value.");
// Tests the 'html' command.
$commands = $this->drupalPostAJAX($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");
$expected = new HtmlCommand('#html_div', 'replacement text');
$this->assertCommand($commands, $expected->render(), "'html' AJAX command issued with correct data.");
// Tests the 'insert' command.
$commands = $this->drupalPostAJAX($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");
$expected = new InsertCommand('#insert_div', 'insert replacement text');
$this->assertCommand($commands, $expected->render(), "'insert' AJAX command issued with correct data.");
// Tests the 'invoke' command.
$commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX invoke command: Invoke addClass() method.")));
$expected = new InvokeCommand('#invoke_div', 'addClass', array('error'));
$this->assertCommand($commands, $expected->render(), "'invoke' AJAX command issued with correct method and argument.");
// Tests the 'prepend' command.
$commands = $this->drupalPostAJAX($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");
$expected = new PrependCommand('#prepend_div', 'prepended text');
$this->assertCommand($commands, $expected->render(), "'prepend' AJAX command issued with correct data.");
// Tests the 'remove' command.
$commands = $this->drupalPostAJAX($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");
$expected = new RemoveCommand('#remove_text');
$this->assertCommand($commands, $expected->render(), "'remove' AJAX command issued with correct command and selector.");
// Tests the 'restripe' command.
$commands = $this->drupalPostAJAX($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");
$expected = new RestripeCommand('#restripe_table');
$this->assertCommand($commands, $expected->render(), "'restripe' AJAX command issued with correct selector.");
// Tests the 'settings' command.
$commands = $this->drupalPostAJAX($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");
$expected = new SettingsCommand(array('ajax_forms_test' => array('foo' => 42)));
$this->assertCommand($commands, $expected->render(), "'settings' AJAX command issued with correct data.");
// Test that the settings command merges settings properly.
$commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'settings' command with setting merging")));
$expected = array(
'command' => 'settings',
'settings' => array('ajax_forms_test' => array('foo' => 9001)),
);
$this->assertCommand($commands, $expected, "'settings' AJAX command with setting merging");
// Tests the 'add_css' command.
$commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'add_css' command")));
$expected = array(
'command' => 'add_css',
'data' => 'my/file.css',
);
$this->assertCommand($commands, $expected, "'add_css' AJAX command issued with correct data");
// Test that the settings command merges settings properly.
$commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'settings' command with setting merging")));
$expected = new SettingsCommand(array('ajax_forms_test' => array('foo' => 9001)), TRUE);
$this->assertCommand($commands, $expected->render(), "'settings' AJAX command with setting merging.");
}
}

View File

@ -2,11 +2,13 @@
/**
* @file
* Definition of Drupal\system\Tests\Ajax\FormValuesTest.
* Contains \Drupal\system\Tests\Ajax\FormValuesTest.
*/
namespace Drupal\system\Tests\Ajax;
use Drupal\Core\Ajax\DataCommand;
/**
* Tests that $form_state['values'] is properly delivered to $ajax['callback'].
*/
@ -36,11 +38,8 @@ class FormValuesTest extends AjaxTestBase {
'select' => $item,
);
$commands = $this->drupalPostAJAX('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");
$expected = new DataCommand('#ajax_selected_color', 'form_state_value_select', $item);
$this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a selectbox issued with a correct value.');
}
// Verify form values of a checkbox element.
@ -49,11 +48,8 @@ class FormValuesTest extends AjaxTestBase {
'checkbox' => $item,
);
$commands = $this->drupalPostAJAX('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");
$expected = new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $item);
$this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a checkbox issued with a correct value.');
}
}
}

View File

@ -2,11 +2,18 @@
/**
* @file
* Definition of Drupal\system\Tests\Ajax\FrameworkTest.
* Contains \Drupal\system\Tests\Ajax\FrameworkTest.
*/
namespace Drupal\system\Tests\Ajax;
use Drupal\Core\Ajax\AddCssCommand;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Ajax\SettingsCommand;
/**
* Tests primary Ajax framework functions.
*/
@ -21,55 +28,83 @@ class FrameworkTest extends AjaxTestBase {
/**
* Ensures ajax_render() returns JavaScript settings from 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() {
// Verify that settings command is generated when JavaScript settings are
// set via drupal_add_js().
$commands = $this->drupalGetAJAX('ajax-test/render');
$expected = new SettingsCommand(array('ajax' => 'test'), TRUE);
$this->assertCommand($commands, $expected->render(), 'ajax_render() loads settings added with drupal_add_js().');
// Verify that there is a command to load settings added with
// drupal_add_js().
$expected = array(
'command' => 'settings',
'settings' => array('ajax' => 'test'),
);
$this->assertCommand($commands, $expected, 'ajax_render() loads settings added with drupal_add_js().');
// Verify that Ajax settings are loaded for #type 'link'.
// Verify that JavaScript settings are loaded for #type 'link'.
$this->drupalGet('ajax-test/link');
$settings = $this->drupalGetSettings();
$this->assertEqual($settings['ajax']['ajax-link']['url'], url('filter/tips'));
$this->assertEqual($settings['ajax']['ajax-link']['wrapper'], 'block-system-main');
}
/**
* Tests AjaxResponse::prepare() AJAX commands ordering.
*/
function testOrder() {
$path = drupal_get_path('module', 'system');
$expected_commands = array();
// Expected commands, in a very specific order.
$expected_commands[0] = new SettingsCommand(array('ajax' => 'test'), TRUE);
drupal_static_reset('drupal_add_css');
drupal_add_css($path . '/system.admin.css');
drupal_add_css($path . '/system.maintenance.css');
$expected_commands[1] = new AddCssCommand(drupal_get_css(drupal_add_css(), TRUE));
drupal_static_reset('drupal_add_js');
drupal_add_js($path . '/system.js');
$expected_commands[2] = new PrependCommand('head', drupal_get_js('header', drupal_add_js(), TRUE));
drupal_static_reset('drupal_add_js');
drupal_add_js($path . '/system.modules.js', array('scope' => 'footer'));
$expected_commands[3] = new AppendCommand('body', drupal_get_js('footer', drupal_add_js(), TRUE));
$expected_commands[4] = new HtmlCommand('body', 'Hello, world!');
// Load any page with at least one CSS file, at least one JavaScript file
// and at least one #ajax-powered element. The latter is an assumption of
// drupalPostAJAX(), the two former are assumptions of
// AjaxReponse::ajaxRender().
// @todo refactor AJAX Framework + tests to make less assumptions.
$this->drupalGet('ajax_forms_test_lazy_load_form');
// Verify AJAX command order — this should always be the order:
// 1. JavaScript settings
// 2. CSS files
// 3. JavaScript files in the header
// 4. JavaScript files in the footer
// 5. Any other AJAX commands, in whatever order they were added.
$commands = $this->drupalPostAJAX(NULL, array(), NULL, 'ajax-test/order', array(), array(), NULL, array());
$this->assertCommand(array_slice($commands, 0, 1), $expected_commands[0]->render(), 'Settings command is first.');
$this->assertCommand(array_slice($commands, 1, 1), $expected_commands[1]->render(), 'CSS command is second (and CSS files are ordered correctly).');
$this->assertCommand(array_slice($commands, 2, 1), $expected_commands[2]->render(), 'Header JS command is third.');
$this->assertCommand(array_slice($commands, 3, 1), $expected_commands[3]->render(), 'Footer JS command is fourth.');
$this->assertCommand(array_slice($commands, 4, 1), $expected_commands[4]->render(), 'HTML command is fifth.');
}
/**
* Tests behavior of ajax_render_error().
*/
function testAJAXRenderError() {
// Verify default error message.
$commands = $this->drupalGetAJAX('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, 'ajax_render_error() invokes alert command.');
$expected = new AlertCommand(t('An error occurred while handling the request: The server received invalid input.'));
$this->assertCommand($commands, $expected->render(), 'ajax_render_error() invokes alert command.');
// Verify custom error message.
$edit = array(
'message' => 'Custom error message.',
);
$commands = $this->drupalGetAJAX('ajax-test/render-error', array('query' => $edit));
$expected = array(
'command' => 'alert',
'text' => $edit['message'],
);
$this->assertCommand($commands, $expected, 'Custom error message is output.');
$expected = new AlertCommand($edit['message']);
$this->assertCommand($commands, $expected->render(), 'Custom error message is output.');
}
/**
* Tests that new JavaScript and CSS files are returned on an Ajax request.
* Tests that new JavaScript and CSS files are lazy-loaded on an AJAX request.
*/
function testLazyLoad() {
$expected = array(
@ -103,16 +138,20 @@ class FrameworkTest extends AjaxTestBase {
// Verify that the base page doesn't have the settings and files that are to
// be lazy loaded as part of the next requests.
$this->assertTrue(!isset($original_settings[$expected['setting_name']]), format_string('Page originally lacks the %setting, as expected.', array('%setting' => $expected['setting_name'])));
$this->assertTrue(!isset($original_settings[$expected['css']]), format_string('Page originally lacks the %css file, as expected.', array('%css' => $expected['css'])));
$this->assertTrue(!isset($original_settings[$expected['js']]), format_string('Page originally lacks the %js file, as expected.', array('%js' => $expected['js'])));
$this->assertTrue(!isset($original_css[$expected['css']]), format_string('Page originally lacks the %css file, as expected.', array('%css' => $expected['css'])));
$this->assertTrue(!isset($original_js[$expected['js']]), format_string('Page originally lacks the %js file, as expected.', array('%js' => $expected['js'])));
// Submit the Ajax request without triggering files getting added.
// Submit the AJAX request without triggering files getting added.
$commands = $this->drupalPostAJAX(NULL, array('add_files' => FALSE), array('op' => t('Submit')));
$new_settings = $this->drupalGetSettings();
$new_css = $new_settings['ajaxPageState']['css'];
$new_js = $new_settings['ajaxPageState']['js'];
// Verify the setting was not added when not expected.
$this->assertTrue(!isset($new_settings['setting_name']), format_string('Page still lacks the %setting, as expected.', array('%setting' => $expected['setting_name'])));
// Verify a settings command does not add CSS or scripts to Drupal.settings
$this->assertTrue(!isset($new_settings[$expected['setting_name']]), format_string('Page still lacks the %setting, as expected.', array('%setting' => $expected['setting_name'])));
$this->assertTrue(!isset($new_css[$expected['css']]), format_string('Page still lacks the %css file, as expected.', array('%css' => $expected['css'])));
$this->assertTrue(!isset($new_js[$expected['js']]), format_string('Page still lacks the %js file, as expected.', array('%js' => $expected['js'])));
// Verify a settings command does not add CSS or scripts to drupalSettings
// and no command inserts the corresponding tags on the page.
$found_settings_command = FALSE;
$found_markup_command = FALSE;
@ -127,27 +166,31 @@ class FrameworkTest extends AjaxTestBase {
$this->assertFalse($found_settings_command, format_string('Page state still lacks the %css and %js files, as expected.', array('%css' => $expected['css'], '%js' => $expected['js'])));
$this->assertFalse($found_markup_command, format_string('Page still lacks the %css and %js files, as expected.', array('%css' => $expected['css'], '%js' => $expected['js'])));
// Submit the Ajax request and trigger adding files.
// Submit the AJAX request and trigger adding files.
$commands = $this->drupalPostAJAX(NULL, array('add_files' => TRUE), array('op' => t('Submit')));
$new_settings = $this->drupalGetSettings();
$new_css = $new_settings['ajaxPageState']['css'];
$new_js = $new_settings['ajaxPageState']['js'];
// Verify the expected setting was added.
// Verify the expected setting was added, both to drupalSettings, and as
// the first AJAX command.
$this->assertIdentical($new_settings[$expected['setting_name']], $expected['setting_value'], format_string('Page now has the %setting.', array('%setting' => $expected['setting_name'])));
$expected_command = new SettingsCommand(array($expected['setting_name'] => $expected['setting_value']), TRUE);
$this->assertCommand(array_slice($commands, 0, 1), $expected_command->render(), format_string('The settings command was first.'));
// Verify the expected CSS file was added, both to Drupal.settings, and as
// an Ajax command for inclusion into the HTML.
// Verify the expected CSS file was added, both to drupalSettings, and as
// the second AJAX command for inclusion into the HTML.
$this->assertEqual($new_css, $original_css + array($expected_css_basename => 1), format_string('Page state now has the %css file.', array('%css' => $expected['css'])));
$this->assertCommand($commands, array('data' => $expected_css_html), format_string('Page now has the %css file.', array('%css' => $expected['css'])));
$this->assertCommand(array_slice($commands, 1, 1), array('data' => $expected_css_html), format_string('Page now has the %css file.', array('%css' => $expected['css'])));
// Verify the expected JS file was added, both to Drupal.settings, and as
// an Ajax command for inclusion into the HTML. By testing for an exact HTML
// string containing the SCRIPT tag, we also ensure that unexpected
// JavaScript code, such as a jQuery.extend() that would potentially clobber
// rather than properly merge settings, didn't accidentally get added.
// Verify the expected JS file was added, both to drupalSettings, and as
// the third AJAX command for inclusion into the HTML. By testing for an
// exact HTML string containing the SCRIPT tag, we also ensure that
// unexpected JavaScript code, such as a jQuery.extend() that would
// potentially clobber rather than properly merge settings, didn't
// accidentally get added.
$this->assertEqual($new_js, $original_js + array($expected['js'] => 1), format_string('Page state now has the %js file.', array('%js' => $expected['js'])));
$this->assertCommand($commands, array('data' => $expected_js_html), format_string('Page now has the %js file.', array('%js' => $expected['js'])));
$this->assertCommand(array_slice($commands, 2, 1), array('data' => $expected_js_html), format_string('Page now has the %js file.', array('%js' => $expected['js'])));
}
/**

View File

@ -502,8 +502,10 @@ function ajax_forms_test_lazy_load_form($form, &$form_state) {
'#type' => 'submit',
'#value' => t('Submit'),
'#ajax' => array(
'wrapper' => 'ajax-forms-test-lazy-load-ajax-wrapper',
'callback' => 'ajax_forms_test_lazy_load_form_ajax',
),
'#prefix' => '<div id="ajax-forms-test-lazy-load-ajax-wrapper"></div>',
);
return $form;
}
@ -522,11 +524,7 @@ function ajax_forms_test_lazy_load_form_submit($form, &$form_state) {
/**
* AJAX form callback: Selects for the ajax_forms_test_lazy_load_form() form.
*
* This function returns nothing, because all we're interested in testing is
* ajax_render() adding commands for JavaScript and CSS added during the page
* request, such as the ones added in ajax_forms_test_lazy_load_form_submit().
*/
function ajax_forms_test_lazy_load_form_ajax($form, &$form_state) {
return NULL;
return array('#markup' => 'new content');
}

View File

@ -5,6 +5,9 @@
* Helper module for Ajax framework tests.
*/
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
/**
* Implements hook_menu().
*/
@ -15,6 +18,13 @@ function ajax_test_menu() {
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['ajax-test/order'] = array(
'title' => 'AJAX commands order',
'page callback' => 'ajax_test_order',
'theme callback' => 'ajax_base_page_theme',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['ajax-test/render-error'] = array(
'title' => 'ajax_render_error',
'page callback' => 'ajax_test_error',
@ -48,7 +58,7 @@ function ajax_test_system_theme_info() {
}
/**
* Page callback: Returns an element suitable for use by ajax_render().
* Menu callback: Returns an element suitable for use by ajax_render().
*
* Additionally ensures that ajax_render() incorporates JavaScript settings
* generated during the page request by invoking drupal_add_js() with a dummy
@ -59,6 +69,27 @@ function ajax_test_render() {
return array('#type' => 'ajax', '#commands' => array());
}
/**
* Menu callback: Returns an AjaxResponse; settings command set last.
*
* Helps verifying AjaxResponse reorders commands to ensure correct execution.
*/
function ajax_test_order() {
$response = new AjaxResponse();
$path = drupal_get_path('module', 'system');
// HTML insertion command.
$response->addCommand(new HtmlCommand('body', 'Hello, world!'));
// Add two CSS files (order should remain the same).
drupal_add_css($path . '/system.admin.css');
drupal_add_css($path . '/system.maintenance.css');
// Add two JavaScript files (first to the footer, should appear last).
drupal_add_js($path . '/system.modules.js', array('scope' => 'footer'));
drupal_add_js($path . '/system.js');
// Finally, add a JavaScript setting.
drupal_add_js(array('ajax' => 'test'), 'setting');
return $response;
}
/**
* Menu callback: Returns AJAX element with #error property set.
*/