Skip to content

Commit

Permalink
Blocks: Add more unit test covering edge cases for Block Hooks
Browse files Browse the repository at this point in the history
These two new unit tests document how Block Hooks behave with `first_child` and `last_child` relative positions. The hooked blocks will only get inserted in the case where the parent block has at least one child block present. While it seems like a limitation, in practice, it's hard to think of a case where the template would use a parent block without its children. It's more likely to happen with patterns in general, but in the case of patterns wired with the block theme, it also seems unlikely. The reasoning here is that out of the box, the block theme should produce a fully functional and valid HTML.

Props ockham.
See #59313.
Follow-up [56649].



git-svn-id: https://develop.svn.wordpress.org/trunk@56701 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
gziolo committed Sep 26, 2023
1 parent 6a61084 commit 77de774
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/phpunit/tests/blocks/serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,40 @@ public function test_traverse_and_serialize_identity_from_parsed( $original ) {

$this->assertSame( $original, $actual );
}

/**
* @ticket 59313
*
* @covers ::traverse_and_serialize_blocks
*/
public function test_traverse_and_serialize_blocks_do_not_insert_in_void_block() {
$markup = '<!-- wp:void /-->';
$blocks = parse_blocks( $markup );

$actual = traverse_and_serialize_blocks(
$blocks,
array( __CLASS__, 'insert_next_to_child_blocks_callback' ),
array( __CLASS__, 'insert_next_to_child_blocks_callback' )
);

$this->assertSame( $markup, $actual );
}

/**
* @ticket 59313
*
* @covers ::traverse_and_serialize_blocks
*/
public function test_traverse_and_serialize_blocks_do_not_insert_in_empty_parent_block() {
$markup = '<!-- wp:outer --><div class="wp-block-outer"></div><!-- /wp:outer -->';
$blocks = parse_blocks( $markup );

$actual = traverse_and_serialize_blocks(
$blocks,
array( __CLASS__, 'insert_next_to_child_blocks_callback' ),
array( __CLASS__, 'insert_next_to_child_blocks_callback' )
);

$this->assertSame( $markup, $actual );
}
}

0 comments on commit 77de774

Please sign in to comment.