Make WordPress Core

Ticket #38693: rest_post_orderby.4.diff

File rest_post_orderby.4.diff, 5.4 KB (added by ChopinBach, 8 years ago)

This one should actually work. Adds orderby params author, parent, none, and modified to the posts endpoint for the REST API. Unit tests revised to not rely upon secondary sorting by SQL. Line endings should be fixed as well.

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

     
    20382038                        'type'               => 'string',
    20392039                        'default'            => 'date',
    20402040                        'enum'               => array(
     2041                                'author',
    20412042                                'date',
    2042                                 'relevance',
    20432043                                'id',
    20442044                                'include',
     2045                                'modified',
     2046                                'none',
     2047                                'parent',
     2048                                'relevance',
     2049                                'slug',
    20452050                                'title',
    2046                                 'slug',
    20472051                        ),
    20482052                );
    20492053
  • tests/phpunit/includes/testcase.php

     
    776776                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    777777                return $id;
    778778        }
     779
     780        /**
     781         * Enables editing of `post_modified` field for posts.
     782         *
     783         * This is taken from test/query/dateQuery.php
     784         */
     785        protected function update_post_modified( $post_id, $date ) {
     786                global $wpdb;
     787                return $wpdb->update(
     788                        $wpdb->posts,
     789                        array(
     790                                'post_modified' => $date,
     791                                'post_modified_gmt' => $date,
     792                        ),
     793                        array(
     794                                'ID' => $post_id,
     795                        ),
     796                        array(
     797                                '%s',
     798                                '%s',
     799                        ),
     800                        array(
     801                                '%d',
     802                        )
     803                );
     804        }
    779805}
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

     
    227227                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    228228        }
    229229
     230        public function test_get_items_orderby_author_query() {
     231                $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) );
     232                $id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) );
     233                $id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$author_id ) );
     234
     235                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     236                $request->set_param( 'include', array( $id1, $id2, $id3 ) );
     237                $request->set_param( 'orderby', 'author' );
     238
     239                $response = $this->server->dispatch( $request );
     240                $data = $response->get_data();
     241
     242                $this->assertEquals( 200, $response->get_status() );
     243                $this->assertEquals( self::$author_id, $data[0]['author'] );
     244                $this->assertEquals( self::$editor_id, $data[1]['author'] );
     245                $this->assertEquals( self::$editor_id, $data[2]['author'] );
     246        }
     247
     248        public function test_get_items_orderby_modified_query() {
     249                $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     250                $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     251                $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     252
     253                $this->update_post_modified( $id1, '2016-04-20 4:26:20' );
     254                $this->update_post_modified( $id2, '2016-02-01 20:24:02' );
     255                $this->update_post_modified( $id3, '2016-02-21 12:24:02' );
     256
     257                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     258                $request->set_param( 'include', array( $id1, $id2, $id3 ) );
     259                $request->set_param( 'orderby', 'modified' );
     260
     261                $response = $this->server->dispatch( $request );
     262                $data = $response->get_data();
     263
     264                $this->assertEquals( 200, $response->get_status() );
     265                $this->assertEquals( $id1, $data[0]['id'] );
     266                $this->assertEquals( $id3, $data[1]['id'] );
     267                $this->assertEquals( $id2, $data[2]['id'] );
     268        }
     269
     270        public function test_get_items_orderby_none_query() {
     271                $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     272                $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     273                $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     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', 'none' );
     278
     279                $response = $this->server->dispatch( $request );
     280                $data = $response->get_data();
     281
     282                $this->assertEquals( 200, $response->get_status() );
     283        }
     284
     285        public function test_get_items_orderby_parent_query() {
     286                $id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) );
     287                $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) );
     288                $id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page', 'post_parent' => $id1 ) );
     289
     290                $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
     291                $request->set_param( 'include', array( $id1, $id2, $id3 ) );
     292                $request->set_param( 'orderby', 'parent' );
     293
     294                $response = $this->server->dispatch( $request );
     295                $data = $response->get_data();
     296
     297                $this->assertEquals( 200, $response->get_status() );
     298                $this->assertEquals( $id3, $data[0]['id'] );
     299                // Check ordering. Default ORDER is DESC.
     300                $this->assertEquals( $id1, $data[0]['parent'] );
     301                $this->assertEquals( 0, $data[1]['parent'] );
     302                $this->assertEquals( 0, $data[2]['parent'] );
     303        }
     304
    230305        public function test_get_items_exclude_query() {
    231306                $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    232307                $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );