Make WordPress Core

Ticket #56712: 56712.diff

File 56712.diff, 6.4 KB (added by SergeyBiryukov, 4 years ago)
  • src/wp-includes/comment.php

     
    34273427 */
    34283428function wp_handle_comment_submission( $comment_data ) {
    34293429        $comment_post_id      = 0;
    3430         $comment_author       = null;
    3431         $comment_author_email = null;
    3432         $comment_author_url   = null;
    3433         $comment_content      = null;
     3430        $comment_author       = '';
     3431        $comment_author_email = '';
     3432        $comment_author_url   = '';
     3433        $comment_content      = '';
    34343434        $comment_parent       = 0;
    34353435        $user_id              = 0;
    34363436
  • tests/phpunit/tests/comment-submission.php

     
    223223         * @covers ::wp_handle_comment_submission
    224224         */
    225225        public function test_submitting_comment_to_password_protected_post_succeeds() {
    226                 if ( PHP_VERSION_ID >= 80100 ) {
    227                         /*
    228                          * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
    229                          * via hooked in filter functions until a more structural solution to the
    230                          * "missing input validation" conundrum has been architected and implemented.
    231                          */
    232                         $this->expectDeprecation();
    233                         $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
    234                 }
    235 
    236226                $password = 'password';
    237227                $hasher   = new PasswordHash( 8, true );
    238228
    … …  
    321311         *
    322312         * @covers ::wp_handle_comment_submission
    323313         */
    324         public function test_submitting_comment_handles_slashes_correctly_handles_slashes() {
    325                 if ( PHP_VERSION_ID >= 80100 ) {
    326                         /*
    327                          * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
    328                          * via hooked in filter functions until a more structural solution to the
    329                          * "missing input validation" conundrum has been architected and implemented.
    330                          */
    331                         $this->expectDeprecation();
    332                         $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
    333                 }
    334 
     314        public function test_submitting_comment_handles_slashes_correctly() {
    335315                $data    = array(
    336316                        'comment_post_ID' => self::$post->ID,
    337317                        'comment'         => 'Comment with 1 slash: \\',
    … …  
    496476         * @covers ::wp_handle_comment_submission
    497477         */
    498478        public function test_anonymous_user_cannot_comment_unfiltered_html() {
    499                 if ( PHP_VERSION_ID >= 80100 ) {
    500                         /*
    501                          * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
    502                          * via hooked in filter functions until a more structural solution to the
    503                          * "missing input validation" conundrum has been architected and implemented.
    504                          */
    505                         $this->expectDeprecation();
    506                         $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
    507                 }
    508 
    509479                $data    = array(
    510480                        'comment_post_ID' => self::$post->ID,
    511481                        'comment'         => 'Comment <script>alert(document.cookie);</script>',
    … …  
    832802         * @covers ::wp_handle_comment_submission
    833803         */
    834804        public function test_submitting_comment_with_empty_type_results_in_correct_type() {
    835                 if ( PHP_VERSION_ID >= 80100 ) {
    836                         /*
    837                          * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
    838                          * via hooked in filter functions until a more structural solution to the
    839                          * "missing input validation" conundrum has been architected and implemented.
    840                          */
    841                         $this->expectDeprecation();
    842                         $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
    843                 }
    844 
    845805                $data    = array(
    846806                        'comment_post_ID' => self::$post->ID,
    847807                        'comment'         => 'Comment',
    … …  
    920880
    921881        }
    922882
     883        /**
     884         * @ticket 56712
     885         *
     886         * @covers ::wp_handle_comment_submission
     887         */
     888        public function test_submitting_comment_without_optional_parameters_sets_them_to_empty_strings() {
     889                $data = array(
     890                        'comment_post_ID' => self::$post->ID,
     891                );
     892
     893                add_filter( 'pre_option_require_name_email', '__return_zero' );
     894                add_filter( 'allow_empty_comment', '__return_true' );
     895
     896                add_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) );
     897
     898                $comment = wp_handle_comment_submission( $data );
     899
     900                $this->assertNotWPError( $comment );
     901                $this->assertInstanceOf( 'WP_Comment', $comment );
     902
     903                $commentdata = $this->preprocess_comment_data;
     904
     905                $this->assertSame( '', $commentdata['comment_author'], 'Comment author should default to an empty string.' );
     906                $this->assertSame( '', $commentdata['comment_author_email'], 'Comment author email should default to an empty string.' );
     907                $this->assertSame( '', $commentdata['comment_author_url'], 'Comment author URL should default to an empty string.' );
     908                $this->assertSame( '', $commentdata['comment_content'], 'Comment content should default to an empty string.' );
     909
     910        }
     911
    923912        public function filter_preprocess_comment( $commentdata ) {
    924913                $this->preprocess_comment_data = $commentdata;
    925914                return $commentdata;
    … …  
    931920         * @covers ::wp_handle_comment_submission
    932921         */
    933922        public function test_submitting_duplicate_comments() {
    934                 if ( PHP_VERSION_ID >= 80100 ) {
    935                         /*
    936                          * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
    937                          * via hooked in filter functions until a more structural solution to the
    938                          * "missing input validation" conundrum has been architected and implemented.
    939                          */
    940                         $this->expectDeprecation();
    941                         $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
    942                 }
    943 
    944923                $data           = array(
    945924                        'comment_post_ID' => self::$post->ID,
    946925                        'comment'         => 'Did I say that?',
    … …  
    959938         * @covers ::wp_handle_comment_submission
    960939         */
    961940        public function test_comments_flood() {
    962                 if ( PHP_VERSION_ID >= 80100 ) {
    963                         /*
    964                          * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
    965                          * via hooked in filter functions until a more structural solution to the
    966                          * "missing input validation" conundrum has been architected and implemented.
    967                          */
    968                         $this->expectDeprecation();
    969                         $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
    970                 }
    971 
    972941                $data          = array(
    973942                        'comment_post_ID' => self::$post->ID,
    974943                        'comment'         => 'Did I say that?',