Make WordPress Core


Ignore:
Timestamp:
09/05/2020 09:50:31 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

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.

File:
1 edited

Legend:

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

    r48939 r48947  
    15141514    }
    15151515
     1516    /**
     1517     * @ticket 50244
     1518     */
     1519    public function test_no_route() {
     1520        $mock_hook = new MockAction();
     1521        add_filter( 'rest_request_after_callbacks', array( $mock_hook, 'filter' ) );
     1522
     1523        $response = rest_do_request( '/test-ns/v1/test' );
     1524        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
     1525
     1526        // Verify that the no route error was not filtered.
     1527        $this->assertCount( 0, $mock_hook->get_events() );
     1528    }
     1529
     1530    /**
     1531     * @ticket 50244
     1532     */
     1533    public function test_invalid_handler() {
     1534        register_rest_route(
     1535            'test-ns/v1',
     1536            '/test',
     1537            array(
     1538                'callback'            => 'invalid_callback',
     1539                'permission_callback' => '__return_true',
     1540            )
     1541        );
     1542
     1543        $mock_hook = new MockAction();
     1544        add_filter( 'rest_request_after_callbacks', array( $mock_hook, 'filter' ) );
     1545
     1546        $response = rest_do_request( '/test-ns/v1/test' );
     1547        $this->assertErrorResponse( 'rest_invalid_handler', $response, 500 );
     1548
     1549        // Verify that the invalid handler error was filtered.
     1550        $events = $mock_hook->get_events();
     1551        $this->assertCount( 1, $events );
     1552        $this->assertWPError( $events[0]['args'][0] );
     1553        $this->assertEquals( 'rest_invalid_handler', $events[0]['args'][0]->get_error_code() );
     1554    }
     1555
     1556    /**
     1557     * @ticket 50244
     1558     */
     1559    public function test_callbacks_are_not_executed_if_request_validation_fails() {
     1560        $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1561        $callback->expects( self::never() )->method( '__invoke' );
     1562        $permission_callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1563        $permission_callback->expects( self::never() )->method( '__invoke' );
     1564
     1565        register_rest_route(
     1566            'test-ns/v1',
     1567            '/test',
     1568            array(
     1569                'callback'            => $callback,
     1570                'permission_callback' => $permission_callback,
     1571                'args'                => array(
     1572                    'test' => array(
     1573                        'validate_callback' => '__return_false',
     1574                    ),
     1575                ),
     1576            )
     1577        );
     1578
     1579        $request = new WP_REST_Request( 'GET', '/test-ns/v1/test' );
     1580        $request->set_query_params( array( 'test' => 'world' ) );
     1581        $response = rest_do_request( $request );
     1582
     1583        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1584    }
     1585
     1586    /**
     1587     * @ticket 50244
     1588     */
     1589    public function test_filters_are_executed_if_request_validation_fails() {
     1590        register_rest_route(
     1591            'test-ns/v1',
     1592            '/test',
     1593            array(
     1594                'callback'            => '__return_empty_array',
     1595                'permission_callback' => '__return_true',
     1596                'args'                => array(
     1597                    'test' => array(
     1598                        'validate_callback' => '__return_false',
     1599                    ),
     1600                ),
     1601            )
     1602        );
     1603
     1604        $mock_hook = new MockAction();
     1605        add_filter( 'rest_request_after_callbacks', array( $mock_hook, 'filter' ) );
     1606
     1607        $request = new WP_REST_Request( 'GET', '/test-ns/v1/test' );
     1608        $request->set_query_params( array( 'test' => 'world' ) );
     1609        $response = rest_do_request( $request );
     1610
     1611        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1612
     1613        // Verify that the invalid param error was filtered.
     1614        $events = $mock_hook->get_events();
     1615        $this->assertCount( 1, $events );
     1616        $this->assertWPError( $events[0]['args'][0] );
     1617        $this->assertEquals( 'rest_invalid_param', $events[0]['args'][0]->get_error_code() );
     1618    }
     1619
    15161620    public function _validate_as_integer_123( $value, $request, $key ) {
    15171621        if ( ! is_int( $value ) ) {
Note: See TracChangeset for help on using the changeset viewer.