Make WordPress Core

Changeset 39628


Ignore:
Timestamp:
12/21/2016 05:12:06 AM (10 years ago)
Author:
pento
Message:

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.

Merges [39597] to the 4.7 branch.

Props jnylen0.
Fixes #38700.

Location:
branches/4.7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39566 r39628  
    665665                }
    666666
     667                if ( ! empty( $prepared_args['comment_post_ID'] ) ) {
     668                        $post = get_post( $prepared_args['comment_post_ID'] );
     669                        if ( empty( $post ) ) {
     670                                return new WP_Error( 'rest_comment_invalid_post_id', __( 'Invalid post ID.' ), array( 'status' => 403 ) );
     671                        }
     672                }
     673
    667674                if ( empty( $prepared_args ) && isset( $request['status'] ) ) {
    668675                        // Only the comment status is being changed.
     
    691698                        $updated = wp_update_comment( wp_slash( (array) $prepared_args ) );
    692699
    693                         if ( 0 === $updated ) {
     700                        if ( false === $updated ) {
    694701                                return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment failed.' ), array( 'status' => 500 ) );
    695702                        }
  • branches/4.7/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r39566 r39628  
    19621962        }
    19631963
     1964        public function test_update_item_no_change() {
     1965                $comment = get_comment( self::$approved_id );
     1966
     1967                wp_set_current_user( self::$admin_id );
     1968                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     1969                $request->set_param( 'post', $comment->comment_post_ID );
     1970
     1971                // Run twice to make sure that the update still succeeds even if no DB
     1972                // rows are updated.
     1973                $response = $this->server->dispatch( $request );
     1974                $this->assertEquals( 200, $response->get_status() );
     1975
     1976                $response = $this->server->dispatch( $request );
     1977                $this->assertEquals( 200, $response->get_status() );
     1978        }
     1979
    19641980        public function test_update_comment_status() {
    19651981                wp_set_current_user( self::$admin_id );
     
    22052221                $response = $this->server->dispatch( $request );
    22062222                $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
     2223        }
     2224
     2225        public function test_update_comment_invalid_post_id() {
     2226                wp_set_current_user( self::$admin_id );
     2227
     2228                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     2229                $request->set_param( 'post', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
     2230
     2231                $response = $this->server->dispatch( $request );
     2232                $this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
    22072233        }
    22082234
Note: See TracChangeset for help on using the changeset viewer.