From e4c1ff79d6af340e42db9ddc659fb751938691ca Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 23 May 2023 16:13:17 +0200 Subject: [PATCH] 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 ); + } +}