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

    r52010 r53557  
    77 */
    88class Tests_Post_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;
     
    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
     
    3943        $_POST               = array();
    4044        $_POST['post_ID']    = $post_id;
    41         $_POST['post_title'] = $this->slash_1;
    42         $_POST['content']    = $this->slash_5;
    43         $_POST['excerpt']    = $this->slash_7;
     45        $_POST['post_title'] = self::SLASH_1;
     46        $_POST['content']    = self::SLASH_5;
     47        $_POST['excerpt']    = self::SLASH_7;
    4448
    4549        $_POST = add_magic_quotes( $_POST ); // The edit_post() function will strip slashes.
     
    4852        $post    = get_post( $post_id );
    4953
    50         $this->assertSame( $this->slash_1, $post->post_title );
    51         $this->assertSame( $this->slash_5, $post->post_content );
    52         $this->assertSame( $this->slash_7, $post->post_excerpt );
     54        $this->assertSame( self::SLASH_1, $post->post_title );
     55        $this->assertSame( self::SLASH_5, $post->post_content );
     56        $this->assertSame( self::SLASH_7, $post->post_excerpt );
    5357
    5458        $_POST               = array();
    5559        $_POST['post_ID']    = $post_id;
    56         $_POST['post_title'] = $this->slash_2;
    57         $_POST['content']    = $this->slash_4;
    58         $_POST['excerpt']    = $this->slash_6;
     60        $_POST['post_title'] = self::SLASH_2;
     61        $_POST['content']    = self::SLASH_4;
     62        $_POST['excerpt']    = self::SLASH_6;
    5963
    6064        $_POST = add_magic_quotes( $_POST ); // The edit_post() function will strip slashes.
     
    6367        $post    = get_post( $post_id );
    6468
    65         $this->assertSame( $this->slash_2, $post->post_title );
    66         $this->assertSame( $this->slash_4, $post->post_content );
    67         $this->assertSame( $this->slash_6, $post->post_excerpt );
     69        $this->assertSame( self::SLASH_2, $post->post_title );
     70        $this->assertSame( self::SLASH_4, $post->post_content );
     71        $this->assertSame( self::SLASH_6, $post->post_excerpt );
    6872    }
    6973
     
    7579            array(
    7680                'post_status'  => 'publish',
    77                 'post_title'   => $this->slash_1,
    78                 'post_content' => $this->slash_3,
    79                 'post_excerpt' => $this->slash_5,
     81                'post_title'   => self::SLASH_1,
     82                'post_content' => self::SLASH_3,
     83                'post_excerpt' => self::SLASH_5,
    8084                'post_type'    => 'post',
    8185                'slashed'      => false,
     
    8488        $post    = get_post( $post_id );
    8589
    86         $this->assertSame( wp_unslash( $this->slash_1 ), $post->post_title );
    87         $this->assertSame( wp_unslash( $this->slash_3 ), $post->post_content );
    88         $this->assertSame( wp_unslash( $this->slash_5 ), $post->post_excerpt );
     90        $this->assertSame( wp_unslash( self::SLASH_1 ), $post->post_title );
     91        $this->assertSame( wp_unslash( self::SLASH_3 ), $post->post_content );
     92        $this->assertSame( wp_unslash( self::SLASH_5 ), $post->post_excerpt );
    8993
    9094        $post_id = wp_insert_post(
    9195            array(
    9296                'post_status'  => 'publish',
    93                 'post_title'   => $this->slash_2,
    94                 'post_content' => $this->slash_4,
    95                 'post_excerpt' => $this->slash_6,
     97                'post_title'   => self::SLASH_2,
     98                'post_content' => self::SLASH_4,
     99                'post_excerpt' => self::SLASH_6,
    96100                'post_type'    => 'post',
    97101            )
     
    99103        $post    = get_post( $post_id );
    100104
    101         $this->assertSame( wp_unslash( $this->slash_2 ), $post->post_title );
    102         $this->assertSame( wp_unslash( $this->slash_4 ), $post->post_content );
    103         $this->assertSame( wp_unslash( $this->slash_6 ), $post->post_excerpt );
     105        $this->assertSame( wp_unslash( self::SLASH_2 ), $post->post_title );
     106        $this->assertSame( wp_unslash( self::SLASH_4 ), $post->post_content );
     107        $this->assertSame( wp_unslash( self::SLASH_6 ), $post->post_excerpt );
    104108    }
    105109
     
    113117            array(
    114118                'ID'           => $post_id,
    115                 'post_title'   => $this->slash_1,
    116                 'post_content' => $this->slash_3,
    117                 'post_excerpt' => $this->slash_5,
     119                'post_title'   => self::SLASH_1,
     120                'post_content' => self::SLASH_3,
     121                'post_excerpt' => self::SLASH_5,
    118122            )
    119123        );
    120124        $post = get_post( $post_id );
    121125
    122         $this->assertSame( wp_unslash( $this->slash_1 ), $post->post_title );
    123         $this->assertSame( wp_unslash( $this->slash_3 ), $post->post_content );
    124         $this->assertSame( wp_unslash( $this->slash_5 ), $post->post_excerpt );
     126        $this->assertSame( wp_unslash( self::SLASH_1 ), $post->post_title );
     127        $this->assertSame( wp_unslash( self::SLASH_3 ), $post->post_content );
     128        $this->assertSame( wp_unslash( self::SLASH_5 ), $post->post_excerpt );
    125129
    126130        wp_update_post(
    127131            array(
    128132                'ID'           => $post_id,
    129                 'post_title'   => $this->slash_2,
    130                 'post_content' => $this->slash_4,
    131                 'post_excerpt' => $this->slash_6,
     133                'post_title'   => self::SLASH_2,
     134                'post_content' => self::SLASH_4,
     135                'post_excerpt' => self::SLASH_6,
    132136            )
    133137        );
    134138        $post = get_post( $post_id );
    135139
    136         $this->assertSame( wp_unslash( $this->slash_2 ), $post->post_title );
    137         $this->assertSame( wp_unslash( $this->slash_4 ), $post->post_content );
    138         $this->assertSame( wp_unslash( $this->slash_6 ), $post->post_excerpt );
     140        $this->assertSame( wp_unslash( self::SLASH_2 ), $post->post_title );
     141        $this->assertSame( wp_unslash( self::SLASH_4 ), $post->post_content );
     142        $this->assertSame( wp_unslash( self::SLASH_6 ), $post->post_excerpt );
    139143    }
    140144
     
    144148    public function test_wp_trash_untrash() {
    145149        $post    = array(
    146             'post_title'   => $this->slash_1,
    147             'post_content' => $this->slash_3,
    148             'post_excerpt' => $this->slash_5,
     150            'post_title'   => self::SLASH_1,
     151            'post_content' => self::SLASH_3,
     152            'post_excerpt' => self::SLASH_5,
    149153        );
    150154        $post_id = wp_insert_post( wp_slash( $post ) );
     
    155159        $post = get_post( $post_id );
    156160
    157         $this->assertSame( $this->slash_1, $post->post_title );
    158         $this->assertSame( $this->slash_3, $post->post_content );
    159         $this->assertSame( $this->slash_5, $post->post_excerpt );
     161        $this->assertSame( self::SLASH_1, $post->post_title );
     162        $this->assertSame( self::SLASH_3, $post->post_content );
     163        $this->assertSame( self::SLASH_5, $post->post_excerpt );
    160164
    161165        $untrashed = wp_untrash_post( $post_id );
     
    164168        $post = get_post( $post_id );
    165169
    166         $this->assertSame( $this->slash_1, $post->post_title );
    167         $this->assertSame( $this->slash_3, $post->post_content );
    168         $this->assertSame( $this->slash_5, $post->post_excerpt );
     170        $this->assertSame( self::SLASH_1, $post->post_title );
     171        $this->assertSame( self::SLASH_3, $post->post_content );
     172        $this->assertSame( self::SLASH_5, $post->post_excerpt );
    169173    }
    170174}
Note: See TracChangeset for help on using the changeset viewer.