Make WordPress Core


Ignore:
Timestamp:
02/21/2018 04:24:30 PM (7 years ago)
Author:
SergeyBiryukov
Message:

Tests: Replace use of $this->server with rest_get_server() for better memory recycling.

Props danielbachhuber.
Fixes #41641.

File:
1 edited

Legend:

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

    r42343 r42724  
    1717        $GLOBALS['wp_rest_server'] = null;
    1818        add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
    19         $this->server = rest_get_server();
     19        $GLOBALS['wp_rest_server'] = rest_get_server();
    2020        remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
    2121    }
     
    4646        $response->header( 'Multiple', 'yes', false );
    4747
    48         $envelope_response = $this->server->envelope_response( $response, false );
     48        $envelope_response = rest_get_server()->envelope_response( $response, false );
    4949
    5050        // The envelope should still be a response, but with defaults.
     
    7676
    7777        $request  = new WP_REST_Request( 'GET', '/test-ns/test' );
    78         $response = $this->server->dispatch( $request );
     78        $response = rest_get_server()->dispatch( $request );
    7979
    8080        $this->assertEquals( 'bar', $request['foo'] );
     
    9797        $request = new WP_REST_Request( 'GET', '/test-ns/test' );
    9898        $request->set_query_params( array( 'foo' => 123 ) );
    99         $response = $this->server->dispatch( $request );
     99        $response = rest_get_server()->dispatch( $request );
    100100
    101101        $this->assertEquals( '123', $request['foo'] );
     
    115115        $request = new WP_REST_Request( 'GET', '/optional/test' );
    116116        $request->set_query_params( array() );
    117         $response = $this->server->dispatch( $request );
     117        $response = rest_get_server()->dispatch( $request );
    118118        $this->assertInstanceOf( 'WP_REST_Response', $response );
    119119        $this->assertEquals( 200, $response->get_status() );
     
    134134        );
    135135        $request = new WP_REST_Request( 'GET', '/no-zero/test' );
    136         $this->server->dispatch( $request );
     136        rest_get_server()->dispatch( $request );
    137137        $this->assertEquals( array( 'foo' => 'bar' ), $request->get_params() );
    138138    }
     
    146146        );
    147147        $request  = new WP_REST_Request( 'HEAD', '/head-request/test' );
    148         $response = $this->server->dispatch( $request );
     148        $response = rest_get_server()->dispatch( $request );
    149149        $this->assertEquals( 200, $response->get_status() );
    150150    }
     
    171171        );
    172172        $request  = new WP_REST_Request( 'HEAD', '/head-request/test' );
    173         $response = $this->server->dispatch( $request );
     173        $response = rest_get_server()->dispatch( $request );
    174174        $this->assertEquals( 200, $response->get_status() );
    175175    }
     
    177177    public function test_url_params_no_numeric_keys() {
    178178
    179         $this->server->register_route(
     179        rest_get_server()->register_route(
    180180            'test', '/test/(?P<data>.*)', array(
    181181                array(
     
    190190
    191191        $request = new WP_REST_Request( 'GET', '/test/some-value' );
    192         $this->server->dispatch( $request );
     192        rest_get_server()->dispatch( $request );
    193193        $this->assertEquals( array( 'data' => 'some-value' ), $request->get_params() );
    194194    }
     
    209209
    210210        $request = new WP_REST_Request( 'GET', '/test-ns/test', array() );
    211         $result  = $this->server->dispatch( $request );
     211        $result  = rest_get_server()->dispatch( $request );
    212212
    213213        $this->assertEquals( 403, $result->get_status() );
     
    234234        wp_set_current_user( $editor );
    235235
    236         $result = $this->server->dispatch( $request );
     236        $result = rest_get_server()->dispatch( $request );
    237237
    238238        $this->assertEquals( 200, $result->get_status() );
     
    255255        $request = new WP_REST_Request( 'GET', '/test-ns/test', array() );
    256256
    257         $result = $this->server->dispatch( $request );
    258         $result = apply_filters( 'rest_post_dispatch', $result, $this->server, $request );
     257        $result = rest_get_server()->dispatch( $request );
     258        $result = apply_filters( 'rest_post_dispatch', $result, rest_get_server(), $request );
    259259
    260260        $this->assertFalse( $result->get_status() !== 200 );
     
    288288        $request = new WP_REST_Request( 'GET', '/test-ns/test', array() );
    289289
    290         $result = $this->server->dispatch( $request );
     290        $result = rest_get_server()->dispatch( $request );
    291291
    292292        $this->assertFalse( $result->get_status() !== 200 );
    293293
    294         $result = apply_filters( 'rest_post_dispatch', $result, $this->server, $request );
     294        $result = apply_filters( 'rest_post_dispatch', $result, rest_get_server(), $request );
    295295
    296296        $sent_headers = $result->get_headers();
     
    323323        $request = new WP_REST_Request( 'GET', '/test-ns/test', array() );
    324324
    325         $result = $this->server->dispatch( $request );
    326         $result = apply_filters( 'rest_post_dispatch', $result, $this->server, $request );
     325        $result = rest_get_server()->dispatch( $request );
     326        $result = apply_filters( 'rest_post_dispatch', $result, rest_get_server(), $request );
    327327
    328328        $this->assertEquals( $result->get_status(), 403 );
     
    348348
    349349        $request  = new WP_REST_Request( 'OPTIONS', '/test-ns/test' );
    350         $response = $this->server->dispatch( $request );
    351 
    352         $result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this->server, $request );
     350        $response = rest_get_server()->dispatch( $request );
     351
     352        $result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), rest_get_server(), $request );
    353353
    354354        $headers = $result->get_headers();
     
    366366        $error   = new WP_Error( $code, $message );
    367367
    368         $response = $this->server->error_to_response( $error );
     368        $response = rest_get_server()->error_to_response( $error );
    369369        $this->assertInstanceOf( 'WP_REST_Response', $response );
    370370
     
    383383        $error   = new WP_Error( $code, $message, array( 'status' => 400 ) );
    384384
    385         $response = $this->server->error_to_response( $error );
     385        $response = rest_get_server()->error_to_response( $error );
    386386        $this->assertInstanceOf( 'WP_REST_Response', $response );
    387387
     
    402402        $error->add( $code2, $message2, array( 'status' => 403 ) );
    403403
    404         $response = $this->server->error_to_response( $error );
     404        $response = rest_get_server()->error_to_response( $error );
    405405        $this->assertInstanceOf( 'WP_REST_Response', $response );
    406406
     
    421421        );
    422422        $expected = wp_json_encode( $data );
    423         $response = $this->server->json_error( 'wp-api-test-error', 'Message text' );
     423        $response = rest_get_server()->json_error( 'wp-api-test-error', 'Message text' );
    424424
    425425        $this->assertEquals( $expected, $response );
     
    451451        $response->add_link( 'alternate', 'http://example.org/', array( 'type' => 'application/xml' ) );
    452452
    453         $data = $this->server->response_to_data( $response, false );
     453        $data = rest_get_server()->response_to_data( $response, false );
    454454        $this->assertArrayHasKey( '_links', $data );
    455455
     
    468468    public function test_link_embedding() {
    469469        // Register our testing route.
    470         $this->server->register_route(
     470        rest_get_server()->register_route(
    471471            'test', '/test/embeddable', array(
    472472                'methods'  => 'GET',
     
    482482        $response->add_link( 'alternate', rest_url( '/test/embeddable' ), array( 'embeddable' => true ) );
    483483
    484         $data = $this->server->response_to_data( $response, true );
     484        $data = rest_get_server()->response_to_data( $response, true );
    485485        $this->assertArrayHasKey( '_embedded', $data );
    486486
     
    501501        $response->add_link( 'https://api.w.org/term', 'http://example.com/' );
    502502
    503         $data  = $this->server->response_to_data( $response, false );
     503        $data  = rest_get_server()->response_to_data( $response, false );
    504504        $links = $data['_links'];
    505505
     
    514514        add_filter( 'rest_response_link_curies', array( $this, 'add_custom_curie' ) );
    515515
    516         $data  = $this->server->response_to_data( $response, false );
     516        $data  = rest_get_server()->response_to_data( $response, false );
    517517        $links = $data['_links'];
    518518
     
    541541    public function test_link_embedding_self() {
    542542        // Register our testing route.
    543         $this->server->register_route(
     543        rest_get_server()->register_route(
    544544            'test', '/test/embeddable', array(
    545545                'methods'  => 'GET',
     
    552552        $response->add_link( 'self', rest_url( '/test/notembeddable' ), array( 'embeddable' => true ) );
    553553
    554         $data = $this->server->response_to_data( $response, true );
     554        $data = rest_get_server()->response_to_data( $response, true );
    555555
    556556        $this->assertArrayNotHasKey( '_embedded', $data );
     
    562562    public function test_link_embedding_params() {
    563563        // Register our testing route.
    564         $this->server->register_route(
     564        rest_get_server()->register_route(
    565565            'test', '/test/embeddable', array(
    566566                'methods'  => 'GET',
     
    574574        $response->add_link( 'alternate', $url, array( 'embeddable' => true ) );
    575575
    576         $data = $this->server->response_to_data( $response, true );
     576        $data = rest_get_server()->response_to_data( $response, true );
    577577
    578578        $this->assertArrayHasKey( '_embedded', $data );
     
    588588    public function test_link_embedding_error() {
    589589        // Register our testing route.
    590         $this->server->register_route(
     590        rest_get_server()->register_route(
    591591            'test', '/test/embeddable', array(
    592592                'methods'  => 'GET',
     
    600600        $response->add_link( 'up', $url, array( 'embeddable' => true ) );
    601601
    602         $data = $this->server->response_to_data( $response, true );
     602        $data = rest_get_server()->response_to_data( $response, true );
    603603
    604604        $this->assertArrayHasKey( '_embedded', $data );
     
    622622            'untouched' => 'data',
    623623        );
    624         $result = $this->server->embed_links( $data );
     624        $result = rest_get_server()->embed_links( $data );
    625625
    626626        $this->assertArrayNotHasKey( '_links', $data );
     
    651651        $response->remove_link( 'self' );
    652652
    653         $data = $this->server->response_to_data( $response, false );
     653        $data = rest_get_server()->response_to_data( $response, false );
    654654        $this->assertArrayHasKey( '_links', $data );
    655655
     
    670670        $response->remove_link( 'self', 'https://example.com/' );
    671671
    672         $data = $this->server->response_to_data( $response, false );
     672        $data = rest_get_server()->response_to_data( $response, false );
    673673        $this->assertArrayHasKey( '_links', $data );
    674674
     
    790790        $request = new WP_REST_Request( 'GET', '/', array() );
    791791
    792         $result  = $this->server->serve_request( '/' );
    793         $headers = $this->server->sent_headers;
     792        $result  = rest_get_server()->serve_request( '/' );
     793        $headers = rest_get_server()->sent_headers;
    794794
    795795        $this->assertEquals( 'noindex', $headers['X-Robots-Tag'] );
     
    802802    public function test_rest_enable_filter_is_deprecated() {
    803803        add_filter( 'rest_enabled', '__return_false' );
    804         $this->server->serve_request( '/' );
     804        rest_get_server()->serve_request( '/' );
    805805        remove_filter( 'rest_enabled', '__return_false' );
    806806
    807         $result = json_decode( $this->server->sent_body );
     807        $result = json_decode( rest_get_server()->sent_body );
    808808
    809809        $this->assertObjectNotHasAttribute( 'code', $result );
     
    815815        $request = new WP_REST_Request( 'GET', '/', array() );
    816816
    817         $result  = $this->server->serve_request( '/' );
    818         $headers = $this->server->sent_headers;
     817        $result  = rest_get_server()->serve_request( '/' );
     818        $headers = rest_get_server()->sent_headers;
    819819
    820820        $this->assertEquals( '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"', $headers['Link'] );
     
    826826        wp_set_current_user( $editor );
    827827
    828         $result  = $this->server->serve_request( '/' );
    829         $headers = $this->server->sent_headers;
     828        $result  = rest_get_server()->serve_request( '/' );
     829        $headers = rest_get_server()->sent_headers;
    830830
    831831        foreach ( wp_get_nocache_headers() as $header => $value ) {
     
    846846        $request = new WP_REST_Request( 'GET', '/', array() );
    847847
    848         $result  = $this->server->serve_request( '/' );
    849         $headers = $this->server->sent_headers;
     848        $result  = rest_get_server()->serve_request( '/' );
     849        $headers = rest_get_server()->sent_headers;
    850850
    851851        foreach ( wp_get_nocache_headers() as $header => $value ) {
     
    856856    public function test_serve_request_url_params_are_unslashed() {
    857857
    858         $this->server->register_route(
     858        rest_get_server()->register_route(
    859859            'test', '/test/(?P<data>.*)', array(
    860860                array(
     
    868868        );
    869869
    870         $result     = $this->server->serve_request( '/test/data\\with\\slashes' );
    871         $url_params = $this->server->last_request->get_url_params();
     870        $result     = rest_get_server()->serve_request( '/test/data\\with\\slashes' );
     871        $url_params = rest_get_server()->last_request->get_url_params();
    872872        $this->assertEquals( 'data\\with\\slashes', $url_params['data'] );
    873873    }
     
    875875    public function test_serve_request_query_params_are_unslashed() {
    876876
    877         $this->server->register_route(
     877        rest_get_server()->register_route(
    878878            'test', '/test', array(
    879879                array(
     
    894894        );
    895895
    896         $result       = $this->server->serve_request( '/test' );
    897         $query_params = $this->server->last_request->get_query_params();
     896        $result       = rest_get_server()->serve_request( '/test' );
     897        $query_params = rest_get_server()->last_request->get_query_params();
    898898        $this->assertEquals( 'data\\with\\slashes', $query_params['data'] );
    899899    }
     
    901901    public function test_serve_request_body_params_are_unslashed() {
    902902
    903         $this->server->register_route(
     903        rest_get_server()->register_route(
    904904            'test', '/test', array(
    905905                array(
     
    920920        );
    921921
    922         $result = $this->server->serve_request( '/test/data' );
    923 
    924         $body_params = $this->server->last_request->get_body_params();
     922        $result = rest_get_server()->serve_request( '/test/data' );
     923
     924        $body_params = rest_get_server()->last_request->get_body_params();
    925925        $this->assertEquals( 'data\\with\\slashes', $body_params['data'] );
    926926    }
     
    928928    public function test_serve_request_json_params_are_unslashed() {
    929929
    930         $this->server->register_route(
     930        rest_get_server()->register_route(
    931931            'test', '/test', array(
    932932                array(
     
    947947        );
    948948
    949         $result      = $this->server->serve_request( '/test' );
    950         $json_params = $this->server->last_request->get_json_params();
     949        $result      = rest_get_server()->serve_request( '/test' );
     950        $json_params = rest_get_server()->last_request->get_json_params();
    951951        $this->assertEquals( 'data\\with\\slashes', $json_params['data'] );
    952952    }
     
    954954    public function test_serve_request_file_params_are_unslashed() {
    955955
    956         $this->server->register_route(
     956        rest_get_server()->register_route(
    957957            'test', '/test', array(
    958958                array(
     
    973973        );
    974974
    975         $result      = $this->server->serve_request( '/test/data\\with\\slashes' );
    976         $file_params = $this->server->last_request->get_file_params();
     975        $result      = rest_get_server()->serve_request( '/test/data\\with\\slashes' );
     976        $file_params = rest_get_server()->last_request->get_file_params();
    977977        $this->assertEquals( 'data\\with\\slashes', $file_params['data']['name'] );
    978978    }
     
    980980    public function test_serve_request_headers_are_unslashed() {
    981981
    982         $this->server->register_route(
     982        rest_get_server()->register_route(
    983983            'test', '/test', array(
    984984                array(
     
    995995        $_SERVER['HTTP_X_MY_HEADER'] = wp_slash( 'data\\with\\slashes' );
    996996
    997         $result = $this->server->serve_request( '/test/data\\with\\slashes' );
    998         $this->assertEquals( 'data\\with\\slashes', $this->server->last_request->get_header( 'x_my_header' ) );
     997        $result = rest_get_server()->serve_request( '/test/data\\with\\slashes' );
     998        $this->assertEquals( 'data\\with\\slashes', rest_get_server()->last_request->get_header( 'x_my_header' ) );
    999999    }
    10001000
     
    10791079            )
    10801080        );
    1081         $response = $this->server->dispatch( $request );
     1081        $response = rest_get_server()->dispatch( $request );
    10821082
    10831083        $this->assertEquals( 200, $response->get_status() );
     
    11381138    protected function helper_make_request_and_return_headers_for_rest_send_refreshed_nonce_tests() {
    11391139        $request = new WP_REST_Request( 'GET', '/', array() );
    1140         $result  = $this->server->serve_request( '/' );
    1141 
    1142         return $this->server->sent_headers;
     1140        $result  = rest_get_server()->serve_request( '/' );
     1141
     1142        return rest_get_server()->sent_headers;
    11431143    }
    11441144}
Note: See TracChangeset for help on using the changeset viewer.