mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
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:
@@ -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' ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user