post->create_and_get( array( 'post_type' => 'post', 'post_status' => 'publish', 'post_name' => 'metaldog', 'post_title' => 'Metal Dog', 'post_content' => 'Metal Dog content', 'post_excerpt' => 'Metal Dog', ) ); self::$other_post = self::factory()->post->create_and_get( array( 'post_type' => 'post', 'post_status' => 'publish', 'post_name' => 'ceilingcat', 'post_title' => 'Ceiling Cat', 'post_content' => 'Ceiling Cat content', 'post_excerpt' => 'Ceiling Cat', ) ); } public function test_rendering_post_template() { $parsed_blocks = parse_blocks( '' ); $block = new WP_Block( $parsed_blocks[0] ); $markup = $block->render(); $post_id = self::$post->ID; $other_post_id = self::$other_post->ID; $expected = <<
  • Ceiling Cat

    Ceiling Cat

  • Metal Dog

    Metal Dog

  • END; $this->assertSame( str_replace( array( "\n", "\t" ), '', $expected ), str_replace( array( "\n", "\t" ), '', $markup ) ); } /** * Tests that the `core/post-template` block triggers the main query loop when rendering within a corresponding * `core/query` block. */ public function test_rendering_post_template_with_main_query_loop() { global $wp_query, $wp_the_query; // Query block with post template block. $content = ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $expected = ''; // Set main query to single post. $wp_query = new WP_Query( array( 'p' => self::$post->ID ) ); $wp_the_query = $wp_query; // Register test block to log `in_the_loop()` results. $in_the_loop_logs = array(); register_block_type( 'test/in-the-loop-logger', array( 'render_callback' => static function () use ( &$in_the_loop_logs ) { $in_the_loop_logs[] = in_the_loop(); return ''; }, ) ); $output = do_blocks( $content ); $this->assertSame( $expected, $output, 'Unexpected parsed blocks content' ); $this->assertSame( array( true ), $in_the_loop_logs, 'Unexpected in_the_loop() result' ); } /** * Tests that the `core/post-template` block does not tamper with the main query loop when rendering within a single post * as the main query loop has already been started. In this case, the main query object needs to be cloned to * prevent an infinite loop. * Also tests that the default query returns posts of the 'post' post type when in a single post of any post type. */ public function test_rendering_post_template_with_main_query_loop_already_started() { global $wp_query, $wp_the_query; // Query block with post template block. $content = ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $expected = ''; // Update the post's content to have a query block for the same query as the main query. wp_update_post( array( 'ID' => self::$post->ID, 'post_content' => $content, 'post_content_filtered' => $content, ) ); // Set main query to single post. $wp_query = new WP_Query( array( 'p' => self::$post->ID ) ); $wp_the_query = $wp_query; // Get post content within main query loop. $output = ''; while ( $wp_query->have_posts() ) { $wp_query->the_post(); $output = get_echo( 'the_content' ); } $this->assertSame( $expected, $output, 'Unexpected parsed post content' ); } /** * Tests that the `core/post-template` block rewinds the default query when not in a single post of any post type. */ public function test_rendering_post_template_with_main_query_loop_not_single_post() { global $wp_query, $wp_the_query; // Query block with post template block. $content = ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $expected = ''; // Update the post's content to have a query block for the same query as the main query. wp_update_post( array( 'ID' => self::$post->ID, 'post_content' => $content, 'post_content_filtered' => $content, ) ); // Set main query to all posts. $wp_query = new WP_Query( array( 'post_type' => 'post' ) ); $wp_the_query = $wp_query; // Get post content within main query loop. $output = ''; while ( $wp_query->have_posts() ) { $wp_query->the_post(); $output = get_echo( 'the_content' ); } $this->assertSame( $expected, $output, 'Unexpected parsed post content' ); } }