Skip to content

Tests: Add unit tests for is_protected_ajax_action() - #14473

Closed
shail-mehta wants to merge 2 commits into
WordPress:trunkfrom
shail-mehta:65819-add-is-protected-ajax-action-tests
Closed

shail-mehta wants to merge 2 commits into
WordPress:trunkfrom
shail-mehta:65819-add-is-protected-ajax-action-tests

Conversation

@shail-mehta

Copy link
Copy Markdown
Member

What

Trac ticket: #65819

Adds unit tests for is_protected_ajax_action() in tests/phpunit/tests/load/isProtectedAjaxAction.php.

Covers non-Ajax requests, missing actions, the default protected action list, and the wp_protected_ajax_actions filter.

Test-only change.

Why

This helper has had no dedicated PHPUnit coverage. Fits the #65819 missing-coverage track, and no open PR covers it.

How

  • Assert non-Ajax requests and Ajax requests without an action are not protected
  • Cover the default protected Ajax actions and an unprotected action
  • Confirm the filter can add actions and does not run when the request is not Ajax

Testing instructions

npm run test:php -- --filter Tests_Load_IsProtectedAjaxAction

Use of AI Tools

  • Cursor

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

Static review found no correctness or isolation issues in this test-only change; tests were not executed.

No open issues 🎉

What changed in this PR

Adds dedicated PHPUnit coverage for is_protected_ajax_action() under ticket #65819, without changing production code.

Changes:

  • Covers non-Ajax requests, missing actions, and protected versus unprotected actions.
  • Tests filter-based protection and confirms the filter is skipped outside Ajax.
  • Restores request state and isolates tests that define DOING_AJAX.
File Description
tests/​phpunit/​tests/​load/​isProtectedAjaxAction.php Adds six tests covering action protection and filter behavior.

Review effort: Balanced

@shail-mehta
shail-mehta marked this pull request as ready for review October 19, 2026 08:42
@github-actions

github-actions Bot commented Oct 19, 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. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props shailu25, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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

Thanks for the tests. The assertions match is_protected_ajax_action(). One suggestion inline that would remove the need for separate processes.

* @preserveGlobalState disabled
*/
public function test_returns_false_when_doing_ajax_without_action() {
define( 'DOING_AJAX', true );

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.

wp_doing_ajax() filters this constant: apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ). Could these tests use add_filter( 'wp_doing_ajax', '__return_true' ); instead of define()? Then they can drop @runInSeparateProcess and run in the normal process. The test framework restores hooks after each test. Each separate process starts WordPress again, so this keeps the suite faster.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 502c65c

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 tests match the helper’s behavior and use existing isolation mechanisms, with no blocking issues identified.

No open issues 🎉

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.

The checks cover Ajax preconditions, the protected-action list, and filter additions. Mukesh’s process-isolation feedback is addressed. Optional cleanup: the non-Ajax false-result test is subsumed by the adjacent filter-not-called test, which asserts the same result.

Unit-tests audit and Copilot review found no blockers. PHPCS passed. Each class passed separately, and the combined batch passed randomized single-site and multisite runs: 63 tests, 95 assertions each (seeds 65829 and 65830). CI is green on the reviewed commit.

AI review · gpt-6

wporg-sync pushed a commit that referenced this pull request Oct 19, 2026
Verify Ajax preconditions, built-in protected actions, unknown actions, and filtered additions. Use the Ajax filter to avoid unnecessary process isolation.

Developed in: #14473

Props shailu25, mukesh27.
See #65819.


git-svn-id: https://develop.svn.wordpress.org/trunk@64552 602fd350-edb4-49c9-b593-d223f7449a82
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Oct 19, 2026
Verify Ajax preconditions, built-in protected actions, unknown actions, and filtered additions. Use the Ajax filter to avoid unnecessary process isolation.

Developed in: WordPress/wordpress-develop#14473

Props shailu25, mukesh27.
See #65819.

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


git-svn-id: http://core.svn.wordpress.org/trunk@63702 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@lancewillett

Copy link
Copy Markdown
Member

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants