diff -u b/core/modules/system/tests/modules/ajax_test/js/insert-ajax.js b/core/modules/system/tests/modules/ajax_test/js/insert-ajax.js --- b/core/modules/system/tests/modules/ajax_test/js/insert-ajax.js +++ b/core/modules/system/tests/modules/ajax_test/js/insert-ajax.js @@ -15,7 +15,7 @@ wrapper: 'ajax-target', base: false, element: false, - method: 'html' + method: event.currentTarget.getAttribute('data-method') }; var myAjaxObject = Drupal.ajax(ajaxSettings); myAjaxObject.execute(); diff -u b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php --- b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php +++ b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php @@ -51,34 +51,12 @@ * @return array * Renderable array of AJAX response contents. */ - public static function renderTypes($type) { - switch ($type) { - case 'pre-wrapped': - $markup = '
' . $type . '
'; - break; - - case 'pre-wrapped-leading-whitespace': - $markup = '
' . $type . '
'; - break; - - case 'not-wrapped': - $markup = $type; - break; - - case 'comment-not-wrapped': - $markup = '
' . $type . '
'; - break; - - case 'mixed': - $markup = ' foo foo bar

some string

additional not wrapped strings,

final string

'; - break; - } - + public function renderTypes($type) { $content = [ '#title' => 'AJAX Dialog & contents', 'content' => [ '#type' => 'inline_template', - '#template' => !empty($markup) ? $markup : '', + '#template' => $this->getRenderTypes()[$type], ], ]; @@ -89,12 +67,9 @@ * Returns a render array of links that directly Drupal.ajax(). */ public function insertLinks() { - $types = [ - 'pre-wrapped', - 'pre-wrapped-leading-whitespace', - 'not-wrapped', - 'comment-not-wrapped', - 'mixed', + $methods = [ + 'html', + 'replaceWith', ]; $build['links'] = [ @@ -106,17 +81,18 @@ '#attached' => ['library' => ['ajax_test/ajax_insert']], ], ]; - - foreach ($types as $type) { - $build['links']['links']['#links'][$type] = [ - 'title' => "Link $type", - 'url' => Url::fromRoute('ajax_test.ajax_render_types', ['type' => $type]), - 'attributes' => [ - 'class' => ['ajax-insert'], - ], - ]; + foreach ($methods as $method) { + foreach (array_keys($this->getRenderTypes()) as $type) { + $build['links']['links']['#links']["$method-$type"] = [ + 'title' => "Link $method $type", + 'url' => Url::fromRoute('ajax_test.ajax_render_types', ['type' => $type]), + 'attributes' => [ + 'class' => ['ajax-insert'], + 'data-method' => $method, + ], + ]; + } } - return $build; } @@ -303 +279,18 @@ + /** + * Render types. + * + * @return array + * Render types. + */ + protected function getRenderTypes() { + return [ + 'pre-wrapped' => '
pre-wrapped
', + 'pre-wrapped-leading-whitespace' => '
pre-wrapped-leading-whitespace
', + 'not-wrapped' => 'not-wrapped', + 'comment-not-wrapped' => '
comment-not-wrapped
', + 'mixed' => ' foo foo bar

some string

additional not wrapped strings,

final string

', + ]; + } + } + diff -u b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php --- b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php @@ -104,39 +104,61 @@ */ public function testInsert() { $assert = $this->assertSession(); - $this->drupalGet('ajax-test/insert'); - // Test that no additional wrapper is added when inserting already wrapped - // response data and all top-level node elements (context) are processed - // correctly. - $this->clickLink('Link pre-wrapped'); - $assert->assertWaitOnAjaxRequest(); - $assert->responseContains($this->wrapAjaxTarget('
pre-wrapped
')); - - // Test that no additional empty leading div is added when the return - // value had a leading space and all top-level node elements (context) are - // processed correctly. - $this->clickLink('Link pre-wrapped-leading-whitespace'); - $assert->assertWaitOnAjaxRequest(); - $assert->responseContains($this->wrapAjaxTarget('
pre-wrapped-leading-whitespace
')); - - // Test that not wrapped response data (text node) is inserted wrapped and - // all top-level node elements (context) are processed correctly. - $this->clickLink('Link not-wrapped'); - $assert->assertWaitOnAjaxRequest(); - $assert->responseContains($this->wrapAjaxTarget('not-wrapped')); - - // Test that top-level comments (which are not lead by text nodes) are - // inserted without wrapper. - $this->clickLink('Link comment-not-wrapped'); - $assert->assertWaitOnAjaxRequest(); - $assert->responseContains($this->wrapAjaxTarget('
comment-not-wrapped
')); - - // Test that wrappend and not-wrapped response data is inserted correctly - // and all top-level node elements (context) are processed correctly. - $this->clickLink('Link mixed'); - $assert->assertWaitOnAjaxRequest(); - $assert->responseContains($this->wrapAjaxTarget(' foo foo bar

some string

additional not wrapped strings,

final string

')); + $test_cases = [ + // Test that no additional wrapper is added when inserting already wrapped + // response data and all top-level node elements (context) are processed + // correctly. + [ + 'render_type' => 'pre-wrapped', + 'expected' => '
pre-wrapped
', + ], + // Test that no additional empty leading div is added when the return + // value had a leading space and all top-level node elements (context) are + // processed correctly. + [ + 'render_type' => 'pre-wrapped-leading-whitespace', + 'expected' => '
pre-wrapped-leading-whitespace
', + ], + // Test that not wrapped response data (text node) is inserted wrapped and + // all top-level node elements (context) are processed correctly. + [ + 'render_type' => 'not-wrapped', + 'expected' => 'not-wrapped', + ], + // Test that top-level comments (which are not lead by text nodes) are + // inserted without wrapper. + [ + 'render_type' => 'comment-not-wrapped', + 'expected' => '
comment-not-wrapped
', + ], + // Test that wrappend and not-wrapped response data is inserted correctly + // and all top-level node elements (context) are processed correctly. + [ + 'method' => 'html', + 'render_type' => 'mixed', + 'expected' => ' foo foo bar

some string

additional not wrapped strings,

final string

', + ], + + ]; + + foreach (['html', 'replaceWith'] as $method) { + foreach ($test_cases as $test_case) { + $this->drupalGet('ajax-test/insert'); + $this->clickLink("Link $method {$test_case['render_type']}"); + $assert->assertWaitOnAjaxRequest(); + switch ($method) { + case 'html': + $assert->responseContains($this->wrapAjaxTarget($test_case['expected'])); + break; + + case 'replaceWith': + $assert->responseContains($test_case['expected']); + $assert->responseNotContains($this->wrapAjaxTarget($test_case['expected'])); + break; + } + } + } } }