WordPress.org

Make WordPress Core

Changeset 32812


Ignore:
Timestamp:
06/17/2015 12:04:08 AM (4 years ago)
Author:
johnbillion
Message:

Comprehensive unit tests for every available capability, and for all roles.

Fixes #32394.

File:
1 edited

Legend:

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

    r32177 r32812  
    3939    }
    4040
    41     // test the default roles
    42 
    43     function test_user_administrator() {
    44         $id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    45         $user = new WP_User($id);
    46         $this->assertTrue($user->exists(), "Problem getting user $id");
    47 
    48         // make sure the role name is correct
    49         $this->assertEquals(array('administrator'), $user->roles);
    50 
    51         // check a few of the main capabilities
    52         $this->assertTrue($user->has_cap('switch_themes'));
    53         $this->assertTrue($user->has_cap('list_users'));
    54         $this->assertTrue($user->has_cap('manage_options'));
    55         $this->assertTrue($user->has_cap('level_10'));
    56     }
    57 
    58     function test_user_editor() {
    59         $id = $this->factory->user->create( array( 'role' => 'editor' ) );
    60         $user = new WP_User($id);
    61         $this->assertTrue($user->exists(), "Problem getting user $id");
    62 
    63         // make sure the role name is correct
    64         $this->assertEquals(array('editor'), $user->roles);
    65 
    66         // check a few of the main capabilities
    67         $this->assertTrue($user->has_cap('moderate_comments'));
    68         $this->assertTrue($user->has_cap('manage_categories'));
    69         $this->assertTrue($user->has_cap('upload_files'));
    70         $this->assertTrue($user->has_cap('level_7'));
    71 
    72         // and a few capabilities this user doesn't have
    73         $this->assertFalse($user->has_cap('switch_themes'));
    74         $this->assertFalse($user->has_cap('edit_users'));
    75         $this->assertFalse($user->has_cap('level_8'));
    76     }
    77 
    78     function test_user_author() {
    79         $id = $this->factory->user->create( array( 'role' => 'author' ) );
    80         $user = new WP_User($id);
    81         $this->assertTrue($user->exists(), "Problem getting user $id");
    82 
    83         // make sure the role name is correct
    84         $this->assertEquals(array('author'), $user->roles);
    85 
    86         // check a few of the main capabilities
    87         $this->assertTrue($user->has_cap('edit_posts'));
    88         $this->assertTrue($user->has_cap('edit_published_posts'));
    89         $this->assertTrue($user->has_cap('upload_files'));
    90         $this->assertTrue($user->has_cap('level_2'));
    91 
    92         // and a few capabilities this user doesn't have
    93         $this->assertFalse($user->has_cap('moderate_comments'));
    94         $this->assertFalse($user->has_cap('manage_categories'));
    95         $this->assertFalse($user->has_cap('level_3'));
    96     }
    97 
    98     function test_user_contributor() {
    99         $id = $this->factory->user->create( array( 'role' => 'contributor' ) );
    100         $user = new WP_User($id);
    101         $this->assertTrue($user->exists(), "Problem getting user $id");
    102 
    103         // make sure the role name is correct
    104         $this->assertEquals(array('contributor'), $user->roles);
    105 
    106         // check a few of the main capabilities
    107         $this->assertTrue($user->has_cap('edit_posts'));
    108         $this->assertTrue($user->has_cap('read'));
    109         $this->assertTrue($user->has_cap('level_1'));
    110         $this->assertTrue($user->has_cap('level_0'));
    111 
    112         // and a few capabilities this user doesn't have
    113         $this->assertFalse($user->has_cap('upload_files'));
    114         $this->assertFalse($user->has_cap('edit_published_posts'));
    115         $this->assertFalse($user->has_cap('level_2'));
    116     }
    117 
    118     function test_user_subscriber() {
    119         $id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    120         $user = new WP_User($id);
    121         $this->assertTrue($user->exists(), "Problem getting user $id");
    122 
    123         // make sure the role name is correct
    124         $this->assertEquals(array('subscriber'), $user->roles);
    125 
    126         // check a few of the main capabilities
    127         $this->assertTrue($user->has_cap('read'));
    128         $this->assertTrue($user->has_cap('level_0'));
    129 
    130         // and a few capabilities this user doesn't have
    131         $this->assertFalse($user->has_cap('upload_files'));
    132         $this->assertFalse($user->has_cap('edit_posts'));
    133         $this->assertFalse($user->has_cap('level_1'));
     41    protected function _getSingleSiteCaps() {
     42        return array(
     43
     44            'unfiltered_html'        => array( 'administrator', 'editor' ),
     45
     46            'manage_network'         => array(),
     47            'manage_sites'           => array(),
     48            'manage_network_users'   => array(),
     49            'manage_network_plugins' => array(),
     50            'manage_network_themes'  => array(),
     51            'manage_network_options' => array(),
     52
     53            'activate_plugins'       => array( 'administrator' ),
     54            'create_users'           => array( 'administrator' ),
     55            'delete_plugins'         => array( 'administrator' ),
     56            'delete_themes'          => array( 'administrator' ),
     57            'delete_users'           => array( 'administrator' ),
     58            'edit_files'             => array( 'administrator' ),
     59            'edit_plugins'           => array( 'administrator' ),
     60            'edit_themes'            => array( 'administrator' ),
     61            'edit_users'             => array( 'administrator' ),
     62            'install_plugins'        => array( 'administrator' ),
     63            'install_themes'         => array( 'administrator' ),
     64            'update_core'            => array( 'administrator' ),
     65            'update_plugins'         => array( 'administrator' ),
     66            'update_themes'          => array( 'administrator' ),
     67            'edit_theme_options'     => array( 'administrator' ),
     68            'export'                 => array( 'administrator' ),
     69            'import'                 => array( 'administrator' ),
     70            'list_users'             => array( 'administrator' ),
     71            'manage_options'         => array( 'administrator' ),
     72            'promote_users'          => array( 'administrator' ),
     73            'remove_users'           => array( 'administrator' ),
     74            'switch_themes'          => array( 'administrator' ),
     75            'edit_dashboard'         => array( 'administrator' ),
     76
     77            'moderate_comments'      => array( 'administrator', 'editor' ),
     78            'manage_categories'      => array( 'administrator', 'editor' ),
     79            'edit_others_posts'      => array( 'administrator', 'editor' ),
     80            'edit_pages'             => array( 'administrator', 'editor' ),
     81            'edit_others_pages'      => array( 'administrator', 'editor' ),
     82            'edit_published_pages'   => array( 'administrator', 'editor' ),
     83            'publish_pages'          => array( 'administrator', 'editor' ),
     84            'delete_pages'           => array( 'administrator', 'editor' ),
     85            'delete_others_posts'    => array( 'administrator', 'editor' ),
     86            'delete_published_pages' => array( 'administrator', 'editor' ),
     87            'delete_others_posts'    => array( 'administrator', 'editor' ),
     88            'delete_private_posts'   => array( 'administrator', 'editor' ),
     89            'edit_private_posts'     => array( 'administrator', 'editor' ),
     90            'read_private_posts'     => array( 'administrator', 'editor' ),
     91            'delete_private_pages'   => array( 'administrator', 'editor' ),
     92            'edit_private_pages'     => array( 'administrator', 'editor' ),
     93            'read_private_pages'     => array( 'administrator', 'editor' ),
     94
     95            'edit_published_posts'   => array( 'administrator', 'editor', 'author' ),
     96            'upload_files'           => array( 'administrator', 'editor', 'author' ),
     97            'publish_posts'          => array( 'administrator', 'editor', 'author' ),
     98            'delete_published_posts' => array( 'administrator', 'editor', 'author' ),
     99
     100            'edit_posts'             => array( 'administrator', 'editor', 'author', 'contributor' ),
     101            'delete_posts'           => array( 'administrator', 'editor', 'author', 'contributor' ),
     102
     103            'read'                   => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
     104
     105            'level_10'               => array( 'administrator' ),
     106            'level_9'                => array( 'administrator' ),
     107            'level_8'                => array( 'administrator' ),
     108            'level_7'                => array( 'administrator', 'editor' ),
     109            'level_6'                => array( 'administrator', 'editor' ),
     110            'level_5'                => array( 'administrator', 'editor' ),
     111            'level_4'                => array( 'administrator', 'editor' ),
     112            'level_3'                => array( 'administrator', 'editor' ),
     113            'level_2'                => array( 'administrator', 'editor', 'author' ),
     114            'level_1'                => array( 'administrator', 'editor', 'author', 'contributor' ),
     115            'level_0'                => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
     116
     117        );
     118
     119    }
     120
     121    protected function _getMultiSiteCaps() {
     122        return array(
     123
     124            'unfiltered_html'        => array(),
     125
     126            'manage_network'         => array(),
     127            'manage_sites'           => array(),
     128            'manage_network_users'   => array(),
     129            'manage_network_plugins' => array(),
     130            'manage_network_themes'  => array(),
     131            'manage_network_options' => array(),
     132            'activate_plugins'       => array(),
     133            'create_users'           => array(),
     134            'delete_plugins'         => array(),
     135            'delete_themes'          => array(),
     136            'delete_users'           => array(),
     137            'edit_files'             => array(),
     138            'edit_plugins'           => array(),
     139            'edit_themes'            => array(),
     140            'edit_users'             => array(),
     141            'install_plugins'        => array(),
     142            'install_themes'         => array(),
     143            'update_core'            => array(),
     144            'update_plugins'         => array(),
     145            'update_themes'          => array(),
     146
     147            'edit_theme_options'     => array( 'administrator' ),
     148            'export'                 => array( 'administrator' ),
     149            'import'                 => array( 'administrator' ),
     150            'list_users'             => array( 'administrator' ),
     151            'manage_options'         => array( 'administrator' ),
     152            'promote_users'          => array( 'administrator' ),
     153            'remove_users'           => array( 'administrator' ),
     154            'switch_themes'          => array( 'administrator' ),
     155            'edit_dashboard'         => array( 'administrator' ),
     156
     157            'moderate_comments'      => array( 'administrator', 'editor' ),
     158            'manage_categories'      => array( 'administrator', 'editor' ),
     159            'edit_others_posts'      => array( 'administrator', 'editor' ),
     160            'edit_pages'             => array( 'administrator', 'editor' ),
     161            'edit_others_pages'      => array( 'administrator', 'editor' ),
     162            'edit_published_pages'   => array( 'administrator', 'editor' ),
     163            'publish_pages'          => array( 'administrator', 'editor' ),
     164            'delete_pages'           => array( 'administrator', 'editor' ),
     165            'delete_others_posts'    => array( 'administrator', 'editor' ),
     166            'delete_published_pages' => array( 'administrator', 'editor' ),
     167            'delete_others_posts'    => array( 'administrator', 'editor' ),
     168            'delete_private_posts'   => array( 'administrator', 'editor' ),
     169            'edit_private_posts'     => array( 'administrator', 'editor' ),
     170            'read_private_posts'     => array( 'administrator', 'editor' ),
     171            'delete_private_pages'   => array( 'administrator', 'editor' ),
     172            'edit_private_pages'     => array( 'administrator', 'editor' ),
     173            'read_private_pages'     => array( 'administrator', 'editor' ),
     174
     175            'edit_published_posts'   => array( 'administrator', 'editor', 'author' ),
     176            'upload_files'           => array( 'administrator', 'editor', 'author' ),
     177            'publish_posts'          => array( 'administrator', 'editor', 'author' ),
     178            'delete_published_posts' => array( 'administrator', 'editor', 'author' ),
     179
     180            'edit_posts'             => array( 'administrator', 'editor', 'author', 'contributor' ),
     181            'delete_posts'           => array( 'administrator', 'editor', 'author', 'contributor' ),
     182
     183            'read'                   => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
     184
     185            'level_10'               => array( 'administrator' ),
     186            'level_9'                => array( 'administrator' ),
     187            'level_8'                => array( 'administrator' ),
     188            'level_7'                => array( 'administrator', 'editor' ),
     189            'level_6'                => array( 'administrator', 'editor' ),
     190            'level_5'                => array( 'administrator', 'editor' ),
     191            'level_4'                => array( 'administrator', 'editor' ),
     192            'level_3'                => array( 'administrator', 'editor' ),
     193            'level_2'                => array( 'administrator', 'editor', 'author' ),
     194            'level_1'                => array( 'administrator', 'editor', 'author', 'contributor' ),
     195            'level_0'                => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
     196
     197        );
     198
     199    }
     200
     201    protected function getCapsAndRoles() {
     202        if ( is_multisite() ) {
     203            return $this->_getMultiSiteCaps();
     204        } else {
     205            return $this->_getSingleSiteCaps();
     206        }
     207    }
     208
     209    // test the tests
     210    function test_single_and_multisite_cap_tests_match() {
     211        $single = $this->_getSingleSiteCaps();
     212        $multi  = $this->_getMultiSiteCaps();
     213        $this->assertEquals( array_keys( $single ), array_keys( $multi ) );
     214    }
     215
     216    // test the default roles and caps
     217    function test_all_roles_and_caps() {
     218        $users = array(
     219            'administrator' => $this->factory->user->create_and_get( array( 'role' => 'administrator' ) ),
     220            'editor'        => $this->factory->user->create_and_get( array( 'role' => 'editor' ) ),
     221            'author'        => $this->factory->user->create_and_get( array( 'role' => 'author' ) ),
     222            'contributor'   => $this->factory->user->create_and_get( array( 'role' => 'contributor' ) ),
     223            'subscriber'    => $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
     224        );
     225        $caps = $this->getCapsAndRoles();
     226
     227        foreach ( $users as $role => $user ) {
     228
     229            // make sure the user is valid
     230            $this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
     231
     232            // make sure the role name is correct
     233            $this->assertEquals( array( $role ), $user->roles, "User should only have the {$role} role" );
     234
     235            foreach ( $caps as $cap => $roles ) {
     236                if ( in_array( $role, $roles, true ) ) {
     237                    $this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
     238                    $this->assertTrue( user_can( $user, $cap ), "User with the {$role} role should have the {$cap} capability" );
     239                } else {
     240                    $this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
     241                    $this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
     242                }
     243            }
     244
     245        }
     246    }
     247
     248    // special case for the link manager
     249    function test_link_manager_caps() {
     250        $users = array(
     251            'administrator' => $this->factory->user->create_and_get( array( 'role' => 'administrator' ) ),
     252            'editor'        => $this->factory->user->create_and_get( array( 'role' => 'editor' ) ),
     253            'author'        => $this->factory->user->create_and_get( array( 'role' => 'author' ) ),
     254            'contributor'   => $this->factory->user->create_and_get( array( 'role' => 'contributor' ) ),
     255            'subscriber'    => $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
     256        );
     257        $caps = array(
     258            'manage_links' => array( 'administrator', 'editor' ),
     259        );
     260
     261        $this->assertSame( '0', get_option( 'link_manager_enabled' ) );
     262
     263        // no-one should have access to the link manager by default
     264        foreach ( $users as $role => $user ) {
     265            foreach ( $caps as $cap => $roles ) {
     266                $this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
     267                $this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
     268            }
     269        }
     270
     271        update_option( 'link_manager_enabled', '1' );
     272        $this->assertSame( '1', get_option( 'link_manager_enabled' ) );
     273
     274        foreach ( $users as $role => $user ) {
     275            foreach ( $caps as $cap => $roles ) {
     276                if ( in_array( $role, $roles, true ) ) {
     277                    $this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
     278                    $this->assertTrue( user_can( $user, $cap ), "User with the {$role} role should have the {$cap} capability" );
     279                } else {
     280                    $this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
     281                    $this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
     282                }
     283            }
     284        }
     285
     286        update_option( 'link_manager_enabled', '0' );
     287        $this->assertSame( '0', get_option( 'link_manager_enabled' ) );
     288
     289    }
     290
     291    function test_super_admin_caps() {
     292        if ( ! is_multisite() ) {
     293            $this->markTestSkipped( 'Test only runs in multisite' );
     294            return;
     295        }
     296        $caps = $this->getCapsAndRoles();
     297
     298        $user = $this->factory->user->create_and_get( array( 'role' => 'administrator' ) );
     299        grant_super_admin( $user->ID );
     300
     301        $this->assertTrue( is_super_admin( $user->ID ) );
     302
     303        foreach ( $caps as $cap => $roles ) {
     304            $this->assertTrue( $user->has_cap( $cap ), "Super Admins should have the {$cap} capability" );
     305            $this->assertTrue( user_can( $user, $cap ), "Super Admins should have the {$cap} capability" );
     306        }
    134307    }
    135308
    136309    // a role that doesn't exist
    137310    function test_bogus_role() {
    138         _disable_wp_die();
    139         $id = $this->factory->user->create( array( 'role' => rand_str() ) );
    140         $user = new WP_User($id);
    141         $this->assertTrue($user->exists(), "Problem getting user $id");
    142 
    143         // user has no role and no capabilities
    144         $this->assertEquals(array(), $user->roles);
    145         $this->assertFalse($user->has_cap('level_0'));
    146         _enable_wp_die();
     311        $user = $this->factory->user->create_and_get( array( 'role' => 'invalid_role' ) );
     312
     313        // make sure the user is valid
     314        $this->assertTrue( $user->exists(), "User does not exist" );
     315
     316        // make sure the role name is correct
     317        $this->assertEquals( array(), $user->roles, "User should not have any roles" );
     318
     319        $caps = $this->getCapsAndRoles();
     320
     321        foreach ( $caps as $cap => $roles ) {
     322            $this->assertFalse( $user->has_cap( $cap ), "User with an invalid role should not have the {$cap} capability" );
     323            $this->assertFalse( user_can( $user, $cap ), "User with an invalid role should not have the {$cap} capability" );
     324        }
    147325    }
    148326
    149327    // a user with multiple roles
    150328    function test_user_subscriber_contributor() {
    151         $id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    152         $user = new WP_User($id);
    153         $this->assertTrue($user->exists(), "Problem getting user $id");
    154         $user->add_role('contributor');
    155 
    156         // nuke and re-fetch the object to make sure it was stored
    157         $user = NULL;
    158         $user = new WP_User($id);
    159         $this->assertTrue($user->exists(), "Problem getting user $id");
     329        $user = $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) );
     330
     331        // make sure the user is valid
     332        $this->assertTrue( $user->exists(), "User does not exist" );
     333
     334        $user->add_role( 'contributor' );
    160335
    161336        // user should have two roles now
    162         $this->assertEquals(array('subscriber', 'contributor'), $user->roles);
    163 
    164         // with contributor capabilities
    165         $this->assertTrue($user->has_cap('edit_posts'));
    166         $this->assertTrue($user->has_cap('read'));
    167         $this->assertTrue($user->has_cap('level_1'));
    168         $this->assertTrue($user->has_cap('level_0'));
    169 
    170         // but not these
    171         $this->assertFalse($user->has_cap('upload_files'));
    172         $this->assertFalse($user->has_cap('edit_published_posts'));
    173         $this->assertFalse($user->has_cap('level_2'));
    174     }
    175 
     337        $this->assertEquals( array( 'subscriber', 'contributor' ), $user->roles );
     338
     339        $caps = $this->getCapsAndRoles();
     340
     341        foreach ( $caps as $cap => $roles ) {
     342            if ( array_intersect( $user->roles, $roles ) ) {
     343                $this->assertTrue( $user->has_cap( $cap ), "User should have the {$cap} capability" );
     344                $this->assertTrue( user_can( $user, $cap ), "User should have the {$cap} capability" );
     345            } else {
     346                $this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
     347                $this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
     348            }
     349        }
     350    }
     351
     352    // newly added empty role
    176353    function test_add_empty_role() {
    177         // add_role($role, $display_name, $capabilities = '')
    178         // randomly named role with no capabilities
    179354        global $wp_roles;
    180         $role_name = rand_str();
    181         add_role($role_name, 'Janitor', array());
    182         $this->_flush_roles();
    183         $this->assertTrue($wp_roles->is_role($role_name));
    184 
    185         $id = $this->factory->user->create( array( 'role' => $role_name ) );
    186 
    187         $user = new WP_User($id);
    188         $this->assertTrue($user->exists(), "Problem getting user $id");
    189 
    190         $this->assertEquals(array($role_name), $user->roles);
    191 
    192         // user shouldn't have any capabilities; test a quick sample
    193         $this->assertFalse($user->has_cap('upload_files'));
    194         $this->assertFalse($user->has_cap('edit_published_posts'));
    195         $this->assertFalse($user->has_cap('level_1'));
    196         $this->assertFalse($user->has_cap('level_0'));
     355
     356        $role_name = 'janitor';
     357        add_role( $role_name, 'Janitor', array() );
     358
     359        $this->_flush_roles();
     360        $this->assertTrue( $wp_roles->is_role( $role_name ) );
     361
     362        $user = $this->factory->user->create_and_get( array( 'role' => $role_name ) );
     363
     364        // make sure the user is valid
     365        $this->assertTrue( $user->exists(), "User does not exist" );
     366
     367        // make sure the role name is correct
     368        $this->assertEquals( array( $role_name ), $user->roles );
     369
     370        $caps = $this->getCapsAndRoles();
     371
     372        foreach ( $caps as $cap => $roles ) {
     373            $this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
     374            $this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
     375        }
    197376
    198377        // clean up
    199         remove_role($role_name);
    200         $this->_flush_roles();
    201         $this->assertFalse($wp_roles->is_role($role_name));
    202     }
    203 
    204 
     378        remove_role( $role_name );
     379        $this->_flush_roles();
     380        $this->assertFalse( $wp_roles->is_role( $role_name ) );
     381    }
     382
     383    // newly added role
    205384    function test_add_role() {
    206         // add_role($role, $display_name, $capabilities = '')
    207         // randomly named role with a few capabilities
    208385        global $wp_roles;
    209         $role_name = rand_str();
    210         add_role($role_name, 'Janitor', array('edit_posts'=>true, 'edit_pages'=>true, 'level_0'=>true, 'level_1'=>true, 'level_2'=>true));
    211         $this->_flush_roles();
    212         $this->assertTrue($wp_roles->is_role($role_name));
    213 
    214         $id = $this->factory->user->create( array( 'role' => $role_name ) );
    215 
    216         $user = new WP_User($id);
    217         $this->assertTrue($user->exists(), "Problem getting user $id");
    218 
    219         $this->assertEquals(array($role_name), $user->roles);
    220 
    221         // the user should have all the above caps
    222         $this->assertTrue($user->has_cap($role_name));
    223         $this->assertTrue($user->has_cap('edit_posts'));
    224         $this->assertTrue($user->has_cap('edit_pages'));
    225         $this->assertTrue($user->has_cap('level_0'));
    226         $this->assertTrue($user->has_cap('level_1'));
    227         $this->assertTrue($user->has_cap('level_2'));
    228 
    229         // shouldn't have any other caps
    230         $this->assertFalse($user->has_cap('upload_files'));
    231         $this->assertFalse($user->has_cap('edit_published_posts'));
    232         $this->assertFalse($user->has_cap('upload_files'));
    233         $this->assertFalse($user->has_cap('level_3'));
     386
     387        $role_name = 'janitor';
     388        $expected_caps = array(
     389            'edit_posts' => true,
     390            'edit_pages' => true,
     391            'level_0'    => true,
     392            'level_1'    => true,
     393            'level_2'    => true,
     394        );
     395        add_role( $role_name, 'Janitor', $expected_caps );
     396        $this->_flush_roles();
     397        $this->assertTrue( $wp_roles->is_role( $role_name ) );
     398
     399        $user = $this->factory->user->create_and_get( array( 'role' => $role_name ) );
     400
     401        // make sure the user is valid
     402        $this->assertTrue( $user->exists(), "User does not exist" );
     403
     404        // make sure the role name is correct
     405        $this->assertEquals( array( $role_name ), $user->roles );
     406
     407        $caps = $this->getCapsAndRoles();
     408
     409        foreach ( $caps as $cap => $roles ) {
     410            // the user should have all the above caps
     411            if ( isset( $expected_caps[ $cap ] ) ) {
     412                $this->assertTrue( $user->has_cap( $cap ), "User should have the {$cap} capability" );
     413                $this->assertTrue( user_can( $user, $cap ), "User should have the {$cap} capability" );
     414            } else {
     415                $this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
     416                $this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
     417            }
     418        }
    234419
    235420        // clean up
    236         remove_role($role_name);
    237         $this->_flush_roles();
    238         $this->assertFalse($wp_roles->is_role($role_name));
     421        remove_role( $role_name );
     422        $this->_flush_roles();
     423        $this->assertFalse( $wp_roles->is_role( $role_name ) );
    239424    }
    240425
Note: See TracChangeset for help on using the changeset viewer.