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) { ...@@ -51,8 +51,8 @@ public function addCommand($command, $prepend = FALSE) {
* @param Request $request * @param Request $request
* A request object. * A request object.
* *
* @return * @return Response
* Response The current response. * The current response.
*/ */
public function prepare(Request $request) { public function prepare(Request $request) {
$this->setData($this->ajaxRender($request)); $this->setData($this->ajaxRender($request));
...@@ -133,7 +133,7 @@ protected function ajaxRender(Request $request) { ...@@ -133,7 +133,7 @@ protected function ajaxRender(Request $request) {
$scripts = drupal_add_js(); $scripts = drupal_add_js();
if (!empty($scripts['settings'])) { if (!empty($scripts['settings'])) {
$settings = drupal_merge_js_settings($scripts['settings']['data']); $settings = drupal_merge_js_settings($scripts['settings']['data']);
$this->addCommand(new SettingsCommand($settings, TRUE)); $this->addCommand(new SettingsCommand($settings, TRUE), TRUE);
} }
$commands = $this->commands; $commands = $this->commands;
......
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
/** /**
* @file * @file
* Definition of Drupal\system\Tests\Ajax\FormValuesTest. * Contains \Drupal\system\Tests\Ajax\FormValuesTest.
*/ */
namespace Drupal\system\Tests\Ajax; namespace Drupal\system\Tests\Ajax;
use Drupal\Core\Ajax\DataCommand;
/** /**
* Tests that $form_state['values'] is properly delivered to $ajax['callback']. * Tests that $form_state['values'] is properly delivered to $ajax['callback'].
*/ */
...@@ -36,11 +38,8 @@ function testSimpleAjaxFormValue() { ...@@ -36,11 +38,8 @@ function testSimpleAjaxFormValue() {
'select' => $item, 'select' => $item,
); );
$commands = $this->drupalPostAJAX('ajax_forms_test_get_form', $edit, 'select'); $commands = $this->drupalPostAJAX('ajax_forms_test_get_form', $edit, 'select');
$expected = array( $expected = new DataCommand('#ajax_selected_color', 'form_state_value_select', $item);
'command' => 'data', $this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a selectbox issued with a correct value.');
'value' => $item,
);
$this->assertCommand($commands, $expected, "verification of AJAX form values from a selectbox issued with a correct value");
} }
// Verify form values of a checkbox element. // Verify form values of a checkbox element.
...@@ -49,11 +48,8 @@ function testSimpleAjaxFormValue() { ...@@ -49,11 +48,8 @@ function testSimpleAjaxFormValue() {
'checkbox' => $item, 'checkbox' => $item,
); );
$commands = $this->drupalPostAJAX('ajax_forms_test_get_form', $edit, 'checkbox'); $commands = $this->drupalPostAJAX('ajax_forms_test_get_form', $edit, 'checkbox');
$expected = array( $expected = new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $item);
'command' => 'data', $this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a checkbox issued with a correct value.');
'value' => (int) $item,
);
$this->assertCommand($commands, $expected, "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) { ...@@ -502,8 +502,10 @@ function ajax_forms_test_lazy_load_form($form, &$form_state) {
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Submit'), '#value' => t('Submit'),
'#ajax' => array( '#ajax' => array(
'wrapper' => 'ajax-forms-test-lazy-load-ajax-wrapper',
'callback' => 'ajax_forms_test_lazy_load_form_ajax', 'callback' => 'ajax_forms_test_lazy_load_form_ajax',
), ),
'#prefix' => '<div id="ajax-forms-test-lazy-load-ajax-wrapper"></div>',
); );
return $form; return $form;
} }
...@@ -522,11 +524,7 @@ function ajax_forms_test_lazy_load_form_submit($form, &$form_state) { ...@@ -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. * 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) { function ajax_forms_test_lazy_load_form_ajax($form, &$form_state) {
return NULL; return array('#markup' => 'new content');
} }
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
* Helper module for Ajax framework tests. * Helper module for Ajax framework tests.
*/ */
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
/** /**
* Implements hook_menu(). * Implements hook_menu().
*/ */
...@@ -15,6 +18,13 @@ function ajax_test_menu() { ...@@ -15,6 +18,13 @@ function ajax_test_menu() {
'access callback' => TRUE, 'access callback' => TRUE,
'type' => MENU_CALLBACK, '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( $items['ajax-test/render-error'] = array(
'title' => 'ajax_render_error', 'title' => 'ajax_render_error',
'page callback' => 'ajax_test_error', 'page callback' => 'ajax_test_error',
...@@ -48,7 +58,7 @@ function ajax_test_system_theme_info() { ...@@ -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 * Additionally ensures that ajax_render() incorporates JavaScript settings
* generated during the page request by invoking drupal_add_js() with a dummy * generated during the page request by invoking drupal_add_js() with a dummy
...@@ -59,6 +69,27 @@ function ajax_test_render() { ...@@ -59,6 +69,27 @@ function ajax_test_render() {
return array('#type' => 'ajax', '#commands' => array()); 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. * 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