| | 944 | * Make sure that a sanitization that transforms the argument type will not |
| | 945 | * cause the validation to fail. |
| | 946 | * |
| | 947 | * @ticket 37192 |
| | 948 | */ |
| | 949 | public function test_rest_validate_before_sanitization() { |
| | 950 | register_rest_route( 'test-ns', '/test', array( |
| | 951 | 'methods' => array( 'GET' ), |
| | 952 | 'callback' => '__return_null', |
| | 953 | 'args' => array( |
| | 954 | 'someinteger' => array( |
| | 955 | 'validate_callback' => array( $this, '_validate_as_integer_123' ), |
| | 956 | 'sanitize_callback' => 'absint', |
| | 957 | ), |
| | 958 | 'somestring' => array( |
| | 959 | 'validate_callback' => array( $this, '_validate_as_string_foo' ), |
| | 960 | 'sanitize_callback' => 'absint', |
| | 961 | ), |
| | 962 | ), |
| | 963 | ) ); |
| | 964 | |
| | 965 | $request = new WP_REST_Request( 'GET', '/test-ns/test' ); |
| | 966 | $request->set_query_params( array( 'someinteger' => 123, 'somestring' => 'foo' ) ); |
| | 967 | $response = $this->server->dispatch( $request ); |
| | 968 | $this->assertEquals( 200, $response->get_status() ); |
| | 969 | } |
| | 970 | |
| | 971 | public function _validate_as_integer_123( $value, $request, $key ) { |
| | 972 | if ( ! is_int( $value ) ) { |
| | 973 | return new WP_Error( 'some-error', 'This is not valid!' ); |
| | 974 | } |
| | 975 | |
| | 976 | return true; |
| | 977 | } |
| | 978 | |
| | 979 | public function _validate_as_string_foo( $value, $request, $key ) { |
| | 980 | if ( ! is_string( $value ) ) { |
| | 981 | return new WP_Error( 'some-error', 'This is not valid!' ); |
| | 982 | } |
| | 983 | |
| | 984 | return true; |
| | 985 | } |
| | 986 | |
| | 987 | /** |