Make WordPress Core

Ticket #41555: 41555.2.diff

File 41555.2.diff, 4.1 KB (added by jnylen0, 8 years ago)

Add a previously failing test case

  • src/wp-includes/rest-api.php

    diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
    index 3b158ea..06bb9f9 100644
    a b define( 'REST_API_VERSION', '2.0' ); 
    1919 *
    2020 * @since 4.4.0
    2121 *
    22  * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server).
    23  *
    2422 * @param string $namespace The first URL segment after core prefix. Should be unique to your package/plugin.
    2523 * @param string $route     The base URL for route you are adding.
    2624 * @param array  $args      Optional. Either an array of options for the endpoint, or an array of arrays for
    define( 'REST_API_VERSION', '2.0' ); 
    3028 * @return bool True on success, false on error.
    3129 */
    3230function register_rest_route( $namespace, $route, $args = array(), $override = false ) {
    33         /** @var WP_REST_Server $wp_rest_server */
    34         global $wp_rest_server;
    35 
    3631        if ( empty( $namespace ) ) {
    3732                /*
    3833                 * Non-namespaced routes are not allowed, with the exception of the main
    function register_rest_route( $namespace, $route, $args = array(), $override = f 
    7469        }
    7570
    7671        $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
    77         $wp_rest_server->register_route( $namespace, $full_route, $args, $override );
     72        rest_get_server()->register_route( $namespace, $full_route, $args, $override );
    7873        return true;
    7974}
    8075
    function create_initial_rest_routes() { 
    245240 * @since 4.4.0
    246241 *
    247242 * @global WP             $wp             Current WordPress environment instance.
    248  * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server).
    249243 */
    250244function rest_api_loaded() {
    251245        if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
    function rest_url( $path = '', $scheme = 'json' ) { 
    387381 *
    388382 * @since 4.4.0
    389383 *
    390  * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server).
    391  *
    392384 * @param WP_REST_Request|string $request Request.
    393385 * @return WP_REST_Response REST response.
    394386 */
    function rest_output_link_header() { 
    704696 * @since 4.4.0
    705697 *
    706698 * @global mixed          $wp_rest_auth_cookie
    707  * @global WP_REST_Server $wp_rest_server      REST server instance.
    708699 *
    709700 * @param WP_Error|mixed $result Error from another authentication handler,
    710701 *                               null if we should handle it, or another value
    function rest_cookie_check_errors( $result ) { 
    716707                return $result;
    717708        }
    718709
    719         global $wp_rest_auth_cookie, $wp_rest_server;
     710        global $wp_rest_auth_cookie;
    720711
    721712        /*
    722713         * Is cookie authentication being used? (If we get an auth
    function rest_cookie_check_errors( $result ) { 
    750741        }
    751742
    752743        // Send a refreshed nonce in header.
    753         $wp_rest_server->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
     744        rest_get_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
    754745
    755746        return true;
    756747}
  • tests/phpunit/tests/rest-api.php

    diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php
    index 689cbdf..64ce29c 100644
    a b class Tests_REST_API extends WP_UnitTestCase { 
    1919                parent::setup();
    2020        }
    2121
     22        public function tearDown() {
     23                remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
     24        }
     25
    2226        /**
    2327         * Checks that the main classes are loaded.
    2428         */
    class Tests_REST_API extends WP_UnitTestCase { 
    446450        public function test_rest_parse_date_force_utc( $string, $value ) {
    447451                $this->assertEquals( $value, rest_parse_date( $string, true ) );
    448452        }
     453
     454        public function filter_wp_rest_server_class( $class_name ) {
     455                return 'Spy_REST_Server';
     456        }
     457
     458        public function test_register_rest_route_without_server() {
     459                $GLOBALS['wp_rest_server'] = null;
     460                add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
     461
     462                register_rest_route( 'test-ns', '/test', array(
     463                        'methods'  => array( 'GET' ),
     464                        'callback' => '__return_null',
     465                ) );
     466
     467                $routes = $GLOBALS['wp_rest_server']->get_routes();
     468                $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
     469        }
    449470}