diff --git a/tests/phpunit/tests/blocks/renderQueryNoResults.php b/tests/phpunit/tests/blocks/renderQueryNoResults.php new file mode 100644 index 0000000000000..4f9d6d3dfeb33 --- /dev/null +++ b/tests/phpunit/tests/blocks/renderQueryNoResults.php @@ -0,0 +1,241 @@ +original_block_to_render = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = null; + $this->original_get = $_GET; + } + + public function tear_down() { + WP_Block_Supports::$block_to_render = $this->original_block_to_render; + $_GET = $this->original_get; + + parent::tear_down(); + } + + /** + * Renders the block. + * + * @param array $attributes Attributes of the block. + * @param string $content Content of the block. + * @param array $query The query of the block. Default is the posts that have a title with the word `Findable`. + * @param array $context Optional. Other context of the block. Default none. + * @return string The rendered block. + */ + private function render( $attributes = array(), $content = '
Nothing here.
', $query = array( 'search' => 'Findable' ), $context = array() ) { + $context['query'] = array_merge( + array( + 'perPage' => 5, + 'postType' => 'post', + 'inherit' => false, + ), + $query + ); + + $block = new WP_Block( + array( + 'blockName' => 'core/query-no-results', + 'attrs' => $attributes, + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ), + $context + ); + + return render_block_core_query_no_results( $attributes, $content, $block ); + } + + /** + * Tests that the content is rendered when the query has no posts. + * + * @ticket 66569 + */ + public function test_should_render_the_content_when_the_query_has_no_posts() { + $this->assertSame( 'Nothing here.
Nothing here.
Nothing here.
Nothing here.
Nothing here.
', + array( + 'search' => 'Findable', + 'perPage' => 2, + ), + array( 'queryId' => 3 ) + ), + 'The second page of two posts should have none.' + ); + + $_GET['query-3-page'] = 1; + + $this->assertSame( + '', + $this->render( + array(), + 'Nothing here.
', + array( + 'search' => 'Findable', + 'perPage' => 2, + ), + array( 'queryId' => 3 ) + ), + 'The first page should have posts.' + ); + } + + /** + * Tests that the query of the page is used when the block inherits it, and has no posts. + * + * @ticket 66569 + */ + public function test_should_use_the_query_of_the_page_when_it_has_no_posts() { + // A query of its own would find this post, and the query of the page does not. + self::factory()->post->create( array( 'post_title' => 'Other post' ) ); + + $this->go_to( '/?s=nothingmatchesthis' ); + + $this->assertSame( 'Nothing here.
Nothing here.
', array( 'inherit' => true ) ) ); + } + + /** + * Tests that the query of the page is used when the block inherits it, and has posts. + * + * @ticket 66569 + */ + public function test_should_render_nothing_when_the_query_of_the_page_has_posts() { + self::factory()->post->create( array( 'post_title' => 'Findable post' ) ); + + $this->go_to( '/?s=Findable' ); + + $this->assertSame( '', $this->render( array(), 'Nothing here.
', array( 'inherit' => true ) ) ); + } + + /** + * Tests that the block is registered with the render callback. + * + * @ticket 66569 + * + * @covers ::register_block_core_query_no_results + */ + public function test_should_register_the_block_with_the_render_callback() { + $registry = WP_Block_Type_Registry::get_instance(); + $original_block = $registry->get_registered( 'core/query-no-results' ); + + if ( $original_block ) { + unregister_block_type( 'core/query-no-results' ); + } + + try { + register_block_core_query_no_results(); + $block_type = $registry->get_registered( 'core/query-no-results' ); + + $this->assertInstanceOf( WP_Block_Type::class, $block_type ); + $this->assertSame( 'render_block_core_query_no_results', $block_type->render_callback ); + } finally { + unregister_block_type( 'core/query-no-results' ); + + if ( $original_block ) { + $registry->register( $original_block ); + } + } + } +}