Make WordPress Core


Ignore:
Timestamp:
10/24/2020 10:44:38 PM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API, XML-RPC: Synchronise empty comment content checks.

The REST API and XML-RPC now uses the same detection methodology for empty comment content as wp_handle_comment_submission(). Specifically, comments now have their content trimmed and '0' is allowed.

Props jaswrks, rmccue, dd32, rachelbaker, Cawa-93, aduth, TimothyBlynJacobs.
Fixes #43177.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r49299 r49303  
    13991399        }
    14001400
     1401        /**
     1402         * @ticket 43177
     1403         */
     1404        public function test_create_item_invalid_only_spaces_content() {
     1405                wp_set_current_user( self::$admin_id );
     1406
     1407                $params = array(
     1408                        'post'         => self::$post_id,
     1409                        'author_name'  => 'Reverend Lovejoy',
     1410                        'author_email' => 'lovejoy@example.com',
     1411                        'author_url'   => 'http://timothylovejoy.jr',
     1412                        'content'      => '   ',
     1413                );
     1414
     1415                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1416                $request->add_header( 'content-type', 'application/json' );
     1417                $request->set_body( wp_json_encode( $params ) );
     1418
     1419                $response = rest_get_server()->dispatch( $request );
     1420                $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
     1421        }
     1422
     1423        /**
     1424         * @ticket 43177
     1425         */
     1426        public function test_create_item_allows_0_as_content() {
     1427                wp_set_current_user( self::$admin_id );
     1428
     1429                $params = array(
     1430                        'post'         => self::$post_id,
     1431                        'author_name'  => 'Reverend Lovejoy',
     1432                        'author_email' => 'lovejoy@example.com',
     1433                        'author_url'   => 'http://timothylovejoy.jr',
     1434                        'content'      => '0',
     1435                );
     1436
     1437                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1438                $request->add_header( 'content-type', 'application/json' );
     1439                $request->set_body( wp_json_encode( $params ) );
     1440
     1441                $response = rest_get_server()->dispatch( $request );
     1442                $this->assertSame( 201, $response->get_status() );
     1443                $this->assertEquals( '0', $response->get_data()['content']['raw'] );
     1444        }
     1445
     1446        /**
     1447         * @ticket 43177
     1448         */
     1449        public function test_create_item_allow_empty_comment_filter() {
     1450                add_filter( 'allow_empty_comment', '__return_true' );
     1451
     1452                wp_set_current_user( self::$admin_id );
     1453
     1454                $params = array(
     1455                        'post'         => self::$post_id,
     1456                        'author_name'  => 'Reverend Lovejoy',
     1457                        'author_email' => 'lovejoy@example.com',
     1458                        'author_url'   => 'http://timothylovejoy.jr',
     1459                        'content'      => '',
     1460                );
     1461
     1462                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1463                $request->add_header( 'content-type', 'application/json' );
     1464                $request->set_body( wp_json_encode( $params ) );
     1465
     1466                $response = rest_get_server()->dispatch( $request );
     1467                $this->assertSame( 201, $response->get_status() );
     1468                $this->assertEquals( '', $response->get_data()['content']['raw'] );
     1469        }
     1470
    14011471        public function test_create_item_invalid_date() {
    14021472                wp_set_current_user( self::$admin_id );
Note: See TracChangeset for help on using the changeset viewer.