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
This is required to sync with WordPress/wordpress-develop#5655
  • Loading branch information
ramonjd committed Feb 15, 2024
1 parent dffac59 commit 5bc68dc
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

0 comments on commit 5bc68dc

Please sign in to comment.