WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/10/2017 01:37:30 AM (3 years ago)
Author:
jnylen0
Message:

REST API: Always call rest_get_server() instead of accessing the $wp_rest_server global.

This is a consistency improvement and also a bug fix for fairly obscure cases involving modified WP load order.

Fixes #41555.

File:
1 edited

Legend:

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

    r41139 r41238  
    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
     
    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}
Note: See TracChangeset for help on using the changeset viewer.