Skip to content

Tests: Reset the $wp_customize global after each test. - #16248

Closed
mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-wp-customize
Closed

mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-wp-customize

Conversation

@mayankmajeji

@mayankmajeji mayankmajeji commented Nov 25, 2026 •

Copy link
Copy Markdown

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 a WP_Customize_Manager and 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_EnableContentEditorForNavigationPostType and Tests_Editor_DisableContentEditorForNavigationPostType publish a customize_changeset post in their customize_changeset data 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_Menus and Test_WP_Customize_Custom_CSS_Setting clear it in clean_up_global_scope(), which runs in set_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_customize is empty, after Tests_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 of nav-menus.php, after Test_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'] to null, next to the screen globals. Tests that need a manager already create one in their own set_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 --filter makes each failing test the first test of its class, as in the random runs. Config A runs customize/nav-menu-setting.php and then widgets/wpNavMenuWidget.php, config B runs adminbar.php and then widgets.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.
  • Both the same on multisite (the configs with <const name="WP_TESTS_MULTISITE" value="1" /> added).
  • Found with -c tests/phpunit/multisite.xml --order-by=random: seed 1616 failed test_wp_widget_is_preview() and seed 1717 failed test_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.
  • Full suite in default order, with this change: OK on single site (32342 tests) and on multisite (33217 tests).
  • PHPCS reports nothing on the changed file.
<phpunit bootstrap="tests/phpunit/includes/bootstrap.php" backupGlobals="false" convertErrorsToExceptions="true" convertWarningsToExceptions="true" convertNoticesToExceptions="true" convertDeprecationsToExceptions="true">
	<testsuites>
		<testsuite name="pair">
			<file>tests/phpunit/tests/customize/nav-menu-setting.php</file>
			<file>tests/phpunit/tests/widgets/wpNavMenuWidget.php</file>
		</testsuite>
	</testsuites>
	<php>
		<const name="WP_RUN_CORE_TESTS" value="1" />
	</php>
</phpunit>

For config B, use adminbar.php and widgets.php as 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.

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.
@github-actions

Copy link
Copy Markdown

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:

Props mayankmajeji.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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'] to null in the shared tear_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 lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

wporg-sync pushed a commit that referenced this pull request Nov 25, 2026
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
@lancewillett

Copy link
Copy Markdown
Member

wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 25, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

3 participants