Make WordPress Core

Ticket #40838: 40838.patch

File 40838.patch, 3.1 KB (added by vagios, 8 years ago)
  • src/wp-includes/rest-api/class-wp-rest-request.php

     
    421421        /**
    422422         * Sets a parameter on the request.
    423423         *
     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         *
    424428         * @since 4.4.0
    425429         * @access public
    426430         *
     
    429433         */
    430434        public function set_param( $key, $value ) {
    431435                $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                }
    433448        }
    434449
    435450        /**
  • tests/phpunit/tests/rest-api/rest-request.php

     
    3030                $this->assertNull( $this->request->get_header( 'missing' ) );
    3131                $this->assertNull( $this->request->get_header_as_array( 'missing' ) );
    3232        }
    33        
     33
    3434        public function test_remove_header() {
    3535                $this->request->add_header( 'Test-Header', 'value' );
    3636                $this->assertEquals( 'value', $this->request->get_header( 'Test-Header' ) );
    37                
     37
    3838                $this->request->remove_header( 'Test-Header' );
    3939                $this->assertNull( $this->request->get_header( 'Test-Header' ) );
    4040        }
     
    607607                        $request->get_json_params()
    608608                );
    609609        }
     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        }
    610644}