Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 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/multisite/network.php

    r41380 r42343  
    33if ( is_multisite() ) :
    44
    5 /**
    6  * Tests specific to networks in multisite.
    7  *
    8  * @group ms-network
    9  * @group multisite
    10  */
    11 class Tests_Multisite_Network extends WP_UnitTestCase {
    12     protected $plugin_hook_count = 0;
    13     protected $suppress = false;
    14 
    15     protected static $different_network_id;
    16     protected static $different_site_ids = array();
    17 
    18     function setUp() {
    19         global $wpdb;
    20         parent::setUp();
    21         $this->suppress = $wpdb->suppress_errors();
     5    /**
     6     * Tests specific to networks in multisite.
     7     *
     8     * @group ms-network
     9     * @group multisite
     10     */
     11    class Tests_Multisite_Network extends WP_UnitTestCase {
     12        protected $plugin_hook_count = 0;
     13        protected $suppress          = false;
     14
     15        protected static $different_network_id;
     16        protected static $different_site_ids = array();
     17
     18        function setUp() {
     19            global $wpdb;
     20            parent::setUp();
     21            $this->suppress = $wpdb->suppress_errors();
     22        }
     23
     24        function tearDown() {
     25            global $wpdb, $current_site;
     26            $wpdb->suppress_errors( $this->suppress );
     27            $current_site->id = 1;
     28            parent::tearDown();
     29        }
     30
     31        public static function wpSetUpBeforeClass( $factory ) {
     32            self::$different_network_id = $factory->network->create(
     33                array(
     34                    'domain' => 'wordpress.org',
     35                    'path'   => '/',
     36                )
     37            );
     38
     39            $sites = array(
     40                array(
     41                    'domain'  => 'wordpress.org',
     42                    'path'    => '/',
     43                    'site_id' => self::$different_network_id,
     44                ),
     45                array(
     46                    'domain'  => 'wordpress.org',
     47                    'path'    => '/foo/',
     48                    'site_id' => self::$different_network_id,
     49                ),
     50                array(
     51                    'domain'  => 'wordpress.org',
     52                    'path'    => '/bar/',
     53                    'site_id' => self::$different_network_id,
     54                ),
     55            );
     56
     57            foreach ( $sites as $site ) {
     58                self::$different_site_ids[] = $factory->blog->create( $site );
     59            }
     60        }
     61
     62        public static function wpTearDownAfterClass() {
     63            global $wpdb;
     64
     65            foreach ( self::$different_site_ids as $id ) {
     66                wpmu_delete_blog( $id, true );
     67            }
     68
     69            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$different_network_id ) );
     70            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$different_network_id ) );
     71
     72            wp_update_network_site_counts();
     73        }
     74
     75        /**
     76         * By default, only one network exists and has a network ID of 1.
     77         */
     78        function test_get_main_network_id_default() {
     79            $this->assertEquals( 1, get_main_network_id() );
     80        }
     81
     82        /**
     83         * If a second network is created, network ID 1 should still be returned
     84         * as the main network ID.
     85         */
     86        function test_get_main_network_id_two_networks() {
     87            self::factory()->network->create();
     88
     89            $this->assertEquals( 1, get_main_network_id() );
     90        }
     91
     92        /**
     93         * When the `$current_site` global is populated with another network, the
     94         * main network should still return as 1.
     95         */
     96        function test_get_main_network_id_after_network_switch() {
     97            global $current_site;
     98
     99            $id = self::factory()->network->create();
     100
     101            $current_site->id = (int) $id;
     102
     103            $this->assertEquals( 1, get_main_network_id() );
     104        }
     105
     106        /**
     107         * When the first network is removed, the next should return as the main
     108         * network ID.
     109         *
     110         * @todo In the future, we'll have a smarter way of deleting a network. For now,
     111         * fake the process with UPDATE queries.
     112         */
     113        function test_get_main_network_id_after_network_delete() {
     114            global $wpdb, $current_site;
     115
     116            $temp_id = self::$different_network_id + 1;
     117
     118            $current_site->id = (int) self::$different_network_id;
     119            $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=%d WHERE id=1", $temp_id ) );
     120            $main_network_id = get_main_network_id();
     121            $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=1 WHERE id=%d", $temp_id ) );
     122
     123            $this->assertEquals( self::$different_network_id, $main_network_id );
     124        }
     125
     126        function test_get_main_network_id_filtered() {
     127            add_filter( 'get_main_network_id', array( $this, '_get_main_network_id' ) );
     128            $this->assertEquals( 3, get_main_network_id() );
     129            remove_filter( 'get_main_network_id', array( $this, '_get_main_network_id' ) );
     130        }
     131
     132        function _get_main_network_id() {
     133            return 3;
     134        }
     135
     136        /**
     137         * @ticket 37050
     138         */
     139        function test_wp_network_object_id_property_is_int() {
     140            $id = self::factory()->network->create();
     141
     142            $network = WP_Network::get_instance( $id );
     143
     144            $this->assertSame( (int) $id, $network->id );
     145        }
     146
     147        /**
     148         * @ticket 22917
     149         */
     150        public function test_get_blog_count_no_filter_applied() {
     151            wp_update_network_counts();
     152            $site_count_start = get_blog_count();
     153
     154            $site_ids = self::factory()->blog->create_many( 1 );
     155            $actual   = (int) get_blog_count(); // count only updated when cron runs, so unchanged
     156
     157            foreach ( $site_ids as $site_id ) {
     158                wpmu_delete_blog( $site_id, true );
     159            }
     160            wp_update_network_counts();
     161
     162            $this->assertEquals( $site_count_start + 1, $actual );
     163        }
     164
     165        /**
     166         * @ticket 22917
     167         */
     168        public function test_get_blog_count_enable_live_network_counts_false() {
     169            wp_update_network_counts();
     170            $site_count_start = get_blog_count();
     171
     172            add_filter( 'enable_live_network_counts', '__return_false' );
     173            $site_ids = self::factory()->blog->create_many( 1 );
     174            $actual   = (int) get_blog_count(); // count only updated when cron runs, so unchanged
     175            remove_filter( 'enable_live_network_counts', '__return_false' );
     176
     177            foreach ( $site_ids as $site_id ) {
     178                wpmu_delete_blog( $site_id, true );
     179            }
     180            wp_update_network_counts();
     181
     182            $this->assertEquals( $site_count_start, $actual );
     183        }
     184
     185        /**
     186         * @ticket 22917
     187         */
     188        public function test_get_blog_count_enabled_live_network_counts_true() {
     189            wp_update_network_counts();
     190            $site_count_start = get_blog_count();
     191
     192            add_filter( 'enable_live_network_counts', '__return_true' );
     193            $site_ids = self::factory()->blog->create_many( 1 );
     194            $actual   = get_blog_count();
     195            remove_filter( 'enable_live_network_counts', '__return_true' );
     196
     197            foreach ( $site_ids as $site_id ) {
     198                wpmu_delete_blog( $site_id, true );
     199            }
     200            wp_update_network_counts();
     201
     202            $this->assertEquals( $site_count_start + 1, $actual );
     203        }
     204
     205        /**
     206         * @ticket 37865
     207         */
     208        public function test_get_blog_count_on_different_network() {
     209            wp_update_network_site_counts( self::$different_network_id );
     210
     211            $site_count = get_blog_count( self::$different_network_id );
     212
     213            $this->assertEquals( count( self::$different_site_ids ), $site_count );
     214        }
     215
     216        /**
     217         * @ticket 37866
     218         */
     219        public function test_get_user_count_on_different_network() {
     220            wp_update_network_user_counts();
     221            $current_network_user_count = get_user_count();
     222
     223            // Add another user to fake the network user count to be different.
     224            wpmu_create_user( 'user', 'pass', 'email' );
     225
     226            wp_update_network_user_counts( self::$different_network_id );
     227
     228            $user_count = get_user_count( self::$different_network_id );
     229
     230            $this->assertEquals( $current_network_user_count + 1, $user_count );
     231        }
     232
     233        /**
     234         * @ticket 22917
     235         */
     236        function test_enable_live_network_user_counts_filter() {
     237            // false for large networks by default
     238            add_filter( 'enable_live_network_counts', '__return_false' );
     239
     240            // Refresh the cache
     241            wp_update_network_counts();
     242            $start_count = get_user_count();
     243
     244            wpmu_create_user( 'user', 'pass', 'email' );
     245
     246            // No change, cache not refreshed
     247            $count = get_user_count();
     248
     249            $this->assertEquals( $start_count, $count );
     250
     251            wp_update_network_counts();
     252            $start_count = get_user_count();
     253
     254            add_filter( 'enable_live_network_counts', '__return_true' );
     255
     256            wpmu_create_user( 'user2', 'pass2', 'email2' );
     257
     258            $count = get_user_count();
     259            $this->assertEquals( $start_count + 1, $count );
     260
     261            remove_filter( 'enable_live_network_counts', '__return_false' );
     262            remove_filter( 'enable_live_network_counts', '__return_true' );
     263        }
     264
     265        function test_active_network_plugins() {
     266            $path = 'hello.php';
     267
     268            // local activate, should be invisible for the network
     269            activate_plugin( $path ); // $network_wide = false
     270            $active_plugins = wp_get_active_network_plugins();
     271            $this->assertEquals( array(), $active_plugins );
     272
     273            add_action( 'deactivated_plugin', array( $this, '_helper_deactivate_hook' ) );
     274
     275            // activate the plugin sitewide
     276            activate_plugin( $path, '', $network_wide = true );
     277            $active_plugins                           = wp_get_active_network_plugins();
     278            $this->assertEquals( array( WP_PLUGIN_DIR . '/hello.php' ), $active_plugins );
     279
     280            //deactivate the plugin
     281            deactivate_plugins( $path );
     282            $active_plugins = wp_get_active_network_plugins();
     283            $this->assertEquals( array(), $active_plugins );
     284
     285            $this->assertEquals( 1, $this->plugin_hook_count ); // testing actions and silent mode
     286
     287            activate_plugin( $path, '', $network_wide = true );
     288            deactivate_plugins( $path, true ); // silent
     289
     290            $this->assertEquals( 1, $this->plugin_hook_count ); // testing actions and silent mode
     291        }
     292
     293        /**
     294         * @ticket 28651
     295         */
     296        function test_duplicate_network_active_plugin() {
     297            $path = 'hello.php';
     298            $mock = new MockAction();
     299            add_action( 'activate_' . $path, array( $mock, 'action' ) );
     300
     301            // should activate on the first try
     302            activate_plugin( $path, '', true );
     303            $active_plugins = wp_get_active_network_plugins();
     304            $this->assertCount( 1, $active_plugins );
     305            $this->assertEquals( 1, $mock->get_call_count() );
     306
     307            // should do nothing on the second try
     308            activate_plugin( $path, '', true );
     309            $active_plugins = wp_get_active_network_plugins();
     310            $this->assertCount( 1, $active_plugins );
     311            $this->assertEquals( 1, $mock->get_call_count() );
     312
     313            remove_action( 'activate_' . $path, array( $mock, 'action' ) );
     314        }
     315
     316        function test_is_plugin_active_for_network_true() {
     317            activate_plugin( 'hello.php', '', true );
     318            $this->assertTrue( is_plugin_active_for_network( 'hello.php' ) );
     319        }
     320
     321        function test_is_plugin_active_for_network_false() {
     322            deactivate_plugins( 'hello.php', false, true );
     323            $this->assertFalse( is_plugin_active_for_network( 'hello.php' ) );
     324        }
     325
     326        function _helper_deactivate_hook() {
     327            $this->plugin_hook_count++;
     328        }
     329
     330        function test_get_user_count() {
     331            // Refresh the cache
     332            wp_update_network_counts();
     333            $start_count = get_user_count();
     334
     335            // Only false for large networks as of 3.7
     336            add_filter( 'enable_live_network_counts', '__return_false' );
     337            self::factory()->user->create( array( 'role' => 'administrator' ) );
     338
     339            $count = get_user_count(); // No change, cache not refreshed
     340            $this->assertEquals( $start_count, $count );
     341
     342            wp_update_network_counts(); // Magic happens here
     343
     344            $count = get_user_count();
     345            $this->assertEquals( $start_count + 1, $count );
     346            remove_filter( 'enable_live_network_counts', '__return_false' );
     347        }
     348
     349        function test_wp_schedule_update_network_counts() {
     350            $this->assertFalse( wp_next_scheduled( 'update_network_counts' ) );
     351
     352            // We can't use wp_schedule_update_network_counts() because WP_INSTALLING is set
     353            wp_schedule_event( time(), 'twicedaily', 'update_network_counts' );
     354
     355            $this->assertInternalType( 'int', wp_next_scheduled( 'update_network_counts' ) );
     356        }
     357
     358        /**
     359         * @expectedDeprecated get_dashboard_blog
     360         */
     361        function test_get_dashboard_blog() {
     362            // if there is no dashboard blog set, current blog is used
     363            $dashboard_blog = get_dashboard_blog();
     364            $this->assertEquals( 1, $dashboard_blog->blog_id );
     365
     366            $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     367            $blog_id = self::factory()->blog->create( array( 'user_id' => $user_id ) );
     368            $this->assertInternalType( 'int', $blog_id );
     369
     370            // set the dashboard blog to another one
     371            update_site_option( 'dashboard_blog', $blog_id );
     372            $dashboard_blog = get_dashboard_blog();
     373            $this->assertEquals( $blog_id, $dashboard_blog->blog_id );
     374        }
     375
     376        /**
     377         * @ticket 37528
     378         */
     379        function test_wp_update_network_site_counts() {
     380            update_network_option( null, 'blog_count', 40 );
     381
     382            $expected = get_sites(
     383                array(
     384                    'network_id' => get_current_network_id(),
     385                    'spam'       => 0,
     386                    'deleted'    => 0,
     387                    'archived'   => 0,
     388                    'count'      => true,
     389                )
     390            );
     391
     392            wp_update_network_site_counts();
     393
     394            $result = get_blog_count();
     395            $this->assertEquals( $expected, $result );
     396        }
     397
     398        /**
     399         * @ticket 37528
     400         */
     401        function test_wp_update_network_site_counts_on_different_network() {
     402            update_network_option( self::$different_network_id, 'blog_count', 40 );
     403
     404            wp_update_network_site_counts( self::$different_network_id );
     405
     406            $result = get_blog_count( self::$different_network_id );
     407            $this->assertEquals( 3, $result );
     408        }
     409
     410        /**
     411         * @ticket 40349
     412         */
     413        public function test_wp_update_network_user_counts() {
     414            global $wpdb;
     415
     416            update_network_option( null, 'user_count', 40 );
     417
     418            $expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
     419
     420            wp_update_network_user_counts();
     421
     422            $result = get_user_count();
     423            $this->assertEquals( $expected, $result );
     424        }
     425
     426        /**
     427         * @ticket 40349
     428         */
     429        public function test_wp_update_network_user_counts_on_different_network() {
     430            global $wpdb;
     431
     432            update_network_option( self::$different_network_id, 'user_count', 40 );
     433
     434            $expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
     435
     436            wp_update_network_user_counts( self::$different_network_id );
     437
     438            $result = get_user_count( self::$different_network_id );
     439            $this->assertEquals( $expected, $result );
     440        }
     441
     442        /**
     443         * @ticket 40386
     444         */
     445        public function test_wp_update_network_counts() {
     446            delete_network_option( null, 'blog_count' );
     447            delete_network_option( null, 'user_count' );
     448
     449            wp_update_network_counts();
     450
     451            $site_count = (int) get_blog_count();
     452            $user_count = (int) get_user_count();
     453
     454            $this->assertTrue( $site_count > 0 && $user_count > 0 );
     455        }
     456
     457        /**
     458         * @ticket 40386
     459         */
     460        public function test_wp_update_network_counts_on_different_network() {
     461            delete_network_option( self::$different_network_id, 'blog_count' );
     462            delete_network_option( self::$different_network_id, 'user_count' );
     463
     464            wp_update_network_counts( self::$different_network_id );
     465
     466            $site_count = (int) get_blog_count( self::$different_network_id );
     467            $user_count = (int) get_user_count( self::$different_network_id );
     468
     469            $this->assertTrue( $site_count > 0 && $user_count > 0 );
     470        }
     471
     472        /**
     473         * @ticket 40489
     474         * @dataProvider data_wp_is_large_network
     475         */
     476        public function test_wp_is_large_network( $using, $count, $expected, $different_network ) {
     477            $network_id     = $different_network ? self::$different_network_id : null;
     478            $network_option = 'users' === $using ? 'user_count' : 'blog_count';
     479
     480            update_network_option( $network_id, $network_option, $count );
     481
     482            $result = wp_is_large_network( $using, $network_id );
     483            if ( $expected ) {
     484                $this->assertTrue( $result );
     485            } else {
     486                $this->assertFalse( $result );
     487            }
     488        }
     489
     490        public function data_wp_is_large_network() {
     491            return array(
     492                array( 'sites', 10000, false, false ),
     493                array( 'sites', 10001, true, false ),
     494                array( 'users', 10000, false, false ),
     495                array( 'users', 10001, true, false ),
     496                array( 'sites', 10000, false, true ),
     497                array( 'sites', 10001, true, true ),
     498                array( 'users', 10000, false, true ),
     499                array( 'users', 10001, true, true ),
     500            );
     501        }
     502
     503        /**
     504         * @ticket 40489
     505         * @dataProvider data_wp_is_large_network_filtered_by_component
     506         */
     507        public function test_wp_is_large_network_filtered_by_component( $using, $count, $expected, $different_network ) {
     508            $network_id     = $different_network ? self::$different_network_id : null;
     509            $network_option = 'users' === $using ? 'user_count' : 'blog_count';
     510
     511            update_network_option( $network_id, $network_option, $count );
     512
     513            add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10, 3 );
     514            $result = wp_is_large_network( $using, $network_id );
     515            remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10 );
     516
     517            if ( $expected ) {
     518                $this->assertTrue( $result );
     519            } else {
     520                $this->assertFalse( $result );
     521            }
     522        }
     523
     524        public function data_wp_is_large_network_filtered_by_component() {
     525            return array(
     526                array( 'sites', 10000, false, false ),
     527                array( 'sites', 10001, true, false ),
     528                array( 'users', 1000, false, false ),
     529                array( 'users', 1001, true, false ),
     530                array( 'sites', 10000, false, true ),
     531                array( 'sites', 10001, true, true ),
     532                array( 'users', 1000, false, true ),
     533                array( 'users', 1001, true, true ),
     534            );
     535        }
     536
     537        public function filter_wp_is_large_network_for_users( $is_large_network, $using, $count ) {
     538            if ( 'users' === $using ) {
     539                return $count > 1000;
     540            }
     541
     542            return $is_large_network;
     543        }
     544
     545        /**
     546         * @ticket 40489
     547         * @dataProvider data_wp_is_large_network_filtered_by_network
     548         */
     549        public function test_wp_is_large_network_filtered_by_network( $using, $count, $expected, $different_network ) {
     550            $network_id     = $different_network ? self::$different_network_id : null;
     551            $network_option = 'users' === $using ? 'user_count' : 'blog_count';
     552
     553            update_network_option( $network_id, $network_option, $count );
     554
     555            add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10, 4 );
     556            $result = wp_is_large_network( $using, $network_id );
     557            remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10 );
     558
     559            if ( $expected ) {
     560                $this->assertTrue( $result );
     561            } else {
     562                $this->assertFalse( $result );
     563            }
     564        }
     565
     566        public function data_wp_is_large_network_filtered_by_network() {
     567            return array(
     568                array( 'sites', 10000, false, false ),
     569                array( 'sites', 10001, true, false ),
     570                array( 'users', 10000, false, false ),
     571                array( 'users', 10001, true, false ),
     572                array( 'sites', 1000, false, true ),
     573                array( 'sites', 1001, true, true ),
     574                array( 'users', 1000, false, true ),
     575                array( 'users', 1001, true, true ),
     576            );
     577        }
     578
     579        public function filter_wp_is_large_network_on_different_network( $is_large_network, $using, $count, $network_id ) {
     580            if ( $network_id === (int) self::$different_network_id ) {
     581                return $count > 1000;
     582            }
     583
     584            return $is_large_network;
     585        }
     586
     587        /**
     588         * @ticket 38699
     589         */
     590        public function test_wpmu_create_blog_updates_correct_network_site_count() {
     591            $original_count = get_blog_count( self::$different_network_id );
     592
     593            $site_id = self::factory()->blog->create(
     594                array(
     595                    'domain'  => 'example.org',
     596                    'path'    => '/',
     597                    'site_id' => self::$different_network_id,
     598                )
     599            );
     600
     601            $result = get_blog_count( self::$different_network_id );
     602
     603            wpmu_delete_blog( $site_id, true );
     604
     605            $this->assertEquals( $original_count + 1, $result );
     606        }
     607
     608        /**
     609         * @ticket 29684
     610         */
     611        public function test_network_blog_id_set() {
     612            $network = get_network( self::$different_network_id );
     613
     614            $this->assertSame( (string) self::$different_site_ids[0], $network->blog_id );
     615        }
    22616    }
    23617
    24     function tearDown() {
    25         global $wpdb, $current_site;
    26         $wpdb->suppress_errors( $this->suppress );
    27         $current_site->id = 1;
    28         parent::tearDown();
    29     }
    30 
    31     public static function wpSetUpBeforeClass( $factory ) {
    32         self::$different_network_id = $factory->network->create( array( 'domain' => 'wordpress.org', 'path' => '/' ) );
    33 
    34         $sites = array(
    35             array( 'domain' => 'wordpress.org', 'path' => '/',     'site_id' => self::$different_network_id ),
    36             array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$different_network_id ),
    37             array( 'domain' => 'wordpress.org', 'path' => '/bar/', 'site_id' => self::$different_network_id ),
    38         );
    39 
    40         foreach ( $sites as $site ) {
    41             self::$different_site_ids[] = $factory->blog->create( $site );
    42         }
    43     }
    44 
    45     public static function wpTearDownAfterClass() {
    46         global $wpdb;
    47 
    48         foreach( self::$different_site_ids as $id ) {
    49             wpmu_delete_blog( $id, true );
    50         }
    51 
    52         $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$different_network_id ) );
    53         $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$different_network_id ) );
    54 
    55         wp_update_network_site_counts();
    56     }
    57 
    58     /**
    59      * By default, only one network exists and has a network ID of 1.
    60      */
    61     function test_get_main_network_id_default() {
    62         $this->assertEquals( 1, get_main_network_id() );
    63     }
    64 
    65     /**
    66      * If a second network is created, network ID 1 should still be returned
    67      * as the main network ID.
    68      */
    69     function test_get_main_network_id_two_networks() {
    70         self::factory()->network->create();
    71 
    72         $this->assertEquals( 1, get_main_network_id() );
    73     }
    74 
    75     /**
    76      * When the `$current_site` global is populated with another network, the
    77      * main network should still return as 1.
    78      */
    79     function test_get_main_network_id_after_network_switch() {
    80         global $current_site;
    81 
    82         $id = self::factory()->network->create();
    83 
    84         $current_site->id = (int) $id;
    85 
    86         $this->assertEquals( 1, get_main_network_id() );
    87     }
    88 
    89     /**
    90      * When the first network is removed, the next should return as the main
    91      * network ID.
    92      *
    93      * @todo In the future, we'll have a smarter way of deleting a network. For now,
    94      * fake the process with UPDATE queries.
    95      */
    96     function test_get_main_network_id_after_network_delete() {
    97         global $wpdb, $current_site;
    98 
    99         $temp_id = self::$different_network_id + 1;
    100 
    101         $current_site->id = (int) self::$different_network_id;
    102         $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=%d WHERE id=1", $temp_id ) );
    103         $main_network_id = get_main_network_id();
    104         $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=1 WHERE id=%d", $temp_id ) );
    105 
    106         $this->assertEquals( self::$different_network_id, $main_network_id );
    107     }
    108 
    109     function test_get_main_network_id_filtered() {
    110         add_filter( 'get_main_network_id', array( $this, '_get_main_network_id' ) );
    111         $this->assertEquals( 3, get_main_network_id() );
    112         remove_filter( 'get_main_network_id', array( $this, '_get_main_network_id' ) );
    113     }
    114 
    115     function _get_main_network_id() {
    116         return 3;
    117     }
    118 
    119     /**
    120      * @ticket 37050
    121      */
    122     function test_wp_network_object_id_property_is_int() {
    123         $id = self::factory()->network->create();
    124 
    125         $network = WP_Network::get_instance( $id );
    126 
    127         $this->assertSame( (int) $id, $network->id );
    128     }
    129 
    130     /**
    131      * @ticket 22917
    132      */
    133     public function test_get_blog_count_no_filter_applied() {
    134         wp_update_network_counts();
    135         $site_count_start = get_blog_count();
    136 
    137         $site_ids = self::factory()->blog->create_many( 1 );
    138         $actual = (int) get_blog_count(); // count only updated when cron runs, so unchanged
    139 
    140         foreach ( $site_ids as $site_id ) {
    141             wpmu_delete_blog( $site_id, true );
    142         }
    143         wp_update_network_counts();
    144 
    145         $this->assertEquals( $site_count_start + 1, $actual );
    146     }
    147 
    148     /**
    149      * @ticket 22917
    150      */
    151     public function test_get_blog_count_enable_live_network_counts_false() {
    152         wp_update_network_counts();
    153         $site_count_start = get_blog_count();
    154 
    155         add_filter( 'enable_live_network_counts', '__return_false' );
    156         $site_ids = self::factory()->blog->create_many( 1 );
    157         $actual = (int) get_blog_count(); // count only updated when cron runs, so unchanged
    158         remove_filter( 'enable_live_network_counts', '__return_false' );
    159 
    160         foreach ( $site_ids as $site_id ) {
    161             wpmu_delete_blog( $site_id, true );
    162         }
    163         wp_update_network_counts();
    164 
    165         $this->assertEquals( $site_count_start, $actual );
    166     }
    167 
    168     /**
    169      * @ticket 22917
    170      */
    171     public function test_get_blog_count_enabled_live_network_counts_true() {
    172         wp_update_network_counts();
    173         $site_count_start = get_blog_count();
    174 
    175         add_filter( 'enable_live_network_counts', '__return_true' );
    176         $site_ids = self::factory()->blog->create_many( 1 );
    177         $actual = get_blog_count();
    178         remove_filter( 'enable_live_network_counts', '__return_true' );
    179 
    180         foreach ( $site_ids as $site_id ) {
    181             wpmu_delete_blog( $site_id, true );
    182         }
    183         wp_update_network_counts();
    184 
    185         $this->assertEquals( $site_count_start + 1, $actual );
    186     }
    187 
    188     /**
    189      * @ticket 37865
    190      */
    191     public function test_get_blog_count_on_different_network() {
    192         wp_update_network_site_counts( self::$different_network_id );
    193 
    194         $site_count = get_blog_count( self::$different_network_id );
    195 
    196         $this->assertEquals( count( self::$different_site_ids ), $site_count );
    197     }
    198 
    199     /**
    200      * @ticket 37866
    201      */
    202     public function test_get_user_count_on_different_network() {
    203         wp_update_network_user_counts();
    204         $current_network_user_count = get_user_count();
    205 
    206         // Add another user to fake the network user count to be different.
    207         wpmu_create_user( 'user', 'pass', 'email' );
    208 
    209         wp_update_network_user_counts( self::$different_network_id );
    210 
    211         $user_count = get_user_count( self::$different_network_id );
    212 
    213         $this->assertEquals( $current_network_user_count + 1, $user_count );
    214     }
    215 
    216     /**
    217      * @ticket 22917
    218      */
    219     function test_enable_live_network_user_counts_filter() {
    220         // false for large networks by default
    221         add_filter( 'enable_live_network_counts', '__return_false' );
    222 
    223         // Refresh the cache
    224         wp_update_network_counts();
    225         $start_count = get_user_count();
    226 
    227         wpmu_create_user( 'user', 'pass', 'email' );
    228 
    229         // No change, cache not refreshed
    230         $count = get_user_count();
    231 
    232         $this->assertEquals( $start_count, $count );
    233 
    234         wp_update_network_counts();
    235         $start_count = get_user_count();
    236 
    237         add_filter( 'enable_live_network_counts', '__return_true' );
    238 
    239         wpmu_create_user( 'user2', 'pass2', 'email2' );
    240 
    241         $count = get_user_count();
    242         $this->assertEquals( $start_count + 1, $count );
    243 
    244         remove_filter( 'enable_live_network_counts', '__return_false' );
    245         remove_filter( 'enable_live_network_counts', '__return_true' );
    246     }
    247 
    248     function test_active_network_plugins() {
    249         $path = "hello.php";
    250 
    251         // local activate, should be invisible for the network
    252         activate_plugin($path); // $network_wide = false
    253         $active_plugins = wp_get_active_network_plugins();
    254         $this->assertEquals( Array(), $active_plugins );
    255 
    256         add_action( 'deactivated_plugin', array( $this, '_helper_deactivate_hook' ) );
    257 
    258         // activate the plugin sitewide
    259         activate_plugin($path, '', $network_wide = true);
    260         $active_plugins = wp_get_active_network_plugins();
    261         $this->assertEquals( Array(WP_PLUGIN_DIR . '/hello.php'), $active_plugins );
    262 
    263         //deactivate the plugin
    264         deactivate_plugins($path);
    265         $active_plugins = wp_get_active_network_plugins();
    266         $this->assertEquals( Array(), $active_plugins );
    267 
    268         $this->assertEquals( 1, $this->plugin_hook_count ); // testing actions and silent mode
    269 
    270         activate_plugin($path, '', $network_wide = true);
    271         deactivate_plugins($path, true); // silent
    272 
    273         $this->assertEquals( 1, $this->plugin_hook_count ); // testing actions and silent mode
    274     }
    275 
    276     /**
    277      * @ticket 28651
    278      */
    279     function test_duplicate_network_active_plugin() {
    280         $path = "hello.php";
    281         $mock = new MockAction();
    282         add_action( 'activate_' . $path, array ( $mock, 'action' ) );
    283 
    284         // should activate on the first try
    285         activate_plugin( $path, '', true );
    286         $active_plugins = wp_get_active_network_plugins();
    287         $this->assertCount( 1, $active_plugins );
    288         $this->assertEquals( 1, $mock->get_call_count() );
    289 
    290         // should do nothing on the second try
    291         activate_plugin( $path, '', true );
    292         $active_plugins = wp_get_active_network_plugins();
    293         $this->assertCount( 1, $active_plugins );
    294         $this->assertEquals( 1, $mock->get_call_count() );
    295 
    296         remove_action( 'activate_' . $path, array ( $mock, 'action' ) );
    297     }
    298 
    299     function test_is_plugin_active_for_network_true() {
    300         activate_plugin( 'hello.php', '', true );
    301         $this->assertTrue( is_plugin_active_for_network( 'hello.php' ) );
    302     }
    303 
    304     function test_is_plugin_active_for_network_false() {
    305         deactivate_plugins( 'hello.php', false, true );
    306         $this->assertFalse( is_plugin_active_for_network( 'hello.php' ) );
    307     }
    308 
    309     function _helper_deactivate_hook() {
    310         $this->plugin_hook_count++;
    311     }
    312 
    313     function test_get_user_count() {
    314         // Refresh the cache
    315         wp_update_network_counts();
    316         $start_count = get_user_count();
    317 
    318         // Only false for large networks as of 3.7
    319         add_filter( 'enable_live_network_counts', '__return_false' );
    320         self::factory()->user->create( array( 'role' => 'administrator' ) );
    321 
    322         $count = get_user_count(); // No change, cache not refreshed
    323         $this->assertEquals( $start_count, $count );
    324 
    325         wp_update_network_counts(); // Magic happens here
    326 
    327         $count = get_user_count();
    328         $this->assertEquals( $start_count + 1, $count );
    329         remove_filter( 'enable_live_network_counts', '__return_false' );
    330     }
    331 
    332     function test_wp_schedule_update_network_counts() {
    333         $this->assertFalse(wp_next_scheduled('update_network_counts'));
    334 
    335         // We can't use wp_schedule_update_network_counts() because WP_INSTALLING is set
    336         wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
    337 
    338         $this->assertInternalType('int', wp_next_scheduled('update_network_counts'));
    339     }
    340 
    341     /**
    342      * @expectedDeprecated get_dashboard_blog
    343      */
    344     function test_get_dashboard_blog() {
    345         // if there is no dashboard blog set, current blog is used
    346         $dashboard_blog = get_dashboard_blog();
    347         $this->assertEquals( 1, $dashboard_blog->blog_id );
    348 
    349         $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
    350         $blog_id = self::factory()->blog->create( array( 'user_id' => $user_id ) );
    351         $this->assertInternalType( 'int', $blog_id );
    352 
    353         // set the dashboard blog to another one
    354         update_site_option( 'dashboard_blog', $blog_id );
    355         $dashboard_blog = get_dashboard_blog();
    356         $this->assertEquals( $blog_id, $dashboard_blog->blog_id );
    357     }
    358 
    359     /**
    360      * @ticket 37528
    361      */
    362     function test_wp_update_network_site_counts() {
    363         update_network_option( null, 'blog_count', 40 );
    364 
    365         $expected = get_sites( array(
    366             'network_id' => get_current_network_id(),
    367             'spam'       => 0,
    368             'deleted'    => 0,
    369             'archived'   => 0,
    370             'count'      => true,
    371         ) );
    372 
    373         wp_update_network_site_counts();
    374 
    375         $result = get_blog_count();
    376         $this->assertEquals( $expected, $result );
    377     }
    378 
    379     /**
    380      * @ticket 37528
    381      */
    382     function test_wp_update_network_site_counts_on_different_network() {
    383         update_network_option( self::$different_network_id, 'blog_count', 40 );
    384 
    385         wp_update_network_site_counts( self::$different_network_id );
    386 
    387         $result = get_blog_count( self::$different_network_id );
    388         $this->assertEquals( 3, $result );
    389     }
    390 
    391     /**
    392      * @ticket 40349
    393      */
    394     public function test_wp_update_network_user_counts() {
    395         global $wpdb;
    396 
    397         update_network_option( null, 'user_count', 40 );
    398 
    399         $expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    400 
    401         wp_update_network_user_counts();
    402 
    403         $result = get_user_count();
    404         $this->assertEquals( $expected, $result );
    405     }
    406 
    407     /**
    408      * @ticket 40349
    409      */
    410     public function test_wp_update_network_user_counts_on_different_network() {
    411         global $wpdb;
    412 
    413         update_network_option( self::$different_network_id, 'user_count', 40 );
    414 
    415         $expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    416 
    417         wp_update_network_user_counts( self::$different_network_id );
    418 
    419         $result = get_user_count( self::$different_network_id );
    420         $this->assertEquals( $expected, $result );
    421     }
    422 
    423     /**
    424      * @ticket 40386
    425      */
    426     public function test_wp_update_network_counts() {
    427         delete_network_option( null, 'blog_count' );
    428         delete_network_option( null, 'user_count' );
    429 
    430         wp_update_network_counts();
    431 
    432         $site_count = (int) get_blog_count();
    433         $user_count = (int) get_user_count();
    434 
    435         $this->assertTrue( $site_count > 0 && $user_count > 0 );
    436     }
    437 
    438     /**
    439      * @ticket 40386
    440      */
    441     public function test_wp_update_network_counts_on_different_network() {
    442         delete_network_option( self::$different_network_id, 'blog_count' );
    443         delete_network_option( self::$different_network_id, 'user_count' );
    444 
    445         wp_update_network_counts( self::$different_network_id );
    446 
    447         $site_count = (int) get_blog_count( self::$different_network_id );
    448         $user_count = (int) get_user_count( self::$different_network_id );
    449 
    450         $this->assertTrue( $site_count > 0 && $user_count > 0 );
    451     }
    452 
    453     /**
    454      * @ticket 40489
    455      * @dataProvider data_wp_is_large_network
    456      */
    457     public function test_wp_is_large_network( $using, $count, $expected, $different_network ) {
    458         $network_id = $different_network ? self::$different_network_id : null;
    459         $network_option = 'users' === $using ? 'user_count' : 'blog_count';
    460 
    461         update_network_option( $network_id, $network_option, $count );
    462 
    463         $result = wp_is_large_network( $using, $network_id );
    464         if ( $expected ) {
    465             $this->assertTrue( $result );
    466         } else {
    467             $this->assertFalse( $result );
    468         }
    469     }
    470 
    471     public function data_wp_is_large_network() {
    472         return array(
    473             array( 'sites', 10000, false, false ),
    474             array( 'sites', 10001, true, false ),
    475             array( 'users', 10000, false, false ),
    476             array( 'users', 10001, true, false ),
    477             array( 'sites', 10000, false, true ),
    478             array( 'sites', 10001, true, true ),
    479             array( 'users', 10000, false, true ),
    480             array( 'users', 10001, true, true ),
    481         );
    482     }
    483 
    484     /**
    485      * @ticket 40489
    486      * @dataProvider data_wp_is_large_network_filtered_by_component
    487      */
    488     public function test_wp_is_large_network_filtered_by_component( $using, $count, $expected, $different_network ) {
    489         $network_id = $different_network ? self::$different_network_id : null;
    490         $network_option = 'users' === $using ? 'user_count' : 'blog_count';
    491 
    492         update_network_option( $network_id, $network_option, $count );
    493 
    494         add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10, 3 );
    495         $result = wp_is_large_network( $using, $network_id );
    496         remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10 );
    497 
    498         if ( $expected ) {
    499             $this->assertTrue( $result );
    500         } else {
    501             $this->assertFalse( $result );
    502         }
    503     }
    504 
    505     public function data_wp_is_large_network_filtered_by_component() {
    506         return array(
    507             array( 'sites', 10000, false, false ),
    508             array( 'sites', 10001, true, false ),
    509             array( 'users', 1000, false, false ),
    510             array( 'users', 1001, true, false ),
    511             array( 'sites', 10000, false, true ),
    512             array( 'sites', 10001, true, true ),
    513             array( 'users', 1000, false, true ),
    514             array( 'users', 1001, true, true ),
    515         );
    516     }
    517 
    518     public function filter_wp_is_large_network_for_users( $is_large_network, $using, $count ) {
    519         if ( 'users' === $using ) {
    520             return $count > 1000;
    521         }
    522 
    523         return $is_large_network;
    524     }
    525 
    526     /**
    527      * @ticket 40489
    528      * @dataProvider data_wp_is_large_network_filtered_by_network
    529      */
    530     public function test_wp_is_large_network_filtered_by_network( $using, $count, $expected, $different_network ) {
    531         $network_id = $different_network ? self::$different_network_id : null;
    532         $network_option = 'users' === $using ? 'user_count' : 'blog_count';
    533 
    534         update_network_option( $network_id, $network_option, $count );
    535 
    536         add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10, 4 );
    537         $result = wp_is_large_network( $using, $network_id );
    538         remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10 );
    539 
    540         if ( $expected ) {
    541             $this->assertTrue( $result );
    542         } else {
    543             $this->assertFalse( $result );
    544         }
    545     }
    546 
    547     public function data_wp_is_large_network_filtered_by_network() {
    548         return array(
    549             array( 'sites', 10000, false, false ),
    550             array( 'sites', 10001, true, false ),
    551             array( 'users', 10000, false, false ),
    552             array( 'users', 10001, true, false ),
    553             array( 'sites', 1000, false, true ),
    554             array( 'sites', 1001, true, true ),
    555             array( 'users', 1000, false, true ),
    556             array( 'users', 1001, true, true ),
    557         );
    558     }
    559 
    560     public function filter_wp_is_large_network_on_different_network( $is_large_network, $using, $count, $network_id ) {
    561         if ( $network_id === (int) self::$different_network_id ) {
    562             return $count > 1000;
    563         }
    564 
    565         return $is_large_network;
    566     }
    567 
    568     /**
    569      * @ticket 38699
    570      */
    571     public function test_wpmu_create_blog_updates_correct_network_site_count() {
    572         $original_count = get_blog_count( self::$different_network_id );
    573 
    574         $site_id = self::factory()->blog->create( array(
    575             'domain'  => 'example.org',
    576             'path'    => '/',
    577             'site_id' => self::$different_network_id,
    578         ) );
    579 
    580         $result = get_blog_count( self::$different_network_id );
    581 
    582         wpmu_delete_blog( $site_id, true );
    583 
    584         $this->assertEquals( $original_count + 1, $result );
    585     }
    586 
    587     /**
    588      * @ticket 29684
    589      */
    590     public function test_network_blog_id_set() {
    591         $network = get_network( self::$different_network_id );
    592 
    593         $this->assertSame( (string) self::$different_site_ids[0], $network->blog_id );
    594     }
    595 }
    596 
    597618endif;
Note: See TracChangeset for help on using the changeset viewer.