public function AjaxTest::testInsertAjaxResponse in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php \Drupal\FunctionalJavascriptTests\Ajax\AjaxTest::testInsertAjaxResponse()
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.
File
- core/
tests/ Drupal/ FunctionalJavascriptTests/ Ajax/ AjaxTest.php, line 97
Class
- AjaxTest
- Tests AJAX responses.
Namespace
Drupal\FunctionalJavascriptTests\AjaxCode
public function testInsertAjaxResponse() {
$render_single_root = [
'pre-wrapped-div' => '<div class="pre-wrapped">pre-wrapped<script> var test;</script></div>',
'pre-wrapped-span' => '<span class="pre-wrapped">pre-wrapped<script> var test;</script></span>',
'pre-wrapped-whitespace' => ' <div class="pre-wrapped-whitespace">pre-wrapped-whitespace</div>' . "\n",
'not-wrapped' => 'not-wrapped',
'comment-string-not-wrapped' => '<!-- COMMENT -->comment-string-not-wrapped',
'comment-not-wrapped' => '<!-- COMMENT --><div class="comment-not-wrapped">comment-not-wrapped</div>',
'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>',
'empty' => '',
];
$render_multiple_root_unwrapper = [
'mixed' => ' foo <!-- COMMENT --> foo bar<div class="a class"><p>some string</p></div> additional not wrapped strings, <!-- ANOTHER COMMENT --> <p>final string</p>',
'top-level-only' => '<div>element #1</div><div>element #2</div>',
'top-level-only-pre-whitespace' => ' <div>element #1</div><div>element #2</div> ',
'top-level-only-middle-whitespace-span' => '<span>element #1</span> <span>element #2</span>',
'top-level-only-middle-whitespace-div' => '<div>element #1</div> <div>element #2</div>',
];
// 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"] = '<div>' . $render . '</div>';
}
$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 = <<<JS
(function(\$, Drupal){
Drupal.theme.ajaxWrapperMultipleRootElements = function (elements) {
return \$('<div class="my-favorite-div"></div>').append(elements);
};
}(jQuery, Drupal));
JS;
$expected = '<div class="my-favorite-div"><span>element #1</span> <span>element #2</span></div>';
$this
->assertInsert('top-level-only-middle-whitespace-span--effect', $expected, $custom_wrapper_multiple_root);
// Checking custom ajaxWrapperNewContent wrapping.
$custom_wrapper_new_content = <<<JS
(function(\$, Drupal){
Drupal.theme.ajaxWrapperNewContent = function (elements) {
return \$('<div class="div-wrapper-forever"></div>').append(elements);
};
}(jQuery, Drupal));
JS;
$expected = '<div class="div-wrapper-forever"></div>';
$this
->assertInsert('empty', $expected, $custom_wrapper_new_content);
}