diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php index 54285fa560bee..4108f9711c8a7 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php @@ -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 ) { 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 8852519ec45c9..1a5a7dbeaed84 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 @@ -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 ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index fb5fa29231a4f..1dbc611631b9b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php @@ -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 ) { diff --git a/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php b/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php index b01ada9be28ff..6f347899d021c 100644 --- a/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php +++ b/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php @@ -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 */ diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 4574bde83621e..4f6fa4d5038f9 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -5637,6 +5637,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. diff --git a/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php b/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php index 014a191f0a13f..d23ec53ba8a53 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php @@ -299,6 +299,46 @@ public function test_get_items() { ); } + /** + * 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 ); + } + /** * @covers WP_REST_Template_Revisions_Controller::get_items_permissions_check