WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/14/2015 05:09:58 PM (5 years ago)
Author:
johnbillion
Message:

Update links to the user profile editing screen in the admin toolbar when the current logged in user has no role on the current site. Covers single site and Multisite and introduces tests.

Fixes #25162

File:
1 edited

Legend:

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

    r25002 r34122  
    1616        parent::setUp();
    1717        $this->current_user = get_current_user_id();
    18         wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) );
    1918    }
    2019
     
    2827     */
    2928    function test_content_post_type() {
     29        wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) );
     30
    3031        register_post_type( 'content', array( 'show_in_admin_bar' => true ) );
    3132
     
    4546     */
    4647    function test_merging_existing_meta_values() {
     48        wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) );
     49
    4750        $admin_bar = new WP_Admin_Bar;
    4851
     
    6265        $this->assertEquals( array( 'class' => 'test-class', 'some-meta' => 'value' ), $node->meta );
    6366    }
     67
     68    /**
     69     * @ticket 25162
     70     */
     71    public function test_admin_bar_contains_correct_links_for_users_with_no_role() {
     72        if ( is_multisite() ) {
     73            $this->markTestSkipped( 'Test does not run in multisite' );
     74        }
     75
     76        $nobody = $this->factory->user->create( array( 'role' => '' ) );
     77        $this->assertFalse( user_can( $nobody, 'read' ) );
     78
     79        wp_set_current_user( $nobody );
     80
     81        $wp_admin_bar = $this->get_standard_admin_bar();
     82
     83        $node_site_name    = $wp_admin_bar->get_node( 'site-name' );
     84        $node_my_account   = $wp_admin_bar->get_node( 'my-account' );
     85        $node_user_info    = $wp_admin_bar->get_node( 'user-info' );
     86        $node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
     87
     88        // Site menu points to the home page instead of the admin URL
     89        $this->assertEquals( home_url( '/' ), $node_site_name->href );
     90
     91        // No profile links as the user doesn't have any permissions on the site
     92        $this->assertFalse( $node_my_account->href );
     93        $this->assertFalse( $node_user_info->href );
     94        $this->assertNull( $node_edit_profile );
     95
     96    }
     97
     98    /**
     99     * @ticket 25162
     100     */
     101    public function test_admin_bar_contains_correct_links_for_users_with_role() {
     102        if ( is_multisite() ) {
     103            $this->markTestSkipped( 'Test does not run in multisite' );
     104        }
     105
     106        $editor = $this->factory->user->create( array( 'role' => 'editor' ) );
     107        $this->assertTrue( user_can( $editor, 'read' ) );
     108
     109        wp_set_current_user( $editor );
     110
     111        $wp_admin_bar = $this->get_standard_admin_bar();
     112
     113        $node_site_name    = $wp_admin_bar->get_node( 'site-name' );
     114        $node_my_account   = $wp_admin_bar->get_node( 'my-account' );
     115        $node_user_info    = $wp_admin_bar->get_node( 'user-info' );
     116        $node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
     117
     118        // Site menu points to the admin URL
     119        $this->assertEquals( admin_url( '/' ), $node_site_name->href );
     120
     121        $profile_url = admin_url( 'profile.php' );
     122
     123        // Profile URLs point to profile.php
     124        $this->assertEquals( $profile_url, $node_my_account->href );
     125        $this->assertEquals( $profile_url, $node_user_info->href );
     126        $this->assertEquals( $profile_url, $node_edit_profile->href );
     127
     128    }
     129
     130    /**
     131     * @ticket 25162
     132     * @group multisite
     133     */
     134    public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_blog() {
     135        if ( ! is_multisite() ) {
     136            $this->markTestSkipped( 'Test only runs in multisite' );
     137        }
     138
     139        $admin  = $this->factory->user->create( array( 'role' => 'administrator' ) );
     140        $editor = $this->factory->user->create( array( 'role' => 'editor' ) );
     141
     142        $this->assertTrue( user_can( $admin, 'read' ) );
     143        $this->assertTrue( user_can( $editor, 'read' ) );
     144
     145        $new_blog_id = $this->factory->blog->create( array(
     146            'user_id' => $admin,
     147        ) );
     148
     149        $this->assertTrue( is_user_member_of_blog( $admin, $new_blog_id ) );
     150        $this->assertFalse( is_user_member_of_blog( $editor, $new_blog_id ) );
     151
     152        wp_set_current_user( $editor );
     153
     154        switch_to_blog( $new_blog_id );
     155
     156        $wp_admin_bar = $this->get_standard_admin_bar();
     157
     158        $node_site_name    = $wp_admin_bar->get_node( 'site-name' );
     159        $node_my_account   = $wp_admin_bar->get_node( 'my-account' );
     160        $node_user_info    = $wp_admin_bar->get_node( 'user-info' );
     161        $node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
     162
     163        // get primary blog
     164        $primary = get_active_blog_for_user( $editor );
     165        $this->assertInternalType( 'object', $primary );
     166
     167        // No Site menu as the user isn't a member of this blog
     168        $this->assertNull( $node_site_name );
     169
     170        $primary_profile_url = get_admin_url( $primary->blog_id, 'profile.php' );
     171
     172        // Ensure the user's primary blog is not the same as the main site
     173        $this->assertNotEquals( $primary_profile_url, admin_url( 'profile.php' ) );
     174
     175        // Profile URLs should go to the user's primary blog
     176        $this->assertEquals( $primary_profile_url, $node_my_account->href );
     177        $this->assertEquals( $primary_profile_url, $node_user_info->href );
     178        $this->assertEquals( $primary_profile_url, $node_edit_profile->href );
     179
     180        restore_current_blog();
     181
     182    }
     183
     184    /**
     185     * @ticket 25162
     186     * @group multisite
     187     */
     188    public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_network() {
     189        if ( ! is_multisite() ) {
     190            $this->markTestSkipped( 'Test only runs in multisite' );
     191        }
     192
     193        $admin  = $this->factory->user->create( array( 'role' => 'administrator' ) );
     194        $nobody = $this->factory->user->create( array( 'role' => '' ) );
     195
     196        $this->assertTrue( user_can( $admin, 'read' ) );
     197        $this->assertFalse( user_can( $nobody, 'read' ) );
     198
     199        $new_blog_id = $this->factory->blog->create( array(
     200            'user_id' => $admin,
     201        ) );
     202
     203        $this->assertTrue( is_user_member_of_blog( $admin, $new_blog_id ) );
     204        $this->assertFalse( is_user_member_of_blog( $nobody, $new_blog_id ) );
     205        $this->assertTrue( is_user_member_of_blog( $nobody, get_current_blog_id() ) );
     206
     207        // Remove `$nobody` from the current blog, so they're not a member of any blog
     208        $removed = remove_user_from_blog( $nobody, get_current_blog_id() );
     209
     210        $this->assertTrue( $removed );
     211        $this->assertFalse( is_user_member_of_blog( $nobody, get_current_blog_id() ) );
     212
     213        wp_set_current_user( $nobody );
     214
     215        switch_to_blog( $new_blog_id );
     216
     217        $wp_admin_bar = $this->get_standard_admin_bar();
     218
     219        $node_site_name    = $wp_admin_bar->get_node( 'site-name' );
     220        $node_my_account   = $wp_admin_bar->get_node( 'my-account' );
     221        $node_user_info    = $wp_admin_bar->get_node( 'user-info' );
     222        $node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
     223
     224        // get primary blog
     225        $primary = get_active_blog_for_user( $nobody );
     226        $this->assertNull( $primary );
     227
     228        // No Site menu as the user isn't a member of this site
     229        $this->assertNull( $node_site_name );
     230
     231        $user_profile_url = user_admin_url( 'profile.php' );
     232
     233        $this->assertNotEquals( $user_profile_url, admin_url( 'profile.php' ) );
     234
     235        // Profile URLs should go to the user's primary blog
     236        $this->assertEquals( $user_profile_url, $node_my_account->href );
     237        $this->assertEquals( $user_profile_url, $node_user_info->href );
     238        $this->assertEquals( $user_profile_url, $node_edit_profile->href );
     239
     240        restore_current_blog();
     241
     242    }
     243
     244    protected function get_standard_admin_bar() {
     245        global $wp_admin_bar;
     246
     247        _wp_admin_bar_init();
     248
     249        $this->assertTrue( is_admin_bar_showing() );
     250        $this->assertInstanceOf( 'WP_Admin_Bar', $wp_admin_bar );
     251
     252        do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
     253
     254        return $wp_admin_bar;
     255    }
     256
    64257}
Note: See TracChangeset for help on using the changeset viewer.