Skip to content

Commit

Permalink
REST API: Fix PHP warning about undefined paged argument in various…
Browse files Browse the repository at this point in the history
… REST API endpoints.

This bug could occur in `WP_REST_Posts_Controller`, `WP_REST_Global_Styles_Revisions_Controller`, `WP_REST_Revisions_Controller`, and any of their child classes. This changeset fixes it throughout.

Props apermo, pbearne, hemant-ahir, flixos90.
Fixes #62292.


git-svn-id: https://develop.svn.wordpress.org/trunk@59630 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
felixarntz committed Jan 15, 2025
1 parent aaf760c commit d2630e0
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function get_items( $request ) {
$revisions_query = new WP_Query();
$revisions = $revisions_query->query( $query_args );
$offset = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
$page = (int) $query_args['paged'];
$page = isset( $query_args['paged'] ) ? (int) $query_args['paged'] : 0;
$total_revisions = $revisions_query->found_posts;

if ( $total_revisions < 1 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ static function ( $format ) {
remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
}

$page = (int) $query_args['paged'];
$page = isset( $query_args['paged'] ) ? (int) $query_args['paged'] : 0;
$total_posts = $posts_query->found_posts;

if ( $total_posts < 1 && $page > 1 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public function get_items( $request ) {
$revisions_query = new WP_Query();
$revisions = $revisions_query->query( $query_args );
$offset = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
$page = (int) $query_args['paged'];
$page = isset( $query_args['paged'] ) ? (int) $query_args['paged'] : 0;
$total_revisions = $revisions_query->found_posts;

if ( $total_revisions < 1 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,47 @@ public function test_get_items_out_of_bounds_page_should_not_error_if_offset() {
$this->assertCount( $expected_count, $response->get_data() );
}


/**
* Tests for the pagination.
*
* @ticket 62292
*
* @covers WP_REST_Global_Styles_Controller::get_items
*/
public function test_get_global_styles_revisions_pagination() {
wp_set_current_user( self::$admin_id );

// Test offset
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_param( 'offset', 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( 3, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 3, $response->get_headers()['X-WP-TotalPages'] );

// Test paged
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_param( 'page', 2 );
$request->set_param( 'per_page', 2 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( 3, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 2, $response->get_headers()['X-WP-TotalPages'] );

// Test out of bounds
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_param( 'page', 4 );
$request->set_param( 'per_page', 6 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_revision_invalid_page_number', $response, 400 );
}

/**
* @doesNotPerformAssertions
*/
Expand Down
39 changes: 39 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5676,6 +5676,45 @@ public function test_multiple_post_format_support() {
$this->assertCount( 2, $response->get_data(), 'Two posts are expected to be returned' );
}

/**
* Tests for the pagination.
*
* @ticket 62292
*
* @covers WP_REST_Posts_Controller::get_items
*/
public function test_get_posts_with_pagination() {

// Test offset
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'offset', 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( 30, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 30, $response->get_headers()['X-WP-TotalPages'] );

// Test paged
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'page', 2 );
$request->set_param( 'per_page', 2 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 2, $data );
$this->assertEquals( 30, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 15, $response->get_headers()['X-WP-TotalPages'] );

// Test out of bounds
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'page', 4 );
$request->set_param( 'per_page', 10 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_page_number', $response, 400 );
}

/**
* Internal function used to disable an insert query which
* will trigger a wpdb error for testing purposes.
Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/tests/rest-api/rest-revisions-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,4 +864,44 @@ public function test_get_items_out_of_bounds_page_should_not_error_if_offset() {
$response = rest_get_server()->dispatch( $request );
$this->assertCount( $expected_count, $response->get_data() );
}

/**
* Tests for the pagination.
*
* @ticket 62292
*
* @covers WP_REST_Revisions_Controller::get_items
*/
public function test_get_template_revisions_pagination() {
wp_set_current_user( self::$editor_id );

// Test offset
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_param( 'offset', 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( $this->total_revisions, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( $this->total_revisions, $response->get_headers()['X-WP-TotalPages'] );

// Test paged
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_param( 'page', 2 );
$request->set_param( 'per_page', 2 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( $this->total_revisions, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( (int) ceil( $this->total_revisions / 2 ), $response->get_headers()['X-WP-TotalPages'] );

// Test out of bounds
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_param( 'page', $this->total_revisions + 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_revision_invalid_page_number', $response, 400 );
}
}
40 changes: 40 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1065,4 +1065,44 @@ public function data_delete_item_not_found() {
),
);
}

/**
* Tests for the pagination.
*
* @ticket 62292
*
* @covers WP_REST_Template_Revisions_Controller::get_items
*/
public function test_get_template_revisions_pagination() {
wp_set_current_user( self::$admin_id );

// Test offset
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
$request->set_param( 'offset', 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( 4, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 4, $response->get_headers()['X-WP-TotalPages'] );

// Test paged
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
$request->set_param( 'page', 2 );
$request->set_param( 'per_page', 2 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 2, $data );
$this->assertEquals( 4, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 2, $response->get_headers()['X-WP-TotalPages'] );

// Test out of bounds
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
$request->set_param( 'page', 4 );
$request->set_param( 'per_page', 6 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_revision_invalid_page_number', $response, 400 );
}
}

0 comments on commit d2630e0

Please sign in to comment.