From d772493d65d39c0646122055532dfca03c6571ec Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Mon, 18 Sep 2023 10:44:29 +0200 Subject: [PATCH 1/4] Blocks: Introduce helper function to retrieve hooked blocks --- src/wp-includes/blocks.php | 21 ++++ tests/phpunit/tests/blocks/blockHooks.php | 120 ++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 tests/phpunit/tests/blocks/blockHooks.php diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 07bbd5ab01c19..bef4ea3bce6e8 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -739,6 +739,27 @@ function get_dynamic_block_names() { return $dynamic_block_names; } +/** + * Retrieves block types (and positions) hooked into the given block. + * + * @since 6.4.0 + * + * @param string $name Block type name including namespace. + * @return array Associative array of `$block_type_name => $position` pairs. + */ +function get_hooked_blocks( $name ) { + $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); + $hooked_blocks = array(); + foreach ( $block_types as $block_type ) { + foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) { + if ( $anchor_block_type === $name ) { + $hooked_blocks[ $block_type->name ] = $relative_position; + } + } + } + return $hooked_blocks; +} + /** * Given an array of attributes, returns a string in the serialized attributes * format prepared for post content. diff --git a/tests/phpunit/tests/blocks/blockHooks.php b/tests/phpunit/tests/blocks/blockHooks.php new file mode 100644 index 0000000000000..34b4d17670c66 --- /dev/null +++ b/tests/phpunit/tests/blocks/blockHooks.php @@ -0,0 +1,120 @@ +is_registered( $block_name ) ) { + $registry->unregister( $block_name ); + } + } + + parent::tear_down(); + } + + /** + * @ticket 59313 + * + * @covers ::get_hooked_blocks + */ + public function test_get_hooked_blocks_no_match_found() { + $result = get_hooked_blocks( 'tests/no-hooked-blocks' ); + + $this->assertSame( array(), $result ); + } + + /** + * @ticket 59313 + * + * @covers ::get_hooked_blocks + * + * @dataProvider data_block_hooked_blocks + * + * @param string $block_name Block name. + * @param array $expected Expected hooked blocks. + */ + public function test_get_hooked_blocks_matches_found( $block_name, $expected ) { + register_block_type( + 'tests/my-block', + array( + 'block_hooks' => array( + 'tests/hooked-before' => 'before', + 'tests/hooked-after' => 'after', + ), + ) + ); + register_block_type( + 'tests/my-container-block', + array( + 'block_hooks' => array( + 'tests/hooked-before' => 'before', + 'tests/hooked-after' => 'after', + 'tests/hooked-first-child' => 'first_child', + 'tests/hooked-last-child' => 'last_child', + ), + ) + ); + + $this->assertSame( + $expected, + get_hooked_blocks( $block_name ) + ); + } + + /** + * Data provider for hooked blocks. + * + * @return array { + * @type array { + * @type string Block name. + * @type array Expected hooked blocks. + * } + * } + */ + public function data_block_hooked_blocks() { + return array( + 'block hooked at the before position' => array( + 'tests/hooked-before', + array( + 'tests/my-block' => 'before', + 'tests/my-container-block' => 'before', + ), + ), + 'block hooked at the after position' => array( + 'tests/hooked-after', + array( + 'tests/my-block' => 'after', + 'tests/my-container-block' => 'after', + ), + ), + 'block hooked at the first child position' => array( + 'tests/hooked-first-child', + array( + 'tests/my-container-block' => 'first_child', + ), + ), + 'block hooked at the last child position' => array( + 'tests/hooked-last-child', + array( + 'tests/my-container-block' => 'last_child', + ), + ), + ); + } +} From d2cfe589b89c230e8b005fc73589adc2545fed22 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Mon, 18 Sep 2023 12:44:08 +0200 Subject: [PATCH 2/4] Address the feedback from code review --- tests/phpunit/tests/blocks/blockHooks.php | 70 ++++++++--------------- 1 file changed, 25 insertions(+), 45 deletions(-) diff --git a/tests/phpunit/tests/blocks/blockHooks.php b/tests/phpunit/tests/blocks/blockHooks.php index 34b4d17670c66..62c5457e5b329 100644 --- a/tests/phpunit/tests/blocks/blockHooks.php +++ b/tests/phpunit/tests/blocks/blockHooks.php @@ -43,13 +43,8 @@ public function test_get_hooked_blocks_no_match_found() { * @ticket 59313 * * @covers ::get_hooked_blocks - * - * @dataProvider data_block_hooked_blocks - * - * @param string $block_name Block name. - * @param array $expected Expected hooked blocks. */ - public function test_get_hooked_blocks_matches_found( $block_name, $expected ) { + public function test_get_hooked_blocks_matches_found() { register_block_type( 'tests/my-block', array( @@ -72,49 +67,34 @@ public function test_get_hooked_blocks_matches_found( $block_name, $expected ) { ); $this->assertSame( - $expected, - get_hooked_blocks( $block_name ) - ); - } - - /** - * Data provider for hooked blocks. - * - * @return array { - * @type array { - * @type string Block name. - * @type array Expected hooked blocks. - * } - * } - */ - public function data_block_hooked_blocks() { - return array( - 'block hooked at the before position' => array( - 'tests/hooked-before', - array( - 'tests/my-block' => 'before', - 'tests/my-container-block' => 'before', - ), + array( + 'tests/my-block' => 'before', + 'tests/my-container-block' => 'before', ), - 'block hooked at the after position' => array( - 'tests/hooked-after', - array( - 'tests/my-block' => 'after', - 'tests/my-container-block' => 'after', - ), + get_hooked_blocks( 'tests/hooked-before' ), + 'block hooked at the before position' + ); + $this->assertSame( + array( + 'tests/my-block' => 'after', + 'tests/my-container-block' => 'after', ), - 'block hooked at the first child position' => array( - 'tests/hooked-first-child', - array( - 'tests/my-container-block' => 'first_child', - ), + get_hooked_blocks( 'tests/hooked-after' ), + 'block hooked at the after position' + ); + $this->assertSame( + array( + 'tests/my-container-block' => 'first_child', ), - 'block hooked at the last child position' => array( - 'tests/hooked-last-child', - array( - 'tests/my-container-block' => 'last_child', - ), + get_hooked_blocks( 'tests/hooked-first-child' ), + 'block hooked at the first child position' + ); + $this->assertSame( + array( + 'tests/my-container-block' => 'last_child', ), + get_hooked_blocks( 'tests/hooked-last-child' ), + 'block hooked at the last child position' ); } } From cf7d12ff357ba9c0aee930e6cfe2fd68823d3194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Mon, 18 Sep 2023 14:07:50 +0200 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Bernie Reiter <96308+ockham@users.noreply.github.com> --- tests/phpunit/tests/blocks/blockHooks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/blockHooks.php b/tests/phpunit/tests/blocks/blockHooks.php index 62c5457e5b329..f5091ee21e3b7 100644 --- a/tests/phpunit/tests/blocks/blockHooks.php +++ b/tests/phpunit/tests/blocks/blockHooks.php @@ -29,7 +29,7 @@ public function tear_down() { } /** - * @ticket 59313 + * @ticket 59383 * * @covers ::get_hooked_blocks */ @@ -40,7 +40,7 @@ public function test_get_hooked_blocks_no_match_found() { } /** - * @ticket 59313 + * @ticket 59383 * * @covers ::get_hooked_blocks */ From 49f6bbad19d210e480349cda935acb9decff36ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Mon, 18 Sep 2023 14:08:57 +0200 Subject: [PATCH 4/4] Update tests/phpunit/tests/blocks/blockHooks.php --- tests/phpunit/tests/blocks/blockHooks.php | 36 +++++++++++------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/phpunit/tests/blocks/blockHooks.php b/tests/phpunit/tests/blocks/blockHooks.php index f5091ee21e3b7..2d5fbb6e9bd85 100644 --- a/tests/phpunit/tests/blocks/blockHooks.php +++ b/tests/phpunit/tests/blocks/blockHooks.php @@ -46,54 +46,54 @@ public function test_get_hooked_blocks_no_match_found() { */ public function test_get_hooked_blocks_matches_found() { register_block_type( - 'tests/my-block', + 'tests/injected-one', array( 'block_hooks' => array( - 'tests/hooked-before' => 'before', - 'tests/hooked-after' => 'after', + 'tests/hooked-at-before' => 'before', + 'tests/hooked-at-after' => 'after', ), ) ); register_block_type( - 'tests/my-container-block', + 'tests/injected-two', array( 'block_hooks' => array( - 'tests/hooked-before' => 'before', - 'tests/hooked-after' => 'after', - 'tests/hooked-first-child' => 'first_child', - 'tests/hooked-last-child' => 'last_child', + 'tests/hooked-at-before' => 'before', + 'tests/hooked-at-after' => 'after', + 'tests/hooked-at-first-child' => 'first_child', + 'tests/hooked-at-last-child' => 'last_child', ), ) ); $this->assertSame( array( - 'tests/my-block' => 'before', - 'tests/my-container-block' => 'before', + 'tests/injected-one' => 'before', + 'tests/injected-two' => 'before', ), - get_hooked_blocks( 'tests/hooked-before' ), + get_hooked_blocks( 'tests/hooked-at-before' ), 'block hooked at the before position' ); $this->assertSame( array( - 'tests/my-block' => 'after', - 'tests/my-container-block' => 'after', + 'tests/injected-one' => 'after', + 'tests/injected-two' => 'after', ), - get_hooked_blocks( 'tests/hooked-after' ), + get_hooked_blocks( 'tests/hooked-at-after' ), 'block hooked at the after position' ); $this->assertSame( array( - 'tests/my-container-block' => 'first_child', + 'tests/injected-two' => 'first_child', ), - get_hooked_blocks( 'tests/hooked-first-child' ), + get_hooked_blocks( 'tests/hooked-at-first-child' ), 'block hooked at the first child position' ); $this->assertSame( array( - 'tests/my-container-block' => 'last_child', + 'tests/injected-two' => 'last_child', ), - get_hooked_blocks( 'tests/hooked-last-child' ), + get_hooked_blocks( 'tests/hooked-at-last-child' ), 'block hooked at the last child position' ); }