Skip to content

Tests: Restore the widget and sidebar globals after Tests_Widgets. - #16249

Closed
mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-widgets-globals
Closed

mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-widgets-globals

Conversation

@mayankmajeji

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/66755

Tests_Widgets::clean_up_global_scope() empties $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates and $wp_widget_factory->widgets before each test, but nothing puts them back after the class. The next class gets whatever the last Tests_Widgets test registered, and none of the core widgets.

When the last test is test_sidebar_guessing(), the primary and secondary sidebars it registers stay registered. Tests_WP_Customize_Manager::test_wp_customize_publish_changeset() then fails at $this->assertFalse( has_filter( 'option_sidebars_widgets' ) ): when it publishes the changeset, customize_register also runs for the previewing manager from set_up(), whose WP_Customize_Widgets adds a new sidebars_widgets[primary] setting for the leftover sidebar and previews it. The test passes on its own.

WP_Test_REST_Widgets_Controller and WP_Test_REST_Sidebars_Controller already rebuild the widgets in wpTearDownAfterClass().

What changes

  • Tests_Widgets saves the five globals in wpSetUpBeforeClass() and restores them in wpTearDownAfterClass(). The tests still start from empty globals, since clean_up_global_scope() is unchanged.

Testing

Locally with PHP 8.4.28, MySQL 9.7.4 and PHPUnit 9.6.39, on trunk at r65781.

The config below runs widgets.php and then customize/manager.php. The --filter makes test_sidebar_guessing() the last test of Tests_Widgets, as in the random run.

  • vendor/bin/phpunit -c pair.xml --filter 'Tests_Widgets::test_sidebar_guessing$|test_wp_customize_publish_changeset': 1 failure on trunk ("Failed asserting that true is false"), OK (2 tests, 8 assertions) with this change.
  • The same on multisite (the config with <const name="WP_TESTS_MULTISITE" value="1" /> added).
  • --group widgets (505 tests) and --group customize (243 tests): OK with this change, and --group widgets also on multisite.
  • The widget classes (--filter Tests_Widgets, 386 tests) in default order, with --order-by=reverse, with --order-by=random --random-order-seed=7, and on multisite: OK with this change.
  • Found with -c tests/phpunit/multisite.xml --order-by=random --random-order-seed=1717. The random order depends on the set of tests, so the seed gives a different order on current trunk; the config reproduces the failure 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/widgets.php</file>
			<file>tests/phpunit/tests/customize/manager.php</file>
		</testsuite>
	</testsuites>
	<php>
		<const name="WP_RUN_CORE_TESTS" value="1" />
	</php>
</phpunit>

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.

The class empties the registered sidebars and widgets before each test, so a later class got the sidebars of the last test and no core widgets. Save the globals before the class and restore them after it.

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

No active review issues are reported for the change that restores widget and sidebar state to improve test isolation.

No open issues 🎉

What changed in this PR

This PR improves test isolation by restoring widget and sidebar state after Tests_Widgets, preventing leftover registrations from affecting later test classes.

Changes:

  • Saves four registration globals and the widget factory’s widget collection before the class runs.
  • Restores the saved state after the class, leaving per-test cleanup unchanged.
File Description
tests/​phpunit/​tests/​widgets.php Adds class-level setup and teardown hooks to preserve and restore widget and sidebar state.

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 the Customizer failure after the sidebar guessing test on single-site and multisite. Restoring the widget and sidebar registries after the class made both ordered runs pass (2 tests, 8 assertions each). All 62 widget tests also passed in random order. Thanks for including the two-test reproducer.

AI review · gpt-6.2-astra

wporg-sync pushed a commit that referenced this pull request Nov 25, 2026
Preserve the sidebar and widget registrations that later Customizer tests inherit.

Developed in: #16249

Props mayankmajeji.
See #66755.


git-svn-id: https://develop.svn.wordpress.org/trunk@65795 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
Preserve the sidebar and widget registrations that later Customizer tests inherit.

Developed in: WordPress/wordpress-develop#16249

Props mayankmajeji.
See #66755.

Built from https://develop.svn.wordpress.org/trunk@65795


git-svn-id: http://core.svn.wordpress.org/trunk@64824 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