Make WordPress Core

Changeset 39196


Ignore:
Timestamp:
11/10/2016 03:34:30 AM (10 years ago)
Author:
joehoyle
Message:

REST API: Allow updating a comment without the content present.

For all resources in the REST API, sending partial updates is supported. This fixes needing to _always_ specify comment content.

Props jnylen.
Fixes #38720.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39179 r39196  
    432432
    433433                /*
    434                  * Do not allow a comment to be created with an empty string for
     434                 * Do not allow a comment to be created with missing or empty
    435435                 * comment_content. See wp_handle_comment_submission().
    436436                 */
    437                 if ( '' === $prepared_comment['comment_content'] ) {
     437                if ( empty( $prepared_comment['comment_content'] ) ) {
    438438                        return new WP_Error( 'rest_comment_content_invalid', __( 'Comment content is invalid.' ), array( 'status' => 400 ) );
    439439                }
     
    635635                        if ( is_wp_error( $prepared_args ) ) {
    636636                                return $prepared_args;
     637                        }
     638
     639                        if ( isset( $prepared_args['comment_content'] ) && empty( $prepared_args['comment_content'] ) ) {
     640                                return new WP_Error( 'rest_comment_content_invalid', __( 'Comment content is invalid.' ), array( 'status' => 400 ) );
    637641                        }
    638642
     
    10631067                                list( $prepared_comment['comment_date'], $prepared_comment['comment_date_gmt'] ) = $date_data;
    10641068                        }
    1065                 }
    1066 
    1067                 // Require 'comment_content' unless only the 'comment_status' is being updated.
    1068                 if ( ! empty( $prepared_comment ) && ! isset( $prepared_comment['comment_content'] ) ) {
    1069                         return new WP_Error( 'rest_comment_content_required', __( 'Missing comment content.' ), array( 'status' => 400 ) );
    10701069                }
    10711070
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r39158 r39196  
    930930        }
    931931
    932         public function test_create_item_invalid_blank_content() {
     932        public function test_create_item_invalid_no_content() {
    933933                wp_set_current_user( 0 );
    934934
     
    938938                        'author_email' => 'lovejoy@example.com',
    939939                        'author_url'   => 'http://timothylovejoy.jr',
    940                         'content'      => '',
    941                 );
    942 
    943                 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    944                 $request->add_header( 'content-type', 'application/json' );
    945                 $request->set_body( wp_json_encode( $params ) );
    946 
     940                );
     941
     942                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     943                $request->add_header( 'content-type', 'application/json' );
     944                $request->set_body( wp_json_encode( $params ) );
     945
     946                $response = $this->server->dispatch( $request );
     947                $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
     948
     949                $params['content'] = '';
     950                $request->set_body( wp_json_encode( $params ) );
    947951                $response = $this->server->dispatch( $request );
    948952                $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
     
    16171621                $this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
    16181622                $this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
     1623        }
     1624
     1625        public function test_update_item_no_content() {
     1626                $post_id = $this->factory->post->create();
     1627
     1628                wp_set_current_user( self::$admin_id );
     1629
     1630                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     1631                $request->set_param( 'author_email', 'another@email.com' );
     1632
     1633                // Sending a request without content is fine.
     1634                $response = $this->server->dispatch( $request );
     1635                $this->assertEquals( 200, $response->get_status() );
     1636
     1637                // Sending a request with empty comment is not fine.
     1638                $request->set_param( 'author_email', 'yetanother@email.com' );
     1639                $request->set_param( 'content', '' );
     1640                $response = $this->server->dispatch( $request );
     1641                $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
    16191642        }
    16201643
Note: See TracChangeset for help on using the changeset viewer.