Make WordPress Core

Ticket #38693: 38693.6.diff

File 38693.6.diff, 6.4 KB (added by jnylen0, 8 years ago)

Avoid duplicating update_post_modified helper

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

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index e0cdc79..bf368d8 100644
    a b class WP_REST_Posts_Controller extends WP_REST_Controller { 
    20382038                        'type'               => 'string',
    20392039                        'default'            => 'date',
    20402040                        'enum'               => array(
     2041                                'author',
    20412042                                'date',
    2042                                 'relevance',
    20432043                                'id',
    20442044                                'include',
    2045                                 'title',
     2045                                'modified',
     2046                                'none',
     2047                                'parent',
     2048                                'relevance',
    20462049                                'slug',
     2050                                'title',
    20472051                        ),
    20482052                );
    20492053
  • tests/phpunit/includes/testcase.php

    diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php
    index 344f9eb..1d33716 100644
    a b class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    776776                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    777777                return $id;
    778778        }
     779
     780        /**
     781         * There's no way to change post_modified through WP functions.
     782         */
     783        protected function update_post_modified( $post_id, $date ) {
     784                global $wpdb;
     785                return $wpdb->update(
     786                        $wpdb->posts,
     787                        array(
     788                                'post_modified' => $date,
     789                                'post_modified_gmt' => $date,
     790                        ),
     791                        array(
     792                                'ID' => $post_id,
     793                        ),
     794                        array(
     795                                '%s',
     796                                '%s',
     797                        ),
     798                        array(
     799                                '%d',
     800                        )
     801                );
     802        }
    779803}
  • tests/phpunit/tests/query/dateQuery.php

    diff --git a/tests/phpunit/tests/query/dateQuery.php b/tests/phpunit/tests/query/dateQuery.php
    index 4af981c..9e3bca6 100644
    a b class Tests_Query_DateQuery extends WP_UnitTestCase { 
    998998                $expected = array( $p1, $p4, $p5, );
    999999                $this->assertEqualSets( $expected, $q->posts );
    10001000        }
    1001 
    1002         /** Helpers **********************************************************/
    1003 
    1004         /**
    1005          * There's no way to change post_modified through the API.
    1006          */
    1007         protected function update_post_modified( $post_id, $date ) {
    1008                 global $wpdb;
    1009                 return $wpdb->update(
    1010                         $wpdb->posts,
    1011                         array(
    1012                                 'post_modified' => $date,
    1013                                 'post_modified_gmt' => $date,
    1014                         ),
    1015                         array(
    1016                                 'ID' => $post_id,
    1017                         ),
    1018                         array(
    1019                                 '%s',
    1020                                 '%s',
    1021                         ),
    1022                         array(
    1023                                 '%d',
    1024                         )
    1025                 );
    1026         }
    10271001}
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index 70db2e2..5b15d10 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    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' ) );