diff --git a/tests/phpunit/tests/blocks/renderPostCommentsCount.php b/tests/phpunit/tests/blocks/renderPostCommentsCount.php new file mode 100644 index 0000000000000..d9c851b68f324 --- /dev/null +++ b/tests/phpunit/tests/blocks/renderPostCommentsCount.php @@ -0,0 +1,156 @@ +post->create(); + self::$post_without_comments_id = $factory->post->create(); + + $factory->comment->create_many( + 3, + array( + 'comment_post_ID' => self::$post_id, + 'comment_approved' => 1, + ) + ); + } + + public function set_up() { + parent::set_up(); + + $this->original_block_to_render = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = null; + } + + public function tear_down() { + WP_Block_Supports::$block_to_render = $this->original_block_to_render; + + parent::tear_down(); + } + + /** + * Renders the block. + * + * @param array $attributes Attributes of the block. + * @param array $context Context of the block, with the post by default. + * @return string|null The rendered block. + */ + private function render( $attributes = array(), $context = null ) { + $block = new WP_Block( + array( + 'blockName' => 'core/post-comments-count', + 'attrs' => $attributes, + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + + $block->context = null === $context ? array( 'postId' => self::$post_id ) : $context; + + return render_block_core_post_comments_count( $attributes, '', $block ); + } + + /** + * Tests that nothing is rendered without a post. + * + * @ticket 66541 + */ + public function test_should_render_nothing_without_a_post() { + $this->assertSame( '', $this->render( array(), array() ) ); + } + + /** + * Tests the number of comments. + * + * @ticket 66541 + */ + public function test_should_render_the_number_of_comments_of_the_post() { + $this->assertSame( '