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

Global styles revisions Controller: return single revision only when it matches the parent id #59049

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading