From 03c947c7fd4e34c438fad140e1d0420eccd1ef72 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 13 Dec 2016 13:52:49 +0000 Subject: [PATCH] REST API: Allow sending an empty or no-op comment update. In general, updates that don't actually change anything should succeed. [39371] added tests for other object types, and this commit fixes empty updates for comments and adds the missing test. Fixes #38700. git-svn-id: https://develop.svn.wordpress.org/trunk@39597 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-comments-controller.php | 9 ++++++- .../rest-api/rest-comments-controller.php | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 94d75cf42a..bd0c920c0f 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -664,6 +664,13 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { return $prepared_args; } + if ( ! empty( $prepared_args['comment_post_ID'] ) ) { + $post = get_post( $prepared_args['comment_post_ID'] ); + if ( empty( $post ) ) { + return new WP_Error( 'rest_comment_invalid_post_id', __( 'Invalid post ID.' ), array( 'status' => 403 ) ); + } + } + if ( empty( $prepared_args ) && isset( $request['status'] ) ) { // Only the comment status is being changed. $change = $this->handle_status_param( $request['status'], $id ); @@ -690,7 +697,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { $updated = wp_update_comment( wp_slash( (array) $prepared_args ) ); - if ( 0 === $updated ) { + if ( false === $updated ) { return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment failed.' ), array( 'status' => 500 ) ); } diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 27e7e4e813..9de7d88308 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -1961,6 +1961,22 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 ); } + public function test_update_item_no_change() { + $comment = get_comment( self::$approved_id ); + + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); + $request->set_param( 'post', $comment->comment_post_ID ); + + // Run twice to make sure that the update still succeeds even if no DB + // rows are updated. + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + } + public function test_update_comment_status() { wp_set_current_user( self::$admin_id ); @@ -2206,6 +2222,16 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 ); } + public function test_update_comment_invalid_post_id() { + wp_set_current_user( self::$admin_id ); + + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); + $request->set_param( 'post', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); + + $response = $this->server->dispatch( $request ); + $this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 ); + } + public function test_update_comment_invalid_permission() { add_filter( 'rest_allow_anonymous_comments', '__return_true' );