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