WordPress.org

Make WordPress Core

Changeset 36047


Ignore:
Timestamp:
12/21/2015 06:56:57 AM (5 years ago)
Author:
johnbillion
Message:

Tests: Shave a second off the user capability tests by reusing its user fixtures.

See #30017, #32394

File:
1 edited

Legend:

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

    r35872 r36047  
    88 */
    99class Tests_User_Capabilities extends WP_UnitTestCase {
    10     protected $user_ids = array();
     10
     11    protected static $users = array();
     12
     13    public static function wpSetUpBeforeClass( $factory ) {
     14        self::$users = array(
     15            'administrator' => $factory->user->create_and_get( array( 'role' => 'administrator' ) ),
     16            'editor'        => $factory->user->create_and_get( array( 'role' => 'editor' ) ),
     17            'author'        => $factory->user->create_and_get( array( 'role' => 'author' ) ),
     18            'contributor'   => $factory->user->create_and_get( array( 'role' => 'contributor' ) ),
     19            'subscriber'    => $factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
     20        );
     21    }
    1122
    1223    function setUp() {
     
    1526        $this->_flush_roles();
    1627
    17         $this->orig_users = get_users();
    1828    }
    1929
     
    234244    // test the tests
    235245    function test_all_caps_of_users_are_being_tested() {
    236         $users = array(
    237             'administrator' => self::factory()->user->create_and_get( array( 'role' => 'administrator' ) ),
    238             'editor'        => self::factory()->user->create_and_get( array( 'role' => 'editor' ) ),
    239             'author'        => self::factory()->user->create_and_get( array( 'role' => 'author' ) ),
    240             'contributor'   => self::factory()->user->create_and_get( array( 'role' => 'contributor' ) ),
    241             'subscriber'    => self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ),
    242         );
    243246        $caps = $this->getCapsAndRoles();
    244247
     
    248251        $this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
    249252
    250         foreach ( $users as $role => $user ) {
     253        foreach ( self::$users as $role => $user ) {
    251254
    252255            // make sure the user is valid
     
    272275    // test the default roles and caps
    273276    function test_all_roles_and_caps() {
    274         $users = array(
    275             'administrator' => self::factory()->user->create_and_get( array( 'role' => 'administrator' ) ),
    276             'editor'        => self::factory()->user->create_and_get( array( 'role' => 'editor' ) ),
    277             'author'        => self::factory()->user->create_and_get( array( 'role' => 'author' ) ),
    278             'contributor'   => self::factory()->user->create_and_get( array( 'role' => 'contributor' ) ),
    279             'subscriber'    => self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ),
    280         );
    281277        $caps = $this->getCapsAndRoles();
    282278
    283         foreach ( $users as $role => $user ) {
     279        foreach ( self::$users as $role => $user ) {
    284280
    285281            // make sure the user is valid
     
    308304    // special case for the link manager
    309305    function test_link_manager_caps() {
    310         $users = array(
    311             'administrator' => self::factory()->user->create_and_get( array( 'role' => 'administrator' ) ),
    312             'editor'        => self::factory()->user->create_and_get( array( 'role' => 'editor' ) ),
    313             'author'        => self::factory()->user->create_and_get( array( 'role' => 'author' ) ),
    314             'contributor'   => self::factory()->user->create_and_get( array( 'role' => 'contributor' ) ),
    315             'subscriber'    => self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ),
    316         );
    317306        $caps = array(
    318307            'manage_links' => array( 'administrator', 'editor' ),
     
    322311
    323312        // no-one should have access to the link manager by default
    324         foreach ( $users as $role => $user ) {
     313        foreach ( self::$users as $role => $user ) {
    325314            foreach ( $caps as $cap => $roles ) {
    326315                $this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
     
    332321        $this->assertSame( '1', get_option( 'link_manager_enabled' ) );
    333322
    334         foreach ( $users as $role => $user ) {
     323        foreach ( self::$users as $role => $user ) {
    335324            foreach ( $caps as $cap => $roles ) {
    336325                if ( in_array( $role, $roles, true ) ) {
     
    351340    // special case for unfiltered uploads
    352341    function test_unfiltered_upload_caps() {
    353         $users = array(
    354             'administrator' => self::factory()->user->create_and_get( array( 'role' => 'administrator' ) ),
    355             'editor'        => self::factory()->user->create_and_get( array( 'role' => 'editor' ) ),
    356             'author'        => self::factory()->user->create_and_get( array( 'role' => 'author' ) ),
    357             'contributor'   => self::factory()->user->create_and_get( array( 'role' => 'contributor' ) ),
    358             'subscriber'    => self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ),
    359         );
    360 
    361342        $this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
    362343
    363344        // no-one should have this cap
    364         foreach ( $users as $role => $user ) {
     345        foreach ( self::$users as $role => $user ) {
    365346            $this->assertFalse( $user->has_cap( 'unfiltered_upload' ), "User with the {$role} role should not have the unfiltered_upload capability" );
    366347            $this->assertFalse( user_can( $user, 'unfiltered_upload' ), "User with the {$role} role should not have the unfiltered_upload capability" );
     
    376357        $caps = $this->getCapsAndRoles();
    377358
    378         $user = self::factory()->user->create_and_get( array( 'role' => 'administrator' ) );
     359        $user = self::$users['administrator'];
    379360        grant_super_admin( $user->ID );
    380361
     
    414395    // a user with multiple roles
    415396    function test_user_subscriber_contributor() {
    416         $user = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) );
     397        $user = self::$users['subscriber'];
    417398
    418399        // make sure the user is valid
     
    435416            }
    436417        }
     418
     419        $user->remove_role( 'contributor' );
     420        // user should have one role now
     421        $this->assertEquals( array( 'subscriber' ), $user->roles );
     422
    437423    }
    438424
     
    720706        // simple tests for some common meta capabilities
    721707
    722         // Make our author
    723         $author = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
     708        // Get our author
     709        $author = self::$users['author'];
    724710
    725711        // make a post
     
    810796
    811797        // Add an editor and contributor
    812         $editor = self::factory()->user->create_and_get( array( 'role' => 'editor' ) );
    813         $contributor = self::factory()->user->create_and_get( array( 'role' => 'contributor' ) );
     798        $editor = self::$users['editor'];
     799        $contributor = self::$users['contributor'];
    814800
    815801        // editor can edit, view, and trash
     
    892878        // simple tests for some common meta capabilities
    893879
    894         // Make our author
    895         $author = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
     880        // Get our author
     881        $author = self::$users['author'];
    896882
    897883        // make a page
     
    902888
    903889        // add some other users
    904         $admin = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     890        $admin = self::$users['administrator'];
    905891        $author_2 = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
    906         $editor = new WP_User( self::factory()->user->create( array( 'role' => 'editor' ) ) );
    907         $contributor = new WP_User( self::factory()->user->create( array( 'role' => 'contributor' ) ) );
     892        $editor = self::$users['editor'];
     893        $contributor = self::$users['contributor'];
    908894
    909895        // administrators, editors and the post owner can edit it
     
    10201006
    10211007    function test_subscriber_cant_edit_posts() {
    1022         $user = new WP_User( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
     1008        $user = self::$users['subscriber'];
    10231009        wp_set_current_user( $user->ID );
    10241010
     
    10351021        }
    10361022
    1037         $user = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
    1038         $other_user = new WP_User( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
     1023        $user = self::$users['administrator'];
     1024        $other_user = self::$users['subscriber'];
    10391025
    10401026        wp_set_current_user( $user->ID );
     
    10491035        }
    10501036
    1051         $user = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     1037        $user = self::$users['administrator'];
    10521038
    10531039        wp_set_current_user( $user->ID );
     
    10621048
    10631049        // Add a contributor
    1064         $contributor = $this->factory->user->create_and_get( array(
    1065             'role' => 'contributor',
    1066         ) );
     1050        $contributor = self::$users['contributor'];
    10671051
    10681052        // Give them a scheduled post
     
    11291113        _unregister_post_type( 'existed' );
    11301114
    1131         $subscriber_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
    1132         $editor_id = self::factory()->user->create( array( 'role' => 'editor' ) );
     1115        $subscriber_id = self::$users['subscriber']->ID;
     1116        $editor_id = self::$users['editor']->ID;
    11331117
    11341118        $this->setExpectedIncorrectUsage( 'map_meta_cap' );
     
    11551139        $cpt = get_post_type_object( 'page_capability' );
    11561140
    1157         $admin       = self::factory()->user->create_and_get( array( 'role' => 'administrator' ) );
    1158         $editor      = self::factory()->user->create_and_get( array( 'role' => 'editor' ) );
    1159         $author      = self::factory()->user->create_and_get( array( 'role' => 'author' ) );
    1160         $contributor = self::factory()->user->create_and_get( array( 'role' => 'contributor' ) );
     1141        $admin       = self::$users['administrator'];
     1142        $editor      = self::$users['editor'];
     1143        $author      = self::$users['author'];
     1144        $contributor = self::$users['contributor'];
    11611145
    11621146        $this->assertEquals( 'edit_pages', $cpt->cap->edit_posts );
Note: See TracChangeset for help on using the changeset viewer.