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( '