Skip to content

Commit

Permalink
Improve test coverage for injecting hooked blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Sep 14, 2023
1 parent c152238 commit b737b99
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/compat/wordpress-6.4/block-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ function gutenberg_insert_hooked_block( $inserted_block, $relative_position, $an
// Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`)
// when rendering blocks, we also need to correctly append a value (`null`, to mark a block
// location) to that array before the remaining HTML content for the inner blocks wrapper.
$chunk_index = 0;
for ( $index = count( $block['innerContent'] ) - 1; $index >= 0; $index-- ) {
if ( is_null( $block['innerContent'][ $index ] ) ) {
$chunk_index = count( $block['innerContent'] );
for ( $index = count( $block['innerContent'] ); $index > 0; $index-- ) {
if ( is_null( $block['innerContent'][ $index - 1 ] ) ) {
$chunk_index = $index;
break;
}
Expand Down
58 changes: 54 additions & 4 deletions phpunit/tests/blocks/renderHookedBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,44 @@ public function test_inject_hooked_block_at_first_child_position() {
$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 = '<!-- wp:tests/group-first-child {"layout":{"type":"constrained"}} /-->';
$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 );

$expected_result = '<!-- wp:tests/group-first-child {"layout":{"type":"constrained"}} --><!-- wp:tests/hooked-block /--><!-- /wp:tests/group-first-child -->';
// @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 );
}

Expand Down Expand Up @@ -92,19 +117,44 @@ public function test_inject_hooked_block_at_last_child_position() {
$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 = '<!-- wp:tests/group-last-child {"layout":{"type":"constrained"}} /-->';
$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 );

$expected_result = '<!-- wp:tests/group-last-child {"layout":{"type":"constrained"}} --><!-- wp:tests/hooked-block /--><!-- /wp:tests/group-last-child -->';
// @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 );
}
}

0 comments on commit b737b99

Please sign in to comment.