Skip to content

Commit c02fb11

Browse files
lancewillettspacedmonkey
authored andcommitted
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: WordPress#13091 Props mdawaffe, jonsurrell, sergeybiryukov, mindctrl. See #65893. git-svn-id: https://develop.svn.wordpress.org/trunk@63435 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 678d1dc commit c02fb11

6 files changed

Lines changed: 72 additions & 16 deletions

File tree

‎tests/phpunit/tests/fonts/font-face/base.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static function set_up_before_class() {
6969
public static function tear_down_after_class() {
7070
// Reset static flags.
7171
self::$requires_switch_theme_fixtures = false;
72+
self::$theme_root = null;
7273

7374
parent::tear_down_after_class();
7475
}

‎tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class Tests_Fonts_WpPrintFontFacesFromStyleVariations extends WP_Font_Face_UnitT
1616
const FONTS_THEME = 'fonts-block-theme';
1717

1818
public static function set_up_before_class() {
19-
parent::set_up_before_class();
2019
self::$requires_switch_theme_fixtures = true;
20+
21+
parent::set_up_before_class();
2122
}
2223

2324
/**

‎tests/phpunit/tests/rest-api/rest-block-type-controller.php‎

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
3131
*/
3232
protected static $subscriber_id;
3333

34+
/**
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+
3448
/**
3549
* Create fake data before our tests run.
3650
*
@@ -49,21 +63,55 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
4963
'role' => 'subscriber',
5064
)
5165
);
52-
53-
$name = 'fake/test';
54-
$settings = array(
55-
'icon' => 'text',
56-
);
57-
58-
register_block_type( $name, $settings );
5966
}
6067

6168
public static function wpTearDownAfterClass() {
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

69117
/**

‎tests/phpunit/tests/sitemaps/sitemaps.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,17 @@ public function test_sitemaps_enabled() {
464464

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' );
478480

@@ -481,8 +483,6 @@ public function test_disable_sitemap_should_return_404() {
481483

482484
/**
483485
* @ticket 50643
484-
* @runInSeparateProcess
485-
* @preserveGlobalState disabled
486486
*/
487487
public function test_empty_url_list_should_return_404() {
488488
wp_register_sitemap_provider( 'foo', new WP_Sitemaps_Empty_Test_Provider( 'foo' ) );

‎tests/phpunit/tests/widgets/wpWidgetMediaAudio.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ public function test_enqueue_admin_scripts() {
316316
* @covers WP_Widget_Media_Audio::render_control_template_scripts
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

321324
ob_start();

‎tests/phpunit/tests/widgets/wpWidgetMediaVideo.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ public function test_enqueue_admin_scripts() {
344344
* @covers WP_Widget_Media_Video::render_control_template_scripts
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

349352
ob_start();

0 commit comments

Comments
 (0)