Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

62292 warning in wp rest posts controllerget items #8021

Original file line number Diff line number Diff line change
@@ -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 ) {
Original file line number Diff line number Diff line change
@@ -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 ) {
Original file line number Diff line number Diff line change
@@ -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 ) {
Original file line number Diff line number Diff line change
@@ -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
*/
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
@@ -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.
40 changes: 40 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php
Original file line number Diff line number Diff line change
@@ -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
Loading