Make WordPress Core


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

    r41883 r42343  
    33if ( is_multisite() ) :
    44
    5 /**
    6  * Tests specific to sites in multisite.
    7  *
    8  * @group ms-site
    9  * @group multisite
    10  */
    11 class Tests_Multisite_Site extends WP_UnitTestCase {
    12     protected $suppress = false;
    13     protected static $network_ids;
    14     protected static $site_ids;
    15 
    16     function setUp() {
    17         global $wpdb;
    18         parent::setUp();
    19         $this->suppress = $wpdb->suppress_errors();
     5    /**
     6     * Tests specific to sites in multisite.
     7     *
     8     * @group ms-site
     9     * @group multisite
     10     */
     11    class Tests_Multisite_Site extends WP_UnitTestCase {
     12        protected $suppress = false;
     13        protected static $network_ids;
     14        protected static $site_ids;
     15
     16        function setUp() {
     17            global $wpdb;
     18            parent::setUp();
     19            $this->suppress = $wpdb->suppress_errors();
     20        }
     21
     22        function tearDown() {
     23            global $wpdb;
     24            $wpdb->suppress_errors( $this->suppress );
     25            parent::tearDown();
     26        }
     27
     28        public static function wpSetUpBeforeClass( $factory ) {
     29            self::$network_ids = array(
     30                'make.wordpress.org/' => array(
     31                    'domain' => 'make.wordpress.org',
     32                    'path'   => '/',
     33                ),
     34            );
     35
     36            foreach ( self::$network_ids as &$id ) {
     37                $id = $factory->network->create( $id );
     38            }
     39            unset( $id );
     40
     41            self::$site_ids = array(
     42                'make.wordpress.org/'     => array(
     43                    'domain'  => 'make.wordpress.org',
     44                    'path'    => '/',
     45                    'site_id' => self::$network_ids['make.wordpress.org/'],
     46                ),
     47                'make.wordpress.org/foo/' => array(
     48                    'domain'  => 'make.wordpress.org',
     49                    'path'    => '/foo/',
     50                    'site_id' => self::$network_ids['make.wordpress.org/'],
     51                ),
     52            );
     53
     54            foreach ( self::$site_ids as &$id ) {
     55                $id = $factory->blog->create( $id );
     56            }
     57            unset( $id );
     58        }
     59
     60        public static function wpTearDownAfterClass() {
     61            global $wpdb;
     62
     63            foreach ( self::$site_ids as $id ) {
     64                wpmu_delete_blog( $id, true );
     65            }
     66
     67            foreach ( self::$network_ids as $id ) {
     68                $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     69                $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     70            }
     71        }
     72
     73        function test_switch_restore_blog() {
     74            global $_wp_switched_stack, $wpdb;
     75
     76            $this->assertEquals( array(), $_wp_switched_stack );
     77            $this->assertFalse( ms_is_switched() );
     78            $current_blog_id = get_current_blog_id();
     79            $this->assertInternalType( 'integer', $current_blog_id );
     80
     81            wp_cache_set( 'switch-test', $current_blog_id, 'switch-test' );
     82            $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     83
     84            $blog_id = self::factory()->blog->create();
     85
     86            $cap_key = wp_get_current_user()->cap_key;
     87            switch_to_blog( $blog_id );
     88            $this->assertNotEquals( $cap_key, wp_get_current_user()->cap_key );
     89            $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack );
     90            $this->assertTrue( ms_is_switched() );
     91            $this->assertEquals( $blog_id, $wpdb->blogid );
     92            $this->assertFalse( wp_cache_get( 'switch-test', 'switch-test' ) );
     93            wp_cache_set( 'switch-test', $blog_id, 'switch-test' );
     94            $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     95
     96            switch_to_blog( $blog_id );
     97            $this->assertEquals( array( $current_blog_id, $blog_id ), $_wp_switched_stack );
     98            $this->assertTrue( ms_is_switched() );
     99            $this->assertEquals( $blog_id, $wpdb->blogid );
     100            $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     101
     102            restore_current_blog();
     103            $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack );
     104            $this->assertTrue( ms_is_switched() );
     105            $this->assertEquals( $blog_id, $wpdb->blogid );
     106            $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     107
     108            restore_current_blog();
     109            $this->assertEquals( $cap_key, wp_get_current_user()->cap_key );
     110            $this->assertEquals( $current_blog_id, get_current_blog_id() );
     111            $this->assertEquals( array(), $_wp_switched_stack );
     112            $this->assertFalse( ms_is_switched() );
     113            $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     114
     115            $this->assertFalse( restore_current_blog() );
     116        }
     117
     118        /**
     119         * Test the cache keys and database tables setup through the creation of a site.
     120         */
     121        function test_created_site_details() {
     122            global $wpdb;
     123
     124            $blog_id = self::factory()->blog->create();
     125
     126            $this->assertInternalType( 'int', $blog_id );
     127            $prefix = $wpdb->get_blog_prefix( $blog_id );
     128
     129            // $get_all = false, only retrieve details from the blogs table
     130            $details = get_blog_details( $blog_id, false );
     131
     132            // Combine domain and path for a site specific cache key.
     133            $key = md5( $details->domain . $details->path );
     134
     135            $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     136
     137            // get_blogaddress_by_name()
     138            $this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
     139
     140            // These are empty until get_blog_details() is called with $get_all = true
     141            $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
     142            $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     143
     144            // $get_all = true, populate the full blog-details cache and the blog slug lookup cache
     145            $details = get_blog_details( $blog_id, true );
     146            $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
     147            $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
     148
     149            // Check existence of each database table for the created site.
     150            foreach ( $wpdb->tables( 'blog', false ) as $table ) {
     151                $suppress     = $wpdb->suppress_errors();
     152                $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
     153                $wpdb->suppress_errors( $suppress );
     154
     155                // The table should exist.
     156                $this->assertNotEmpty( $table_fields );
     157
     158                // And the table should not be empty, unless commentmeta, termmeta, or links.
     159                $result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
     160                if ( 'commentmeta' == $table || 'termmeta' == $table || 'links' == $table ) {
     161                    $this->assertEmpty( $result );
     162                } else {
     163                    $this->assertNotEmpty( $result );
     164                }
     165            }
     166
     167            // update the blog count cache to use get_blog_count()
     168            wp_update_network_counts();
     169            $this->assertEquals( 2, (int) get_blog_count() );
     170        }
     171
     172        public function test_site_caches_should_invalidate_when_invalidation_is_not_suspended() {
     173            $site_id = self::factory()->blog->create();
     174
     175            $details = get_site( $site_id );
     176
     177            $suspend = wp_suspend_cache_invalidation( false );
     178            update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
     179            $new_details = get_site( $site_id );
     180            wp_suspend_cache_invalidation( $suspend );
     181
     182            $this->assertNotEquals( $details->path, $new_details->path );
     183        }
     184
     185        public function test_site_caches_should_not_invalidate_when_invalidation_is_suspended() {
     186            $site_id = self::factory()->blog->create();
     187
     188            $details = get_site( $site_id );
     189
     190            $suspend = wp_suspend_cache_invalidation();
     191            update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
     192            $new_details = get_site( $site_id );
     193            wp_suspend_cache_invalidation( $suspend );
     194
     195            $this->assertEquals( $details->path, $new_details->path );
     196        }
     197
     198        /**
     199         * When a site is flagged as 'deleted', its data should be cleared from cache.
     200         */
     201        function test_data_in_cache_after_wpmu_delete_blog_drop_false() {
     202            $blog_id = self::factory()->blog->create();
     203
     204            $details = get_blog_details( $blog_id, false );
     205            $key     = md5( $details->domain . $details->path );
     206
     207            // Delete the site without forcing a table drop.
     208            wpmu_delete_blog( $blog_id, false );
     209
     210            $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
     211            $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     212            $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     213            $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
     214        }
     215
     216        /**
     217         * When a site is flagged as 'deleted', its data should remain in the database.
     218         */
     219        function test_data_in_tables_after_wpmu_delete_blog_drop_false() {
     220            global $wpdb;
     221
     222            $blog_id = self::factory()->blog->create();
     223
     224            // Delete the site without forcing a table drop.
     225            wpmu_delete_blog( $blog_id, false );
     226
     227            $prefix = $wpdb->get_blog_prefix( $blog_id );
     228            foreach ( $wpdb->tables( 'blog', false ) as $table ) {
     229                $suppress     = $wpdb->suppress_errors();
     230                $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
     231                $wpdb->suppress_errors( $suppress );
     232                $this->assertNotEmpty( $table_fields, $prefix . $table );
     233            }
     234        }
     235
     236        /**
     237         * When a site is fully deleted, its data should be cleared from cache.
     238         */
     239        function test_data_in_cache_after_wpmu_delete_blog_drop_true() {
     240            $blog_id = self::factory()->blog->create();
     241
     242            $details = get_blog_details( $blog_id, false );
     243            $key     = md5( $details->domain . $details->path );
     244
     245            // Delete the site and force a table drop.
     246            wpmu_delete_blog( $blog_id, true );
     247
     248            $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
     249            $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     250            $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     251            $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
     252        }
     253
     254        /**
     255         * When a site is fully deleted, its data should be removed from the database.
     256         */
     257        function test_data_in_tables_after_wpmu_delete_blog_drop_true() {
     258            global $wpdb;
     259
     260            $blog_id = self::factory()->blog->create();
     261
     262            // Delete the site and force a table drop.
     263            wpmu_delete_blog( $blog_id, true );
     264
     265            $prefix = $wpdb->get_blog_prefix( $blog_id );
     266            foreach ( $wpdb->tables( 'blog', false ) as $table ) {
     267                $suppress     = $wpdb->suppress_errors();
     268                $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
     269                $wpdb->suppress_errors( $suppress );
     270                $this->assertEmpty( $table_fields );
     271            }
     272        }
     273
     274        /**
     275         * When the main site of a network is fully deleted, its data should be cleared from cache.
     276         */
     277        function test_data_in_cache_after_wpmu_delete_blog_main_site_drop_true() {
     278            $blog_id = 1; // The main site in our test suite has an ID of 1.
     279
     280            $details = get_blog_details( $blog_id, false );
     281            $key     = md5( $details->domain . $details->path );
     282
     283            // Delete the site and force a table drop.
     284            wpmu_delete_blog( $blog_id, true );
     285
     286            $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
     287            $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     288            $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     289            $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
     290        }
     291
     292        /**
     293         * When the main site of a network is fully deleted, its data should remain in the database.
     294         */
     295        function test_data_in_tables_after_wpmu_delete_blog_main_site_drop_true() {
     296            global $wpdb;
     297
     298            $blog_id = 1; // The main site in our test suite has an ID of 1.
     299
     300            // Delete the site and force a table drop.
     301            wpmu_delete_blog( $blog_id, true );
     302
     303            $prefix = $wpdb->get_blog_prefix( $blog_id );
     304            foreach ( $wpdb->tables( 'blog', false ) as $table ) {
     305                $suppress     = $wpdb->suppress_errors();
     306                $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
     307                $wpdb->suppress_errors( $suppress );
     308                $this->assertNotEmpty( $table_fields, $prefix . $table );
     309            }
     310        }
     311
     312        /**
     313         * The site count of a network should change when a site is flagged as 'deleted'.
     314         */
     315        function test_network_count_after_wpmu_delete_blog_drop_false() {
     316            $blog_id = self::factory()->blog->create();
     317
     318            // Delete the site without forcing a table drop.
     319            wpmu_delete_blog( $blog_id, false );
     320
     321            // update the blog count cache to use get_blog_count()
     322            wp_update_network_counts();
     323            $this->assertEquals( 1, get_blog_count() );
     324        }
     325
     326        /**
     327         * The site count of a network should change when a site is fully deleted.
     328         */
     329        function test_blog_count_after_wpmu_delete_blog_drop_true() {
     330            $blog_id = self::factory()->blog->create();
     331
     332            // Delete the site and force a table drop.
     333            wpmu_delete_blog( $blog_id, true );
     334
     335            // update the blog count cache to use get_blog_count()
     336            wp_update_network_counts();
     337            $this->assertEquals( 1, get_blog_count() );
     338        }
     339
     340        /**
     341         * When a site is deleted with wpmu_delete_blog(), only the files associated with
     342         * that site should be removed. When wpmu_delete_blog() is run a second time, nothing
     343         * should change with upload directories.
     344         */
     345        function test_upload_directories_after_multiple_wpmu_delete_blog() {
     346            $filename = __FUNCTION__ . '.jpg';
     347            $contents = __FUNCTION__ . '_contents';
     348
     349            // Upload a file to the main site on the network.
     350            $file1 = wp_upload_bits( $filename, null, $contents );
     351
     352            $blog_id = self::factory()->blog->create();
     353
     354            switch_to_blog( $blog_id );
     355            $file2 = wp_upload_bits( $filename, null, $contents );
     356            restore_current_blog();
     357
     358            wpmu_delete_blog( $blog_id, true );
     359
     360            // The file on the main site should still exist. The file on the deleted site should not.
     361            $this->assertFileExists( $file1['file'] );
     362            $this->assertFileNotExists( $file2['file'] );
     363
     364            wpmu_delete_blog( $blog_id, true );
     365
     366            // The file on the main site should still exist. The file on the deleted site should not.
     367            $this->assertFileExists( $file1['file'] );
     368            $this->assertFileNotExists( $file2['file'] );
     369        }
     370
     371        function test_wpmu_update_blogs_date() {
     372            global $wpdb;
     373
     374            wpmu_update_blogs_date();
     375
     376            // compare the update time with the current time, allow delta < 2
     377            $blog            = get_site( get_current_blog_id() );
     378            $current_time    = time();
     379            $time_difference = $current_time - strtotime( $blog->last_updated );
     380            $this->assertLessThan( 2, $time_difference );
     381        }
     382
     383        /**
     384         * Provide a counter to determine that hooks are firing when intended.
     385         */
     386        function _action_counter_cb() {
     387            global $test_action_counter;
     388            $test_action_counter++;
     389        }
     390
     391        /**
     392         * Test cached data for a site that does not exist and then again after it exists.
     393         *
     394         * @ticket 23405
     395         */
     396        function test_get_blog_details_when_site_does_not_exist() {
     397            // Create an unused site so that we can then assume an invalid site ID.
     398            $blog_id = self::factory()->blog->create();
     399            $blog_id++;
     400
     401            // Prime the cache for an invalid site.
     402            get_blog_details( $blog_id );
     403
     404            // When the cache is primed with an invalid site, the value is set to -1.
     405            $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
     406
     407            // Create a site in the invalid site's place.
     408            self::factory()->blog->create();
     409
     410            // When a new site is created, its cache is cleared through refresh_blog_details.
     411            $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
     412
     413            $blog = get_blog_details( $blog_id );
     414
     415            // When the cache is refreshed, it should now equal the site data.
     416            $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
     417        }
     418
     419        /**
     420         * Updating a field returns the sme value that was passed.
     421         */
     422        function test_update_blog_status() {
     423            $result = update_blog_status( 1, 'spam', 0 );
     424            $this->assertEquals( 0, $result );
     425        }
     426
     427        /**
     428         * Updating an invalid field returns the same value that was passed.
     429         */
     430        function test_update_blog_status_invalid_status() {
     431            $result = update_blog_status( 1, 'doesnotexist', 'invalid' );
     432            $this->assertEquals( 'invalid', $result );
     433        }
     434
     435        function test_update_blog_status_make_ham_blog_action() {
     436            global $test_action_counter;
     437            $test_action_counter = 0;
     438
     439            $blog_id = self::factory()->blog->create();
     440            update_blog_details( $blog_id, array( 'spam' => 1 ) );
     441
     442            add_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 );
     443            update_blog_status( $blog_id, 'spam', 0 );
     444            $blog = get_site( $blog_id );
     445
     446            $this->assertEquals( '0', $blog->spam );
     447            $this->assertEquals( 1, $test_action_counter );
     448
     449            // The action should fire if the status of 'spam' stays the same.
     450            update_blog_status( $blog_id, 'spam', 0 );
     451            $blog = get_site( $blog_id );
     452
     453            $this->assertEquals( '0', $blog->spam );
     454            $this->assertEquals( 2, $test_action_counter );
     455
     456            remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 );
     457        }
     458
     459        function test_update_blog_status_make_spam_blog_action() {
     460            global $test_action_counter;
     461            $test_action_counter = 0;
     462
     463            $blog_id = self::factory()->blog->create();
     464
     465            add_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 );
     466            update_blog_status( $blog_id, 'spam', 1 );
     467            $blog = get_site( $blog_id );
     468
     469            $this->assertEquals( '1', $blog->spam );
     470            $this->assertEquals( 1, $test_action_counter );
     471
     472            // The action should fire if the status of 'spam' stays the same.
     473            update_blog_status( $blog_id, 'spam', 1 );
     474            $blog = get_site( $blog_id );
     475
     476            $this->assertEquals( '1', $blog->spam );
     477            $this->assertEquals( 2, $test_action_counter );
     478
     479            remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 );
     480        }
     481
     482        function test_update_blog_status_archive_blog_action() {
     483            global $test_action_counter;
     484            $test_action_counter = 0;
     485
     486            $blog_id = self::factory()->blog->create();
     487
     488            add_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 );
     489            update_blog_status( $blog_id, 'archived', 1 );
     490            $blog = get_site( $blog_id );
     491
     492            $this->assertEquals( '1', $blog->archived );
     493            $this->assertEquals( 1, $test_action_counter );
     494
     495            // The action should fire if the status of 'archived' stays the same.
     496            update_blog_status( $blog_id, 'archived', 1 );
     497            $blog = get_site( $blog_id );
     498
     499            $this->assertEquals( '1', $blog->archived );
     500            $this->assertEquals( 2, $test_action_counter );
     501
     502            remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 );
     503        }
     504
     505        function test_update_blog_status_unarchive_blog_action() {
     506            global $test_action_counter;
     507            $test_action_counter = 0;
     508
     509            $blog_id = self::factory()->blog->create();
     510            update_blog_details( $blog_id, array( 'archived' => 1 ) );
     511
     512            add_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 );
     513            update_blog_status( $blog_id, 'archived', 0 );
     514            $blog = get_site( $blog_id );
     515
     516            $this->assertEquals( '0', $blog->archived );
     517            $this->assertEquals( 1, $test_action_counter );
     518
     519            // The action should fire if the status of 'archived' stays the same.
     520            update_blog_status( $blog_id, 'archived', 0 );
     521            $blog = get_site( $blog_id );
     522            $this->assertEquals( '0', $blog->archived );
     523            $this->assertEquals( 2, $test_action_counter );
     524
     525            remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 );
     526        }
     527
     528        function test_update_blog_status_make_delete_blog_action() {
     529            global $test_action_counter;
     530            $test_action_counter = 0;
     531
     532            $blog_id = self::factory()->blog->create();
     533
     534            add_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 );
     535            update_blog_status( $blog_id, 'deleted', 1 );
     536            $blog = get_site( $blog_id );
     537
     538            $this->assertEquals( '1', $blog->deleted );
     539            $this->assertEquals( 1, $test_action_counter );
     540
     541            // The action should fire if the status of 'deleted' stays the same.
     542            update_blog_status( $blog_id, 'deleted', 1 );
     543            $blog = get_site( $blog_id );
     544
     545            $this->assertEquals( '1', $blog->deleted );
     546            $this->assertEquals( 2, $test_action_counter );
     547
     548            remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 );
     549        }
     550
     551        function test_update_blog_status_make_undelete_blog_action() {
     552            global $test_action_counter;
     553            $test_action_counter = 0;
     554
     555            $blog_id = self::factory()->blog->create();
     556            update_blog_details( $blog_id, array( 'deleted' => 1 ) );
     557
     558            add_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 );
     559            update_blog_status( $blog_id, 'deleted', 0 );
     560            $blog = get_site( $blog_id );
     561
     562            $this->assertEquals( '0', $blog->deleted );
     563            $this->assertEquals( 1, $test_action_counter );
     564
     565            // The action should fire if the status of 'deleted' stays the same.
     566            update_blog_status( $blog_id, 'deleted', 0 );
     567            $blog = get_site( $blog_id );
     568
     569            $this->assertEquals( '0', $blog->deleted );
     570            $this->assertEquals( 2, $test_action_counter );
     571
     572            remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 );
     573        }
     574
     575        function test_update_blog_status_mature_blog_action() {
     576            global $test_action_counter;
     577            $test_action_counter = 0;
     578
     579            $blog_id = self::factory()->blog->create();
     580
     581            add_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 );
     582            update_blog_status( $blog_id, 'mature', 1 );
     583            $blog = get_site( $blog_id );
     584
     585            $this->assertEquals( '1', $blog->mature );
     586            $this->assertEquals( 1, $test_action_counter );
     587
     588            // The action should fire if the status of 'mature' stays the same.
     589            update_blog_status( $blog_id, 'mature', 1 );
     590            $blog = get_site( $blog_id );
     591
     592            $this->assertEquals( '1', $blog->mature );
     593            $this->assertEquals( 2, $test_action_counter );
     594
     595            remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 );
     596        }
     597
     598        function test_update_blog_status_unmature_blog_action() {
     599            global $test_action_counter;
     600            $test_action_counter = 0;
     601
     602            $blog_id = self::factory()->blog->create();
     603            update_blog_details( $blog_id, array( 'mature' => 1 ) );
     604
     605            add_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 );
     606            update_blog_status( $blog_id, 'mature', 0 );
     607
     608            $blog = get_site( $blog_id );
     609            $this->assertEquals( '0', $blog->mature );
     610            $this->assertEquals( 1, $test_action_counter );
     611
     612            // The action should fire if the status of 'mature' stays the same.
     613            update_blog_status( $blog_id, 'mature', 0 );
     614            $blog = get_site( $blog_id );
     615
     616            $this->assertEquals( '0', $blog->mature );
     617            $this->assertEquals( 2, $test_action_counter );
     618
     619            remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 );
     620        }
     621
     622        function test_update_blog_status_update_blog_public_action() {
     623            global $test_action_counter;
     624            $test_action_counter = 0;
     625
     626            $blog_id = self::factory()->blog->create();
     627
     628            add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 );
     629            update_blog_status( $blog_id, 'public', 0 );
     630
     631            $blog = get_site( $blog_id );
     632            $this->assertEquals( '0', $blog->public );
     633            $this->assertEquals( 1, $test_action_counter );
     634
     635            // The action should fire if the status of 'mature' stays the same.
     636            update_blog_status( $blog_id, 'public', 0 );
     637            $blog = get_site( $blog_id );
     638
     639            $this->assertEquals( '0', $blog->public );
     640            $this->assertEquals( 2, $test_action_counter );
     641
     642            remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 );
     643        }
     644
     645        /**
     646         * @ticket 27952
     647         */
     648        function test_posts_count() {
     649            self::factory()->post->create();
     650            $post2 = self::factory()->post->create();
     651            $this->assertEquals( 2, get_site()->post_count );
     652
     653            wp_delete_post( $post2 );
     654            $this->assertEquals( 1, get_site()->post_count );
     655        }
     656
     657        /**
     658         * @ticket 26410
     659         */
     660        function test_blog_details_cache_invalidation() {
     661            update_option( 'blogname', 'foo' );
     662            $details = get_site( get_current_blog_id() );
     663            $this->assertEquals( 'foo', $details->blogname );
     664
     665            update_option( 'blogname', 'bar' );
     666            $details = get_site( get_current_blog_id() );
     667            $this->assertEquals( 'bar', $details->blogname );
     668        }
     669
     670        /**
     671         * Test the original and cached responses for a created and then deleted site when
     672         * the blog ID is requested through get_blog_id_from_url().
     673         */
     674        function test_get_blog_id_from_url() {
     675            $blog_id = self::factory()->blog->create();
     676            $details = get_site( $blog_id );
     677            $key     = md5( $details->domain . $details->path );
     678
     679            // Test the original response and cached response for the newly created site.
     680            $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
     681            $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
     682        }
     683
     684        /**
     685         * Test the case insensitivity of the site lookup.
     686         */
     687        function test_get_blog_id_from_url_is_case_insensitive() {
     688            $blog_id = self::factory()->blog->create(
     689                array(
     690                    'domain' => 'example.com',
     691                    'path'   => '/xyz',
     692                )
     693            );
     694            $details = get_site( $blog_id );
     695
     696            $this->assertEquals( $blog_id, get_blog_id_from_url( strtoupper( $details->domain ), strtoupper( $details->path ) ) );
     697        }
     698
     699        /**
     700         * Test the first and cached responses for a site that does not exist.
     701         */
     702        function test_get_blog_id_from_url_that_does_not_exist() {
     703            $blog_id = self::factory()->blog->create( array( 'path' => '/xyz' ) );
     704            $details = get_site( $blog_id );
     705
     706            $this->assertEquals( 0, get_blog_id_from_url( $details->domain, 'foo' ) );
     707            $this->assertEquals( -1, wp_cache_get( md5( $details->domain . 'foo' ), 'blog-id-cache' ) );
     708        }
     709
     710        /**
     711         * A blog ID is still available if only the `deleted` flag is set for a site. The same
     712         * behavior would be expected if passing `false` explicitly to `wpmu_delete_blog()`.
     713         */
     714        function test_get_blog_id_from_url_with_deleted_flag() {
     715            $blog_id = self::factory()->blog->create();
     716            $details = get_site( $blog_id );
     717            $key     = md5( $details->domain . $details->path );
     718            wpmu_delete_blog( $blog_id );
     719
     720            $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
     721            $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
     722        }
     723
     724        /**
     725         * When deleted with the drop parameter as true, the cache will first be false, then set to
     726         * -1 after an attempt at `get_blog_id_from_url()` is made.
     727         */
     728        function test_get_blog_id_from_url_after_dropped() {
     729            $blog_id = self::factory()->blog->create();
     730            $details = get_site( $blog_id );
     731            $key     = md5( $details->domain . $details->path );
     732            wpmu_delete_blog( $blog_id, true );
     733
     734            $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
     735            $this->assertEquals( 0, get_blog_id_from_url( $details->domain, $details->path ) );
     736            $this->assertEquals( -1, wp_cache_get( $key, 'blog-id-cache' ) );
     737        }
     738
     739        /**
     740         * Test with default parameter of site_id as null.
     741         */
     742        function test_is_main_site() {
     743            $this->assertTrue( is_main_site() );
     744        }
     745
     746        /**
     747         * Test with a site id of get_current_blog_id(), which should be the same as the
     748         * default parameter tested above.
     749         */
     750        function test_current_blog_id_is_main_site() {
     751            $this->assertTrue( is_main_site( get_current_blog_id() ) );
     752        }
     753
     754        /**
     755         * Test with a site ID other than the main site to ensure a false response.
     756         */
     757        function test_is_main_site_is_false_with_other_blog_id() {
     758            $blog_id = self::factory()->blog->create();
     759
     760            $this->assertFalse( is_main_site( $blog_id ) );
     761        }
     762
     763        /**
     764         * Test with no passed ID after switching to another site ID.
     765         */
     766        function test_is_main_site_is_false_after_switch_to_blog() {
     767            $blog_id = self::factory()->blog->create();
     768            switch_to_blog( $blog_id );
     769
     770            $this->assertFalse( is_main_site() );
     771
     772            restore_current_blog();
     773        }
     774
     775        function test_switch_upload_dir() {
     776            $this->assertTrue( is_main_site() );
     777
     778            $site = get_current_site();
     779
     780            $info = wp_upload_dir();
     781            $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['url'] );
     782            $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['path'] );
     783            $this->assertEquals( gmstrftime( '/%Y/%m' ), $info['subdir'] );
     784            $this->assertEquals( '', $info['error'] );
     785
     786            $blog_id = self::factory()->blog->create();
     787
     788            switch_to_blog( $blog_id );
     789            $info = wp_upload_dir();
     790            $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime( '%Y/%m' ), $info['url'] );
     791            $this->assertEquals( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime( '%Y/%m' ), $info['path'] );
     792            $this->assertEquals( gmstrftime( '/%Y/%m' ), $info['subdir'] );
     793            $this->assertEquals( '', $info['error'] );
     794            restore_current_blog();
     795
     796            $info = wp_upload_dir();
     797            $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['url'] );
     798            $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['path'] );
     799            $this->assertEquals( gmstrftime( '/%Y/%m' ), $info['subdir'] );
     800            $this->assertEquals( '', $info['error'] );
     801        }
     802
     803        /**
     804         * Test the primary purpose of get_blog_post(), to retrieve a post from
     805         * another site on the network.
     806         */
     807        function test_get_blog_post_from_another_site_on_network() {
     808            $blog_id = self::factory()->blog->create();
     809            $post_id = self::factory()->post->create(); // Create a post on the primary site, ID 1.
     810            $post    = get_post( $post_id );
     811            switch_to_blog( $blog_id );
     812
     813            // The post created and retrieved on the main site should match the one retrieved "remotely".
     814            $this->assertEquals( $post, get_blog_post( 1, $post_id ) );
     815
     816            restore_current_blog();
     817        }
     818
     819        /**
     820         * If get_blog_post() is used on the same site, it should still work.
     821         */
     822        function test_get_blog_post_from_same_site() {
     823            $post_id = self::factory()->post->create();
     824
     825            $this->assertEquals( get_blog_post( 1, $post_id ), get_post( $post_id ) );
     826        }
     827
     828        /**
     829         * A null response should be returned if an invalid post is requested.
     830         */
     831        function test_get_blog_post_invalid_returns_null() {
     832            $this->assertNull( get_blog_post( 1, 999999 ) );
     833        }
     834
     835        /**
     836         * Added as a callback to the domain_exists filter to provide manual results for
     837         * the testing of the filter and for a test which does not need the database.
     838         */
     839        function _domain_exists_cb( $exists, $domain, $path, $site_id ) {
     840            if ( 'foo' == $domain && 'bar/' == $path ) {
     841                return 1234;
     842            } else {
     843                return null;
     844            }
     845        }
     846
     847        function test_domain_exists_with_default_site_id() {
     848            $details = get_site( 1 );
     849
     850            $this->assertEquals( 1, domain_exists( $details->domain, $details->path ) );
     851        }
     852
     853        function test_domain_exists_with_specified_site_id() {
     854            $details = get_site( 1 );
     855
     856            $this->assertEquals( 1, domain_exists( $details->domain, $details->path, $details->site_id ) );
     857        }
     858
     859        /**
     860         * When the domain is valid, but the resulting site does not belong to the specified network,
     861         * it is marked as not existing.
     862         */
     863        function test_domain_does_not_exist_with_invalid_site_id() {
     864            $details = get_site( 1 );
     865
     866            $this->assertEquals( null, domain_exists( $details->domain, $details->path, 999 ) );
     867        }
     868
     869        function test_invalid_domain_does_not_exist_with_default_site_id() {
     870            $this->assertEquals( null, domain_exists( 'foo', 'bar' ) );
     871        }
     872
     873        function test_domain_filtered_to_exist() {
     874            add_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
     875            $exists = domain_exists( 'foo', 'bar' );
     876            remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
     877            $this->assertEquals( 1234, $exists );
     878        }
     879
     880        /**
     881         * When a path is passed to domain_exists, it is immediately trailing slashed. A path
     882         * value with or without the slash should result in the same return value.
     883         */
     884        function test_slashed_path_in_domain_exists() {
     885            add_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
     886            $exists1 = domain_exists( 'foo', 'bar' );
     887            $exists2 = domain_exists( 'foo', 'bar/' );
     888            remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
     889
     890            // Make sure the same result is returned with or without a trailing slash
     891            $this->assertEquals( $exists1, $exists2 );
     892        }
     893
     894        /**
     895         * Tests returning an address for a given valid id.
     896         */
     897        function test_get_blogaddress_by_id_with_valid_id() {
     898            $blogaddress = get_blogaddress_by_id( 1 );
     899            $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/', $blogaddress );
     900        }
     901
     902        /**
     903         * Tests returning the appropriate response for a invalid id given.
     904         */
     905        function test_get_blogaddress_by_id_with_invalid_id() {
     906            $blogaddress = get_blogaddress_by_id( 42 );
     907            $this->assertEquals( '', $blogaddress );
     908        }
     909
     910        /**
     911         * @ticket 14867
     912         */
     913        function test_get_blogaddress_by_id_scheme_reflects_blog_scheme() {
     914            $blog = self::factory()->blog->create();
     915
     916            $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
     917
     918            update_blog_option( $blog, 'home', set_url_scheme( get_blog_option( $blog, 'home' ), 'https' ) );
     919
     920            $this->assertSame( 'https', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
     921        }
     922
     923        /**
     924         * @ticket 14867
     925         */
     926        function test_get_blogaddress_by_id_scheme_is_unaffected_by_request() {
     927            $blog = self::factory()->blog->create();
     928
     929            $this->assertFalse( is_ssl() );
     930            $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
     931
     932            $_SERVER['HTTPS'] = 'on';
     933
     934            $is_ssl  = is_ssl();
     935            $address = parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME );
     936
     937            $this->assertTrue( $is_ssl );
     938            $this->assertSame( 'http', $address );
     939        }
     940
     941        /**
     942         * @ticket 33620
     943         * @dataProvider data_new_blog_url_schemes
     944         */
     945        function test_new_blog_url_schemes( $home_scheme, $siteurl_scheme, $force_ssl_admin ) {
     946            $current_site = get_current_site();
     947
     948            $home    = get_option( 'home' );
     949            $siteurl = get_site_option( 'siteurl' );
     950            $admin   = force_ssl_admin();
     951
     952            // Setup:
     953            update_option( 'home', set_url_scheme( $home, $home_scheme ) );
     954            update_site_option( 'siteurl', set_url_scheme( $siteurl, $siteurl_scheme ) );
     955            force_ssl_admin( $force_ssl_admin );
     956
     957            // Install:
     958            $new = wpmu_create_blog( $current_site->domain, '/new-blog/', 'New Blog', get_current_user_id() );
     959
     960            // Reset:
     961            update_option( 'home', $home );
     962            update_site_option( 'siteurl', $siteurl );
     963            force_ssl_admin( $admin );
     964
     965            // Assert:
     966            $this->assertNotWPError( $new );
     967            $this->assertSame( $home_scheme, parse_url( get_blog_option( $new, 'home' ), PHP_URL_SCHEME ) );
     968            $this->assertSame( $siteurl_scheme, parse_url( get_blog_option( $new, 'siteurl' ), PHP_URL_SCHEME ) );
     969        }
     970
     971        function data_new_blog_url_schemes() {
     972            return array(
     973                array(
     974                    'https',
     975                    'https',
     976                    false,
     977                ),
     978                array(
     979                    'http',
     980                    'https',
     981                    false,
     982                ),
     983                array(
     984                    'https',
     985                    'http',
     986                    false,
     987                ),
     988                array(
     989                    'http',
     990                    'http',
     991                    false,
     992                ),
     993                array(
     994                    'http',
     995                    'http',
     996                    true,
     997                ),
     998            );
     999        }
     1000
     1001        /**
     1002         * @ticket 36918
     1003         */
     1004        function test_new_blog_locale() {
     1005            $current_site = get_current_site();
     1006
     1007            add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     1008            update_site_option( 'WPLANG', 'de_DE' );
     1009            remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     1010
     1011            // No locale, use default locale.
     1012            add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     1013            $blog_id = wpmu_create_blog( $current_site->domain, '/de-de/', 'New Blog', get_current_user_id() );
     1014            remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     1015
     1016            $this->assertNotWPError( $blog_id );
     1017            $this->assertSame( 'de_DE', get_blog_option( $blog_id, 'WPLANG' ) );
     1018
     1019            // Custom locale.
     1020            add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     1021            $blog_id = wpmu_create_blog( $current_site->domain, '/es-es/', 'New Blog', get_current_user_id(), array( 'WPLANG' => 'es_ES' ) );
     1022            remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     1023
     1024            $this->assertNotWPError( $blog_id );
     1025            $this->assertSame( 'es_ES', get_blog_option( $blog_id, 'WPLANG' ) );
     1026
     1027            // en_US locale.
     1028            add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     1029            $blog_id = wpmu_create_blog( $current_site->domain, '/en-us/', 'New Blog', get_current_user_id(), array( 'WPLANG' => '' ) );
     1030            remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     1031
     1032            $this->assertNotWPError( $blog_id );
     1033            $this->assertSame( '', get_blog_option( $blog_id, 'WPLANG' ) );
     1034        }
     1035
     1036        /**
     1037         * @ticket 40503
     1038         */
     1039        function test_different_network_language() {
     1040            $network = get_network( self::$network_ids['make.wordpress.org/'] );
     1041
     1042            add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     1043
     1044            update_network_option( self::$network_ids['make.wordpress.org/'], 'WPLANG', 'wibble' );
     1045            $blog_id = wpmu_create_blog( $network->domain, '/de-de/', 'New Blog', get_current_user_id(), array(), $network->id );
     1046
     1047            remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     1048
     1049            $this->assertSame( get_network_option( self::$network_ids['make.wordpress.org/'], 'WPLANG' ), get_blog_option( $blog_id, 'WPLANG' ) );
     1050        }
     1051
     1052        /**
     1053         * Allows to set the WPLANG option to any language.
     1054         *
     1055         * @param string $value          The sanitized option value.
     1056         * @param string $option         The option name.
     1057         * @param string $original_value The original value passed to the function.
     1058         * @return string The orginal value.
     1059         */
     1060        function filter_allow_unavailable_languages( $value, $option, $original_value ) {
     1061            return $original_value;
     1062        }
     1063
     1064        /**
     1065         * @ticket 29684
     1066         */
     1067        public function test_is_main_site_different_network() {
     1068            $this->assertTrue( is_main_site( self::$site_ids['make.wordpress.org/'], self::$network_ids['make.wordpress.org/'] ) );
     1069        }
     1070
     1071        /**
     1072         * @ticket 29684
     1073         */
     1074        public function test_is_main_site_different_network_random_site() {
     1075            $this->assertFalse( is_main_site( self::$site_ids['make.wordpress.org/foo/'], self::$network_ids['make.wordpress.org/'] ) );
     1076        }
     1077
     1078        /**
     1079         * @ticket 40201
     1080         * @dataProvider data_get_site_caches
     1081         */
     1082        public function test_clean_blog_cache( $key, $group ) {
     1083            $site = get_site( self::$site_ids['make.wordpress.org/'] );
     1084
     1085            $replacements = array(
     1086                '%blog_id%'         => $site->blog_id,
     1087                '%domain%'          => $site->domain,
     1088                '%path%'            => $site->path,
     1089                '%domain_path_key%' => md5( $site->domain . $site->path ),
     1090            );
     1091
     1092            $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
     1093
     1094            if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
     1095                wp_cache_set( $key, (object) $site->to_array(), $group );
     1096            } else {
     1097                wp_cache_set( $key, 'something', $group );
     1098            }
     1099
     1100            clean_blog_cache( $site );
     1101            $this->assertFalse( wp_cache_get( $key, $group ) );
     1102        }
     1103
     1104        /**
     1105         * @ticket 40201
     1106         * @dataProvider data_get_site_caches
     1107         */
     1108        public function test_clean_blog_cache_with_id( $key, $group ) {
     1109            $site = get_site( self::$site_ids['make.wordpress.org/'] );
     1110
     1111            $replacements = array(
     1112                '%blog_id%'         => $site->blog_id,
     1113                '%domain%'          => $site->domain,
     1114                '%path%'            => $site->path,
     1115                '%domain_path_key%' => md5( $site->domain . $site->path ),
     1116            );
     1117
     1118            $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
     1119
     1120            if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
     1121                wp_cache_set( $key, (object) $site->to_array(), $group );
     1122            } else {
     1123                wp_cache_set( $key, 'something', $group );
     1124            }
     1125
     1126            clean_blog_cache( $site->blog_id );
     1127            $this->assertFalse( wp_cache_get( $key, $group ) );
     1128        }
     1129
     1130        /**
     1131         * @ticket 40201
     1132         */
     1133        public function test_clean_blog_cache_resets_last_changed() {
     1134            $site = get_site( self::$site_ids['make.wordpress.org/'] );
     1135
     1136            wp_cache_delete( 'last_changed', 'sites' );
     1137
     1138            clean_blog_cache( $site );
     1139            $this->assertNotFalse( wp_cache_get( 'last_changed', 'sites' ) );
     1140        }
     1141
     1142        /**
     1143         * @ticket 40201
     1144         */
     1145        public function test_clean_blog_cache_fires_action() {
     1146            $site = get_site( self::$site_ids['make.wordpress.org/'] );
     1147
     1148            $old_count = did_action( 'clean_site_cache' );
     1149
     1150            clean_blog_cache( $site );
     1151            $this->assertEquals( $old_count + 1, did_action( 'clean_site_cache' ) );
     1152        }
     1153
     1154        /**
     1155         * @ticket 40201
     1156         */
     1157        public function test_clean_blog_cache_bails_on_suspend_cache_invalidation() {
     1158            $site = get_site( self::$site_ids['make.wordpress.org/'] );
     1159
     1160            $old_count = did_action( 'clean_site_cache' );
     1161
     1162            $suspend = wp_suspend_cache_invalidation();
     1163            clean_blog_cache( $site );
     1164            wp_suspend_cache_invalidation( $suspend );
     1165            $this->assertEquals( $old_count, did_action( 'clean_site_cache' ) );
     1166        }
     1167
     1168        /**
     1169         * @ticket 40201
     1170         */
     1171        public function test_clean_blog_cache_bails_on_empty_input() {
     1172            $old_count = did_action( 'clean_site_cache' );
     1173
     1174            clean_blog_cache( null );
     1175            $this->assertEquals( $old_count, did_action( 'clean_site_cache' ) );
     1176        }
     1177
     1178        /**
     1179         * @ticket 40201
     1180         */
     1181        public function test_clean_blog_cache_bails_on_non_numeric_input() {
     1182            $old_count = did_action( 'clean_site_cache' );
     1183
     1184            clean_blog_cache( 'something' );
     1185            $this->assertEquals( $old_count, did_action( 'clean_site_cache' ) );
     1186        }
     1187
     1188        /**
     1189         * @ticket 40201
     1190         */
     1191        public function test_clean_blog_cache_works_with_deleted_site() {
     1192            $site_id = 12345;
     1193
     1194            wp_cache_set( $site_id, 'something', 'site-details' );
     1195
     1196            clean_blog_cache( $site_id );
     1197            $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
     1198        }
     1199
     1200        /**
     1201         * @ticket 40201
     1202         * @dataProvider data_get_site_caches
     1203         */
     1204        public function test_refresh_blog_details( $key, $group ) {
     1205            $site = get_site( self::$site_ids['make.wordpress.org/'] );
     1206
     1207            $replacements = array(
     1208                '%blog_id%'         => $site->blog_id,
     1209                '%domain%'          => $site->domain,
     1210                '%path%'            => $site->path,
     1211                '%domain_path_key%' => md5( $site->domain . $site->path ),
     1212            );
     1213
     1214            $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
     1215
     1216            if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
     1217                wp_cache_set( $key, (object) $site->to_array(), $group );
     1218            } else {
     1219                wp_cache_set( $key, 'something', $group );
     1220            }
     1221
     1222            refresh_blog_details( $site->blog_id );
     1223            $this->assertFalse( wp_cache_get( $key, $group ) );
     1224        }
     1225
     1226        /**
     1227         * @ticket 40201
     1228         */
     1229        public function test_refresh_blog_details_works_with_deleted_site() {
     1230            $site_id = 12345;
     1231
     1232            wp_cache_set( $site_id, 'something', 'site-details' );
     1233
     1234            refresh_blog_details( $site_id );
     1235            $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
     1236        }
     1237
     1238        /**
     1239         * @ticket 40201
     1240         */
     1241        public function test_refresh_blog_details_uses_current_site_as_default() {
     1242            $site_id = get_current_blog_id();
     1243
     1244            wp_cache_set( $site_id, 'something', 'site-details' );
     1245
     1246            refresh_blog_details();
     1247            $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
     1248        }
     1249
     1250        public function data_get_site_caches() {
     1251            return array(
     1252                array( '%blog_id%', 'sites' ),
     1253                array( '%blog_id%', 'site-details' ),
     1254                array( '%blog_id%', 'blog-details' ),
     1255                array( '%blog_id%' . 'short', 'blog-details' ),
     1256                array( '%domain_path_key%', 'blog-lookup' ),
     1257                array( '%domain_path_key%', 'blog-id-cache' ),
     1258                array( 'current_blog_%domain%', 'site-options' ),
     1259                array( 'current_blog_%domain%%path%', 'site-options' ),
     1260            );
     1261        }
    201262    }
    211263
    22     function tearDown() {
    23         global $wpdb;
    24         $wpdb->suppress_errors( $this->suppress );
    25         parent::tearDown();
    26     }
    27 
    28     public static function wpSetUpBeforeClass( $factory ) {
    29         self::$network_ids = array(
    30             'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
    31         );
    32 
    33         foreach ( self::$network_ids as &$id ) {
    34             $id = $factory->network->create( $id );
    35         }
    36         unset( $id );
    37 
    38         self::$site_ids = array(
    39             'make.wordpress.org/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/',         'site_id' => self::$network_ids['make.wordpress.org/'] ),
    40             'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/',     'site_id' => self::$network_ids['make.wordpress.org/'] ),
    41         );
    42 
    43         foreach ( self::$site_ids as &$id ) {
    44             $id = $factory->blog->create( $id );
    45         }
    46         unset( $id );
    47     }
    48 
    49     public static function wpTearDownAfterClass() {
    50         global $wpdb;
    51 
    52         foreach( self::$site_ids as $id ) {
    53             wpmu_delete_blog( $id, true );
    54         }
    55 
    56         foreach( self::$network_ids as $id ) {
    57             $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    58             $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    59         }
    60     }
    61 
    62     function test_switch_restore_blog() {
    63         global $_wp_switched_stack, $wpdb;
    64 
    65         $this->assertEquals( array(), $_wp_switched_stack );
    66         $this->assertFalse( ms_is_switched() );
    67         $current_blog_id = get_current_blog_id();
    68         $this->assertInternalType( 'integer', $current_blog_id );
    69 
    70         wp_cache_set( 'switch-test', $current_blog_id, 'switch-test' );
    71         $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    72 
    73         $blog_id = self::factory()->blog->create();
    74 
    75         $cap_key = wp_get_current_user()->cap_key;
    76         switch_to_blog( $blog_id );
    77         $this->assertNotEquals( $cap_key, wp_get_current_user()->cap_key );
    78         $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack );
    79         $this->assertTrue( ms_is_switched() );
    80         $this->assertEquals( $blog_id, $wpdb->blogid );
    81         $this->assertFalse( wp_cache_get( 'switch-test', 'switch-test' ) );
    82         wp_cache_set( 'switch-test', $blog_id, 'switch-test' );
    83         $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    84 
    85         switch_to_blog( $blog_id );
    86         $this->assertEquals( array( $current_blog_id, $blog_id ), $_wp_switched_stack );
    87         $this->assertTrue( ms_is_switched() );
    88         $this->assertEquals( $blog_id, $wpdb->blogid );
    89         $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    90 
    91         restore_current_blog();
    92         $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack );
    93         $this->assertTrue( ms_is_switched() );
    94         $this->assertEquals( $blog_id, $wpdb->blogid );
    95         $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    96 
    97         restore_current_blog();
    98         $this->assertEquals( $cap_key, wp_get_current_user()->cap_key );
    99         $this->assertEquals( $current_blog_id, get_current_blog_id() );
    100         $this->assertEquals( array(), $_wp_switched_stack );
    101         $this->assertFalse( ms_is_switched() );
    102         $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    103 
    104         $this->assertFalse( restore_current_blog() );
    105     }
    106 
    107     /**
    108      * Test the cache keys and database tables setup through the creation of a site.
    109      */
    110     function test_created_site_details() {
    111         global $wpdb;
    112 
    113         $blog_id = self::factory()->blog->create();
    114 
    115         $this->assertInternalType( 'int', $blog_id );
    116         $prefix = $wpdb->get_blog_prefix( $blog_id );
    117 
    118         // $get_all = false, only retrieve details from the blogs table
    119         $details = get_blog_details( $blog_id, false );
    120 
    121         // Combine domain and path for a site specific cache key.
    122         $key = md5( $details->domain . $details->path );
    123 
    124         $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    125 
    126         // get_blogaddress_by_name()
    127         $this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
    128 
    129         // These are empty until get_blog_details() is called with $get_all = true
    130         $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    131         $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
    132 
    133         // $get_all = true, populate the full blog-details cache and the blog slug lookup cache
    134         $details = get_blog_details( $blog_id, true );
    135         $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
    136         $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
    137 
    138         // Check existence of each database table for the created site.
    139         foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    140             $suppress     = $wpdb->suppress_errors();
    141             $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    142             $wpdb->suppress_errors( $suppress );
    143 
    144             // The table should exist.
    145             $this->assertNotEmpty( $table_fields );
    146 
    147             // And the table should not be empty, unless commentmeta, termmeta, or links.
    148             $result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
    149             if ( 'commentmeta' == $table || 'termmeta' == $table || 'links' == $table ) {
    150                 $this->assertEmpty( $result );
    151             } else {
    152                 $this->assertNotEmpty( $result );
    153             }
    154         }
    155 
    156         // update the blog count cache to use get_blog_count()
    157         wp_update_network_counts();
    158         $this->assertEquals( 2, (int) get_blog_count() );
    159     }
    160 
    161     public function test_site_caches_should_invalidate_when_invalidation_is_not_suspended() {
    162         $site_id = self::factory()->blog->create();
    163 
    164         $details = get_site( $site_id );
    165 
    166         $suspend = wp_suspend_cache_invalidation( false );
    167         update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
    168         $new_details = get_site( $site_id );
    169         wp_suspend_cache_invalidation( $suspend );
    170 
    171         $this->assertNotEquals( $details->path, $new_details->path );
    172     }
    173 
    174     public function test_site_caches_should_not_invalidate_when_invalidation_is_suspended() {
    175         $site_id = self::factory()->blog->create();
    176 
    177         $details = get_site( $site_id );
    178 
    179         $suspend = wp_suspend_cache_invalidation();
    180         update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
    181         $new_details = get_site( $site_id );
    182         wp_suspend_cache_invalidation( $suspend );
    183 
    184         $this->assertEquals( $details->path, $new_details->path );
    185     }
    186 
    187     /**
    188      * When a site is flagged as 'deleted', its data should be cleared from cache.
    189      */
    190     function test_data_in_cache_after_wpmu_delete_blog_drop_false() {
    191         $blog_id = self::factory()->blog->create();
    192 
    193         $details = get_blog_details( $blog_id, false );
    194         $key = md5( $details->domain . $details->path );
    195 
    196         // Delete the site without forcing a table drop.
    197         wpmu_delete_blog( $blog_id, false );
    198 
    199         $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    200         $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    201         $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
    202         $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    203     }
    204 
    205     /**
    206      * When a site is flagged as 'deleted', its data should remain in the database.
    207      */
    208     function test_data_in_tables_after_wpmu_delete_blog_drop_false() {
    209         global $wpdb;
    210 
    211         $blog_id = self::factory()->blog->create();
    212 
    213         // Delete the site without forcing a table drop.
    214         wpmu_delete_blog( $blog_id, false );
    215 
    216         $prefix = $wpdb->get_blog_prefix( $blog_id );
    217         foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    218             $suppress = $wpdb->suppress_errors();
    219             $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    220             $wpdb->suppress_errors( $suppress );
    221             $this->assertNotEmpty( $table_fields, $prefix . $table );
    222         }
    223     }
    224 
    225     /**
    226      * When a site is fully deleted, its data should be cleared from cache.
    227      */
    228     function test_data_in_cache_after_wpmu_delete_blog_drop_true() {
    229         $blog_id = self::factory()->blog->create();
    230 
    231         $details = get_blog_details( $blog_id, false );
    232         $key = md5( $details->domain . $details->path );
    233 
    234         // Delete the site and force a table drop.
    235         wpmu_delete_blog( $blog_id, true );
    236 
    237         $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    238         $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    239         $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
    240         $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    241     }
    242 
    243     /**
    244      * When a site is fully deleted, its data should be removed from the database.
    245      */
    246     function test_data_in_tables_after_wpmu_delete_blog_drop_true() {
    247         global $wpdb;
    248 
    249         $blog_id = self::factory()->blog->create();
    250 
    251         // Delete the site and force a table drop.
    252         wpmu_delete_blog( $blog_id, true );
    253 
    254         $prefix = $wpdb->get_blog_prefix( $blog_id );
    255         foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    256             $suppress = $wpdb->suppress_errors();
    257             $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    258             $wpdb->suppress_errors( $suppress );
    259             $this->assertEmpty( $table_fields );
    260         }
    261     }
    262 
    263     /**
    264      * When the main site of a network is fully deleted, its data should be cleared from cache.
    265      */
    266     function test_data_in_cache_after_wpmu_delete_blog_main_site_drop_true() {
    267         $blog_id = 1; // The main site in our test suite has an ID of 1.
    268 
    269         $details = get_blog_details( $blog_id, false );
    270         $key = md5( $details->domain . $details->path );
    271 
    272         // Delete the site and force a table drop.
    273         wpmu_delete_blog( $blog_id, true );
    274 
    275         $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    276         $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    277         $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
    278         $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    279     }
    280 
    281     /**
    282      * When the main site of a network is fully deleted, its data should remain in the database.
    283      */
    284     function test_data_in_tables_after_wpmu_delete_blog_main_site_drop_true() {
    285         global $wpdb;
    286 
    287         $blog_id = 1; // The main site in our test suite has an ID of 1.
    288 
    289         // Delete the site and force a table drop.
    290         wpmu_delete_blog( $blog_id, true );
    291 
    292         $prefix = $wpdb->get_blog_prefix( $blog_id );
    293         foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    294             $suppress = $wpdb->suppress_errors();
    295             $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    296             $wpdb->suppress_errors( $suppress );
    297             $this->assertNotEmpty( $table_fields, $prefix . $table );
    298         }
    299     }
    300 
    301     /**
    302      * The site count of a network should change when a site is flagged as 'deleted'.
    303      */
    304     function test_network_count_after_wpmu_delete_blog_drop_false() {
    305         $blog_id = self::factory()->blog->create();
    306 
    307         // Delete the site without forcing a table drop.
    308         wpmu_delete_blog( $blog_id, false );
    309 
    310         // update the blog count cache to use get_blog_count()
    311         wp_update_network_counts();
    312         $this->assertEquals( 1, get_blog_count() );
    313     }
    314 
    315     /**
    316      * The site count of a network should change when a site is fully deleted.
    317      */
    318     function test_blog_count_after_wpmu_delete_blog_drop_true() {
    319         $blog_id = self::factory()->blog->create();
    320 
    321         // Delete the site and force a table drop.
    322         wpmu_delete_blog( $blog_id, true );
    323 
    324         // update the blog count cache to use get_blog_count()
    325         wp_update_network_counts();
    326         $this->assertEquals( 1, get_blog_count() );
    327     }
    328 
    329     /**
    330      * When a site is deleted with wpmu_delete_blog(), only the files associated with
    331      * that site should be removed. When wpmu_delete_blog() is run a second time, nothing
    332      * should change with upload directories.
    333      */
    334     function test_upload_directories_after_multiple_wpmu_delete_blog() {
    335         $filename = __FUNCTION__ . '.jpg';
    336         $contents = __FUNCTION__ . '_contents';
    337 
    338         // Upload a file to the main site on the network.
    339         $file1 = wp_upload_bits( $filename, null, $contents );
    340 
    341         $blog_id = self::factory()->blog->create();
    342 
    343         switch_to_blog( $blog_id );
    344         $file2 = wp_upload_bits( $filename, null, $contents );
    345         restore_current_blog();
    346 
    347         wpmu_delete_blog( $blog_id, true );
    348 
    349         // The file on the main site should still exist. The file on the deleted site should not.
    350         $this->assertFileExists( $file1['file'] );
    351         $this->assertFileNotExists( $file2['file'] );
    352 
    353         wpmu_delete_blog( $blog_id, true );
    354 
    355         // The file on the main site should still exist. The file on the deleted site should not.
    356         $this->assertFileExists( $file1['file'] );
    357         $this->assertFileNotExists( $file2['file'] );
    358     }
    359 
    360     function test_wpmu_update_blogs_date() {
    361         global $wpdb;
    362 
    363         wpmu_update_blogs_date();
    364 
    365         // compare the update time with the current time, allow delta < 2
    366         $blog = get_site( get_current_blog_id() );
    367         $current_time = time();
    368         $time_difference = $current_time - strtotime( $blog->last_updated );
    369         $this->assertLessThan( 2, $time_difference );
    370     }
    371 
    372     /**
    373      * Provide a counter to determine that hooks are firing when intended.
    374      */
    375     function _action_counter_cb() {
    376         global $test_action_counter;
    377         $test_action_counter++;
    378     }
    379 
    380     /**
    381      * Test cached data for a site that does not exist and then again after it exists.
    382      *
    383      * @ticket 23405
    384      */
    385     function test_get_blog_details_when_site_does_not_exist() {
    386         // Create an unused site so that we can then assume an invalid site ID.
    387         $blog_id = self::factory()->blog->create();
    388         $blog_id++;
    389 
    390         // Prime the cache for an invalid site.
    391         get_blog_details( $blog_id );
    392 
    393         // When the cache is primed with an invalid site, the value is set to -1.
    394         $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
    395 
    396         // Create a site in the invalid site's place.
    397         self::factory()->blog->create();
    398 
    399         // When a new site is created, its cache is cleared through refresh_blog_details.
    400         $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' )  );
    401 
    402         $blog = get_blog_details( $blog_id );
    403 
    404         // When the cache is refreshed, it should now equal the site data.
    405         $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
    406     }
    407 
    408     /**
    409      * Updating a field returns the sme value that was passed.
    410      */
    411     function test_update_blog_status() {
    412         $result = update_blog_status( 1, 'spam', 0 );
    413         $this->assertEquals( 0, $result );
    414     }
    415 
    416     /**
    417      * Updating an invalid field returns the same value that was passed.
    418      */
    419     function test_update_blog_status_invalid_status() {
    420         $result = update_blog_status( 1, 'doesnotexist', 'invalid' );
    421         $this->assertEquals( 'invalid', $result );
    422     }
    423 
    424     function test_update_blog_status_make_ham_blog_action() {
    425         global $test_action_counter;
    426         $test_action_counter = 0;
    427 
    428         $blog_id = self::factory()->blog->create();
    429         update_blog_details( $blog_id, array( 'spam' => 1 ) );
    430 
    431         add_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 );
    432         update_blog_status( $blog_id, 'spam', 0 );
    433         $blog = get_site( $blog_id );
    434 
    435         $this->assertEquals( '0', $blog->spam );
    436         $this->assertEquals( 1, $test_action_counter );
    437 
    438         // The action should fire if the status of 'spam' stays the same.
    439         update_blog_status( $blog_id, 'spam', 0 );
    440         $blog = get_site( $blog_id );
    441 
    442         $this->assertEquals( '0', $blog->spam );
    443         $this->assertEquals( 2, $test_action_counter );
    444 
    445         remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 );
    446     }
    447 
    448     function test_update_blog_status_make_spam_blog_action() {
    449         global $test_action_counter;
    450         $test_action_counter = 0;
    451 
    452         $blog_id = self::factory()->blog->create();
    453 
    454         add_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 );
    455         update_blog_status( $blog_id, 'spam', 1 );
    456         $blog = get_site( $blog_id );
    457 
    458         $this->assertEquals( '1', $blog->spam );
    459         $this->assertEquals( 1, $test_action_counter );
    460 
    461         // The action should fire if the status of 'spam' stays the same.
    462         update_blog_status( $blog_id, 'spam', 1 );
    463         $blog = get_site( $blog_id );
    464 
    465         $this->assertEquals( '1', $blog->spam );
    466         $this->assertEquals( 2, $test_action_counter );
    467 
    468         remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 );
    469     }
    470 
    471     function test_update_blog_status_archive_blog_action() {
    472         global $test_action_counter;
    473         $test_action_counter = 0;
    474 
    475         $blog_id = self::factory()->blog->create();
    476 
    477         add_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 );
    478         update_blog_status( $blog_id, 'archived', 1 );
    479         $blog = get_site( $blog_id );
    480 
    481         $this->assertEquals( '1', $blog->archived );
    482         $this->assertEquals( 1, $test_action_counter );
    483 
    484         // The action should fire if the status of 'archived' stays the same.
    485         update_blog_status( $blog_id, 'archived', 1 );
    486         $blog = get_site( $blog_id );
    487 
    488         $this->assertEquals( '1', $blog->archived );
    489         $this->assertEquals( 2, $test_action_counter );
    490 
    491         remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 );
    492     }
    493 
    494     function test_update_blog_status_unarchive_blog_action() {
    495         global $test_action_counter;
    496         $test_action_counter = 0;
    497 
    498         $blog_id = self::factory()->blog->create();
    499         update_blog_details( $blog_id, array( 'archived' => 1 ) );
    500 
    501         add_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 );
    502         update_blog_status( $blog_id, 'archived', 0 );
    503         $blog = get_site( $blog_id );
    504 
    505         $this->assertEquals( '0', $blog->archived );
    506         $this->assertEquals( 1, $test_action_counter );
    507 
    508         // The action should fire if the status of 'archived' stays the same.
    509         update_blog_status( $blog_id, 'archived', 0 );
    510         $blog = get_site( $blog_id );
    511         $this->assertEquals( '0', $blog->archived );
    512         $this->assertEquals( 2, $test_action_counter );
    513 
    514         remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 );
    515     }
    516 
    517     function test_update_blog_status_make_delete_blog_action() {
    518         global $test_action_counter;
    519         $test_action_counter = 0;
    520 
    521         $blog_id = self::factory()->blog->create();
    522 
    523         add_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 );
    524         update_blog_status( $blog_id, 'deleted', 1 );
    525         $blog = get_site( $blog_id );
    526 
    527         $this->assertEquals( '1', $blog->deleted );
    528         $this->assertEquals( 1, $test_action_counter );
    529 
    530         // The action should fire if the status of 'deleted' stays the same.
    531         update_blog_status( $blog_id, 'deleted', 1 );
    532         $blog = get_site( $blog_id );
    533 
    534         $this->assertEquals( '1', $blog->deleted );
    535         $this->assertEquals( 2, $test_action_counter );
    536 
    537         remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 );
    538     }
    539 
    540     function test_update_blog_status_make_undelete_blog_action() {
    541         global $test_action_counter;
    542         $test_action_counter = 0;
    543 
    544         $blog_id = self::factory()->blog->create();
    545         update_blog_details( $blog_id, array( 'deleted' => 1 ) );
    546 
    547         add_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 );
    548         update_blog_status( $blog_id, 'deleted', 0 );
    549         $blog = get_site( $blog_id );
    550 
    551         $this->assertEquals( '0', $blog->deleted );
    552         $this->assertEquals( 1, $test_action_counter );
    553 
    554         // The action should fire if the status of 'deleted' stays the same.
    555         update_blog_status( $blog_id, 'deleted', 0 );
    556         $blog = get_site( $blog_id );
    557 
    558         $this->assertEquals( '0', $blog->deleted );
    559         $this->assertEquals( 2, $test_action_counter );
    560 
    561         remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 );
    562     }
    563 
    564     function test_update_blog_status_mature_blog_action() {
    565         global $test_action_counter;
    566         $test_action_counter = 0;
    567 
    568         $blog_id = self::factory()->blog->create();
    569 
    570         add_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 );
    571         update_blog_status( $blog_id, 'mature', 1 );
    572         $blog = get_site( $blog_id );
    573 
    574         $this->assertEquals( '1', $blog->mature );
    575         $this->assertEquals( 1, $test_action_counter );
    576 
    577         // The action should fire if the status of 'mature' stays the same.
    578         update_blog_status( $blog_id, 'mature', 1 );
    579         $blog = get_site( $blog_id );
    580 
    581         $this->assertEquals( '1', $blog->mature );
    582         $this->assertEquals( 2, $test_action_counter );
    583 
    584         remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 );
    585     }
    586 
    587     function test_update_blog_status_unmature_blog_action() {
    588         global $test_action_counter;
    589         $test_action_counter = 0;
    590 
    591         $blog_id = self::factory()->blog->create();
    592         update_blog_details( $blog_id, array( 'mature' => 1 ) );
    593 
    594         add_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 );
    595         update_blog_status( $blog_id, 'mature', 0 );
    596 
    597         $blog = get_site( $blog_id );
    598         $this->assertEquals( '0', $blog->mature );
    599         $this->assertEquals( 1, $test_action_counter );
    600 
    601         // The action should fire if the status of 'mature' stays the same.
    602         update_blog_status( $blog_id, 'mature', 0 );
    603         $blog = get_site( $blog_id );
    604 
    605         $this->assertEquals( '0', $blog->mature );
    606         $this->assertEquals( 2, $test_action_counter );
    607 
    608         remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 );
    609     }
    610 
    611     function test_update_blog_status_update_blog_public_action() {
    612         global $test_action_counter;
    613         $test_action_counter = 0;
    614 
    615         $blog_id = self::factory()->blog->create();
    616 
    617         add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 );
    618         update_blog_status( $blog_id, 'public', 0 );
    619 
    620         $blog = get_site( $blog_id );
    621         $this->assertEquals( '0', $blog->public );
    622         $this->assertEquals( 1, $test_action_counter );
    623 
    624         // The action should fire if the status of 'mature' stays the same.
    625         update_blog_status( $blog_id, 'public', 0 );
    626         $blog = get_site( $blog_id );
    627 
    628         $this->assertEquals( '0', $blog->public );
    629         $this->assertEquals( 2, $test_action_counter );
    630 
    631         remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 );
    632     }
    633 
    634     /**
    635      * @ticket 27952
    636      */
    637     function test_posts_count() {
    638         self::factory()->post->create();
    639         $post2 = self::factory()->post->create();
    640         $this->assertEquals( 2, get_site()->post_count );
    641 
    642         wp_delete_post( $post2 );
    643         $this->assertEquals( 1, get_site()->post_count );
    644     }
    645 
    646     /**
    647      * @ticket 26410
    648      */
    649     function test_blog_details_cache_invalidation() {
    650         update_option( 'blogname', 'foo' );
    651         $details = get_site( get_current_blog_id() );
    652         $this->assertEquals( 'foo', $details->blogname );
    653 
    654         update_option( 'blogname', 'bar' );
    655         $details = get_site( get_current_blog_id() );
    656         $this->assertEquals( 'bar', $details->blogname );
    657     }
    658 
    659     /**
    660      * Test the original and cached responses for a created and then deleted site when
    661      * the blog ID is requested through get_blog_id_from_url().
    662      */
    663     function test_get_blog_id_from_url() {
    664         $blog_id = self::factory()->blog->create();
    665         $details = get_site( $blog_id );
    666         $key = md5( $details->domain . $details->path );
    667 
    668         // Test the original response and cached response for the newly created site.
    669         $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
    670         $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
    671     }
    672 
    673     /**
    674      * Test the case insensitivity of the site lookup.
    675      */
    676     function test_get_blog_id_from_url_is_case_insensitive() {
    677         $blog_id = self::factory()->blog->create( array( 'domain' => 'example.com', 'path' => '/xyz' ) );
    678         $details = get_site( $blog_id );
    679 
    680         $this->assertEquals( $blog_id, get_blog_id_from_url( strtoupper( $details->domain ), strtoupper( $details->path ) ) );
    681     }
    682 
    683     /**
    684      * Test the first and cached responses for a site that does not exist.
    685      */
    686     function test_get_blog_id_from_url_that_does_not_exist() {
    687         $blog_id = self::factory()->blog->create( array( 'path' => '/xyz' ) );
    688         $details = get_site( $blog_id );
    689 
    690         $this->assertEquals( 0, get_blog_id_from_url( $details->domain, 'foo' ) );
    691         $this->assertEquals( -1, wp_cache_get( md5( $details->domain . 'foo' ), 'blog-id-cache' ) );
    692     }
    693 
    694     /**
    695      * A blog ID is still available if only the `deleted` flag is set for a site. The same
    696      * behavior would be expected if passing `false` explicitly to `wpmu_delete_blog()`.
    697      */
    698     function test_get_blog_id_from_url_with_deleted_flag() {
    699         $blog_id = self::factory()->blog->create();
    700         $details = get_site( $blog_id );
    701         $key = md5( $details->domain . $details->path );
    702         wpmu_delete_blog( $blog_id );
    703 
    704         $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
    705         $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
    706     }
    707 
    708     /**
    709      * When deleted with the drop parameter as true, the cache will first be false, then set to
    710      * -1 after an attempt at `get_blog_id_from_url()` is made.
    711      */
    712     function test_get_blog_id_from_url_after_dropped() {
    713         $blog_id = self::factory()->blog->create();
    714         $details = get_site( $blog_id );
    715         $key = md5( $details->domain . $details->path );
    716         wpmu_delete_blog( $blog_id, true );
    717 
    718         $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    719         $this->assertEquals( 0, get_blog_id_from_url( $details->domain, $details->path ) );
    720         $this->assertEquals( -1, wp_cache_get( $key, 'blog-id-cache' ) );
    721     }
    722 
    723     /**
    724      * Test with default parameter of site_id as null.
    725      */
    726     function test_is_main_site() {
    727         $this->assertTrue( is_main_site() );
    728     }
    729 
    730     /**
    731      * Test with a site id of get_current_blog_id(), which should be the same as the
    732      * default parameter tested above.
    733      */
    734     function test_current_blog_id_is_main_site() {
    735         $this->assertTrue( is_main_site( get_current_blog_id() ) );
    736     }
    737 
    738     /**
    739      * Test with a site ID other than the main site to ensure a false response.
    740      */
    741     function test_is_main_site_is_false_with_other_blog_id() {
    742         $blog_id = self::factory()->blog->create();
    743 
    744         $this->assertFalse( is_main_site( $blog_id ) );
    745     }
    746 
    747     /**
    748      * Test with no passed ID after switching to another site ID.
    749      */
    750     function test_is_main_site_is_false_after_switch_to_blog() {
    751         $blog_id = self::factory()->blog->create();
    752         switch_to_blog( $blog_id );
    753 
    754         $this->assertFalse( is_main_site() );
    755 
    756         restore_current_blog();
    757     }
    758 
    759     function test_switch_upload_dir() {
    760         $this->assertTrue( is_main_site() );
    761 
    762         $site = get_current_site();
    763 
    764         $info = wp_upload_dir();
    765         $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
    766         $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
    767         $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
    768         $this->assertEquals( '', $info['error'] );
    769 
    770         $blog_id = self::factory()->blog->create();
    771 
    772         switch_to_blog( $blog_id );
    773         $info = wp_upload_dir();
    774         $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime('%Y/%m'), $info['url'] );
    775         $this->assertEquals( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime('%Y/%m'), $info['path'] );
    776         $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
    777         $this->assertEquals( '', $info['error'] );
    778         restore_current_blog();
    779 
    780         $info = wp_upload_dir();
    781         $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
    782         $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
    783         $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
    784         $this->assertEquals( '', $info['error'] );
    785     }
    786 
    787     /**
    788      * Test the primary purpose of get_blog_post(), to retrieve a post from
    789      * another site on the network.
    790      */
    791     function test_get_blog_post_from_another_site_on_network() {
    792         $blog_id = self::factory()->blog->create();
    793         $post_id = self::factory()->post->create(); // Create a post on the primary site, ID 1.
    794         $post = get_post( $post_id );
    795         switch_to_blog( $blog_id );
    796 
    797         // The post created and retrieved on the main site should match the one retrieved "remotely".
    798         $this->assertEquals( $post, get_blog_post( 1, $post_id ) );
    799 
    800         restore_current_blog();
    801     }
    802 
    803     /**
    804      * If get_blog_post() is used on the same site, it should still work.
    805      */
    806     function test_get_blog_post_from_same_site() {
    807         $post_id = self::factory()->post->create();
    808 
    809         $this->assertEquals( get_blog_post( 1, $post_id ), get_post( $post_id ) );
    810     }
    811 
    812     /**
    813      * A null response should be returned if an invalid post is requested.
    814      */
    815     function test_get_blog_post_invalid_returns_null() {
    816         $this->assertNull( get_blog_post( 1, 999999 ) );
    817     }
    818 
    819     /**
    820      * Added as a callback to the domain_exists filter to provide manual results for
    821      * the testing of the filter and for a test which does not need the database.
    822      */
    823     function _domain_exists_cb( $exists, $domain, $path, $site_id ) {
    824         if ( 'foo' == $domain && 'bar/' == $path )
    825             return 1234;
    826         else
    827             return null;
    828     }
    829 
    830     function test_domain_exists_with_default_site_id() {
    831         $details = get_site( 1 );
    832 
    833         $this->assertEquals( 1, domain_exists( $details->domain, $details->path ) );
    834     }
    835 
    836     function test_domain_exists_with_specified_site_id() {
    837         $details = get_site( 1 );
    838 
    839         $this->assertEquals( 1, domain_exists( $details->domain, $details->path, $details->site_id ) );
    840     }
    841 
    842     /**
    843      * When the domain is valid, but the resulting site does not belong to the specified network,
    844      * it is marked as not existing.
    845      */
    846     function test_domain_does_not_exist_with_invalid_site_id() {
    847         $details = get_site( 1 );
    848 
    849         $this->assertEquals( null, domain_exists( $details->domain, $details->path, 999 ) );
    850     }
    851 
    852     function test_invalid_domain_does_not_exist_with_default_site_id() {
    853         $this->assertEquals( null, domain_exists( 'foo', 'bar' ) );
    854     }
    855 
    856     function test_domain_filtered_to_exist() {
    857         add_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
    858         $exists = domain_exists( 'foo', 'bar' );
    859         remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
    860         $this->assertEquals( 1234, $exists );
    861     }
    862 
    863     /**
    864      * When a path is passed to domain_exists, it is immediately trailing slashed. A path
    865      * value with or without the slash should result in the same return value.
    866      */
    867     function test_slashed_path_in_domain_exists() {
    868         add_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
    869         $exists1 = domain_exists( 'foo', 'bar' );
    870         $exists2 = domain_exists( 'foo', 'bar/' );
    871         remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
    872 
    873         // Make sure the same result is returned with or without a trailing slash
    874         $this->assertEquals( $exists1, $exists2 );
    875     }
    876 
    877     /**
    878      * Tests returning an address for a given valid id.
    879      */
    880     function test_get_blogaddress_by_id_with_valid_id() {
    881         $blogaddress = get_blogaddress_by_id( 1 );
    882         $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/', $blogaddress );
    883     }
    884 
    885     /**
    886      * Tests returning the appropriate response for a invalid id given.
    887      */
    888     function test_get_blogaddress_by_id_with_invalid_id() {
    889         $blogaddress = get_blogaddress_by_id( 42 );
    890         $this->assertEquals( '', $blogaddress );
    891     }
    892 
    893     /**
    894      * @ticket 14867
    895      */
    896     function test_get_blogaddress_by_id_scheme_reflects_blog_scheme() {
    897         $blog = self::factory()->blog->create();
    898 
    899         $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
    900 
    901         update_blog_option( $blog, 'home', set_url_scheme( get_blog_option( $blog, 'home' ), 'https' ) );
    902 
    903         $this->assertSame( 'https', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
    904     }
    905 
    906     /**
    907      * @ticket 14867
    908      */
    909     function test_get_blogaddress_by_id_scheme_is_unaffected_by_request() {
    910         $blog = self::factory()->blog->create();
    911 
    912         $this->assertFalse( is_ssl() );
    913         $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
    914 
    915         $_SERVER['HTTPS'] = 'on';
    916 
    917         $is_ssl  = is_ssl();
    918         $address = parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME );
    919 
    920         $this->assertTrue( $is_ssl );
    921         $this->assertSame( 'http', $address );
    922     }
    923 
    924     /**
    925      * @ticket 33620
    926      * @dataProvider data_new_blog_url_schemes
    927      */
    928     function test_new_blog_url_schemes( $home_scheme, $siteurl_scheme, $force_ssl_admin ) {
    929         $current_site = get_current_site();
    930 
    931         $home    = get_option( 'home' );
    932         $siteurl = get_site_option( 'siteurl' );
    933         $admin   = force_ssl_admin();
    934 
    935         // Setup:
    936         update_option( 'home', set_url_scheme( $home, $home_scheme ) );
    937         update_site_option( 'siteurl', set_url_scheme( $siteurl, $siteurl_scheme ) );
    938         force_ssl_admin( $force_ssl_admin );
    939 
    940         // Install:
    941         $new = wpmu_create_blog( $current_site->domain, '/new-blog/', 'New Blog', get_current_user_id() );
    942 
    943         // Reset:
    944         update_option( 'home', $home );
    945         update_site_option( 'siteurl', $siteurl );
    946         force_ssl_admin( $admin );
    947 
    948         // Assert:
    949         $this->assertNotWPError( $new );
    950         $this->assertSame( $home_scheme, parse_url( get_blog_option( $new, 'home' ), PHP_URL_SCHEME ) );
    951         $this->assertSame( $siteurl_scheme, parse_url( get_blog_option( $new, 'siteurl' ), PHP_URL_SCHEME ) );
    952     }
    953 
    954     function data_new_blog_url_schemes() {
    955         return array(
    956             array(
    957                 'https',
    958                 'https',
    959                 false,
    960             ),
    961             array(
    962                 'http',
    963                 'https',
    964                 false,
    965             ),
    966             array(
    967                 'https',
    968                 'http',
    969                 false,
    970             ),
    971             array(
    972                 'http',
    973                 'http',
    974                 false,
    975             ),
    976             array(
    977                 'http',
    978                 'http',
    979                 true,
    980             ),
    981         );
    982     }
    983 
    984     /**
    985      * @ticket 36918
    986      */
    987     function test_new_blog_locale() {
    988         $current_site = get_current_site();
    989 
    990         add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    991         update_site_option( 'WPLANG', 'de_DE' );
    992         remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    993 
    994         // No locale, use default locale.
    995         add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    996         $blog_id = wpmu_create_blog( $current_site->domain, '/de-de/', 'New Blog', get_current_user_id() );
    997         remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    998 
    999         $this->assertNotWPError( $blog_id );
    1000         $this->assertSame( 'de_DE', get_blog_option( $blog_id, 'WPLANG' ) );
    1001 
    1002         // Custom locale.
    1003         add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    1004         $blog_id = wpmu_create_blog( $current_site->domain, '/es-es/', 'New Blog', get_current_user_id(), array( 'WPLANG' => 'es_ES' ) );
    1005         remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    1006 
    1007         $this->assertNotWPError( $blog_id );
    1008         $this->assertSame( 'es_ES', get_blog_option( $blog_id, 'WPLANG' ) );
    1009 
    1010         // en_US locale.
    1011         add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    1012         $blog_id = wpmu_create_blog( $current_site->domain, '/en-us/', 'New Blog', get_current_user_id(), array( 'WPLANG' => '' ) );
    1013         remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    1014 
    1015         $this->assertNotWPError( $blog_id );
    1016         $this->assertSame( '', get_blog_option( $blog_id, 'WPLANG' ) );
    1017     }
    1018 
    1019     /**
    1020      * @ticket 40503
    1021      */
    1022     function test_different_network_language() {
    1023         $network = get_network( self::$network_ids['make.wordpress.org/'] );
    1024 
    1025         add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    1026 
    1027         update_network_option( self::$network_ids['make.wordpress.org/'], 'WPLANG', 'wibble' );
    1028         $blog_id = wpmu_create_blog( $network->domain, '/de-de/', 'New Blog', get_current_user_id(), array(), $network->id );
    1029 
    1030         remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    1031 
    1032         $this->assertSame( get_network_option( self::$network_ids['make.wordpress.org/'], 'WPLANG' ), get_blog_option( $blog_id, 'WPLANG' ) );
    1033     }
    1034 
    1035     /**
    1036      * Allows to set the WPLANG option to any language.
    1037      *
    1038      * @param string $value          The sanitized option value.
    1039      * @param string $option         The option name.
    1040      * @param string $original_value The original value passed to the function.
    1041      * @return string The orginal value.
    1042      */
    1043     function filter_allow_unavailable_languages( $value, $option, $original_value ) {
    1044         return $original_value;
    1045     }
    1046 
    1047     /**
    1048      * @ticket 29684
    1049      */
    1050     public function test_is_main_site_different_network() {
    1051         $this->assertTrue( is_main_site( self::$site_ids['make.wordpress.org/'], self::$network_ids['make.wordpress.org/'] ) );
    1052     }
    1053 
    1054     /**
    1055      * @ticket 29684
    1056      */
    1057     public function test_is_main_site_different_network_random_site() {
    1058         $this->assertFalse( is_main_site( self::$site_ids['make.wordpress.org/foo/'], self::$network_ids['make.wordpress.org/'] ) );
    1059     }
    1060 
    1061     /**
    1062      * @ticket 40201
    1063      * @dataProvider data_get_site_caches
    1064      */
    1065     public function test_clean_blog_cache( $key, $group ) {
    1066         $site = get_site( self::$site_ids['make.wordpress.org/'] );
    1067 
    1068         $replacements = array(
    1069             '%blog_id%'         => $site->blog_id,
    1070             '%domain%'          => $site->domain,
    1071             '%path%'            => $site->path,
    1072             '%domain_path_key%' => md5( $site->domain . $site->path ),
    1073         );
    1074 
    1075         $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
    1076 
    1077         if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
    1078             wp_cache_set( $key, (object) $site->to_array(), $group );
    1079         } else {
    1080             wp_cache_set( $key, 'something', $group );
    1081         }
    1082 
    1083         clean_blog_cache( $site );
    1084         $this->assertFalse( wp_cache_get( $key, $group ) );
    1085     }
    1086 
    1087     /**
    1088      * @ticket 40201
    1089      * @dataProvider data_get_site_caches
    1090      */
    1091     public function test_clean_blog_cache_with_id( $key, $group ) {
    1092         $site = get_site( self::$site_ids['make.wordpress.org/'] );
    1093 
    1094         $replacements = array(
    1095             '%blog_id%'         => $site->blog_id,
    1096             '%domain%'          => $site->domain,
    1097             '%path%'            => $site->path,
    1098             '%domain_path_key%' => md5( $site->domain . $site->path ),
    1099         );
    1100 
    1101         $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
    1102 
    1103         if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
    1104             wp_cache_set( $key, (object) $site->to_array(), $group );
    1105         } else {
    1106             wp_cache_set( $key, 'something', $group );
    1107         }
    1108 
    1109         clean_blog_cache( $site->blog_id );
    1110         $this->assertFalse( wp_cache_get( $key, $group ) );
    1111     }
    1112 
    1113     /**
    1114      * @ticket 40201
    1115      */
    1116     public function test_clean_blog_cache_resets_last_changed() {
    1117         $site = get_site( self::$site_ids['make.wordpress.org/'] );
    1118 
    1119         wp_cache_delete( 'last_changed', 'sites' );
    1120 
    1121         clean_blog_cache( $site );
    1122         $this->assertNotFalse( wp_cache_get( 'last_changed', 'sites' ) );
    1123     }
    1124 
    1125     /**
    1126      * @ticket 40201
    1127      */
    1128     public function test_clean_blog_cache_fires_action() {
    1129         $site = get_site( self::$site_ids['make.wordpress.org/'] );
    1130 
    1131         $old_count = did_action( 'clean_site_cache' );
    1132 
    1133         clean_blog_cache( $site );
    1134         $this->assertEquals( $old_count + 1, did_action( 'clean_site_cache' ) );
    1135     }
    1136 
    1137     /**
    1138      * @ticket 40201
    1139      */
    1140     public function test_clean_blog_cache_bails_on_suspend_cache_invalidation() {
    1141         $site = get_site( self::$site_ids['make.wordpress.org/'] );
    1142 
    1143         $old_count = did_action( 'clean_site_cache' );
    1144 
    1145         $suspend = wp_suspend_cache_invalidation();
    1146         clean_blog_cache( $site );
    1147         wp_suspend_cache_invalidation( $suspend );
    1148         $this->assertEquals( $old_count, did_action( 'clean_site_cache' ) );
    1149     }
    1150 
    1151     /**
    1152      * @ticket 40201
    1153      */
    1154     public function test_clean_blog_cache_bails_on_empty_input() {
    1155         $old_count = did_action( 'clean_site_cache' );
    1156 
    1157         clean_blog_cache( null );
    1158         $this->assertEquals( $old_count, did_action( 'clean_site_cache' ) );
    1159     }
    1160 
    1161     /**
    1162      * @ticket 40201
    1163      */
    1164     public function test_clean_blog_cache_bails_on_non_numeric_input() {
    1165         $old_count = did_action( 'clean_site_cache' );
    1166 
    1167         clean_blog_cache( 'something' );
    1168         $this->assertEquals( $old_count, did_action( 'clean_site_cache' ) );
    1169     }
    1170 
    1171     /**
    1172      * @ticket 40201
    1173      */
    1174     public function test_clean_blog_cache_works_with_deleted_site() {
    1175         $site_id = 12345;
    1176 
    1177         wp_cache_set( $site_id, 'something', 'site-details' );
    1178 
    1179         clean_blog_cache( $site_id );
    1180         $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
    1181     }
    1182 
    1183     /**
    1184      * @ticket 40201
    1185      * @dataProvider data_get_site_caches
    1186      */
    1187     public function test_refresh_blog_details( $key, $group ) {
    1188         $site = get_site( self::$site_ids['make.wordpress.org/'] );
    1189 
    1190         $replacements = array(
    1191             '%blog_id%'         => $site->blog_id,
    1192             '%domain%'          => $site->domain,
    1193             '%path%'            => $site->path,
    1194             '%domain_path_key%' => md5( $site->domain . $site->path ),
    1195         );
    1196 
    1197         $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
    1198 
    1199         if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
    1200             wp_cache_set( $key, (object) $site->to_array(), $group );
    1201         } else {
    1202             wp_cache_set( $key, 'something', $group );
    1203         }
    1204 
    1205         refresh_blog_details( $site->blog_id );
    1206         $this->assertFalse( wp_cache_get( $key, $group ) );
    1207     }
    1208 
    1209     /**
    1210      * @ticket 40201
    1211      */
    1212     public function test_refresh_blog_details_works_with_deleted_site() {
    1213         $site_id = 12345;
    1214 
    1215         wp_cache_set( $site_id, 'something', 'site-details' );
    1216 
    1217         refresh_blog_details( $site_id );
    1218         $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
    1219     }
    1220 
    1221     /**
    1222      * @ticket 40201
    1223      */
    1224     public function test_refresh_blog_details_uses_current_site_as_default() {
    1225         $site_id = get_current_blog_id();
    1226 
    1227         wp_cache_set( $site_id, 'something', 'site-details' );
    1228 
    1229         refresh_blog_details();
    1230         $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
    1231     }
    1232 
    1233     public function data_get_site_caches() {
    1234         return array(
    1235             array( '%blog_id%', 'sites' ),
    1236             array( '%blog_id%', 'site-details' ),
    1237             array( '%blog_id%', 'blog-details' ),
    1238             array( '%blog_id%' . 'short' , 'blog-details' ),
    1239             array( '%domain_path_key%', 'blog-lookup' ),
    1240             array( '%domain_path_key%', 'blog-id-cache' ),
    1241             array( 'current_blog_%domain%', 'site-options' ),
    1242             array( 'current_blog_%domain%%path%', 'site-options' ),
    1243         );
    1244     }
    1245 }
    1246 
    12471264endif;
Note: See TracChangeset for help on using the changeset viewer.