Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

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

    r41661 r42343  
    33if ( is_multisite() ) :
    44
    5 /**
    6  * Tests specific to users in multisite.
    7  *
    8  * @group user
    9  * @group ms-user
    10  * @group multisite
    11  */
    12 class Tests_Multisite_User extends WP_UnitTestCase {
    13         protected $suppress = false;
    14 
    15         function setUp() {
    16                 global $wpdb;
    17                 parent::setUp();
    18                 $this->suppress = $wpdb->suppress_errors();
     5        /**
     6         * Tests specific to users in multisite.
     7         *
     8         * @group user
     9         * @group ms-user
     10         * @group multisite
     11         */
     12        class Tests_Multisite_User extends WP_UnitTestCase {
     13                protected $suppress = false;
     14
     15                function setUp() {
     16                        global $wpdb;
     17                        parent::setUp();
     18                        $this->suppress = $wpdb->suppress_errors();
     19                }
     20
     21                function tearDown() {
     22                        global $wpdb;
     23                        $wpdb->suppress_errors( $this->suppress );
     24                        parent::tearDown();
     25                }
     26
     27                function test_remove_user_from_blog() {
     28                        $user1 = self::factory()->user->create_and_get();
     29                        $user2 = self::factory()->user->create_and_get();
     30
     31                        $post_id = self::factory()->post->create( array( 'post_author' => $user1->ID ) );
     32
     33                        remove_user_from_blog( $user1->ID, 1, $user2->ID );
     34
     35                        $post = get_post( $post_id );
     36
     37                        $this->assertNotEquals( $user1->ID, $post->post_author );
     38                        $this->assertEquals( $user2->ID, $post->post_author );
     39                }
     40
     41                /**
     42                 * Test the returned data from get_blogs_of_user()
     43                 */
     44                function test_get_blogs_of_user() {
     45                        $user1_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     46
     47                        // Maintain a list of 6 total sites and include the primary network site.
     48                        $blog_ids = self::factory()->blog->create_many( 5, array( 'user_id' => $user1_id ) );
     49                        $blog_ids = array_merge( array( 1 ), $blog_ids );
     50
     51                        // All sites are new and not marked as spam, archived, or deleted.
     52                        $blog_ids_of_user = array_keys( get_blogs_of_user( $user1_id ) );
     53
     54                        // User should be a member of the created sites and the network's initial site.
     55                        $this->assertEquals( $blog_ids, $blog_ids_of_user );
     56
     57                        $this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[0] ) );
     58                        $this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[2] ) );
     59                        $this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[4] ) );
     60
     61                        unset( $blog_ids[0] );
     62                        unset( $blog_ids[2] );
     63                        unset( $blog_ids[4] );
     64                        sort( $blog_ids );
     65
     66                        $blogs_of_user = get_blogs_of_user( $user1_id, false );
     67
     68                        // The user should still be a member of all remaining sites.
     69                        $blog_ids_of_user = array_keys( $blogs_of_user );
     70                        $this->assertEquals( $blog_ids, $blog_ids_of_user );
     71
     72                        // Each site retrieved should match the expected structure.
     73                        foreach ( $blogs_of_user as $blog_id => $blog ) {
     74                                $this->assertEquals( $blog_id, $blog->userblog_id );
     75                                $this->assertTrue( isset( $blog->userblog_id ) );
     76                                $this->assertTrue( isset( $blog->blogname ) );
     77                                $this->assertTrue( isset( $blog->domain ) );
     78                                $this->assertTrue( isset( $blog->path ) );
     79                                $this->assertTrue( isset( $blog->site_id ) );
     80                                $this->assertTrue( isset( $blog->siteurl ) );
     81                                $this->assertTrue( isset( $blog->archived ) );
     82                                $this->assertTrue( isset( $blog->spam ) );
     83                                $this->assertTrue( isset( $blog->deleted ) );
     84                        }
     85
     86                        // Mark each remaining site as spam, archived, and deleted.
     87                        update_blog_details( $blog_ids[0], array( 'spam' => 1 ) );
     88                        update_blog_details( $blog_ids[1], array( 'archived' => 1 ) );
     89                        update_blog_details( $blog_ids[2], array( 'deleted' => 1 ) );
     90
     91                        // Passing true as the second parameter should retrieve ALL sites, even if marked.
     92                        $blogs_of_user    = get_blogs_of_user( $user1_id, true );
     93                        $blog_ids_of_user = array_keys( $blogs_of_user );
     94                        $this->assertEquals( $blog_ids, $blog_ids_of_user );
     95
     96                        // Check if sites are flagged as expected.
     97                        $this->assertEquals( 1, $blogs_of_user[ $blog_ids[0] ]->spam );
     98                        $this->assertEquals( 1, $blogs_of_user[ $blog_ids[1] ]->archived );
     99                        $this->assertEquals( 1, $blogs_of_user[ $blog_ids[2] ]->deleted );
     100
     101                        unset( $blog_ids[0] );
     102                        unset( $blog_ids[1] );
     103                        unset( $blog_ids[2] );
     104                        sort( $blog_ids );
     105
     106                        // Passing false (the default) as the second parameter should retrieve only good sites.
     107                        $blog_ids_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
     108                        $this->assertEquals( $blog_ids, $blog_ids_of_user );
     109                }
     110
     111                /**
     112                 * @expectedDeprecated is_blog_user
     113                 */
     114                function test_is_blog_user() {
     115                        global $wpdb;
     116
     117                        $user1_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     118
     119                        $old_current = get_current_user_id();
     120                        wp_set_current_user( $user1_id );
     121
     122                        $this->assertTrue( is_blog_user() );
     123                        $this->assertTrue( is_blog_user( get_current_blog_id() ) );
     124
     125                        $blog_ids = array();
     126
     127                        $blog_ids = self::factory()->blog->create_many( 1 );
     128                        foreach ( $blog_ids as $blog_id ) {
     129                                $this->assertInternalType( 'int', $blog_id );
     130                                $this->assertTrue( is_blog_user( $blog_id ) );
     131                                $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
     132                                $this->assertFalse( is_blog_user( $blog_id ) );
     133                        }
     134
     135                        wp_set_current_user( $old_current );
     136                }
     137
     138                function test_is_user_member_of_blog() {
     139                        global $wpdb;
     140
     141                        $user1_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     142                        $user2_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     143
     144                        $old_current = get_current_user_id();
     145
     146                        $this->assertSame( 0, $old_current );
     147
     148                        // test for "get current user" when not logged in
     149                        $this->assertFalse( is_user_member_of_blog() );
     150
     151                        wp_set_current_user( $user1_id );
     152                        $site_id = get_current_blog_id();
     153
     154                        $this->assertTrue( is_user_member_of_blog() );
     155                        $this->assertTrue( is_user_member_of_blog( 0, 0 ) );
     156                        $this->assertTrue( is_user_member_of_blog( 0, $site_id ) );
     157                        $this->assertTrue( is_user_member_of_blog( $user1_id ) );
     158                        $this->assertTrue( is_user_member_of_blog( $user1_id, $site_id ) );
     159
     160                        $blog_ids = self::factory()->blog->create_many( 1 );
     161                        foreach ( $blog_ids as $blog_id ) {
     162                                $this->assertInternalType( 'int', $blog_id );
     163
     164                                // Current user gets added to new blogs
     165                                $this->assertTrue( is_user_member_of_blog( $user1_id, $blog_id ) );
     166                                // Other users should not
     167                                $this->assertFalse( is_user_member_of_blog( $user2_id, $blog_id ) );
     168
     169                                switch_to_blog( $blog_id );
     170
     171                                $this->assertTrue( is_user_member_of_blog( $user1_id ) );
     172                                $this->assertFalse( is_user_member_of_blog( $user2_id ) );
     173
     174                                // Remove user 1 from blog
     175                                $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
     176
     177                                // Add user 2 to blog
     178                                $this->assertTrue( add_user_to_blog( $blog_id, $user2_id, 'subscriber' ) );
     179
     180                                $this->assertFalse( is_user_member_of_blog( $user1_id ) );
     181                                $this->assertTrue( is_user_member_of_blog( $user2_id ) );
     182
     183                                restore_current_blog();
     184
     185                                $this->assertFalse( is_user_member_of_blog( $user1_id, $blog_id ) );
     186                                $this->assertTrue( is_user_member_of_blog( $user2_id, $blog_id ) );
     187                        }
     188
     189                        wpmu_delete_user( $user1_id );
     190                        $user = new WP_User( $user1_id );
     191                        $this->assertFalse( $user->exists() );
     192                        $this->assertFalse( is_user_member_of_blog( $user1_id ) );
     193
     194                        wp_set_current_user( $old_current );
     195                }
     196
     197                /**
     198                 * @ticket 23192
     199                 */
     200                function test_is_user_spammy() {
     201                        $user_id = self::factory()->user->create(
     202                                array(
     203                                        'role'       => 'author',
     204                                        'user_login' => 'testuser1',
     205                                )
     206                        );
     207
     208                        $spam_username = (string) $user_id;
     209                        $spam_user_id  = self::factory()->user->create(
     210                                array(
     211                                        'role'       => 'author',
     212                                        'user_login' => $spam_username,
     213                                )
     214                        );
     215                        update_user_status( $spam_user_id, 'spam', '1' );
     216
     217                        $this->assertTrue( is_user_spammy( $spam_username ) );
     218                        $this->assertFalse( is_user_spammy( 'testuser1' ) );
     219                }
     220
     221                /**
     222                 * @ticket 20601
     223                 */
     224                function test_user_member_of_blog() {
     225                        global $wp_rewrite;
     226
     227                        self::factory()->blog->create();
     228                        $user_id = self::factory()->user->create();
     229                        self::factory()->blog->create( array( 'user_id' => $user_id ) );
     230
     231                        $blogs = get_blogs_of_user( $user_id );
     232                        $this->assertCount( 2, $blogs );
     233                        $first = reset( $blogs )->userblog_id;
     234                        remove_user_from_blog( $user_id, $first );
     235
     236                        $blogs  = get_blogs_of_user( $user_id );
     237                        $second = reset( $blogs )->userblog_id;
     238                        $this->assertCount( 1, $blogs );
     239
     240                        switch_to_blog( $first );
     241                        $wp_rewrite->init();
     242
     243                        $this->go_to( get_author_posts_url( $user_id ) );
     244                        $this->assertQueryTrue( 'is_404' );
     245
     246                        switch_to_blog( $second );
     247                        $wp_rewrite->init();
     248
     249                        $this->go_to( get_author_posts_url( $user_id ) );
     250                        $this->assertQueryTrue( 'is_author', 'is_archive' );
     251
     252                        add_user_to_blog( $first, $user_id, 'administrator' );
     253                        $blogs = get_blogs_of_user( $user_id );
     254                        $this->assertCount( 2, $blogs );
     255
     256                        switch_to_blog( $first );
     257                        $wp_rewrite->init();
     258
     259                        $this->go_to( get_author_posts_url( $user_id ) );
     260                        $this->assertQueryTrue( 'is_author', 'is_archive' );
     261                }
     262
     263                function test_revoked_super_admin_can_be_deleted() {
     264                        if ( isset( $GLOBALS['super_admins'] ) ) {
     265                                $old_global = $GLOBALS['super_admins'];
     266                                unset( $GLOBALS['super_admins'] );
     267                        }
     268
     269                        $user_id = self::factory()->user->create();
     270                        grant_super_admin( $user_id );
     271                        revoke_super_admin( $user_id );
     272
     273                        $this->assertTrue( wpmu_delete_user( $user_id ) );
     274
     275                        if ( isset( $old_global ) ) {
     276                                $GLOBALS['super_admins'] = $old_global;
     277                        }
     278                }
     279
     280                function test_revoked_super_admin_is_deleted() {
     281                        if ( isset( $GLOBALS['super_admins'] ) ) {
     282                                $old_global = $GLOBALS['super_admins'];
     283                                unset( $GLOBALS['super_admins'] );
     284                        }
     285
     286                        $user_id = self::factory()->user->create();
     287                        grant_super_admin( $user_id );
     288                        revoke_super_admin( $user_id );
     289                        wpmu_delete_user( $user_id );
     290                        $user = new WP_User( $user_id );
     291
     292                        $this->assertFalse( $user->exists(), 'WP_User->exists' );
     293
     294                        if ( isset( $old_global ) ) {
     295                                $GLOBALS['super_admins'] = $old_global;
     296                        }
     297                }
     298
     299                function test_super_admin_cannot_be_deleted() {
     300                        if ( isset( $GLOBALS['super_admins'] ) ) {
     301                                $old_global = $GLOBALS['super_admins'];
     302                                unset( $GLOBALS['super_admins'] );
     303                        }
     304
     305                        $user_id = self::factory()->user->create();
     306                        grant_super_admin( $user_id );
     307
     308                        $this->assertFalse( wpmu_delete_user( $user_id ) );
     309
     310                        if ( isset( $old_global ) ) {
     311                                $GLOBALS['super_admins'] = $old_global;
     312                        }
     313                }
     314
     315                /**
     316                 * @ticket 27205
     317                 */
     318                function test_granting_super_admins() {
     319                        if ( isset( $GLOBALS['super_admins'] ) ) {
     320                                $old_global = $GLOBALS['super_admins'];
     321                                unset( $GLOBALS['super_admins'] );
     322                        }
     323
     324                        $user_id = self::factory()->user->create();
     325
     326                        $this->assertFalse( is_super_admin( $user_id ) );
     327                        $this->assertFalse( revoke_super_admin( $user_id ) );
     328                        $this->assertTrue( grant_super_admin( $user_id ) );
     329                        $this->assertTrue( is_super_admin( $user_id ) );
     330                        $this->assertFalse( grant_super_admin( $user_id ) );
     331                        $this->assertTrue( revoke_super_admin( $user_id ) );
     332
     333                        // None of these operations should set the $super_admins global.
     334                        $this->assertFalse( isset( $GLOBALS['super_admins'] ) );
     335
     336                        // Try with two users.
     337                        $second_user = self::factory()->user->create();
     338                        $this->assertTrue( grant_super_admin( $user_id ) );
     339                        $this->assertTrue( grant_super_admin( $second_user ) );
     340                        $this->assertTrue( is_super_admin( $second_user ) );
     341                        $this->assertTrue( is_super_admin( $user_id ) );
     342                        $this->assertTrue( revoke_super_admin( $user_id ) );
     343                        $this->assertTrue( revoke_super_admin( $second_user ) );
     344
     345                        if ( isset( $old_global ) ) {
     346                                $GLOBALS['super_admins'] = $old_global;
     347                        }
     348                }
     349
     350                public function test_numeric_string_user_id() {
     351                        $u = self::factory()->user->create();
     352
     353                        $u_string = (string) $u;
     354                        $this->assertTrue( wpmu_delete_user( $u_string ) );
     355                        $this->assertFalse( get_user_by( 'id', $u ) );
     356                }
     357
     358                /**
     359                 * @ticket 33800
     360                 */
     361                public function test_should_return_false_for_non_numeric_string_user_id() {
     362                        $this->assertFalse( wpmu_delete_user( 'abcde' ) );
     363                }
     364
     365                /**
     366                 * @ticket 33800
     367                 */
     368                public function test_should_return_false_for_object_user_id() {
     369                        $u_obj = self::factory()->user->create_and_get();
     370                        $this->assertFalse( wpmu_delete_user( $u_obj ) );
     371                        $this->assertEquals( $u_obj->ID, username_exists( $u_obj->user_login ) );
     372                }
     373
     374                /**
     375                 * @ticket 38356
     376                 */
     377                public function test_add_user_to_blog_subscriber() {
     378                        $site_id = self::factory()->blog->create();
     379                        $user_id = self::factory()->user->create();
     380
     381                        add_user_to_blog( $site_id, $user_id, 'subscriber' );
     382
     383                        switch_to_blog( $site_id );
     384                        $user = get_user_by( 'id', $user_id );
     385                        restore_current_blog();
     386
     387                        wpmu_delete_blog( $site_id );
     388                        wpmu_delete_user( $user_id );
     389
     390                        $this->assertContains( 'subscriber', $user->roles );
     391                }
     392
     393                /**
     394                 * @ticket 38356
     395                 */
     396                public function test_add_user_to_blog_invalid_user() {
     397                        $site_id = self::factory()->blog->create();
     398
     399                        $result = add_user_to_blog( 73622, $site_id, 'subscriber' );
     400                        wpmu_delete_blog( $site_id );
     401
     402                        $this->assertWPError( $result );
     403                }
     404
     405                /**
     406                 * @ticket 41101
     407                 */
     408                public function test_should_fail_can_add_user_to_blog_filter() {
     409                        $site_id = self::factory()->blog->create();
     410                        $user_id = self::factory()->user->create();
     411
     412                        add_filter( 'can_add_user_to_blog', '__return_false' );
     413                        $result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
     414
     415                        $this->assertWPError( $result );
     416                }
     417
     418                /**
     419                 * @ticket 41101
     420                 */
     421                public function test_should_succeed_can_add_user_to_blog_filter() {
     422                        $site_id = self::factory()->blog->create();
     423                        $user_id = self::factory()->user->create();
     424
     425                        add_filter( 'can_add_user_to_blog', '__return_true' );
     426                        $result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
     427
     428                        $this->assertTrue( $result );
     429                }
     430
     431                /**
     432                 * @ticket 23016
     433                 */
     434                public function test_wp_roles_global_is_reset() {
     435                        global $wp_roles;
     436                        $role      = 'test_global_is_reset';
     437                        $role_name = 'Test Global Is Reset';
     438                        $blog_id   = self::factory()->blog->create();
     439
     440                        $wp_roles->add_role( $role, $role_name, array() );
     441
     442                        $this->assertNotEmpty( $wp_roles->get_role( $role ) );
     443
     444                        switch_to_blog( $blog_id );
     445
     446                        $this->assertEmpty( $wp_roles->get_role( $role ) );
     447
     448                        $wp_roles->add_role( $role, $role_name, array() );
     449
     450                        $this->assertNotEmpty( $wp_roles->get_role( $role ) );
     451
     452                        restore_current_blog();
     453
     454                        $this->assertNotEmpty( $wp_roles->get_role( $role ) );
     455
     456                        $wp_roles->remove_role( $role );
     457                }
     458
    19459        }
    20460
    21         function tearDown() {
    22                 global $wpdb;
    23                 $wpdb->suppress_errors( $this->suppress );
    24                 parent::tearDown();
    25         }
    26 
    27         function test_remove_user_from_blog() {
    28                 $user1 = self::factory()->user->create_and_get();
    29                 $user2 = self::factory()->user->create_and_get();
    30 
    31                 $post_id = self::factory()->post->create( array( 'post_author' => $user1->ID ) );
    32 
    33                 remove_user_from_blog( $user1->ID, 1, $user2->ID );
    34 
    35                 $post = get_post( $post_id );
    36 
    37                 $this->assertNotEquals( $user1->ID, $post->post_author );
    38                 $this->assertEquals( $user2->ID, $post->post_author );
    39         }
    40 
    41         /**
    42          * Test the returned data from get_blogs_of_user()
    43          */
    44         function test_get_blogs_of_user() {
    45                 $user1_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
    46 
    47                 // Maintain a list of 6 total sites and include the primary network site.
    48                 $blog_ids = self::factory()->blog->create_many( 5, array( 'user_id' => $user1_id ) );
    49                 $blog_ids = array_merge( array( 1 ), $blog_ids );
    50 
    51                 // All sites are new and not marked as spam, archived, or deleted.
    52                 $blog_ids_of_user = array_keys( get_blogs_of_user( $user1_id ) );
    53 
    54                 // User should be a member of the created sites and the network's initial site.
    55                 $this->assertEquals( $blog_ids, $blog_ids_of_user );
    56 
    57                 $this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[0] ) );
    58                 $this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[2] ) );
    59                 $this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[4] ) );
    60 
    61                 unset( $blog_ids[0] );
    62                 unset( $blog_ids[2] );
    63                 unset( $blog_ids[4] );
    64                 sort( $blog_ids );
    65 
    66                 $blogs_of_user = get_blogs_of_user( $user1_id, false );
    67 
    68                 // The user should still be a member of all remaining sites.
    69                 $blog_ids_of_user = array_keys( $blogs_of_user );
    70                 $this->assertEquals( $blog_ids, $blog_ids_of_user );
    71 
    72                 // Each site retrieved should match the expected structure.
    73                 foreach ( $blogs_of_user as $blog_id => $blog ) {
    74                         $this->assertEquals( $blog_id, $blog->userblog_id );
    75                         $this->assertTrue( isset( $blog->userblog_id ) );
    76                         $this->assertTrue( isset( $blog->blogname ) );
    77                         $this->assertTrue( isset( $blog->domain ) );
    78                         $this->assertTrue( isset( $blog->path ) );
    79                         $this->assertTrue( isset( $blog->site_id ) );
    80                         $this->assertTrue( isset( $blog->siteurl ) );
    81                         $this->assertTrue( isset( $blog->archived ) );
    82                         $this->assertTrue( isset( $blog->spam ) );
    83                         $this->assertTrue( isset( $blog->deleted ) );
    84                 }
    85 
    86                 // Mark each remaining site as spam, archived, and deleted.
    87                 update_blog_details( $blog_ids[0], array( 'spam' => 1 ) );
    88                 update_blog_details( $blog_ids[1], array( 'archived' => 1 ) );
    89                 update_blog_details( $blog_ids[2], array( 'deleted' => 1 ) );
    90 
    91                 // Passing true as the second parameter should retrieve ALL sites, even if marked.
    92                 $blogs_of_user = get_blogs_of_user( $user1_id, true );
    93                 $blog_ids_of_user = array_keys( $blogs_of_user );
    94                 $this->assertEquals( $blog_ids, $blog_ids_of_user );
    95 
    96                 // Check if sites are flagged as expected.
    97                 $this->assertEquals( 1, $blogs_of_user[ $blog_ids[0] ]->spam );
    98                 $this->assertEquals( 1, $blogs_of_user[ $blog_ids[1] ]->archived );
    99                 $this->assertEquals( 1, $blogs_of_user[ $blog_ids[2] ]->deleted );
    100 
    101                 unset( $blog_ids[0] );
    102                 unset( $blog_ids[1] );
    103                 unset( $blog_ids[2] );
    104                 sort( $blog_ids );
    105 
    106                 // Passing false (the default) as the second parameter should retrieve only good sites.
    107                 $blog_ids_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
    108                 $this->assertEquals( $blog_ids, $blog_ids_of_user );
    109         }
    110 
    111         /**
    112          * @expectedDeprecated is_blog_user
    113          */
    114         function test_is_blog_user() {
    115                 global $wpdb;
    116 
    117                 $user1_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
    118 
    119                 $old_current = get_current_user_id();
    120                 wp_set_current_user( $user1_id );
    121 
    122                 $this->assertTrue( is_blog_user() );
    123                 $this->assertTrue( is_blog_user( get_current_blog_id() ) );
    124 
    125                 $blog_ids = array();
    126 
    127                 $blog_ids = self::factory()->blog->create_many( 1 );
    128                 foreach ( $blog_ids as $blog_id ) {
    129                         $this->assertInternalType( 'int', $blog_id );
    130                         $this->assertTrue( is_blog_user( $blog_id ) );
    131                         $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
    132                         $this->assertFalse( is_blog_user( $blog_id ) );
    133                 }
    134 
    135                 wp_set_current_user( $old_current );
    136         }
    137 
    138         function test_is_user_member_of_blog() {
    139                 global $wpdb;
    140 
    141                 $user1_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
    142                 $user2_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
    143 
    144                 $old_current = get_current_user_id();
    145 
    146                 $this->assertSame( 0, $old_current );
    147 
    148                 // test for "get current user" when not logged in
    149                 $this->assertFalse( is_user_member_of_blog() );
    150 
    151                 wp_set_current_user( $user1_id );
    152                 $site_id = get_current_blog_id();
    153 
    154                 $this->assertTrue( is_user_member_of_blog() );
    155                 $this->assertTrue( is_user_member_of_blog( 0, 0 ) );
    156                 $this->assertTrue( is_user_member_of_blog( 0, $site_id ) );
    157                 $this->assertTrue( is_user_member_of_blog( $user1_id ) );
    158                 $this->assertTrue( is_user_member_of_blog( $user1_id, $site_id ) );
    159 
    160                 $blog_ids = self::factory()->blog->create_many( 1 );
    161                 foreach ( $blog_ids as $blog_id ) {
    162                         $this->assertInternalType( 'int', $blog_id );
    163 
    164                         // Current user gets added to new blogs
    165                         $this->assertTrue( is_user_member_of_blog( $user1_id, $blog_id ) );
    166                         // Other users should not
    167                         $this->assertFalse( is_user_member_of_blog( $user2_id, $blog_id ) );
    168 
    169                         switch_to_blog( $blog_id );
    170 
    171                         $this->assertTrue( is_user_member_of_blog( $user1_id ) );
    172                         $this->assertFalse( is_user_member_of_blog( $user2_id ) );
    173 
    174                         // Remove user 1 from blog
    175                         $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
    176 
    177                         // Add user 2 to blog
    178                         $this->assertTrue( add_user_to_blog( $blog_id, $user2_id, 'subscriber' ) );
    179 
    180                         $this->assertFalse( is_user_member_of_blog( $user1_id ) );
    181                         $this->assertTrue( is_user_member_of_blog( $user2_id ) );
    182 
    183                         restore_current_blog();
    184 
    185                         $this->assertFalse( is_user_member_of_blog( $user1_id, $blog_id ) );
    186                         $this->assertTrue( is_user_member_of_blog( $user2_id, $blog_id ) );
    187                 }
    188 
    189                 wpmu_delete_user( $user1_id );
    190                 $user = new WP_User( $user1_id );
    191                 $this->assertFalse( $user->exists() );
    192                 $this->assertFalse( is_user_member_of_blog( $user1_id ) );
    193 
    194                 wp_set_current_user( $old_current );
    195         }
    196 
    197         /**
    198          * @ticket 23192
    199          */
    200         function test_is_user_spammy() {
    201                 $user_id = self::factory()->user->create( array(
    202                         'role' => 'author',
    203                         'user_login' => 'testuser1',
    204                 ) );
    205 
    206                 $spam_username = (string) $user_id;
    207                 $spam_user_id = self::factory()->user->create( array(
    208                         'role' => 'author',
    209                         'user_login' => $spam_username,
    210                 ) );
    211                 update_user_status( $spam_user_id, 'spam', '1' );
    212 
    213                 $this->assertTrue( is_user_spammy( $spam_username ) );
    214                 $this->assertFalse( is_user_spammy( 'testuser1' ) );
    215         }
    216 
    217         /**
    218          * @ticket 20601
    219          */
    220         function test_user_member_of_blog() {
    221                 global $wp_rewrite;
    222 
    223                 self::factory()->blog->create();
    224                 $user_id = self::factory()->user->create();
    225                 self::factory()->blog->create( array( 'user_id' => $user_id ) );
    226 
    227                 $blogs = get_blogs_of_user( $user_id );
    228                 $this->assertCount( 2, $blogs );
    229                 $first = reset( $blogs )->userblog_id;
    230                 remove_user_from_blog( $user_id, $first );
    231 
    232                 $blogs = get_blogs_of_user( $user_id );
    233                 $second = reset( $blogs )->userblog_id;
    234                 $this->assertCount( 1, $blogs );
    235 
    236                 switch_to_blog( $first );
    237                 $wp_rewrite->init();
    238 
    239                 $this->go_to( get_author_posts_url( $user_id ) );
    240                 $this->assertQueryTrue( 'is_404' );
    241 
    242                 switch_to_blog( $second );
    243                 $wp_rewrite->init();
    244 
    245                 $this->go_to( get_author_posts_url( $user_id ) );
    246                 $this->assertQueryTrue( 'is_author', 'is_archive' );
    247 
    248                 add_user_to_blog( $first, $user_id, 'administrator' );
    249                 $blogs = get_blogs_of_user( $user_id );
    250                 $this->assertCount( 2, $blogs );
    251 
    252                 switch_to_blog( $first );
    253                 $wp_rewrite->init();
    254 
    255                 $this->go_to( get_author_posts_url( $user_id ) );
    256                 $this->assertQueryTrue( 'is_author', 'is_archive' );
    257         }
    258 
    259         function test_revoked_super_admin_can_be_deleted() {
    260                 if ( isset( $GLOBALS['super_admins'] ) ) {
    261                         $old_global = $GLOBALS['super_admins'];
    262                         unset( $GLOBALS['super_admins'] );
    263                 }
    264 
    265                 $user_id = self::factory()->user->create();
    266                 grant_super_admin( $user_id );
    267                 revoke_super_admin( $user_id );
    268 
    269                 $this->assertTrue( wpmu_delete_user( $user_id ) );
    270 
    271                 if ( isset( $old_global ) ) {
    272                         $GLOBALS['super_admins'] = $old_global;
    273                 }
    274         }
    275 
    276         function test_revoked_super_admin_is_deleted() {
    277                 if ( isset( $GLOBALS['super_admins'] ) ) {
    278                         $old_global = $GLOBALS['super_admins'];
    279                         unset( $GLOBALS['super_admins'] );
    280                 }
    281 
    282                 $user_id = self::factory()->user->create();
    283                 grant_super_admin( $user_id );
    284                 revoke_super_admin( $user_id );
    285                 wpmu_delete_user( $user_id );
    286                 $user = new WP_User( $user_id );
    287 
    288                 $this->assertFalse( $user->exists(), 'WP_User->exists' );
    289 
    290                 if ( isset( $old_global ) ) {
    291                         $GLOBALS['super_admins'] = $old_global;
    292                 }
    293         }
    294 
    295         function test_super_admin_cannot_be_deleted() {
    296                 if ( isset( $GLOBALS['super_admins'] ) ) {
    297                         $old_global = $GLOBALS['super_admins'];
    298                         unset( $GLOBALS['super_admins'] );
    299                 }
    300 
    301                 $user_id = self::factory()->user->create();
    302                 grant_super_admin( $user_id );
    303 
    304                 $this->assertFalse( wpmu_delete_user( $user_id ) );
    305 
    306                 if ( isset( $old_global ) ) {
    307                         $GLOBALS['super_admins'] = $old_global;
    308                 }
    309         }
    310 
    311         /**
    312          * @ticket 27205
    313          */
    314         function test_granting_super_admins() {
    315                 if ( isset( $GLOBALS['super_admins'] ) ) {
    316                         $old_global = $GLOBALS['super_admins'];
    317                         unset( $GLOBALS['super_admins'] );
    318                 }
    319 
    320                 $user_id = self::factory()->user->create();
    321 
    322                 $this->assertFalse( is_super_admin( $user_id ) );
    323                 $this->assertFalse( revoke_super_admin( $user_id ) );
    324                 $this->assertTrue( grant_super_admin( $user_id ) );
    325                 $this->assertTrue( is_super_admin( $user_id ) );
    326                 $this->assertFalse( grant_super_admin( $user_id ) );
    327                 $this->assertTrue( revoke_super_admin( $user_id ) );
    328 
    329                 // None of these operations should set the $super_admins global.
    330                 $this->assertFalse( isset( $GLOBALS['super_admins'] ) );
    331 
    332                 // Try with two users.
    333                 $second_user = self::factory()->user->create();
    334                 $this->assertTrue( grant_super_admin( $user_id ) );
    335                 $this->assertTrue( grant_super_admin( $second_user ) );
    336                 $this->assertTrue( is_super_admin( $second_user ) );
    337                 $this->assertTrue( is_super_admin( $user_id ) );
    338                 $this->assertTrue( revoke_super_admin( $user_id ) );
    339                 $this->assertTrue( revoke_super_admin( $second_user ) );
    340 
    341                 if ( isset( $old_global ) ) {
    342                         $GLOBALS['super_admins'] = $old_global;
    343                 }
    344         }
    345 
    346         public function test_numeric_string_user_id() {
    347                 $u = self::factory()->user->create();
    348 
    349                 $u_string = (string) $u;
    350                 $this->assertTrue( wpmu_delete_user( $u_string ) );
    351                 $this->assertFalse( get_user_by( 'id', $u ) );
    352         }
    353 
    354         /**
    355          * @ticket 33800
    356          */
    357         public function test_should_return_false_for_non_numeric_string_user_id() {
    358                 $this->assertFalse( wpmu_delete_user( 'abcde' ) );
    359         }
    360 
    361         /**
    362          * @ticket 33800
    363          */
    364         public function test_should_return_false_for_object_user_id() {
    365                 $u_obj = self::factory()->user->create_and_get();
    366                 $this->assertFalse( wpmu_delete_user( $u_obj ) );
    367                 $this->assertEquals( $u_obj->ID, username_exists( $u_obj->user_login ) );
    368         }
    369 
    370         /**
    371          * @ticket 38356
    372          */
    373         public function test_add_user_to_blog_subscriber() {
    374                 $site_id = self::factory()->blog->create();
    375                 $user_id = self::factory()->user->create();
    376 
    377                 add_user_to_blog( $site_id, $user_id, 'subscriber' );
    378 
    379                 switch_to_blog( $site_id );
    380                 $user = get_user_by( 'id', $user_id );
    381                 restore_current_blog();
    382 
    383                 wpmu_delete_blog( $site_id );
    384                 wpmu_delete_user( $user_id );
    385 
    386                 $this->assertContains( 'subscriber', $user->roles );
    387         }
    388 
    389         /**
    390          * @ticket 38356
    391          */
    392         public function test_add_user_to_blog_invalid_user() {
    393                 $site_id = self::factory()->blog->create();
    394 
    395                 $result = add_user_to_blog( 73622, $site_id, 'subscriber' );
    396                 wpmu_delete_blog( $site_id );
    397 
    398                 $this->assertWPError( $result );
    399         }
    400 
    401         /**
    402          * @ticket 41101
    403          */
    404         public function test_should_fail_can_add_user_to_blog_filter() {
    405                 $site_id = self::factory()->blog->create();
    406                 $user_id = self::factory()->user->create();
    407 
    408                 add_filter( 'can_add_user_to_blog', '__return_false' );
    409                 $result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
    410 
    411                 $this->assertWPError( $result );
    412         }
    413 
    414         /**
    415          * @ticket 41101
    416          */
    417         public function test_should_succeed_can_add_user_to_blog_filter() {
    418                 $site_id = self::factory()->blog->create();
    419                 $user_id = self::factory()->user->create();
    420 
    421                 add_filter( 'can_add_user_to_blog', '__return_true' );
    422                 $result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
    423 
    424                 $this->assertTrue( $result );
    425         }
    426 
    427         /**
    428          * @ticket 23016
    429          */
    430         public function test_wp_roles_global_is_reset() {
    431                 global $wp_roles;
    432                 $role = 'test_global_is_reset';
    433                 $role_name = 'Test Global Is Reset';
    434                 $blog_id = self::factory()->blog->create();
    435 
    436                 $wp_roles->add_role( $role, $role_name, array() );
    437 
    438                 $this->assertNotEmpty( $wp_roles->get_role( $role ) );
    439 
    440                 switch_to_blog( $blog_id );
    441 
    442                 $this->assertEmpty( $wp_roles->get_role( $role ) );
    443 
    444                 $wp_roles->add_role( $role, $role_name, array() );
    445 
    446                 $this->assertNotEmpty( $wp_roles->get_role( $role ) );
    447 
    448                 restore_current_blog();
    449 
    450                 $this->assertNotEmpty( $wp_roles->get_role( $role ) );
    451 
    452                 $wp_roles->remove_role( $role );
    453         }
    454 
    455 }
    456 
    457 endif ;
     461endif;
Note: See TracChangeset for help on using the changeset viewer.