Pull merge.
[yaffs-website] / web / core / tests / Drupal / FunctionalJavascriptTests / Ajax / AjaxTest.php
1 <?php
2
3 namespace Drupal\FunctionalJavascriptTests\Ajax;
4
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6
7 /**
8  * Tests AJAX responses.
9  *
10  * @group Ajax
11  */
12 class AjaxTest extends WebDriverTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['ajax_test'];
18
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();
25
26     $account = $this->drupalCreateUser(['view the administration theme']);
27     $this->drupalLogin($account);
28
29     // First visit the site directly via the URL. This should render it in the
30     // admin theme.
31     $this->drupalGet('admin/ajax-test/theme');
32     $assert = $this->assertSession();
33     $assert->pageTextContains('Current theme: seven');
34
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();
40
41     $assert->pageTextContains('Current theme: stable');
42     $assert->pageTextNotContains('Current theme: seven');
43   }
44
45   /**
46    * Test that AJAX loaded libraries are not retained between requests.
47    *
48    * @see https://www.drupal.org/node/2647916
49    */
50   public function testDrupalSettingsCachingRegression() {
51     $this->drupalGet('ajax-test/dialog');
52     $assert = $this->assertSession();
53     $session = $this->getSession();
54
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';");
58
59     $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
60     // Test that the fake library is set.
61     $this->assertContains($fake_library, $libraries);
62
63     // Click on the AJAX link.
64     $this->clickLink('Link 8 (ajax)');
65     $assert->assertWaitOnAjaxRequest();
66
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);
70
71     // Reload the page, this should reset the loaded libraries and remove the
72     // fake library.
73     $this->drupalGet('ajax-test/dialog');
74     $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
75     $this->assertNotContains($fake_library, $libraries);
76
77     // Click on the AJAX link again, and the libraries should still not contain
78     // the fake library.
79     $this->clickLink('Link 8 (ajax)');
80     $assert->assertWaitOnAjaxRequest();
81     $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
82     $this->assertNotContains($fake_library, $libraries);
83   }
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
200 }