Skip to content

Tests: Add unit tests for add_menu_page(), add_submenu_page() and menu_page_url() - #15720

Open
hmbashar wants to merge 1 commit into
WordPress:trunkfrom
hmbashar:66557-admin-menu-tests
Open

hmbashar wants to merge 1 commit into
WordPress:trunkfrom
hmbashar:66557-admin-menu-tests

Conversation

@hmbashar

@hmbashar hmbashar commented Nov 6, 2026

Copy link
Copy Markdown

Summary

Adds tests for add_menu_page(), add_submenu_page() and menu_page_url(). The positions of submenus, the helper functions (add_options_page() and the others), wp_admin_page_exists() and the URLs of a few pages are already tested in includesPlugin.php, so these tests add the structure of the menus and the cases that were missing.

addMenuPage.php

  • add_menu_page() adds the menu with all its keys and the hook name, with the default icon or with an icon (dashicon, data URI, URL with its scheme, none), and makes a slug in the plugins directory relative.
  • It records the page in $admin_page_hooks, $_registered_pages and $_parent_pages, and hooks the callback only for a user who can access the page.
  • A menu without a position is added last, a menu is added at its position without replacing the menu that has it (the collision avoider), and a position that is not a number is reported and ignored.
  • add_submenu_page() starts the submenu with the parent menu (only when the user can access it), makes the slugs relative, uses the real file of a parent, records a page that the user cannot access, and registers the page and its callback.
  • Its position: not a number, zero, negative, and after the end.

menuPageUrl.php

  • menu_page_url() outputs the URL by default and not when $display is false, returns an empty string for a page that does not exist, adds the page to the file of the parent, uses admin.php for a parent that is a page, and escapes the URL.

Testing

vendor/bin/phpunit --filter 'Tests_Admin_Includes_Plugin_(AddMenuPage|MenuPageUrl)::'
vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_Admin_Includes_Plugin_(AddMenuPage|MenuPageUrl)::'
vendor/bin/phpcs tests/phpunit/tests/admin/includes/plugin/addMenuPage.php tests/phpunit/tests/admin/includes/plugin/menuPageUrl.php

Mutation check: 24 changes to the three functions (the slug, the icon, the callback and its capability, the collision avoider, the registries, the real parent file, the pages that the user cannot access, the parent menu copy, the positions and the URL) were each caught by a test, on single-site and multisite.

One change was not caught and is equivalent: in add_menu_page(), $position = null; after the notice about a position that is not a number. The condition that follows treats a position that is not numeric like null.

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

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.

@github-actions

github-actions Bot commented Nov 6, 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.

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 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 are identified in the added PHPUnit coverage for admin menu registration and URL generation.

No open issues 🎉

What changed in this PR

Adds PHPUnit coverage for WordPress admin menu registration and URL generation, complementing the existing tests in includesPlugin.php. No production code is changed.

Changes:

  • Tests menu structure, icons, slug normalization, permissions, callbacks, and page registration.
  • Covers menu position handling, collisions, and submenu parent behavior.
  • Tests URL output, missing pages, parent routing, and escaping.
File Description
tests/​phpunit/​tests/​admin/​includes/​plugin/​menuPageUrl.php Adds tests for menu page URL generation and output.
tests/​phpunit/​tests/​admin/​includes/​plugin/​addMenuPage.php Adds tests for top-level menu and submenu registration and positioning.

Review effort: Balanced

@huzaifaalmesbah huzaifaalmesbah 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 patch, @hmbashar! Excellent coverage for add_menu_page(), add_submenu_page(), and menu_page_url().

The global menu state backup and teardown ($menu, $submenu, $_parent_pages, $_registered_pages) are very well-handled, preventing test pollution. All 96 CI checks pass.

Looks great to me!

AI Review: Gemini 3.8 Flash

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