From 17c875c133685223805108ef4c2a385b71fd46aa Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 17 Feb 2025 16:44:35 +0100 Subject: [PATCH 1/2] Block Hooks: Add test coverage for child insertion --- .../tests/blocks/applyBlockHooksToContent.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/phpunit/tests/blocks/applyBlockHooksToContent.php b/tests/phpunit/tests/blocks/applyBlockHooksToContent.php index 22e3c6e8dffb5..dee5f9f60cc5a 100644 --- a/tests/phpunit/tests/blocks/applyBlockHooksToContent.php +++ b/tests/phpunit/tests/blocks/applyBlockHooksToContent.php @@ -28,6 +28,15 @@ public static function wpSetUpBeforeClass() { ) ); + register_block_type( + 'tests/hooked-block-as-last-child', + array( + 'block_hooks' => array( + 'tests/anchor-block-for-child-insertion' => 'last_child', + ), + ) + ); + register_block_type( 'tests/hooked-block-with-multiple-false', array( @@ -91,6 +100,21 @@ public function test_apply_block_hooks_to_content_inserts_hooked_block() { ); } + public function test_apply_block_hooks_to_content_inserts_hooked_block_as_last_child_if_sibling_is_present() { + $block_opener = ''; + $block_closer = ''; + $sibling_block = ''; + + $context = new WP_Block_Template(); + $context->content = $block_opener . $sibling_block . $block_closer; + + $actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' ); + $this->assertSame( + $block_opener . $sibling_block . '' . $block_closer, + $actual + ); + } + /** * @ticket 61074 */ From 6f893d04057abdb22ae9fd37a6b451377f3ec433 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 17 Feb 2025 16:48:45 +0100 Subject: [PATCH 2/2] Add test coverage for Classic block as sibling --- .../tests/blocks/applyBlockHooksToContent.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/phpunit/tests/blocks/applyBlockHooksToContent.php b/tests/phpunit/tests/blocks/applyBlockHooksToContent.php index dee5f9f60cc5a..963211ae4920a 100644 --- a/tests/phpunit/tests/blocks/applyBlockHooksToContent.php +++ b/tests/phpunit/tests/blocks/applyBlockHooksToContent.php @@ -68,6 +68,7 @@ public static function wpTearDownAfterClass() { $registry = WP_Block_Type_Registry::get_instance(); $registry->unregister( 'tests/hooked-block' ); + $registry->unregister( 'tests/hooked-block-as-last-child' ); $registry->unregister( 'tests/hooked-block-with-multiple-false' ); $registry->unregister( 'tests/dynamically-hooked-block-with-multiple-false' ); } @@ -115,6 +116,21 @@ public function test_apply_block_hooks_to_content_inserts_hooked_block_as_last_c ); } + public function test_apply_block_hooks_to_content_inserts_hooked_block_as_last_child_if_sibling_is_classic_block() { + $block_opener = ''; + $block_closer = ''; + $sibling_block = '

Hello World!

'; + + $context = new WP_Block_Template(); + $context->content = $block_opener . $sibling_block . $block_closer; + + $actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' ); + $this->assertSame( + $block_opener . $sibling_block . '' . $block_closer, + $actual + ); + } + /** * @ticket 61074 */