From 77de774860638df07f5b128f69b0b5be3b916a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Tue, 26 Sep 2023 08:07:07 +0000 Subject: [PATCH] Blocks: Add more unit test covering edge cases for Block Hooks 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 --- tests/phpunit/tests/blocks/serialize.php | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/phpunit/tests/blocks/serialize.php b/tests/phpunit/tests/blocks/serialize.php index 3b237480c9231..12da03e3cd11f 100644 --- a/tests/phpunit/tests/blocks/serialize.php +++ b/tests/phpunit/tests/blocks/serialize.php @@ -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 = ''; + $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 = '
'; + $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 ); + } }