Make WordPress Core

Ticket #54798: 54798.1.diff

File 54798.1.diff, 3.0 KB (added by kirtan95, 3 years ago)

Fixed a last minute change in unit test 😅

  • src/wp-admin/includes/plugin.php

    diff --git a/src/wp-admin/includes/plugin.php b/src/wp-admin/includes/plugin.php
    index c0ac17a881..cab7e4be99 100644
    a b function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $func 
    13291329                $position            = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ), -5 ) * 0.00001;
    13301330                $menu[ "$position" ] = $new_menu;
    13311331        } else {
     1332                if ( ! is_int( $position ) ) {
     1333                        _doing_it_wrong(
     1334                                __FUNCTION__,
     1335                                sprintf(
     1336                                /* translators: %s: add_submenu_page() */
     1337                                        __( 'The seventh parameter passed to %s should be an integer representing menu position.' ),
     1338                                        '<code>add_menu_page()</code>'
     1339                                ),
     1340                                '6.0.0'
     1341                        );
     1342                        // Check if the position is non-string(i.e. float) and convert it to string.
     1343                        if ( ! is_string( $position ) ) {
     1344                                $position = (string) $position;
     1345                        }
     1346                }
    13321347                $menu[ $position ] = $new_menu;
    13331348        }
    13341349
  • tests/phpunit/tests/admin/includesPlugin.php

    diff --git a/tests/phpunit/tests/admin/includesPlugin.php b/tests/phpunit/tests/admin/includesPlugin.php
    index 2fde9195fd..52a4b509e9 100644
    a b class Tests_Admin_IncludesPlugin extends WP_UnitTestCase { 
    296296        }
    297297
    298298        /**
    299          * Passing a string as position will fail.
     299         * Passing a string as position will fail in submenu.
    300300         *
    301301         * @ticket 48599
    302302         */
    303         public function test_passing_string_as_position_fires_doing_it_wrong() {
     303        public function test_passing_string_as_position_fires_doing_it_wrong_submenu() {
    304304                $this->setExpectedIncorrectUsage( 'add_submenu_page' );
    305305                global $submenu, $menu;
    306306
    class Tests_Admin_IncludesPlugin extends WP_UnitTestCase { 
    324324                $this->assertSame( 'submenu_page_1', $submenu['main_slug'][1][2] );
    325325        }
    326326
     327        /**
     328         * Passing a string as position will fail in menu.
     329         *
     330         * @ticket 48599
     331         */
     332        public function test_passing_string_as_position_fires_doing_it_wrong_menu() {
     333                $this->setExpectedIncorrectUsage( 'add_menu_page' );
     334                global $submenu, $menu;
     335
     336                // Reset menus.
     337                $submenu      = array();
     338                $menu         = array();
     339                $current_user = get_current_user_id();
     340                $admin_user   = self::factory()->user->create( array( 'role' => 'administrator' ) );
     341                wp_set_current_user( $admin_user );
     342                set_current_screen( 'dashboard' );
     343
     344                // Setup a menu with some items.
     345                add_menu_page( 'Main Menu', 'Main Menu', 'manage_options', 'main_slug', 'main_page_callback', 'icon_url', '1' );
     346                add_menu_page( 'Main Menu 1', 'Main Menu 1', 'manage_options', 'main1_slug', 'main1_page_callback', 'icon_url1', 1.5 );
     347
     348                // Clean up the temporary user.
     349                wp_set_current_user( $current_user );
     350                wp_delete_user( $admin_user );
     351
     352                // Verify the menu was inserted.
     353                $this->assertSame( 'main_slug', $menu[1][2] );
     354                // Verify the menu was inserted correctly on passing float as position.
     355                $this->assertSame( 'main1_slug', $menu['1.5'][2] );
     356        }
     357
    327358        public function test_is_plugin_active_true() {
    328359                activate_plugin( 'hello.php' );
    329360                $test = is_plugin_active( 'hello.php' );