Changeset 42343 for trunk/tests/phpunit/tests/rest-api.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/rest-api.php (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api.php
r42250 r42343 78 78 */ 79 79 public function test_route_canonicalized() { 80 register_rest_route( 'test-ns', '/test', array( 81 'methods' => array( 'GET' ), 82 'callback' => '__return_null', 83 ) ); 80 register_rest_route( 81 'test-ns', '/test', array( 82 'methods' => array( 'GET' ), 83 'callback' => '__return_null', 84 ) 85 ); 84 86 85 87 // Check the route was registered correctly. … … 99 101 $this->assertCount( 1, $endpoint ); 100 102 $this->assertArrayHasKey( 'callback', $endpoint[0] ); 101 $this->assertArrayHasKey( 'methods', $endpoint[0] );102 $this->assertArrayHasKey( 'args', $endpoint[0] );103 $this->assertArrayHasKey( 'methods', $endpoint[0] ); 104 $this->assertArrayHasKey( 'args', $endpoint[0] ); 103 105 } 104 106 … … 109 111 */ 110 112 public function test_route_canonicalized_multiple() { 111 register_rest_route( 'test-ns', '/test', array( 112 array( 113 'methods' => array( 'GET' ), 114 'callback' => '__return_null', 115 ), 116 array( 117 'methods' => array( 'POST' ), 118 'callback' => '__return_null', 119 ), 120 ) ); 113 register_rest_route( 114 'test-ns', '/test', array( 115 array( 116 'methods' => array( 'GET' ), 117 'callback' => '__return_null', 118 ), 119 array( 120 'methods' => array( 'POST' ), 121 'callback' => '__return_null', 122 ), 123 ) 124 ); 121 125 122 126 // Check the route was registered correctly. … … 131 135 132 136 $filtered_endpoints = $GLOBALS['wp_rest_server']->get_routes(); 133 $endpoint = $filtered_endpoints['/test-ns/test'];137 $endpoint = $filtered_endpoints['/test-ns/test']; 134 138 $this->assertCount( 2, $endpoint ); 135 139 … … 137 141 foreach ( array( 0, 1 ) as $key ) { 138 142 $this->assertArrayHasKey( 'callback', $endpoint[ $key ] ); 139 $this->assertArrayHasKey( 'methods', $endpoint[ $key ] );140 $this->assertArrayHasKey( 'args', $endpoint[ $key ] );143 $this->assertArrayHasKey( 'methods', $endpoint[ $key ] ); 144 $this->assertArrayHasKey( 'args', $endpoint[ $key ] ); 141 145 } 142 146 } … … 146 150 */ 147 151 public function test_route_merge() { 148 register_rest_route( 'test-ns', '/test', array( 149 'methods' => array( 'GET' ), 150 'callback' => '__return_null', 151 ) ); 152 register_rest_route( 'test-ns', '/test', array( 153 'methods' => array( 'POST' ), 154 'callback' => '__return_null', 155 ) ); 152 register_rest_route( 153 'test-ns', '/test', array( 154 'methods' => array( 'GET' ), 155 'callback' => '__return_null', 156 ) 157 ); 158 register_rest_route( 159 'test-ns', '/test', array( 160 'methods' => array( 'POST' ), 161 'callback' => '__return_null', 162 ) 163 ); 156 164 157 165 // Check both routes exist. 158 166 $endpoints = $GLOBALS['wp_rest_server']->get_routes(); 159 $endpoint = $endpoints['/test-ns/test'];167 $endpoint = $endpoints['/test-ns/test']; 160 168 $this->assertCount( 2, $endpoint ); 161 169 } … … 165 173 */ 166 174 public function test_route_override() { 167 register_rest_route( 'test-ns', '/test', array( 168 'methods' => array( 'GET' ), 169 'callback' => '__return_null', 170 'should_exist' => false, 171 ) ); 172 register_rest_route( 'test-ns', '/test', array( 173 'methods' => array( 'POST' ), 174 'callback' => '__return_null', 175 'should_exist' => true, 176 ), true ); 175 register_rest_route( 176 'test-ns', '/test', array( 177 'methods' => array( 'GET' ), 178 'callback' => '__return_null', 179 'should_exist' => false, 180 ) 181 ); 182 register_rest_route( 183 'test-ns', '/test', array( 184 'methods' => array( 'POST' ), 185 'callback' => '__return_null', 186 'should_exist' => true, 187 ), true 188 ); 177 189 178 190 // Check we only have one route. 179 191 $endpoints = $GLOBALS['wp_rest_server']->get_routes(); 180 $endpoint = $endpoints['/test-ns/test'];192 $endpoint = $endpoints['/test-ns/test']; 181 193 $this->assertCount( 1, $endpoint ); 182 194 … … 192 204 */ 193 205 public function test_route_reject_empty_namespace() { 194 register_rest_route( '', '/test-empty-namespace', array( 195 'methods' => array( 'POST' ), 196 'callback' => '__return_null', 197 ), true ); 206 register_rest_route( 207 '', '/test-empty-namespace', array( 208 'methods' => array( 'POST' ), 209 'callback' => '__return_null', 210 ), true 211 ); 198 212 $endpoints = $GLOBALS['wp_rest_server']->get_routes(); 199 213 $this->assertFalse( isset( $endpoints['/test-empty-namespace'] ) ); … … 206 220 */ 207 221 public function test_route_reject_empty_route() { 208 register_rest_route( '/test-empty-route', '', array( 209 'methods' => array( 'POST' ), 210 'callback' => '__return_null', 211 ), true ); 222 register_rest_route( 223 '/test-empty-route', '', array( 224 'methods' => array( 'POST' ), 225 'callback' => '__return_null', 226 ), true 227 ); 212 228 $endpoints = $GLOBALS['wp_rest_server']->get_routes(); 213 229 $this->assertFalse( isset( $endpoints['/test-empty-route'] ) ); … … 223 239 224 240 public function test_route_method() { 225 register_rest_route( 'test-ns', '/test', array( 226 'methods' => array( 'GET' ), 227 'callback' => '__return_null', 228 ) ); 241 register_rest_route( 242 'test-ns', '/test', array( 243 'methods' => array( 'GET' ), 244 'callback' => '__return_null', 245 ) 246 ); 229 247 230 248 $routes = $GLOBALS['wp_rest_server']->get_routes(); … … 237 255 */ 238 256 public function test_route_method_string() { 239 register_rest_route( 'test-ns', '/test', array( 240 'methods' => 'GET', 241 'callback' => '__return_null', 242 ) ); 257 register_rest_route( 258 'test-ns', '/test', array( 259 'methods' => 'GET', 260 'callback' => '__return_null', 261 ) 262 ); 243 263 244 264 $routes = $GLOBALS['wp_rest_server']->get_routes(); … … 251 271 */ 252 272 public function test_route_method_array() { 253 register_rest_route( 'test-ns', '/test', array( 254 'methods' => array( 'GET', 'POST' ), 255 'callback' => '__return_null', 256 ) ); 273 register_rest_route( 274 'test-ns', '/test', array( 275 'methods' => array( 'GET', 'POST' ), 276 'callback' => '__return_null', 277 ) 278 ); 257 279 258 280 $routes = $GLOBALS['wp_rest_server']->get_routes(); 259 281 260 $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true, 'POST' => true ) ); 282 $this->assertEquals( 283 $routes['/test-ns/test'][0]['methods'], array( 284 'GET' => true, 285 'POST' => true, 286 ) 287 ); 261 288 } 262 289 … … 265 292 */ 266 293 public function test_route_method_comma_seperated() { 267 register_rest_route( 'test-ns', '/test', array( 268 'methods' => 'GET,POST', 269 'callback' => '__return_null', 270 ) ); 294 register_rest_route( 295 'test-ns', '/test', array( 296 'methods' => 'GET,POST', 297 'callback' => '__return_null', 298 ) 299 ); 271 300 272 301 $routes = $GLOBALS['wp_rest_server']->get_routes(); 273 302 274 $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true, 'POST' => true ) ); 303 $this->assertEquals( 304 $routes['/test-ns/test'][0]['methods'], array( 305 'GET' => true, 306 'POST' => true, 307 ) 308 ); 275 309 } 276 310 277 311 public function test_options_request() { 278 register_rest_route( 'test-ns', '/test', array( 279 'methods' => 'GET,POST', 280 'callback' => '__return_null', 281 ) ); 282 283 $request = new WP_REST_Request( 'OPTIONS', '/test-ns/test' ); 312 register_rest_route( 313 'test-ns', '/test', array( 314 'methods' => 'GET,POST', 315 'callback' => '__return_null', 316 ) 317 ); 318 319 $request = new WP_REST_Request( 'OPTIONS', '/test-ns/test' ); 284 320 $response = rest_handle_options_request( null, $GLOBALS['wp_rest_server'], $request ); 285 321 $response = rest_send_allow_header( $response, $GLOBALS['wp_rest_server'], $request ); 286 $headers = $response->get_headers();322 $headers = $response->get_headers(); 287 323 $this->assertArrayHasKey( 'Allow', $headers ); 288 324 … … 294 330 */ 295 331 public function test_options_request_not_options() { 296 register_rest_route( 'test-ns', '/test', array( 297 'methods' => 'GET,POST', 298 'callback' => '__return_true', 299 ) ); 300 301 $request = new WP_REST_Request( 'GET', '/test-ns/test' ); 332 register_rest_route( 333 'test-ns', '/test', array( 334 'methods' => 'GET,POST', 335 'callback' => '__return_true', 336 ) 337 ); 338 339 $request = new WP_REST_Request( 'GET', '/test-ns/test' ); 302 340 $response = rest_handle_options_request( null, $GLOBALS['wp_rest_server'], $request ); 303 341 … … 322 360 public function test_rest_filter_response_fields_single_field_filter() { 323 361 $response = new WP_REST_Response(); 324 $response->set_data( array( 325 'a' => 0, 326 'b' => 1, 327 'c' => 2, 328 ) ); 329 $request = array( 330 '_fields' => 'b' 331 ); 332 333 $response = rest_filter_response_fields( $response, null, $request ); 334 $this->assertEquals( array( 'b' => 1 ), $response->get_data() ); 335 } 336 337 /** 338 * Ensure that multiple comma-separated fields may be whitelisted with request['_fields']. 339 */ 340 public function test_rest_filter_response_fields_multi_field_filter() { 341 $response = new WP_REST_Response(); 342 $response->set_data( array( 343 'a' => 0, 344 'b' => 1, 345 'c' => 2, 346 'd' => 3, 347 'e' => 4, 348 'f' => 5, 349 ) ); 350 $request = array( 351 '_fields' => 'b,c,e' 352 ); 353 354 $response = rest_filter_response_fields( $response, null, $request ); 355 $this->assertEquals( array( 356 'b' => 1, 357 'c' => 2, 358 'e' => 4, 359 ), $response->get_data() ); 360 } 361 362 /** 363 * Ensure that multiple comma-separated fields may be whitelisted 364 * with request['_fields'] using query parameter array syntax. 365 */ 366 public function test_rest_filter_response_fields_multi_field_filter_array() { 367 $response = new WP_REST_Response(); 368 369 $response->set_data( array( 370 'a' => 0, 371 'b' => 1, 372 'c' => 2, 373 'd' => 3, 374 'e' => 4, 375 'f' => 5, 376 ) ); 377 $request = array( 378 '_fields' => array( 'b', 'c', 'e' ) 379 ); 380 381 $response = rest_filter_response_fields( $response, null, $request ); 382 $this->assertEquals( array( 383 'b' => 1, 384 'c' => 2, 385 'e' => 4, 386 ), $response->get_data() ); 387 } 388 389 /** 390 * Ensure that request['_fields'] whitelists apply to items in response collections. 391 */ 392 public function test_rest_filter_response_fields_numeric_array() { 393 $response = new WP_REST_Response(); 394 $response->set_data( array( 362 $response->set_data( 395 363 array( 396 364 'a' => 0, 397 365 'b' => 1, 398 366 'c' => 2, 399 ), 367 ) 368 ); 369 $request = array( 370 '_fields' => 'b', 371 ); 372 373 $response = rest_filter_response_fields( $response, null, $request ); 374 $this->assertEquals( array( 'b' => 1 ), $response->get_data() ); 375 } 376 377 /** 378 * Ensure that multiple comma-separated fields may be whitelisted with request['_fields']. 379 */ 380 public function test_rest_filter_response_fields_multi_field_filter() { 381 $response = new WP_REST_Response(); 382 $response->set_data( 400 383 array( 401 'a' => 3, 402 'b' => 4, 403 'c' => 5, 404 ), 405 array( 406 'a' => 6, 407 'b' => 7, 408 'c' => 8, 409 ), 410 ) ); 384 'a' => 0, 385 'b' => 1, 386 'c' => 2, 387 'd' => 3, 388 'e' => 4, 389 'f' => 5, 390 ) 391 ); 411 392 $request = array( 412 '_fields' => 'b,c '393 '_fields' => 'b,c,e', 413 394 ); 414 395 415 396 $response = rest_filter_response_fields( $response, null, $request ); 416 $this->assertEquals( array(397 $this->assertEquals( 417 398 array( 418 399 'b' => 1, 419 400 'c' => 2, 420 ), 401 'e' => 4, 402 ), $response->get_data() 403 ); 404 } 405 406 /** 407 * Ensure that multiple comma-separated fields may be whitelisted 408 * with request['_fields'] using query parameter array syntax. 409 */ 410 public function test_rest_filter_response_fields_multi_field_filter_array() { 411 $response = new WP_REST_Response(); 412 413 $response->set_data( 421 414 array( 422 'b' => 4, 423 'c' => 5, 424 ), 415 'a' => 0, 416 'b' => 1, 417 'c' => 2, 418 'd' => 3, 419 'e' => 4, 420 'f' => 5, 421 ) 422 ); 423 $request = array( 424 '_fields' => array( 'b', 'c', 'e' ), 425 ); 426 427 $response = rest_filter_response_fields( $response, null, $request ); 428 $this->assertEquals( 425 429 array( 426 'b' => 7, 427 'c' => 8, 428 ), 429 ), $response->get_data() ); 430 'b' => 1, 431 'c' => 2, 432 'e' => 4, 433 ), $response->get_data() 434 ); 435 } 436 437 /** 438 * Ensure that request['_fields'] whitelists apply to items in response collections. 439 */ 440 public function test_rest_filter_response_fields_numeric_array() { 441 $response = new WP_REST_Response(); 442 $response->set_data( 443 array( 444 array( 445 'a' => 0, 446 'b' => 1, 447 'c' => 2, 448 ), 449 array( 450 'a' => 3, 451 'b' => 4, 452 'c' => 5, 453 ), 454 array( 455 'a' => 6, 456 'b' => 7, 457 'c' => 8, 458 ), 459 ) 460 ); 461 $request = array( 462 '_fields' => 'b,c', 463 ); 464 465 $response = rest_filter_response_fields( $response, null, $request ); 466 $this->assertEquals( 467 array( 468 array( 469 'b' => 1, 470 'c' => 2, 471 ), 472 array( 473 'b' => 4, 474 'c' => 5, 475 ), 476 array( 477 'b' => 7, 478 'c' => 8, 479 ), 480 ), $response->get_data() 481 ); 430 482 } 431 483 … … 453 505 public function test_rest_url_scheme() { 454 506 $_SERVER['SERVER_NAME'] = parse_url( home_url(), PHP_URL_HOST ); 455 $_siteurl = get_option( 'siteurl' );507 $_siteurl = get_option( 'siteurl' ); 456 508 457 509 set_current_screen( 'edit.php' ); … … 465 517 // Test an HTTPS URL 466 518 $_SERVER['HTTPS'] = 'on'; 467 $url = get_rest_url();519 $url = get_rest_url(); 468 520 $this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) ); 469 521 … … 480 532 // // Test an HTTPS URL 481 533 $_SERVER['HTTPS'] = 'on'; 482 $url = get_rest_url();534 $url = get_rest_url(); 483 535 $this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) ); 484 536 … … 544 596 return array( 545 597 // Valid dates with timezones 546 array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30, 0,1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),547 array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30, 0,1, 16, 2017 ) + 5.5 * HOUR_IN_SECONDS ),548 array( '2017-01-16T11:30:00-05' , gmmktime( 11, 30, 0,1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),549 array( '2017-01-16T11:30:00+05' , gmmktime( 11, 30, 0,1, 16, 2017 ) - 5 * HOUR_IN_SECONDS ),550 array( '2017-01-16T11:30:00-00' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),551 array( '2017-01-16T11:30:00+00' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),552 array( '2017-01-16T11:30:00Z' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),598 array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ), 599 array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30, 0, 1, 16, 2017 ) + 5.5 * HOUR_IN_SECONDS ), 600 array( '2017-01-16T11:30:00-05', gmmktime( 11, 30, 0, 1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ), 601 array( '2017-01-16T11:30:00+05', gmmktime( 11, 30, 0, 1, 16, 2017 ) - 5 * HOUR_IN_SECONDS ), 602 array( '2017-01-16T11:30:00-00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 603 array( '2017-01-16T11:30:00+00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 604 array( '2017-01-16T11:30:00Z', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 553 605 554 606 // Valid dates without timezones 555 array( '2017-01-16T11:30:00' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),607 array( '2017-01-16T11:30:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 556 608 557 609 // Invalid dates (TODO: support parsing partial dates as ranges, see #38641) … … 576 628 return array( 577 629 // Valid dates with timezones 578 array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30, 0,1, 16, 2017 ) ),579 array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30, 0,1, 16, 2017 ) ),580 array( '2017-01-16T11:30:00-05' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),581 array( '2017-01-16T11:30:00+05' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),582 array( '2017-01-16T11:30:00-00' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),583 array( '2017-01-16T11:30:00+00' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),584 array( '2017-01-16T11:30:00Z' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),630 array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 631 array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 632 array( '2017-01-16T11:30:00-05', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 633 array( '2017-01-16T11:30:00+05', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 634 array( '2017-01-16T11:30:00-00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 635 array( '2017-01-16T11:30:00+00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 636 array( '2017-01-16T11:30:00Z', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 585 637 586 638 // Valid dates without timezones 587 array( '2017-01-16T11:30:00' , gmmktime( 11, 30, 0,1, 16, 2017 ) ),639 array( '2017-01-16T11:30:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ), 588 640 589 641 // Invalid dates (TODO: support parsing partial dates as ranges, see #38641) … … 613 665 add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) ); 614 666 615 register_rest_route( 'test-ns', '/test', array( 616 'methods' => array( 'GET' ), 617 'callback' => '__return_null', 618 ) ); 667 register_rest_route( 668 'test-ns', '/test', array( 669 'methods' => array( 'GET' ), 670 'callback' => '__return_null', 671 ) 672 ); 619 673 620 674 $routes = $GLOBALS['wp_rest_server']->get_routes();
Note: See TracChangeset
for help on using the changeset viewer.