Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Hooks: Return early from saving meta data for the navigation without a $post->ID #59875

Merged
merged 11 commits into from
Mar 15, 2024
8 changes: 8 additions & 0 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,14 @@ 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 ( empty( $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.
Expand Down
47 changes: 46 additions & 1 deletion phpunit/blocks/block-navigation-block-hooks-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
* @group blocks
*/
class Block_Navigation_Block_Hooks_Test extends WP_UnitTestCase {
/**
* @var int
*/
protected static $admin_id;

/**
* Original markup.
*
Expand All @@ -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 = '<!-- wp:navigation-link {"label":"News & About","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->';

self::$navigation_post = self::factory()->post->create_and_get(
Expand All @@ -41,6 +54,13 @@ public static function wpSetUpBeforeClass() {
);
}

/**
*
*/
public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );
}

/**
* Tear down each test method.
*/
Expand Down Expand Up @@ -92,4 +112,29 @@ 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
*/
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.' );
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
}

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(
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() );
$this->assertSame( 201, $response->get_status() );
}
}
Loading