Make WordPress Core


Ignore:
Timestamp:
09/30/2024 11:03:51 AM (21 months ago)
Author:
johnbillion
Message:

Role/Capability: Introduce the user_can_for_blog() function.

This complements the existing user capability checking functions and enables checking a capability of any user on any site on a Multisite network.

Props tmanoilov, rajinsharwar, n8finch, johnbillion

Fixes #45197

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/capabilities.php

    r59122 r59123  
    16541654    }
    16551655
     1656    /**
     1657     * @group can_for_blog
     1658     */
    16561659    public function test_current_user_can_for_blog() {
    16571660        global $wpdb;
     
    16631666        $this->assertTrue( current_user_can_for_blog( get_current_blog_id(), 'edit_posts' ) );
    16641667        $this->assertFalse( current_user_can_for_blog( get_current_blog_id(), 'foo_the_bar' ) );
     1668
    16651669        if ( ! is_multisite() ) {
    16661670            $this->assertTrue( current_user_can_for_blog( 12345, 'edit_posts' ) );
     1671            $this->assertFalse( current_user_can_for_blog( 12345, 'foo_the_bar' ) );
    16671672            return;
    16681673        }
     
    16731678
    16741679        $blog_id = self::factory()->blog->create( array( 'user_id' => $user->ID ) );
     1680
     1681        $this->assertNotWPError( $blog_id );
    16751682        $this->assertTrue( current_user_can_for_blog( $blog_id, 'edit_posts' ) );
    16761683        $this->assertFalse( current_user_can_for_blog( $blog_id, 'foo_the_bar' ) );
    16771684
     1685        $another_blog_id = self::factory()->blog->create( array( 'user_id' => self::$users['author']->ID ) );
     1686
     1687        $this->assertNotWPError( $another_blog_id );
     1688
     1689        // Verify the user doesn't have a capability
     1690        $this->assertFalse( current_user_can_for_blog( $another_blog_id, 'edit_posts' ) );
     1691
     1692        // Add the current user to the site
     1693        add_user_to_blog( $another_blog_id, $user->ID, 'author' );
     1694
     1695        // Verify they now have the capability
     1696        $this->assertTrue( current_user_can_for_blog( $another_blog_id, 'edit_posts' ) );
     1697
    16781698        wp_set_current_user( $old_uid );
     1699    }
     1700
     1701    /**
     1702     * @group can_for_blog
     1703     */
     1704    public function test_user_can_for_blog() {
     1705        $user = self::$users['editor'];
     1706
     1707        $this->assertTrue( user_can_for_blog( $user->ID, get_current_blog_id(), 'edit_posts' ) );
     1708        $this->assertFalse( user_can_for_blog( $user->ID, get_current_blog_id(), 'foo_the_bar' ) );
     1709
     1710        if ( ! is_multisite() ) {
     1711            $this->assertTrue( user_can_for_blog( $user->ID, 12345, 'edit_posts' ) );
     1712            $this->assertFalse( user_can_for_blog( $user->ID, 12345, 'foo_the_bar' ) );
     1713            return;
     1714        }
     1715
     1716        $blog_id = self::factory()->blog->create( array( 'user_id' => $user->ID ) );
     1717
     1718        $this->assertNotWPError( $blog_id );
     1719        $this->assertTrue( user_can_for_blog( $user->ID, $blog_id, 'edit_posts' ) );
     1720        $this->assertFalse( user_can_for_blog( $user->ID, $blog_id, 'foo_the_bar' ) );
     1721
     1722        $author = self::$users['author'];
     1723
     1724        // Verify another user doesn't have a capability
     1725        $this->assertFalse( is_user_member_of_blog( $author->ID, $blog_id ) );
     1726        $this->assertFalse( user_can_for_blog( $author->ID, $blog_id, 'edit_posts' ) );
     1727
     1728        // Add the author to the site
     1729        add_user_to_blog( $blog_id, $author->ID, 'author' );
     1730
     1731        // Verify they now have the capability
     1732        $this->assertTrue( is_user_member_of_blog( $author->ID, $blog_id ) );
     1733        $this->assertTrue( user_can_for_blog( $author->ID, $blog_id, 'edit_posts' ) );
     1734
     1735        // Verify the user doesn't have a capability for a non-existent site
     1736        $this->assertFalse( user_can_for_blog( $user->ID, -1, 'edit_posts' ) );
    16791737    }
    16801738
Note: See TracChangeset for help on using the changeset viewer.