3 namespace Drupal\FunctionalJavascriptTests\Ajax;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 * Tests AJAX responses.
12 class AjaxTest extends WebDriverTestBase {
17 public static $modules = ['ajax_test'];
19 public function testAjaxWithAdminRoute() {
20 \Drupal::service('theme_installer')->install(['stable', 'seven']);
21 $theme_config = \Drupal::configFactory()->getEditable('system.theme');
22 $theme_config->set('admin', 'seven');
23 $theme_config->set('default', 'stable');
24 $theme_config->save();
26 $account = $this->drupalCreateUser(['view the administration theme']);
27 $this->drupalLogin($account);
29 // First visit the site directly via the URL. This should render it in the
31 $this->drupalGet('admin/ajax-test/theme');
32 $assert = $this->assertSession();
33 $assert->pageTextContains('Current theme: seven');
35 // Now click the modal, which should also use the admin theme.
36 $this->drupalGet('ajax-test/dialog');
37 $assert->pageTextNotContains('Current theme: stable');
38 $this->clickLink('Link 8 (ajax)');
39 $assert->assertWaitOnAjaxRequest();
41 $assert->pageTextContains('Current theme: stable');
42 $assert->pageTextNotContains('Current theme: seven');
46 * Test that AJAX loaded libraries are not retained between requests.
48 * @see https://www.drupal.org/node/2647916
50 public function testDrupalSettingsCachingRegression() {
51 $this->drupalGet('ajax-test/dialog');
52 $assert = $this->assertSession();
53 $session = $this->getSession();
55 // Insert a fake library into the already loaded library settings.
56 $fake_library = 'fakeLibrary/fakeLibrary';
57 $session->evaluateScript("drupalSettings.ajaxPageState.libraries = drupalSettings.ajaxPageState.libraries + ',$fake_library';");
59 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
60 // Test that the fake library is set.
61 $this->assertContains($fake_library, $libraries);
63 // Click on the AJAX link.
64 $this->clickLink('Link 8 (ajax)');
65 $assert->assertWaitOnAjaxRequest();
67 // Test that the fake library is still set after the AJAX call.
68 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
69 $this->assertContains($fake_library, $libraries);
71 // Reload the page, this should reset the loaded libraries and remove the
73 $this->drupalGet('ajax-test/dialog');
74 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
75 $this->assertNotContains($fake_library, $libraries);
77 // Click on the AJAX link again, and the libraries should still not contain
79 $this->clickLink('Link 8 (ajax)');
80 $assert->assertWaitOnAjaxRequest();
81 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
82 $this->assertNotContains($fake_library, $libraries);
86 * Tests that various AJAX responses with DOM elements are correctly inserted.
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.
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>',
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>',
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>';
117 $expected_renders = array_merge(
119 $render_multiple_root_wrapper,
120 $render_multiple_root_unwrapper
123 // Checking default process of wrapping Ajax content.
124 foreach ($expected_renders as $render_type => $expected) {
125 $this->assertInsert($render_type, $expected);
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);
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);
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);
147 $expected = '<div class="div-wrapper-forever"></div>';
148 $this->assertInsert('empty', $expected, $custom_wrapper_new_content);
154 * @param string $render_type
156 * @param string $expected
158 * @param string $script
159 * Script for additional theming.
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>');
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>');
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>');
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>');
186 * Asserts that page contains an expected value after waiting.
188 * @param string $expected
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");