mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
REST API: Refactor WP_REST_Server::dispatch() to make internal logic reusable.
#50244 aims to introduce batch processing in the REST API. An important feature is the ability to enforce that all requests have valid data before executing the route callbacks in "pre-validate" mode. This necessitates splitting `WP_REST_Server::dispatch()` into two methods so the batch controller can determine the request handler to perform pre-validation and then respond to the requests. The two new methods, `match_request_to_handler` and `respond_to_request`, have a public visibility, but are marked as `@access private`. This is to allow for iteration on the batch controller to happen in the Gutenberg repository. Developers should not rely upon these methods, their visibility may change in the future. See #50244. Props andraganescu, zieladam, TimothyBlynJacobs. git-svn-id: https://develop.svn.wordpress.org/trunk@48947 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1513,6 +1513,110 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
|
||||
$this->assertSame( 204, $response->get_status(), '/test-ns/v1/test' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50244
|
||||
*/
|
||||
public function test_no_route() {
|
||||
$mock_hook = new MockAction();
|
||||
add_filter( 'rest_request_after_callbacks', array( $mock_hook, 'filter' ) );
|
||||
|
||||
$response = rest_do_request( '/test-ns/v1/test' );
|
||||
$this->assertErrorResponse( 'rest_no_route', $response, 404 );
|
||||
|
||||
// Verify that the no route error was not filtered.
|
||||
$this->assertCount( 0, $mock_hook->get_events() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50244
|
||||
*/
|
||||
public function test_invalid_handler() {
|
||||
register_rest_route(
|
||||
'test-ns/v1',
|
||||
'/test',
|
||||
array(
|
||||
'callback' => 'invalid_callback',
|
||||
'permission_callback' => '__return_true',
|
||||
)
|
||||
);
|
||||
|
||||
$mock_hook = new MockAction();
|
||||
add_filter( 'rest_request_after_callbacks', array( $mock_hook, 'filter' ) );
|
||||
|
||||
$response = rest_do_request( '/test-ns/v1/test' );
|
||||
$this->assertErrorResponse( 'rest_invalid_handler', $response, 500 );
|
||||
|
||||
// Verify that the invalid handler error was filtered.
|
||||
$events = $mock_hook->get_events();
|
||||
$this->assertCount( 1, $events );
|
||||
$this->assertWPError( $events[0]['args'][0] );
|
||||
$this->assertEquals( 'rest_invalid_handler', $events[0]['args'][0]->get_error_code() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50244
|
||||
*/
|
||||
public function test_callbacks_are_not_executed_if_request_validation_fails() {
|
||||
$callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
|
||||
$callback->expects( self::never() )->method( '__invoke' );
|
||||
$permission_callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
|
||||
$permission_callback->expects( self::never() )->method( '__invoke' );
|
||||
|
||||
register_rest_route(
|
||||
'test-ns/v1',
|
||||
'/test',
|
||||
array(
|
||||
'callback' => $callback,
|
||||
'permission_callback' => $permission_callback,
|
||||
'args' => array(
|
||||
'test' => array(
|
||||
'validate_callback' => '__return_false',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/v1/test' );
|
||||
$request->set_query_params( array( 'test' => 'world' ) );
|
||||
$response = rest_do_request( $request );
|
||||
|
||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50244
|
||||
*/
|
||||
public function test_filters_are_executed_if_request_validation_fails() {
|
||||
register_rest_route(
|
||||
'test-ns/v1',
|
||||
'/test',
|
||||
array(
|
||||
'callback' => '__return_empty_array',
|
||||
'permission_callback' => '__return_true',
|
||||
'args' => array(
|
||||
'test' => array(
|
||||
'validate_callback' => '__return_false',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$mock_hook = new MockAction();
|
||||
add_filter( 'rest_request_after_callbacks', array( $mock_hook, 'filter' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/v1/test' );
|
||||
$request->set_query_params( array( 'test' => 'world' ) );
|
||||
$response = rest_do_request( $request );
|
||||
|
||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||
|
||||
// Verify that the invalid param error was filtered.
|
||||
$events = $mock_hook->get_events();
|
||||
$this->assertCount( 1, $events );
|
||||
$this->assertWPError( $events[0]['args'][0] );
|
||||
$this->assertEquals( 'rest_invalid_param', $events[0]['args'][0]->get_error_code() );
|
||||
}
|
||||
|
||||
public function _validate_as_integer_123( $value, $request, $key ) {
|
||||
if ( ! is_int( $value ) ) {
|
||||
return new WP_Error( 'some-error', 'This is not valid!' );
|
||||
|
||||
Reference in New Issue
Block a user