WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/28/2018 12:22:48 PM (3 years ago)
Author:
flixos90
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.
Fixes #40510.

File:
1 edited

Legend:

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

    r43571 r43584  
    88
    99/**
    10  * @group restapi
     10 * @group restapi-revisions
    1111 */
    1212class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase {
     
    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    }
     
    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
     
    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 );
    103 
    104         $this->assertEquals( $this->revision_id1, $data[1]['id'] );
    105         $this->check_get_revision_response( $data[1], $this->revision_1 );
     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 );
     116
     117        $this->assertEquals( $this->revision_id1, $data[2]['id'] );
     118        $this->check_get_revision_response( $data[2], $this->revision_1 );
    106119    }
    107120
     
    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}
Note: See TracChangeset for help on using the changeset viewer.