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.

git-svn-id: https://develop.svn.wordpress.org/trunk@47849 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling
2020-05-23 14:34:38 +00:00
parent 772a11b72a
commit 584df2a169
3 changed files with 58 additions and 13 deletions

View File

@@ -942,7 +942,7 @@ class Tests_REST_API extends WP_UnitTestCase {
/**
* @ticket 40614
*/
function test_rest_ensure_response_accepts_path_string() {
function test_rest_ensure_request_accepts_path_string() {
$request = rest_ensure_request( '/wp/v2/posts' );
$this->assertInstanceOf( 'WP_REST_Request', $request );
$this->assertEquals( '/wp/v2/posts', $request->get_route() );
@@ -1315,4 +1315,39 @@ class Tests_REST_API extends WP_UnitTestCase {
),
);
}
function test_rest_ensure_response_accepts_wp_error_and_returns_wp_error() {
$response = rest_ensure_response( new WP_Error() );
$this->assertInstanceOf( 'WP_Error', $response );
}
/**
* @dataProvider rest_ensure_response_data_provider
* @group test1
*
* @param mixed $response The response passed to rest_ensure_response().
* @param mixed $expected_data The expected data a response should include.
*/
function test_rest_ensure_response_returns_instance_of_wp_rest_response( $response, $expected_data ) {
$response_object = rest_ensure_response( $response );
$this->assertInstanceOf( 'WP_REST_Response', $response_object );
$this->assertSame( $expected_data, $response_object->get_data() );
}
/**
* Data provider for test_rest_ensure_response_returns_instance_of_wp_rest_response().
*
* @return array
*/
function rest_ensure_response_data_provider() {
return array(
array( null, null ),
array( array( 'chocolate' => 'cookies' ), array( 'chocolate' => 'cookies' ) ),
array( 123, 123 ),
array( true, true ),
array( 'chocolate', 'chocolate' ),
array( new WP_HTTP_Response( 'http' ), 'http' ),
array( new WP_REST_Response( 'rest' ), 'rest' ),
);
}
}