From a2c8edbefb80de1cff6b610f48bda0c2a617baef Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 25 May 2023 11:48:04 +0200 Subject: [PATCH 01/13] Comment Template Block: Add test coverage for context setting --- .../tests/blocks/renderCommentTemplate.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index a206fa5e2b3b0..411d21c145b5d 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -512,4 +512,41 @@ public function test_rendering_comment_template_unmoderated_preview() { 'Should not include any unapproved comments after removing filter' ); } + + public function test_rendering_comment_template_sets_comment_id_context() { + $parsed_comment_author_name_block = parse_blocks( '' )[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(); + + $render_block_callback = 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; + }; + + add_filter( 'render_block', $render_block_callback, 10, 3 ); + $parsed_blocks = parse_blocks( + '' + ); + $block = new WP_Block( + $parsed_blocks[0], + array( + 'postId' => self::$custom_post->ID, + ) + ); + $markup = $block->render(); + remove_filter( 'render_block', $render_block_callback ); + + $this->assertStringContainsString( $comment_author_name_block_markup, $markup ); + } } From 5d2bc8a0846eba61468d4679d1ccae9ffb0b2b32 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 17:29:00 +0200 Subject: [PATCH 02/13] Verify that inner block inserted by render_block_data is retained. --- .../tests/blocks/renderCommentTemplate.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 411d21c145b5d..de89aa230286b 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -549,4 +549,60 @@ public function test_rendering_comment_template_sets_comment_id_context() { $this->assertStringContainsString( $comment_author_name_block_markup, $markup ); } + + 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; + + $inserted_blocks = parse_blocks( $inserted_block_markup ); + + $parsed_block['innerBlocks'][] = $inserted_blocks[0]; + } + return $parsed_block; + }; + + // Remove auto-insertion filter so it won't collide. + remove_filter( 'render_block_data', 'gutenberg_auto_insert_child_block' ); + + add_filter( 'render_block_data', $render_block_data_callback, 10, 1 ); + $parsed_blocks = parse_blocks( + '' + ); + $block = new WP_Block( + $parsed_blocks[0], + array( + 'postId' => self::$custom_post->ID, + ) + ); + $block->render(); + remove_filter( 'render_block_data', $render_block_data_callback ); + // Add back auto-insertion filter. + add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 1 ); + + $this->assertSame( 5, $render_block_callback->get_call_count() ); + + $args = $render_block_callback->get_args(); + $this->assertSame( 'core/comment-content', $args[0][2]->name ); + $this->assertSame( 'core/comment-template', $args[1][2]->name ); + $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." + ); + } } From 75c4fa891ef4056b42f5e85c654bf831a5f1a3fc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 17 Jul 2023 18:47:52 +0200 Subject: [PATCH 03/13] Remove now-obsolete gutenberg_auto_insert_child_block filter handling --- tests/phpunit/tests/blocks/renderCommentTemplate.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index de89aa230286b..59b66a0e7bef2 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -570,9 +570,6 @@ public function test_inner_block_inserted_by_render_block_data_is_retained() { return $parsed_block; }; - // Remove auto-insertion filter so it won't collide. - remove_filter( 'render_block_data', 'gutenberg_auto_insert_child_block' ); - add_filter( 'render_block_data', $render_block_data_callback, 10, 1 ); $parsed_blocks = parse_blocks( '' @@ -585,8 +582,6 @@ public function test_inner_block_inserted_by_render_block_data_is_retained() { ); $block->render(); remove_filter( 'render_block_data', $render_block_data_callback ); - // Add back auto-insertion filter. - add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 1 ); $this->assertSame( 5, $render_block_callback->get_call_count() ); From 458197fbddc597b51f9a67596414dfd64f6a6a2b Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Mon, 17 Jul 2023 18:49:06 +0200 Subject: [PATCH 04/13] Fix multiline comment Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- tests/phpunit/tests/blocks/renderCommentTemplate.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 59b66a0e7bef2..df16cd291a65c 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -524,8 +524,10 @@ public function test_rendering_comment_template_sets_comment_id_context() { $comment_author_name_block_markup = $comment_author_name_block->render(); $render_block_callback = 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. + /* + * 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; } From c7d7afd598b1ae53ba04c02e8d3b96566443b4ed Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Jul 2023 12:22:51 +0200 Subject: [PATCH 05/13] Add assertion error messages --- .../tests/blocks/renderCommentTemplate.php | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index df16cd291a65c..8bde38d20b092 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -585,12 +585,28 @@ public function test_inner_block_inserted_by_render_block_data_is_retained() { $block->render(); remove_filter( 'render_block_data', $render_block_data_callback ); - $this->assertSame( 5, $render_block_callback->get_call_count() ); + $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 ); - $this->assertSame( 'core/comment-template', $args[1][2]->name ); - $this->assertCount( 2, $args[1][2]->inner_blocks, "Inner block inserted by render_block_data filter wasn't retained." ); + $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], From bd67fd5b8feabc8f0016625e43e103384437ac15 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Jul 2023 16:25:30 +0200 Subject: [PATCH 06/13] PHPDoc for commentID context test --- tests/phpunit/tests/blocks/renderCommentTemplate.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 8bde38d20b092..357a6bffd1c4e 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -513,6 +513,13 @@ public function test_rendering_comment_template_unmoderated_preview() { ); } + /** + * Verify 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() { $parsed_comment_author_name_block = parse_blocks( '' )[0]; $comment_author_name_block = new WP_Block( From 3807fa94cde9c44b121f2f05fc1fd3c1598c8821 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Jul 2023 16:29:50 +0200 Subject: [PATCH 07/13] Add PHPDoc for inner block insertion test --- tests/phpunit/tests/blocks/renderCommentTemplate.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 357a6bffd1c4e..a25c86caf2674 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -559,6 +559,13 @@ public function test_rendering_comment_template_sets_comment_id_context() { $this->assertStringContainsString( $comment_author_name_block_markup, $markup ); } + /** + * Verify 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 ); From ec9a7aeba45566546fde4608e8091fefe0128d23 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Jul 2023 16:38:14 +0200 Subject: [PATCH 08/13] Inline filter --- .../tests/blocks/renderCommentTemplate.php | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index a25c86caf2674..70a0030e8da62 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -530,20 +530,24 @@ public function test_rendering_comment_template_sets_comment_id_context() { ); $comment_author_name_block_markup = $comment_author_name_block->render(); - $render_block_callback = 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; - }; + 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 + ); - add_filter( 'render_block', $render_block_callback, 10, 3 ); $parsed_blocks = parse_blocks( '' ); @@ -554,7 +558,6 @@ public function test_rendering_comment_template_sets_comment_id_context() { ) ); $markup = $block->render(); - remove_filter( 'render_block', $render_block_callback ); $this->assertStringContainsString( $comment_author_name_block_markup, $markup ); } From d3e3ffd88080027abb834d3739bc09f6f5063810 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Jul 2023 17:25:08 +0200 Subject: [PATCH 09/13] Add test coverage for commentId block context present in render_block_context filter at priority 2 --- .../tests/blocks/renderCommentTemplate.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 70a0030e8da62..b01c161b035c1 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -521,6 +521,9 @@ public function test_rendering_comment_template_unmoderated_preview() { * @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( '' )[0]; $comment_author_name_block = new WP_Block( $parsed_comment_author_name_block, @@ -560,6 +563,19 @@ static function( $block_content, $block ) use ( $parsed_comment_author_name_bloc $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." + ); } /** From 08a0c1dc76a14e5d99f23ca92fb223d2c06d823c Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Tue, 18 Jul 2023 17:37:37 +0200 Subject: [PATCH 10/13] Whitespace Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/blocks/renderCommentTemplate.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index b01c161b035c1..0ecade24edf51 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -517,6 +517,7 @@ public function test_rendering_comment_template_unmoderated_preview() { * Verify 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 */ From 471cf46e0b4175cf748bf6a89c0d6e4947702976 Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Tue, 18 Jul 2023 17:37:52 +0200 Subject: [PATCH 11/13] More whitespace Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/blocks/renderCommentTemplate.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 0ecade24edf51..527c82f9ee7c0 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -583,6 +583,7 @@ static function( $block_content, $block ) use ( $parsed_comment_author_name_bloc * Verify 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 */ From 7298df3031a913a423470e58dc36da06f242f572 Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Tue, 18 Jul 2023 17:40:54 +0200 Subject: [PATCH 12/13] PHPDoc wording Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com> --- tests/phpunit/tests/blocks/renderCommentTemplate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 527c82f9ee7c0..9df19a4590002 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -514,7 +514,7 @@ public function test_rendering_comment_template_unmoderated_preview() { } /** - * Verify that the Comment Template block makes comment ID context available to programmatically inserted child blocks. + * Tests that the Comment Template block makes comment ID context available to programmatically inserted child blocks. * * @ticket 58839 * From e7ff8ac560714f6b22672fa893b54477dfc3da99 Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Tue, 18 Jul 2023 17:41:04 +0200 Subject: [PATCH 13/13] More PHPDoc wording Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com> --- tests/phpunit/tests/blocks/renderCommentTemplate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 9df19a4590002..796bbcef8692f 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -580,7 +580,7 @@ static function( $block_content, $block ) use ( $parsed_comment_author_name_bloc } /** - * Verify that an inner block added via the render_block_data filter is retained at render_block stage. + * Tests that an inner block added via the render_block_data filter is retained at render_block stage. * * @ticket 58839 *