Skip to content

Tests: Reset scripts and styles after each Test_WP_Customize_Nav_Menus test - #16159

Closed
noruzzamans wants to merge 1 commit into
WordPress:trunkfrom
noruzzamans:tests/66755-isolate-customize-nav-menus
Closed

noruzzamans wants to merge 1 commit into
WordPress:trunkfrom
noruzzamans:tests/66755-isolate-customize-nav-menus

Conversation

@noruzzamans

Copy link
Copy Markdown
Member

Test_WP_Customize_Nav_Menus::test_customize_preview_enqueue_deps() calls WP_Customize_Nav_Menus::customize_preview_enqueue_deps(), which enqueues the customize-preview-nav-menus script and its dependencies (jquery, wp-util, customize-preview, customize-selective-refresh). In addition, test setup instantiates WP_Customize_Manager into $GLOBALS['wp_customize'].

Previously, Test_WP_Customize_Nav_Menus did not implement tear_down(), leaving $wp_customize, $wp_scripts, and $wp_styles populated in the global scope across tests.

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

What changes

  • Implements tear_down() in Test_WP_Customize_Nav_Menus to reset $this->wp_customize = null;, unset( $GLOBALS['wp_customize'] );, unset( $GLOBALS['wp_scripts'] );, and unset( $GLOBALS['wp_styles'] ); before calling parent::tear_down(), matching the isolation pattern used in Tests_WP_Customize_Widgets and Tests_WP_Customize_Manager.

Testing

Locally tested with Docker (PHP 8.4, MySQL 9.7, PHPUnit 9.6.39):

  • npm run test:php -- --filter Test_WP_Customize_Nav_Menus: OK (29 tests, 196 assertions).
  • npm run test:php -- --group customize: OK (243 tests, 2,258 assertions).
  • npm run test:php -- --group customize --order-by=random --random-order-seed=42: OK (243 tests, 2,254 assertions).
  • npm run test:php -- -c tests/phpunit/multisite.xml --group customize: OK (244 tests, 2,264 assertions).
  • ./vendor/bin/phpcs tests/phpunit/tests/customize/nav-menus.php: 0 errors, 0 warnings.

…s test

Test_WP_Customize_Nav_Menus::test_customize_preview_enqueue_deps() calls
WP_Customize_Nav_Menus::customize_preview_enqueue_deps(), which enqueues
the customize-preview-nav-menus script and its dependencies. Test setup also
instantiates WP_Customize_Manager into $GLOBALS['wp_customize'].

Adding tear_down() ensures $this->wp_customize, $GLOBALS['wp_customize'],
$GLOBALS['wp_scripts'], and $GLOBALS['wp_styles'] are cleanly reset between
tests, preventing leaked global state across test runs.

Props noruzzamanrubel.
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 noruzzaman.

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 remain for the per-test Customizer and asset-state cleanup.

No open issues 🎉

What changed in this PR

Improves test isolation for Test_WP_Customize_Nav_Menus by clearing Customizer and asset state after each test, following the cleanup pattern used by related Customizer tests.

Changes:

  • Adds tear_down() to clear the instance’s Customizer reference and unset the wp_customize, wp_scripts, and wp_styles globals.
  • Calls the parent teardown after cleanup.
File Description
tests/​phpunit/​tests/​customize/​nav-menus.php Adds per-test cleanup to prevent shared global state from leaking into subsequent tests.

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.

Could you show a pair of existing tests that fails on trunk in a fixed order and passes with the smallest reset? The nav-menu preview method enqueues customize-preview-nav-menus. Its dependencies are registered, but they aren’t added individually to WP_Dependencies::$queue. That distinction matters for the selective-refresh test, which inspects the queue directly.

Please narrow the teardown to the state that the failing test reads. This class already clears $wp_customize at the start of each test through clean_up_global_scope(). If an existing class fixture sees it before that reset, please name it and include it in the reproduction. The results currently listed in the PR all pass with the patch and don’t establish which of the four resets is necessary.

AI review · gpt-6

@noruzzamans

Copy link
Copy Markdown
Member Author

Thank you for the detailed inspection and explanation, @lancewillett. You are completely right—since clean_up_global_scope() already handles $wp_customize, and the script queue state does not break subsequent tests like selective-refresh on trunk, there is no reproducible test pair failure.

Closing this PR so ticket #66755 stays strictly focused on reproducible isolation failures.

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