Changeset 42724 for trunk/tests/phpunit/tests/rest-api/rest-server.php
- Timestamp:
- 02/21/2018 04:24:30 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-server.php
r42343 r42724 17 17 $GLOBALS['wp_rest_server'] = null; 18 18 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(); 20 20 remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) ); 21 21 } … … 46 46 $response->header( 'Multiple', 'yes', false ); 47 47 48 $envelope_response = $this->server->envelope_response( $response, false );48 $envelope_response = rest_get_server()->envelope_response( $response, false ); 49 49 50 50 // The envelope should still be a response, but with defaults. … … 76 76 77 77 $request = new WP_REST_Request( 'GET', '/test-ns/test' ); 78 $response = $this->server->dispatch( $request );78 $response = rest_get_server()->dispatch( $request ); 79 79 80 80 $this->assertEquals( 'bar', $request['foo'] ); … … 97 97 $request = new WP_REST_Request( 'GET', '/test-ns/test' ); 98 98 $request->set_query_params( array( 'foo' => 123 ) ); 99 $response = $this->server->dispatch( $request );99 $response = rest_get_server()->dispatch( $request ); 100 100 101 101 $this->assertEquals( '123', $request['foo'] ); … … 115 115 $request = new WP_REST_Request( 'GET', '/optional/test' ); 116 116 $request->set_query_params( array() ); 117 $response = $this->server->dispatch( $request );117 $response = rest_get_server()->dispatch( $request ); 118 118 $this->assertInstanceOf( 'WP_REST_Response', $response ); 119 119 $this->assertEquals( 200, $response->get_status() ); … … 134 134 ); 135 135 $request = new WP_REST_Request( 'GET', '/no-zero/test' ); 136 $this->server->dispatch( $request );136 rest_get_server()->dispatch( $request ); 137 137 $this->assertEquals( array( 'foo' => 'bar' ), $request->get_params() ); 138 138 } … … 146 146 ); 147 147 $request = new WP_REST_Request( 'HEAD', '/head-request/test' ); 148 $response = $this->server->dispatch( $request );148 $response = rest_get_server()->dispatch( $request ); 149 149 $this->assertEquals( 200, $response->get_status() ); 150 150 } … … 171 171 ); 172 172 $request = new WP_REST_Request( 'HEAD', '/head-request/test' ); 173 $response = $this->server->dispatch( $request );173 $response = rest_get_server()->dispatch( $request ); 174 174 $this->assertEquals( 200, $response->get_status() ); 175 175 } … … 177 177 public function test_url_params_no_numeric_keys() { 178 178 179 $this->server->register_route(179 rest_get_server()->register_route( 180 180 'test', '/test/(?P<data>.*)', array( 181 181 array( … … 190 190 191 191 $request = new WP_REST_Request( 'GET', '/test/some-value' ); 192 $this->server->dispatch( $request );192 rest_get_server()->dispatch( $request ); 193 193 $this->assertEquals( array( 'data' => 'some-value' ), $request->get_params() ); 194 194 } … … 209 209 210 210 $request = new WP_REST_Request( 'GET', '/test-ns/test', array() ); 211 $result = $this->server->dispatch( $request );211 $result = rest_get_server()->dispatch( $request ); 212 212 213 213 $this->assertEquals( 403, $result->get_status() ); … … 234 234 wp_set_current_user( $editor ); 235 235 236 $result = $this->server->dispatch( $request );236 $result = rest_get_server()->dispatch( $request ); 237 237 238 238 $this->assertEquals( 200, $result->get_status() ); … … 255 255 $request = new WP_REST_Request( 'GET', '/test-ns/test', array() ); 256 256 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 ); 259 259 260 260 $this->assertFalse( $result->get_status() !== 200 ); … … 288 288 $request = new WP_REST_Request( 'GET', '/test-ns/test', array() ); 289 289 290 $result = $this->server->dispatch( $request );290 $result = rest_get_server()->dispatch( $request ); 291 291 292 292 $this->assertFalse( $result->get_status() !== 200 ); 293 293 294 $result = apply_filters( 'rest_post_dispatch', $result, $this->server, $request );294 $result = apply_filters( 'rest_post_dispatch', $result, rest_get_server(), $request ); 295 295 296 296 $sent_headers = $result->get_headers(); … … 323 323 $request = new WP_REST_Request( 'GET', '/test-ns/test', array() ); 324 324 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 ); 327 327 328 328 $this->assertEquals( $result->get_status(), 403 ); … … 348 348 349 349 $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 ); 353 353 354 354 $headers = $result->get_headers(); … … 366 366 $error = new WP_Error( $code, $message ); 367 367 368 $response = $this->server->error_to_response( $error );368 $response = rest_get_server()->error_to_response( $error ); 369 369 $this->assertInstanceOf( 'WP_REST_Response', $response ); 370 370 … … 383 383 $error = new WP_Error( $code, $message, array( 'status' => 400 ) ); 384 384 385 $response = $this->server->error_to_response( $error );385 $response = rest_get_server()->error_to_response( $error ); 386 386 $this->assertInstanceOf( 'WP_REST_Response', $response ); 387 387 … … 402 402 $error->add( $code2, $message2, array( 'status' => 403 ) ); 403 403 404 $response = $this->server->error_to_response( $error );404 $response = rest_get_server()->error_to_response( $error ); 405 405 $this->assertInstanceOf( 'WP_REST_Response', $response ); 406 406 … … 421 421 ); 422 422 $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' ); 424 424 425 425 $this->assertEquals( $expected, $response ); … … 451 451 $response->add_link( 'alternate', 'http://example.org/', array( 'type' => 'application/xml' ) ); 452 452 453 $data = $this->server->response_to_data( $response, false );453 $data = rest_get_server()->response_to_data( $response, false ); 454 454 $this->assertArrayHasKey( '_links', $data ); 455 455 … … 468 468 public function test_link_embedding() { 469 469 // Register our testing route. 470 $this->server->register_route(470 rest_get_server()->register_route( 471 471 'test', '/test/embeddable', array( 472 472 'methods' => 'GET', … … 482 482 $response->add_link( 'alternate', rest_url( '/test/embeddable' ), array( 'embeddable' => true ) ); 483 483 484 $data = $this->server->response_to_data( $response, true );484 $data = rest_get_server()->response_to_data( $response, true ); 485 485 $this->assertArrayHasKey( '_embedded', $data ); 486 486 … … 501 501 $response->add_link( 'https://api.w.org/term', 'http://example.com/' ); 502 502 503 $data = $this->server->response_to_data( $response, false );503 $data = rest_get_server()->response_to_data( $response, false ); 504 504 $links = $data['_links']; 505 505 … … 514 514 add_filter( 'rest_response_link_curies', array( $this, 'add_custom_curie' ) ); 515 515 516 $data = $this->server->response_to_data( $response, false );516 $data = rest_get_server()->response_to_data( $response, false ); 517 517 $links = $data['_links']; 518 518 … … 541 541 public function test_link_embedding_self() { 542 542 // Register our testing route. 543 $this->server->register_route(543 rest_get_server()->register_route( 544 544 'test', '/test/embeddable', array( 545 545 'methods' => 'GET', … … 552 552 $response->add_link( 'self', rest_url( '/test/notembeddable' ), array( 'embeddable' => true ) ); 553 553 554 $data = $this->server->response_to_data( $response, true );554 $data = rest_get_server()->response_to_data( $response, true ); 555 555 556 556 $this->assertArrayNotHasKey( '_embedded', $data ); … … 562 562 public function test_link_embedding_params() { 563 563 // Register our testing route. 564 $this->server->register_route(564 rest_get_server()->register_route( 565 565 'test', '/test/embeddable', array( 566 566 'methods' => 'GET', … … 574 574 $response->add_link( 'alternate', $url, array( 'embeddable' => true ) ); 575 575 576 $data = $this->server->response_to_data( $response, true );576 $data = rest_get_server()->response_to_data( $response, true ); 577 577 578 578 $this->assertArrayHasKey( '_embedded', $data ); … … 588 588 public function test_link_embedding_error() { 589 589 // Register our testing route. 590 $this->server->register_route(590 rest_get_server()->register_route( 591 591 'test', '/test/embeddable', array( 592 592 'methods' => 'GET', … … 600 600 $response->add_link( 'up', $url, array( 'embeddable' => true ) ); 601 601 602 $data = $this->server->response_to_data( $response, true );602 $data = rest_get_server()->response_to_data( $response, true ); 603 603 604 604 $this->assertArrayHasKey( '_embedded', $data ); … … 622 622 'untouched' => 'data', 623 623 ); 624 $result = $this->server->embed_links( $data );624 $result = rest_get_server()->embed_links( $data ); 625 625 626 626 $this->assertArrayNotHasKey( '_links', $data ); … … 651 651 $response->remove_link( 'self' ); 652 652 653 $data = $this->server->response_to_data( $response, false );653 $data = rest_get_server()->response_to_data( $response, false ); 654 654 $this->assertArrayHasKey( '_links', $data ); 655 655 … … 670 670 $response->remove_link( 'self', 'https://example.com/' ); 671 671 672 $data = $this->server->response_to_data( $response, false );672 $data = rest_get_server()->response_to_data( $response, false ); 673 673 $this->assertArrayHasKey( '_links', $data ); 674 674 … … 790 790 $request = new WP_REST_Request( 'GET', '/', array() ); 791 791 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; 794 794 795 795 $this->assertEquals( 'noindex', $headers['X-Robots-Tag'] ); … … 802 802 public function test_rest_enable_filter_is_deprecated() { 803 803 add_filter( 'rest_enabled', '__return_false' ); 804 $this->server->serve_request( '/' );804 rest_get_server()->serve_request( '/' ); 805 805 remove_filter( 'rest_enabled', '__return_false' ); 806 806 807 $result = json_decode( $this->server->sent_body );807 $result = json_decode( rest_get_server()->sent_body ); 808 808 809 809 $this->assertObjectNotHasAttribute( 'code', $result ); … … 815 815 $request = new WP_REST_Request( 'GET', '/', array() ); 816 816 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; 819 819 820 820 $this->assertEquals( '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"', $headers['Link'] ); … … 826 826 wp_set_current_user( $editor ); 827 827 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; 830 830 831 831 foreach ( wp_get_nocache_headers() as $header => $value ) { … … 846 846 $request = new WP_REST_Request( 'GET', '/', array() ); 847 847 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; 850 850 851 851 foreach ( wp_get_nocache_headers() as $header => $value ) { … … 856 856 public function test_serve_request_url_params_are_unslashed() { 857 857 858 $this->server->register_route(858 rest_get_server()->register_route( 859 859 'test', '/test/(?P<data>.*)', array( 860 860 array( … … 868 868 ); 869 869 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(); 872 872 $this->assertEquals( 'data\\with\\slashes', $url_params['data'] ); 873 873 } … … 875 875 public function test_serve_request_query_params_are_unslashed() { 876 876 877 $this->server->register_route(877 rest_get_server()->register_route( 878 878 'test', '/test', array( 879 879 array( … … 894 894 ); 895 895 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(); 898 898 $this->assertEquals( 'data\\with\\slashes', $query_params['data'] ); 899 899 } … … 901 901 public function test_serve_request_body_params_are_unslashed() { 902 902 903 $this->server->register_route(903 rest_get_server()->register_route( 904 904 'test', '/test', array( 905 905 array( … … 920 920 ); 921 921 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(); 925 925 $this->assertEquals( 'data\\with\\slashes', $body_params['data'] ); 926 926 } … … 928 928 public function test_serve_request_json_params_are_unslashed() { 929 929 930 $this->server->register_route(930 rest_get_server()->register_route( 931 931 'test', '/test', array( 932 932 array( … … 947 947 ); 948 948 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(); 951 951 $this->assertEquals( 'data\\with\\slashes', $json_params['data'] ); 952 952 } … … 954 954 public function test_serve_request_file_params_are_unslashed() { 955 955 956 $this->server->register_route(956 rest_get_server()->register_route( 957 957 'test', '/test', array( 958 958 array( … … 973 973 ); 974 974 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(); 977 977 $this->assertEquals( 'data\\with\\slashes', $file_params['data']['name'] ); 978 978 } … … 980 980 public function test_serve_request_headers_are_unslashed() { 981 981 982 $this->server->register_route(982 rest_get_server()->register_route( 983 983 'test', '/test', array( 984 984 array( … … 995 995 $_SERVER['HTTP_X_MY_HEADER'] = wp_slash( 'data\\with\\slashes' ); 996 996 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' ) ); 999 999 } 1000 1000 … … 1079 1079 ) 1080 1080 ); 1081 $response = $this->server->dispatch( $request );1081 $response = rest_get_server()->dispatch( $request ); 1082 1082 1083 1083 $this->assertEquals( 200, $response->get_status() ); … … 1138 1138 protected function helper_make_request_and_return_headers_for_rest_send_refreshed_nonce_tests() { 1139 1139 $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; 1143 1143 } 1144 1144 }
Note: See TracChangeset
for help on using the changeset viewer.