Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2684 from WP-API/comments-order-descending-2588
Browse files Browse the repository at this point in the history
Alter default comment sort order to be "desc"
  • Loading branch information
danielbachhuber committed Sep 9, 2016
2 parents f3647e5 + b4ef5e5 commit bb69048
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/endpoints/class-wp-rest-comments-controller.php
Expand Up @@ -1006,7 +1006,7 @@ public function get_collection_params() {
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
'default' => 'asc',
'default' => 'desc',
'enum' => array(
'asc',
'desc',
Expand Down
24 changes: 23 additions & 1 deletion tests/test-rest-comments-controller.php
Expand Up @@ -179,7 +179,8 @@ public function test_get_items_include_query() {
$this->factory->comment->create( $args );
$id3 = $this->factory->comment->create( $args );
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
// Orderby=>desc
// Order=>asc
$request->set_param( 'order', 'asc' );
$request->set_param( 'include', array( $id3, $id1 ) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
Expand Down Expand Up @@ -236,6 +237,27 @@ public function test_get_items_offset_query() {
$this->assertCount( 2, $response->get_data() );
}

public function test_get_items_order_query() {
wp_set_current_user( $this->admin_id );
$args = array(
'comment_approved' => 1,
'comment_post_ID' => $this->post_id,
);
$this->factory->comment->create( $args );
$this->factory->comment->create( $args );
$id3 = $this->factory->comment->create( $args );
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
// order defaults to 'desc'
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( $id3, $data[0]['id'] );
// order=>asc
$request->set_param( 'order', 'asc' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( $this->approved_id, $data[0]['id'] );
}

public function test_get_items_private_post_no_permissions() {
wp_set_current_user( 0 );
$post_id = $this->factory->post->create( array( 'post_status' => 'private' ) );
Expand Down
2 changes: 1 addition & 1 deletion tests/test-rest-posts-controller.php
Expand Up @@ -265,7 +265,7 @@ public function test_get_items_order_and_orderby() {
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] );
// order=>desc
// order=>asc
$request->set_param( 'order', 'asc' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
Expand Down

0 comments on commit bb69048

Please sign in to comment.