comparison core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\FunctionalJavascriptTests\Ajax; 3 namespace Drupal\FunctionalJavascriptTests\Ajax;
4 4
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase; 5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6 6
7 /** 7 /**
8 * Tests AJAX responses. 8 * Tests AJAX responses.
9 * 9 *
10 * @group Ajax 10 * @group Ajax
11 */ 11 */
12 class AjaxTest extends JavascriptTestBase { 12 class AjaxTest extends WebDriverTestBase {
13 13
14 /** 14 /**
15 * {@inheritdoc} 15 * {@inheritdoc}
16 */ 16 */
17 public static $modules = ['ajax_test']; 17 public static $modules = ['ajax_test'];
80 $assert->assertWaitOnAjaxRequest(); 80 $assert->assertWaitOnAjaxRequest();
81 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries'); 81 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
82 $this->assertNotContains($fake_library, $libraries); 82 $this->assertNotContains($fake_library, $libraries);
83 } 83 }
84 84
85 /**
86 * Tests that various AJAX responses with DOM elements are correctly inserted.
87 *
88 * After inserting DOM elements, Drupal JavaScript behaviors should be
89 * reattached and all top-level elements of type Node.ELEMENT_NODE need to be
90 * part of the context.
91 */
92 public function testInsertAjaxResponse() {
93 $render_single_root = [
94 'pre-wrapped-div' => '<div class="pre-wrapped">pre-wrapped<script> var test;</script></div>',
95 'pre-wrapped-span' => '<span class="pre-wrapped">pre-wrapped<script> var test;</script></span>',
96 'pre-wrapped-whitespace' => ' <div class="pre-wrapped-whitespace">pre-wrapped-whitespace</div>' . "\n",
97 'not-wrapped' => 'not-wrapped',
98 'comment-string-not-wrapped' => '<!-- COMMENT -->comment-string-not-wrapped',
99 'comment-not-wrapped' => '<!-- COMMENT --><div class="comment-not-wrapped">comment-not-wrapped</div>',
100 'svg' => '<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect x="0" y="0" height="10" width="10" fill="green"/></svg>',
101 'empty' => '',
102 ];
103 $render_multiple_root_unwrapper = [
104 'mixed' => ' foo <!-- COMMENT --> foo bar<div class="a class"><p>some string</p></div> additional not wrapped strings, <!-- ANOTHER COMMENT --> <p>final string</p>',
105 'top-level-only' => '<div>element #1</div><div>element #2</div>',
106 'top-level-only-pre-whitespace' => ' <div>element #1</div><div>element #2</div> ',
107 'top-level-only-middle-whitespace-span' => '<span>element #1</span> <span>element #2</span>',
108 'top-level-only-middle-whitespace-div' => '<div>element #1</div> <div>element #2</div>',
109 ];
110
111 // This is temporary behavior for BC reason.
112 $render_multiple_root_wrapper = [];
113 foreach ($render_multiple_root_unwrapper as $key => $render) {
114 $render_multiple_root_wrapper["$key--effect"] = '<div>' . $render . '</div>';
115 }
116
117 $expected_renders = array_merge(
118 $render_single_root,
119 $render_multiple_root_wrapper,
120 $render_multiple_root_unwrapper
121 );
122
123 // Checking default process of wrapping Ajax content.
124 foreach ($expected_renders as $render_type => $expected) {
125 $this->assertInsert($render_type, $expected);
126 }
127
128 // Checking custom ajaxWrapperMultipleRootElements wrapping.
129 $custom_wrapper_multiple_root = <<<JS
130 (function($, Drupal){
131 Drupal.theme.ajaxWrapperMultipleRootElements = function (elements) {
132 return $('<div class="my-favorite-div"></div>').append(elements);
133 };
134 }(jQuery, Drupal));
135 JS;
136 $expected = '<div class="my-favorite-div"><span>element #1</span> <span>element #2</span></div>';
137 $this->assertInsert('top-level-only-middle-whitespace-span--effect', $expected, $custom_wrapper_multiple_root);
138
139 // Checking custom ajaxWrapperNewContent wrapping.
140 $custom_wrapper_new_content = <<<JS
141 (function($, Drupal){
142 Drupal.theme.ajaxWrapperNewContent = function (elements) {
143 return $('<div class="div-wrapper-forever"></div>').append(elements);
144 };
145 }(jQuery, Drupal));
146 JS;
147 $expected = '<div class="div-wrapper-forever"></div>';
148 $this->assertInsert('empty', $expected, $custom_wrapper_new_content);
149 }
150
151 /**
152 * Assert insert.
153 *
154 * @param string $render_type
155 * Render type.
156 * @param string $expected
157 * Expected result.
158 * @param string $script
159 * Script for additional theming.
160 */
161 public function assertInsert($render_type, $expected, $script = '') {
162 // Check insert to block element.
163 $this->drupalGet('ajax-test/insert-block-wrapper');
164 $this->getSession()->executeScript($script);
165 $this->clickLink("Link html $render_type");
166 $this->assertWaitPageContains('<div class="ajax-target-wrapper"><div id="ajax-target">' . $expected . '</div></div>');
167
168 $this->drupalGet('ajax-test/insert-block-wrapper');
169 $this->getSession()->executeScript($script);
170 $this->clickLink("Link replaceWith $render_type");
171 $this->assertWaitPageContains('<div class="ajax-target-wrapper">' . $expected . '</div>');
172
173 // Check insert to inline element.
174 $this->drupalGet('ajax-test/insert-inline-wrapper');
175 $this->getSession()->executeScript($script);
176 $this->clickLink("Link html $render_type");
177 $this->assertWaitPageContains('<div class="ajax-target-wrapper"><span id="ajax-target-inline">' . $expected . '</span></div>');
178
179 $this->drupalGet('ajax-test/insert-inline-wrapper');
180 $this->getSession()->executeScript($script);
181 $this->clickLink("Link replaceWith $render_type");
182 $this->assertWaitPageContains('<div class="ajax-target-wrapper">' . $expected . '</div>');
183 }
184
185 /**
186 * Asserts that page contains an expected value after waiting.
187 *
188 * @param string $expected
189 * A needle text.
190 */
191 protected function assertWaitPageContains($expected) {
192 $page = $this->getSession()->getPage();
193 $this->assertTrue($page->waitFor(10, function () use ($page, $expected) {
194 // Clear content from empty styles and "processed" classes after effect.
195 $content = str_replace([' class="processed"', ' processed', ' style=""'], '', $page->getContent());
196 return stripos($content, $expected) !== FALSE;
197 }), "Page contains expected value: $expected");
198 }
199
85 } 200 }