WordPress.org

Make WordPress Core

Changeset 29937


Ignore:
Timestamp:
10/17/2014 03:01:21 AM (6 years ago)
Author:
jeremyfelt
Message:

Improve and reduce tests for get_blogs_of_user()

  • Create half as many factory sites. See #30017
  • Test the removal of a user from multiple sites.
  • Expand tests to include second parameter for flagged sites.
  • Remove duplicate test for deleted user.

Fixes #29996

Location:
trunk/tests/phpunit/tests
Files:
2 edited

Legend:

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

    r29819 r29937  
    376376        else
    377377            wp_delete_user( $user_id );
     378
     379        $user = new WP_User( $user_id );
     380        $this->assertFalse( $user->exists(), 'WP_User->exists' );
    378381        $this->assertEquals( array(), get_blogs_of_user( $user_id ) );
    379382    }
  • trunk/tests/phpunit/tests/user/multisite.php

    r29916 r29937  
    4141    }
    4242
     43    /**
     44     * Test the returned data from get_blogs_of_user()
     45     */
    4346    function test_get_blogs_of_user() {
    44         // Logged out users don't have blogs.
    45         $this->assertEquals( array(), get_blogs_of_user( 0 ) );
    46 
    4747        $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    48         $blog_ids = $this->factory->blog->create_many( 10, array( 'user_id' => $user1_id ) );
    49 
    50         foreach ( $blog_ids as $blog_id )
    51             $this->assertInternalType( 'int', $blog_id );
    52 
    53         $blogs_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
    54         sort( $blogs_of_user );
    55         $this->assertEquals ( array_merge( array( 1 ), $blog_ids), $blogs_of_user );
    56 
    57         $this->assertTrue( remove_user_from_blog( $user1_id, 1 ) );
    58 
    59         $blogs_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
    60         sort( $blogs_of_user );
    61         $this->assertEquals ( $blog_ids, $blogs_of_user );
    62 
    63         foreach ( get_blogs_of_user( $user1_id, false ) as $blog ) {
     48
     49        // Maintain a list of 6 total sites and include the primary network site.
     50        $blog_ids = $this->factory->blog->create_many( 5, array( 'user_id' => $user1_id ) );
     51        $blog_ids = array_merge( array( 1 ), $blog_ids );
     52
     53        // All sites are new and not marked as spam, archived, or deleted.
     54        $blog_ids_of_user = array_keys( get_blogs_of_user( $user1_id ) );
     55
     56        // User should be a member of the created sites and the network's initial site.
     57        $this->assertEquals( $blog_ids, $blog_ids_of_user );
     58
     59        $this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[0] ) );
     60        $this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[2] ) );
     61        $this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[4] ) );
     62
     63        unset( $blog_ids[0] );
     64        unset( $blog_ids[2] );
     65        unset( $blog_ids[4] );
     66        sort( $blog_ids );
     67
     68        $blogs_of_user = get_blogs_of_user( $user1_id, false );
     69
     70        // The user should still be a member of all remaining sites.
     71        $blog_ids_of_user = array_keys( $blogs_of_user );
     72        $this->assertEquals( $blog_ids, $blog_ids_of_user );
     73
     74        // Each site retrieved should match the expected structure.
     75        foreach ( $blogs_of_user as $blog_id => $blog ) {
     76            $this->assertEquals( $blog_id, $blog->userblog_id );
    6477            $this->assertTrue( isset( $blog->userblog_id ) );
    6578            $this->assertTrue( isset( $blog->blogname ) );
     
    7386        }
    7487
    75         // Non-existent users don't have blogs.
    76         wpmu_delete_user( $user1_id );
    77         $user = new WP_User( $user1_id );
    78         $this->assertFalse( $user->exists(), 'WP_User->exists' );
    79         $this->assertEquals( array(), get_blogs_of_user( $user1_id ) );
     88        // Mark each remaining site as spam, archived, and deleted.
     89        update_blog_details( $blog_ids[0], array( 'spam' => 1 ) );
     90        update_blog_details( $blog_ids[1], array( 'archived' => 1 ) );
     91        update_blog_details( $blog_ids[2], array( 'deleted' => 1 ) );
     92
     93        // Passing true as the second parameter should retrieve ALL sites, even if marked.
     94        $blog_ids_of_user = array_keys( get_blogs_of_user( $user1_id, true ) );
     95        $this->assertEquals( $blog_ids, $blog_ids_of_user );
     96
     97        unset( $blog_ids[0] );
     98        unset( $blog_ids[1] );
     99        unset( $blog_ids[2] );
     100        sort( $blog_ids );
     101
     102        // Passing false (the default) as the second parameter should retrieve only good sites.
     103        $blog_ids_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
     104        $this->assertEquals( $blog_ids, $blog_ids_of_user );
    80105    }
    81106
Note: See TracChangeset for help on using the changeset viewer.