Make WordPress Core


Ignore:
Timestamp:
01/15/2025 06:34:35 PM (11 months ago)
Author:
flixos90
Message:

REST API: Fix PHP warning about undefined paged argument in various 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r57987 r59630  
    865865        $this->assertCount( $expected_count, $response->get_data() );
    866866    }
     867
     868    /**
     869     * Tests for the pagination.
     870     *
     871     * @ticket 62292
     872     *
     873     * @covers WP_REST_Revisions_Controller::get_items
     874     */
     875    public function test_get_template_revisions_pagination() {
     876        wp_set_current_user( self::$editor_id );
     877
     878        // Test offset
     879        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     880        $request->set_param( 'offset', 1 );
     881        $request->set_param( 'per_page', 1 );
     882        $response = rest_get_server()->dispatch( $request );
     883        $this->assertEquals( 200, $response->get_status() );
     884        $data = $response->get_data();
     885        $this->assertCount( 1, $data );
     886        $this->assertEquals( $this->total_revisions, $response->get_headers()['X-WP-Total'] );
     887        $this->assertEquals( $this->total_revisions, $response->get_headers()['X-WP-TotalPages'] );
     888
     889        // Test paged
     890        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     891        $request->set_param( 'page', 2 );
     892        $request->set_param( 'per_page', 2 );
     893        $response = rest_get_server()->dispatch( $request );
     894        $this->assertEquals( 200, $response->get_status() );
     895        $data = $response->get_data();
     896        $this->assertCount( 1, $data );
     897        $this->assertEquals( $this->total_revisions, $response->get_headers()['X-WP-Total'] );
     898        $this->assertEquals( (int) ceil( $this->total_revisions / 2 ), $response->get_headers()['X-WP-TotalPages'] );
     899
     900        // Test out of bounds
     901        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
     902        $request->set_param( 'page', $this->total_revisions + 1 );
     903        $request->set_param( 'per_page', 1 );
     904        $response = rest_get_server()->dispatch( $request );
     905        $this->assertErrorResponse( 'rest_revision_invalid_page_number', $response, 400 );
     906    }
    867907}
Note: See TracChangeset for help on using the changeset viewer.