Skip to content

Tests: Check that private user fields are only returned to the user and to callers who can edit them - #16246

Open
hmbashar wants to merge 2 commits into
WordPress:trunkfrom
hmbashar:66549-users-ability-private-fields
Open

hmbashar wants to merge 2 commits into
WordPress:trunkfrom
hmbashar:66549-users-ability-private-fields

Conversation

@hmbashar

@hmbashar hmbashar commented Nov 25, 2026 •

Copy link
Copy Markdown

Summary

Extends two existing tests in Tests_Abilities_API_WpRegisterCoreUsersAbility, following the suggestions in comment 4 on the ticket: public and private user fields returned to callers with different permissions.

Behavior this protects

core/users-query returns a user's login, email, first and last name, nickname, locale, registration date and roles only to the user and to callers who can edit that user. Name, description, URL, link and slug are public.

The existing tests check email and roles for the different roles, but not first_name, last_name, nickname, locale, registered and username, which sit behind the same check in WP_Abilities_Users::format_user(). If that check were removed or widened, those fields would be exposed to any caller who can read a public author, and no test would fail.

  • test_roles_have_expected_field_visibility_for_another_public_author() now requests every private field of a public author, plus description and url, and checks that each private field is returned only to the administrator, while the public fields are returned to every role.
  • test_visible_current_user_reads_their_own_sensitive_fields() now checks that the user receives their own first and last name, nickname, locale and registration date.
  • A small helper sets the profile fields of the public author for both tests.

Testing

vendor/bin/phpunit --filter 'Tests_Abilities_API_WpRegisterCoreUsersAbility::'
vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_Abilities_API_WpRegisterCoreUsersAbility::'
vendor/bin/phpcs tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php

Mutation check (quality check only): 9 of 10 changes to class-wp-abilities-users.php were caught (the shared check set to true, the roles check removed, the edit_user check removed, and the value or check of nickname, locale, last_name, description, url and registered). The one that survives removes the "is the current user" check. It is an equivalent change, since a user can always edit themselves, so edit_user already returns true for them.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Written with AI and manually reviewed by me.


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.

…nd to callers who can edit them.

See #66549.
@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 hmbashar, 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.

@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 extra profile fields are worth covering. I checked by omitting the first name from an authorized response: the existing 108 tests still passed, and your new self and administrator cases caught it.

Could you fold the new field assertions into test_roles_have_expected_field_visibility_for_another_public_author() and test_visible_current_user_reads_their_own_sensitive_fields()? Those tests already set up these callers and query the same public author. That keeps the additional coverage and avoids maintaining a second role matrix.

AI review · gpt-6.2-astra

@lancewillett
lancewillett requested a review from gziolo November 25, 2026 17:50

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

There are no active review issues for the test-only change adding user-field visibility coverage.

No open issues 🎉

What changed in this PR

Adds regression coverage for core/users-query field visibility, checking that private user fields are returned only to the user or callers who can edit them.

Changes:

  • Adds a parameterized test covering the user, an administrator, an editor, a contributor, and a subscriber.
  • Checks the exact returned field set for each caller and selected public and private field values.
  • Changes tests only; production behavior is unchanged.
File Description
tests/​phpunit/​tests/​abilities-api/​wpRegisterCoreUsersAbility.php Adds the field-visibility test and caller data provider.

Review effort: Balanced

@hmbashar

Copy link
Copy Markdown
Author

Thanks @lancewillett, done in 051c49a. I removed the separate test and provider and moved the checks into test_roles_have_expected_field_visibility_for_another_public_author() and test_visible_current_user_reads_their_own_sensitive_fields(), with a small helper that sets the profile fields of the public author.

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 reported for the expanded user-field visibility tests.

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.

Thanks for folding the field checks into the existing role and self tests. I ran the class in random order on single-site (108 tests) and multisite (110). Returning an empty first name for the current user failed the revised assertion in both modes. The fixture stays inside the test transaction. The Abilities API review from @gziolo is still requested.

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