Make WordPress Core

Ticket #38355: 38355.3.diff

File 38355.3.diff, 3.6 KB (added by jeremyfelt, 9 years ago)
  • src/wp-includes/ms-functions.php

     
    5353        if ( false !== $primary_blog ) {
    5454                if ( ! isset( $blogs[ $primary_blog ] ) ) {
    5555                        update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
    56                         $primary = get_blog_details( $first_blog->userblog_id );
     56                        $primary = get_site( $first_blog->userblog_id );
    5757                } else {
    58                         $primary = get_blog_details( $primary_blog );
     58                        $primary = get_site( $primary_blog );
    5959                }
    6060        } else {
    6161                //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
     
    7171                        foreach ( (array) $blogs as $blog_id => $blog ) {
    7272                                if ( $blog->site_id != $wpdb->siteid )
    7373                                        continue;
    74                                 $details = get_blog_details( $blog_id );
     74                                $details = get_site( $blog_id );
    7575                                if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
    7676                                        $ret = $blog;
    7777                                        if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
  • tests/phpunit/tests/user/getActiveBlogForUser.php

     
     1<?php
     2
     3if ( is_multisite() ) :
     4
     5/**
     6 * Tests specific to users in multisite.
     7 *
     8 * @group user
     9 * @group ms-user
     10 * @group multisite
     11 */
     12class Tests_Multisite_getActiveBlogForUser extends WP_UnitTestCase {
     13
     14        /**
     15         * @ticket 38355
     16         */
     17        public function test_get_active_blog_for_user_with_no_sites() {
     18                $current_site_id = get_current_blog_id();
     19                $user_id = self::factory()->user->create();
     20                remove_user_from_blog( $user_id, $current_site_id );
     21
     22                $result = get_active_blog_for_user( $user_id );
     23                $this->assertNull( $result );
     24        }
     25
     26        /**
     27         * @ticket 38355
     28         */
     29        public function test_get_active_blog_for_user_with_primary_site() {
     30                $user_id = self::factory()->user->create();
     31                self::factory()->blog->create( array( 'user_id' => $user_id ) );
     32                self::factory()->blog->create( array( 'user_id' => $user_id ) );
     33
     34                $sites = get_blogs_of_user( $user_id );
     35                $site_ids = array_keys( $sites );
     36                $primary_site_id = $site_ids[1];
     37
     38                update_user_meta( $user_id, 'primary_blog', $primary_site_id );
     39
     40                $result = get_active_blog_for_user( $user_id );
     41                $this->assertEquals( $primary_site_id, $result->id );
     42        }
     43
     44        /**
     45         * @ticket 38355
     46         */
     47        public function test_get_active_blog_for_user_without_primary_site() {
     48                $user_id = self::factory()->user->create();
     49                self::factory()->blog->create( array( 'user_id' => $user_id ) );
     50
     51                $sites = get_blogs_of_user( $user_id );
     52                $site_ids = array_keys( $sites );
     53                $primary_site_id = $site_ids[0];
     54
     55                delete_user_meta( $user_id, 'primary_blog' );
     56
     57                $result = get_active_blog_for_user( $user_id );
     58                $this->assertEquals( $primary_site_id, $result->id );
     59        }
     60
     61        /**
     62         * @ticket 38355
     63         */
     64        public function test_get_active_blog_for_user_with_spam_site() {
     65                $current_site_id = get_current_blog_id();
     66                $user_id = self::factory()->user->create();
     67
     68                $site_id = self::factory()->blog->create( array(
     69                        'user_id' => $user_id,
     70                        'meta'    => array( 'spam' => 1 ),
     71                ) );
     72
     73                add_user_to_blog( $site_id, $user_id, 'subscriber' );
     74                update_user_meta( $user_id, 'primary_blog', $site_id );
     75
     76                $result = get_active_blog_for_user( $user_id );
     77                $this->assertEquals( $current_site_id, $result->id );
     78        }
     79}
     80
     81endif ;