Make WordPress Core

Changeset 53942


Ignore:
Timestamp:
08/25/2022 03:34:24 PM (22 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Explicitly declare all properties in various tests.

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 each of the cases included in this commit, one or more individual tests set a property to allow a filter or action access to certain information.

This commit:

  • Explicitly declares these properties and documents in which tests they are being used.
  • Adds a reset to the default value of the property to a pre-existing tear_down() method or adds that method specifically for that purpose. This ensures that tests do not accidentally “taint” each other.

As these properties are being declared on test classes, they are marked as private. Even though the original dynamic property was public, this should not be considered a backward compatibility break as this only involves test classes.

Includes:

  • In the Tests_Post_Query class, there were two tests assigning a value to an undeclared $post_id property, but subsequently not using the property, so those assignments should have been to a local variable (if they should be assignments at all).
  • In the Test_User_Capabilities class, the property name had a leading _ underscore. This is an outdated PHP 4 practice to indicate a private property. As PHP 4 is no longer supported, the leading underscore is removed from the property name.
  • In the Tests_User_Capabilities class, an unused $_role_test_wp_roles_role property was declared somewhere in the middle of the class. That property is now removed in favor of $_role_test_wp_roles_init, which appears to be the intended name, previously misspelled.

Follow-up to [27294], [36277], [36750], [37712], [38571], [39082], [40290], [43049], [44628], [48328], [53557], [53558], [53850], [53851], [53852], [53853], [53854], [53856], [53916], [53935], [53936], [53937], [53938].

Props jrf.
See #56033.

Location:
trunk/tests/phpunit/tests
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/actions.php

    r53808 r53942  
    77 */
    88class Tests_Actions extends WP_UnitTestCase {
     9
     10    /**
     11     * Flag to keep track whether a certain filter has been applied.
     12     *
     13     * Used in the `test_doing_filter_real()` test method.
     14     *
     15     * @var bool
     16     */
     17    private $apply_testing_filter = false;
     18
     19    /**
     20     * Flag to keep track whether a certain filter has been applied.
     21     *
     22     * Used in the `test_doing_filter_real()` test method.
     23     *
     24     * @var bool
     25     */
     26    private $apply_testing_nested_filter = false;
     27
     28    /**
     29     * Clean up after each test.
     30     */
     31    public function tear_down() {
     32        // Make sure potentially changed properties are reverted to their default value.
     33        $this->apply_testing_filter        = false;
     34        $this->apply_testing_nested_filter = false;
     35
     36        parent::tear_down();
     37    }
    938
    1039    /**
  • trunk/tests/phpunit/tests/admin/wpPrivacyRequestsTable.php

    r51462 r53942  
    1717 */
    1818class Tests_Admin_wpPrivacyRequestsTable extends WP_UnitTestCase {
     19
     20    /**
     21     * Temporary storage for SQL to allow a filter to access it.
     22     *
     23     * Used in the `test_columns_should_be_sortable()` test method.
     24     *
     25     * @var string
     26     */
     27    private $sql;
     28
     29    /**
     30     * Clean up after each test.
     31     */
     32    public function tear_down() {
     33        unset( $this->sql );
     34
     35        parent::tear_down();
     36    }
     37
    1938    /**
    2039     * Get instance for mocked class.
  • trunk/tests/phpunit/tests/comment/query.php

    r53863 r53942  
    1010    protected $comment_id;
    1111
     12    /**
     13     * Temporary storage for comment exclusions to allow a filter to access these.
     14     *
     15     * Used in the following tests:
     16     * - `test_comment_clauses_prepend_callback_should_be_respected_when_filling_descendants()`
     17     * - `test_comment_clauses_append_callback_should_be_respected_when_filling_descendants()`
     18     *
     19     * @var array
     20     */
     21    private $to_exclude;
     22
    1223    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    1324        self::$post_id = $factory->post->create();
     25    }
     26
     27    public function tear_down() {
     28        unset( $this->to_exclude );
     29        parent::tear_down();
    1430    }
    1531
     
    43024318        remove_filter( 'comments_clauses', array( $this, 'prepend_exclusions' ) );
    43034319
    4304         unset( $this->to_exclude );
    4305 
    43064320        $this->assertEqualSets( array( $top_level_0, $child1_of_0, $top_level_comments[0], $top_level_comments[2] ), wp_list_pluck( $q->comments, 'comment_ID' ) );
    43074321    }
     
    43604374        );
    43614375        remove_filter( 'comments_clauses', array( $this, 'append_exclusions' ) );
    4362 
    4363         unset( $this->to_exclude );
    43644376
    43654377        $this->assertEqualSets( array( $top_level_0, $child1_of_0, $top_level_comments[0], $top_level_comments[2] ), wp_list_pluck( $q->comments, 'comment_ID' ) );
  • trunk/tests/phpunit/tests/hooks/addFilter.php

    r53804 r53942  
    1212    public $hook;
    1313
     14    /**
     15     * Temporary storage for action output.
     16     *
     17     * Used in the following tests:
     18     * - `test_remove_and_add_action()`
     19     * - `test_remove_and_add_last_action()`
     20     * - `test_remove_and_recurse_and_add_action()`
     21     *
     22     * @var array
     23     */
     24    private $action_output = '';
     25
     26    public function tear_down() {
     27        $this->action_output = '';
     28        parent::tear_down();
     29    }
     30
    1431    public function test_add_filter_with_function() {
    1532        $callback      = '__return_null';
     
    203220
    204221    public function test_remove_and_add_action() {
    205         $this->hook          = new WP_Hook();
    206         $this->action_output = '';
     222        $this->hook = new WP_Hook();
    207223
    208224        $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );
     
    218234
    219235    public function test_remove_and_add_last_action() {
    220         $this->hook          = new WP_Hook();
    221         $this->action_output = '';
     236        $this->hook = new WP_Hook();
    222237
    223238        $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );
     
    233248
    234249    public function test_remove_and_recurse_and_add_action() {
    235         $this->hook          = new WP_Hook();
    236         $this->action_output = '';
     250        $this->hook = new WP_Hook();
    237251
    238252        $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );
  • trunk/tests/phpunit/tests/post/query.php

    r52389 r53942  
    66 */
    77class Tests_Post_Query extends WP_UnitTestCase {
     8
     9    /**
     10     * Temporary storage for a post ID for tests using filter callbacks.
     11     *
     12     * Used in the `test_posts_pre_query_filter_should_respect_set_found_posts()` method.
     13     *
     14     * @var int
     15     */
     16    private $post_id;
     17
     18    /**
     19     * Clean up after each test.
     20     */
     21    public function tear_down() {
     22        unset( $this->post_id );
     23
     24        parent::tear_down();
     25    }
     26
    827    /**
    928     * @group taxonomy
     
    730749     */
    731750    public function test_found_posts_should_be_integer_not_string() {
    732         $this->post_id = self::factory()->post->create();
     751        $post_id = self::factory()->post->create();
    733752
    734753        $q = new WP_Query(
     
    745764     */
    746765    public function test_found_posts_should_be_integer_even_if_found_posts_filter_returns_string_value() {
    747         $this->post_id = self::factory()->post->create();
     766        $post_id = self::factory()->post->create();
    748767
    749768        add_filter( 'found_posts', '__return_empty_string' );
  • trunk/tests/phpunit/tests/rewrite.php

    r52389 r53942  
    99    private $home_url;
    1010
     11    /**
     12     * Temporary storage for blog id for use with filters.
     13     *
     14     * Used in the `test_url_to_postid_of_http_site_when_current_site_uses_https()` method.
     15     *
     16     * @var int
     17     */
     18    private $blog_id_35531;
     19
    1120    public function set_up() {
    1221        parent::set_up();
     
    2332
    2433        update_option( 'home', $this->home_url );
     34        unset( $this->blog_id_35531 );
    2535        parent::tear_down();
    2636    }
  • trunk/tests/phpunit/tests/term/query.php

    r51462 r53942  
    55 */
    66class Tests_Term_Query extends WP_UnitTestCase {
     7
     8    /**
     9     * Temporary storage for a term ID for tests using filter callbacks.
     10     *
     11     * Used in the following tests:
     12     * - `test_null_term_object_should_be_discarded()`
     13     * - `test_error_term_object_should_be_discarded()`
     14     *
     15     * @var int
     16     */
     17    private $term_id;
     18
     19    /**
     20     * Clean up after each test.
     21     */
     22    public function tear_down() {
     23        unset( $this->term_id );
     24
     25        parent::tear_down();
     26    }
     27
    728    /**
    829     * @ticket 37545
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r52921 r53942  
    88    private $taxonomy = 'wptests_tax';
    99
     10    /**
     11     * Temporary storage for taxonomies for tests using filter callbacks.
     12     *
     13     * Used in the `test_taxonomies_passed_to_wp_get_object_terms_filter_should_be_quoted()` method.
     14     *
     15     * @var array
     16     */
     17    private $taxonomies;
     18
    1019    public function set_up() {
    1120        parent::set_up();
    1221        register_taxonomy( 'wptests_tax', 'post' );
     22    }
     23
     24    /**
     25     * Clean up after each test.
     26     */
     27    public function tear_down() {
     28        unset( $this->taxonomies );
     29
     30        parent::tear_down();
    1331    }
    1432
  • trunk/tests/phpunit/tests/user/capabilities.php

    r53408 r53942  
    3131    protected static $block_id;
    3232
     33    /**
     34     * Temporary storage for roles for tests using filter callbacks.
     35     *
     36     * Used in the `test_wp_roles_init_action()` method.
     37     *
     38     * @var array
     39     */
     40    private $role_test_wp_roles_init;
     41
    3342    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    3443        self::$users       = array(
     
    5968        $this->flush_roles();
    6069
     70    }
     71
     72    /**
     73     * Clean up after each test.
     74     */
     75    public function tear_down() {
     76        unset( $this->role_test_wp_roles_init );
     77
     78        parent::tear_down();
    6179    }
    6280
     
    19972015    }
    19982016
    1999 
    2000     protected $_role_test_wp_roles_role;
    20012017    /**
    20022018     * @ticket 23016
    20032019     */
    20042020    public function test_wp_roles_init_action() {
    2005         $this->_role_test_wp_roles_init = array(
     2021        $this->role_test_wp_roles_init = array(
    20062022            'role' => 'test_wp_roles_init',
    20072023            'info' => array(
     
    20162032        remove_action( 'wp_roles_init', array( $this, '_hook_wp_roles_init' ) );
    20172033
    2018         $expected = new WP_Role( $this->_role_test_wp_roles_init['role'], $this->_role_test_wp_roles_init['info']['capabilities'] );
    2019 
    2020         $role = $wp_roles->get_role( $this->_role_test_wp_roles_init['role'] );
     2034        $expected = new WP_Role( $this->role_test_wp_roles_init['role'], $this->role_test_wp_roles_init['info']['capabilities'] );
     2035
     2036        $role = $wp_roles->get_role( $this->role_test_wp_roles_init['role'] );
    20212037
    20222038        $this->assertEquals( $expected, $role );
    2023         $this->assertContains( $this->_role_test_wp_roles_init['info']['name'], $wp_roles->role_names );
     2039        $this->assertContains( $this->role_test_wp_roles_init['info']['name'], $wp_roles->role_names );
    20242040    }
    20252041
    20262042    public function _hook_wp_roles_init( $wp_roles ) {
    2027         $wp_roles->add_role( $this->_role_test_wp_roles_init['role'], $this->_role_test_wp_roles_init['info']['name'], $this->_role_test_wp_roles_init['info']['capabilities'] );
     2043        $wp_roles->add_role( $this->role_test_wp_roles_init['role'], $this->role_test_wp_roles_init['info']['name'], $this->role_test_wp_roles_init['info']['capabilities'] );
    20282044    }
    20292045
Note: See TracChangeset for help on using the changeset viewer.