From bec65b2d1baa14322f25a5c18f525cfeff2c6340 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 15:23:27 +0600 Subject: [PATCH] Tests: Add unit tests for the server-side rendering of the Post Comments Count block. See #66541. --- .../tests/blocks/renderPostCommentsCount.php | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 tests/phpunit/tests/blocks/renderPostCommentsCount.php 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( '
3
', $this->render() ); + $this->assertSame( '
0
', $this->render( array(), array( 'postId' => self::$post_without_comments_id ) ), 'A post without comments should show zero.' ); + } + + /** + * Tests the class of the text alignment. + * + * @ticket 66541 + * + * @dataProvider data_text_alignments + * + * @param array $attributes Attributes of the block. + * @param string $expected Expected start of the output. + */ + public function test_should_add_the_class_of_the_text_alignment( $attributes, $expected ) { + $this->assertStringStartsWith( $expected, $this->render( $attributes ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_text_alignments() { + return array( + 'no alignment' => array( array(), '
' ), + 'center' => array( array( 'textAlign' => 'center' ), '
' ), + 'right' => array( array( 'textAlign' => 'right' ), '
' ), + ); + } + + /** + * Tests that the block is registered with the render callback. + * + * @ticket 66541 + * + * @covers ::register_block_core_post_comments_count + */ + public function test_should_register_the_block_with_the_render_callback() { + $registry = WP_Block_Type_Registry::get_instance(); + $was_registered = $registry->is_registered( 'core/post-comments-count' ); + + if ( $was_registered ) { + unregister_block_type( 'core/post-comments-count' ); + } + + register_block_core_post_comments_count(); + $block_type = $registry->get_registered( 'core/post-comments-count' ); + + if ( ! $was_registered ) { + unregister_block_type( 'core/post-comments-count' ); + } + + $this->assertInstanceOf( WP_Block_Type::class, $block_type ); + $this->assertSame( 'render_block_core_post_comments_count', $block_type->render_callback ); + } +}