Skip to content

Tests: Cover the admin branch of wp_heartbeat_settings(). - #16223

Open
mukeshpanchal27 wants to merge 4 commits into
WordPress:trunkfrom
mukeshpanchal27:test/heartbeat-settings-admin
Open

mukeshpanchal27 wants to merge 4 commits into
WordPress:trunkfrom
mukeshpanchal27:test/heartbeat-settings-admin

Conversation

@mukeshpanchal27

@mukeshpanchal27 mukeshpanchal27 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

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

Description

Follow-up to [65768], which added tests for wp_heartbeat_settings().

wp_heartbeat_settings() adds ajaxurl only when ! is_admin(), because admin screens define ajaxurl themselves. It adds the heartbeat nonce whenever a user is logged in.

The tests from [65768] all run outside the admin. When I removed the ! is_admin() check, so that ajaxurl was always added, all three tests still passed.

This adds test_wp_heartbeat_settings_admin(), which runs on the dashboard screen with a logged-in user. It checks that ajaxurl is not added and that the nonce is valid for heartbeat-nonce.

A logged-out admin case would run the same is_admin() branch, and the existing front-end tests already cover both login states, so one case is enough.

WP_UnitTestCase resets the current screen after each test, so no tear_down() is needed.

Testing

  • npm run test:php -- --filter Tests_General_WpHeartbeatSettings: 4 tests pass on single site and multisite.
  • With the ! is_admin() check removed, the new test fails.
  • PHPCS reports no issues.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Finding the untested branch, writing the test, and drafting this description. I reviewed the result.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

🤖 Generated with Claude Code

mukeshpanchal27 and others added 2 commits November 25, 2026 09:29
`wp_heartbeat_settings()` adds `ajaxurl` only outside the admin, because admin screens define it themselves. The tests added in [65768] all run outside the admin, so they pass even when the `is_admin()` check is removed.

This adds a test for admin requests, logged in and logged out. It checks that `ajaxurl` is not added, and that a valid nonce is added only for a logged-in user.

Follow-up to [65768].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…` test.

See #66757.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 25, 2026 •

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 mukesh27, lancewillett.

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 were identified with the added admin-context heartbeat settings test coverage.

No open issues 🎉

What changed in this PR

Adds coverage for the admin branch of wp_heartbeat_settings(), complementing the existing non-admin tests.

Changes:

  • Adds a dashboard-screen test with logged-in and logged-out cases.
  • Checks that admin settings omit ajaxurl and include a valid heartbeat nonce only when logged in.
File Description
tests/​phpunit/​tests/​general/​wpHeartbeatSettings.php Adds the admin-context test and its authentication-state data provider.

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.

Could you keep one logged-in dashboard case? Check that ajaxurl is absent and the heartbeat nonce verifies. The existing frontend tests already cover both login states, and the logged-out dashboard row exercises the same is_admin() branch.

I tried the single case on single-site and multisite. Removing the admin guard still failed it, so we can drop the provider and the conditional assertion.

I checked the ticket mentioned in the description. #66614 tracks the original frontend tests, so their annotations are accurate. Please leave those in place and link the admin follow-up from a current component ticket. Original heartbeat coverage.

AI review · gpt-6

mukeshpanchal27 and others added 2 commits November 25, 2026 21:06
… test.

The logged-out case runs the same `is_admin()` branch, and the existing front-end tests already cover both login states. Drop the data provider and the conditional assertion.

See #66757.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lancewillett
lancewillett requested a balanced review from Copilot November 25, 2026 16:27

@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.

The single logged-in dashboard case covers both behaviors. It passed on single-site and multisite and failed in both when I removed the admin guard. The remaining step before landing is to link a current General component ticket for the follow-up. The existing frontend tests can keep #66614.

AI review · gpt-6.2-astra

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

The added admin heartbeat regression coverage has no active review issues.

No open issues 🎉

Review effort: Balanced

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