Commit 57043315 authored by webchick's avatar webchick

Issue #1848880 by Wim Leers, effulgentsia: Test order of AJAX commands: add...

Issue #1848880 by Wim Leers, effulgentsia: Test order of AJAX commands: add Ajax\FrameworkTest::testOrder(), strengthen testLazyLoad(), fix JS settings not being prepended.
parent a7a746a6
......@@ -51,8 +51,8 @@ public function addCommand($command, $prepend = FALSE) {
* @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 @@ protected function ajaxRender(Request $request) {
$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;
......
......@@ -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 @@ function testSimpleAjaxFormValue() {
'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 @@ function testSimpleAjaxFormValue() {
'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.');
}
}
}
......@@ -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');
}
......@@ -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.
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment