Make WordPress Core


Ignore:
Timestamp:
04/09/2025 01:29:39 PM (6 weeks ago)
Author:
SergeyBiryukov
Message:

Tests: Use the ms-required group where appropriate.

This replaces the if ( is_multisite() ) conditional wrapping entire test classes with the ms-required group for more consistency across the test suite.

Follow-up to [40520].

See #63167.

File:
1 edited

Legend:

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

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