Skip to content

Commit

Permalink
Add PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Sep 9, 2023
1 parent 705bdf6 commit 21f711f
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions phpunit/blocks/render-block-table-of-contents-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Table of Contents block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Table of Contents block.
*
* @group blocks
*/
class Tests_Blocks_Render_Table_Of_Contents extends WP_UnitTestCase {
/** @var WP_Post */
protected static $page;

/** @var array|null */
protected $original_block_supports;

public static function wpSetUpBeforeClass() {
self::$page = self::factory()->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 = '<nav class="wp-block-table-of-contents"><ol><li><a class="wp-block-table-of-contents__entry" href="#heading-text">Heading text</a><ol><li><a class="wp-block-table-of-contents__entry" href="#a-sub-heading">A sub-heading</a></li></ol></li></ol></nav>';

$parsed_blocks = parse_blocks(
'<!-- wp:table-of-contents {"headings":[{"content":"Heading text","level":2,"link":"#heading-text"},{"content":"A sub-heading","level":3,"link":"#a-sub-heading"}]} -->
<nav class="wp-block-table-of-contents"><ol><li><a class="wp-block-table-of-contents__entry" href="#heading-text">Heading text</a><ol><li><a class="wp-block-table-of-contents__entry" href="#a-sub-heading">A sub-heading</a></li></ol></li></ol></nav>
<!-- /wp:table-of-contents -->'
);
$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(
'<a class="wp-block-table-of-contents__entry" href="#heading-text">Heading text</a>',
$new_content,
'Failed to render a heading element'
);
$this->assertStringContainsString(
'<a class="wp-block-table-of-contents__entry" href="#a-sub-heading">A sub-heading</a>',
$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( '<!-- wp:table-of-contents /-->' );
$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(
'<a class="wp-block-table-of-contents__entry" href="#heading-text">Heading text</a>',
$new_content,
'Failed to render a heading element from meta'
);
$this->assertStringContainsString(
'<a class="wp-block-table-of-contents__entry" href="#a-sub-heading">A sub-heading</a>',
$new_content,
'Failed to render a sub-heading element from meta'
);
}
}

0 comments on commit 21f711f

Please sign in to comment.