Repository navigation
Tests: Reset the $wp_customize global after each test. - #16248
mayankmajeji wants to merge 1 commit into
Conversation
Several tests leave a `WP_Customize_Manager` in the global, so the first test of a later class runs as if in a Customizer preview. Reset it in `tear_down()` next to the other globals. See #66755.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the change that clears the Customizer global between tests.
No open issues 🎉
What changed in this PR
This PR prevents Customizer state from leaking between PHPUnit tests, addressing order-dependent failures caused by a leftover $wp_customize global.
Changes:
- Reset
$GLOBALS['wp_customize']tonullin the sharedtear_down()method, alongside existing global cleanup. - Add a comment explaining that the reset makes
is_customize_preview()false for the next test.
| File | Description |
|---|---|
tests/phpunit/includes/abstract-testcase.php |
Clear the Customizer global after each test to improve test isolation. |
Review effort: Balanced
lancewillett
left a comment
There was a problem hiding this comment.
I reproduced both reported test-order failures on single-site and multisite. With the same four pairs in the same order, clearing the Customizer manager in the shared teardown made every pair pass. That gives this reset two existing consumers.
AI review · gpt-6.2-astra
Reset `$wp_customize` in teardown so later widget tests run outside the Customizer preview. Developed in: #16248 Props mayankmajeji. See #66755. git-svn-id: https://develop.svn.wordpress.org/trunk@65784 602fd350-edb4-49c9-b593-d223f7449a82
|
PR #16248 landed in https://core.trac.wordpress.org/changeset/65784 |
Reset `$wp_customize` in teardown so later widget tests run outside the Customizer preview. Developed in: WordPress/wordpress-develop#16248 Props mayankmajeji. See #66755. Built from https://develop.svn.wordpress.org/trunk@65784 git-svn-id: http://core.svn.wordpress.org/trunk@64813 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Trac ticket: https://core.trac.wordpress.org/ticket/66755
WP_UnitTestCase_Base::tear_down()resets the query, post, screen and comment globals, but not$wp_customize. Tests that create aWP_Customize_Managerand keep it in the global leave it there for the next class:Tests_AdminBar::test_customize_link()sets it and never clears it.Tests_Editor_EnableContentEditorForNavigationPostTypeandTests_Editor_DisableContentEditorForNavigationPostTypepublish acustomize_changesetpost in theircustomize_changesetdata set, and_wp_customize_publish_changeset()creates the manager in the global.Test_WP_Customize_Nav_Menu_Setting,Test_WP_Customize_Nav_Menu_Item_Setting,Test_WP_Customize_Nav_MenusandTest_WP_Customize_Custom_CSS_Settingclear it inclean_up_global_scope(), which runs inset_up(), so the manager of their last test stays.With a manager in the global,
is_customize_preview()is true. Two tests fail when they are the first test of their class to run after one of these:Tests_Widgets::test_wp_widget_is_preview()asserts that$wp_customizeis empty, afterTests_AdminBar::test_customize_link().Tests_Widgets_wpNavMenuWidget::test_form_displays_fields_with_saved_settings()gets the Customizer link (javascript: wp.customize.panel( "nav_menus" ).focus();) instead ofnav-menus.php, afterTest_WP_Customize_Nav_Menu_Setting.Inside those classes, the previous test's
tear_down()clears the global, so only the first test fails. Both pass on their own.What changes
WP_UnitTestCase_Base::tear_down()sets$GLOBALS['wp_customize']tonull, next to the screen globals. Tests that need a manager already create one in their ownset_up()or in the test.Testing
Locally with PHP 8.4.28, MySQL 9.7.4 and PHPUnit 9.6.39, on trunk at r65781.
The
--filtermakes each failing test the first test of its class, as in the random runs. Config A runscustomize/nav-menu-setting.phpand thenwidgets/wpNavMenuWidget.php, config B runsadminbar.phpand thenwidgets.php(config below).vendor/bin/phpunit -c pair-a.xml --filter 'Test_WP_Customize_Nav_Menu_Setting::test_constants$|test_form_displays_fields_with_saved_settings': 1 failure on trunk, OK (2 tests, 15 assertions) with this change.vendor/bin/phpunit -c pair-b.xml --filter 'test_customize_link$|test_wp_widget_is_preview': 1 failure on trunk ("Failed asserting that an object is empty"), OK (2 tests, 8 assertions) with this change.<const name="WP_TESTS_MULTISITE" value="1" />added).-c tests/phpunit/multisite.xml --order-by=random: seed 1616 failedtest_wp_widget_is_preview()and seed 1717 failedtest_form_displays_fields_with_saved_settings(). The random order depends on the set of tests, so the same seeds give a different order on current trunk; the configs above reproduce both failures without a seed.For config B, use
adminbar.phpandwidgets.phpas the two files.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Investigation, code, local testing and PR wording. I reviewed the reasoning, the code and the test results, and I take responsibility for the contribution.