Changeset 47559
- Timestamp:
- 04/09/2020 07:28:58 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/class-wp-rest-request.php
r47122 r47559 421 421 * Sets a parameter on the request. 422 422 * 423 * If the given parameter key exists in any parameter type an update will take place, 424 * otherwise a new param will be created in the first parameter type (respecting 425 * get_parameter_order()). 426 * 423 427 * @since 4.4.0 424 428 * … … 427 431 */ 428 432 public function set_param( $key, $value ) { 429 $order = $this->get_parameter_order(); 430 $this->params[ $order[0] ][ $key ] = $value; 433 $order = $this->get_parameter_order(); 434 $found_key = false; 435 436 foreach ( $order as $type ) { 437 if ( 'defaults' !== $type && array_key_exists( $key, $this->params[ $type ] ) ) { 438 $this->params[ $type ][ $key ] = $value; 439 $found_key = true; 440 } 441 } 442 443 if ( ! $found_key ) { 444 $this->params[ $order[0] ][ $key ] = $value; 445 } 431 446 } 432 447 -
trunk/tests/phpunit/tests/rest-api/rest-request.php
r46586 r47559 651 651 ); 652 652 } 653 654 /** 655 * @ticket 40838 656 */ 657 public function test_set_param_updates_param_in_json_and_query() { 658 $request = new WP_REST_Request(); 659 $request->add_header( 'content-type', 'application/json' ); 660 $request->set_method( 'POST' ); 661 $request->set_body( 662 wp_json_encode( 663 array( 664 'param' => 'value_body', 665 ) 666 ) 667 ); 668 $request->set_query_params( 669 array( 670 'param' => 'value_query', 671 ) 672 ); 673 $request->set_param( 'param', 'new_value' ); 674 675 $this->assertEquals( 'new_value', $request->get_param( 'param' ) ); 676 $this->assertEquals( array(), $request->get_body_params() ); 677 $this->assertEquals( array( 'param' => 'new_value' ), $request->get_json_params() ); 678 $this->assertEquals( array( 'param' => 'new_value' ), $request->get_query_params() ); 679 } 680 681 /** 682 * @ticket 40838 683 */ 684 public function test_set_param_updates_param_if_already_exists_in_query() { 685 $request = new WP_REST_Request(); 686 $request->add_header( 'content-type', 'application/json' ); 687 $request->set_method( 'POST' ); 688 $request->set_body( 689 wp_json_encode( 690 array( 691 'param_body' => 'value_body', 692 ) 693 ) 694 ); 695 $original_defaults = array( 696 'param_query' => 'default_query_value', 697 'param_body' => 'default_body_value', 698 ); 699 $request->set_default_params( $original_defaults ); 700 $request->set_query_params( 701 array( 702 'param_query' => 'value_query', 703 ) 704 ); 705 $request->set_param( 'param_query', 'new_value' ); 706 707 $this->assertEquals( 'new_value', $request->get_param( 'param_query' ) ); 708 $this->assertEquals( array(), $request->get_body_params() ); 709 $this->assertEquals( array( 'param_body' => 'value_body' ), $request->get_json_params() ); 710 $this->assertEquals( array( 'param_query' => 'new_value' ), $request->get_query_params() ); 711 // Verify the default wasn't overwritten. 712 $this->assertEquals( $original_defaults, $request->get_default_params() ); 713 } 714 715 /** 716 * @ticket 40838 717 */ 718 public function test_set_param_to_null_updates_param_in_json_and_query() { 719 $request = new WP_REST_Request(); 720 $request->add_header( 'content-type', 'application/json' ); 721 $request->set_method( 'POST' ); 722 $request->set_body( 723 wp_json_encode( 724 array( 725 'param' => 'value_body', 726 ) 727 ) 728 ); 729 $request->set_query_params( 730 array( 731 'param' => 'value_query', 732 ) 733 ); 734 $request->set_param( 'param', null ); 735 736 $this->assertEquals( null, $request->get_param( 'param' ) ); 737 $this->assertEquals( array(), $request->get_body_params() ); 738 $this->assertEquals( array( 'param' => null ), $request->get_json_params() ); 739 $this->assertEquals( array( 'param' => null ), $request->get_query_params() ); 740 } 741 742 /** 743 * @ticket 40838 744 */ 745 public function test_set_param_from_null_updates_param_in_json_and_query_with_null() { 746 $request = new WP_REST_Request(); 747 $request->add_header( 'content-type', 'application/json' ); 748 $request->set_method( 'POST' ); 749 $request->set_body( 750 wp_json_encode( 751 array( 752 'param' => null, 753 ) 754 ) 755 ); 756 $request->set_query_params( 757 array( 758 'param' => null, 759 ) 760 ); 761 $request->set_param( 'param', 'new_value' ); 762 763 $this->assertEquals( 'new_value', $request->get_param( 'param' ) ); 764 $this->assertEquals( array(), $request->get_body_params() ); 765 $this->assertEquals( array( 'param' => 'new_value' ), $request->get_json_params() ); 766 $this->assertEquals( array( 'param' => 'new_value' ), $request->get_query_params() ); 767 } 653 768 }
Note: See TracChangeset
for help on using the changeset viewer.