From a0d476d0b838d854df8032c1aa6de9f9d839b1c7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 23 May 2023 16:13:17 +0200 Subject: [PATCH 1/7] Comment Template Block: Add test coverage for context setting --- .../blocks/render-comment-template-test.php | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 phpunit/blocks/render-comment-template-test.php diff --git a/phpunit/blocks/render-comment-template-test.php b/phpunit/blocks/render-comment-template-test.php new file mode 100644 index 00000000000000..847e6662fe40d1 --- /dev/null +++ b/phpunit/blocks/render-comment-template-test.php @@ -0,0 +1,106 @@ + $original_value ) { + update_option( $option, $original_value ); + } + + parent::tear_down(); + } + + public function set_up() { + parent::set_up(); + + update_option( 'page_comments', true ); + update_option( 'comments_per_page', self::$per_page ); + + self::$custom_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'dogs', + 'post_status' => 'publish', + 'post_name' => 'metaldog', + 'post_title' => 'Metal Dog', + 'post_content' => 'Metal Dog content', + 'post_excerpt' => 'Metal Dog', + ) + ); + + self::$comment_ids = self::factory()->comment->create_post_comments( + self::$custom_post->ID, + 1, + array( + 'comment_author' => 'Test', + 'comment_author_email' => 'test@example.org', + 'comment_author_url' => 'http://example.com/author-url/', + 'comment_content' => 'Hello world', + ) + ); + } + + public function test_rendering_comment_template_sets_comment_id_context() { + $render_block_callback = new MockAction(); + add_filter( 'render_block', array( $render_block_callback, 'filter' ), 10, 3 ); + + $parsed_blocks = parse_blocks( + '' + ); + $block = new WP_Block( + $parsed_blocks[0], + array( + 'postId' => self::$custom_post->ID, + ) + ); + $block->render(); + + $this->assertSame( 3, $render_block_callback->get_call_count() ); + + $args = $render_block_callback->get_args(); + + $this->assertSame( 'core/comment-author-name', $args[0][2]->name ); + $this->assertArrayHasKey( 'commentId', $args[0][2]->context ); + $this->assertSame( strval( self::$comment_ids[0] ), $args[0][2]->context['commentId'] ); + + $this->assertSame( 'core/comment-template', $args[1][2]->name ); + $this->assertSame( 'core/comment-template', $args[2][2]->name ); + } +} From 2aa99062387f40df041105aff7b56c87af07dfa9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 23 May 2023 16:58:40 +0200 Subject: [PATCH 2/7] Test via inserted block --- .../blocks/render-comment-template-test.php | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/phpunit/blocks/render-comment-template-test.php b/phpunit/blocks/render-comment-template-test.php index 847e6662fe40d1..2e6544d8d88f7c 100644 --- a/phpunit/blocks/render-comment-template-test.php +++ b/phpunit/blocks/render-comment-template-test.php @@ -78,11 +78,19 @@ public function set_up() { } public function test_rendering_comment_template_sets_comment_id_context() { - $render_block_callback = new MockAction(); - add_filter( 'render_block', array( $render_block_callback, 'filter' ), 10, 3 ); - + $render_block_callback = static function( $block_content, $block ) { + if ( 'core/comment-content' !== $block['blockName'] ) { + return $block_content; + } + $inserted_block_markup = ''; + $inserted_blocks = parse_blocks( $inserted_block_markup ); + $inserted_content = render_block( $inserted_blocks[0] ); + 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], @@ -90,17 +98,12 @@ public function test_rendering_comment_template_sets_comment_id_context() { 'postId' => self::$custom_post->ID, ) ); - $block->render(); - - $this->assertSame( 3, $render_block_callback->get_call_count() ); - - $args = $render_block_callback->get_args(); + $markup = $block->render(); + remove_filter( 'render_block', $render_block_callback ); - $this->assertSame( 'core/comment-author-name', $args[0][2]->name ); - $this->assertArrayHasKey( 'commentId', $args[0][2]->context ); - $this->assertSame( strval( self::$comment_ids[0] ), $args[0][2]->context['commentId'] ); - - $this->assertSame( 'core/comment-template', $args[1][2]->name ); - $this->assertSame( 'core/comment-template', $args[2][2]->name ); + $this->assertSame( + str_replace( array( "\n", "\t" ), '', '
  1. Hello world

' ), + str_replace( array( "\n", "\t" ), '', $markup ) + ); } } From 20eeb3ab2de78c5b9399c6dd088c35c2498d0209 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 23 May 2023 17:12:26 +0200 Subject: [PATCH 3/7] Fix whitespace, add explanatory comment --- phpunit/blocks/render-comment-template-test.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/phpunit/blocks/render-comment-template-test.php b/phpunit/blocks/render-comment-template-test.php index 2e6544d8d88f7c..632f70d9578993 100644 --- a/phpunit/blocks/render-comment-template-test.php +++ b/phpunit/blocks/render-comment-template-test.php @@ -77,8 +77,10 @@ public function set_up() { ); } - public function test_rendering_comment_template_sets_comment_id_context() { + public function test_rendering_comment_template_sets_comment_id_context() {. $render_block_callback = static function( $block_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; } @@ -92,13 +94,13 @@ public function test_rendering_comment_template_sets_comment_id_context() { $parsed_blocks = parse_blocks( '' ); - $block = new WP_Block( + $block = new WP_Block( $parsed_blocks[0], array( 'postId' => self::$custom_post->ID, ) ); - $markup = $block->render(); + $markup = $block->render(); remove_filter( 'render_block', $render_block_callback ); $this->assertSame( From 3260bb917dd91bc384146e4dcd381837805dde4d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 23 May 2023 17:14:27 +0200 Subject: [PATCH 4/7] Typo --- phpunit/blocks/render-comment-template-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/blocks/render-comment-template-test.php b/phpunit/blocks/render-comment-template-test.php index 632f70d9578993..9487c12c387174 100644 --- a/phpunit/blocks/render-comment-template-test.php +++ b/phpunit/blocks/render-comment-template-test.php @@ -77,7 +77,7 @@ public function set_up() { ); } - public function test_rendering_comment_template_sets_comment_id_context() {. + public function test_rendering_comment_template_sets_comment_id_context() { $render_block_callback = static function( $block_content, $block ) { // Insert a Comment Author Name block (which requires `commentId` // block context to work) after the Comment Content block. From 5da64117d5448f4c8860d5a94aeb3453cfbc64c8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 24 May 2023 15:52:01 +0200 Subject: [PATCH 5/7] Update `@since` to 6.3.0 --- phpunit/blocks/render-comment-template-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/blocks/render-comment-template-test.php b/phpunit/blocks/render-comment-template-test.php index 9487c12c387174..8834ef1cde1f0f 100644 --- a/phpunit/blocks/render-comment-template-test.php +++ b/phpunit/blocks/render-comment-template-test.php @@ -4,7 +4,7 @@ * * @package WordPress * @subpackage Blocks - * @since 6.0.0 + * @since 6.3.0 * * @group blocks */ From 4ab65a5c7d7a878eaf8212717c5daa9b99672ad0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 24 May 2023 16:04:16 +0200 Subject: [PATCH 6/7] Make test more semantic --- .../blocks/render-comment-template-test.php | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/phpunit/blocks/render-comment-template-test.php b/phpunit/blocks/render-comment-template-test.php index 8834ef1cde1f0f..b5064499063aec 100644 --- a/phpunit/blocks/render-comment-template-test.php +++ b/phpunit/blocks/render-comment-template-test.php @@ -78,15 +78,23 @@ public function set_up() { } public function test_rendering_comment_template_sets_comment_id_context() { - $render_block_callback = static function( $block_content, $block ) { + $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_block_markup = ''; - $inserted_blocks = parse_blocks( $inserted_block_markup ); - $inserted_content = render_block( $inserted_blocks[0] ); + + $inserted_content = render_block( $parsed_comment_author_name_block ); return $inserted_content . $block_content; }; @@ -103,9 +111,6 @@ public function test_rendering_comment_template_sets_comment_id_context() { $markup = $block->render(); remove_filter( 'render_block', $render_block_callback ); - $this->assertSame( - str_replace( array( "\n", "\t" ), '', '
  1. Hello world

' ), - str_replace( array( "\n", "\t" ), '', $markup ) - ); + $this->assertStringContainsString( $comment_author_name_block_markup, $markup ); } } From 321f8d868e9f70ae437a52d173565d071619aad2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 30 May 2023 14:45:02 +0200 Subject: [PATCH 7/7] Assert that Comment Author Name block markup is not empty --- phpunit/blocks/render-comment-template-test.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/phpunit/blocks/render-comment-template-test.php b/phpunit/blocks/render-comment-template-test.php index b5064499063aec..500d3c7b665af6 100644 --- a/phpunit/blocks/render-comment-template-test.php +++ b/phpunit/blocks/render-comment-template-test.php @@ -86,6 +86,10 @@ public function test_rendering_comment_template_sets_comment_id_context() { ) ); $comment_author_name_block_markup = $comment_author_name_block->render(); + $this->assertNotEmpty( + $comment_author_name_block_markup, + 'Comment Author Name block rendered markup is empty.' + ); $render_block_callback = static function( $block_content, $block ) use ( $parsed_comment_author_name_block ) { // Insert a Comment Author Name block (which requires `commentId` @@ -111,6 +115,10 @@ 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 ); + $this->assertStringContainsString( + $comment_author_name_block_markup, + $markup, + "Rendered markup doesn't contain Comment Author Name block." + ); } }