Skip to content

Commit

Permalink
Reinstate edit_theme_options check and add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 18, 2024
1 parent 8caa9f5 commit 768770e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/class-wp-rest-global-styles-controller-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,6 @@ public function get_item_schema() {
public function get_theme_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
/*
* Verify if the current user has edit_posts capability.
* This capability is required to view global styles.
*/
if ( current_user_can( 'edit_posts' ) ) {
return true;
Expand All @@ -552,6 +551,13 @@ public function get_theme_item_permissions_check( $request ) { // phpcs:ignore V
}
}

/*
* Verify if the current user has edit_theme_options capability.
*/
if ( current_user_can( 'edit_theme_options' ) ) {
return true;
}

return new WP_Error(
'rest_cannot_read_global_styles',
__( 'Sorry, you are not allowed to access the global styles on this site.', 'gutenberg' ),
Expand Down
35 changes: 35 additions & 0 deletions phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class WP_REST_Global_Styles_Controller_Gutenberg_Test extends WP_Test_REST_Contr
*/
protected static $subscriber_id;

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

/**
* @var int
*/
Expand Down Expand Up @@ -61,6 +66,18 @@ public static function wpSetupBeforeClass( $factory ) {
)
);

self::$theme_manager_id = $factory->user->create(
array(
'role' => 'subscriber',
)
);

// Add the 'edit_theme_options' capability to the theme manager (subscriber).
$theme_manager_id = get_user_by('id', self::$theme_manager_id );

Check failure on line 76 in phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found
if ( $theme_manager_id instanceof WP_User ) {
$theme_manager_id->add_cap('edit_theme_options');

Check failure on line 78 in phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 78 in phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found
}

// This creates the global styles for the current theme.
self::$global_styles_id = $factory->post->create(
array(
Expand All @@ -83,7 +100,9 @@ public static function wpSetupBeforeClass( $factory ) {
*/
public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );
self::delete_user( self::$editor_id );
self::delete_user( self::$subscriber_id );
self::delete_user( self::$theme_manager_id );
}

/**
Expand Down Expand Up @@ -236,6 +255,22 @@ public function test_get_theme_item_editor_permission_check() {
$this->assertArrayHasKey( 'self', $links, 'Links do not have a "self" key' );
}

/**
* @covers WP_REST_Global_Styles_Controller_Gutenberg::get_theme_item
*/
public function test_get_theme_item_theme_options_manager_permission_check() {
wp_set_current_user( self::$theme_manager_id );
switch_theme( 'emptytheme' );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/emptytheme' );
$response = rest_get_server()->dispatch( $request );
// Checks that the response has the expected keys.
$data = $response->get_data();
$links = $response->get_links();
$this->assertArrayHasKey( 'settings', $data, 'Data does not have "settings" key' );
$this->assertArrayHasKey( 'styles', $data, 'Data does not have "styles" key' );
$this->assertArrayHasKey( 'self', $links, 'Links do not have a "self" key' );
}

/**
* @covers WP_REST_Global_Styles_Controller_Gutenberg::get_theme_item
*/
Expand Down

0 comments on commit 768770e

Please sign in to comment.