Changeset 42343 for trunk/tests/phpunit/tests/rest-api/rest-request.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-request.php
r41139 r42343 31 31 $this->assertNull( $this->request->get_header_as_array( 'missing' ) ); 32 32 } 33 33 34 34 public function test_remove_header() { 35 35 $this->request->add_header( 'Test-Header', 'value' ); 36 36 $this->assertEquals( 'value', $this->request->get_header( 'Test-Header' ) ); 37 37 38 38 $this->request->remove_header( 'Test-Header' ); 39 39 $this->assertNull( $this->request->get_header( 'Test-Header' ) ); … … 97 97 $parsed = $this->request->get_content_type(); 98 98 99 $this->assertEquals( $value, $parsed['value'] );100 $this->assertEquals( $type, $parsed['type'] );101 $this->assertEquals( $subtype, $parsed['subtype'] );99 $this->assertEquals( $value, $parsed['value'] ); 100 $this->assertEquals( $type, $parsed['type'] ); 101 $this->assertEquals( $subtype, $parsed['subtype'] ); 102 102 $this->assertEquals( $parameters, $parsed['parameters'] ); 103 103 } 104 104 105 105 protected function request_with_parameters() { 106 $this->request->set_url_params( array( 107 'source' => 'url', 108 'has_url_params' => true, 109 ) ); 110 $this->request->set_query_params( array( 111 'source' => 'query', 112 'has_query_params' => true, 113 ) ); 114 $this->request->set_body_params( array( 115 'source' => 'body', 116 'has_body_params' => true, 117 ) ); 118 119 $json_data = wp_json_encode( array( 120 'source' => 'json', 121 'has_json_params' => true, 122 ) ); 106 $this->request->set_url_params( 107 array( 108 'source' => 'url', 109 'has_url_params' => true, 110 ) 111 ); 112 $this->request->set_query_params( 113 array( 114 'source' => 'query', 115 'has_query_params' => true, 116 ) 117 ); 118 $this->request->set_body_params( 119 array( 120 'source' => 'body', 121 'has_body_params' => true, 122 ) 123 ); 124 125 $json_data = wp_json_encode( 126 array( 127 'source' => 'json', 128 'has_json_params' => true, 129 ) 130 ); 123 131 $this->request->set_body( $json_data ); 124 132 125 $this->request->set_default_params( array( 126 'source' => 'defaults', 127 'has_default_params' => true, 128 ) ); 133 $this->request->set_default_params( 134 array( 135 'source' => 'defaults', 136 'has_default_params' => true, 137 ) 138 ); 129 139 } 130 140 … … 229 239 public function test_non_post_body_parameters( $request_method ) { 230 240 $data = array( 231 'foo' => 'bar',241 'foo' => 'bar', 232 242 'alot' => array( 233 243 'of' => 'parameters', … … 249 259 public function test_parameters_for_json_put() { 250 260 $data = array( 251 'foo' => 'bar',261 'foo' => 'bar', 252 262 'alot' => array( 253 263 'of' => 'parameters', … … 271 281 public function test_parameters_for_json_post() { 272 282 $data = array( 273 'foo' => 'bar',283 'foo' => 'bar', 274 284 'alot' => array( 275 285 'of' => 'parameters', … … 307 317 308 318 public function test_parameter_merging_with_numeric_keys() { 309 $this->request->set_query_params( array( 310 '1' => 'hello', 311 '2' => 'goodbye', 312 ) ); 319 $this->request->set_query_params( 320 array( 321 '1' => 'hello', 322 '2' => 'goodbye', 323 ) 324 ); 313 325 $expected = array( 314 '1' => 'hello',315 '2' => 'goodbye',326 '1' => 'hello', 327 '2' => 'goodbye', 316 328 ); 317 329 $this->assertEquals( $expected, $this->request->get_params() ); … … 319 331 320 332 public function test_sanitize_params() { 321 $this->request->set_url_params( array( 322 'someinteger' => '123', 323 'somestring' => 'hello', 324 )); 325 326 $this->request->set_attributes( array( 327 'args' => array( 328 'someinteger' => array( 329 'sanitize_callback' => 'absint', 330 ), 331 'somestring' => array( 332 'sanitize_callback' => 'absint', 333 ), 334 ), 335 ) ); 333 $this->request->set_url_params( 334 array( 335 'someinteger' => '123', 336 'somestring' => 'hello', 337 ) 338 ); 339 340 $this->request->set_attributes( 341 array( 342 'args' => array( 343 'someinteger' => array( 344 'sanitize_callback' => 'absint', 345 ), 346 'somestring' => array( 347 'sanitize_callback' => 'absint', 348 ), 349 ), 350 ) 351 ); 336 352 337 353 $this->request->sanitize_params(); … … 342 358 343 359 public function test_sanitize_params_error() { 344 $this->request->set_url_params( array( 345 'successparam' => '123', 346 'failparam' => '123', 347 )); 348 $this->request->set_attributes( array( 349 'args' => array( 350 'successparam' => array( 351 'sanitize_callback' => 'absint', 352 ), 353 'failparam' => array( 354 'sanitize_callback' => array( $this, '_return_wp_error_on_validate_callback' ), 355 ), 356 ), 357 )); 360 $this->request->set_url_params( 361 array( 362 'successparam' => '123', 363 'failparam' => '123', 364 ) 365 ); 366 $this->request->set_attributes( 367 array( 368 'args' => array( 369 'successparam' => array( 370 'sanitize_callback' => 'absint', 371 ), 372 'failparam' => array( 373 'sanitize_callback' => array( $this, '_return_wp_error_on_validate_callback' ), 374 ), 375 ), 376 ) 377 ); 358 378 359 379 $valid = $this->request->sanitize_params(); … … 363 383 364 384 public function test_sanitize_params_with_null_callback() { 365 $this->request->set_url_params( array( 366 'some_email' => '', 367 ) ); 368 369 $this->request->set_attributes( array( 370 'args' => array( 371 'some_email' => array( 372 'type' => 'string', 373 'format' => 'email', 374 'sanitize_callback' => null, 375 ), 376 ), 377 ) ); 385 $this->request->set_url_params( 386 array( 387 'some_email' => '', 388 ) 389 ); 390 391 $this->request->set_attributes( 392 array( 393 'args' => array( 394 'some_email' => array( 395 'type' => 'string', 396 'format' => 'email', 397 'sanitize_callback' => null, 398 ), 399 ), 400 ) 401 ); 378 402 379 403 $this->assertTrue( $this->request->sanitize_params() ); … … 381 405 382 406 public function test_sanitize_params_with_false_callback() { 383 $this->request->set_url_params( array( 384 'some_uri' => 1.23422, 385 ) ); 386 387 $this->request->set_attributes( array( 388 'args' => array( 389 'some_uri' => array( 390 'type' => 'string', 391 'format' => 'uri', 392 'sanitize_callback' => false, 393 ), 394 ), 395 ) ); 407 $this->request->set_url_params( 408 array( 409 'some_uri' => 1.23422, 410 ) 411 ); 412 413 $this->request->set_attributes( 414 array( 415 'args' => array( 416 'some_uri' => array( 417 'type' => 'string', 418 'format' => 'uri', 419 'sanitize_callback' => false, 420 ), 421 ), 422 ) 423 ); 396 424 397 425 $this->assertTrue( $this->request->sanitize_params() ); … … 399 427 400 428 public function test_has_valid_params_required_flag() { 401 $this->request->set_attributes( array( 402 'args' => array( 403 'someinteger' => array( 404 'required' => true, 405 ), 406 ), 407 ) ); 429 $this->request->set_attributes( 430 array( 431 'args' => array( 432 'someinteger' => array( 433 'required' => true, 434 ), 435 ), 436 ) 437 ); 408 438 409 439 $valid = $this->request->has_valid_params(); … … 414 444 415 445 public function test_has_valid_params_required_flag_multiple() { 416 $this->request->set_attributes( array( 417 'args' => array( 418 'someinteger' => array( 419 'required' => true, 420 ), 421 'someotherinteger' => array( 422 'required' => true, 423 ), 424 ), 425 )); 446 $this->request->set_attributes( 447 array( 448 'args' => array( 449 'someinteger' => array( 450 'required' => true, 451 ), 452 'someotherinteger' => array( 453 'required' => true, 454 ), 455 ), 456 ) 457 ); 426 458 427 459 $valid = $this->request->has_valid_params(); … … 437 469 438 470 public function test_has_valid_params_validate_callback() { 439 $this->request->set_url_params( array( 440 'someinteger' => '123', 441 )); 442 443 $this->request->set_attributes( array( 444 'args' => array( 445 'someinteger' => array( 446 'validate_callback' => '__return_false', 447 ), 448 ), 449 )); 471 $this->request->set_url_params( 472 array( 473 'someinteger' => '123', 474 ) 475 ); 476 477 $this->request->set_attributes( 478 array( 479 'args' => array( 480 'someinteger' => array( 481 'validate_callback' => '__return_false', 482 ), 483 ), 484 ) 485 ); 450 486 451 487 $valid = $this->request->has_valid_params(); … … 484 520 485 521 public function test_has_multiple_invalid_params_validate_callback() { 486 $this->request->set_url_params( array( 487 'someinteger' => '123', 488 'someotherinteger' => '123', 489 )); 490 491 $this->request->set_attributes( array( 492 'args' => array( 493 'someinteger' => array( 494 'validate_callback' => '__return_false', 495 ), 496 'someotherinteger' => array( 497 'validate_callback' => '__return_false', 498 ), 499 ), 500 )); 522 $this->request->set_url_params( 523 array( 524 'someinteger' => '123', 525 'someotherinteger' => '123', 526 ) 527 ); 528 529 $this->request->set_attributes( 530 array( 531 'args' => array( 532 'someinteger' => array( 533 'validate_callback' => '__return_false', 534 ), 535 'someotherinteger' => array( 536 'validate_callback' => '__return_false', 537 ), 538 ), 539 ) 540 ); 501 541 502 542 $valid = $this->request->has_valid_params(); … … 512 552 513 553 public function test_invalid_params_error_response_format() { 514 $this->request->set_url_params( array( 515 'someinteger' => '123', 516 'someotherparams' => '123', 517 )); 518 519 $this->request->set_attributes( array( 520 'args' => array( 521 'someinteger' => array( 522 'validate_callback' => '__return_false', 523 ), 524 'someotherparams' => array( 525 'validate_callback' => array( $this, '_return_wp_error_on_validate_callback' ), 526 ), 527 ), 528 )); 554 $this->request->set_url_params( 555 array( 556 'someinteger' => '123', 557 'someotherparams' => '123', 558 ) 559 ); 560 561 $this->request->set_attributes( 562 array( 563 'args' => array( 564 'someinteger' => array( 565 'validate_callback' => '__return_false', 566 ), 567 'someotherparams' => array( 568 'validate_callback' => array( $this, '_return_wp_error_on_validate_callback' ), 569 ), 570 ), 571 ) 572 ); 529 573 530 574 $valid = $this->request->has_valid_params(); … … 563 607 $this->assertInstanceOf( 'WP_REST_Request', $request ); 564 608 $this->assertEquals( '/wp/v2/posts/1', $request->get_route() ); 565 $this->assertEqualSets( array( 566 'foo' => 'bar', 567 ), $request->get_query_params() ); 609 $this->assertEqualSets( 610 array( 611 'foo' => 'bar', 612 ), $request->get_query_params() 613 ); 568 614 } 569 615 … … 574 620 update_option( 'permalink_structure', $permalink_structure ); 575 621 $using_site = site_url( '/wp/v2/posts/1' ); 576 $request = WP_REST_Request::from_url( $using_site );622 $request = WP_REST_Request::from_url( $using_site ); 577 623 $this->assertFalse( $request ); 578 624 579 $using_home = home_url( '/wp/v2/posts/1' ) ;580 $request = WP_REST_Request::from_url( $using_home );625 $using_home = home_url( '/wp/v2/posts/1' ); 626 $request = WP_REST_Request::from_url( $using_home ); 581 627 $this->assertFalse( $request ); 582 628 } … … 592 638 $request->add_header( 'content-type', 'application/json' ); 593 639 $request->set_method( 'POST' ); 594 $request->set_body( wp_json_encode( array( 595 'param' => 'value' 596 ) ) ); 640 $request->set_body( 641 wp_json_encode( 642 array( 643 'param' => 'value', 644 ) 645 ) 646 ); 597 647 $this->assertEquals( 'value', $request->get_param( 'param' ) ); 598 648 $this->assertEquals(
Note: See TracChangeset
for help on using the changeset viewer.