From fa739cdeec1f44279eebfa3fb10636eed054ee26 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 12:43:59 +0000 Subject: [PATCH 01/11] Return early if there is no -ID --- .../block-library/src/navigation/index.php | 6 ++++++ .../block-navigation-block-hooks-test.php | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index ca146868f4bbd..e8fdc8d5622d7 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -1512,6 +1512,12 @@ function block_core_navigation_set_ignored_hooked_blocks_metadata( $inner_blocks * @return stdClass The updated post object. */ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { + // In this scenario the user has likely tried to create a navigation via the REST API. + // In which case we won't have a post ID to work with and store meta against. + if ( ! isset( $post->ID ) ) { + return $post; + } + /* * We run the Block Hooks mechanism to inject the `metadata.ignoredHookedBlocks` attribute into * all anchor blocks. For the root level, we create a mock Navigation and extract them from there. diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index e0e7bd45cfc17..1bffa98f4f377 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -92,4 +92,22 @@ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_pres 'Block was not added to ignored hooked blocks metadata.' ); } + + /** + * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta being called via the REST API. + */ + public function test_block_core_navigation_rest_creation() { + wp_set_current_user( 1 ); + + $post_type_object = get_post_type_object( 'wp_navigation' ); + $request = new WP_REST_Request( 'POST', '/wp/v2/' . $post_type_object->rest_base ); + $request->set_param( 'title', 'Title ' . $post_type_object->label ); + $request->set_param( 'content', $post_type_object->label ); + $request->set_param( '_locale', 'user' ); + + $response = rest_get_server()->dispatch( $request ); // Triggers the error. + + $this->assertNotEmpty( $response->get_status() ); + $this->assertSame( 201, $response->get_status() ); + } } From 86b22e57c19498ac08a31bb42f31a0edbb203182 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 13:26:30 +0000 Subject: [PATCH 02/11] Update comment --- packages/block-library/src/navigation/index.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index e8fdc8d5622d7..48d9ca4ea2445 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -1512,8 +1512,10 @@ function block_core_navigation_set_ignored_hooked_blocks_metadata( $inner_blocks * @return stdClass The updated post object. */ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { - // In this scenario the user has likely tried to create a navigation via the REST API. - // In which case we won't have a post ID to work with and store meta against. + /* + * In this scenario the user has likely tried to create a navigation via the REST API. + * In which case we won't have a post ID to work with and store meta against. + */ if ( ! isset( $post->ID ) ) { return $post; } From 4b2f9f5c6975f403faee84f1696ba8d9c5d6d07f Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 13:31:14 +0000 Subject: [PATCH 03/11] Remove comment --- phpunit/blocks/block-navigation-block-hooks-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index 1bffa98f4f377..777c73ab22415 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -105,7 +105,7 @@ public function test_block_core_navigation_rest_creation() { $request->set_param( 'content', $post_type_object->label ); $request->set_param( '_locale', 'user' ); - $response = rest_get_server()->dispatch( $request ); // Triggers the error. + $response = rest_get_server()->dispatch( $request ); $this->assertNotEmpty( $response->get_status() ); $this->assertSame( 201, $response->get_status() ); From 12ee7e73d850e0c2e1cb5dd0637a89876fb10545 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 14:08:46 +0000 Subject: [PATCH 04/11] Update packages/block-library/src/navigation/index.php Co-authored-by: Bernie Reiter <96308+ockham@users.noreply.github.com> --- packages/block-library/src/navigation/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 48d9ca4ea2445..0bdad65201618 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -1513,9 +1513,9 @@ function block_core_navigation_set_ignored_hooked_blocks_metadata( $inner_blocks */ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { /* - * In this scenario the user has likely tried to create a navigation via the REST API. - * In which case we won't have a post ID to work with and store meta against. - */ + * In this scenario the user has likely tried to create a navigation via the REST API. + * In which case we won't have a post ID to work with and store meta against. + */ if ( ! isset( $post->ID ) ) { return $post; } From f3b6cb576d072c33dcebb1f78f4b384474e77fea Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 14:08:52 +0000 Subject: [PATCH 05/11] Update phpunit/blocks/block-navigation-block-hooks-test.php Co-authored-by: Bernie Reiter <96308+ockham@users.noreply.github.com> --- phpunit/blocks/block-navigation-block-hooks-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index 777c73ab22415..f5a751a876464 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -94,7 +94,7 @@ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_pres } /** - * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta being called via the REST API. + * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta */ public function test_block_core_navigation_rest_creation() { wp_set_current_user( 1 ); From 341d0ccbb0738e3e83df3ad03f382a5c091dba1e Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 14:20:08 +0000 Subject: [PATCH 06/11] Update tests --- phpunit/blocks/block-navigation-block-hooks-test.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index f5a751a876464..46c106de52d88 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -101,10 +101,13 @@ public function test_block_core_navigation_rest_creation() { $post_type_object = get_post_type_object( 'wp_navigation' ); $request = new WP_REST_Request( 'POST', '/wp/v2/' . $post_type_object->rest_base ); - $request->set_param( 'title', 'Title ' . $post_type_object->label ); - $request->set_param( 'content', $post_type_object->label ); - $request->set_param( '_locale', 'user' ); - + $request->set_body_params( + array( + 'title' => 'Title ' . $post_type_object->label, + 'content' => $post_type_object->label, + '_locale' => 'user', + ) + ); $response = rest_get_server()->dispatch( $request ); $this->assertNotEmpty( $response->get_status() ); From 32acb0573246ca21d17a8666769e48138a3f2dc2 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 14:58:32 +0000 Subject: [PATCH 07/11] Update packages/block-library/src/navigation/index.php Co-authored-by: Hugo Drelon <69580439+Hug0-Drelon@users.noreply.github.com> --- packages/block-library/src/navigation/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 0bdad65201618..568de8fb0edf4 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -1516,7 +1516,7 @@ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { * In this scenario the user has likely tried to create a navigation via the REST API. * In which case we won't have a post ID to work with and store meta against. */ - if ( ! isset( $post->ID ) ) { + if ( empty( $post->ID ) ) { return $post; } From 63a839290a42a1ea346655fc2022b84e0134c516 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 15:30:55 +0000 Subject: [PATCH 08/11] Correct admin user setup --- .../block-navigation-block-hooks-test.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index 46c106de52d88..695402a1c2a44 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -12,6 +12,11 @@ * @group blocks */ class Block_Navigation_Block_Hooks_Test extends WP_UnitTestCase { + /** + * @var int + */ + protected static $admin_id; + /** * Original markup. * @@ -28,8 +33,16 @@ class Block_Navigation_Block_Hooks_Test extends WP_UnitTestCase { /** * Setup method. + * + * * @param WP_UnitTest_Factory $factory Helper that lets us create fake data. */ - public static function wpSetUpBeforeClass() { + public static function wpSetUpBeforeClass( $factory ) { + self::$admin_id = $factory->user->create( + array( + 'role' => 'administrator', + ) + ); + //self::$original_markup = ''; self::$navigation_post = self::factory()->post->create_and_get( @@ -41,6 +54,13 @@ public static function wpSetUpBeforeClass() { ); } + /** + * + */ + public static function wpTearDownAfterClass() { + self::delete_user( self::$admin_id ); + } + /** * Tear down each test method. */ @@ -97,7 +117,7 @@ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_pres * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta */ public function test_block_core_navigation_rest_creation() { - wp_set_current_user( 1 ); + wp_set_current_user( self::$admin_id ); $post_type_object = get_post_type_object( 'wp_navigation' ); $request = new WP_REST_Request( 'POST', '/wp/v2/' . $post_type_object->rest_base ); From 4fdd98e981264f71c7c2642681c229bebaa74e6c Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 15:33:39 +0000 Subject: [PATCH 09/11] Skip tests for incompatible versions --- phpunit/blocks/block-navigation-block-hooks-test.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index 695402a1c2a44..118e7aa80cc82 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -117,6 +117,10 @@ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_pres * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta */ public function test_block_core_navigation_rest_creation() { + if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { + $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionalit.' ); + } + wp_set_current_user( self::$admin_id ); $post_type_object = get_post_type_object( 'wp_navigation' ); From 668812d019f5f1ceb50fb14c44e835d58ad2da9c Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 15:42:25 +0000 Subject: [PATCH 10/11] Refactor unit test --- .../block-navigation-block-hooks-test.php | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index 118e7aa80cc82..b0fc9c6ca60f9 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -79,7 +79,7 @@ public function tear_down() { */ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_preserves_entities() { if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { - $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionalit.' ); + $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' ); } register_block_type( @@ -116,25 +116,30 @@ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_pres /** * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta */ - public function test_block_core_navigation_rest_creation() { + public function test_block_core_navigation_dont_modify_no_post_id() { if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { - $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionalit.' ); + $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' ); } - wp_set_current_user( self::$admin_id ); - - $post_type_object = get_post_type_object( 'wp_navigation' ); - $request = new WP_REST_Request( 'POST', '/wp/v2/' . $post_type_object->rest_base ); - $request->set_body_params( + register_block_type( + 'tests/my-block', array( - 'title' => 'Title ' . $post_type_object->label, - 'content' => $post_type_object->label, - '_locale' => 'user', + 'block_hooks' => array( + 'core/navigation' => 'last_child', + ), ) ); - $response = rest_get_server()->dispatch( $request ); - $this->assertNotEmpty( $response->get_status() ); - $this->assertSame( 201, $response->get_status() ); + $original_markup = ''; + $post = new stdClass(); + $post->post_content = $original_markup; + + $post = gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta( $post ); + + $this->assertSame( + $original_markup, + $post->post_content, + 'Post content did not match the original markup.' + ); } } From c5b40828787ca16c340a9db51c724b6534f65658 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 15:44:38 +0000 Subject: [PATCH 11/11] Remove leftover code --- .../block-navigation-block-hooks-test.php | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index b0fc9c6ca60f9..ba30c2e26d25c 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -12,11 +12,6 @@ * @group blocks */ class Block_Navigation_Block_Hooks_Test extends WP_UnitTestCase { - /** - * @var int - */ - protected static $admin_id; - /** * Original markup. * @@ -33,16 +28,8 @@ class Block_Navigation_Block_Hooks_Test extends WP_UnitTestCase { /** * Setup method. - * - * * @param WP_UnitTest_Factory $factory Helper that lets us create fake data. */ - public static function wpSetUpBeforeClass( $factory ) { - self::$admin_id = $factory->user->create( - array( - 'role' => 'administrator', - ) - ); - + public static function wpSetUpBeforeClass() { //self::$original_markup = ''; self::$navigation_post = self::factory()->post->create_and_get( @@ -54,13 +41,6 @@ public static function wpSetUpBeforeClass( $factory ) { ); } - /** - * - */ - public static function wpTearDownAfterClass() { - self::delete_user( self::$admin_id ); - } - /** * Tear down each test method. */