[wp-trac] [WordPress Trac] #53431: Tests: Reset global $current_screen between tests to avoid cross-test interdependencies

WordPress Trac noreply at wordpress.org
Wed Jun 16 21:30:54 UTC 2021


#53431: Tests: Reset global $current_screen between tests to avoid cross-test
interdependencies
--------------------------------------+-----------------------------
 Reporter:  hellofromTonya            |       Owner:  hellofromTonya
     Type:  defect (bug)              |      Status:  assigned
 Priority:  normal                    |   Milestone:  5.9
Component:  Build/Test Tools          |     Version:
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+-----------------------------

Comment (by hellofromTonya):

 [https://github.com/WordPress/wordpress-develop/pull/1380 PR 1380] does
 the following:

 - Resets the globals associated with current screen in
 `WP_UnitTestCase_Base::tearDown()`
 - Removes the resetting from the individual tests (I think I got them all)

 How is doing the reset?

 There were a few approaches to resetting, but the most popular was:

 {{{#!php
 set_current_screen( 'front' );
 }}}

 What does this do?

 - It instantiates a new `WP_Screen` object
 - Sets global `$current_screen` to the instance
 - Sets global `$taxnow` to an empty string
 - Sets global `$typenow` to an empty string
 - Fires `'current_screen'` action event passing the instance to callbacks

 I wondered: Why is `'front'` needed as a starting state? Is it needed?

 - Looking at the other resets in the base test case, they are set to
 `null`. Tried it for current screen globals after removing resets from
 individual tests. Tests all passed.
 - How about unsetting the globals? Tests all passed.

 Alrighty, `'front'` isn't needed. Cool.

 Which approach did the PR implement?
 - Setting the 3 globals to `null`
 - Why? Consistency with the other resets

 @SergeyBiryukov @johnbillion What do you think about
 [https://github.com/WordPress/wordpress-
 develop/blob/a538457bfb0d4eff7184f5c68ad5f05cf90f6ee6/tests/phpunit/includes
 /abstract-testcase.php#L174-L177 this reset approach]?
 {{{#!php
 $current_screen_globals = array( 'current_screen', 'taxnow', 'typenow' );
 foreach ( $current_screen_globals as $global ) {
         $GLOBALS[ $global ] = null;
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53431#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list