mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Menus: add a position argument to add_submenu_page and the helper functions that use it.
Add a position argument to the `add_submenu_page` function similar to the one already in `add_menu_page`. When adding sub menus enables setting the position in the sub menu where the item should appear. In addition, add the position argument to functions that call `add_submenu_page` under the hood: `add_management_page`, `add_options_page`, `add_theme_page`, `add_plugins_page`, `add_users_page`, `add_dashboard_page`, `add_posts_page`, `add_media_page`, `add_links_page`, `add_pages_page` and `add_comments_page`. Props welcher, birgire, alexvorn2. Fixes #39776. git-svn-id: https://develop.svn.wordpress.org/trunk@46197 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -57,6 +57,208 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase {
|
||||
wp_set_current_user( $current_user );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the priority parameter.
|
||||
*
|
||||
* @ticket 39776
|
||||
*
|
||||
* @covers ::add_submenu_page
|
||||
*
|
||||
* @param int $priority The position of the new item.
|
||||
* @param int $expected_position Where the new item is expected to appear.
|
||||
*
|
||||
* @dataProvider data_submenu_priority
|
||||
*/
|
||||
function test_submenu_priority( $priority, $expected_position ) {
|
||||
global $submenu;
|
||||
global $menu;
|
||||
$current_user = get_current_user_id();
|
||||
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
|
||||
wp_set_current_user( $admin_user );
|
||||
set_current_screen( 'dashboard' );
|
||||
|
||||
// Setup a menu with some items.
|
||||
$parent = add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' );
|
||||
foreach ( $this->submenus_to_add() as $menu_to_add ) {
|
||||
add_submenu_page( $parent, $menu_to_add[0], $menu_to_add[1], $menu_to_add[2], $menu_to_add[3], $menu_to_add[4] );
|
||||
}
|
||||
|
||||
// Insert the new page.
|
||||
add_submenu_page( $parent, 'New Page', 'New Page', 'manage_options', 'custom-position', 'custom_pos', $priority );
|
||||
wp_set_current_user( $current_user );
|
||||
|
||||
// Clean up the temporary user.
|
||||
wp_delete_user( $admin_user );
|
||||
|
||||
// Verify the menu was inserted at the expected position.
|
||||
$this->assertSame( 'custom-position', $submenu[ $parent ][ $expected_position ][2] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the priority parameter for menu helper functions.
|
||||
*
|
||||
* @ticket 39776
|
||||
*
|
||||
* @covers ::add_management_page
|
||||
* @covers ::add_options_page
|
||||
* @covers ::add_theme_page
|
||||
* @covers ::add_plugins_page
|
||||
* @covers ::add_users_page
|
||||
* @covers ::add_dashboard_page
|
||||
* @covers ::add_posts_page
|
||||
* @covers ::add_media_page
|
||||
* @covers ::add_links_page
|
||||
* @covers ::add_pages_page
|
||||
* @covers ::add_comments_page
|
||||
*
|
||||
* @param int $priority The position of the new item.
|
||||
* @param int $expected_position Where the new item is expected to appear.
|
||||
*
|
||||
* @dataProvider data_submenu_priority
|
||||
*/
|
||||
function test_submenu_helpers_priority( $priority, $expected_position ) {
|
||||
global $submenu;
|
||||
global $menu;
|
||||
|
||||
// Skip for multisite.
|
||||
if ( is_multisite() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset menus.
|
||||
$submenu = array();
|
||||
$menu = array();
|
||||
|
||||
$current_user = get_current_user_id();
|
||||
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
|
||||
wp_set_current_user( $admin_user );
|
||||
set_current_screen( 'dashboard' );
|
||||
|
||||
// Test the helper functions that use `add_submenu_page`. Each helper adds to a specific menu root.
|
||||
$helper_functions = array(
|
||||
array(
|
||||
'callback' => 'add_management_page',
|
||||
'menu_root' => 'tools.php',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_options_page',
|
||||
'menu_root' => 'options-general.php',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_theme_page',
|
||||
'menu_root' => 'themes.php',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_plugins_page',
|
||||
'menu_root' => 'plugins.php',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_users_page',
|
||||
'menu_root' => 'users.php',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_dashboard_page',
|
||||
'menu_root' => 'index.php',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_posts_page',
|
||||
'menu_root' => 'edit.php',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_media_page',
|
||||
'menu_root' => 'upload.php',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_links_page',
|
||||
'menu_root' => 'link-manager.php',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_pages_page',
|
||||
'menu_root' => 'edit.php?post_type=page',
|
||||
),
|
||||
array(
|
||||
'callback' => 'add_comments_page',
|
||||
'menu_root' => 'edit-comments.php',
|
||||
),
|
||||
);
|
||||
|
||||
$actual_positions = array();
|
||||
|
||||
foreach ( $helper_functions as $helper_function ) {
|
||||
|
||||
// Build up demo pages on the menu root.
|
||||
foreach ( $this->submenus_to_add() as $menu_to_add ) {
|
||||
add_menu_page( $menu_to_add[0], $menu_to_add[1], $menu_to_add[2], $helper_function['menu_root'], $helper_function['menu_root'] );
|
||||
}
|
||||
|
||||
$test = 'test_' . $helper_function['callback'];
|
||||
|
||||
// Call the helper function, passing the desired priority.
|
||||
call_user_func_array( $helper_function['callback'], array( $test, $test, 'manage_options', 'custom-position', '', $priority ) );
|
||||
|
||||
$actual_positions[ $test ] = $submenu[ $helper_function['menu_root'] ][ $expected_position ][2];
|
||||
}
|
||||
|
||||
// Clean up the temporary user.
|
||||
wp_delete_user( $admin_user );
|
||||
|
||||
foreach ( $actual_positions as $test => $actual_position ) {
|
||||
// Verify the menu was inserted at the expected position.
|
||||
$this->assertSame( 'custom-position', $actual_position, 'Menu not inserted at the expected position with ' . $test );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to store the menus that are to be added, so getting the length is programmatically done.
|
||||
*
|
||||
* @since 5.3.0
|
||||
*
|
||||
* @return array {
|
||||
* @type array {
|
||||
* @type string Page title.
|
||||
* @type string Menu_title.
|
||||
* @type string Capability.
|
||||
* @type string Menu slug.
|
||||
* @type string Function.
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
function submenus_to_add() {
|
||||
return array(
|
||||
array( 'Submenu Priority', 'Submenu Priority', 'manage_options', 'sub-page', '' ),
|
||||
array( 'Submenu Priority 2', 'Submenu Priority 2', 'manage_options', 'sub-page2', '' ),
|
||||
array( 'Submenu Priority 3', 'Submenu Priority 3', 'manage_options', 'sub-page3', '' ),
|
||||
array( 'Submenu Priority 4', 'Submenu Priority 4', 'manage_options', 'sub-page4', '' ),
|
||||
array( 'Submenu Priority 5', 'Submenu Priority 5', 'manage_options', 'sub-page5', '' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_submenu_helpers_priority().
|
||||
*
|
||||
* @since 5.3.0
|
||||
*
|
||||
* @return array {
|
||||
* @type array {
|
||||
* @type int|null Priority.
|
||||
* @type int Expected position.
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
function data_submenu_priority() {
|
||||
$menu_count = count( $this->submenus_to_add() );
|
||||
return array(
|
||||
array( null, $menu_count ), // Insert at the end of the menu if null is passed. Default behavior.
|
||||
array( 0, 0 ), // Insert at the beginning of the menu if 0 is passed.
|
||||
array( -1, 0 ), // Negative numbers are treated the same as passing 0.
|
||||
array( -7, 0 ), // Negative numbers are treated the same as passing 0.
|
||||
array( 1, 1 ), // Insert as the second item.
|
||||
array( 3, 3 ), // Insert as the 4th item.
|
||||
array( $menu_count, $menu_count ), // Numbers equal to the number of items are added at the end.
|
||||
array( 123456, $menu_count ), // Numbers higher than the number of items are added at the end.
|
||||
);
|
||||
}
|
||||
|
||||
function test_is_plugin_active_true() {
|
||||
activate_plugin( 'hello.php' );
|
||||
$test = is_plugin_active( 'hello.php' );
|
||||
|
||||
Reference in New Issue
Block a user