Make WordPress Core

Changeset 43716


Ignore:
Timestamp:
10/11/2018 07:15:34 AM (5 years ago)
Author:
SergeyBiryukov
Message:

REST API: Support pagination, order, search and other common query parameters for revisions.

The original REST API revisions controller relied on wp_get_post_revisions(), getting all revisions of a post without any possibility to restrict the result. This changeset replaces that function call with a proper WP_Query setup, replicating how wp_get_post_revisions() works while offering parameters to alter the default behavior.

Props adamsilverstein, birgire, flixos90.
Merges [43584-43586], [43647] to the 5.0 branch.
Fixes #40510.

Location:
branches/5.0
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0

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

    r43694 r43716  
    865865             * @param string $value The query_var value.
    866866             */
    867             $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value );
     867            $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
    868868        }
    869869
  • branches/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r43445 r43716  
    198198        }
    199199
    200         $revisions = wp_get_post_revisions( $request['parent'] );
     200        // Ensure a search string is set in case the orderby is set to 'relevance'.
     201        if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
     202            return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) );
     203        }
     204
     205        // Ensure an include parameter is set in case the orderby is set to 'include'.
     206        if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
     207            return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.' ), array( 'status' => 400 ) );
     208        }
     209
     210        if ( wp_revisions_enabled( $parent ) ) {
     211            $registered = $this->get_collection_params();
     212            $args       = array(
     213                'post_parent'      => $parent->ID,
     214                'post_type'        => 'revision',
     215                'post_status'      => 'inherit',
     216                'posts_per_page'   => -1,
     217                'orderby'          => 'date ID',
     218                'order'            => 'DESC',
     219                'suppress_filters' => true,
     220            );
     221
     222            $parameter_mappings = array(
     223                'exclude'  => 'post__not_in',
     224                'include'  => 'post__in',
     225                'offset'   => 'offset',
     226                'order'    => 'order',
     227                'orderby'  => 'orderby',
     228                'page'     => 'paged',
     229                'per_page' => 'posts_per_page',
     230                'search'   => 's',
     231            );
     232
     233            foreach ( $parameter_mappings as $api_param => $wp_param ) {
     234                if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
     235                    $args[ $wp_param ] = $request[ $api_param ];
     236                }
     237            }
     238
     239            // For backward-compatibility, 'date' needs to resolve to 'date ID'.
     240            if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) {
     241                $args['orderby'] = 'date ID';
     242            }
     243
     244            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
     245            $args       = apply_filters( 'rest_revision_query', $args, $request );
     246            $query_args = $this->prepare_items_query( $args, $request );
     247
     248            $revisions_query = new WP_Query();
     249            $revisions       = $revisions_query->query( $query_args );
     250            $offset          = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
     251            $page            = (int) $query_args['paged'];
     252            $total_revisions = $revisions_query->found_posts;
     253
     254            if ( $total_revisions < 1 ) {
     255                // Out-of-bounds, run the query again without LIMIT for total count.
     256                unset( $query_args['paged'], $query_args['offset'] );
     257
     258                $count_query = new WP_Query();
     259                $count_query->query( $query_args );
     260
     261                $total_revisions = $count_query->found_posts;
     262            }
     263
     264            if ( $revisions_query->query_vars['posts_per_page'] > 0 ) {
     265                $max_pages = ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] );
     266            } else {
     267                $max_pages = $total_revisions > 0 ? 1 : 0;
     268            }
     269
     270            if ( $total_revisions > 0 ) {
     271                if ( $offset >= $total_revisions ) {
     272                    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 ) );
     273                } elseif ( ! $offset && $page > $max_pages ) {
     274                    return new WP_Error( 'rest_revision_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) );
     275                }
     276            }
     277        } else {
     278            $revisions       = array();
     279            $total_revisions = 0;
     280            $max_pages       = 0;
     281            $page            = (int) $request['page'];
     282        }
    201283
    202284        $response = array();
     
    205287            $response[] = $this->prepare_response_for_collection( $data );
    206288        }
    207         return rest_ensure_response( $response );
     289
     290        $response = rest_ensure_response( $response );
     291
     292        $response->header( 'X-WP-Total', (int) $total_revisions );
     293        $response->header( 'X-WP-TotalPages', (int) $max_pages );
     294
     295        $request_params = $request->get_query_params();
     296        $base           = add_query_arg( $request_params, rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ) );
     297
     298        if ( $page > 1 ) {
     299            $prev_page = $page - 1;
     300
     301            if ( $prev_page > $max_pages ) {
     302                $prev_page = $max_pages;
     303            }
     304
     305            $prev_link = add_query_arg( 'page', $prev_page, $base );
     306            $response->link_header( 'prev', $prev_link );
     307        }
     308        if ( $max_pages > $page ) {
     309            $next_page = $page + 1;
     310            $next_link = add_query_arg( 'page', $next_page, $base );
     311
     312            $response->link_header( 'next', $next_link );
     313        }
     314
     315        return $response;
    208316    }
    209317
     
    316424        $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
    317425        return $response;
     426    }
     427
     428    /**
     429     * Determines the allowed query_vars for a get_items() response and prepares
     430     * them for WP_Query.
     431     *
     432     * @since 5.0.0
     433     *
     434     * @param array           $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
     435     * @param WP_REST_Request $request       Optional. Full details about the request.
     436     * @return array Items query arguments.
     437     */
     438    protected function prepare_items_query( $prepared_args = array(), $request = null ) {
     439        $query_args = array();
     440
     441        foreach ( $prepared_args as $key => $value ) {
     442            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
     443            $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
     444        }
     445
     446        // Map to proper WP_Query orderby param.
     447        if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
     448            $orderby_mappings = array(
     449                'id'            => 'ID',
     450                'include'       => 'post__in',
     451                'slug'          => 'post_name',
     452                'include_slugs' => 'post_name__in',
     453            );
     454
     455            if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
     456                $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
     457            }
     458        }
     459
     460        return $query_args;
    318461    }
    319462
     
    538681     */
    539682    public function get_collection_params() {
    540         return array(
    541             'context' => $this->get_context_param( array( 'default' => 'view' ) ),
     683        $query_params = parent::get_collection_params();
     684
     685        $query_params['context']['default'] = 'view';
     686
     687        unset( $query_params['per_page']['default'] );
     688
     689        $query_params['exclude'] = array(
     690            'description' => __( 'Ensure result set excludes specific IDs.' ),
     691            'type'        => 'array',
     692            'items'       => array(
     693                'type' => 'integer',
     694            ),
     695            'default'     => array(),
    542696        );
     697
     698        $query_params['include'] = array(
     699            'description' => __( 'Limit result set to specific IDs.' ),
     700            'type'        => 'array',
     701            'items'       => array(
     702                'type' => 'integer',
     703            ),
     704            'default'     => array(),
     705        );
     706
     707        $query_params['offset'] = array(
     708            'description' => __( 'Offset the result set by a specific number of items.' ),
     709            'type'        => 'integer',
     710        );
     711
     712        $query_params['order'] = array(
     713            'description' => __( 'Order sort attribute ascending or descending.' ),
     714            'type'        => 'string',
     715            'default'     => 'desc',
     716            'enum'        => array( 'asc', 'desc' ),
     717        );
     718
     719        $query_params['orderby'] = array(
     720            'description' => __( 'Sort collection by object attribute.' ),
     721            'type'        => 'string',
     722            'default'     => 'date',
     723            'enum'        => array(
     724                'date',
     725                'id',
     726                'include',
     727                'relevance',
     728                'slug',
     729                'include_slugs',
     730                'title',
     731            ),
     732        );
     733
     734        return $query_params;
    543735    }
    544736
  • branches/5.0/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r43445 r43716  
    77 */
    88
    9  /**
    10   * @group restapi
    11   */
     9/**
     10 * @group restapi
     11 */
    1212class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase {
    1313    protected static $post_id;
     
    3131        wp_update_post( array( 'post_content' => 'This content is better.', 'ID' => self::$post_id ) );
    3232        wp_update_post( array( 'post_content' => 'This content is marvelous.', 'ID' => self::$post_id ) );
     33        wp_update_post(
     34            array(
     35                'post_content' => 'This content is fantastic.',
     36                'ID'           => self::$post_id,
     37            )
     38        );
    3339        wp_set_current_user( 0 );
    3440    }
     
    4652        parent::setUp();
    4753
    48         $revisions = wp_get_post_revisions( self::$post_id );
    49         $this->revision_1 = array_pop( $revisions );
    50         $this->revision_id1 = $this->revision_1->ID;
    51         $this->revision_2 = array_pop( $revisions );
    52         $this->revision_id2 = $this->revision_2->ID;
     54        $revisions             = wp_get_post_revisions( self::$post_id );
     55        $this->total_revisions = count( $revisions );
     56        $this->revisions       = $revisions;
     57        $this->revision_1      = array_pop( $revisions );
     58        $this->revision_id1    = $this->revision_1->ID;
     59        $this->revision_2      = array_pop( $revisions );
     60        $this->revision_id2    = $this->revision_2->ID;
     61        $this->revision_3      = array_pop( $revisions );
     62        $this->revision_id3    = $this->revision_3->ID;
    5363    }
    5464
     
    8292        $data = $response->get_data();
    8393        $this->assertEquals( 200, $response->get_status() );
    84         $this->assertCount( 2, $data );
     94        $this->assertCount( $this->total_revisions, $data );
    8595
    8696        // Reverse chron
    87         $this->assertEquals( $this->revision_id2, $data[0]['id'] );
    88         $this->check_get_revision_response( $data[0], $this->revision_2 );
    89 
    90         $this->assertEquals( $this->revision_id1, $data[1]['id'] );
    91         $this->check_get_revision_response( $data[1], $this->revision_1 );
     97        $this->assertEquals( $this->revision_id3, $data[0]['id'] );
     98        $this->check_get_revision_response( $data[0], $this->revision_3 );
     99
     100        $this->assertEquals( $this->revision_id2, $data[1]['id'] );
     101        $this->check_get_revision_response( $data[1], $this->revision_2 );
     102
     103        $this->assertEquals( $this->revision_id1, $data[2]['id'] );
     104        $this->check_get_revision_response( $data[2], $this->revision_1 );
    92105    }
    93106
     
    362375    }
    363376
     377    /**
     378     * Test the pagination header of the first page.
     379     *
     380     * @ticket 40510
     381     */
     382    public function test_get_items_pagination_header_of_the_first_page() {
     383        wp_set_current_user( self::$editor_id );
     384
     385        $rest_route  = '/wp/v2/posts/' . self::$post_id . '/revisions';
     386        $per_page    = 2;
     387        $total_pages = (int) ceil( $this->total_revisions / $per_page );
     388        $page        = 1;  // First page.
     389
     390        $request = new WP_REST_Request( 'GET', $rest_route );
     391        $request->set_query_params(
     392            array(
     393                'per_page' => $per_page,
     394                'page'     => $page,
     395            )
     396        );
     397        $response = rest_get_server()->dispatch( $request );
     398        $headers  = $response->get_headers();
     399        $this->assertSame( $this->total_revisions, $headers['X-WP-Total'] );
     400        $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
     401        $next_link = add_query_arg(
     402            array(
     403                'per_page' => $per_page,
     404                'page'     => $page + 1,
     405            ),
     406            rest_url( $rest_route )
     407        );
     408        $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
     409        $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
     410    }
     411
     412    /**
     413     * Test the pagination header of the last page.
     414     *
     415     * @ticket 40510
     416     */
     417    public function test_get_items_pagination_header_of_the_last_page() {
     418        wp_set_current_user( self::$editor_id );
     419
     420        $rest_route  = '/wp/v2/posts/' . self::$post_id . '/revisions';
     421        $per_page    = 2;
     422        $total_pages = (int) ceil( $this->total_revisions / $per_page );
     423        $page        = 2;  // Last page.
     424
     425        $request = new WP_REST_Request( 'GET', $rest_route );
     426        $request->set_query_params(
     427            array(
     428                'per_page' => $per_page,
     429                'page'     => $page,
     430            )
     431        );
     432        $response = rest_get_server()->dispatch( $request );
     433        $headers  = $response->get_headers();
     434        $this->assertSame( $this->total_revisions, $headers['X-WP-Total'] );
     435        $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
     436        $prev_link = add_query_arg(
     437            array(
     438                'per_page' => $per_page,
     439                'page'     => $page - 1,
     440            ),
     441            rest_url( $rest_route )
     442        );
     443        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
     444    }
     445
     446    /**
     447     * Test that invalid 'per_page' query should error.
     448     *
     449     * @ticket 40510
     450     */
     451    public function test_get_items_invalid_per_page_should_error() {
     452        wp_set_current_user( self::$editor_id );
     453
     454        $per_page        = -1; // Invalid number.
     455        $expected_error  = 'rest_invalid_param';
     456        $expected_status = 400;
     457
     458        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     459        $request->set_param( 'per_page', $per_page );
     460        $response = rest_get_server()->dispatch( $request );
     461        $this->assertErrorResponse( $expected_error, $response, $expected_status );
     462    }
     463
     464    /**
     465     * Test that out of bounds 'page' query should error.
     466     *
     467     * @ticket 40510
     468     */
     469    public function test_get_items_out_of_bounds_page_should_error() {
     470        wp_set_current_user( self::$editor_id );
     471
     472        $per_page        = 2;
     473        $total_pages     = (int) ceil( $this->total_revisions / $per_page );
     474        $page            = $total_pages + 1; // Out of bound page.
     475        $expected_error  = 'rest_revision_invalid_page_number';
     476        $expected_status = 400;
     477
     478        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     479        $request->set_query_params(
     480            array(
     481                'per_page' => $per_page,
     482                'page'     => $page,
     483            )
     484        );
     485        $response = rest_get_server()->dispatch( $request );
     486        $this->assertErrorResponse( $expected_error, $response, $expected_status );
     487    }
     488
     489    /**
     490     * Test that impossibly high 'page' query should error.
     491     *
     492     * @ticket 40510
     493     */
     494    public function test_get_items_invalid_max_pages_should_error() {
     495        wp_set_current_user( self::$editor_id );
     496
     497        $per_page        = 2;
     498        $page            = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER; // Invalid number.
     499        $expected_error  = 'rest_revision_invalid_page_number';
     500        $expected_status = 400;
     501
     502        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     503        $request->set_query_params(
     504            array(
     505                'per_page' => $per_page,
     506                'page'     => $page,
     507            )
     508        );
     509        $response = rest_get_server()->dispatch( $request );
     510        $this->assertErrorResponse( $expected_error, $response, $expected_status );
     511    }
     512
     513    /**
     514     * Test the search query.
     515     *
     516     * @ticket 40510
     517     */
     518    public function test_get_items_search_query() {
     519        wp_set_current_user( self::$editor_id );
     520
     521        $search_string    = 'better';
     522        $expected_count   = 1;
     523        $expected_content = 'This content is better.';
     524
     525        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     526        $request->set_param( 'search', $search_string );
     527        $response = rest_get_server()->dispatch( $request );
     528        $data     = $response->get_data();
     529        $this->assertCount( $expected_count, $data );
     530        $this->assertContains( $expected_content, $data[0]['content']['rendered'] );
     531    }
     532
     533    /**
     534     * Test that the default query should fetch all revisions.
     535     *
     536     * @ticket 40510
     537     */
     538    public function test_get_items_default_query_should_fetch_all_revisons() {
     539        wp_set_current_user( self::$editor_id );
     540
     541        $expected_count = $this->total_revisions;
     542
     543        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     544        $response = rest_get_server()->dispatch( $request );
     545        $this->assertCount( $expected_count, $response->get_data() );
     546    }
     547
     548    /**
     549     * Test that 'offset' query shouldn't work without 'per_page' (fallback -1).
     550     *
     551     * @ticket 40510
     552     */
     553    public function test_get_items_offset_should_not_work_without_per_page() {
     554        wp_set_current_user( self::$editor_id );
     555
     556        $offset         = 1;
     557        $expected_count = $this->total_revisions;
     558
     559        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     560        $request->set_param( 'offset', $offset );
     561        $response = rest_get_server()->dispatch( $request );
     562        $this->assertCount( $expected_count, $response->get_data() );
     563    }
     564
     565    /**
     566     * Test that 'offset' query should work with 'per_page'.
     567     *
     568     * @ticket 40510
     569     */
     570    public function test_get_items_offset_should_work_with_per_page() {
     571        wp_set_current_user( self::$editor_id );
     572
     573        $per_page       = 2;
     574        $offset         = 1;
     575        $expected_count = 2;
     576
     577        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     578        $request->set_query_params(
     579            array(
     580                'offset'   => $offset,
     581                'per_page' => $per_page,
     582            )
     583        );
     584        $response = rest_get_server()->dispatch( $request );
     585        $this->assertCount( $expected_count, $response->get_data() );
     586    }
     587
     588    /**
     589     * Test that 'offset' query should take priority over 'page'.
     590     *
     591     * @ticket 40510
     592     */
     593    public function test_get_items_offset_should_take_priority_over_page() {
     594        wp_set_current_user( self::$editor_id );
     595
     596        $per_page       = 2;
     597        $offset         = 1;
     598        $page           = 1;
     599        $expected_count = 2;
     600
     601        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     602        $request->set_query_params(
     603            array(
     604                'offset'   => $offset,
     605                'per_page' => $per_page,
     606                'page'     => $page,
     607            )
     608        );
     609        $response = rest_get_server()->dispatch( $request );
     610        $this->assertCount( $expected_count, $response->get_data() );
     611    }
     612
     613    /**
     614     * Test that 'offset' query, as the total revisions count, should return empty data.
     615     *
     616     * @ticket 40510
     617     */
     618    public function test_get_items_total_revisions_offset_should_return_empty_data() {
     619        wp_set_current_user( self::$editor_id );
     620
     621        $per_page        = 2;
     622        $offset          = $this->total_revisions;
     623        $expected_error  = 'rest_revision_invalid_offset_number';
     624        $expected_status = 400;
     625
     626        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     627        $request->set_query_params(
     628            array(
     629                'offset'   => $offset,
     630                'per_page' => $per_page,
     631            )
     632        );
     633        $response = rest_get_server()->dispatch( $request );
     634        $this->assertErrorResponse( $expected_error, $response, $expected_status );
     635    }
     636
     637    /**
     638     * Test that out of bound 'offset' query should error.
     639     *
     640     * @ticket 40510
     641     */
     642    public function test_get_items_out_of_bound_offset_should_error() {
     643        wp_set_current_user( self::$editor_id );
     644
     645        $per_page        = 2;
     646        $offset          = $this->total_revisions + 1;
     647        $expected_error  = 'rest_revision_invalid_offset_number';
     648        $expected_status = 400;
     649
     650        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     651        $request->set_query_params(
     652            array(
     653                'offset'   => $offset,
     654                'per_page' => $per_page,
     655            )
     656        );
     657        $response = rest_get_server()->dispatch( $request );
     658        $this->assertErrorResponse( $expected_error, $response, $expected_status );
     659    }
     660
     661    /**
     662     * Test that impossible high number for 'offset' query should error.
     663     *
     664     * @ticket 40510
     665     */
     666    public function test_get_items_impossible_high_number_offset_should_error() {
     667        wp_set_current_user( self::$editor_id );
     668
     669        $per_page        = 2;
     670        $offset          = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER;
     671        $expected_error  = 'rest_revision_invalid_offset_number';
     672        $expected_status = 400;
     673
     674        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     675        $request->set_query_params(
     676            array(
     677                'offset'   => $offset,
     678                'per_page' => $per_page,
     679            )
     680        );
     681        $response = rest_get_server()->dispatch( $request );
     682        $this->assertErrorResponse( $expected_error, $response, $expected_status );
     683    }
     684
     685    /**
     686     * Test that invalid 'offset' query should error.
     687     *
     688     * @ticket 40510
     689     */
     690    public function test_get_items_invalid_offset_should_error() {
     691        wp_set_current_user( self::$editor_id );
     692
     693        $per_page        = 2;
     694        $offset          = 'moreplease';
     695        $expected_error  = 'rest_invalid_param';
     696        $expected_status = 400;
     697
     698        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     699        $request->set_query_params(
     700            array(
     701                'offset'   => $offset,
     702                'per_page' => $per_page,
     703            )
     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(
     725            array(
     726                'offset'   => 1,
     727                'per_page' => $per_page,
     728                'page'     => $page,
     729            )
     730        );
     731        $response = rest_get_server()->dispatch( $request );
     732        $this->assertCount( $expected_count, $response->get_data() );
     733    }
    364734}
  • branches/5.0/tests/qunit/fixtures/wp-api-generated.js

    r43713 r43716  
    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                    }
     
    12631327                            ],
    12641328                            "description": "Scope under which the request is made; determines fields present in response.",
     1329                            "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.",
    12651393                            "type": "string"
    12661394                        }
  • branches/5.0/tests/qunit/wp-includes/js/wp-api.js

    r41678 r43716  
    401401                    // Get the main endpoint.
    402402                    var endpoint = theModels.at(0);
     403                    var expectedMetas = '{"meta_key":"meta_value"}';
     404                    if ( 'Tags' === modelType ) {
     405                        expectedMetas = '{"test_single":"","test_multi":[],"meta_key":"meta_value","test_tag_meta":""}';
     406                    }
    403407
    404408                    // Verify the meta object returned correctly from `getMetas()`.
    405                     assert.equal( JSON.stringify( endpoint.getMetas() ), '{"meta_key":"meta_value"}', 'Full meta key/values object should be readable.' );
     409                    assert.equal( JSON.stringify( endpoint.getMetas() ), expectedMetas, 'Full meta key/values object should be readable.' );
    406410
    407411                    // Verify single meta returned correctly from `getMeta()`
Note: See TracChangeset for help on using the changeset viewer.