Make WordPress Core

Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#61337 closed defect (bug) (fixed)

Tests: Cached data in `WP_Theme_JSON_Resolver` causes cross pollution in unit tests

Reported by: joemcgill's profile joemcgill Owned by: joemcgill's profile joemcgill
Milestone: 6.6 Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

While working on #59595, @thekt12 and I discovered that while all of the tests in the Tests_Theme_WpAddGlobalStylesForBlocks class were passing when run together, there were a few that failed when run in isolation, specifically:

  • test_blocks_inline_styles_get_rendered()
  • test_third_party_blocks_inline_styles_for_elements_get_rendered()

In both cases, the tests were relying on cached data that had been set up in previous tests by calling $this->set_up_third_party_block(); to still be returned by the WP_Theme_JSON_Resolver class even though these tests don't call $this->set_up_third_party_block() themselves.

To confirm, run the following to see all tests in the class pass:

npm run test:php -- --filter Tests_Theme_WpAddGlobalStylesForBlocks

And the following to see an example of a test that fails in isolation

npm run test:php -- --filter test_blocks_inline_styles_get_rendered

The reason this happens is that the theme gets reset between each test that extends the WP_Theme_UnitTestCase but the wp_clean_theme_json_cache() function was not being called to clean Theme JSON related caches (including the WP_Theme_JSON_Resolver::clean_cached_data() method).

Change History (5)

This ticket was mentioned in PR #6696 on WordPress/wordpress-develop by @joemcgill.


9 months ago
#1

  • Keywords has-patch has-unit-tests added

This clears the theme json related caches in the WP_Theme_UnitTestCase:::tear_down() method to avoid cross pollution between tests when a theme is changed during a test run and fixes a few tests that were only passing by relying on this cross pollution.

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

#2 @joemcgill
9 months ago

@isabel_brison could you confirm that I'm not missing something with this change, since you've recently updated these unit tests?

This ticket was mentioned in Slack in #core-performance by joemcgill. View the logs.


9 months ago

#4 @joemcgill
9 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 58295:

Build/Test Tools: Reduce cross-pollution in Tests_Theme_WpAddGlobalStylesForBlocks.

This ensures cached Theme JSON data is cleared between all test classes that extend WP_Theme_UnitTestCase and fixes a few tests that would fail in isolation, even though they full test suite would pass.

Props isabel_brison, thekt12, joemcgill.
Fixes #61337.

Note: See TracTickets for help on using tickets.