-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Blocks: Fix incorrect placement for hooked blocks in the parent conta…
…iner (#54349) * Blocks: Fix incorrect placement for hooked blocks in the parent container * Improve the way chunk index gets detected * Add unit tests covering hooking blocks at first and last child positions * Update lib/experimental/block-hooks.php Co-authored-by: Bernie Reiter <[email protected]> * Small tweaks after code review and rebasing with trunk. Props to @ockham for spotting issues. * Add tests covering the edge case where the container block does not have inner blocks * Improve test coverage for injecting hooked blocks --------- Co-authored-by: Bernie Reiter <[email protected]>
- Loading branch information
Showing
3 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "tests/hooked-block", | ||
"blockHooks": { | ||
"tests/group-first-child": "firstChild", | ||
"tests/group-last-child": "lastChild" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
use PHP_CodeSniffer\Generators\HTML; | ||
|
||
/** | ||
* Tests for hooked blocks rendering. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
* @since 6.4.0 | ||
* | ||
* @group blocks | ||
*/ | ||
class Tests_Blocks_RenderHookedBlocks extends WP_UnitTestCase { | ||
/** | ||
* @ticket 59313 | ||
*/ | ||
public function test_inject_hooked_block_at_first_child_position() { | ||
$content = <<<HTML | ||
<!-- wp:tests/group-first-child {"layout":{"type":"constrained"}} --> | ||
<div class="wp-block-group"> | ||
<!-- wp:paragraph --> | ||
<p>Foo</p> | ||
<!-- /wp:paragraph --> | ||
</div> | ||
<!-- /wp:tests/group-first-child --> | ||
HTML; | ||
|
||
$block_type = register_block_type( GUTENBERG_DIR_TESTFIXTURES . '/hooked-block/' ); | ||
$blocks = parse_blocks( $content ); | ||
$result = gutenberg_serialize_blocks( $blocks ); | ||
|
||
unregister_block_type( $block_type->name ); | ||
|
||
$expected_result = <<<HTML | ||
<!-- wp:tests/group-first-child {"layout":{"type":"constrained"}} --> | ||
<div class="wp-block-group"> | ||
<!-- wp:tests/hooked-block /--><!-- wp:paragraph --> | ||
<p>Foo</p> | ||
<!-- /wp:paragraph --> | ||
</div> | ||
<!-- /wp:tests/group-first-child --> | ||
HTML; | ||
$this->assertSame( $expected_result, $result ); | ||
} | ||
|
||
/** | ||
* @ticket 59313 | ||
*/ | ||
public function test_inject_hooked_block_at_first_child_position_no_inner_content() { | ||
$content = '<!-- wp:tests/group-first-child /-->'; | ||
|
||
$block_type = register_block_type( GUTENBERG_DIR_TESTFIXTURES . '/hooked-block/' ); | ||
$blocks = parse_blocks( $content ); | ||
$result = gutenberg_serialize_blocks( $blocks ); | ||
|
||
unregister_block_type( $block_type->name ); | ||
|
||
$expected_result = '<!-- wp:tests/group-first-child --><!-- wp:tests/hooked-block /--><!-- /wp:tests/group-first-child -->'; | ||
$this->assertSame( $expected_result, $result ); | ||
} | ||
|
||
/** | ||
* @ticket 59313 | ||
*/ | ||
public function test_inject_hooked_block_at_first_child_position_no_child_blocks() { | ||
$content = <<<HTML | ||
<!-- wp:tests/group-first-child --> | ||
<div class="wp-block-group"></div> | ||
<!-- /wp:tests/group-first-child --> | ||
HTML; | ||
|
||
$block_type = register_block_type( GUTENBERG_DIR_TESTFIXTURES . '/hooked-block/' ); | ||
$blocks = parse_blocks( $content ); | ||
$result = gutenberg_serialize_blocks( $blocks ); | ||
|
||
unregister_block_type( $block_type->name ); | ||
|
||
// @todo In the perfect world, the hooked block would be injected inside the `div` tag. | ||
$expected_result = <<<HTML | ||
<!-- wp:tests/group-first-child --><!-- wp:tests/hooked-block /--> | ||
<div class="wp-block-group"></div> | ||
<!-- /wp:tests/group-first-child --> | ||
HTML; | ||
$this->assertSame( $expected_result, $result ); | ||
} | ||
|
||
/** | ||
* @ticket 59313 | ||
*/ | ||
public function test_inject_hooked_block_at_last_child_position() { | ||
$content = <<<HTML | ||
<!-- wp:tests/group-last-child {"layout":{"type":"constrained"}} --> | ||
<div class="wp-block-group"> | ||
<!-- wp:paragraph --> | ||
<p>Foo</p> | ||
<!-- /wp:paragraph --> | ||
</div> | ||
<!-- /wp:tests/group-last-child --> | ||
HTML; | ||
|
||
$block_type = register_block_type( GUTENBERG_DIR_TESTFIXTURES . '/hooked-block/' ); | ||
$blocks = parse_blocks( $content ); | ||
$result = gutenberg_serialize_blocks( $blocks ); | ||
|
||
unregister_block_type( $block_type->name ); | ||
|
||
$expected_result = <<<HTML | ||
<!-- wp:tests/group-last-child {"layout":{"type":"constrained"}} --> | ||
<div class="wp-block-group"> | ||
<!-- wp:paragraph --> | ||
<p>Foo</p> | ||
<!-- /wp:paragraph --><!-- wp:tests/hooked-block /--> | ||
</div> | ||
<!-- /wp:tests/group-last-child --> | ||
HTML; | ||
$this->assertSame( $expected_result, $result ); | ||
} | ||
|
||
/** | ||
* @ticket 59313 | ||
*/ | ||
public function test_inject_hooked_block_at_last_child_position_no_inner_content() { | ||
$content = '<!-- wp:tests/group-last-child /-->'; | ||
|
||
$block_type = register_block_type( GUTENBERG_DIR_TESTFIXTURES . '/hooked-block/' ); | ||
$blocks = parse_blocks( $content ); | ||
$result = gutenberg_serialize_blocks( $blocks ); | ||
|
||
unregister_block_type( $block_type->name ); | ||
|
||
$expected_result = '<!-- wp:tests/group-last-child --><!-- wp:tests/hooked-block /--><!-- /wp:tests/group-last-child -->'; | ||
$this->assertSame( $expected_result, $result ); | ||
} | ||
|
||
/** | ||
* @ticket 59313 | ||
*/ | ||
public function test_inject_hooked_block_at_last_child_position_no_child_blocks() { | ||
$content = <<<HTML | ||
<!-- wp:tests/group-last-child --> | ||
<div class="wp-block-group"></div> | ||
<!-- /wp:tests/group-last-child --> | ||
HTML; | ||
|
||
$block_type = register_block_type( GUTENBERG_DIR_TESTFIXTURES . '/hooked-block/' ); | ||
$blocks = parse_blocks( $content ); | ||
$result = gutenberg_serialize_blocks( $blocks ); | ||
|
||
unregister_block_type( $block_type->name ); | ||
|
||
// @todo In the perfect world, the hooked block would be injected inside the `div` tag. | ||
$expected_result = <<<HTML | ||
<!-- wp:tests/group-last-child --> | ||
<div class="wp-block-group"></div> | ||
<!-- wp:tests/hooked-block /--><!-- /wp:tests/group-last-child --> | ||
HTML; | ||
$this->assertSame( $expected_result, $result ); | ||
} | ||
} |