Make WordPress Core

Ticket #38693: rest_post_orderby.2.diff

File rest_post_orderby.2.diff, 10.2 KB (added by ChopinBach, 8 years ago)

Fixed the test in this patch.

  • 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/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( $id1, $data[0]['id'] );
     244                $this->assertEquals( $id2, $data[1]['id'] );
     245                $this->assertEquals( $id3, $data[2]['id'] );
     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                $this->assertEquals( $id1, $data[0]['id'] );
     284                $this->assertEquals( $id2, $data[1]['id'] );
     285                $this->assertEquals( $id3, $data[2]['id'] );
     286        }
     287
     288        public function test_get_items_orderby_parent_query() {
     289                $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     290                $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     291                $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_parent' => $id1 ) );
     292
     293                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     294                $request->set_param( 'include', array( $id1, $id2, $id3 ) );
     295                $request->set_param( 'orderby', 'parent' );
     296
     297                $response = $this->server->dispatch( $request );
     298                $data = $response->get_data();
     299
     300                $this->assertEquals( 200, $response->get_status() );
     301                $this->assertEquals( $id2, $data[0]['id'] );
     302                $this->assertEquals( $id1, $data[1]['id'] );
     303                $this->assertEquals( $id3, $data[2]['id'] );
     304        }
     305
    230306        public function test_get_items_exclude_query() {
    231307                $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    232308                $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     
    26472723                        'post-my-test-template.php' => 'My Test Template',
    26482724                );
    26492725        }
     2726
     2727        /**
     2728         * There's no way to change post_modified through the API.
     2729         */
     2730        protected function update_post_modified( $post_id, $date ) {
     2731                global $wpdb;
     2732                return $wpdb->update(
     2733                        $wpdb->posts,
     2734                        array(
     2735                                'post_modified' => $date,
     2736                                'post_modified_gmt' => $date,
     2737                        ),
     2738                        array(
     2739                                'ID' => $post_id,
     2740                        ),
     2741                        array(
     2742                                '%s',
     2743                                '%s',
     2744                        ),
     2745                        array(
     2746                                '%d',
     2747                        )
     2748                );
     2749        }
    26502750}