Skip to content

Fix: clean up registered theme directories and theme cache in teardown - #896

Open
CookieDarb wants to merge 1 commit into
WordPress:trunkfrom
CookieDarb:fix/test-theme-directory-isolation
Open

CookieDarb wants to merge 1 commit into
WordPress:trunkfrom
CookieDarb:fix/test-theme-directory-isolation

Conversation

@CookieDarb

Copy link
Copy Markdown

What?

Closes #895

Fixes a test isolation failure where CbtThemeLocale and CbtThemeReadme test suites leak custom theme directory registrations into the global $wp_theme_directories array and leave stale theme caches, causing 1 error and 5 failures in subsequent test classes (Test_Create_Block_Theme_Api and Test_Create_Block_Theme_Fonts).

Why?

CBT_Theme_Locale_UnitTestCase and CBT_Theme_Readme_UnitTestCase register DIR_TESTDATA . '/themes/' via register_theme_directory() during set_up(). In tear_down(), they switch back to the original theme, but never remove the registered directory from $wp_theme_directories and never call wp_clean_themes_cache().

This leaves registered theme directories leaked in the global $wp_theme_directories array and theme caches stale across the test suite. Subsequent tests that dynamically create themes on disk (such as Test_Create_Block_Theme_Api and Test_Create_Block_Theme_Fonts) fail to resolve them properly, leading to test failures during full suite runs.

How?

  • tests/CbtThemeLocale/base.php:
    • Captured the original $wp_theme_directories in set_up().
    • Restored $wp_theme_directories to its original state and called wp_clean_themes_cache() in tear_down().
  • tests/CbtThemeReadme/base.php:
    • Captured the original $wp_theme_directories in set_up().
    • Restored $wp_theme_directories to its original state and called wp_clean_themes_cache() in tear_down().

Testing Instructions

  1. Run CBT_Theme_Locale_EscapeAttribute immediately followed by Test_Create_Block_Theme_Api:
    npm run test:unit:php:base -- --filter "CBT_Theme_Locale_EscapeAttribute|Test_Create_Block_Theme_Api"
    Verify that all 19 tests pass without failures.
  2. Run the full unit test suite:
    npm run test:unit:php:base
    Verify that all 273 tests run cleanly with 0 errors and 0 failures.

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini
Used for: Diagnosing test pollution root cause in WordPress Core theme lookup and implementing test teardown cleanup. Final decisions and edits were made by me.

@CookieDarb
CookieDarb marked this pull request as ready for review October 22, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix test suite isolation by cleaning up registered theme directories and cache in teardown

1 participant