Make WordPress Core


Ignore:
Timestamp:
01/15/2025 06:34:35 PM (21 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/wpRestTemplateRevisionsController.php

    r59605 r59630  
    10661066                );
    10671067        }
     1068
     1069        /**
     1070         * Tests for the pagination.
     1071         *
     1072         * @ticket 62292
     1073         *
     1074         * @covers WP_REST_Template_Revisions_Controller::get_items
     1075         */
     1076        public function test_get_template_revisions_pagination() {
     1077                wp_set_current_user( self::$admin_id );
     1078
     1079                // Test offset
     1080                $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
     1081                $request->set_param( 'offset', 1 );
     1082                $request->set_param( 'per_page', 1 );
     1083                $response = rest_get_server()->dispatch( $request );
     1084                $this->assertEquals( 200, $response->get_status() );
     1085                $data = $response->get_data();
     1086                $this->assertCount( 1, $data );
     1087                $this->assertEquals( 4, $response->get_headers()['X-WP-Total'] );
     1088                $this->assertEquals( 4, $response->get_headers()['X-WP-TotalPages'] );
     1089
     1090                // Test paged
     1091                $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
     1092                $request->set_param( 'page', 2 );
     1093                $request->set_param( 'per_page', 2 );
     1094                $response = rest_get_server()->dispatch( $request );
     1095                $this->assertEquals( 200, $response->get_status() );
     1096                $data = $response->get_data();
     1097                $this->assertCount( 2, $data );
     1098                $this->assertEquals( 4, $response->get_headers()['X-WP-Total'] );
     1099                $this->assertEquals( 2, $response->get_headers()['X-WP-TotalPages'] );
     1100
     1101                // Test out of bounds
     1102                $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
     1103                $request->set_param( 'page', 4 );
     1104                $request->set_param( 'per_page', 6 );
     1105                $response = rest_get_server()->dispatch( $request );
     1106                $this->assertErrorResponse( 'rest_revision_invalid_page_number', $response, 400 );
     1107        }
    10681108}
Note: See TracChangeset for help on using the changeset viewer.