WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/10/2017 06:51:28 PM (2 years ago)
Author:
jnylen0
Message:

REST API: Add author, modified, and parent sort order options for posts.

These (and a few others that can be revisited later if needed) were present in
beta versions of the WP REST API but were removed during the merge to WP 4.7.

Props ChopinBach, jnylen0.
Fixes #38693.

File:
1 edited

Legend:

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

    r40602 r40605  
    266266        $response = $this->server->dispatch( $request );
    267267        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     268    }
     269
     270    public function test_get_items_orderby_author_query() {
     271        $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) );
     272        $id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) );
     273        $id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$author_id ) );
     274
     275        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     276        $request->set_param( 'include', array( $id1, $id2, $id3 ) );
     277        $request->set_param( 'orderby', 'author' );
     278
     279        $response = $this->server->dispatch( $request );
     280        $data = $response->get_data();
     281
     282        $this->assertEquals( 200, $response->get_status() );
     283        $this->assertEquals( self::$author_id, $data[0]['author'] );
     284        $this->assertEquals( self::$editor_id, $data[1]['author'] );
     285        $this->assertEquals( self::$editor_id, $data[2]['author'] );
     286
     287        $this->assertPostsOrderedBy( '{posts}.post_author DESC' );
     288    }
     289
     290    public function test_get_items_orderby_modified_query() {
     291        $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     292        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     293        $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     294
     295        $this->update_post_modified( $id1, '2016-04-20 4:26:20' );
     296        $this->update_post_modified( $id2, '2016-02-01 20:24:02' );
     297        $this->update_post_modified( $id3, '2016-02-21 12:24:02' );
     298
     299        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     300        $request->set_param( 'include', array( $id1, $id2, $id3 ) );
     301        $request->set_param( 'orderby', 'modified' );
     302
     303        $response = $this->server->dispatch( $request );
     304        $data = $response->get_data();
     305
     306        $this->assertEquals( 200, $response->get_status() );
     307        $this->assertEquals( $id1, $data[0]['id'] );
     308        $this->assertEquals( $id3, $data[1]['id'] );
     309        $this->assertEquals( $id2, $data[2]['id'] );
     310
     311        $this->assertPostsOrderedBy( '{posts}.post_modified DESC' );
     312    }
     313
     314    public function test_get_items_orderby_parent_query() {
     315        $id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) );
     316        $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) );
     317        $id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page', 'post_parent' => $id1 ) );
     318
     319        $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
     320        $request->set_param( 'include', array( $id1, $id2, $id3 ) );
     321        $request->set_param( 'orderby', 'parent' );
     322
     323        $response = $this->server->dispatch( $request );
     324        $data = $response->get_data();
     325
     326        $this->assertEquals( 200, $response->get_status() );
     327        $this->assertEquals( $id3, $data[0]['id'] );
     328        // Check ordering. Default ORDER is DESC.
     329        $this->assertEquals( $id1, $data[0]['parent'] );
     330        $this->assertEquals( 0, $data[1]['parent'] );
     331        $this->assertEquals( 0, $data[2]['parent'] );
     332
     333        $this->assertPostsOrderedBy( '{posts}.post_parent DESC' );
    268334    }
    269335
Note: See TracChangeset for help on using the changeset viewer.