annotate core/modules/system/src/Tests/Ajax/DialogTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\Ajax;
Chris@0 4
Chris@0 5 use Drupal\ajax_test\Controller\AjaxTestController;
Chris@0 6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
Chris@0 7 use Drupal\Core\Form\FormBuilderInterface;
Chris@0 8 use Drupal\Core\Url;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Performs tests on opening and manipulating dialogs via AJAX commands.
Chris@0 12 *
Chris@0 13 * @group Ajax
Chris@0 14 */
Chris@0 15 class DialogTest extends AjaxTestBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Modules to enable.
Chris@0 19 *
Chris@0 20 * @var array
Chris@0 21 */
Chris@0 22 public static $modules = ['ajax_test', 'ajax_forms_test', 'contact'];
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Test sending non-JS and AJAX requests to open and manipulate modals.
Chris@0 26 */
Chris@0 27 public function testDialog() {
Chris@0 28 $this->drupalLogin($this->drupalCreateUser(['administer contact forms']));
Chris@0 29 // Ensure the elements render without notices or exceptions.
Chris@0 30 $this->drupalGet('ajax-test/dialog');
Chris@0 31
Chris@0 32 // Set up variables for this test.
Chris@0 33 $dialog_renderable = AjaxTestController::dialogContents();
Chris@0 34 $dialog_contents = \Drupal::service('renderer')->renderRoot($dialog_renderable);
Chris@0 35 $modal_expected_response = [
Chris@0 36 'command' => 'openDialog',
Chris@0 37 'selector' => '#drupal-modal',
Chris@0 38 'settings' => NULL,
Chris@0 39 'data' => $dialog_contents,
Chris@0 40 'dialogOptions' => [
Chris@0 41 'modal' => TRUE,
Chris@0 42 'title' => 'AJAX Dialog & contents',
Chris@0 43 ],
Chris@0 44 ];
Chris@0 45 $form_expected_response = [
Chris@0 46 'command' => 'openDialog',
Chris@0 47 'selector' => '#drupal-modal',
Chris@0 48 'settings' => NULL,
Chris@0 49 'dialogOptions' => [
Chris@0 50 'modal' => TRUE,
Chris@0 51 'title' => 'Ajax Form contents',
Chris@0 52 ],
Chris@0 53 ];
Chris@0 54 $entity_form_expected_response = [
Chris@0 55 'command' => 'openDialog',
Chris@0 56 'selector' => '#drupal-modal',
Chris@0 57 'settings' => NULL,
Chris@0 58 'dialogOptions' => [
Chris@0 59 'modal' => TRUE,
Chris@0 60 'title' => 'Add contact form',
Chris@0 61 ],
Chris@0 62 ];
Chris@0 63 $normal_expected_response = [
Chris@0 64 'command' => 'openDialog',
Chris@0 65 'selector' => '#ajax-test-dialog-wrapper-1',
Chris@0 66 'settings' => NULL,
Chris@0 67 'data' => $dialog_contents,
Chris@0 68 'dialogOptions' => [
Chris@0 69 'modal' => FALSE,
Chris@0 70 'title' => 'AJAX Dialog & contents',
Chris@0 71 ],
Chris@0 72 ];
Chris@0 73 $no_target_expected_response = [
Chris@0 74 'command' => 'openDialog',
Chris@0 75 'selector' => '#drupal-dialog-ajax-testdialog-contents',
Chris@0 76 'settings' => NULL,
Chris@0 77 'data' => $dialog_contents,
Chris@0 78 'dialogOptions' => [
Chris@0 79 'modal' => FALSE,
Chris@0 80 'title' => 'AJAX Dialog & contents',
Chris@0 81 ],
Chris@0 82 ];
Chris@0 83 $close_expected_response = [
Chris@0 84 'command' => 'closeDialog',
Chris@0 85 'selector' => '#ajax-test-dialog-wrapper-1',
Chris@0 86 'persist' => FALSE,
Chris@0 87 ];
Chris@0 88
Chris@0 89 // Check that requesting a modal dialog without JS goes to a page.
Chris@0 90 $this->drupalGet('ajax-test/dialog-contents');
Chris@0 91 $this->assertRaw($dialog_contents, 'Non-JS modal dialog page present.');
Chris@0 92
Chris@0 93 // Check that requesting a modal dialog with XMLHttpRequest goes to a page.
Chris@0 94 $this->drupalGetXHR('ajax-test/dialog-contents');
Chris@0 95 $this->assertRaw($dialog_contents, 'Modal dialog page on XMLHttpRequest present.');
Chris@0 96
Chris@0 97 // Emulate going to the JS version of the page and check the JSON response.
Chris@0 98 $ajax_result = $this->drupalGetAjax('ajax-test/dialog-contents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal']]);
Chris@0 99 $this->assertEqual($modal_expected_response, $ajax_result[3], 'Modal dialog JSON response matches.');
Chris@0 100 // Test the HTML escaping of & character.
Chris@0 101 $this->assertEqual($ajax_result[3]['dialogOptions']['title'], 'AJAX Dialog & contents');
Chris@0 102 $this->assertNotEqual($ajax_result[3]['dialogOptions']['title'], 'AJAX Dialog &amp; contents');
Chris@0 103
Chris@0 104 // Check that requesting a "normal" dialog without JS goes to a page.
Chris@0 105 $this->drupalGet('ajax-test/dialog-contents');
Chris@0 106 $this->assertRaw($dialog_contents, 'Non-JS normal dialog page present.');
Chris@0 107
Chris@0 108 // Emulate going to the JS version of the page and check the JSON response.
Chris@0 109 // This needs to use WebTestBase::drupalPostAjaxForm() so that the correct
Chris@0 110 // dialog options are sent.
Chris@0 111 $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', [
Chris@0 112 // We have to mock a form element to make drupalPost submit from a link.
Chris@0 113 'textfield' => 'test',
Chris@0 114 ], [], 'ajax-test/dialog-contents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_dialog']], [], NULL, [
Chris@0 115 'submit' => [
Chris@0 116 'dialogOptions[target]' => 'ajax-test-dialog-wrapper-1',
Chris@0 117 ]
Chris@0 118 ]);
Chris@0 119 $this->assertEqual($normal_expected_response, $ajax_result[3], 'Normal dialog JSON response matches.');
Chris@0 120
Chris@0 121 // Emulate going to the JS version of the page and check the JSON response.
Chris@0 122 // This needs to use WebTestBase::drupalPostAjaxForm() so that the correct
Chris@0 123 // dialog options are sent.
Chris@0 124 $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', [
Chris@0 125 // We have to mock a form element to make drupalPost submit from a link.
Chris@0 126 'textfield' => 'test',
Chris@0 127 ], [], 'ajax-test/dialog-contents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_dialog']], [], NULL, [
Chris@0 128 // Don't send a target.
Chris@0 129 'submit' => []
Chris@0 130 ]);
Chris@0 131 // Make sure the selector ID starts with the right string.
Chris@0 132 $this->assert(strpos($ajax_result[3]['selector'], $no_target_expected_response['selector']) === 0, 'Selector starts with right string.');
Chris@0 133 unset($ajax_result[3]['selector']);
Chris@0 134 unset($no_target_expected_response['selector']);
Chris@0 135 $this->assertEqual($no_target_expected_response, $ajax_result[3], 'Normal dialog with no target JSON response matches.');
Chris@0 136
Chris@0 137 // Emulate closing the dialog via an AJAX request. There is no non-JS
Chris@0 138 // version of this test.
Chris@0 139 $ajax_result = $this->drupalGetAjax('ajax-test/dialog-close');
Chris@0 140 $this->assertEqual($close_expected_response, $ajax_result[0], 'Close dialog JSON response matches.');
Chris@0 141
Chris@0 142 // Test submitting via a POST request through the button for modals. This
Chris@0 143 // approach more accurately reflects the real responses by Drupal because
Chris@0 144 // all of the necessary page variables are emulated.
Chris@0 145 $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', [], 'button1');
Chris@0 146
Chris@0 147 // Check that CSS and JavaScript are "added" to the page dynamically.
Chris@0 148 $this->assertTrue(in_array('core/drupal.dialog.ajax', explode(',', $ajax_result[0]['settings']['ajaxPageState']['libraries'])), 'core/drupal.dialog.ajax library is added to the page.');
Chris@0 149 $dialog_css_exists = strpos($ajax_result[1]['data'], 'dialog.css') !== FALSE;
Chris@0 150 $this->assertTrue($dialog_css_exists, 'jQuery UI dialog CSS added to the page.');
Chris@0 151 $dialog_js_exists = strpos($ajax_result[2]['data'], 'dialog-min.js') !== FALSE;
Chris@0 152 $this->assertTrue($dialog_js_exists, 'jQuery UI dialog JS added to the page.');
Chris@0 153 $dialog_js_exists = strpos($ajax_result[2]['data'], 'dialog.ajax.js') !== FALSE;
Chris@0 154 $this->assertTrue($dialog_js_exists, 'Drupal dialog JS added to the page.');
Chris@0 155
Chris@0 156 // Check that the response matches the expected value.
Chris@0 157 $this->assertEqual($modal_expected_response, $ajax_result[4], 'POST request modal dialog JSON response matches.');
Chris@0 158 // Test the HTML escaping of & character.
Chris@0 159 $this->assertNotEqual($ajax_result[4]['dialogOptions']['title'], 'AJAX Dialog &amp; contents');
Chris@0 160
Chris@0 161 // Abbreviated test for "normal" dialogs, testing only the difference.
Chris@0 162 $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', [], 'button2');
Chris@0 163 $this->assertEqual($normal_expected_response, $ajax_result[4], 'POST request normal dialog JSON response matches.');
Chris@0 164
Chris@0 165 // Check that requesting a form dialog without JS goes to a page.
Chris@0 166 $this->drupalGet('ajax-test/dialog-form');
Chris@0 167 // Check we get a chunk of the code, we can't test the whole form as form
Chris@0 168 // build id and token with be different.
Chris@0 169 $form = $this->xpath("//form[@id='ajax-test-form']");
Chris@0 170 $this->assertTrue(!empty($form), 'Non-JS form page present.');
Chris@0 171
Chris@0 172 // Emulate going to the JS version of the form and check the JSON response.
Chris@0 173 $ajax_result = $this->drupalGetAjax('ajax-test/dialog-form', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal']]);
Chris@0 174 $expected_ajax_settings = [
Chris@0 175 'edit-preview' => [
Chris@0 176 'callback' => '::preview',
Chris@0 177 'event' => 'click',
Chris@0 178 'url' => Url::fromRoute('ajax_test.dialog_form', [], [
Chris@0 179 'query' => [
Chris@0 180 MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal',
Chris@0 181 FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
Chris@0 182 ],
Chris@0 183 ])->toString(),
Chris@0 184 'dialogType' => 'ajax',
Chris@0 185 'submit' => [
Chris@0 186 '_triggering_element_name' => 'op',
Chris@0 187 '_triggering_element_value' => 'Preview',
Chris@0 188 ],
Chris@0 189 ],
Chris@0 190 ];
Chris@0 191 $this->assertEqual($expected_ajax_settings, $ajax_result[0]['settings']['ajax']);
Chris@0 192 $this->setRawContent($ajax_result[3]['data']);
Chris@0 193 // Remove the data, the form build id and token will never match.
Chris@0 194 unset($ajax_result[3]['data']);
Chris@0 195 $form = $this->xpath("//form[@id='ajax-test-form']");
Chris@0 196 $this->assertTrue(!empty($form), 'Modal dialog JSON contains form.');
Chris@0 197 $this->assertEqual($form_expected_response, $ajax_result[3]);
Chris@0 198
Chris@0 199 // Check that requesting an entity form dialog without JS goes to a page.
Chris@0 200 $this->drupalGet('admin/structure/contact/add');
Chris@0 201 // Check we get a chunk of the code, we can't test the whole form as form
Chris@0 202 // build id and token with be different.
Chris@0 203 $form = $this->xpath("//form[@id='contact-form-add-form']");
Chris@0 204 $this->assertTrue(!empty($form), 'Non-JS entity form page present.');
Chris@0 205
Chris@0 206 // Emulate going to the JS version of the form and check the JSON response.
Chris@0 207 $ajax_result = $this->drupalGetAjax('admin/structure/contact/add', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal']]);
Chris@0 208 $this->setRawContent($ajax_result[3]['data']);
Chris@0 209 // Remove the data, the form build id and token will never match.
Chris@0 210 unset($ajax_result[3]['data']);
Chris@0 211 $form = $this->xpath("//form[@id='contact-form-add-form']");
Chris@0 212 $this->assertTrue(!empty($form), 'Modal dialog JSON contains entity form.');
Chris@0 213 $this->assertEqual($entity_form_expected_response, $ajax_result[3]);
Chris@0 214 }
Chris@0 215
Chris@0 216 }