Make WordPress Core


Ignore:
Timestamp:
12/09/2019 09:01:13 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Menus: Avoid a PHP warning in add_submenu_page() when the same value is passed for both $parent_slug and $menu_slug parameters.

Props welcher, ispreview, ayeshrajans, NextScripts, adamsilverstein, garrett-eclipse, 123host.
Fixes #48599.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPlugin.php

    r46683 r46868  
    256256    }
    257257
     258    /**
     259     * Test that when a submenu has the same slug as a parent item, that it's just appended and ignores the priority.
     260     *
     261     * @ticket 48599
     262     */
     263    function test_priority_when_parent_slug_child_slug_are_the_same() {
     264        global $submenu, $menu;
     265
     266        // Reset menus.
     267        $submenu      = array();
     268        $menu         = array();
     269        $current_user = get_current_user_id();
     270        $admin_user   = self::factory()->user->create( array( 'role' => 'administrator' ) );
     271        wp_set_current_user( $admin_user );
     272        set_current_screen( 'dashboard' );
     273
     274        // Setup a menu with some items.
     275        add_menu_page( 'Main Menu', 'Main Menu', 'manage_options', 'main_slug', 'main_page_callback' );
     276        add_submenu_page( 'main_slug', 'SubMenu 1', 'SubMenu 1', 'manage_options', 'main_slug', 'submenu_callback_1', 1 );
     277        add_submenu_page( 'main_slug', 'SubMenu 2', 'SubMenu 2', 'manage_options', 'submenu_page2', 'submenu_callback_2', 2 );
     278        add_submenu_page( 'main_slug', 'SubMenu 3', 'SubMenu 3', 'manage_options', 'submenu_page3', 'submenu_callback_3', 3 );
     279
     280        // Clean up the temporary user.
     281        wp_set_current_user( $current_user );
     282        wp_delete_user( $admin_user );
     283
     284        // Verify the menu was inserted at the expected position.
     285        $this->assertSame( 'main_slug', $submenu['main_slug'][0][2] );
     286        $this->assertSame( 'submenu_page2', $submenu['main_slug'][1][2] );
     287        $this->assertSame( 'submenu_page3', $submenu['main_slug'][2][2] );
     288    }
     289
     290    /**
     291     * Passing a string as priority will fail.
     292     *
     293     * @ticket 48599
     294     */
     295    function test_passing_string_as_priority_fires_doing_it_wrong() {
     296        $this->setExpectedIncorrectUsage( 'add_submenu_page' );
     297        global $submenu, $menu;
     298
     299        // Reset menus.
     300        $submenu      = array();
     301        $menu         = array();
     302        $current_user = get_current_user_id();
     303        $admin_user   = self::factory()->user->create( array( 'role' => 'administrator' ) );
     304        wp_set_current_user( $admin_user );
     305        set_current_screen( 'dashboard' );
     306
     307        // Setup a menu with some items.
     308        add_menu_page( 'Main Menu', 'Main Menu', 'manage_options', 'main_slug', 'main_page_callback' );
     309        add_submenu_page( 'main_slug', 'SubMenu 1', 'SubMenu 1', 'manage_options', 'submenu_page_1', 'submenu_callback_1', '2' );
     310
     311        // Clean up the temporary user.
     312        wp_set_current_user( $current_user );
     313        wp_delete_user( $admin_user );
     314
     315        // Verify the menu was inserted at the expected position.
     316        $this->assertSame( 'submenu_page_1', $submenu['main_slug'][1][2] );
     317    }
     318
    258319    function test_is_plugin_active_true() {
    259320        activate_plugin( 'hello.php' );
Note: See TracChangeset for help on using the changeset viewer.