Skip to content

Commit

Permalink
Add test coverage for _build_block_template_result_from_post
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jan 30, 2024
1 parent d99cd43 commit 9a122e3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/phpunit/tests/block-templates/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
'post_type' => 'wp_template',
'post_name' => 'my_template',
'post_title' => 'My Template',
'post_content' => 'Content',
'post_content' => '<!-- wp:heading {"level":1} --><h1>Template</h1><!-- /wp:heading -->' ,

Check failure on line 42 in tests/phpunit/tests/block-templates/base.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected no space between "'<!-- wp:heading {"level":1} --><h1>Template</h1><!-- /wp:heading -->'" and the comma. Found: 1 space
'post_excerpt' => 'Description of my template',
'tax_input' => array(
'wp_theme' => array(
Expand All @@ -57,7 +57,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
'post_type' => 'wp_template_part',
'post_name' => 'my_template_part',
'post_title' => 'My Template Part',
'post_content' => 'Content',
'post_content' => '<!-- wp:heading {"level":2} --><h2>Template Part</h2><!-- /wp:heading -->' ,

Check failure on line 60 in tests/phpunit/tests/block-templates/base.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected no space between "'<!-- wp:heading {"level":2} --><h2>Template Part</h2><!-- /wp:heading -->'" and the comma. Found: 1 space
'post_excerpt' => 'Description of my template part',
'tax_input' => array(
'wp_theme' => array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
*/
class Tests_Block_Templates_BuildBlockTemplateResultFromPost extends WP_Block_Templates_UnitTestCase {

/**
* Tear down each test method.
*
* @since 6.5.0
*/
public function tear_down() {
$registry = WP_Block_Type_Registry::get_instance();

if ( $registry->is_registered( 'tests/my-block' ) ) {
$registry->unregister( 'tests/my-block' );
}

parent::tear_down();
}

/**
* @ticket 54335
*/
Expand Down Expand Up @@ -49,4 +64,46 @@ public function test_should_build_template_part() {
$this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
$this->assertSame( self::$template_part_post->post_modified, $template_part->modified, 'Template part result properties match' );
}

/**
* @ticket 59646
*/
public function test_should_inject_hooked_block_into_template() {
register_block_type(
'tests/my-block',
array(
'block_hooks' => array(
'core/heading' => 'before',
),
)
);

$template = _build_block_template_result_from_post(
self::$template_post,
'wp_template'
);
$this->assertStringStartsWith( '<!-- wp:tests/my-block /-->', $template->content );
$this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template->content );
}

/**
* @ticket 59646
*/
public function test_should_inject_hooked_block_into_template_part() {
register_block_type(
'tests/my-block',
array(
'block_hooks' => array(
'core/heading' => 'after',
),
)
);

$template_part = _build_block_template_result_from_post(
self::$template_part_post,
'wp_template_part'
);
$this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
$this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template_part->content );
}
}

0 comments on commit 9a122e3

Please sign in to comment.