Make WordPress Core


Ignore:
Timestamp:
06/23/2022 02:24:08 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Remove dynamic properties in Tests_*_Slashes.

Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.

In this particular case, the test classes contain a set_up() method that sets a group of properties, which are used by the tests, but the values of these properties are never changed by the tests.

In other words, setting these properties in the set_up() is an unnecessary overhead and the properties should be changed to class constants.

Follow-up to [1041/tests], [1071/tests].

Props jrf.
See #56033.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/slashes.php

    r52010 r53557  
    77 */
    88class Tests_Comment_Slashes extends WP_UnitTestCase {
     9
     10    /*
     11     * It is important to test with both even and odd numbered slashes,
     12     * as KSES does a strip-then-add slashes in some of its function calls.
     13     */
     14
     15    const SLASH_1 = 'String with 1 slash \\';
     16    const SLASH_2 = 'String with 2 slashes \\\\';
     17    const SLASH_3 = 'String with 3 slashes \\\\\\';
     18    const SLASH_4 = 'String with 4 slashes \\\\\\\\';
     19    const SLASH_5 = 'String with 5 slashes \\\\\\\\\\';
     20    const SLASH_6 = 'String with 6 slashes \\\\\\\\\\\\';
     21    const SLASH_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
     22
    923    protected static $author_id;
    1024    protected static $post_id;
     
    2034
    2135        wp_set_current_user( self::$author_id );
    22 
    23         // It is important to test with both even and odd numbered slashes,
    24         // as KSES does a strip-then-add slashes in some of its function calls.
    25         $this->slash_1 = 'String with 1 slash \\';
    26         $this->slash_2 = 'String with 2 slashes \\\\';
    27         $this->slash_3 = 'String with 3 slashes \\\\\\';
    28         $this->slash_4 = 'String with 4 slashes \\\\\\\\';
    29         $this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
    30         $this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
    31         $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
    3236    }
    3337
     
    4246        $data       = array(
    4347            'comment_post_ID'      => $post_id,
    44             'comment_author'       => $this->slash_1,
     48            'comment_author'       => self::SLASH_1,
    4549            'comment_author_url'   => '',
    4650            'comment_author_email' => '',
    4751            'comment_type'         => '',
    48             'comment_content'      => $this->slash_7,
     52            'comment_content'      => self::SLASH_7,
    4953        );
    5054        $comment_id = wp_new_comment( $data );
     
    5256        $comment = get_comment( $comment_id );
    5357
    54         $this->assertSame( wp_unslash( $this->slash_1 ), $comment->comment_author );
    55         $this->assertSame( wp_unslash( $this->slash_7 ), $comment->comment_content );
     58        $this->assertSame( wp_unslash( self::SLASH_1 ), $comment->comment_author );
     59        $this->assertSame( wp_unslash( self::SLASH_7 ), $comment->comment_content );
    5660
    5761        $data       = array(
    5862            'comment_post_ID'      => $post_id,
    59             'comment_author'       => $this->slash_2,
     63            'comment_author'       => self::SLASH_2,
    6064            'comment_author_url'   => '',
    6165            'comment_author_email' => '',
    6266            'comment_type'         => '',
    63             'comment_content'      => $this->slash_4,
     67            'comment_content'      => self::SLASH_4,
    6468        );
    6569        $comment_id = wp_new_comment( $data );
     
    6771        $comment = get_comment( $comment_id );
    6872
    69         $this->assertSame( wp_unslash( $this->slash_2 ), $comment->comment_author );
    70         $this->assertSame( wp_unslash( $this->slash_4 ), $comment->comment_content );
     73        $this->assertSame( wp_unslash( self::SLASH_2 ), $comment->comment_author );
     74        $this->assertSame( wp_unslash( self::SLASH_4 ), $comment->comment_content );
    7175    }
    7276
     
    8791        $_POST['comment_ID']              = $comment_id;
    8892        $_POST['comment_status']          = '';
    89         $_POST['newcomment_author']       = $this->slash_1;
     93        $_POST['newcomment_author']       = self::SLASH_1;
    9094        $_POST['newcomment_author_url']   = '';
    9195        $_POST['newcomment_author_email'] = '';
    92         $_POST['content']                 = $this->slash_7;
     96        $_POST['content']                 = self::SLASH_7;
    9397
    9498        $_POST = add_magic_quotes( $_POST ); // The edit_comment() function will strip slashes.
     
    97101        $comment = get_comment( $comment_id );
    98102
    99         $this->assertSame( $this->slash_1, $comment->comment_author );
    100         $this->assertSame( $this->slash_7, $comment->comment_content );
     103        $this->assertSame( self::SLASH_1, $comment->comment_author );
     104        $this->assertSame( self::SLASH_7, $comment->comment_content );
    101105
    102106        $_POST                            = array();
    103107        $_POST['comment_ID']              = $comment_id;
    104108        $_POST['comment_status']          = '';
    105         $_POST['newcomment_author']       = $this->slash_2;
     109        $_POST['newcomment_author']       = self::SLASH_2;
    106110        $_POST['newcomment_author_url']   = '';
    107111        $_POST['newcomment_author_email'] = '';
    108         $_POST['content']                 = $this->slash_4;
     112        $_POST['content']                 = self::SLASH_4;
    109113
    110114        $_POST = add_magic_quotes( $_POST ); // The edit_comment() function will strip slashes.
     
    113117        $comment = get_comment( $comment_id );
    114118
    115         $this->assertSame( $this->slash_2, $comment->comment_author );
    116         $this->assertSame( $this->slash_4, $comment->comment_content );
     119        $this->assertSame( self::SLASH_2, $comment->comment_author );
     120        $this->assertSame( self::SLASH_4, $comment->comment_content );
    117121    }
    118122
     
    126130            array(
    127131                'comment_post_ID' => $post_id,
    128                 'comment_author'  => $this->slash_1,
    129                 'comment_content' => $this->slash_7,
     132                'comment_author'  => self::SLASH_1,
     133                'comment_content' => self::SLASH_7,
    130134            )
    131135        );
    132136        $comment    = get_comment( $comment_id );
    133137
    134         $this->assertSame( wp_unslash( $this->slash_1 ), $comment->comment_author );
    135         $this->assertSame( wp_unslash( $this->slash_7 ), $comment->comment_content );
     138        $this->assertSame( wp_unslash( self::SLASH_1 ), $comment->comment_author );
     139        $this->assertSame( wp_unslash( self::SLASH_7 ), $comment->comment_content );
    136140
    137141        $comment_id = wp_insert_comment(
    138142            array(
    139143                'comment_post_ID' => $post_id,
    140                 'comment_author'  => $this->slash_2,
    141                 'comment_content' => $this->slash_4,
     144                'comment_author'  => self::SLASH_2,
     145                'comment_content' => self::SLASH_4,
    142146            )
    143147        );
    144148        $comment    = get_comment( $comment_id );
    145149
    146         $this->assertSame( wp_unslash( $this->slash_2 ), $comment->comment_author );
    147         $this->assertSame( wp_unslash( $this->slash_4 ), $comment->comment_content );
     150        $this->assertSame( wp_unslash( self::SLASH_2 ), $comment->comment_author );
     151        $this->assertSame( wp_unslash( self::SLASH_4 ), $comment->comment_content );
    148152    }
    149153
     
    162166            array(
    163167                'comment_ID'      => $comment_id,
    164                 'comment_author'  => $this->slash_1,
    165                 'comment_content' => $this->slash_7,
     168                'comment_author'  => self::SLASH_1,
     169                'comment_content' => self::SLASH_7,
    166170            )
    167171        );
    168172        $comment = get_comment( $comment_id );
    169173
    170         $this->assertSame( wp_unslash( $this->slash_1 ), $comment->comment_author );
    171         $this->assertSame( wp_unslash( $this->slash_7 ), $comment->comment_content );
     174        $this->assertSame( wp_unslash( self::SLASH_1 ), $comment->comment_author );
     175        $this->assertSame( wp_unslash( self::SLASH_7 ), $comment->comment_content );
    172176
    173177        wp_update_comment(
    174178            array(
    175179                'comment_ID'      => $comment_id,
    176                 'comment_author'  => $this->slash_2,
    177                 'comment_content' => $this->slash_4,
     180                'comment_author'  => self::SLASH_2,
     181                'comment_content' => self::SLASH_4,
    178182            )
    179183        );
    180184        $comment = get_comment( $comment_id );
    181185
    182         $this->assertSame( wp_unslash( $this->slash_2 ), $comment->comment_author );
    183         $this->assertSame( wp_unslash( $this->slash_4 ), $comment->comment_content );
     186        $this->assertSame( wp_unslash( self::SLASH_2 ), $comment->comment_author );
     187        $this->assertSame( wp_unslash( self::SLASH_4 ), $comment->comment_content );
    184188    }
    185189
Note: See TracChangeset for help on using the changeset viewer.