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: ensure the revisions endpoint permissions match the global styles controller #50270

Merged
merged 4 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -265,10 +265,13 @@ public function get_item_permissions_check( $request ) {
return $post;
}

/*
* The same check as WP_REST_Global_Styles_Controller->get_item_permissions_check.
*/
Copy link
Member Author

Choose a reason for hiding this comment

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

Adding comment for context.

if ( ! current_user_can( 'read_post', $post->ID ) ) {
return new WP_Error(
'rest_cannot_view',
__( 'Sorry, you are not allowed to view revisions for this global style.', 'gutenberg' ),
__( 'Sorry, you are not allowed to view this global style.', 'gutenberg' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Just double-checking, the only intended change in this PR is the wording on this line + adding tests?

In the other PR it looks like a different check was being used. In this case, the current_user_can( 'read_post', $post->ID ) check appears to be consistent with WP_REST_Global_Styles_Controller 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry 🤦

The intention was to ensure the check was exactly the same as WP_REST_Global_Styles_Controller and I copied the translation over as well.

That means the only purpose of this PR is to add 1 x comment and add a test.

Thanks @andrewserong

Copy link
Contributor

Choose a reason for hiding this comment

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

All good, thanks for clarifying! 🙂

array( 'status' => rest_authorization_required_code() )
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class Gutenberg_REST_Global_Styles_Revisions_Controller_Test extends WP_Test_RES
*/
protected static $second_admin_id;

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

/**
* @var int
*/
Expand All @@ -37,6 +42,11 @@ public static function wpSetupBeforeClass( $factory ) {
'role' => 'administrator',
)
);
self::$author_id = $factory->user->create(
array(
'role' => 'author',
)
);
// This creates the global styles for the current theme.
self::$global_styles_id = wp_insert_post(
array(
Expand Down Expand Up @@ -160,6 +170,17 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'modified_gmt', $properties, 'Schema properties array does not have "modified_gmt" key' );
}

/**
* @covers Gutenberg_REST_Global_Styles_Revisions_Controller::get_item_permissions_check
*/
public function test_get_item_permissions_check() {
wp_set_current_user( self::$author_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );

$this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
}

/**
* @doesNotPerformAssertions
*/
Expand Down