Make WordPress Core

Ticket #41555: 41555.3.diff

File 41555.3.diff, 4.2 KB (added by jnylen0, 8 years ago)

Fix PHPUnit setUp and tearDown calls

  • 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..90a480d 100644
    a b class Tests_REST_API extends WP_UnitTestCase { 
    1616        public function setUp() {
    1717                // Override the normal server with our spying server.
    1818                $GLOBALS['wp_rest_server'] = new Spy_REST_Server();
    19                 parent::setup();
     19                parent::setUp();
     20        }
     21
     22        public function tearDown() {
     23                remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
     24                parent::tearDown();
    2025        }
    2126
    2227        /**
    class Tests_REST_API extends WP_UnitTestCase { 
    446451        public function test_rest_parse_date_force_utc( $string, $value ) {
    447452                $this->assertEquals( $value, rest_parse_date( $string, true ) );
    448453        }
     454
     455        public function filter_wp_rest_server_class( $class_name ) {
     456                return 'Spy_REST_Server';
     457        }
     458
     459        public function test_register_rest_route_without_server() {
     460                $GLOBALS['wp_rest_server'] = null;
     461                add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
     462
     463                register_rest_route( 'test-ns', '/test', array(
     464                        'methods'  => array( 'GET' ),
     465                        'callback' => '__return_null',
     466                ) );
     467
     468                $routes = $GLOBALS['wp_rest_server']->get_routes();
     469                $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
     470        }
    449471}