X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Ftests%2FDrupal%2FFunctionalJavascriptTests%2FAjax%2FAjaxTest.php;fp=web%2Fcore%2Ftests%2FDrupal%2FFunctionalJavascriptTests%2FAjax%2FAjaxTest.php;h=5d11ce49b4bc758fffdb8864cc3af79f94d4b94d;hp=e05940537c4730e752b2dbe3ff0fe5abd6d86a86;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php b/web/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php index e05940537..5d11ce49b 100644 --- a/web/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php +++ b/web/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php @@ -2,14 +2,14 @@ namespace Drupal\FunctionalJavascriptTests\Ajax; -use Drupal\FunctionalJavascriptTests\JavascriptTestBase; +use Drupal\FunctionalJavascriptTests\WebDriverTestBase; /** * Tests AJAX responses. * * @group Ajax */ -class AjaxTest extends JavascriptTestBase { +class AjaxTest extends WebDriverTestBase { /** * {@inheritdoc} @@ -82,4 +82,119 @@ class AjaxTest extends JavascriptTestBase { $this->assertNotContains($fake_library, $libraries); } + /** + * Tests that various AJAX responses with DOM elements are correctly inserted. + * + * After inserting DOM elements, Drupal JavaScript behaviors should be + * reattached and all top-level elements of type Node.ELEMENT_NODE need to be + * part of the context. + */ + public function testInsertAjaxResponse() { + $render_single_root = [ + 'pre-wrapped-div' => '
pre-wrapped
', + 'pre-wrapped-span' => 'pre-wrapped', + 'pre-wrapped-whitespace' => '
pre-wrapped-whitespace
' . "\n", + 'not-wrapped' => 'not-wrapped', + 'comment-string-not-wrapped' => 'comment-string-not-wrapped', + 'comment-not-wrapped' => '
comment-not-wrapped
', + 'svg' => '', + 'empty' => '', + ]; + $render_multiple_root_unwrapper = [ + 'mixed' => ' foo foo bar

some string

additional not wrapped strings,

final string

', + 'top-level-only' => '
element #1
element #2
', + 'top-level-only-pre-whitespace' => '
element #1
element #2
', + 'top-level-only-middle-whitespace-span' => 'element #1 element #2', + 'top-level-only-middle-whitespace-div' => '
element #1
element #2
', + ]; + + // This is temporary behavior for BC reason. + $render_multiple_root_wrapper = []; + foreach ($render_multiple_root_unwrapper as $key => $render) { + $render_multiple_root_wrapper["$key--effect"] = '
' . $render . '
'; + } + + $expected_renders = array_merge( + $render_single_root, + $render_multiple_root_wrapper, + $render_multiple_root_unwrapper + ); + + // Checking default process of wrapping Ajax content. + foreach ($expected_renders as $render_type => $expected) { + $this->assertInsert($render_type, $expected); + } + + // Checking custom ajaxWrapperMultipleRootElements wrapping. + $custom_wrapper_multiple_root = <<').append(elements); + }; + }(jQuery, Drupal)); +JS; + $expected = '
element #1 element #2
'; + $this->assertInsert('top-level-only-middle-whitespace-span--effect', $expected, $custom_wrapper_multiple_root); + + // Checking custom ajaxWrapperNewContent wrapping. + $custom_wrapper_new_content = <<').append(elements); + }; + }(jQuery, Drupal)); +JS; + $expected = '
'; + $this->assertInsert('empty', $expected, $custom_wrapper_new_content); + } + + /** + * Assert insert. + * + * @param string $render_type + * Render type. + * @param string $expected + * Expected result. + * @param string $script + * Script for additional theming. + */ + public function assertInsert($render_type, $expected, $script = '') { + // Check insert to block element. + $this->drupalGet('ajax-test/insert-block-wrapper'); + $this->getSession()->executeScript($script); + $this->clickLink("Link html $render_type"); + $this->assertWaitPageContains('
' . $expected . '
'); + + $this->drupalGet('ajax-test/insert-block-wrapper'); + $this->getSession()->executeScript($script); + $this->clickLink("Link replaceWith $render_type"); + $this->assertWaitPageContains('
' . $expected . '
'); + + // Check insert to inline element. + $this->drupalGet('ajax-test/insert-inline-wrapper'); + $this->getSession()->executeScript($script); + $this->clickLink("Link html $render_type"); + $this->assertWaitPageContains('
' . $expected . '
'); + + $this->drupalGet('ajax-test/insert-inline-wrapper'); + $this->getSession()->executeScript($script); + $this->clickLink("Link replaceWith $render_type"); + $this->assertWaitPageContains('
' . $expected . '
'); + } + + /** + * Asserts that page contains an expected value after waiting. + * + * @param string $expected + * A needle text. + */ + protected function assertWaitPageContains($expected) { + $page = $this->getSession()->getPage(); + $this->assertTrue($page->waitFor(10, function () use ($page, $expected) { + // Clear content from empty styles and "processed" classes after effect. + $content = str_replace([' class="processed"', ' processed', ' style=""'], '', $page->getContent()); + return stripos($content, $expected) !== FALSE; + }), "Page contains expected value: $expected"); + } + }