WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/24/2017 04:28:30 PM (4 years ago)
Author:
flixos90
Message:

Multisite: Show menu items in My Sites > Network Admin admin bar menu based on more precise capability checks.

Previously the menu items would all be displayed solely based on the manage_network capability. This change provides parity with the network admin menu.

Unit tests for the network admin menu functionality of wp_admin_bar_my_sites_menu() have been added.

Props chandrapatel.
Fixes #39082.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/adminbar.php

    r39918 r39948  
    584584        $this->assertNotContains( 'changeset_uuid', $query_params['url'] );
    585585    }
     586
     587    /**
     588     * @ticket 39082
     589     */
     590    public function test_my_sites_network_menu_for_regular_user() {
     591        if ( ! is_multisite() ) {
     592            $this->markTestSkipped( 'Test only runs in multisite' );
     593        }
     594
     595        wp_set_current_user( self::$editor_id );
     596
     597        $wp_admin_bar = $this->get_standard_admin_bar();
     598
     599        $nodes = $wp_admin_bar->get_nodes();
     600        foreach ( $this->get_my_sites_network_menu_items() as $id => $cap ) {
     601            $this->assertFalse( isset( $nodes[ $id ] ), sprintf( 'Menu item %s must not display for a regular user.', $id ) );
     602        }
     603    }
     604
     605    /**
     606     * @ticket 39082
     607     */
     608    public function test_my_sites_network_menu_for_super_admin() {
     609        if ( ! is_multisite() ) {
     610            $this->markTestSkipped( 'Test only runs in multisite' );
     611        }
     612
     613        wp_set_current_user( self::$editor_id );
     614
     615        grant_super_admin( self::$editor_id );
     616        $wp_admin_bar = $this->get_standard_admin_bar();
     617        revoke_super_admin( self::$editor_id );
     618
     619        $nodes = $wp_admin_bar->get_nodes();
     620        foreach ( $this->get_my_sites_network_menu_items() as $id => $cap ) {
     621            $this->assertTrue( isset( $nodes[ $id ] ), sprintf( 'Menu item %s must display for a super admin.', $id ) );
     622        }
     623    }
     624
     625    /**
     626     * @ticket 39082
     627     */
     628    public function test_my_sites_network_menu_for_regular_user_with_network_caps() {
     629        global $current_user;
     630
     631        if ( ! is_multisite() ) {
     632            $this->markTestSkipped( 'Test only runs in multisite' );
     633        }
     634
     635        $network_user_caps = array( 'manage_network', 'manage_network_themes', 'manage_network_plugins' );
     636
     637        wp_set_current_user( self::$editor_id );
     638
     639        foreach ( $network_user_caps as $network_cap ) {
     640            $current_user->add_cap( $network_cap );
     641        }
     642        $wp_admin_bar = $this->get_standard_admin_bar();
     643        foreach ( $network_user_caps as $network_cap ) {
     644            $current_user->remove_cap( $network_cap );
     645        }
     646
     647        $nodes = $wp_admin_bar->get_nodes();
     648        foreach ( $this->get_my_sites_network_menu_items() as $id => $cap ) {
     649            if ( in_array( $cap, $network_user_caps ) ) {
     650                $this->assertTrue( isset( $nodes[ $id ] ), sprintf( 'Menu item %1$s must display for a user with the %2$s cap.', $id, $cap ) );
     651            } else {
     652                $this->assertFalse( isset( $nodes[ $id ] ), sprintf( 'Menu item %1$s must not display for a user without the %2$s cap.', $id, $cap ) );
     653            }
     654        }
     655    }
     656
     657    private function get_my_sites_network_menu_items() {
     658        return array(
     659            'my-sites-super-admin' => 'manage_network',
     660            'network-admin'        => 'manage_network',
     661            'network-admin-d'      => 'manage_network',
     662            'network-admin-s'      => 'manage_sites',
     663            'network-admin-u'      => 'manage_network_users',
     664            'network-admin-t'      => 'manage_network_themes',
     665            'network-admin-p'      => 'manage_network_plugins',
     666            'network-admin-o'      => 'manage_network_options',
     667        );
     668    }
    586669}
Note: See TracChangeset for help on using the changeset viewer.