Skip to content

Tests: Restore the theme in the template REST tests that switch to block-theme. - #16266

Open
mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-templates-restore-theme
Open

mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-templates-restore-theme

Conversation

@mayankmajeji

Copy link
Copy Markdown

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

Follow-up to comment:74 and comment:82.

Tests_REST_WpRestTemplatesController::test_get_item_dates_are_null_for_file_backed_template() and ::test_get_template_fallback() call switch_theme( 'block-theme' ) and never switch back. The test case rolls back the theme options, but wp_clean_theme_json_cache() only runs on switch_theme, so the WP_Theme_JSON_Resolver caches keep block-theme's data. A later block render then picks up block-theme's duotone settings, and Tests_Media::test_wp_filter_content_tags_does_not_lazy_load_first_featured_image_in_block_theme() gets wp-duotone-custom-duotone on the featured image.

What changes

  • Both tests store get_stylesheet() before switching and call switch_theme() with it in a finally block, which runs the existing cache invalidation hook.

Testing

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

The config below runs rest-api/wpRestTemplatesController.php and then media.php.

  • vendor/bin/phpunit -c pair.xml --filter '/test_get_item_dates_are_null_for_file_backed_template|test_wp_filter_content_tags_does_not_lazy_load_first_featured_image_in_block_theme/': 1 failure on trunk, OK (2 tests, 4 assertions) with this change, on single site and multisite.
  • The same with test_get_template_fallback$ in place of the first test: 1 failure on trunk, OK (2 tests, 4 assertions) with this change, on single site and multisite.
  • The test_get_item_with_valid_theme_dirname() data sets also switch themes, but they do not affect the media test (OK on trunk), so they are unchanged.
  • Both classes in full with the same config: no failures (415 tests), single site and multisite.
  • Tests_REST_WpRestTemplatesController with --order-by=reverse and --order-by=random --random-order-seed=7: OK (50 tests).
  • PHPCS reports nothing on the changed file.

I also looked for a test that renders blocks while block-theme is active. The only one is Tests_Block_Template, which switches in set_up() and back in tear_down(). Each of its template rendering tests, and the whole class, followed by the media test passes on single site and multisite (35 tests), so there is no separate duotone reproducer to add.

<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/rest-api/wpRestTemplatesController.php</file>
			<file>tests/phpunit/tests/media.php</file>
		</testsuite>
	</testsuites>
	<php>
		<const name="WP_RUN_CORE_TESTS" value="1" />
	</php>
</phpunit>

…lock-theme`.

`test_get_item_dates_are_null_for_file_backed_template()` and `test_get_template_fallback()` left block-theme's theme.json data in the resolver caches, so a later block render in `Tests_Media` picked up its duotone settings. Switch back to the previous theme in a `finally` block, which clears those caches.

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 remain for the theme-restoration test changes.

No open issues 🎉

What changed in this PR

This PR improves REST template test isolation by restoring the original theme after two tests use block-theme, triggering cache invalidation and preventing theme data from leaking into later tests.

Changes:

  • Save the active stylesheet before switching themes in both tests.
  • Restore it in finally blocks, including when assertions fail, without changing existing test assertions.
File Description
tests/​phpunit/​tests/​rest-api/​wpRestTemplatesController.php Adds theme restoration to the file-backed template date and template fallback 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.

Both theme-switching tests independently caused the following media test to fail. Restoring the original theme in each finally block made all four producer/consumer runs pass across single-site and multisite. The controller class also passed in randomized order.

AI review · gpt-6.2-astra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

3 participants