Make WordPress Core

Changeset 63608


Ignore:
Timestamp:
09/13/2026 04:21:34 PM (7 hours ago)
Author:
lancewillett
Message:

Tests: Improve isolation of admin tests.

Reset menu registries and plugin-dependency static properties before and after tests. Remove the temporary MU-plugin directory after its fixture files are deleted.

Give list-table fixtures distinct screens and preserve whether the original hook suffix was set. Align the comments table's expected primary column with its declared default.

Allow updater tests to load Composer dependencies outside the checkout while exercising open_basedir restrictions.

Developed in: https://github.com/WordPress/wordpress-develop/pull/13181

Props jonsurrell, mindctrl.
See #65893.

Location:
trunk/tests/phpunit/tests/admin
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPlugin.php

    r61006 r63608  
    1212         */
    1313        public static $admin_id;
     14
     15        public function set_up() {
     16                parent::set_up();
     17                $this->reset_menu_globals();
     18        }
     19
     20        public function tear_down() {
     21                $this->reset_menu_globals();
     22                parent::tear_down();
     23        }
     24
     25        /**
     26         * Resets the global menu registries modified by the menu API tests.
     27         */
     28        private function reset_menu_globals() {
     29                global $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages;
     30
     31                $menu              = array();
     32                $submenu           = array();
     33                $admin_page_hooks  = array();
     34                $_registered_pages = array();
     35                $_parent_pages     = array();
     36        }
    1437
    1538        public static function wpSetUpBeforeClass( $factory ) {
     
    502525                unlink( WPMU_PLUGIN_DIR . '/foo.php' );
    503526                unlink( WPMU_PLUGIN_DIR . '/bar.txt' );
     527                rmdir( WPMU_PLUGIN_DIR );
    504528
    505529                $this->assertSame( array( 'foo.php' ), array_keys( $found ) );
  • trunk/tests/phpunit/tests/admin/plugin-dependencies/base.php

    r60729 r63608  
    6262
    6363        /**
     64         * Resets all static properties to a default value before each test.
     65         */
     66        public function set_up() {
     67                parent::set_up();
     68                $this->reset_static_properties();
     69        }
     70
     71        /**
    6472         * Resets all static properties to a default value after each test.
    6573         */
    6674        public function tear_down() {
     75                $this->reset_static_properties();
     76                parent::tear_down();
     77        }
     78
     79        /**
     80         * Resets all static properties to their default values.
     81         */
     82        private function reset_static_properties() {
    6783                foreach ( self::$static_properties as $name => $default_value ) {
    6884                        $this->set_property_value( $name, $default_value );
    6985                }
    70 
    71                 parent::tear_down();
    7286        }
    7387
  • trunk/tests/phpunit/tests/admin/wpAutomaticUpdater.php

    r60729 r63608  
    3939                parent::set_up();
    4040                add_filter( 'pre_wp_mail', '__return_false' );
     41        }
     42
     43        /**
     44         * Builds an open_basedir value that allows PHPUnit to load Composer dependencies.
     45         *
     46         * Composer dependencies may live outside a secondary worktree, and PHPUnit can
     47         * load assertion-related classes after the restriction is set.
     48         *
     49         * @param string $abspath_grandparent Directory containing the repository.
     50         * @return string The open_basedir value.
     51         */
     52        private function get_open_basedir_for_tests( $abspath_grandparent ) {
     53                $composer_vendor_dir = dirname(
     54                        ( new ReflectionClass( Composer\Autoload\ClassLoader::class ) )->getFileName(),
     55                        2
     56                );
     57
     58                return implode(
     59                        PATH_SEPARATOR,
     60                        array(
     61                                sys_get_temp_dir(),
     62                                wp_normalize_path( $abspath_grandparent ),
     63                                wp_normalize_path( $composer_vendor_dir ),
     64                        )
     65                );
    4166        }
    4267
     
    613638
    614639                $open_basedir_backup = ini_get( 'open_basedir' );
    615                 // Allow access to the directory one level above the repository.
    616                 ini_set( 'open_basedir', sys_get_temp_dir() . PATH_SEPARATOR . wp_normalize_path( $abspath_grandparent ) );
     640                ini_set( 'open_basedir', $this->get_open_basedir_for_tests( $abspath_grandparent ) );
    617641
    618642                // Checking an allowed directory should succeed.
     
    648672
    649673                $open_basedir_backup = ini_get( 'open_basedir' );
    650                 // Allow access to the directory one level above the repository.
    651                 ini_set( 'open_basedir', sys_get_temp_dir() . PATH_SEPARATOR . wp_normalize_path( $abspath_grandparent ) );
     674                ini_set( 'open_basedir', $this->get_open_basedir_for_tests( $abspath_grandparent ) );
    652675
    653676                // Checking a directory not within the allowed path should trigger an `open_basedir` warning.
  • trunk/tests/phpunit/tests/admin/wpListTable.php

    r60729 r63608  
    1818         * Original value of $GLOBALS['hook_suffix'].
    1919         *
    20          * @var string
     20         * @var string|null
    2121         */
    2222        private static $original_hook_suffix;
     23
     24        /**
     25         * Whether $GLOBALS['hook_suffix'] existed before the test class ran.
     26         *
     27         * @var bool
     28         */
     29        private static $hook_suffix_was_set;
    2330
    2431        public static function set_up_before_class() {
    2532                parent::set_up_before_class();
    2633
    27                 static::$original_hook_suffix = $GLOBALS['hook_suffix'];
     34                static::$hook_suffix_was_set  = array_key_exists( 'hook_suffix', $GLOBALS );
     35                static::$original_hook_suffix = $GLOBALS['hook_suffix'] ?? null;
    2836
    2937                require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
     
    3846
    3947        public function clean_up_global_scope() {
    40                 global $hook_suffix;
    41                 $hook_suffix = static::$original_hook_suffix;
     48                if ( static::$hook_suffix_was_set ) {
     49                        $GLOBALS['hook_suffix'] = static::$original_hook_suffix;
     50                } else {
     51                        unset( $GLOBALS['hook_suffix'] );
     52                }
     53
    4254                parent::clean_up_global_scope();
    4355        }
     
    6274                add_filter( 'list_table_primary_column', array( $hook, 'filter' ) );
    6375
    64                 /*
    65                  * Set a dummy value for the current screen in the admin to prevent
    66                  * `_get_list_table()` throwing.
    67                  */
    68                 $GLOBALS['hook_suffix'] = 'my-hook';
    69 
    70                 $list_table = _get_list_table( $list_class );
     76                $list_table = _get_list_table(
     77                        $list_class,
     78                        array( 'screen' => 'wp-list-table-test-' . sanitize_key( $list_class ) )
     79                );
    7180
    7281                $column_headers = new ReflectionProperty( $list_table, '_column_headers' );
     
    98107                $list_primary_columns = array(
    99108                        'WP_Application_Passwords_List_Table'         => 'name',
    100                         'WP_Comments_List_Table'                      => 'author',
     109                        'WP_Comments_List_Table'                      => 'comment',
    101110                        'WP_Links_List_Table'                         => 'name',
    102111                        'WP_Media_List_Table'                         => 'title',
Note: See TracChangeset for help on using the changeset viewer.