Skip to content

Commit

Permalink
Backports from Core, the changes in WordPress/wordpress-develop#6108 (#…
Browse files Browse the repository at this point in the history
…59049)

This is required to sync with WordPress/wordpress-develop#5655

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
  • Loading branch information
3 people authored Feb 15, 2024
1 parent 626f7c7 commit ac607f4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public function get_item( $request ) {
return $revision;
}

if ( (int) $parent->ID !== (int) $revision->post_parent ) {
return new WP_Error(
'rest_revision_parent_id_mismatch',
/* translators: %d: A post id. */
sprintf( __( 'The revision does not belong to the specified parent with id of "%d"' ), $parent->ID ),
array( 'status' => 404 )
);
}

$response = $this->prepare_item_for_response( $revision, $request );
return rest_ensure_response( $response );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class Gutenberg_REST_Global_Styles_Revisions_Controller_Test extends WP_Test_RES
*/
protected static $global_styles_id;

/**
* @var int
*/
protected static $global_styles_id_2;

/**
* @var array
*/
Expand Down Expand Up @@ -56,6 +61,20 @@ public static function wpSetupBeforeClass( $factory ) {
)
);

// This creates another global styles post for the current theme.
self::$global_styles_id_2 = $factory->post->create(
array(
'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
'post_status' => 'publish',
'post_title' => __( 'Custom Styles', 'default' ),
'post_type' => 'wp_global_styles',
'post_name' => 'wp-global-styles-tt1-blocks-revisions-2',
'tax_input' => array(
'wp_theme' => 'tt1-blocks',
),
)
);

// Update post to create a new revisions.
$new_styles_post = array(
'ID' => self::$global_styles_id,
Expand Down Expand Up @@ -189,6 +208,36 @@ public function test_register_routes() {
);
}

/**
* @ticket 59810
*
* @covers WP_REST_Global_Styles_Controller::get_items
*/
public function test_get_item_valid_parent_id() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/' . $this->revision_1_id );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( self::$global_styles_id, $data['parent'], "The returned revision's id should match the parent id." );
$this->check_get_revision_response( $data, $this->revision_1 );
}

/**
* @ticket 59810
*
* @covers WP_REST_Global_Styles_Controller::get_items
*/
public function test_get_item_invalid_parent_id() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id_2 . '/revisions/' . $this->revision_1_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_revision_parent_id_mismatch', $response, 404 );

$expected_message = 'The revision does not belong to the specified parent with id of "' . self::$global_styles_id_2 . '"';
$this->assertSame( $expected_message, $response->as_error()->get_error_messages()[0], 'The message must contain the correct parent ID.' );
}

/**
* Utility function to check the items in WP_REST_Global_Styles_Controller::get_items
* against the expected values.
Expand Down

1 comment on commit ac607f4

@github-actions
Copy link

@github-actions github-actions bot commented on ac607f4 Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in ac607f4.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7911656008
📝 Reported issues:

Please sign in to comment.