Make WordPress Core


Ignore:
Timestamp:
05/23/2020 02:34:38 PM (3 years ago)
Author:
ocean90
Message:

Rest API: Ensure rest_ensure_response() upgrades WP_HTTP_Response to WP_REST_Response.

An instance of WP_HTTP_Response doesn't ensure that the required methods used in WP_REST_Server::dispatch() exist, currently causing a fatal error.

Props ali11007, TimothyBlynJacobs, ocean90.
Fixes #49495.

File:
1 edited

Legend:

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

    r47842 r47849  
    943943     * @ticket 40614
    944944     */
    945     function test_rest_ensure_response_accepts_path_string() {
     945    function test_rest_ensure_request_accepts_path_string() {
    946946        $request = rest_ensure_request( '/wp/v2/posts' );
    947947        $this->assertInstanceOf( 'WP_REST_Request', $request );
     
    13161316        );
    13171317    }
     1318
     1319    function test_rest_ensure_response_accepts_wp_error_and_returns_wp_error() {
     1320        $response = rest_ensure_response( new WP_Error() );
     1321        $this->assertInstanceOf( 'WP_Error', $response );
     1322    }
     1323
     1324    /**
     1325     * @dataProvider rest_ensure_response_data_provider
     1326     * @group test1
     1327     *
     1328     * @param mixed $response      The response passed to rest_ensure_response().
     1329     * @param mixed $expected_data The expected data a response should include.
     1330     */
     1331    function test_rest_ensure_response_returns_instance_of_wp_rest_response( $response, $expected_data ) {
     1332        $response_object = rest_ensure_response( $response );
     1333        $this->assertInstanceOf( 'WP_REST_Response', $response_object );
     1334        $this->assertSame( $expected_data, $response_object->get_data() );
     1335    }
     1336
     1337    /**
     1338     * Data provider for test_rest_ensure_response_returns_instance_of_wp_rest_response().
     1339     *
     1340     * @return array
     1341     */
     1342    function rest_ensure_response_data_provider() {
     1343        return array(
     1344            array( null, null ),
     1345            array( array( 'chocolate' => 'cookies' ), array( 'chocolate' => 'cookies' ) ),
     1346            array( 123, 123 ),
     1347            array( true, true ),
     1348            array( 'chocolate', 'chocolate' ),
     1349            array( new WP_HTTP_Response( 'http' ), 'http' ),
     1350            array( new WP_REST_Response( 'rest' ), 'rest' ),
     1351        );
     1352    }
    13181353}
Note: See TracChangeset for help on using the changeset viewer.