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 23017d7c29..4862fae564 100644 --- 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 @@ -2130,12 +2130,15 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { 'type' => 'string', 'default' => 'date', 'enum' => array( + 'author', 'date', - 'relevance', 'id', 'include', - 'title', + 'modified', + 'parent', + 'relevance', 'slug', + 'title', ), ); diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index ba1c47ffdb..c8c4857c10 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -868,4 +868,28 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); return $id; } + + /** + * There's no way to change post_modified through WP functions. + */ + protected function update_post_modified( $post_id, $date ) { + global $wpdb; + return $wpdb->update( + $wpdb->posts, + array( + 'post_modified' => $date, + 'post_modified_gmt' => $date, + ), + array( + 'ID' => $post_id, + ), + array( + '%s', + '%s', + ), + array( + '%d', + ) + ); + } } diff --git a/tests/phpunit/tests/query/dateQuery.php b/tests/phpunit/tests/query/dateQuery.php index 4af981c860..9e3bca6066 100644 --- a/tests/phpunit/tests/query/dateQuery.php +++ b/tests/phpunit/tests/query/dateQuery.php @@ -998,30 +998,4 @@ class Tests_Query_DateQuery extends WP_UnitTestCase { $expected = array( $p1, $p4, $p5, ); $this->assertEqualSets( $expected, $q->posts ); } - - /** Helpers **********************************************************/ - - /** - * There's no way to change post_modified through the API. - */ - protected function update_post_modified( $post_id, $date ) { - global $wpdb; - return $wpdb->update( - $wpdb->posts, - array( - 'post_modified' => $date, - 'post_modified_gmt' => $date, - ), - array( - 'ID' => $post_id, - ), - array( - '%s', - '%s', - ), - array( - '%d', - ) - ); - } } diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 43e72758ec..c86879cffc 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -267,6 +267,72 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } + public function test_get_items_orderby_author_query() { + $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) ); + $id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) ); + $id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$author_id ) ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'include', array( $id1, $id2, $id3 ) ); + $request->set_param( 'orderby', 'author' ); + + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( self::$author_id, $data[0]['author'] ); + $this->assertEquals( self::$editor_id, $data[1]['author'] ); + $this->assertEquals( self::$editor_id, $data[2]['author'] ); + + $this->assertPostsOrderedBy( '{posts}.post_author DESC' ); + } + + public function test_get_items_orderby_modified_query() { + $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + + $this->update_post_modified( $id1, '2016-04-20 4:26:20' ); + $this->update_post_modified( $id2, '2016-02-01 20:24:02' ); + $this->update_post_modified( $id3, '2016-02-21 12:24:02' ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'include', array( $id1, $id2, $id3 ) ); + $request->set_param( 'orderby', 'modified' ); + + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( $id1, $data[0]['id'] ); + $this->assertEquals( $id3, $data[1]['id'] ); + $this->assertEquals( $id2, $data[2]['id'] ); + + $this->assertPostsOrderedBy( '{posts}.post_modified DESC' ); + } + + public function test_get_items_orderby_parent_query() { + $id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) ); + $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) ); + $id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page', 'post_parent' => $id1 ) ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); + $request->set_param( 'include', array( $id1, $id2, $id3 ) ); + $request->set_param( 'orderby', 'parent' ); + + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( $id3, $data[0]['id'] ); + // Check ordering. Default ORDER is DESC. + $this->assertEquals( $id1, $data[0]['parent'] ); + $this->assertEquals( 0, $data[1]['parent'] ); + $this->assertEquals( 0, $data[2]['parent'] ); + + $this->assertPostsOrderedBy( '{posts}.post_parent DESC' ); + } + public function test_get_items_exclude_query() { $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 0df5b9e5b2..3f1f304f5e 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -226,12 +226,15 @@ mockedApiResponse.Schema = { "required": false, "default": "date", "enum": [ + "author", "date", - "relevance", "id", "include", - "title", - "slug" + "modified", + "parent", + "relevance", + "slug", + "title" ], "description": "Sort collection by object attribute.", "type": "string" @@ -844,12 +847,15 @@ mockedApiResponse.Schema = { "required": false, "default": "date", "enum": [ + "author", "date", - "relevance", "id", "include", - "title", + "modified", + "parent", + "relevance", "slug", + "title", "menu_order" ], "description": "Sort collection by object attribute.", @@ -1379,12 +1385,15 @@ mockedApiResponse.Schema = { "required": false, "default": "date", "enum": [ + "author", "date", - "relevance", "id", "include", - "title", - "slug" + "modified", + "parent", + "relevance", + "slug", + "title" ], "description": "Sort collection by object attribute.", "type": "string"