Make WordPress Core


Ignore:
Timestamp:
09/18/2024 05:17:05 AM (19 months ago)
Author:
ramonopoly
Message:

Global Styles: allow read access to users with edit_posts capabilities

This patch any role that can edit a post, including custom post types, or edit theme options to read global styles from the API. This enables read-only access to global styles in the post editor. Test coverage in included.

Props ramonopoly, peterwilsoncc, mukesh27, aaronrobertshaw, mamaduka, spacedmonkey, talldanwp, timothyblynjacobs.
Fixes #62042.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php

    r58466 r59048  
    2020     * @var int
    2121     */
     22    protected static $editor_id;
     23
     24    /**
     25     * @var int
     26     */
    2227    protected static $subscriber_id;
     28
     29    /**
     30     * @var int
     31     */
     32    protected static $theme_manager_id;
    2333
    2434    /**
     
    5565        );
    5666
     67        self::$editor_id = $factory->user->create(
     68            array(
     69                'role' => 'editor',
     70            )
     71        );
     72
    5773        self::$subscriber_id = $factory->user->create(
    5874            array(
     
    6076            )
    6177        );
     78
     79        self::$theme_manager_id = $factory->user->create(
     80            array(
     81                'role' => 'subscriber',
     82            )
     83        );
     84
     85        // Add the 'edit_theme_options' capability to the theme manager (subscriber).
     86        $theme_manager_id = get_user_by( 'id', self::$theme_manager_id );
     87        if ( $theme_manager_id instanceof WP_User ) {
     88            $theme_manager_id->add_cap( 'edit_theme_options' );
     89        }
    6290
    6391        // This creates the global styles for the current theme.
     
    79107
    80108    /**
    81      *
     109     * Clean up after our tests run.
    82110     */
    83111    public static function wpTearDownAfterClass() {
    84112        self::delete_user( self::$admin_id );
     113        self::delete_user( self::$editor_id );
    85114        self::delete_user( self::$subscriber_id );
     115        self::delete_user( self::$theme_manager_id );
    86116    }
    87117
     
    265295        $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
    266296        $response = rest_get_server()->dispatch( $request );
    267         $this->assertErrorResponse( 'rest_cannot_manage_global_styles', $response, 401 );
     297        $this->assertErrorResponse( 'rest_cannot_read_global_styles', $response, 401 );
    268298    }
    269299
     
    271301     * @covers WP_REST_Global_Styles_Controller::get_theme_item
    272302     * @ticket 54516
    273      */
    274     public function test_get_theme_item_permission_check() {
     303     * @ticket 62042
     304     */
     305    public function test_get_theme_item_subscriber_permission_check() {
    275306        wp_set_current_user( self::$subscriber_id );
    276307        $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
    277308        $response = rest_get_server()->dispatch( $request );
    278         $this->assertErrorResponse( 'rest_cannot_manage_global_styles', $response, 403 );
     309        $this->assertErrorResponse( 'rest_cannot_read_global_styles', $response, 403 );
     310    }
     311
     312    /**
     313     * @covers WP_REST_Global_Styles_Controller::get_theme_item
     314     * @ticket 62042
     315     */
     316    public function test_get_theme_item_editor_permission_check() {
     317        wp_set_current_user( self::$editor_id );
     318        $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
     319        $response = rest_get_server()->dispatch( $request );
     320        // Checks that the response has the expected keys.
     321        $data  = $response->get_data();
     322        $links = $response->get_links();
     323        $this->assertArrayHasKey( 'settings', $data, 'Data does not have "settings" key' );
     324        $this->assertArrayHasKey( 'styles', $data, 'Data does not have "styles" key' );
     325        $this->assertArrayHasKey( 'self', $links, 'Links do not have a "self" key' );
     326    }
     327
     328    /**
     329     * @covers WP_REST_Global_Styles_Controller_Gutenberg::get_theme_item
     330     * @ticket 62042
     331     */
     332    public function test_get_theme_item_theme_options_manager_permission_check() {
     333        wp_set_current_user( self::$theme_manager_id );
     334        switch_theme( 'emptytheme' );
     335        $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/emptytheme' );
     336        $response = rest_get_server()->dispatch( $request );
     337        // Checks that the response has the expected keys.
     338        $data  = $response->get_data();
     339        $links = $response->get_links();
     340        $this->assertArrayHasKey( 'settings', $data, 'Data does not have "settings" key' );
     341        $this->assertArrayHasKey( 'styles', $data, 'Data does not have "styles" key' );
     342        $this->assertArrayHasKey( 'self', $links, 'Links do not have a "self" key' );
    279343    }
    280344
     
    608672     * of saving via the API.
    609673     *
    610      * @covers WP_REST_Global_Styles_Controller_Gutenberg::update_item
     674     * @covers WP_REST_Global_Styles_Controller::update_item
    611675     * @ticket 61312
    612676     * @ticket 61451
Note: See TracChangeset for help on using the changeset viewer.