<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56262] trunk/tests/phpunit/tests/blocks/renderCommentTemplate.php: Editor: Add test for context setting in Comment Template block.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56262">56262</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56262","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>Bernhard Reiter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-07-18 16:07:49 +0000 (Tue, 18 Jul 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Editor: Add test for context setting in Comment Template block.
Test that `commentId` context is correctly set and made available by the Comment Template block to the `render_block_context` filter (at priority 2 and higher), and to the `render_block` filter (important when programmatically inserting child blocks into the Comment Template block).
Furthermore, test that child blocks inserted via the `render_block_data` filter are retained and thus present at `render_block` filter stage.
Props andrewserong, ramonopoly, peterwilsoncc, costdev, mukesh27, flixos90.
Fixes <a href="https://core.trac.wordpress.org/ticket/58839">#58839</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsblocksrenderCommentTemplatephp">trunk/tests/phpunit/tests/blocks/renderCommentTemplate.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsblocksrenderCommentTemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/blocks/renderCommentTemplate.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/renderCommentTemplate.php 2023-07-18 14:07:02 UTC (rev 56261)
+++ trunk/tests/phpunit/tests/blocks/renderCommentTemplate.php 2023-07-18 16:07:49 UTC (rev 56262)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -512,4 +512,145 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'Should not include any unapproved comments after removing filter'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Tests that the Comment Template block makes comment ID context available to programmatically inserted child blocks.
+ *
+ * @ticket 58839
+ *
+ * @covers ::render_block_core_comment_template
+ * @covers ::block_core_comment_template_render_comments
+ */
+ public function test_rendering_comment_template_sets_comment_id_context() {
+ $render_block_context_callback = new MockAction();
+ add_filter( 'render_block_context', array( $render_block_context_callback, 'filter' ), 2, 3 );
+
+ $parsed_comment_author_name_block = parse_blocks( '<!-- wp:comment-author-name /-->' )[0];
+ $comment_author_name_block = new WP_Block(
+ $parsed_comment_author_name_block,
+ array(
+ 'commentId' => self::$comment_ids[0],
+ )
+ );
+ $comment_author_name_block_markup = $comment_author_name_block->render();
+
+ add_filter(
+ 'render_block',
+ static function( $block_content, $block ) use ( $parsed_comment_author_name_block ) {
+ /*
+ * Insert a Comment Author Name block (which requires `commentId`
+ * block context to work) after the Comment Content block.
+ */
+ if ( 'core/comment-content' !== $block['blockName'] ) {
+ return $block_content;
+ }
+
+ $inserted_content = render_block( $parsed_comment_author_name_block );
+ return $inserted_content . $block_content;
+ },
+ 10,
+ 3
+ );
+
+ $parsed_blocks = parse_blocks(
+ '<!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template -->'
+ );
+ $block = new WP_Block(
+ $parsed_blocks[0],
+ array(
+ 'postId' => self::$custom_post->ID,
+ )
+ );
+ $markup = $block->render();
+
+ $this->assertStringContainsString( $comment_author_name_block_markup, $markup );
+
+ $args = $render_block_context_callback->get_args();
+ $context = $args[0][0];
+ $this->assertArrayHasKey(
+ 'commentId',
+ $context,
+ "commentId block context wasn't set for render_block_context filter at priority 2."
+ );
+ $this->assertSame(
+ strval( self::$comment_ids[0] ),
+ $context['commentId'],
+ "commentId block context wasn't set correctly."
+ );
+ }
+
+ /**
+ * Tests that an inner block added via the render_block_data filter is retained at render_block stage.
+ *
+ * @ticket 58839
+ *
+ * @covers ::render_block_core_comment_template
+ * @covers ::block_core_comment_template_render_comments
+ */
+ public function test_inner_block_inserted_by_render_block_data_is_retained() {
+ $render_block_callback = new MockAction();
+ add_filter( 'render_block', array( $render_block_callback, 'filter' ), 10, 3 );
+
+ $render_block_data_callback = static function( $parsed_block ) {
+ // Add a Social Links block to a Comment Template block's inner blocks.
+ if ( 'core/comment-template' === $parsed_block['blockName'] ) {
+ $inserted_block_markup = <<<END
+<!-- wp:social-links -->
+<ul class="wp-block-social-links"><!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /--></ul>
+<!-- /wp:social-links -->'
+END;
+
+ $inserted_blocks = parse_blocks( $inserted_block_markup );
+
+ $parsed_block['innerBlocks'][] = $inserted_blocks[0];
+ }
+ return $parsed_block;
+ };
+
+ add_filter( 'render_block_data', $render_block_data_callback, 10, 1 );
+ $parsed_blocks = parse_blocks(
+ '<!-- wp:comments --><!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template --><!-- /wp:comments -->'
+ );
+ $block = new WP_Block(
+ $parsed_blocks[0],
+ array(
+ 'postId' => self::$custom_post->ID,
+ )
+ );
+ $block->render();
+ remove_filter( 'render_block_data', $render_block_data_callback );
+
+ $this->assertSame(
+ 5,
+ $render_block_callback->get_call_count(),
+ "render_block filter wasn't called the correct number of 5 times."
+ );
+
+ $args = $render_block_callback->get_args();
+ $this->assertSame(
+ 'core/comment-content',
+ $args[0][2]->name,
+ "render_block filter didn't receive Comment Content block instance upon first call."
+ );
+ $this->assertSame(
+ 'core/comment-template',
+ $args[1][2]->name,
+ "render_block filter didn't receive Comment Template block instance upon second call."
+ );
+ $this->assertCount(
+ 2,
+ $args[1][2]->inner_blocks,
+ "Inner block inserted by render_block_data filter wasn't retained."
+ );
+ $this->assertInstanceOf(
+ 'WP_Block',
+ $args[1][2]->inner_blocks[1],
+ "Inner block inserted by render_block_data isn't a WP_Block class instance."
+ );
+ $this->assertSame(
+ 'core/social-links',
+ $args[1][2]->inner_blocks[1]->name,
+ "Inner block inserted by render_block_data isn't named as expected."
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>