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
This commit is contained in:
parent
25330a0220
commit
03c947c7fd
@ -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 ) );
|
||||
}
|
||||
|
||||
|
@ -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' );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user