Make WordPress Core

Ticket #38817: 38817.2.diff

File 38817.2.diff, 2.3 KB (added by rachelbaker, 9 years ago)

Added ! empty spacing and a unit test

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

     
    10411041                        $prepared_comment['comment_author_IP'] = $request['author_ip'];
    10421042                }
    10431043
    1044                 if ( isset( $request['author_user_agent'] ) ) {
     1044                if ( ! empty( $request['author_user_agent'] ) ) {
    10451045                        $prepared_comment['comment_agent'] = $request['author_user_agent'];
     1046                } elseif ( $request->get_header( 'user_agent' ) ) {
     1047                        $prepared_comment['comment_agent'] = $request->get_header( 'user_agent' );
    10461048                }
    10471049
    10481050                if ( isset( $request['type'] ) ) {
  • tests/phpunit/tests/rest-api/rest-comments-controller.php

     
    12461246                $this->assertEquals( 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', $data['author_user_agent'] );
    12471247        }
    12481248
     1249        public function test_create_comment_user_agent_header() {
     1250                wp_set_current_user( self::$admin_id );
     1251
     1252                $params = array(
     1253                        'post'         => self::$post_id,
     1254                        'author_name'  => 'Homer Jay Simpson',
     1255                        'author_email' => 'chunkylover53@aol.com',
     1256                        'author_url'   => 'http://compuglobalhypermeganet.com',
     1257                        'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
     1258                );
     1259
     1260                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1261                $request->add_header( 'content-type', 'application/json' );
     1262                $request->add_header( 'user_agent', 'Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)' );
     1263                $request->set_body( wp_json_encode( $params ) );
     1264
     1265                $response = $this->server->dispatch( $request );
     1266                $this->assertEquals( 201, $response->get_status() );
     1267
     1268                $data = $response->get_data();
     1269
     1270                $new_comment = get_comment( $data['id'] );
     1271                $this->assertEquals( 'Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)', $new_comment->comment_agent );
     1272        }
     1273
    12491274        public function test_create_comment_invalid_author_IP() {
    12501275                wp_set_current_user( self::$admin_id );
    12511276