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/meta/slashes.php

    r52010 r53557  
    77 */
    88class Tests_Meta_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 $editor_id;
    1024    protected static $post_id;
     
    2337
    2438        wp_set_current_user( self::$editor_id );
    25 
    26         $this->slash_1 = 'String with 1 slash \\';
    27         $this->slash_2 = 'String with 2 slashes \\\\';
    28         $this->slash_3 = 'String with 3 slashes \\\\\\';
    29         $this->slash_4 = 'String with 4 slashes \\\\\\\\';
    30         $this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
    31         $this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
    32         $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
    3339    }
    3440
     
    5460        $_POST['metakeyselect'] = '#NONE#';
    5561        $_POST['metakeyinput']  = 'slash_test_0';
    56         $_POST['metavalue']     = $this->slash_6;
     62        $_POST['metavalue']     = self::SLASH_6;
    5763        $_POST['meta']          = array(
    5864            $meta_1 => array(
    5965                'key'   => 'slash_test_1',
    60                 'value' => $this->slash_1,
     66                'value' => self::SLASH_1,
    6167            ),
    6268            $meta_2 => array(
    6369                'key'   => 'slash_test_2',
    64                 'value' => $this->slash_3,
     70                'value' => self::SLASH_3,
    6571            ),
    6672            $meta_3 => array(
    6773                'key'   => 'slash_test_3',
    68                 'value' => $this->slash_4,
     74                'value' => self::SLASH_4,
    6975            ),
    7076        );
     
    7581        $post = get_post( $post_id );
    7682
    77         $this->assertSame( $this->slash_6, get_post_meta( $post_id, 'slash_test_0', true ) );
    78         $this->assertSame( $this->slash_1, get_post_meta( $post_id, 'slash_test_1', true ) );
    79         $this->assertSame( $this->slash_3, get_post_meta( $post_id, 'slash_test_2', true ) );
    80         $this->assertSame( $this->slash_4, get_post_meta( $post_id, 'slash_test_3', true ) );
     83        $this->assertSame( self::SLASH_6, get_post_meta( $post_id, 'slash_test_0', true ) );
     84        $this->assertSame( self::SLASH_1, get_post_meta( $post_id, 'slash_test_1', true ) );
     85        $this->assertSame( self::SLASH_3, get_post_meta( $post_id, 'slash_test_2', true ) );
     86        $this->assertSame( self::SLASH_4, get_post_meta( $post_id, 'slash_test_3', true ) );
    8187
    8288        $_POST                  = array();
     
    8490        $_POST['metakeyselect'] = '#NONE#';
    8591        $_POST['metakeyinput']  = 'slash_test_0';
    86         $_POST['metavalue']     = $this->slash_7;
     92        $_POST['metavalue']     = self::SLASH_7;
    8793        $_POST['meta']          = array(
    8894            $meta_1 => array(
    8995                'key'   => 'slash_test_1',
    90                 'value' => $this->slash_2,
     96                'value' => self::SLASH_2,
    9197            ),
    9298            $meta_2 => array(
    9399                'key'   => 'slash_test_2',
    94                 'value' => $this->slash_4,
     100                'value' => self::SLASH_4,
    95101            ),
    96102            $meta_3 => array(
    97103                'key'   => 'slash_test_3',
    98                 'value' => $this->slash_5,
     104                'value' => self::SLASH_5,
    99105            ),
    100106        );
     
    105111        $post = get_post( $post_id );
    106112
    107         $this->assertSame( $this->slash_2, get_post_meta( $post_id, 'slash_test_1', true ) );
    108         $this->assertSame( $this->slash_4, get_post_meta( $post_id, 'slash_test_2', true ) );
    109         $this->assertSame( $this->slash_5, get_post_meta( $post_id, 'slash_test_3', true ) );
     113        $this->assertSame( self::SLASH_2, get_post_meta( $post_id, 'slash_test_1', true ) );
     114        $this->assertSame( self::SLASH_4, get_post_meta( $post_id, 'slash_test_2', true ) );
     115        $this->assertSame( self::SLASH_5, get_post_meta( $post_id, 'slash_test_3', true ) );
    110116    }
    111117
     
    116122        $post_id = self::$post_id;
    117123
    118         add_post_meta( $post_id, 'slash_test_1', addslashes( $this->slash_1 ) );
    119         add_post_meta( $post_id, 'slash_test_2', addslashes( $this->slash_3 ) );
    120         add_post_meta( $post_id, 'slash_test_3', addslashes( $this->slash_4 ) );
    121 
    122         $this->assertSame( $this->slash_1, get_post_meta( $post_id, 'slash_test_1', true ) );
    123         $this->assertSame( $this->slash_3, get_post_meta( $post_id, 'slash_test_2', true ) );
    124         $this->assertSame( $this->slash_4, get_post_meta( $post_id, 'slash_test_3', true ) );
     124        add_post_meta( $post_id, 'slash_test_1', addslashes( self::SLASH_1 ) );
     125        add_post_meta( $post_id, 'slash_test_2', addslashes( self::SLASH_3 ) );
     126        add_post_meta( $post_id, 'slash_test_3', addslashes( self::SLASH_4 ) );
     127
     128        $this->assertSame( self::SLASH_1, get_post_meta( $post_id, 'slash_test_1', true ) );
     129        $this->assertSame( self::SLASH_3, get_post_meta( $post_id, 'slash_test_2', true ) );
     130        $this->assertSame( self::SLASH_4, get_post_meta( $post_id, 'slash_test_3', true ) );
    125131    }
    126132
     
    131137        $post_id = self::$post_id;
    132138
    133         update_post_meta( $post_id, 'slash_test_1', addslashes( $this->slash_1 ) );
    134         update_post_meta( $post_id, 'slash_test_2', addslashes( $this->slash_3 ) );
    135         update_post_meta( $post_id, 'slash_test_3', addslashes( $this->slash_4 ) );
    136 
    137         $this->assertSame( $this->slash_1, get_post_meta( $post_id, 'slash_test_1', true ) );
    138         $this->assertSame( $this->slash_3, get_post_meta( $post_id, 'slash_test_2', true ) );
    139         $this->assertSame( $this->slash_4, get_post_meta( $post_id, 'slash_test_3', true ) );
     139        update_post_meta( $post_id, 'slash_test_1', addslashes( self::SLASH_1 ) );
     140        update_post_meta( $post_id, 'slash_test_2', addslashes( self::SLASH_3 ) );
     141        update_post_meta( $post_id, 'slash_test_3', addslashes( self::SLASH_4 ) );
     142
     143        $this->assertSame( self::SLASH_1, get_post_meta( $post_id, 'slash_test_1', true ) );
     144        $this->assertSame( self::SLASH_3, get_post_meta( $post_id, 'slash_test_2', true ) );
     145        $this->assertSame( self::SLASH_4, get_post_meta( $post_id, 'slash_test_3', true ) );
    140146    }
    141147
     
    146152        $comment_id = self::$comment_id;
    147153
    148         add_comment_meta( $comment_id, 'slash_test_1', $this->slash_1 );
    149         add_comment_meta( $comment_id, 'slash_test_2', $this->slash_3 );
    150         add_comment_meta( $comment_id, 'slash_test_3', $this->slash_5 );
    151 
    152         $this->assertSame( wp_unslash( $this->slash_1 ), get_comment_meta( $comment_id, 'slash_test_1', true ) );
    153         $this->assertSame( wp_unslash( $this->slash_3 ), get_comment_meta( $comment_id, 'slash_test_2', true ) );
    154         $this->assertSame( wp_unslash( $this->slash_5 ), get_comment_meta( $comment_id, 'slash_test_3', true ) );
    155 
    156         add_comment_meta( $comment_id, 'slash_test_4', $this->slash_2 );
    157         add_comment_meta( $comment_id, 'slash_test_5', $this->slash_4 );
    158         add_comment_meta( $comment_id, 'slash_test_6', $this->slash_6 );
    159 
    160         $this->assertSame( wp_unslash( $this->slash_2 ), get_comment_meta( $comment_id, 'slash_test_4', true ) );
    161         $this->assertSame( wp_unslash( $this->slash_4 ), get_comment_meta( $comment_id, 'slash_test_5', true ) );
    162         $this->assertSame( wp_unslash( $this->slash_6 ), get_comment_meta( $comment_id, 'slash_test_6', true ) );
     154        add_comment_meta( $comment_id, 'slash_test_1', self::SLASH_1 );
     155        add_comment_meta( $comment_id, 'slash_test_2', self::SLASH_3 );
     156        add_comment_meta( $comment_id, 'slash_test_3', self::SLASH_5 );
     157
     158        $this->assertSame( wp_unslash( self::SLASH_1 ), get_comment_meta( $comment_id, 'slash_test_1', true ) );
     159        $this->assertSame( wp_unslash( self::SLASH_3 ), get_comment_meta( $comment_id, 'slash_test_2', true ) );
     160        $this->assertSame( wp_unslash( self::SLASH_5 ), get_comment_meta( $comment_id, 'slash_test_3', true ) );
     161
     162        add_comment_meta( $comment_id, 'slash_test_4', self::SLASH_2 );
     163        add_comment_meta( $comment_id, 'slash_test_5', self::SLASH_4 );
     164        add_comment_meta( $comment_id, 'slash_test_6', self::SLASH_6 );
     165
     166        $this->assertSame( wp_unslash( self::SLASH_2 ), get_comment_meta( $comment_id, 'slash_test_4', true ) );
     167        $this->assertSame( wp_unslash( self::SLASH_4 ), get_comment_meta( $comment_id, 'slash_test_5', true ) );
     168        $this->assertSame( wp_unslash( self::SLASH_6 ), get_comment_meta( $comment_id, 'slash_test_6', true ) );
    163169    }
    164170
     
    173179        add_comment_meta( $comment_id, 'slash_test_3', 'foo' );
    174180
    175         update_comment_meta( $comment_id, 'slash_test_1', $this->slash_1 );
    176         update_comment_meta( $comment_id, 'slash_test_2', $this->slash_3 );
    177         update_comment_meta( $comment_id, 'slash_test_3', $this->slash_5 );
    178 
    179         $this->assertSame( wp_unslash( $this->slash_1 ), get_comment_meta( $comment_id, 'slash_test_1', true ) );
    180         $this->assertSame( wp_unslash( $this->slash_3 ), get_comment_meta( $comment_id, 'slash_test_2', true ) );
    181         $this->assertSame( wp_unslash( $this->slash_5 ), get_comment_meta( $comment_id, 'slash_test_3', true ) );
    182 
    183         update_comment_meta( $comment_id, 'slash_test_1', $this->slash_2 );
    184         update_comment_meta( $comment_id, 'slash_test_2', $this->slash_4 );
    185         update_comment_meta( $comment_id, 'slash_test_3', $this->slash_6 );
    186 
    187         $this->assertSame( wp_unslash( $this->slash_2 ), get_comment_meta( $comment_id, 'slash_test_1', true ) );
    188         $this->assertSame( wp_unslash( $this->slash_4 ), get_comment_meta( $comment_id, 'slash_test_2', true ) );
    189         $this->assertSame( wp_unslash( $this->slash_6 ), get_comment_meta( $comment_id, 'slash_test_3', true ) );
     181        update_comment_meta( $comment_id, 'slash_test_1', self::SLASH_1 );
     182        update_comment_meta( $comment_id, 'slash_test_2', self::SLASH_3 );
     183        update_comment_meta( $comment_id, 'slash_test_3', self::SLASH_5 );
     184
     185        $this->assertSame( wp_unslash( self::SLASH_1 ), get_comment_meta( $comment_id, 'slash_test_1', true ) );
     186        $this->assertSame( wp_unslash( self::SLASH_3 ), get_comment_meta( $comment_id, 'slash_test_2', true ) );
     187        $this->assertSame( wp_unslash( self::SLASH_5 ), get_comment_meta( $comment_id, 'slash_test_3', true ) );
     188
     189        update_comment_meta( $comment_id, 'slash_test_1', self::SLASH_2 );
     190        update_comment_meta( $comment_id, 'slash_test_2', self::SLASH_4 );
     191        update_comment_meta( $comment_id, 'slash_test_3', self::SLASH_6 );
     192
     193        $this->assertSame( wp_unslash( self::SLASH_2 ), get_comment_meta( $comment_id, 'slash_test_1', true ) );
     194        $this->assertSame( wp_unslash( self::SLASH_4 ), get_comment_meta( $comment_id, 'slash_test_2', true ) );
     195        $this->assertSame( wp_unslash( self::SLASH_6 ), get_comment_meta( $comment_id, 'slash_test_3', true ) );
    190196    }
    191197
     
    196202        $user_id = self::$user_id;
    197203
    198         add_user_meta( $user_id, 'slash_test_1', $this->slash_1 );
    199         add_user_meta( $user_id, 'slash_test_2', $this->slash_3 );
    200         add_user_meta( $user_id, 'slash_test_3', $this->slash_5 );
    201 
    202         $this->assertSame( wp_unslash( $this->slash_1 ), get_user_meta( $user_id, 'slash_test_1', true ) );
    203         $this->assertSame( wp_unslash( $this->slash_3 ), get_user_meta( $user_id, 'slash_test_2', true ) );
    204         $this->assertSame( wp_unslash( $this->slash_5 ), get_user_meta( $user_id, 'slash_test_3', true ) );
    205 
    206         add_user_meta( $user_id, 'slash_test_4', $this->slash_2 );
    207         add_user_meta( $user_id, 'slash_test_5', $this->slash_4 );
    208         add_user_meta( $user_id, 'slash_test_6', $this->slash_6 );
    209 
    210         $this->assertSame( wp_unslash( $this->slash_2 ), get_user_meta( $user_id, 'slash_test_4', true ) );
    211         $this->assertSame( wp_unslash( $this->slash_4 ), get_user_meta( $user_id, 'slash_test_5', true ) );
    212         $this->assertSame( wp_unslash( $this->slash_6 ), get_user_meta( $user_id, 'slash_test_6', true ) );
     204        add_user_meta( $user_id, 'slash_test_1', self::SLASH_1 );
     205        add_user_meta( $user_id, 'slash_test_2', self::SLASH_3 );
     206        add_user_meta( $user_id, 'slash_test_3', self::SLASH_5 );
     207
     208        $this->assertSame( wp_unslash( self::SLASH_1 ), get_user_meta( $user_id, 'slash_test_1', true ) );
     209        $this->assertSame( wp_unslash( self::SLASH_3 ), get_user_meta( $user_id, 'slash_test_2', true ) );
     210        $this->assertSame( wp_unslash( self::SLASH_5 ), get_user_meta( $user_id, 'slash_test_3', true ) );
     211
     212        add_user_meta( $user_id, 'slash_test_4', self::SLASH_2 );
     213        add_user_meta( $user_id, 'slash_test_5', self::SLASH_4 );
     214        add_user_meta( $user_id, 'slash_test_6', self::SLASH_6 );
     215
     216        $this->assertSame( wp_unslash( self::SLASH_2 ), get_user_meta( $user_id, 'slash_test_4', true ) );
     217        $this->assertSame( wp_unslash( self::SLASH_4 ), get_user_meta( $user_id, 'slash_test_5', true ) );
     218        $this->assertSame( wp_unslash( self::SLASH_6 ), get_user_meta( $user_id, 'slash_test_6', true ) );
    213219    }
    214220
     
    223229        add_user_meta( $user_id, 'slash_test_3', 'foo' );
    224230
    225         update_user_meta( $user_id, 'slash_test_1', $this->slash_1 );
    226         update_user_meta( $user_id, 'slash_test_2', $this->slash_3 );
    227         update_user_meta( $user_id, 'slash_test_3', $this->slash_5 );
    228 
    229         $this->assertSame( wp_unslash( $this->slash_1 ), get_user_meta( $user_id, 'slash_test_1', true ) );
    230         $this->assertSame( wp_unslash( $this->slash_3 ), get_user_meta( $user_id, 'slash_test_2', true ) );
    231         $this->assertSame( wp_unslash( $this->slash_5 ), get_user_meta( $user_id, 'slash_test_3', true ) );
    232 
    233         update_user_meta( $user_id, 'slash_test_1', $this->slash_2 );
    234         update_user_meta( $user_id, 'slash_test_2', $this->slash_4 );
    235         update_user_meta( $user_id, 'slash_test_3', $this->slash_6 );
    236 
    237         $this->assertSame( wp_unslash( $this->slash_2 ), get_user_meta( $user_id, 'slash_test_1', true ) );
    238         $this->assertSame( wp_unslash( $this->slash_4 ), get_user_meta( $user_id, 'slash_test_2', true ) );
    239         $this->assertSame( wp_unslash( $this->slash_6 ), get_user_meta( $user_id, 'slash_test_3', true ) );
     231        update_user_meta( $user_id, 'slash_test_1', self::SLASH_1 );
     232        update_user_meta( $user_id, 'slash_test_2', self::SLASH_3 );
     233        update_user_meta( $user_id, 'slash_test_3', self::SLASH_5 );
     234
     235        $this->assertSame( wp_unslash( self::SLASH_1 ), get_user_meta( $user_id, 'slash_test_1', true ) );
     236        $this->assertSame( wp_unslash( self::SLASH_3 ), get_user_meta( $user_id, 'slash_test_2', true ) );
     237        $this->assertSame( wp_unslash( self::SLASH_5 ), get_user_meta( $user_id, 'slash_test_3', true ) );
     238
     239        update_user_meta( $user_id, 'slash_test_1', self::SLASH_2 );
     240        update_user_meta( $user_id, 'slash_test_2', self::SLASH_4 );
     241        update_user_meta( $user_id, 'slash_test_3', self::SLASH_6 );
     242
     243        $this->assertSame( wp_unslash( self::SLASH_2 ), get_user_meta( $user_id, 'slash_test_1', true ) );
     244        $this->assertSame( wp_unslash( self::SLASH_4 ), get_user_meta( $user_id, 'slash_test_2', true ) );
     245        $this->assertSame( wp_unslash( self::SLASH_6 ), get_user_meta( $user_id, 'slash_test_3', true ) );
    240246    }
    241247}
Note: See TracChangeset for help on using the changeset viewer.