Make WordPress Core

Ticket #42947: 42947.diff

File 42947.diff, 3.8 KB (added by audrasjb, 3 years ago)

patch refreshed against trunk

  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 068cc70bc2..3d4c0e4a01 100644
    a b class WP_REST_Posts_Controller extends WP_REST_Controller { 
    10541054        protected function prepare_items_query( $prepared_args = array(), $request = null ) {
    10551055                $query_args = array();
    10561056
     1057                if ( isset( $request['status'] ) ) {
     1058                        $query_args['perm'] = 'edit' === $request['context'] ? 'editable' : 'readable';
     1059                }
     1060
    10571061                foreach ( $prepared_args as $key => $value ) {
    10581062                        /**
    10591063                         * Filters the query_vars used in get_items() for the constructed query.
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index b35921a73d..a6dec7c595 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    51545154                $GLOBALS['wp_rest_server']->override_by_default = false;
    51555155        }
    51565156
     5157        /**
     5158         * @ticket 42947
     5159         * @dataProvider data_get_items_status_permissions
     5160         * @param string[] $grant_cap   Capability to grant the contributor.
     5161         * @param string   $context     The context to request the posts in.
     5162         * @param bool     $has_private Whether the other user's private post should be included.
     5163         * @param bool     $has_public  Whether the other user's public post should be included.
     5164         */
     5165        public function test_get_items_status_permissions( $grant_cap, $context, $has_private, $has_public ) {
     5166                $user = self::factory()->user->create_and_get( array( 'role' => 'contributor' ) );
     5167
     5168                foreach ( $grant_cap as $cap ) {
     5169                        $user->add_cap( $cap );
     5170                }
     5171
     5172                // Isolate to just these posts.
     5173                $tag       = self::factory()->term->create();
     5174                $a_private = self::factory()->post->create(
     5175                        array(
     5176                                'post_author' => self::$author_id,
     5177                                'post_status' => 'private',
     5178                                'tags_input'  => array( $tag ),
     5179                        )
     5180                );
     5181                $a_publish = self::factory()->post->create(
     5182                        array(
     5183                                'post_author' => self::$author_id,
     5184                                'post_status' => 'publish',
     5185                                'tags_input'  => array( $tag ),
     5186                        )
     5187                );
     5188                $c_private = self::factory()->post->create(
     5189                        array(
     5190                                'post_author' => $user->ID,
     5191                                'post_status' => 'private',
     5192                                'tags_input'  => array( $tag ),
     5193                        )
     5194                );
     5195                $c_publish = self::factory()->post->create(
     5196                        array(
     5197                                'post_author' => $user->ID,
     5198                                'post_status' => 'publish',
     5199                                'tags_input'  => array( $tag ),
     5200                        )
     5201                );
     5202
     5203                $expected = array( $c_private, $c_publish );
     5204
     5205                if ( $has_public ) {
     5206                        $expected[] = $a_publish;
     5207                }
     5208
     5209                if ( $has_private ) {
     5210                        $expected[] = $a_private;
     5211                }
     5212
     5213                $count = count( $expected );
     5214
     5215                wp_set_current_user( $user->ID );
     5216                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5217                $request->set_query_params(
     5218                        array(
     5219                                'status'   => 'publish,private',
     5220                                'per_page' => $count,
     5221                                'orderby'  => 'id',
     5222                                'context'  => $context,
     5223                                'tags'     => array( $tag ),
     5224                        )
     5225                );
     5226
     5227                $posts = rest_do_request( $request );
     5228                $this->assertNotWPError( $posts->as_error() );
     5229                $this->assertSameSets( $expected, wp_list_pluck( $posts->get_data(), 'id' ) );
     5230
     5231                $this->assertEquals( $count, $posts->get_headers()['X-WP-Total'] );
     5232        }
     5233
     5234        public function data_get_items_status_permissions() {
     5235                return array(
     5236                        array( array(), 'view', false, true ),
     5237                        array( array( 'read_private_posts' ), 'view', true, true ),
     5238                        array( array(), 'edit', false, false ),
     5239                        array( array( 'read_private_posts', 'edit_others_posts' ), 'edit', true, true ),
     5240                );
     5241        }
     5242
    51575243        public function tear_down() {
    51585244                if ( isset( $this->attachment_id ) ) {
    51595245                        $this->remove_added_uploads();