Skip to content

Tests: Add unit test coverage for user_can_access_admin_page() - #14372

Closed
huzaifaalmesbah wants to merge 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:tests/user-can-access-admin-page
Closed

huzaifaalmesbah wants to merge 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:tests/user-can-access-admin-page

Conversation

@huzaifaalmesbah

Copy link
Copy Markdown
Member

Description

Adds tests for user_can_access_admin_page(), which decides whether the current user may load the current admin screen and had no direct coverage.

The data provider sets up a menu, submenus and admin page hooks like those built in wp-admin/menu.php, then checks each path through the function with administrator, editor and subscriber users:

  • core submenu pages using the capability of their submenu item, and pages marked as not accessible in $_wp_submenu_nopriv;
  • plugin subpages: registered pages using their submenu capability, unregistered pages being refused, and pages marked as not accessible;
  • top-level plugin pages, a plugin page under a top-level plugin menu using its own capability, and a top-level plugin page in $_wp_menu_nopriv;
  • pages and plugin pages without a parent, which are accessible unless $_wp_menu_nopriv or any $_wp_submenu_nopriv entry lists them;
  • falling back to the parent menu item's capability when no submenu item matches the page, and allowing access when the parent is not in the menu.

Removing each guard or capability check in the function makes at least one dataset fail, except the $_wp_submenu_nopriv[ $pagenow ][ $pagenow ] check for pages without a parent, which the loop over all $_wp_submenu_nopriv keys that follows already covers.

All menu globals used by the function are saved in set_up() and restored in tear_down().

This is a test-only change; no production code is modified.

Testing Instructions

  1. Run the tests on single site:
    npm run test:php -- --filter 'Tests_Admin_Includes_Plugin_UserCanAccessAdminPage'
  2. Run them on multisite:
    npm run test:php -- --filter 'Tests_Admin_Includes_Plugin_UserCanAccessAdminPage' -c tests/phpunit/multisite.xml
  3. Run them in random order:
    npm run test:php -- --filter 'Tests_Admin_Includes_Plugin_UserCanAccessAdminPage' --order-by=random
  4. Run the plugins group:
    npm run test:php -- --group plugins
  5. Run PHPCS on the test file:
    node ./tools/local-env/scripts/docker.js run --rm php ./vendor/bin/phpcs tests/phpunit/tests/admin/includes/plugin/userCanAccessAdminPage.php

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Brainstorming edge-case scenarios and drafting test boilerplate; test architecture, assertions, and local PHPUnit verification were written and validated by me.

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

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.

The tests look good, and the global handling in set_up() and tear_down() is careful. One optional suggestion inline.

'editor',
true,
),
'top-level plugin page marked as not accessible' => array(

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.

Optional: this case returns false from the _wp_menu_nopriv check. It would also return false if the hook name did not match _registered_pages. A paired case without _wp_menu_nopriv that returns true would prove the test hits the intended branch:

'top-level plugin page not in a menu' => array(
	array(
		'pagenow'           => 'admin.php',
		'plugin_page'       => 'hidden-plugin',
		'_registered_pages' => array( 'admin_page_hidden-plugin' => true ),
	),
	'administrator',
	true,
),

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

Reviewed the tests against the implementation and existing coverage, including the unit-tests audit. No blocking findings. The affected classes passed locally in randomized order on single site (21 tests) and multisite (21 tests); PHPCS and current CI pass.

Optional follow-up: pair the hidden-plugin denial case with an accessible case lacking _wp_menu_nopriv, to isolate the denial branch.

AI review · gpt-6

wporg-sync pushed a commit that referenced this pull request Oct 17, 2026
Check capability and registration decisions for core submenus, plugin pages, hidden pages, and parent-menu fallbacks. Restore menu and request globals between cases.

Developed in: #14372

Props huzaifaalmesbah, mukesh27.
See #65819.


git-svn-id: https://develop.svn.wordpress.org/trunk@64464 602fd350-edb4-49c9-b593-d223f7449a82
@lancewillett

Copy link
Copy Markdown
Member

wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Oct 17, 2026
Check capability and registration decisions for core submenus, plugin pages, hidden pages, and parent-menu fallbacks. Restore menu and request globals between cases.

Developed in: WordPress/wordpress-develop#14372

Props huzaifaalmesbah, mukesh27.
See #65819.

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


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

None yet

Development

Successfully merging this pull request may close these issues.

3 participants