WordPress.org

Make WordPress Core

Changeset 39035


Ignore:
Timestamp:
10/30/2016 06:30:03 PM (4 years ago)
Author:
johnbillion
Message:

Role/Capability: Introduce capability tests for promoting, removing, and deleting users, and capability tests for editing comments.

Also introduces a shared fixture for a super admin user.

See #38236

File:
1 edited

Legend:

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

    r39016 r39035  
    99class Tests_User_Capabilities extends WP_UnitTestCase {
    1010
    11     protected static $users = array();
     11    protected static $users = array(
     12        'administrator' => null,
     13        'editor'        => null,
     14        'author'        => null,
     15        'contributor'   => null,
     16        'subscriber'    => null,
     17    );
     18    protected static $super_admin = null;
    1219
    1320    public static function wpSetUpBeforeClass( $factory ) {
     
    1926            'subscriber'    => $factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
    2027        );
     28        self::$super_admin = $factory->user->create_and_get( array( 'role' => 'contributor' ) );
     29        grant_super_admin( self::$super_admin->ID );
    2130    }
    2231
     
    522531    }
    523532
     533    /**
     534     * @dataProvider data_user_with_role_can_edit_own_post
     535     *
     536     * @param  string $role              User role name
     537     * @param  bool   $can_edit_own_post Can users with this role edit their own posts?
     538     */
     539    public function test_user_can_edit_comment_on_own_post( $role, $can_edit_own_post ) {
     540        $owner   = self::$users[ $role ];
     541        $post    = self::factory()->post->create_and_get( array(
     542            'post_author' => $owner->ID,
     543        ) );
     544        $comment = self::factory()->comment->create_and_get( array(
     545            'comment_post_ID' => $post->ID,
     546        ) );
     547
     548        $owner_can_edit = user_can( $owner->ID, 'edit_comment', $comment->comment_ID );
     549        $this->assertSame( $can_edit_own_post, $owner_can_edit );
     550    }
     551
     552    /**
     553     * @dataProvider data_user_with_role_can_edit_others_posts
     554     *
     555     * @param  string $role                 User role name
     556     * @param  bool   $can_edit_others_post Can users with this role edit others' posts?
     557     */
     558    public function test_user_can_edit_comment_on_others_post( $role, $can_edit_others_post ) {
     559        $user    = self::$users[ $role ];
     560        $owner   = self::factory()->user->create_and_get( array(
     561            'role' => 'editor',
     562        ) );
     563        $post    = self::factory()->post->create_and_get( array(
     564            'post_author' => $owner->ID,
     565        ) );
     566        $comment = self::factory()->comment->create_and_get( array(
     567            'comment_post_ID' => $post->ID,
     568        ) );
     569
     570        $user_can_edit = user_can( $user->ID, 'edit_comment', $comment->comment_ID );
     571        $this->assertSame( $can_edit_others_post, $user_can_edit );
     572    }
     573
     574    public function data_user_with_role_can_edit_own_post() {
     575        $data  = array();
     576        $caps  = $this->getPrimitiveCapsAndRoles();
     577
     578        foreach ( self::$users as $role => $null ) {
     579            $data[] = array(
     580                $role,
     581                in_array( $role, $caps['edit_published_posts'], true ),
     582            );
     583        }
     584
     585        return $data;
     586    }
     587
     588    public function data_user_with_role_can_edit_others_posts() {
     589        $data  = array();
     590        $caps  = $this->getPrimitiveCapsAndRoles();
     591
     592        foreach ( self::$users as $role => $null ) {
     593            $data[] = array(
     594                $role,
     595                in_array( $role, $caps['edit_others_posts'], true ),
     596            );
     597        }
     598
     599        return $data;
     600    }
     601
    524602    function test_super_admin_caps() {
    525603        if ( ! is_multisite() ) {
     
    528606        }
    529607        $caps = $this->getAllCapsAndRoles();
    530 
    531         $user = self::$users['administrator'];
    532         grant_super_admin( $user->ID );
     608        $user = self::$super_admin;
    533609
    534610        $this->assertTrue( is_super_admin( $user->ID ) );
     
    13461422    }
    13471423
     1424    public function test_only_admins_and_super_admins_can_remove_users() {
     1425        if ( is_multisite() ) {
     1426            $this->assertTrue( user_can( self::$super_admin->ID,        'remove_user', self::$users['subscriber']->ID ) );
     1427        }
     1428
     1429        $this->assertTrue( user_can( self::$users['administrator']->ID, 'remove_user', self::$users['subscriber']->ID ) );
     1430
     1431        $this->assertFalse( user_can( self::$users['editor']->ID,       'remove_user', self::$users['subscriber']->ID ) );
     1432        $this->assertFalse( user_can( self::$users['author']->ID,       'remove_user', self::$users['subscriber']->ID ) );
     1433        $this->assertFalse( user_can( self::$users['contributor']->ID,  'remove_user', self::$users['subscriber']->ID ) );
     1434        $this->assertFalse( user_can( self::$users['subscriber']->ID,   'remove_user', self::$users['subscriber']->ID ) );
     1435    }
     1436
     1437    public function test_only_super_admins_can_delete_users_on_multisite() {
     1438        if ( ! is_multisite() ) {
     1439            $this->markTestSkipped( 'Test only runs on multisite' );
     1440        }
     1441
     1442        $this->assertTrue( user_can( self::$super_admin->ID,             'delete_user', self::$users['subscriber']->ID ) );
     1443
     1444        $this->assertFalse( user_can( self::$users['administrator']->ID, 'delete_user', self::$users['subscriber']->ID ) );
     1445        $this->assertFalse( user_can( self::$users['editor']->ID,        'delete_user', self::$users['subscriber']->ID ) );
     1446        $this->assertFalse( user_can( self::$users['author']->ID,        'delete_user', self::$users['subscriber']->ID ) );
     1447        $this->assertFalse( user_can( self::$users['contributor']->ID,   'delete_user', self::$users['subscriber']->ID ) );
     1448        $this->assertFalse( user_can( self::$users['subscriber']->ID,    'delete_user', self::$users['subscriber']->ID ) );
     1449    }
     1450
     1451    public function test_only_admins_can_delete_users_on_single_site() {
     1452        if ( is_multisite() ) {
     1453            $this->markTestSkipped( 'Test does not run on multisite' );
     1454        }
     1455
     1456        $this->assertTrue( user_can( self::$users['administrator']->ID, 'delete_user', self::$users['subscriber']->ID ) );
     1457
     1458        $this->assertFalse( user_can( self::$users['editor']->ID,       'delete_user', self::$users['subscriber']->ID ) );
     1459        $this->assertFalse( user_can( self::$users['author']->ID,       'delete_user', self::$users['subscriber']->ID ) );
     1460        $this->assertFalse( user_can( self::$users['contributor']->ID,  'delete_user', self::$users['subscriber']->ID ) );
     1461        $this->assertFalse( user_can( self::$users['subscriber']->ID,   'delete_user', self::$users['subscriber']->ID ) );
     1462    }
     1463
     1464    public function test_only_admins_and_super_admins_can_promote_users() {
     1465        if ( is_multisite() ) {
     1466            $this->assertTrue( user_can( self::$super_admin->ID,              'promote_user', self::$users['subscriber']->ID ) );
     1467        }
     1468
     1469        $this->assertTrue( user_can( self::$users['administrator']->ID, 'promote_user', self::$users['subscriber']->ID ) );
     1470
     1471        $this->assertFalse( user_can( self::$users['editor']->ID,       'promote_user', self::$users['subscriber']->ID ) );
     1472        $this->assertFalse( user_can( self::$users['author']->ID,       'promote_user', self::$users['subscriber']->ID ) );
     1473        $this->assertFalse( user_can( self::$users['contributor']->ID,  'promote_user', self::$users['subscriber']->ID ) );
     1474        $this->assertFalse( user_can( self::$users['subscriber']->ID,   'promote_user', self::$users['subscriber']->ID ) );
     1475    }
     1476
    13481477    /**
    13491478     * @ticket 33694
     
    14051534        $user = self::$users['administrator'];
    14061535        $user->add_cap( 'manage_network_users' );
    1407         $super_admin = self::$users['subscriber'];
    1408         grant_super_admin( $super_admin->ID );
    14091536
    14101537        wp_set_current_user( $user->ID );
    14111538
    1412         $can_edit_user = current_user_can( 'edit_user', $super_admin->ID );
     1539        $can_edit_user = current_user_can( 'edit_user', self::$super_admin->ID );
    14131540
    14141541        $user->remove_cap( 'manage_network_users' );
    1415         revoke_super_admin( $super_admin->ID );
    14161542
    14171543        $this->assertFalse( $can_edit_user );
Note: See TracChangeset for help on using the changeset viewer.