From b737b990ef6a012fc48c14a79b4089a5ea25370e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Zi=C3=B3=C5=82kowski?= Date: Thu, 14 Sep 2023 09:37:31 +0200 Subject: [PATCH] Improve test coverage for injecting hooked blocks --- lib/compat/wordpress-6.4/block-hooks.php | 6 +-- phpunit/tests/blocks/renderHookedBlocks.php | 58 +++++++++++++++++++-- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-6.4/block-hooks.php b/lib/compat/wordpress-6.4/block-hooks.php index ba48096045cc81..6fd7d4a4af1b4d 100644 --- a/lib/compat/wordpress-6.4/block-hooks.php +++ b/lib/compat/wordpress-6.4/block-hooks.php @@ -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; } diff --git a/phpunit/tests/blocks/renderHookedBlocks.php b/phpunit/tests/blocks/renderHookedBlocks.php index f0081eb0f19331..de256a0ec2be19 100644 --- a/phpunit/tests/blocks/renderHookedBlocks.php +++ b/phpunit/tests/blocks/renderHookedBlocks.php @@ -44,11 +44,31 @@ 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 = ''; + + $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 = ''; + $this->assertSame( $expected_result, $result ); + } + /** * @ticket 59313 */ public function test_inject_hooked_block_at_first_child_position_no_child_blocks() { - $content = ''; + $content = << +
+ +HTML; $block_type = register_block_type( GUTENBERG_DIR_TESTFIXTURES . '/hooked-block/' ); $blocks = parse_blocks( $content ); @@ -56,7 +76,12 @@ public function test_inject_hooked_block_at_first_child_position_no_child_blocks unregister_block_type( $block_type->name ); - $expected_result = ''; + // @todo In the perfect world, the hooked block would be injected inside the `div` tag. + $expected_result = << +
+ +HTML; $this->assertSame( $expected_result, $result ); } @@ -92,11 +117,31 @@ 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 = ''; + + $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 = ''; + $this->assertSame( $expected_result, $result ); + } + /** * @ticket 59313 */ public function test_inject_hooked_block_at_last_child_position_no_child_blocks() { - $content = ''; + $content = << +
+ +HTML; $block_type = register_block_type( GUTENBERG_DIR_TESTFIXTURES . '/hooked-block/' ); $blocks = parse_blocks( $content ); @@ -104,7 +149,12 @@ public function test_inject_hooked_block_at_last_child_position_no_child_blocks( unregister_block_type( $block_type->name ); - $expected_result = ''; + // @todo In the perfect world, the hooked block would be injected inside the `div` tag. + $expected_result = << +
+ +HTML; $this->assertSame( $expected_result, $result ); } }