Make WordPress Core

Ticket #39061: 39061-comments.diff

File 39061-comments.diff, 2.8 KB (added by joehoyle, 7 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
    index 117529d..a4b1573 100644
    a b class WP_REST_Comments_Controller extends WP_REST_Controller { 
    235235                }
    236236
    237237                if ( isset( $registered['page'] ) && empty( $request['offset'] ) ) {
    238                         $prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 );
     238                        $prepared_args['offset'] = $prepared_args['number'] * ( $request['page'] - 1 );
    239239                }
    240240
    241241                /**
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    278278                        $max_pages = ceil( $total_comments / $request['per_page'] );
    279279                }
    280280
     281                if ( $request['page'] > $max_pages && $total_comments > 0 ) {
     282                        return new WP_Error( 'rest_comment_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) );
     283                }
     284
    281285                $response = rest_ensure_response( $comments );
    282286                $response->header( 'X-WP-Total', $total_comments );
    283287                $response->header( 'X-WP-TotalPages', $max_pages );
  • tests/phpunit/tests/rest-api/rest-comments-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
    index 502a6d2..b7b3701 100644
    a b class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    469469                // 'offset' takes priority over 'page'
    470470                $request->set_param( 'page', 3 );
    471471                $response = $this->server->dispatch( $request );
    472                 $this->assertCount( 2, $response->get_data() );
     472                $headers = $response->get_headers();
     473                $this->assertErrorResponse( 'rest_comment_invalid_page_number', $response, 400 );
     474
    473475                // 'offset' with invalid value errors.
    474476                $request->set_param( 'offset', 'moreplease' );
    475477                $response = $this->server->dispatch( $request );
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    731733                $request->set_param( 'page', 8 );
    732734                $response = $this->server->dispatch( $request );
    733735                $headers = $response->get_headers();
    734                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    735                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
    736                 $prev_link = add_query_arg( array(
    737                         'page'    => 6,
    738                         ), rest_url( '/wp/v2/comments' ) );
    739                 $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    740                 $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     736                $this->assertErrorResponse( 'rest_comment_invalid_page_number', $response, 400 );
    741737        }
    742738
    743739        public function test_get_comments_invalid_date() {