Ticket #40838: 40838.patch
File 40838.patch, 3.1 KB (added by , 8 years ago) |
---|
-
src/wp-includes/rest-api/class-wp-rest-request.php
421 421 /** 422 422 * Sets a parameter on the request. 423 423 * 424 * If the given parameter key exists in any parameter type an update will take place, 425 * otherwise a new param will be created in the first parameter type (respecting 426 * get_parameter_order()). 427 * 424 428 * @since 4.4.0 425 429 * @access public 426 430 * … … 429 433 */ 430 434 public function set_param( $key, $value ) { 431 435 $order = $this->get_parameter_order(); 432 $this->params[ $order[0] ][ $key ] = $value; 436 $found_key = false; 437 438 foreach ( $order as $type ) { 439 if ( isset( $this->params[ $type ][ $key ] ) ) { 440 $this->params[ $type ][ $key ] = $value; 441 $found_key = true; 442 } 443 } 444 445 if ( !$found_key ) { 446 $this->params[ $order[0] ][ $key ] = $value; 447 } 433 448 } 434 449 435 450 /** -
tests/phpunit/tests/rest-api/rest-request.php
30 30 $this->assertNull( $this->request->get_header( 'missing' ) ); 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' ) ); 40 40 } … … 607 607 $request->get_json_params() 608 608 ); 609 609 } 610 611 public function test_set_param_updates_param_in_json_and_query() { 612 $request = new WP_REST_Request(); 613 $request->add_header( 'content-type', 'application/json' ); 614 $request->set_method( 'POST' ); 615 $request->set_body( wp_json_encode( array( 616 'param' => 'value_body', 617 ) ) ); 618 $request->set_query_params( array( 619 'param' => 'value_query', 620 ) ); 621 $request->set_param( 'param', 'new_value' ); 622 623 $this->assertEquals( 'new_value', $request->get_param( 'param' ) ); 624 $this->assertEquals( array( 'param' => 'new_value' ), $request->get_json_params() ); 625 $this->assertEquals( array( 'param' => 'new_value' ), $request->get_query_params() ); 626 } 627 628 public function test_set_param_updates_param_if_already_exists_in_query() { 629 $request = new WP_REST_Request(); 630 $request->add_header( 'content-type', 'application/json' ); 631 $request->set_method( 'POST' ); 632 $request->set_body( wp_json_encode( array( 633 'param_body' => 'value_body', 634 ) ) ); 635 $request->set_query_params( array( 636 'param_query' => 'value_query', 637 ) ); 638 $request->set_param( 'param_query', 'new_value' ); 639 640 $this->assertEquals( 'new_value', $request->get_param( 'param_query' ) ); 641 $this->assertEquals( array( 'param_body' => 'value_body' ), $request->get_json_params() ); 642 $this->assertEquals( array( 'param_query' => 'new_value' ), $request->get_query_params() ); 643 } 610 644 }