Skip to content

Tests: Clear the theme.json cache before each WP_Theme_UnitTestCase test. - #16226

Closed
mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-theme-json-cache
Closed

mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-theme-json-cache

Conversation

@mayankmajeji

@mayankmajeji mayankmajeji commented Nov 25, 2026 •

Copy link
Copy Markdown

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

WP_Theme_UnitTestCase::tear_down() calls wp_clean_theme_json_cache(), but set_up() only calls wp_clean_themes_cache(). The static caches in WP_Theme_JSON_Resolver ($theme, $merged and the others reset by clean_cached_data()) last for the whole PHP process, so the first test of a theme class runs with whatever theme.json data the previous class left there. Inside a class, the previous test's tear_down() clears it, which is why the default order passes.

Two Tests_Theme_WpGetGlobalStylesheet tests fail when they are the first test of their class to run:

  • After Tests_Theme_wpThemeGetPostTemplates, which switches to block-theme, test_switching_themes_should_recalculate_stylesheet() finds the block theme's --wp--preset--font-size--custom: 100px in the stylesheet for the default theme.
  • After Tests_Theme_wpThemeJson, test_theme_color_palette_presets_output_when_border_support_enabled() gets theme data computed before it added editor-color-palette, so its colors are missing.

Both pass on their own.

What changes

  • WP_Theme_UnitTestCase::set_up() calls wp_clean_theme_json_cache() next to wp_clean_themes_cache(), as tear_down() already does.

Testing

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

The --filter makes the stylesheet test the first test of its class, as in the random runs. Config A runs theme/wpThemeGetPostTemplates.php and then theme/wpGetGlobalStylesheet.php, config B runs theme/wpThemeJson.php and then theme/wpGetGlobalStylesheet.php (configs below).

  • vendor/bin/phpunit -c pair-a.xml --filter 'Tests_Theme_wpThemeGetPostTemplates|test_switching_themes_should_recalculate_stylesheet': 1 failure on trunk ("Custom font size (100px) should not present for default theme"), OK (7 tests, 32 assertions) with this change.
  • vendor/bin/phpunit -c pair-b.xml --filter 'Tests_Theme_wpThemeJson|test_theme_color_palette_presets_output_when_border_support_enabled': 1 failure on trunk (--wp--preset--color--nice-black: #000000 missing), OK (288 tests, 516 assertions) with this change.
  • Both the same on multisite (the configs with <const name="WP_TESTS_MULTISITE" value="1" /> added).
  • Each of the two stylesheet tests on its own: OK on trunk.
  • vendor/bin/phpunit --group themes --order-by=random --random-order-seed=N for N = 1 to 10: OK (817 tests) with this change, and for seeds 2 and 7 with -c tests/phpunit/multisite.xml (821 tests).
  • The seven classes that extend WP_Theme_UnitTestCase, each on its own in default order, with --order-by=reverse and with --order-by=random --random-order-seed=7: OK with this change.
  • Found in a sweep of --group themes in random order (20 seeds): seed 2 and seed 7 failed on trunk.
  • 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/theme/wpThemeGetPostTemplates.php</file>
			<file>tests/phpunit/tests/theme/wpGetGlobalStylesheet.php</file>
		</testsuite>
	</testsuites>
	<php>
		<const name="WP_RUN_CORE_TESTS" value="1" />
	</php>
</phpunit>

For config B, replace theme/wpThemeGetPostTemplates.php with theme/wpThemeJson.php.

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.

@github-actions

github-actions Bot commented Nov 25, 2026 •

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, lancewillett, mukesh27.

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.

@mukeshpanchal27

Copy link
Copy Markdown
Member

@mayankmajeji Are you able to split the commits in to two:

  • first commit: just do the changes that demonstrate the bug
  • second commit: the proposed fix

Then can you force push just the first commit to this PR, wait for the tests to run and then push the fix? Depending on your permissions you might be able to get Claude to push, monitor the test status and push again.

@mayankmajeji
mayankmajeji force-pushed the tests/66755-theme-json-cache branch from a1c01f1 to 4e0de9a Compare November 25, 2026 07:50
@mayankmajeji

Copy link
Copy Markdown
Author

Thanks @mukeshpanchal27, done.

The original failures only show up in some random orders, and CI runs the default order, so the bug needed a test that fails in the default order too. The first commit adds Tests_Theme_WpThemeUnitTestCase: its wpSetUpBeforeClass() registers an editor-color-palette, calls wp_get_global_settings() to fill the theme.json caches the way an earlier class can, and removes the palette again. Its test then checks that the stale palette color is not in the stylesheet.

Locally the new test also fails on trunk inside --group themes in default order (818 tests, 1 failure) and passes with the fix, as do --group themes in reverse order and with random seeds 2 and 7, on single site and multisite.

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.

🟡 Changes recommended

The regression test can pass without the fix when an existing resolver cache bypasses the temporary palette, so the cache must be cleared before priming to avoid class-order dependence.

1 open issue
What changed in this PR

This PR improves isolation for tests extending WP_Theme_UnitTestCase by clearing theme.json caches before each test, preventing cached data from earlier classes from affecting results.

Changes:

  • Clears theme.json caches during setup, matching existing teardown behavior.
  • Adds a regression test that seeds a temporary palette and checks that its stale CSS variable is absent after setup.
File Description
tests/​phpunit/​tests/​theme/​wpThemeUnitTestCase.php Adds regression coverage for stale theme.json palette data.
tests/​phpunit/​tests/​theme/​base.php Clears theme.json caches before each theme test.

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.

The one-line set_up() fix is justified. I reproduced both failures using only the existing classes, on single-site and multisite. With the reset, the same ordered runs passed (7 tests and 288 tests respectively).

Please keep that line and drop tests/phpunit/tests/theme/wpThemeUnitTestCase.php from the final patch. The two-commit demonstration was helpful. I also reproduced Copilot’s concern: without the fix, the new test fails alone but passes after Tests_Theme_wpThemeGetPostTemplates. The existing stylesheet test still fails after that same class and catches the real problem. Please keep those two reproduction commands in the description for future reviewers.

AI review · gpt-6

The resolver caches outlive a test class, so the first test of a `WP_Theme_UnitTestCase` class could read theme.json data from the previous class. Clear them in `set_up()` as `tear_down()` already does.

See #66755.
@mayankmajeji
mayankmajeji force-pushed the tests/66755-theme-json-cache branch from 3de6c62 to 2d78672 Compare November 25, 2026 14:21
@mayankmajeji

Copy link
Copy Markdown
Author

Thanks @lancewillett. Done: I force-pushed a single commit with only the set_up() line, so wpThemeUnitTestCase.php is no longer in the patch. The description is back to the version with the two reproduction commands (configs A and B).

You and Copilot are right about the test: its wp_get_global_settings() call can hit data an earlier class already cached, so the temporary palette never gets in and the test passes without the fix.

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

The revised patch keeps the single reset and removes the synthetic class. I verified the changed file matches the one I tested against both existing stylesheet failures. In the same class order, both site modes passed with the reset: 7 tests in the post-template pair and 288 in the theme JSON pair. Thanks for preserving both reproductions in the description.

AI review · gpt-6

@lancewillett lancewillett moved this from Backlog to In review in WordPress Project Build Tooling Nov 25, 2026
wporg-sync pushed a commit that referenced this pull request Nov 25, 2026
…eme_UnitTestCase`.

Prevent stylesheet tests from reading theme data cached by an earlier test class.

Developed in: #16226

Props mayankmajeji, mukesh27.
See #66755.


git-svn-id: https://develop.svn.wordpress.org/trunk@65780 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
…eme_UnitTestCase`.

Prevent stylesheet tests from reading theme data cached by an earlier test class.

Developed in: WordPress/wordpress-develop#16226

Props mayankmajeji, mukesh27.
See #66755.

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


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

4 participants