Make WordPress Core

Changeset 63435


Ignore:
Timestamp:
09/02/2026 02:58:58 PM (41 hours ago)
Author:
lancewillett
Message:

Build/Test Tools: Remove implicit dependencies on leaked state.

Several PHPUnit tests rely on state left by earlier tests or leak state into later tests.

Improve setup and teardown for font-face, REST block type, sitemap, and media widget tests so they can run independently and in random order.

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

Props mdawaffe, jonsurrell, sergeybiryukov, mindctrl.
See #65893.

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

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/fonts/font-face/base.php

    r56500 r63435  
    7070                // Reset static flags.
    7171                self::$requires_switch_theme_fixtures = false;
     72                self::$theme_root                     = null;
    7273
    7374                parent::tear_down_after_class();
  • trunk/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php

    r61411 r63435  
    1717
    1818        public static function set_up_before_class() {
     19                self::$requires_switch_theme_fixtures = true;
     20
    1921                parent::set_up_before_class();
    20                 self::$requires_switch_theme_fixtures = true;
    2122        }
    2223
  • trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php

    r62957 r63435  
    3333
    3434        /**
     35         * Pre-existing block types registered before the current test.
     36         *
     37         * @var string[] $registered_block_types
     38         */
     39        private $registered_block_types = array();
     40
     41        /**
     42         * Pre-existing block styles registered before the current test, grouped by block type name.
     43         *
     44         * @var array[] $registered_block_styles
     45         */
     46        private $registered_block_styles = array();
     47
     48        /**
    3549         * Create fake data before our tests run.
    3650         *
     
    5064                        )
    5165                );
    52 
    53                 $name     = 'fake/test';
    54                 $settings = array(
    55                         'icon' => 'text',
    56                 );
    57 
    58                 register_block_type( $name, $settings );
    5966        }
    6067
     
    6269                self::delete_user( self::$admin_id );
    6370                self::delete_user( self::$subscriber_id );
    64                 unregister_block_type( 'fake/test' );
    65                 unregister_block_type( 'fake/invalid' );
    66                 unregister_block_type( 'fake/false' );
     71        }
     72
     73        /**
     74         * Sets up each test method.
     75         *
     76         * Records the registered block types and block styles so that anything
     77         * registered by a test can be unregistered again in tear_down(), and
     78         * registers a block type used by many tests in this class.
     79         */
     80        public function set_up() {
     81                parent::set_up();
     82
     83                $this->registered_block_types  = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() );
     84                $this->registered_block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
     85
     86                register_block_type(
     87                        'fake/test',
     88                        array(
     89                                'icon' => 'text',
     90                        )
     91                );
     92        }
     93
     94        /**
     95         * Tears down each test method.
     96         *
     97         * Unregisters any block types and block styles registered while the test ran.
     98         */
     99        public function tear_down() {
     100                foreach ( WP_Block_Styles_Registry::get_instance()->get_all_registered() as $block_name => $block_styles ) {
     101                        foreach ( array_keys( $block_styles ) as $block_style_name ) {
     102                                if ( ! isset( $this->registered_block_styles[ $block_name ][ $block_style_name ] ) ) {
     103                                        unregister_block_style( $block_name, $block_style_name );
     104                                }
     105                        }
     106                }
     107
     108                foreach ( array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() ) as $block_name ) {
     109                        if ( ! in_array( $block_name, $this->registered_block_types, true ) ) {
     110                                unregister_block_type( $block_name );
     111                        }
     112                }
     113
     114                parent::tear_down();
    67115        }
    68116
  • trunk/tests/phpunit/tests/sitemaps/sitemaps.php

    r57987 r63435  
    465465        /**
    466466         * @ticket 50643
    467          * @runInSeparateProcess
    468          * @preserveGlobalState disabled
    469467         */
    470468        public function test_disable_sitemap_should_return_404() {
    471469                add_filter( 'wp_sitemaps_enabled', '__return_false' );
    472470
     471                // Instantiate the server before navigating: registering the sitemap
     472                // rewrite tags is what adds `sitemap` to `$wp->public_query_vars`.
     473                $sitemaps = wp_sitemaps_get_server();
     474
    473475                $this->go_to( home_url( '/?sitemap=index' ) );
    474476
    475                 wp_sitemaps_get_server()->render_sitemaps();
     477                $sitemaps->render_sitemaps();
    476478
    477479                remove_filter( 'wp_sitemaps_enabled', '__return_false' );
     
    482484        /**
    483485         * @ticket 50643
    484          * @runInSeparateProcess
    485          * @preserveGlobalState disabled
    486486         */
    487487        public function test_empty_url_list_should_return_404() {
  • trunk/tests/phpunit/tests/widgets/wpWidgetMediaAudio.php

    r59987 r63435  
    317317         */
    318318        public function test_render_control_template_scripts() {
     319                // Provides wp_underscore_audio_template(), normally loaded by wp_enqueue_media().
     320                require_once ABSPATH . WPINC . '/media-template.php';
     321
    319322                $widget = new WP_Widget_Media_Audio();
    320323
  • trunk/tests/phpunit/tests/widgets/wpWidgetMediaVideo.php

    r59954 r63435  
    345345         */
    346346        public function test_render_control_template_scripts() {
     347                // Provides wp_underscore_video_template(), normally loaded by wp_enqueue_media().
     348                require_once ABSPATH . WPINC . '/media-template.php';
     349
    347350                $widget = new WP_Widget_Media_Video();
    348351
Note: See TracChangeset for help on using the changeset viewer.