From dda05b82fb7140786f257142de373bdbe55fa87c Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 16:38:10 +0600 Subject: [PATCH] Tests: Add unit tests for the server-side rendering of the Post Template block. See #66544. --- .../tests/blocks/renderPostTemplate.php | 550 ++++++++++++++++++ 1 file changed, 550 insertions(+) create mode 100644 tests/phpunit/tests/blocks/renderPostTemplate.php diff --git a/tests/phpunit/tests/blocks/renderPostTemplate.php b/tests/phpunit/tests/blocks/renderPostTemplate.php new file mode 100644 index 0000000000000..ed8ac5c668aa1 --- /dev/null +++ b/tests/phpunit/tests/blocks/renderPostTemplate.php @@ -0,0 +1,550 @@ + $title ) { + self::$post_ids[] = $factory->post->create( + array( + 'post_title' => $title, + 'post_date' => '2023-01-0' . ( $index + 1 ) . ' 10:00:00', + ) + ); + } + } + + public function set_up() { + parent::set_up(); + + $this->original_block_to_render = WP_Block_Supports::$block_to_render; + $this->original_get = $_GET; + WP_Block_Supports::$block_to_render = null; + + $this->was_registered = WP_Block_Type_Registry::get_instance()->is_registered( 'core/post-template' ); + + if ( ! $this->was_registered ) { + register_block_core_post_template(); + } + + register_block_type( + 'tests/post-info', + array( + 'uses_context' => array( 'postId', 'postType' ), + 'render_callback' => static function ( $attributes, $content, $block ) { + return sprintf( '[%d|%s|%s]', $block->context['postId'], $block->context['postType'], get_the_title( $block->context['postId'] ) ); + }, + ) + ); + } + + public function tear_down() { + $_GET = $this->original_get; + + unregister_block_type( 'tests/post-info' ); + + if ( ! $this->was_registered ) { + unregister_block_type( 'core/post-template' ); + } + + WP_Block_Supports::$block_to_render = $this->original_block_to_render; + + parent::tear_down(); + } + + /** + * Creates a parsed block. + * + * @param string $name Name of the block. + * @param array $attrs Attributes of the block. + * @param array $inner_blocks Parsed inner blocks. + * @return array The parsed block. + */ + private function parsed_block( $name, $attrs = array(), $inner_blocks = array() ) { + return array( + 'blockName' => $name, + 'attrs' => $attrs, + 'innerBlocks' => $inner_blocks, + 'innerHTML' => '', + 'innerContent' => array_fill( 0, count( $inner_blocks ), null ), + ); + } + + /** + * Gets the query of the block. + * + * @param array $query Values to use instead of the defaults. + * @return array The query. + */ + private function get_query( $query = array() ) { + return array_merge( + array( + 'perPage' => 10, + 'pages' => 0, + 'offset' => 0, + 'postType' => 'post', + 'order' => 'asc', + 'orderBy' => 'date', + 'inherit' => false, + ), + $query + ); + } + + /** + * Renders the block with a block that prints the post inside. + * + * @param array $attributes Attributes of the block. + * @param array $context Values to add to the context. + * @return string The rendered block. + */ + private function render( $attributes = array(), $context = array() ) { + $block = new WP_Block( + $this->parsed_block( 'core/post-template', $attributes, array( $this->parsed_block( 'tests/post-info' ) ) ), + array_merge( array( 'query' => $this->get_query() ), $context ) + ); + + $block->context = array_merge( array( 'query' => $this->get_query() ), $context ); + + return render_block_core_post_template( $attributes, '', $block ); + } + + /** + * Gets the list of the titles that are printed by the inner blocks. + * + * @param string $html Rendered block. + * @return string[] The titles. + */ + private function get_titles( $html ) { + preg_match_all( '/\[\d+\|[a-z_]+\|([^\]]*)\]/', $html, $matches ); + + return $matches[1]; + } + + /** + * Tests that a block is found in the inner blocks. + * + * @ticket 66544 + * + * @covers ::block_core_post_template_uses_featured_image + * + * @dataProvider data_inner_blocks + * + * @param array $inner_blocks Parsed inner blocks. + * @param bool $expected Whether the featured image is used. + */ + public function test_should_find_the_blocks_that_use_the_featured_image( $inner_blocks, $expected ) { + $block = new WP_Block( $this->parsed_block( 'core/post-template', array(), $inner_blocks ) ); + + $this->assertSame( $expected, block_core_post_template_uses_featured_image( $block->inner_blocks ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_inner_blocks() { + $block = static function ( $name, $attrs = array(), $inner = array() ) { + return array( + 'blockName' => $name, + 'attrs' => $attrs, + 'innerBlocks' => $inner, + 'innerHTML' => '', + 'innerContent' => array_fill( 0, count( $inner ), null ), + ); + }; + + return array( + 'no blocks' => array( array(), false ), + 'other blocks' => array( array( $block( 'core/post-title' ), $block( 'core/post-excerpt' ) ), false ), + 'a featured image' => array( array( $block( 'core/post-title' ), $block( 'core/post-featured-image' ) ), true ), + 'a cover' => array( array( $block( 'core/cover' ) ), false ), + 'a cover without the image' => array( array( $block( 'core/cover', array( 'useFeaturedImage' => false ) ) ), false ), + 'a cover with the image' => array( array( $block( 'core/cover', array( 'useFeaturedImage' => true ) ) ), true ), + 'a featured image that is inside' => array( array( $block( 'core/group', array(), array( $block( 'core/columns', array(), array( $block( 'core/post-featured-image' ) ) ) ) ) ), true ), + 'a cover that is inside' => array( array( $block( 'core/group', array(), array( $block( 'core/cover', array( 'useFeaturedImage' => true ) ) ) ) ), true ), + 'another block with the setting' => array( array( $block( 'core/group', array( 'useFeaturedImage' => true ) ) ), false ), + 'blocks that are inside' => array( array( $block( 'core/group', array(), array( $block( 'core/paragraph' ) ) ) ), false ), + ); + } + + /** + * Tests that nothing is rendered without posts. + * + * @ticket 66544 + * + * @covers ::render_block_core_post_template + */ + public function test_should_render_nothing_without_posts() { + $this->assertSame( '', $this->render( array(), array( 'query' => $this->get_query( array( 'postType' => 'page' ) ) ) ) ); + } + + /** + * Tests the list of the posts. + * + * @ticket 66544 + * + * @covers ::render_block_core_post_template + */ + public function test_should_render_a_list_item_for_each_post_with_the_inner_blocks() { + $output = $this->render(); + + $this->assertStringStartsWith( '', $output ); + $this->assertSame( array( 'First', 'Second', 'Third' ), $this->get_titles( $output ) ); + $this->assertSame( 3, substr_count( $output, '
  • \[/', $output ); + } + + /** + * Tests the number and the order of the posts. + * + * @ticket 66544 + * + * @covers ::render_block_core_post_template + * + * @dataProvider data_queries + * + * @param array $query Values of the query. + * @param string[] $expected Expected titles. + */ + public function test_should_use_the_query_of_the_block( $query, $expected ) { + $this->assertSame( $expected, $this->get_titles( $this->render( array(), array( 'query' => $this->get_query( $query ) ) ) ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_queries() { + return array( + 'oldest first' => array( array(), array( 'First', 'Second', 'Third' ) ), + 'newest first' => array( array( 'order' => 'desc' ), array( 'Third', 'Second', 'First' ) ), + 'two posts' => array( array( 'perPage' => 2 ), array( 'First', 'Second' ) ), + 'an offset' => array( + array( + 'perPage' => 2, + 'offset' => 1, + ), + array( 'Second', 'Third' ), + ), + ); + } + + /** + * Tests the page of the query. + * + * @ticket 66544 + * + * @covers ::render_block_core_post_template + * + * @dataProvider data_page_keys + * + * @param array $context Context of the block. + * @param string $key Key of the page in the request. + */ + public function test_should_use_the_page_of_the_request( $context, $key ) { + $_GET[ $key ] = '2'; + + $titles = $this->get_titles( $this->render( array(), array_merge( array( 'query' => $this->get_query( array( 'perPage' => 2 ) ) ), $context ) ) ); + + $this->assertSame( array( 'Third' ), $titles ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_page_keys() { + return array( + 'a query without an ID' => array( array(), 'query-page' ), + 'a query with an ID' => array( array( 'queryId' => 7 ), 'query-7-page' ), + ); + } + + /** + * Tests that the page of another query is ignored. + * + * @ticket 66544 + * + * @covers ::render_block_core_post_template + */ + public function test_should_ignore_the_page_of_another_query() { + $_GET['query-8-page'] = '2'; + + $titles = $this->get_titles( + $this->render( + array(), + array( + 'queryId' => 7, + 'query' => $this->get_query( array( 'perPage' => 2 ) ), + ) + ) + ); + + $this->assertSame( array( 'First', 'Second' ), $titles ); + } + + /** + * Tests the key of the items for the enhanced pagination. + * + * @ticket 66544 + * + * @covers ::render_block_core_post_template + */ + public function test_should_add_the_key_of_the_items_for_the_enhanced_pagination() { + $without = $this->render(); + $with = $this->render( array(), array( 'enhancedPagination' => true ) ); + + $this->assertStringNotContainsString( 'data-wp-key', $without ); + $this->assertStringContainsString( '
  • ', + ), + 'a list layout' => array( array(), array( 'displayLayout' => array( 'type' => 'list' ) ), '