Make WordPress Core


Ignore:
Timestamp:
06/23/2022 02:24:08 PM (2 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/user/slashes.php

    r52010 r53557  
    77 */
    88class Tests_User_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 $user_id;
     
    1933
    2034        wp_set_current_user( self::$author_id );
    21 
    22         // It is important to test with both even and odd numbered slashes,
    23         // as KSES does a strip-then-add slashes in some of its function calls.
    24         $this->slash_1 = 'String with 1 slash \\';
    25         $this->slash_2 = 'String with 2 slashes \\\\';
    26         $this->slash_3 = 'String with 3 slashes \\\\\\';
    27         $this->slash_4 = 'String with 4 slashes \\\\\\\\';
    28         $this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
    29         $this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
    30         $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
    3135    }
    3236
     
    4347        $_POST['role']         = 'subscriber';
    4448        $_POST['email']        = 'user1@example.com';
    45         $_POST['first_name']   = $this->slash_1;
    46         $_POST['last_name']    = $this->slash_3;
    47         $_POST['nickname']     = $this->slash_5;
    48         $_POST['display_name'] = $this->slash_7;
    49         $_POST['description']  = $this->slash_3;
     49        $_POST['first_name']   = self::SLASH_1;
     50        $_POST['last_name']    = self::SLASH_3;
     51        $_POST['nickname']     = self::SLASH_5;
     52        $_POST['display_name'] = self::SLASH_7;
     53        $_POST['description']  = self::SLASH_3;
    5054
    5155        $_POST = add_magic_quotes( $_POST ); // The add_user() function will strip slashes.
     
    5458        $user    = get_user_to_edit( $user_id );
    5559
    56         $this->assertSame( $this->slash_1, $user->first_name );
    57         $this->assertSame( $this->slash_3, $user->last_name );
    58         $this->assertSame( $this->slash_5, $user->nickname );
    59         $this->assertSame( $this->slash_7, $user->display_name );
    60         $this->assertSame( $this->slash_3, $user->description );
     60        $this->assertSame( self::SLASH_1, $user->first_name );
     61        $this->assertSame( self::SLASH_3, $user->last_name );
     62        $this->assertSame( self::SLASH_5, $user->nickname );
     63        $this->assertSame( self::SLASH_7, $user->display_name );
     64        $this->assertSame( self::SLASH_3, $user->description );
    6165
    6266        $_POST                 = array();
     
    6872        $_POST['role']         = 'subscriber';
    6973        $_POST['email']        = 'user2@example.com';
    70         $_POST['first_name']   = $this->slash_2;
    71         $_POST['last_name']    = $this->slash_4;
    72         $_POST['nickname']     = $this->slash_6;
    73         $_POST['display_name'] = $this->slash_2;
    74         $_POST['description']  = $this->slash_4;
     74        $_POST['first_name']   = self::SLASH_2;
     75        $_POST['last_name']    = self::SLASH_4;
     76        $_POST['nickname']     = self::SLASH_6;
     77        $_POST['display_name'] = self::SLASH_2;
     78        $_POST['description']  = self::SLASH_4;
    7579
    7680        $_POST = add_magic_quotes( $_POST ); // The add_user() function will strip slashes.
     
    7983        $user    = get_user_to_edit( $user_id );
    8084
    81         $this->assertSame( $this->slash_2, $user->first_name );
    82         $this->assertSame( $this->slash_4, $user->last_name );
    83         $this->assertSame( $this->slash_6, $user->nickname );
    84         $this->assertSame( $this->slash_2, $user->display_name );
    85         $this->assertSame( $this->slash_4, $user->description );
     85        $this->assertSame( self::SLASH_2, $user->first_name );
     86        $this->assertSame( self::SLASH_4, $user->last_name );
     87        $this->assertSame( self::SLASH_6, $user->nickname );
     88        $this->assertSame( self::SLASH_2, $user->display_name );
     89        $this->assertSame( self::SLASH_4, $user->description );
    8690    }
    8791
     
    97101        $_POST['role']         = 'subscriber';
    98102        $_POST['email']        = 'user1@example.com';
    99         $_POST['first_name']   = $this->slash_1;
    100         $_POST['last_name']    = $this->slash_3;
    101         $_POST['nickname']     = $this->slash_5;
    102         $_POST['display_name'] = $this->slash_7;
    103         $_POST['description']  = $this->slash_3;
     103        $_POST['first_name']   = self::SLASH_1;
     104        $_POST['last_name']    = self::SLASH_3;
     105        $_POST['nickname']     = self::SLASH_5;
     106        $_POST['display_name'] = self::SLASH_7;
     107        $_POST['description']  = self::SLASH_3;
    104108
    105109        $_POST = add_magic_quotes( $_POST ); // The edit_user() function will strip slashes.
     
    108112        $user    = get_user_to_edit( $user_id );
    109113
    110         $this->assertSame( $this->slash_1, $user->first_name );
    111         $this->assertSame( $this->slash_3, $user->last_name );
    112         $this->assertSame( $this->slash_5, $user->nickname );
    113         $this->assertSame( $this->slash_7, $user->display_name );
    114         $this->assertSame( $this->slash_3, $user->description );
     114        $this->assertSame( self::SLASH_1, $user->first_name );
     115        $this->assertSame( self::SLASH_3, $user->last_name );
     116        $this->assertSame( self::SLASH_5, $user->nickname );
     117        $this->assertSame( self::SLASH_7, $user->display_name );
     118        $this->assertSame( self::SLASH_3, $user->description );
    115119
    116120        $_POST                 = array();
     
    119123        $_POST['role']         = 'subscriber';
    120124        $_POST['email']        = 'user2@example.com';
    121         $_POST['first_name']   = $this->slash_2;
    122         $_POST['last_name']    = $this->slash_4;
    123         $_POST['nickname']     = $this->slash_6;
    124         $_POST['display_name'] = $this->slash_2;
    125         $_POST['description']  = $this->slash_4;
     125        $_POST['first_name']   = self::SLASH_2;
     126        $_POST['last_name']    = self::SLASH_4;
     127        $_POST['nickname']     = self::SLASH_6;
     128        $_POST['display_name'] = self::SLASH_2;
     129        $_POST['description']  = self::SLASH_4;
    126130
    127131        $_POST = add_magic_quotes( $_POST ); // The edit_user() function will strip slashes.
     
    130134        $user    = get_user_to_edit( $user_id );
    131135
    132         $this->assertSame( $this->slash_2, $user->first_name );
    133         $this->assertSame( $this->slash_4, $user->last_name );
    134         $this->assertSame( $this->slash_6, $user->nickname );
    135         $this->assertSame( $this->slash_2, $user->display_name );
    136         $this->assertSame( $this->slash_4, $user->description );
     136        $this->assertSame( self::SLASH_2, $user->first_name );
     137        $this->assertSame( self::SLASH_4, $user->last_name );
     138        $this->assertSame( self::SLASH_6, $user->nickname );
     139        $this->assertSame( self::SLASH_2, $user->display_name );
     140        $this->assertSame( self::SLASH_4, $user->description );
    137141    }
    138142
     
    146150                'role'         => 'subscriber',
    147151                'email'        => 'user3@example.com',
    148                 'first_name'   => $this->slash_1,
    149                 'last_name'    => $this->slash_3,
    150                 'nickname'     => $this->slash_5,
    151                 'display_name' => $this->slash_7,
    152                 'description'  => $this->slash_3,
     152                'first_name'   => self::SLASH_1,
     153                'last_name'    => self::SLASH_3,
     154                'nickname'     => self::SLASH_5,
     155                'display_name' => self::SLASH_7,
     156                'description'  => self::SLASH_3,
    153157                'user_pass'    => '',
    154158            )
     
    156160        $user    = get_user_to_edit( $user_id );
    157161
    158         $this->assertSame( wp_unslash( $this->slash_1 ), $user->first_name );
    159         $this->assertSame( wp_unslash( $this->slash_3 ), $user->last_name );
    160         $this->assertSame( wp_unslash( $this->slash_5 ), $user->nickname );
    161         $this->assertSame( wp_unslash( $this->slash_7 ), $user->display_name );
    162         $this->assertSame( wp_unslash( $this->slash_3 ), $user->description );
     162        $this->assertSame( wp_unslash( self::SLASH_1 ), $user->first_name );
     163        $this->assertSame( wp_unslash( self::SLASH_3 ), $user->last_name );
     164        $this->assertSame( wp_unslash( self::SLASH_5 ), $user->nickname );
     165        $this->assertSame( wp_unslash( self::SLASH_7 ), $user->display_name );
     166        $this->assertSame( wp_unslash( self::SLASH_3 ), $user->description );
    163167
    164168        $user_id = wp_insert_user(
     
    167171                'role'         => 'subscriber',
    168172                'email'        => 'user3@example.com',
    169                 'first_name'   => $this->slash_2,
    170                 'last_name'    => $this->slash_4,
    171                 'nickname'     => $this->slash_6,
    172                 'display_name' => $this->slash_2,
    173                 'description'  => $this->slash_4,
     173                'first_name'   => self::SLASH_2,
     174                'last_name'    => self::SLASH_4,
     175                'nickname'     => self::SLASH_6,
     176                'display_name' => self::SLASH_2,
     177                'description'  => self::SLASH_4,
    174178                'user_pass'    => '',
    175179            )
     
    177181        $user    = get_user_to_edit( $user_id );
    178182
    179         $this->assertSame( wp_unslash( $this->slash_2 ), $user->first_name );
    180         $this->assertSame( wp_unslash( $this->slash_4 ), $user->last_name );
    181         $this->assertSame( wp_unslash( $this->slash_6 ), $user->nickname );
    182         $this->assertSame( wp_unslash( $this->slash_2 ), $user->display_name );
    183         $this->assertSame( wp_unslash( $this->slash_4 ), $user->description );
     183        $this->assertSame( wp_unslash( self::SLASH_2 ), $user->first_name );
     184        $this->assertSame( wp_unslash( self::SLASH_4 ), $user->last_name );
     185        $this->assertSame( wp_unslash( self::SLASH_6 ), $user->nickname );
     186        $this->assertSame( wp_unslash( self::SLASH_2 ), $user->display_name );
     187        $this->assertSame( wp_unslash( self::SLASH_4 ), $user->description );
    184188    }
    185189
     
    193197                'ID'           => $user_id,
    194198                'role'         => 'subscriber',
    195                 'first_name'   => $this->slash_1,
    196                 'last_name'    => $this->slash_3,
    197                 'nickname'     => $this->slash_5,
    198                 'display_name' => $this->slash_7,
    199                 'description'  => $this->slash_3,
    200             )
    201         );
    202         $user    = get_user_to_edit( $user_id );
    203 
    204         $this->assertSame( wp_unslash( $this->slash_1 ), $user->first_name );
    205         $this->assertSame( wp_unslash( $this->slash_3 ), $user->last_name );
    206         $this->assertSame( wp_unslash( $this->slash_5 ), $user->nickname );
    207         $this->assertSame( wp_unslash( $this->slash_7 ), $user->display_name );
    208         $this->assertSame( wp_unslash( $this->slash_3 ), $user->description );
     199                'first_name'   => self::SLASH_1,
     200                'last_name'    => self::SLASH_3,
     201                'nickname'     => self::SLASH_5,
     202                'display_name' => self::SLASH_7,
     203                'description'  => self::SLASH_3,
     204            )
     205        );
     206        $user    = get_user_to_edit( $user_id );
     207
     208        $this->assertSame( wp_unslash( self::SLASH_1 ), $user->first_name );
     209        $this->assertSame( wp_unslash( self::SLASH_3 ), $user->last_name );
     210        $this->assertSame( wp_unslash( self::SLASH_5 ), $user->nickname );
     211        $this->assertSame( wp_unslash( self::SLASH_7 ), $user->display_name );
     212        $this->assertSame( wp_unslash( self::SLASH_3 ), $user->description );
    209213
    210214        $user_id = wp_update_user(
     
    212216                'ID'           => $user_id,
    213217                'role'         => 'subscriber',
    214                 'first_name'   => $this->slash_2,
    215                 'last_name'    => $this->slash_4,
    216                 'nickname'     => $this->slash_6,
    217                 'display_name' => $this->slash_2,
    218                 'description'  => $this->slash_4,
    219             )
    220         );
    221         $user    = get_user_to_edit( $user_id );
    222 
    223         $this->assertSame( wp_unslash( $this->slash_2 ), $user->first_name );
    224         $this->assertSame( wp_unslash( $this->slash_4 ), $user->last_name );
    225         $this->assertSame( wp_unslash( $this->slash_6 ), $user->nickname );
    226         $this->assertSame( wp_unslash( $this->slash_2 ), $user->display_name );
    227         $this->assertSame( wp_unslash( $this->slash_4 ), $user->description );
     218                'first_name'   => self::SLASH_2,
     219                'last_name'    => self::SLASH_4,
     220                'nickname'     => self::SLASH_6,
     221                'display_name' => self::SLASH_2,
     222                'description'  => self::SLASH_4,
     223            )
     224        );
     225        $user    = get_user_to_edit( $user_id );
     226
     227        $this->assertSame( wp_unslash( self::SLASH_2 ), $user->first_name );
     228        $this->assertSame( wp_unslash( self::SLASH_4 ), $user->last_name );
     229        $this->assertSame( wp_unslash( self::SLASH_6 ), $user->nickname );
     230        $this->assertSame( wp_unslash( self::SLASH_2 ), $user->display_name );
     231        $this->assertSame( wp_unslash( self::SLASH_4 ), $user->description );
    228232    }
    229233
Note: See TracChangeset for help on using the changeset viewer.