diff --git a/phpunit/blocks/render-block-table-of-contents-test.php b/phpunit/blocks/render-block-table-of-contents-test.php new file mode 100644 index 00000000000000..34ea7ff88e04fe --- /dev/null +++ b/phpunit/blocks/render-block-table-of-contents-test.php @@ -0,0 +1,112 @@ +post->create_and_get( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + + add_post_meta( + self::$page->ID, + 'table_of_contents', + '[{"content":"Heading text","level":2,"link":"#heading-text"},{"content":"A sub-heading","level":3,"link":"#a-sub-heading"}]', + true + ); + } + + public function set_up() { + parent::set_up(); + $this->original_block_supports = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = array( + 'attrs' => array(), + 'blockName' => 'core/table-of-contents', + ); + } + + public function tear_down() { + WP_Block_Supports::$block_to_render = $this->original_block_supports; + parent::tear_down(); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$page->ID, true ); + } + + /** + * @covers ::render_block_core_table_of_contents + */ + public function test_render_deprecated_content() { + $content = ''; + + $parsed_blocks = parse_blocks( + ' + + ' + ); + $block = new WP_Block( + $parsed_blocks[0], + array( + 'postId' => self::$page->ID, + 'postType' => self::$page->post_type, + ) + ); + + $new_content = gutenberg_render_block_core_table_of_contents( array(), $content, $block ); + $this->assertStringContainsString( + 'Heading text', + $new_content, + 'Failed to render a heading element' + ); + $this->assertStringContainsString( + 'A sub-heading', + $new_content, + 'Failed to render a sub-heading element' + ); + } + + /** + * @covers ::render_block_core_table_of_contents + */ + public function test_render_table_of_contents_from_meta() { + $parsed_blocks = parse_blocks( '' ); + $block = new WP_Block( + $parsed_blocks[0], + array( + 'postId' => self::$page->ID, + 'postType' => self::$page->post_type, + ) + ); + + $new_content = gutenberg_render_block_core_table_of_contents( array(), '', $block ); + $this->assertStringContainsString( + 'Heading text', + $new_content, + 'Failed to render a heading element from meta' + ); + $this->assertStringContainsString( + 'A sub-heading', + $new_content, + 'Failed to render a sub-heading element from meta' + ); + } +}