Make WordPress Core

Ticket #40510: 40510.6.diff

File 40510.6.diff, 29.6 KB (added by flixos90, 7 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

     
    876876                         *
    877877                         * @param string $value The query_var value.
    878878                         */
    879                         $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value );
     879                        $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
    880880                }
    881881
    882882                if ( 'post' !== $this->post_type || ! isset( $query_args['ignore_sticky_posts'] ) ) {
  • src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

     
    205205                        return $parent;
    206206                }
    207207
    208                 $revisions = wp_get_post_revisions( $request['parent'] );
     208                // Ensure a search string is set in case the orderby is set to 'relevance'.
     209                if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
     210                        return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) );
     211                }
     212
     213                // Ensure an include parameter is set in case the orderby is set to 'include'.
     214                if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
     215                        return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.' ), array( 'status' => 400 ) );
     216                }
     217
     218                if ( wp_revisions_enabled( $parent ) ) {
     219                        $registered = $this->get_collection_params();
     220                        $args       = array(
     221                                'post_parent'      => $parent->ID,
     222                                'post_type'        => 'revision',
     223                                'post_status'      => 'inherit',
     224                                'posts_per_page'   => -1,
     225                                'orderby'          => 'date ID',
     226                                'order'            => 'DESC',
     227                                'suppress_filters' => true,
     228                        );
     229
     230                        $parameter_mappings = array(
     231                                'exclude'  => 'post__not_in',
     232                                'include'  => 'post__in',
     233                                'offset'   => 'offset',
     234                                'order'    => 'order',
     235                                'orderby'  => 'orderby',
     236                                'page'     => 'paged',
     237                                'per_page' => 'posts_per_page',
     238                                'search'   => 's',
     239                        );
     240
     241                        foreach ( $parameter_mappings as $api_param => $wp_param ) {
     242                                if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
     243                                        $args[ $wp_param ] = $request[ $api_param ];
     244                                }
     245                        }
     246
     247                        // For backward-compatibility, 'date' needs to resolve to 'date ID'.
     248                        if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) {
     249                                $args['orderby'] = 'date ID';
     250                        }
     251
     252                        /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
     253                        $args       = apply_filters( 'rest_revision_query', $args, $request );
     254                        $query_args = $this->prepare_items_query( $args, $request );
     255
     256                        $revisions_query = new WP_Query();
     257                        $revisions       = $revisions_query->query( $query_args );
     258                        $offset          = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
     259                        $page            = (int) $query_args['paged'];
     260                        $total_revisions = $revisions_query->found_posts;
     261
     262                        if ( $total_revisions < 1 ) {
     263                                // Out-of-bounds, run the query again without LIMIT for total count.
     264                                unset( $query_args['paged'], $query_args['offset'] );
     265
     266                                $count_query = new WP_Query();
     267                                $count_query->query( $query_args );
     268
     269                                $total_revisions = $count_query->found_posts;
     270                        }
     271
     272                        if ( $revisions_query->query_vars['posts_per_page'] > 0 ) {
     273                                $max_pages = ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] );
     274                        } else {
     275                                $max_pages = $total_revisions > 0 ? 1 : 0;
     276                        }
     277
     278                        if ( $total_revisions > 0 ) {
     279                                if ( $offset >= $total_revisions ) {
     280                                        return new WP_Error( 'rest_revision_invalid_offset_number', __( 'The offset number requested is larger than or equal to the number of available revisions.' ), array( 'status' => 400 ) );
     281                                } elseif ( ! $offset && $page > $max_pages ) {
     282                                        return new WP_Error( 'rest_revision_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) );
     283                                }
     284                        }
     285                } else {
     286                        $revisions       = array();
     287                        $total_revisions = 0;
     288                        $max_pages       = 0;
     289                        $page            = (int) $request['page'];
     290                }
    209291
    210292                $response = array();
    211293                foreach ( $revisions as $revision ) {
    212294                        $data       = $this->prepare_item_for_response( $revision, $request );
    213295                        $response[] = $this->prepare_response_for_collection( $data );
    214296                }
    215                 return rest_ensure_response( $response );
     297
     298                $response = rest_ensure_response( $response );
     299
     300                $response->header( 'X-WP-Total', (int) $total_revisions );
     301                $response->header( 'X-WP-TotalPages', (int) $max_pages );
     302
     303                $request_params = $request->get_query_params();
     304                $base           = add_query_arg( $request_params, rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ) );
     305
     306                if ( $page > 1 ) {
     307                        $prev_page = $page - 1;
     308
     309                        if ( $prev_page > $max_pages ) {
     310                                $prev_page = $max_pages;
     311                        }
     312
     313                        $prev_link = add_query_arg( 'page', $prev_page, $base );
     314                        $response->link_header( 'prev', $prev_link );
     315                }
     316                if ( $max_pages > $page ) {
     317                        $next_page = $page + 1;
     318                        $next_link = add_query_arg( 'page', $next_page, $base );
     319
     320                        $response->link_header( 'next', $next_link );
     321                }
     322
     323                return $response;
    216324        }
    217325
    218326        /**
     
    331439        }
    332440
    333441        /**
     442         * Determines the allowed query_vars for a get_items() response and prepares
     443         * them for WP_Query.
     444         *
     445         * @since 5.0.0
     446         *
     447         * @param array           $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
     448         * @param WP_REST_Request $request       Optional. Full details about the request.
     449         * @return array Items query arguments.
     450         */
     451        protected function prepare_items_query( $prepared_args = array(), $request = null ) {
     452                $query_args = array();
     453
     454                foreach ( $prepared_args as $key => $value ) {
     455                        /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
     456                        $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
     457                }
     458
     459                // Map to proper WP_Query orderby param.
     460                if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
     461                        $orderby_mappings = array(
     462                                'id'            => 'ID',
     463                                'include'       => 'post__in',
     464                                'slug'          => 'post_name',
     465                                'include_slugs' => 'post_name__in',
     466                        );
     467
     468                        if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
     469                                $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
     470                        }
     471                }
     472
     473                return $query_args;
     474        }
     475
     476        /**
    334477         * Prepares the revision for the REST response.
    335478         *
    336479         * @since 4.7.0
     
    550693         * @return array Collection parameters.
    551694         */
    552695        public function get_collection_params() {
    553                 return array(
    554                         'context' => $this->get_context_param( array( 'default' => 'view' ) ),
     696                $query_params = parent::get_collection_params();
     697
     698                $query_params['context']['default'] = 'view';
     699
     700                unset( $query_params['per_page']['default'] );
     701
     702                $query_params['exclude'] = array(
     703                        'description' => __( 'Ensure result set excludes specific IDs.' ),
     704                        'type'        => 'array',
     705                        'items'       => array(
     706                                'type' => 'integer',
     707                        ),
     708                        'default'     => array(),
    555709                );
     710
     711                $query_params['include'] = array(
     712                        'description' => __( 'Limit result set to specific IDs.' ),
     713                        'type'        => 'array',
     714                        'items'       => array(
     715                                'type' => 'integer',
     716                        ),
     717                        'default'     => array(),
     718                );
     719
     720                $query_params['offset'] = array(
     721                        'description' => __( 'Offset the result set by a specific number of items.' ),
     722                        'type'        => 'integer',
     723                );
     724
     725                $query_params['order'] = array(
     726                        'description' => __( 'Order sort attribute ascending or descending.' ),
     727                        'type'        => 'string',
     728                        'default'     => 'desc',
     729                        'enum'        => array( 'asc', 'desc' ),
     730                );
     731
     732                $query_params['orderby'] = array(
     733                        'description' => __( 'Sort collection by object attribute.' ),
     734                        'type'        => 'string',
     735                        'default'     => 'date',
     736                        'enum'        => array(
     737                                'date',
     738                                'id',
     739                                'include',
     740                                'relevance',
     741                                'slug',
     742                                'include_slugs',
     743                                'title',
     744                        ),
     745                );
     746
     747                return $query_params;
    556748        }
    557749
    558750        /**
  • tests/phpunit/tests/rest-api/rest-revisions-controller.php

     
    77 */
    88
    99/**
    10  * @group restapi
     10 * @group restapi-revisions
    1111 */
    1212class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase {
    1313        protected static $post_id;
     
    4444                                'ID'           => self::$post_id,
    4545                        )
    4646                );
     47                wp_update_post(
     48                        array(
     49                                'post_content' => 'This content is fantastic.',
     50                                'ID'           => self::$post_id,
     51                        )
     52                );
    4753                wp_set_current_user( 0 );
    4854        }
    4955
     
    5965        public function setUp() {
    6066                parent::setUp();
    6167
    62                 $revisions          = wp_get_post_revisions( self::$post_id );
    63                 $this->revision_1   = array_pop( $revisions );
    64                 $this->revision_id1 = $this->revision_1->ID;
    65                 $this->revision_2   = array_pop( $revisions );
    66                 $this->revision_id2 = $this->revision_2->ID;
     68                $revisions             = wp_get_post_revisions( self::$post_id );
     69                $this->total_revisions = count( $revisions );
     70                $this->revisions       = $revisions;
     71                $this->revision_1      = array_pop( $revisions );
     72                $this->revision_id1    = $this->revision_1->ID;
     73                $this->revision_2      = array_pop( $revisions );
     74                $this->revision_id2    = $this->revision_2->ID;
     75                $this->revision_3      = array_pop( $revisions );
     76                $this->revision_id3    = $this->revision_3->ID;
    6777        }
    6878
    6979        public function test_register_routes() {
     
    95105                $response = rest_get_server()->dispatch( $request );
    96106                $data     = $response->get_data();
    97107                $this->assertEquals( 200, $response->get_status() );
    98                 $this->assertCount( 2, $data );
     108                $this->assertCount( $this->total_revisions, $data );
    99109
    100110                // Reverse chron
    101                 $this->assertEquals( $this->revision_id2, $data[0]['id'] );
    102                 $this->check_get_revision_response( $data[0], $this->revision_2 );
     111                $this->assertEquals( $this->revision_id3, $data[0]['id'] );
     112                $this->check_get_revision_response( $data[0], $this->revision_3 );
     113
     114                $this->assertEquals( $this->revision_id2, $data[1]['id'] );
     115                $this->check_get_revision_response( $data[1], $this->revision_2 );
    103116
    104                 $this->assertEquals( $this->revision_id1, $data[1]['id'] );
    105                 $this->check_get_revision_response( $data[1], $this->revision_1 );
     117                $this->assertEquals( $this->revision_id1, $data[2]['id'] );
     118                $this->check_get_revision_response( $data[2], $this->revision_1 );
    106119        }
    107120
    108121        public function test_get_items_no_permission() {
     
    382395                $this->assertEquals( $parent_post_id, self::$post_id );
    383396        }
    384397
     398        /**
     399         * Test the pagination header of the first page.
     400         *
     401         * @ticket 40510
     402         */
     403        public function test_get_items_pagination_header_of_the_first_page() {
     404                wp_set_current_user( self::$editor_id );
     405
     406                $rest_route  = '/wp/v2/posts/' . self::$post_id . '/revisions';
     407                $per_page    = 2;
     408                $total_pages = (int) ceil( $this->total_revisions / $per_page );
     409                $page        = 1;  // First page.
     410
     411                $request = new WP_REST_Request( 'GET', $rest_route );
     412                $request->set_query_params( array(
     413                        'per_page' => $per_page,
     414                        'page'     => $page,
     415                ));
     416                $response = rest_get_server()->dispatch( $request );
     417                $headers  = $response->get_headers();
     418                $this->assertSame( $this->total_revisions, $headers['X-WP-Total'] );
     419                $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
     420                $next_link = add_query_arg(
     421                        array(
     422                                'per_page' => $per_page,
     423                                'page'     => $page + 1,
     424                        ),
     425                        rest_url( $rest_route )
     426                );
     427                $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
     428                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
     429        }
     430
     431        /**
     432         * Test the pagination header of the last page.
     433         *
     434         * @ticket 40510
     435         */
     436        public function test_get_items_pagination_header_of_the_last_page() {
     437                wp_set_current_user( self::$editor_id );
     438
     439                $rest_route  = '/wp/v2/posts/' . self::$post_id . '/revisions';
     440                $per_page    = 2;
     441                $total_pages = (int) ceil( $this->total_revisions / $per_page );
     442                $page        = 2;  // Last page.
     443
     444                $request = new WP_REST_Request( 'GET', $rest_route );
     445                $request->set_query_params( array(
     446                        'per_page' => $per_page,
     447                        'page'     => $page,
     448                ));
     449                $response = rest_get_server()->dispatch( $request );
     450                $headers  = $response->get_headers();
     451                $this->assertSame( $this->total_revisions, $headers['X-WP-Total'] );
     452                $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
     453                $prev_link = add_query_arg(
     454                        array(
     455                                'per_page' => $per_page,
     456                                'page'     => $page - 1,
     457                        ), rest_url( $rest_route )
     458                );
     459                $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
     460        }
     461
     462        /**
     463         * Test that invalid 'per_page' query should error.
     464         *
     465         * @ticket 40510
     466         */
     467        public function test_get_items_invalid_per_page_should_error() {
     468                wp_set_current_user( self::$editor_id );
     469
     470                $per_page        = -1; // Invalid number.
     471                $expected_error  = 'rest_invalid_param';
     472                $expected_status = 400;
     473
     474                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     475                $request->set_param( 'per_page', $per_page );
     476                $response = rest_get_server()->dispatch( $request );
     477                $this->assertErrorResponse( $expected_error, $response, $expected_status );
     478        }
     479
     480        /**
     481         * Test that out of bounds 'page' query should error.
     482         *
     483         * @ticket 40510
     484         */
     485        public function test_get_items_out_of_bounds_page_should_error() {
     486                wp_set_current_user( self::$editor_id );
     487
     488                $per_page        = 2;
     489                $total_pages     = (int) ceil( $this->total_revisions / $per_page );
     490                $page            = $total_pages + 1; // Out of bound page.
     491                $expected_error  = 'rest_revision_invalid_page_number';
     492                $expected_status = 400;
     493
     494                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     495                $request->set_query_params( array(
     496                        'per_page' => $per_page,
     497                        'page'     => $page,
     498                ));
     499                $response = rest_get_server()->dispatch( $request );
     500                $this->assertErrorResponse( $expected_error, $response, $expected_status );
     501        }
     502
     503        /**
     504         * Test that impossibly high 'page' query should error.
     505         *
     506         * @ticket 40510
     507         */
     508        public function test_get_items_invalid_max_pages_should_error() {
     509                wp_set_current_user( self::$editor_id );
     510
     511                $per_page        = 2;
     512                $page            = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER; // Invalid number.
     513                $expected_error  = 'rest_revision_invalid_page_number';
     514                $expected_status = 400;
     515
     516                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     517                $request->set_query_params( array(
     518                        'per_page' => $per_page,
     519                        'page'     => $page,
     520                ));
     521                $response = rest_get_server()->dispatch( $request );
     522                $this->assertErrorResponse( $expected_error, $response, $expected_status );
     523        }
     524
     525        /**
     526         * Test the search query.
     527         *
     528         * @ticket 40510
     529         */
     530        public function test_get_items_search_query() {
     531                wp_set_current_user( self::$editor_id );
     532
     533                $search_string    = 'better';
     534                $expected_count   = 1;
     535                $expected_content = 'This content is better.';
     536
     537                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     538                $request->set_param( 'search', $search_string );
     539                $response = rest_get_server()->dispatch( $request );
     540                $data     = $response->get_data();
     541                $this->assertCount( $expected_count, $data );
     542                $this->assertContains( $expected_content, $data[0]['content']['rendered'] );
     543        }
     544
     545        /**
     546         * Test that the default query should fetch all revisions.
     547         *
     548         * @ticket 40510
     549         */
     550        public function test_get_items_default_query_should_fetch_all_revisons() {
     551                wp_set_current_user( self::$editor_id );
     552
     553                $expected_count = $this->total_revisions;
     554
     555                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     556                $response = rest_get_server()->dispatch( $request );
     557                $this->assertCount( $expected_count, $response->get_data() );
     558        }
     559
     560        /**
     561         * Test that 'offset' query shouldn't work without 'per_page' (fallback -1).
     562         *
     563         * @ticket 40510
     564         */
     565        public function test_get_items_offset_should_not_work_without_per_page() {
     566                wp_set_current_user( self::$editor_id );
     567
     568                $offset         = 1;
     569                $expected_count = $this->total_revisions;
     570
     571                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     572                $request->set_param( 'offset', $offset );
     573                $response = rest_get_server()->dispatch( $request );
     574                $this->assertCount( $expected_count, $response->get_data() );
     575        }
     576
     577        /**
     578         * Test that 'offset' query should work with 'per_page'.
     579         *
     580         * @ticket 40510
     581         */
     582        public function test_get_items_offset_should_work_with_per_page() {
     583                wp_set_current_user( self::$editor_id );
     584
     585                $per_page       = 2;
     586                $offset         = 1;
     587                $expected_count = 2;
     588
     589                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     590                $request->set_query_params( array(
     591                        'offset'   => $offset,
     592                        'per_page' => $per_page,
     593                ));
     594                $response = rest_get_server()->dispatch( $request );
     595                $this->assertCount( $expected_count, $response->get_data() );
     596        }
     597
     598        /**
     599         * Test that 'offset' query should take priority over 'page'.
     600         *
     601         * @ticket 40510
     602         */
     603        public function test_get_items_offset_should_take_priority_over_page() {
     604                wp_set_current_user( self::$editor_id );
     605
     606                $per_page       = 2;
     607                $offset         = 1;
     608                $page           = 1;
     609                $expected_count = 2;
     610
     611                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     612                $request->set_query_params( array(
     613                        'offset'   => $offset,
     614                        'per_page' => $per_page,
     615                        'page'     => $page,
     616                ));
     617                $response = rest_get_server()->dispatch( $request );
     618                $this->assertCount( $expected_count, $response->get_data() );
     619        }
     620
     621        /**
     622         * Test that 'offset' query, as the total revisions count, should return empty data.
     623         *
     624         * @ticket 40510
     625         */
     626        public function test_get_items_total_revisions_offset_should_return_empty_data() {
     627                wp_set_current_user( self::$editor_id );
     628
     629                $per_page        = 2;
     630                $offset          = $this->total_revisions;
     631                $expected_error  = 'rest_revision_invalid_offset_number';
     632                $expected_status = 400;
     633
     634                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     635                $request->set_query_params( array(
     636                        'offset'   => $offset,
     637                        'per_page' => $per_page,
     638                ));
     639                $response = rest_get_server()->dispatch( $request );
     640                $this->assertErrorResponse( $expected_error, $response, $expected_status );
     641        }
     642
     643        /**
     644         * Test that out of bound 'offset' query should error.
     645         *
     646         * @ticket 40510
     647         */
     648        public function test_get_items_out_of_bound_offset_should_error() {
     649                wp_set_current_user( self::$editor_id );
     650
     651                $per_page        = 2;
     652                $offset          = $this->total_revisions + 1;
     653                $expected_error  = 'rest_revision_invalid_offset_number';
     654                $expected_status = 400;
     655
     656                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     657                $request->set_query_params( array(
     658                        'offset'   => $offset,
     659                        'per_page' => $per_page,
     660                ));
     661                $response = rest_get_server()->dispatch( $request );
     662                $this->assertErrorResponse( $expected_error, $response, $expected_status );
     663        }
     664
     665        /**
     666         * Test that impossible high number for 'offset' query should error.
     667         *
     668         * @ticket 40510
     669         */
     670        public function test_get_items_impossible_high_number_offset_should_error() {
     671                wp_set_current_user( self::$editor_id );
     672
     673                $per_page        = 2;
     674                $offset          = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER;
     675                $expected_error  = 'rest_revision_invalid_offset_number';
     676                $expected_status = 400;
     677
     678                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     679                $request->set_query_params( array(
     680                        'offset'   => $offset,
     681                        'per_page' => $per_page,
     682                ));
     683                $response = rest_get_server()->dispatch( $request );
     684                $this->assertErrorResponse( $expected_error, $response, $expected_status );
     685        }
     686
     687        /**
     688         * Test that invalid 'offset' query should error.
     689         *
     690         * @ticket 40510
     691         */
     692        public function test_get_items_invalid_offset_should_error() {
     693                wp_set_current_user( self::$editor_id );
     694
     695                $per_page        = 2;
     696                $offset          = 'moreplease';
     697                $expected_error  = 'rest_invalid_param';
     698                $expected_status = 400;
     699
     700                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     701                $request->set_query_params( array(
     702                        'offset'   => $offset,
     703                        'per_page' => $per_page,
     704                ));
     705                $response = rest_get_server()->dispatch( $request );
     706                $this->assertErrorResponse( $expected_error, $response, $expected_status );
     707        }
     708
     709        /**
     710         * Test that out of bounds 'page' query should not error when offset is provided,
     711         * because it takes precedence.
     712         *
     713         * @ticket 40510
     714         */
     715        public function test_get_items_out_of_bounds_page_should_not_error_if_offset() {
     716                wp_set_current_user( self::$editor_id );
     717
     718                $per_page       = 2;
     719                $total_pages    = (int) ceil( $this->total_revisions / $per_page );
     720                $page           = $total_pages + 1; // Out of bound page.
     721                $expected_count = 2;
     722
     723                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     724                $request->set_query_params( array(
     725                        'offset'   => 1,
     726                        'per_page' => $per_page,
     727                        'page'     => $page,
     728                ));
     729                $response = rest_get_server()->dispatch( $request );
     730                $this->assertCount( $expected_count, $response->get_data() );
     731        }
    385732}
  • tests/qunit/fixtures/wp-api-generated.js

     
    721721                            ],
    722722                            "description": "Scope under which the request is made; determines fields present in response.",
    723723                            "type": "string"
     724                        },
     725                        "page": {
     726                            "required": false,
     727                            "default": 1,
     728                            "description": "Current page of the collection.",
     729                            "type": "integer"
     730                        },
     731                        "per_page": {
     732                            "required": false,
     733                            "description": "Maximum number of items to be returned in result set.",
     734                            "type": "integer"
     735                        },
     736                        "search": {
     737                            "required": false,
     738                            "description": "Limit results to those matching a string.",
     739                            "type": "string"
     740                        },
     741                        "exclude": {
     742                            "required": false,
     743                            "default": [],
     744                            "description": "Ensure result set excludes specific IDs.",
     745                            "type": "array",
     746                            "items": {
     747                                "type": "integer"
     748                            }
     749                        },
     750                        "include": {
     751                            "required": false,
     752                            "default": [],
     753                            "description": "Limit result set to specific IDs.",
     754                            "type": "array",
     755                            "items": {
     756                                "type": "integer"
     757                            }
     758                        },
     759                        "offset": {
     760                            "required": false,
     761                            "description": "Offset the result set by a specific number of items.",
     762                            "type": "integer"
     763                        },
     764                        "order": {
     765                            "required": false,
     766                            "default": "desc",
     767                            "enum": [
     768                                "asc",
     769                                "desc"
     770                            ],
     771                            "description": "Order sort attribute ascending or descending.",
     772                            "type": "string"
     773                        },
     774                        "orderby": {
     775                            "required": false,
     776                            "default": "date",
     777                            "enum": [
     778                                "date",
     779                                "id",
     780                                "include",
     781                                "relevance",
     782                                "slug",
     783                                "include_slugs",
     784                                "title"
     785                            ],
     786                            "description": "Sort collection by object attribute.",
     787                            "type": "string"
    724788                        }
    725789                    }
    726790                }
     
    12631327                            ],
    12641328                            "description": "Scope under which the request is made; determines fields present in response.",
    12651329                            "type": "string"
     1330                        },
     1331                        "page": {
     1332                            "required": false,
     1333                            "default": 1,
     1334                            "description": "Current page of the collection.",
     1335                            "type": "integer"
     1336                        },
     1337                        "per_page": {
     1338                            "required": false,
     1339                            "description": "Maximum number of items to be returned in result set.",
     1340                            "type": "integer"
     1341                        },
     1342                        "search": {
     1343                            "required": false,
     1344                            "description": "Limit results to those matching a string.",
     1345                            "type": "string"
     1346                        },
     1347                        "exclude": {
     1348                            "required": false,
     1349                            "default": [],
     1350                            "description": "Ensure result set excludes specific IDs.",
     1351                            "type": "array",
     1352                            "items": {
     1353                                "type": "integer"
     1354                            }
     1355                        },
     1356                        "include": {
     1357                            "required": false,
     1358                            "default": [],
     1359                            "description": "Limit result set to specific IDs.",
     1360                            "type": "array",
     1361                            "items": {
     1362                                "type": "integer"
     1363                            }
     1364                        },
     1365                        "offset": {
     1366                            "required": false,
     1367                            "description": "Offset the result set by a specific number of items.",
     1368                            "type": "integer"
     1369                        },
     1370                        "order": {
     1371                            "required": false,
     1372                            "default": "desc",
     1373                            "enum": [
     1374                                "asc",
     1375                                "desc"
     1376                            ],
     1377                            "description": "Order sort attribute ascending or descending.",
     1378                            "type": "string"
     1379                        },
     1380                        "orderby": {
     1381                            "required": false,
     1382                            "default": "date",
     1383                            "enum": [
     1384                                "date",
     1385                                "id",
     1386                                "include",
     1387                                "relevance",
     1388                                "slug",
     1389                                "include_slugs",
     1390                                "title"
     1391                            ],
     1392                            "description": "Sort collection by object attribute.",
     1393                            "type": "string"
    12661394                        }
    12671395                    }
    12681396                }
     
    42864414        "taxonomy": "category",
    42874415        "parent": 0,
    42884416        "meta": {
    4289             "meta_key": ""
     4417            "test_single": "",
     4418            "test_multi": [],
     4419            "meta_key": "",
     4420            "test_cat_single": "",
     4421            "test_cat_multi": []
    42904422        },
    42914423        "_links": {
    42924424            "self": [
     
    43304462    "taxonomy": "category",
    43314463    "parent": 0,
    43324464    "meta": {
    4333         "meta_key": ""
     4465        "test_single": "",
     4466        "test_multi": [],
     4467        "meta_key": "",
     4468        "test_cat_single": "",
     4469        "test_cat_multi": []
    43344470    }
    43354471};
    43364472
     
    43444480        "slug": "restapi-client-fixture-tag",
    43454481        "taxonomy": "post_tag",
    43464482        "meta": {
    4347             "meta_key": "meta_value"
     4483            "test_single": "",
     4484            "test_multi": [],
     4485            "meta_key": "meta_value",
     4486            "test_tag_meta": ""
    43484487        },
    43494488        "_links": {
    43504489            "self": [
     
    43874526    "slug": "restapi-client-fixture-tag",
    43884527    "taxonomy": "post_tag",
    43894528    "meta": {
    4390         "meta_key": "meta_value"
     4529        "test_single": "",
     4530        "test_multi": [],
     4531        "meta_key": "meta_value",
     4532        "test_tag_meta": ""
    43914533    }
    43924534};
    43934535