Make WordPress Core

Changeset 64006


Ignore:
Timestamp:
09/29/2026 09:49:08 PM (5 weeks ago)
Author:
lancewillett
Message:

Tests: Initialize the REST API test server only when needed.

Avoid initializing the REST server for tests that do not use it. Initialize it explicitly in route tests and reset the global server between tests. Remove redundant server restoration from preload tests.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13694

Props sainathpoojary, mindctrl.
See #66074.

File:
1 edited

Legend:

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

    r63882 r64006  
    1818                parent::set_up();
    1919
    20                 // Override the normal server with our spying server.
    21                 $GLOBALS['wp_rest_server'] = new Spy_REST_Server();
    22                 do_action( 'rest_api_init', $GLOBALS['wp_rest_server'] );
     20                $GLOBALS['wp_rest_server'] = null;
    2321        }
    2422
    2523        public function tear_down() {
     24                $GLOBALS['wp_rest_server'] = null;
    2625                remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
    2726                parent::tear_down();
    … …  
    9089         */
    9190        public function test_route_canonicalized() {
     91                rest_get_server();
     92
    9293                register_rest_route(
    9394                        'test-ns',
    … …  
    126127         */
    127128        public function test_route_canonicalized_multiple() {
     129                rest_get_server();
     130
    128131                register_rest_route(
    129132                        'test-ns',
    … …  
    169172         */
    170173        public function test_route_merge() {
     174                rest_get_server();
     175
    171176                register_rest_route(
    172177                        'test-ns',
    … …  
    198203         */
    199204        public function test_route_override() {
     205                rest_get_server();
     206
    200207                register_rest_route(
    201208                        'test-ns',
    … …  
    236243         */
    237244        public function test_route_reject_empty_namespace() {
     245                rest_get_server();
     246
    238247                register_rest_route(
    239248                        '',
    … …  
    256265         */
    257266        public function test_route_reject_empty_route() {
     267                rest_get_server();
     268
    258269                register_rest_route(
    259270                        '/test-empty-route',
    … …  
    279290
    280291        public function test_route_method() {
     292                rest_get_server();
     293
    281294                register_rest_route(
    282295                        'test-ns',
    … …  
    298311         */
    299312        public function test_route_method_string() {
     313                rest_get_server();
     314
    300315                register_rest_route(
    301316                        'test-ns',
    … …  
    317332         */
    318333        public function test_route_method_array() {
     334                rest_get_server();
     335
    319336                register_rest_route(
    320337                        'test-ns',
    … …  
    342359         */
    343360        public function test_route_method_comma_separated() {
     361                rest_get_server();
     362
    344363                register_rest_route(
    345364                        'test-ns',
    … …  
    372391         */
    373392        public function test_route_method_array_with_comma_separated_values() {
     393                rest_get_server();
     394
    374395                global $wp_rest_server;
    375396
    … …  
    407428         */
    408429        public function test_route_method_array_with_multi_method_constant() {
     430                rest_get_server();
     431
    409432                global $wp_rest_server;
    410433
    … …  
    441464         */
    442465        public function test_route_method_array_with_multi_method_constant_dispatches() {
     466                rest_get_server();
     467
    443468                register_rest_route(
    444469                        'test-ns',
    … …  
    468493         */
    469494        public function test_route_method_empty_array() {
     495                rest_get_server();
     496
    470497                global $wp_rest_server;
    471498
    … …  
    486513
    487514        public function test_options_request() {
     515                rest_get_server();
     516
    488517                register_rest_route(
    489518                        'test-ns',
    … …  
    509538         */
    510539        public function test_options_request_not_options() {
     540                rest_get_server();
     541
    511542                register_rest_route(
    512543                        'test-ns',
    … …  
    10441075
    10451076        public function test_register_rest_route_without_server() {
     1077                rest_get_server();
     1078
    10461079                $GLOBALS['wp_rest_server'] = null;
    10471080                add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
    … …  
    10621095
    10631096        public function test_rest_preload_api_request_with_method() {
    1064                 $rest_server               = $GLOBALS['wp_rest_server'];
    1065                 $GLOBALS['wp_rest_server'] = null;
    1066 
    10671097                $preload_paths = array(
    10681098                        '/wp/v2/types',
    … …  
    10781108                $this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS' ) );
    10791109                $this->assertArrayHasKey( '/wp/v2/media', $preload_data['OPTIONS'] );
    1080 
    1081                 $GLOBALS['wp_rest_server'] = $rest_server;
    10821110        }
    10831111
    … …  
    10921120         */
    10931121        public function test_rest_preload_api_request_removes_trailing_slashes( $preload_path, $expected_preload_path ) {
    1094                 $rest_server               = $GLOBALS['wp_rest_server'];
    1095                 $GLOBALS['wp_rest_server'] = null;
    1096 
    10971122                $actual_preload_path = rest_preload_api_request( array(), $preload_path );
    10981123                if ( '' !== $preload_path ) {
    … …  
    11001125                }
    11011126                $this->assertSame( $expected_preload_path, $actual_preload_path );
    1102 
    1103                 $GLOBALS['wp_rest_server'] = $rest_server;
    11041127        }
    11051128
    … …  
    11731196         */
    11741197        public function test_register_route_with_invalid_namespace() {
     1198                rest_get_server();
     1199
    11751200                $this->setExpectedIncorrectUsage( 'register_rest_route' );
    11761201
    … …  
    11941219         */
    11951220        public function test_register_route_with_missing_permission_callback_top_level_route() {
     1221                rest_get_server();
     1222
    11961223                $this->setExpectedIncorrectUsage( 'register_rest_route' );
    11971224
    … …  
    12111238         */
    12121239        public function test_register_route_with_missing_permission_callback_single_wrapped_route() {
     1240                rest_get_server();
     1241
    12131242                $this->setExpectedIncorrectUsage( 'register_rest_route' );
    12141243
    … …  
    12311260         */
    12321261        public function test_register_route_with_missing_permission_callback_multiple_wrapped_route() {
     1262                rest_get_server();
     1263
    12331264                $this->setExpectedIncorrectUsage( 'register_rest_route' );
    12341265
    … …  
    28692900         */
    28702901        public function test_route_args_is_array_of_arrays() {
     2902                rest_get_server();
     2903
    28712904                $this->setExpectedIncorrectUsage( 'register_rest_route' );
    28722905
Note: See TracChangeset for help on using the changeset viewer.