Make WordPress Core

Ticket #38720: 38720.diff

File 38720.diff, 2.9 KB (added by jnylen0, 7 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    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 2b7057c..1021cc2 100644
    a b class WP_REST_Comments_Controller extends WP_REST_Controller { 
    431431                }
    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'] ) {
    438                         return new WP_Error( 'rest_comment_content_invalid', __( 'Comment content is invalid.' ), array( 'status' => 400 ) );
     437                if ( empty( $prepared_comment['comment_content'] ) ) {
     438                        return new WP_Error( 'rest_comment_content_required', __( 'Missing comment content.' ), array( 'status' => 400 ) );
    439439                }
    440440
    441441                // Setting remaining values before wp_insert_comment so we can use wp_allow_comment().
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    10641064                        }
    10651065                }
    10661066
    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 ) );
    1070                 }
    1071 
    10721067                /**
    10731068                 * Filters a comment after it is prepared for the database.
    10741069                 *
  • tests/phpunit/tests/rest-api/rest-comments-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
    index e225cbc..895a91a 100644
    a b class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    827827                $this->assertEquals( self::$post_id, $data['post'] );
    828828        }
    829829
     830        public function test_create_item_no_content() {
     831                wp_set_current_user( 0 );
     832
     833                $params = array(
     834                        'post'    => self::$post_id,
     835                        'author_name'  => 'Comic Book Guy',
     836                        'author_email' => 'cbg@androidsdungeon.com',
     837                        'author_url'   => 'http://androidsdungeon.com',
     838                        'content' => '',
     839                        'date'    => '2014-11-07T10:14:25',
     840                );
     841
     842                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     843                $request->add_header( 'content-type', 'application/json' );
     844                $request->set_body( wp_json_encode( $params ) );
     845                $response = $this->server->dispatch( $request );
     846                $this->assertErrorResponse( 'rest_comment_content_required', $response, 400 );
     847
     848                unset( $params['content'] );
     849                $request->set_body( wp_json_encode( $params ) );
     850                $response = $this->server->dispatch( $request );
     851                $this->assertErrorResponse( 'rest_comment_content_required', $response, 400 );
     852        }
     853
    830854        public function test_create_item_using_accepted_content_raw_value() {
    831855                wp_set_current_user( 0 );
    832856