Repository navigation
Tests: Log out the current user after class fixtures that need one. - #16185
mayankmajeji wants to merge 1 commit into
Conversation
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.
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
There was a problem hiding this comment.
🟢 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 fivewpSetUpBeforeClass()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
left a comment
There was a problem hiding this comment.
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
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
|
PR #16185 landed in https://core.trac.wordpress.org/changeset/65746 |
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
Trac ticket: https://core.trac.wordpress.org/ticket/66755
WP_UnitTestCase_Basesaves the hooks once, in theset_up()of the first test that runs, and restores that copy in every latertear_down(). The first class'swpSetUpBeforeClass()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_wpGetPostAutosaveWP_Test_REST_Autosaves_ControllerTests_REST_wpRestTemplateAutosavesControllerTests_Ajax_wpAjaxHeartbeatTests_REST_wpRestTemplateRevisionsControllerOn single site both roles have
unfiltered_html, sokses_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 everytear_down()restores them that way.tear_down()then callswp_set_current_user( 0 ), which only runskses_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_TheTitleAttributecreates its posts inwpSetUpBeforeClass(), so a raw&is stored in the title andthe_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()gets0instead oftrash, since the link is not normalized before the disallowed keys check, andtest_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
wp_set_current_user( 0 )at the end ofwpSetUpBeforeClass(), asWP_Test_REST_Revisions_Controllerdoes. 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:
rest-api/rest-autosaves-controller.php,image/header.php(a class that does not change the user),post/theTitleAttribute.phpandcomment/wpHandleCommentSubmission.php,vendor/bin/phpunit -c pair.xml: 3 failures on trunk (the twoTests_Post_TheTitleAttributetests and the filtered HTML disallowed keys test), OK (105 tests, 410 assertions) with this change.rest-api/rest-autosaves-controller.php: OK (62 tests, 177 assertions) on trunk.image/header.phpandpost/theTitleAttribute.php: 2 failures on trunk for each, OK with this change.--order-by=reverse, with--order-by=random --random-order-seed=7, and with-c tests/phpunit/multisite.xml: OK with this change (Tests_Ajax_wpAjaxHeartbeatwith--group ajax).--order-by=random --random-order-seed=303, whereWP_Test_REST_Autosaves_Controllerran first. The same title attribute and disallowed keys failures showed up with seed 505.--order-by=random --random-order-seed=303: 5 failures on trunk, 1 with this change. The one left isWP_Test_REST_Themes_Controller::test_theme_supports_custom_header(), a separate leak handled in Tests: Restore theme features after eachTests_Image_Headertest. #16182.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.