Make WordPress Core

Ticket #38971: 38971.5.diff

File 38971.5.diff, 11.9 KB (added by jnylen0, 8 years ago)

Fix author updates (allow unsetting name and email)

  • 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 b07ced4..9df1e33 100644
    a b class WP_REST_Comments_Controller extends WP_REST_Controller { 
    508508
    509509                // Honor the discussion setting that requires a name and email address of the comment author.
    510510                if ( get_option( 'require_name_email' ) ) {
    511                         if ( ! isset( $prepared_comment['comment_author'] ) && ! isset( $prepared_comment['comment_author_email'] ) ) {
    512                                 return new WP_Error( 'rest_comment_author_data_required', __( 'Creating a comment requires valid author name and email values.' ), array( 'status' => 400 ) );
    513                         }
    514 
    515                         if ( ! isset( $prepared_comment['comment_author'] ) ) {
    516                                 return new WP_Error( 'rest_comment_author_required', __( 'Creating a comment requires a valid author name.' ), array( 'status' => 400 ) );
    517                         }
    518 
    519                         if ( ! isset( $prepared_comment['comment_author_email'] ) ) {
    520                                 return new WP_Error( 'rest_comment_author_email_required', __( 'Creating a comment requires a valid author email.' ), array( 'status' => 400 ) );
     511                        if ( empty( $prepared_comment['comment_author'] ) || empty( $prepared_comment['comment_author_email'] ) ) {
     512                                return new WP_Error( 'rest_comment_author_required', __( 'Creating a comment requires valid author name and email values.' ), array( 'status' => 400 ) );
    521513                        }
    522514                }
    523515
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    11551147                                        'type'         => 'string',
    11561148                                        'format'       => 'email',
    11571149                                        'context'      => array( 'edit' ),
     1150                                        'arg_options'  => array(
     1151                                                'sanitize_callback' => array( $this, 'check_author_email' ),
     1152                                                'validate_callback' => null, // skip built-in validation of 'email'
     1153                                        ),
    11581154                                ),
    11591155                                'author_ip'     => array(
    11601156                                        'description'  => __( 'IP address for the object author.' ),
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    15811577
    15821578                return current_user_can( 'edit_comment', $comment->comment_ID );
    15831579        }
     1580
     1581        /**
     1582         * Checks a comment author email for validity.
     1583         *
     1584         * Author emails can either be a valid email address or blank to unset the
     1585         * email.  Unsetting the email is only allowed on update; a blank email on
     1586         * create is detected later on.
     1587         *
     1588         * @since 4.7.0
     1589         *
     1590         * @param  mixed            $value   The username submitted in the request.
     1591         * @param  WP_REST_Request  $request Full details about the request.
     1592         * @param  string           $param   The parameter name.
     1593         * @return WP_Error|string The sanitized email address, if valid, otherwise an error.
     1594         */
     1595        public function check_author_email( $value, $request, $param ) {
     1596                $email = (string) $value;
     1597                if ( empty( $email ) ) {
     1598                        return $email;
     1599                }
     1600                $check_email = rest_validate_request_arg( $email, $request, $param );
     1601                if ( is_wp_error( $check_email ) ) {
     1602                        return $check_email;
     1603                }
     1604                return $email;
     1605        }
    15841606}
  • 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 112a26c..448693a 100644
    a b class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    9898        }
    9999
    100100        public function tearDown() {
     101                remove_filter( 'rest_allow_anonymous_comments', '__return_true' );
     102                update_option( 'require_name_email', 0 );
     103                update_option( 'comment_registration', 0 );
     104                update_option( 'show_avatars', 1 );
    101105                parent::tearDown();
    102106        }
    103107
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    987991                $this->assertEquals( $params['content']['raw'], $new_comment->comment_content );
    988992        }
    989993
    990         public function test_create_comment_missing_required_author_name_and_email_per_option_value() {
     994        public function test_create_comment_missing_required_author_name() {
    991995                add_filter( 'rest_allow_anonymous_comments', '__return_true' );
    992996                update_option( 'require_name_email', 1 );
    993997
    994998                $params = array(
    995                         'post'    => self::$post_id,
    996                         'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
     999                        'post'         => self::$post_id,
     1000                        'author_email' => 'ekrabappel@springfield-elementary.edu',
     1001                        'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
    9971002                );
    9981003
    9991004                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    10021007
    10031008                $response = $this->server->dispatch( $request );
    10041009
    1005                 $this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 );
    1006 
    1007                 update_option( 'require_name_email', 0 );
     1010                $this->assertErrorResponse( 'rest_comment_author_required', $response, 400 );
    10081011        }
    10091012
    1010         public function test_create_comment_missing_required_author_name_per_option_value() {
    1011                 wp_set_current_user( self::$admin_id );
     1013        public function test_create_comment_empty_required_author_name() {
     1014                add_filter( 'rest_allow_anonymous_comments', '__return_true' );
    10121015                update_option( 'require_name_email', 1 );
    10131016
    10141017                $params = array(
    1015                         'post'         => self::$post_id,
     1018                        'author_name'  => '',
    10161019                        'author_email' => 'ekrabappel@springfield-elementary.edu',
     1020                        'post'         => self::$post_id,
    10171021                        'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
    10181022                );
    10191023
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    10221026                $request->set_body( wp_json_encode( $params ) );
    10231027
    10241028                $response = $this->server->dispatch( $request );
    1025                 $this->assertErrorResponse( 'rest_comment_author_required', $response, 400 );
    10261029
    1027                 update_option( 'require_name_email', 0 );
     1030                $this->assertErrorResponse( 'rest_comment_author_required', $response, 400 );
    10281031        }
    10291032
    1030         public function test_create_comment_missing_required_author_email_per_option_value() {
     1033        public function test_create_comment_missing_required_author_email() {
    10311034                wp_set_current_user( self::$admin_id );
    10321035                update_option( 'require_name_email', 1 );
    10331036
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    10421045                $request->set_body( wp_json_encode( $params ) );
    10431046
    10441047                $response = $this->server->dispatch( $request );
    1045                 $this->assertErrorResponse( 'rest_comment_author_email_required', $response, 400 );
     1048                $this->assertErrorResponse( 'rest_comment_author_required', $response, 400 );
     1049        }
    10461050
    1047                 update_option( 'require_name_email', 0 );
     1051        public function test_create_comment_empty_required_author_email() {
     1052                wp_set_current_user( self::$admin_id );
     1053                update_option( 'require_name_email', 1 );
     1054
     1055                $params = array(
     1056                        'post'         => self::$post_id,
     1057                        'author_name'  => 'Edna Krabappel',
     1058                        'author_email' => '',
     1059                        'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
     1060                );
     1061
     1062                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1063                $request->add_header( 'content-type', 'application/json' );
     1064                $request->set_body( wp_json_encode( $params ) );
     1065
     1066                $response = $this->server->dispatch( $request );
     1067                $this->assertErrorResponse( 'rest_comment_author_required', $response, 400 );
    10481068        }
    10491069
    10501070        public function test_create_comment_author_email_too_short() {
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    19731993                $this->assertEquals( $params['date_gmt'], mysql_to_rfc3339( $updated->comment_date_gmt ) );
    19741994        }
    19751995
     1996        public function test_update_comment_author_email_only() {
     1997                wp_set_current_user( self::$editor_id );
     1998                update_option( 'require_name_email', 1 );
     1999
     2000                $params = array(
     2001                        'post'         => self::$post_id,
     2002                        'author_email' => 'ekrabappel@springfield-elementary.edu',
     2003                        'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
     2004                );
     2005
     2006                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     2007                $request->add_header( 'content-type', 'application/json' );
     2008                $request->set_body( wp_json_encode( $params ) );
     2009
     2010                $response = $this->server->dispatch( $request );
     2011                $this->assertEquals( 200, $response->get_status() );
     2012        }
     2013
     2014        public function test_update_comment_empty_author_name() {
     2015                wp_set_current_user( self::$editor_id );
     2016                update_option( 'require_name_email', 1 );
     2017
     2018                $params = array(
     2019                        'author_name'  => '',
     2020                        'author_email' => 'ekrabappel@springfield-elementary.edu',
     2021                        'post'         => self::$post_id,
     2022                        'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
     2023                );
     2024
     2025                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     2026                $request->add_header( 'content-type', 'application/json' );
     2027                $request->set_body( wp_json_encode( $params ) );
     2028
     2029                $response = $this->server->dispatch( $request );
     2030                $this->assertEquals( 200, $response->get_status() );
     2031        }
     2032
     2033        public function test_update_comment_author_name_only() {
     2034                wp_set_current_user( self::$admin_id );
     2035                update_option( 'require_name_email', 1 );
     2036
     2037                $params = array(
     2038                        'post'        => self::$post_id,
     2039                        'author_name' => 'Edna Krabappel',
     2040                        'content'     => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
     2041                );
     2042
     2043                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     2044                $request->add_header( 'content-type', 'application/json' );
     2045                $request->set_body( wp_json_encode( $params ) );
     2046
     2047                $response = $this->server->dispatch( $request );
     2048                $this->assertEquals( 200, $response->get_status() );
     2049        }
     2050
     2051        public function test_update_comment_empty_author_email() {
     2052                wp_set_current_user( self::$admin_id );
     2053                update_option( 'require_name_email', 1 );
     2054
     2055                $params = array(
     2056                        'post'         => self::$post_id,
     2057                        'author_name'  => 'Edna Krabappel',
     2058                        'author_email' => '',
     2059                        'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
     2060                );
     2061
     2062                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     2063                $request->add_header( 'content-type', 'application/json' );
     2064                $request->set_body( wp_json_encode( $params ) );
     2065
     2066                $response = $this->server->dispatch( $request );
     2067                $this->assertEquals( 200, $response->get_status() );
     2068        }
     2069
     2070        public function test_update_comment_author_email_too_short() {
     2071                wp_set_current_user( self::$admin_id );
     2072
     2073                $params = array(
     2074                        'post'         => self::$post_id,
     2075                        'author_name'  => 'Homer J. Simpson',
     2076                        'author_email' => 'a@b',
     2077                        'content'      => 'in this house, we obey the laws of thermodynamics!',
     2078                );
     2079
     2080                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     2081                $request->add_header( 'content-type', 'application/json' );
     2082                $request->set_body( wp_json_encode( $params ) );
     2083                $response = $this->server->dispatch( $request );
     2084
     2085                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     2086                $data = $response->get_data();
     2087                $this->assertArrayHasKey( 'author_email', $data['data']['params'] );
     2088        }
     2089
    19762090        public function test_update_comment_invalid_type() {
    19772091                wp_set_current_user( self::$admin_id );
    19782092