Skip to content

Commit eeb8fbf

Browse files
committed
REST API: Return a more appropriate HTTP 400 response code when attempting to create or update a non-existent setting.
This switches the response from a 200, which is not appropriate for invalid requests. Props sheldorofazeroth, johnbillion Fixes #41604 git-svn-id: https://develop.svn.wordpress.org/trunk@60301 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1f9d279 commit eeb8fbf

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ public function update_item( $request ) {
147147

148148
$params = $request->get_params();
149149

150+
if ( empty( $params ) || ! empty( array_diff_key( $params, $options ) ) ) {
151+
$message = empty( $params )
152+
? __( 'Request body cannot be empty.' )
153+
: __( 'Invalid parameter(s) provided.' );
154+
155+
return new WP_Error(
156+
'rest_invalid_param',
157+
$message,
158+
array( 'status' => 400 )
159+
);
160+
}
161+
150162
foreach ( $options as $name => $args ) {
151163
if ( ! array_key_exists( $name, $params ) ) {
152164
continue;

tests/phpunit/tests/rest-api/rest-settings-controller.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,21 @@ public function test_get_item_with_invalid_object_array_in_options() {
385385
}
386386

387387
/**
388-
* @doesNotPerformAssertions
388+
* Settings can't be created
389389
*/
390390
public function test_create_item() {
391-
// Controller does not implement create_item().
391+
wp_set_current_user( self::$administrator );
392+
393+
$request = new WP_REST_Request( 'POST', '/wp/v2/settings' );
394+
$request->set_param( 'new_setting', 'New value' );
395+
$response = rest_get_server()->dispatch( $request );
396+
397+
$this->assertSame( 400, $response->get_status() );
392398
}
393399

394400
public function test_update_item() {
395401
wp_set_current_user( self::$administrator );
402+
396403
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
397404
$request->set_param( 'title', 'The new title!' );
398405
$response = rest_get_server()->dispatch( $request );
@@ -403,6 +410,27 @@ public function test_update_item() {
403410
$this->assertSame( get_option( 'blogname' ), $data['title'] );
404411
}
405412

413+
public function test_update_nonexistent_item() {
414+
wp_set_current_user( self::$administrator );
415+
416+
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
417+
$request->set_param( 'i_do_no_exist', 'New value' );
418+
$response = rest_get_server()->dispatch( $request );
419+
420+
$this->assertSame( 400, $response->get_status() );
421+
}
422+
423+
public function test_update_partially_valid_items() {
424+
wp_set_current_user( self::$administrator );
425+
426+
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
427+
$request->set_param( 'title', 'The new title!' );
428+
$request->set_param( 'i_do_no_exist', 'New value' );
429+
$response = rest_get_server()->dispatch( $request );
430+
431+
$this->assertSame( 400, $response->get_status() );
432+
}
433+
406434
public function update_setting_custom_callback( $result, $name, $value, $args ) {
407435
if ( 'title' === $name && 'The new title!' === $value ) {
408436
// Do not allow changing the title in this case.

0 commit comments

Comments
 (0)