Skip to content

Tests: Log out the current user after class fixtures that need one. - #16185

Closed
mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-kses-snapshot
Closed

mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-kses-snapshot

Conversation

@mayankmajeji

Copy link
Copy Markdown

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

WP_UnitTestCase_Base saves the hooks once, in the set_up() of the first test that runs, and restores that copy in every later tear_down(). The first class's wpSetUpBeforeClass() has already run at that point. Five classes log in an administrator or editor there to create their fixtures, and never log out:

  • Tests_Post_wpGetPostAutosave
  • WP_Test_REST_Autosaves_Controller
  • Tests_REST_wpRestTemplateAutosavesController
  • Tests_Ajax_wpAjaxHeartbeat
  • Tests_REST_wpRestTemplateRevisionsController

On single site both roles have unfiltered_html, so kses_init() removes the kses filters when the user is set. When one of these classes runs first, the saved hooks have no kses filters, and every tear_down() restores them that way. tear_down() then calls wp_set_current_user( 0 ), which only runs kses_init() again if a test changed the user. After a test that did not, the next test and the next class fixtures run without kses for a logged out user:

  • Tests_Post_TheTitleAttribute creates its posts in wpSetUpBeforeClass(), so a raw & is stored in the title and the_title_attribute() returns & instead of & (test_strips_html_tags(), test_escapes_special_characters_for_attributes()).
  • Tests_Comment_wpHandleCommentSubmission::test_disallowed_keys_filtered_html_match_does_not_call_check_comment_flood_action_twice() gets 0 instead of trash, since the link is not normalized before the disallowed keys check, and test_anonymous_user_cannot_comment_unfiltered_html() finds the <script> tag in the comment.

All of these pass on their own. On multisite only super admins have unfiltered_html, so the multisite runs are not affected.

WP_Test_REST_Revisions_Controller::wpSetUpBeforeClass() already logs out after creating its fixtures.

What changes

  • The five classes call wp_set_current_user( 0 ) at the end of wpSetUpBeforeClass(), as WP_Test_REST_Revisions_Controller does. Their tests already set the user they need.

Testing

Locally with PHP 8.4.28, MySQL 9.7.4 and PHPUnit 9.6.39 on single site, on trunk at r65744:

  • With the config below, which runs rest-api/rest-autosaves-controller.php, image/header.php (a class that does not change the user), post/theTitleAttribute.php and comment/wpHandleCommentSubmission.php, vendor/bin/phpunit -c pair.xml: 3 failures on trunk (the two Tests_Post_TheTitleAttribute tests and the filtered HTML disallowed keys test), OK (105 tests, 410 assertions) with this change.
  • The same config without rest-api/rest-autosaves-controller.php: OK (62 tests, 177 assertions) on trunk.
  • With each of the five classes as the first file, followed by image/header.php and post/theTitleAttribute.php: 2 failures on trunk for each, OK with this change.
  • Each of the five classes on its own, in default order, with --order-by=reverse, with --order-by=random --random-order-seed=7, and with -c tests/phpunit/multisite.xml: OK with this change (Tests_Ajax_wpAjaxHeartbeat with --group ajax).
  • Found in a full-suite run with --order-by=random --random-order-seed=303, where WP_Test_REST_Autosaves_Controller ran first. The same title attribute and disallowed keys failures showed up with seed 505.
  • Full suite with --order-by=random --random-order-seed=303: 5 failures on trunk, 1 with this change. The one left is WP_Test_REST_Themes_Controller::test_theme_supports_custom_header(), a separate leak handled in Tests: Restore theme features after each Tests_Image_Header test. #16182.
  • PHPCS reports nothing on the changed files.
<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/rest-autosaves-controller.php</file>
			<file>tests/phpunit/tests/image/header.php</file>
			<file>tests/phpunit/tests/post/theTitleAttribute.php</file>
			<file>tests/phpunit/tests/comment/wpHandleCommentSubmission.php</file>
		</testsuite>
	</testsuites>
	<php>
		<const name="WP_RUN_CORE_TESTS" value="1" />
	</php>
</phpunit>

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.

When one of these classes runs first, the hooks are saved with the kses filters removed for an administrator or editor, and later tests for a logged out user run without kses. Reset the user at the end of `wpSetUpBeforeClass()`, as the REST revisions controller tests do.

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 are recorded for the fixture-user logout changes intended to improve PHPUnit test isolation.

No open issues 🎉

What changed in this PR

This PR improves PHPUnit test isolation by logging out fixture users before the shared hook state is captured. This prevents order-dependent sanitization failures in later tests on single-site installations.

Changes:

  • Add wp_set_current_user( 0 ) at the end of five wpSetUpBeforeClass() methods, preserving authenticated fixture creation while restoring logged-out filter state.
File Description
tests/​phpunit/​tests/​rest-api/​wpRestTemplateRevisionsController.php Log out after creating template revision fixtures.
tests/​phpunit/​tests/​rest-api/​wpRestTemplateAutosavesController.php Log out after creating template autosave fixtures.
tests/​phpunit/​tests/​rest-api/​rest-autosaves-controller.php Log out after creating REST autosave fixtures.
tests/​phpunit/​tests/​post/​wpGetAutoSave.php Log out after creating the post fixture.
tests/​phpunit/​tests/​ajax/​wpAjaxHeartbeat.php Log out after creating heartbeat post fixtures.

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.

I ran each of the five fixture classes first, followed by the title attribute tests. Each order failed on trunk and passed with the logout in place. Running the REST autosaves fixture first also reproduced the comment sanitization failure.

Logging out inside class setup restores the filters before the harness saves them. With both isolation fixes applied, a randomized run of the affected classes passed on single-site and multisite (369 tests each).

AI review · gpt-6

wporg-sync pushed a commit that referenced this pull request Nov 23, 2026
Restore the logged-out user before the test harness saves the sanitization filters for subsequent tests.

Developed in: #16185

Props mayankmajeji.
See #66755.


git-svn-id: https://develop.svn.wordpress.org/trunk@65746 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 23, 2026
Restore the logged-out user before the test harness saves the sanitization filters for subsequent tests.

Developed in: WordPress/wordpress-develop#16185

Props mayankmajeji.
See #66755.

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


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

3 participants