Make WordPress Core

Changeset 60148


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

Tests: Use the ms-required group where appropriate.

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

Follow-up to [40520].

See #63167.

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

Legend:

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

    r57987 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Tests specific to `avoid_blog_page_permalink_collision()` in multisite.
     5 *
     6 * @group ms-required
     7 * @group multisite
     8 * @group post
     9 */
     10class Tests_Multisite_AvoidBlogPagePermalinkCollision extends WP_UnitTestCase {
     11
     12    protected static $site_id;
     13    protected static $root_page;
     14    protected static $child_page;
     15    protected static $post_and_blog_path = 'permalink-collision';
    416
    517    /**
    6      * Tests specific to `avoid_blog_page_permalink_collision()` in multisite.
    7      *
    8      * @group multisite
    9      * @group post
     18     * Create a blog and the pages we need to test the collision.
    1019     */
    11     class Tests_Multisite_AvoidBlogPagePermalinkCollision extends WP_UnitTestCase {
    12         protected static $site_id;
    13         protected static $root_page;
    14         protected static $child_page;
    15         protected static $post_and_blog_path = 'permalink-collision';
     20    public static function wpSetUpBeforeClass( $factory ) {
     21        self::$site_id = self::factory()->blog->create(
     22            array(
     23                'path' => '/' . self::$post_and_blog_path,
     24            )
     25        );
    1626
    17         /**
    18          * Create a blog and the pages we need to test the collision.
    19          */
    20         public static function wpSetUpBeforeClass( $factory ) {
    21             self::$site_id = self::factory()->blog->create(
    22                 array(
    23                     'path' => '/' . self::$post_and_blog_path,
    24                 )
    25             );
     27        self::$root_page = self::factory()->post->create_and_get(
     28            array(
     29                'post_type'  => 'page',
     30                'post_title' => 'Bar',
     31                'post_name'  => self::$post_and_blog_path,
     32            )
     33        );
    2634
    27             self::$root_page = self::factory()->post->create_and_get(
    28                 array(
    29                     'post_type'  => 'page',
    30                     'post_title' => 'Bar',
    31                     'post_name'  => self::$post_and_blog_path,
    32                 )
    33             );
    34 
    35             self::$child_page = self::factory()->post->create_and_get(
    36                 array(
    37                     'post_parent' => self::$root_page->ID,
    38                     'post_type'   => 'page',
    39                     'post_title'  => 'Bar',
    40                     'post_name'   => self::$post_and_blog_path,
    41                 )
    42             );
    43         }
    44 
    45         /**
    46          * Delete blog and pages we created.
    47          */
    48         public static function wpTearDownAfterClass() {
    49             wp_delete_site( self::$site_id );
    50 
    51             wp_delete_post( self::$root_page->ID );
    52             wp_delete_post( self::$child_page->ID );
    53         }
    54 
    55         public function test_avoid_blog_page_permalink_collision_renames_post_name() {
    56             $this->assertNotSame( self::$post_and_blog_path, self::$root_page->post_name );
    57         }
    58 
    59         /**
    60          * Ensure `avoid_blog_page_permalink_collision()` doesn't rename child pages' post_name.
    61          *
    62          * @ticket 51147
    63          */
    64         public function test_avoid_blog_page_permalink_collision_doesnt_rename_child_pages() {
    65             $this->assertSame( self::$post_and_blog_path, self::$child_page->post_name );
    66         }
     35        self::$child_page = self::factory()->post->create_and_get(
     36            array(
     37                'post_parent' => self::$root_page->ID,
     38                'post_type'   => 'page',
     39                'post_title'  => 'Bar',
     40                'post_name'   => self::$post_and_blog_path,
     41            )
     42        );
    6743    }
    6844
    69 endif;
     45    /**
     46     * Delete blog and pages we created.
     47     */
     48    public static function wpTearDownAfterClass() {
     49        wp_delete_site( self::$site_id );
     50
     51        wp_delete_post( self::$root_page->ID );
     52        wp_delete_post( self::$child_page->ID );
     53    }
     54
     55    public function test_avoid_blog_page_permalink_collision_renames_post_name() {
     56        $this->assertNotSame( self::$post_and_blog_path, self::$root_page->post_name );
     57    }
     58
     59    /**
     60     * Ensure `avoid_blog_page_permalink_collision()` doesn't rename child pages' post_name.
     61     *
     62     * @ticket 51147
     63     */
     64    public function test_avoid_blog_page_permalink_collision_doesnt_rename_child_pages() {
     65        $this->assertSame( self::$post_and_blog_path, self::$child_page->post_name );
     66    }
     67}
  • trunk/tests/phpunit/tests/multisite/bootstrap.php

    r52010 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    5     /**
    6      * Tests specific to the bootstrap process of Multisite.
     3/**
     4 * Tests specific to the bootstrap process of Multisite.
     5 *
     6 * @group ms-bootstrap
     7 * @group ms-required
     8 * @group multisite
     9 */
     10class Tests_Multisite_Bootstrap extends WP_UnitTestCase {
     11
     12    protected static $network_ids;
     13    protected static $site_ids;
     14
     15    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     16        self::$network_ids = array(
     17            'wordpress.org/'         => array(
     18                'domain' => 'wordpress.org',
     19                'path'   => '/',
     20            ),
     21            'make.wordpress.org/'    => array(
     22                'domain' => 'make.wordpress.org',
     23                'path'   => '/',
     24            ),
     25            'wordpress.org/one/'     => array(
     26                'domain' => 'wordpress.org',
     27                'path'   => '/one/',
     28            ),
     29            'wordpress.org/one/b/'   => array(
     30                'domain' => 'wordpress.org',
     31                'path'   => '/one/b/',
     32            ),
     33            'wordpress.net/'         => array(
     34                'domain' => 'wordpress.net',
     35                'path'   => '/',
     36            ),
     37            'www.wordpress.net/'     => array(
     38                'domain' => 'www.wordpress.net',
     39                'path'   => '/',
     40            ),
     41            'www.wordpress.net/two/' => array(
     42                'domain' => 'www.wordpress.net',
     43                'path'   => '/two/',
     44            ),
     45            'wordpress.net/three/'   => array(
     46                'domain' => 'wordpress.net',
     47                'path'   => '/three/',
     48            ),
     49        );
     50
     51        foreach ( self::$network_ids as &$id ) {
     52            $id = $factory->network->create( $id );
     53        }
     54        unset( $id );
     55
     56        self::$site_ids = array(
     57            'wordpress.org/'          => array(
     58                'domain'     => 'wordpress.org',
     59                'path'       => '/',
     60                'network_id' => self::$network_ids['wordpress.org/'],
     61            ),
     62            'wordpress.org/foo/'      => array(
     63                'domain'     => 'wordpress.org',
     64                'path'       => '/foo/',
     65                'network_id' => self::$network_ids['wordpress.org/'],
     66            ),
     67            'wordpress.org/foo/bar/'  => array(
     68                'domain'     => 'wordpress.org',
     69                'path'       => '/foo/bar/',
     70                'network_id' => self::$network_ids['wordpress.org/'],
     71            ),
     72            'make.wordpress.org/'     => array(
     73                'domain'     => 'make.wordpress.org',
     74                'path'       => '/',
     75                'network_id' => self::$network_ids['make.wordpress.org/'],
     76            ),
     77            'make.wordpress.org/foo/' => array(
     78                'domain'     => 'make.wordpress.org',
     79                'path'       => '/foo/',
     80                'network_id' => self::$network_ids['make.wordpress.org/'],
     81            ),
     82            'www.w.org/'              => array(
     83                'domain' => 'www.w.org',
     84                'path'   => '/',
     85            ),
     86            'www.w.org/foo/'          => array(
     87                'domain' => 'www.w.org',
     88                'path'   => '/foo/',
     89            ),
     90            'www.w.org/foo/bar/'      => array(
     91                'domain' => 'www.w.org',
     92                'path'   => '/foo/bar/',
     93            ),
     94        );
     95
     96        foreach ( self::$site_ids as &$id ) {
     97            $id = $factory->blog->create( $id );
     98        }
     99        unset( $id );
     100    }
     101
     102    public static function wpTearDownAfterClass() {
     103        global $wpdb;
     104
     105        foreach ( self::$site_ids as $id ) {
     106            wp_delete_site( $id );
     107        }
     108
     109        foreach ( self::$network_ids as $id ) {
     110            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     111            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     112        }
     113
     114        wp_update_network_site_counts();
     115    }
     116
     117    /**
     118     * @ticket 27003
     119     * @dataProvider data_get_network_by_path
    7120     *
    8      * @group ms-bootstrap
    9      * @group multisite
    10      */
    11     class Tests_Multisite_Bootstrap extends WP_UnitTestCase {
    12         protected static $network_ids;
    13         protected static $site_ids;
    14 
    15         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    16             self::$network_ids = array(
    17                 'wordpress.org/'         => array(
    18                     'domain' => 'wordpress.org',
    19                     'path'   => '/',
    20                 ),
    21                 'make.wordpress.org/'    => array(
    22                     'domain' => 'make.wordpress.org',
    23                     'path'   => '/',
    24                 ),
    25                 'wordpress.org/one/'     => array(
    26                     'domain' => 'wordpress.org',
    27                     'path'   => '/one/',
    28                 ),
    29                 'wordpress.org/one/b/'   => array(
    30                     'domain' => 'wordpress.org',
    31                     'path'   => '/one/b/',
    32                 ),
    33                 'wordpress.net/'         => array(
    34                     'domain' => 'wordpress.net',
    35                     'path'   => '/',
    36                 ),
    37                 'www.wordpress.net/'     => array(
    38                     'domain' => 'www.wordpress.net',
    39                     'path'   => '/',
    40                 ),
    41                 'www.wordpress.net/two/' => array(
    42                     'domain' => 'www.wordpress.net',
    43                     'path'   => '/two/',
    44                 ),
    45                 'wordpress.net/three/'   => array(
    46                     'domain' => 'wordpress.net',
    47                     'path'   => '/three/',
    48                 ),
    49             );
    50 
    51             foreach ( self::$network_ids as &$id ) {
    52                 $id = $factory->network->create( $id );
    53             }
    54             unset( $id );
    55 
    56             self::$site_ids = array(
    57                 'wordpress.org/'          => array(
    58                     'domain'     => 'wordpress.org',
    59                     'path'       => '/',
    60                     'network_id' => self::$network_ids['wordpress.org/'],
    61                 ),
    62                 'wordpress.org/foo/'      => array(
    63                     'domain'     => 'wordpress.org',
    64                     'path'       => '/foo/',
    65                     'network_id' => self::$network_ids['wordpress.org/'],
    66                 ),
    67                 'wordpress.org/foo/bar/'  => array(
    68                     'domain'     => 'wordpress.org',
    69                     'path'       => '/foo/bar/',
    70                     'network_id' => self::$network_ids['wordpress.org/'],
    71                 ),
    72                 'make.wordpress.org/'     => array(
    73                     'domain'     => 'make.wordpress.org',
    74                     'path'       => '/',
    75                     'network_id' => self::$network_ids['make.wordpress.org/'],
    76                 ),
    77                 'make.wordpress.org/foo/' => array(
    78                     'domain'     => 'make.wordpress.org',
    79                     'path'       => '/foo/',
    80                     'network_id' => self::$network_ids['make.wordpress.org/'],
    81                 ),
    82                 'www.w.org/'              => array(
    83                     'domain' => 'www.w.org',
    84                     'path'   => '/',
    85                 ),
    86                 'www.w.org/foo/'          => array(
    87                     'domain' => 'www.w.org',
    88                     'path'   => '/foo/',
    89                 ),
    90                 'www.w.org/foo/bar/'      => array(
    91                     'domain' => 'www.w.org',
    92                     'path'   => '/foo/bar/',
    93                 ),
    94             );
    95 
    96             foreach ( self::$site_ids as &$id ) {
    97                 $id = $factory->blog->create( $id );
    98             }
    99             unset( $id );
    100         }
    101 
    102         public static function wpTearDownAfterClass() {
    103             global $wpdb;
    104 
    105             foreach ( self::$site_ids as $id ) {
    106                 wp_delete_site( $id );
    107             }
    108 
    109             foreach ( self::$network_ids as $id ) {
    110                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    111                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    112             }
    113 
    114             wp_update_network_site_counts();
    115         }
    116 
    117         /**
    118          * @ticket 27003
    119          * @dataProvider data_get_network_by_path
    120          *
    121          * @param string $expected_key The array key associated with expected data for the test.
    122          * @param string $domain       The requested domain.
    123          * @param string $path         The requested path.
    124          * @param string $message      The message to pass for failed tests.
    125          */
    126         public function test_get_network_by_path( $expected_key, $domain, $path, $message ) {
    127             $network = get_network_by_path( $domain, $path );
    128             $this->assertSame( self::$network_ids[ $expected_key ], $network->id, $message );
    129         }
    130 
    131         public function data_get_network_by_path() {
    132             return array(
    133                 array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ),
    134                 array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A missing path on a top level domain should find the correct network.' ),
    135                 array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A missing path should find the correct network.' ),
    136                 array( 'wordpress.org/one/', 'www.wordpress.org', '/one/', 'Should find the path despite the www.' ),
    137                 array( 'wordpress.org/one/', 'wordpress.org', '/one/page/', 'A request with two path segments should find the correct network.' ),
    138                 array( 'wordpress.org/one/b/', 'wordpress.org', '/one/b/', 'A request with two valid path segments should find the correct network.' ),
    139                 array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should not find path because domains do not match.' ),
    140                 array( 'wordpress.net/three/', 'wordpress.net', '/three/', 'A network can have a path.' ),
    141                 array( 'www.wordpress.net/two/', 'www.wordpress.net', '/two/', 'A www network with a path can coexist with a non-www network.' ),
    142                 array( 'wordpress.net/', 'site1.wordpress.net', '/notapath/', 'An invalid subdomain should find the top level network domain.' ),
    143                 array( 'wordpress.net/', 'site1.wordpress.net', '/three/', 'An invalid subdomain and path should find the top level network domain.' ),
    144                 array( 'wordpress.net/', 'x.y.wordpress.net', '/', 'An invalid two level subdomain should find the top level network domain.' ),
    145             );
    146         }
    147 
    148         /**
    149          * @ticket 37217
    150          * @dataProvider data_get_network_by_path_with_zero_path_segments
    151          *
    152          * @param string $expected_key The array key associated with expected data for the test.
    153          * @param string $domain       The requested domain.
    154          * @param string $path         The requested path.
    155          * @param string $message      The message to pass for failed tests.
    156          */
    157         public function test_get_network_by_path_with_zero_path_segments( $expected_key, $domain, $path, $message ) {
    158             add_filter( 'network_by_path_segments_count', '__return_zero' );
    159 
    160             $network = get_network_by_path( $domain, $path );
    161 
    162             remove_filter( 'network_by_path_segments_count', '__return_zero' );
    163 
    164             $this->assertSame( self::$network_ids[ $expected_key ], $network->id, $message );
    165         }
    166 
    167         public function data_get_network_by_path_with_zero_path_segments() {
    168             return array(
    169                 array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ),
    170                 array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A network matching a top level domain should be found regardless of path.' ),
    171                 array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A network matching a domain should be found regardless of path.' ),
    172                 array( 'wordpress.org/', 'www.wordpress.org', '/one/', 'Should find the network despite the www and regardless of path.' ),
    173                 array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should find the network with the corresponding top level domain regardless of path.' ),
    174                 array( 'www.wordpress.net/', 'www.wordpress.net', '/two/', 'A www network can coexist with a non-www network.' ),
    175                 array( 'make.wordpress.org/', 'make.wordpress.org', '/notapath/', 'A subdomain network should be found regardless of path.' ),
    176                 array( 'wordpress.net/', 'x.y.wordpress.net', '/', 'An invalid two level subdomain should find the top level network domain.' ),
    177             );
    178         }
    179 
    180         /**
    181          * Even if a matching network is available, it should not match if the the filtered
    182          * value for network path segments is fewer than the number of paths passed.
    183          */
    184         public function test_get_network_by_path_with_forced_single_path_segment_returns_single_path_network() {
    185             add_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
    186             $network = get_network_by_path( 'wordpress.org', '/one/b/' );
    187             remove_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
    188 
    189             $this->assertSame( self::$network_ids['wordpress.org/one/'], $network->id );
    190         }
    191 
    192         public function filter_network_path_segments() {
    193             return 1;
    194         }
    195 
    196         /**
    197          * @ticket 27003
    198          * @ticket 27927
    199          * @dataProvider data_get_site_by_path
    200          *
    201          * @param string $expected_key The array key associated with expected data for the test.
    202          * @param string $domain       The requested domain.
    203          * @param string $path         The requested path.
    204          * @param int    $segments     Optional. Number of segments to use in `get_site_by_path()`.
    205          */
    206         public function test_get_site_by_path( $expected_key, $domain, $path, $segments = null ) {
    207             $site = get_site_by_path( $domain, $path, $segments );
    208 
    209             if ( $expected_key ) {
    210                 $this->assertEquals( self::$site_ids[ $expected_key ], $site->blog_id );
    211             } else {
    212                 $this->assertFalse( $site );
    213             }
    214         }
    215 
    216         public function data_get_site_by_path() {
    217             return array(
    218                 array( 'wordpress.org/', 'wordpress.org', '/notapath/' ),
    219                 array( 'wordpress.org/', 'www.wordpress.org', '/notapath/' ),
    220                 array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/' ),
    221                 array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/' ),
    222                 array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 3 ),
    223                 array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 3 ),
    224                 array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 2 ),
    225                 array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 2 ),
    226                 array( 'wordpress.org/foo/', 'wordpress.org', '/foo/bar/baz/', 1 ),
    227                 array( 'wordpress.org/foo/', 'www.wordpress.org', '/foo/bar/baz/', 1 ),
    228                 array( 'wordpress.org/', 'wordpress.org', '/', 0 ),
    229                 array( 'wordpress.org/', 'www.wordpress.org', '/', 0 ),
    230                 array( 'make.wordpress.org/foo/', 'make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
    231                 array( 'make.wordpress.org/foo/', 'www.make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
    232                 array( 'www.w.org/', 'www.w.org', '/', 0 ),
    233                 array( 'www.w.org/', 'www.w.org', '/notapath' ),
    234                 array( 'www.w.org/foo/bar/', 'www.w.org', '/foo/bar/baz/' ),
    235                 array( 'www.w.org/foo/', 'www.w.org', '/foo/bar/baz/', 1 ),
    236 
    237                 // A site installed with www will not be found by the root domain.
    238                 array( false, 'w.org', '/' ),
    239                 array( false, 'w.org', '/notapath/' ),
    240                 array( false, 'w.org', '/foo/bar/baz/' ),
    241                 array( false, 'w.org', '/foo/bar/baz/', 1 ),
    242 
    243                 // A site will not be found by its root domain when an invalid subdomain is requested.
    244                 array( false, 'invalid.wordpress.org', '/' ),
    245                 array( false, 'invalid.wordpress.org', '/foo/bar/' ),
    246             );
    247         }
    248 
    249         /**
    250          * @ticket 27884
    251          * @dataProvider data_multisite_bootstrap
    252          *
    253          * @param string $site_key    The array key associated with the expected site for the test.
    254          * @param string $network_key The array key associated with the expected network for the test.
    255          * @param string $domain      The requested domain.
    256          * @param string $path        The requested path.
    257          */
    258         public function test_multisite_bootstrap( $site_key, $network_key, $domain, $path ) {
    259             global $current_blog;
    260 
    261             $expected = array(
    262                 'network_id' => self::$network_ids[ $network_key ],
    263                 'site_id'    => self::$site_ids[ $site_key ],
    264             );
    265 
    266             ms_load_current_site_and_network( $domain, $path );
    267             $actual = array(
    268                 'network_id' => $current_blog->site_id,
    269                 'site_id'    => $current_blog->blog_id,
    270             );
    271             ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
    272 
    273             $this->assertEqualSetsWithIndex( $expected, $actual );
    274         }
    275 
    276         public function data_multisite_bootstrap() {
    277             return array(
    278                 array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/' ),
    279                 array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/2014/04/23/hello-world/' ),
    280                 array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/sample-page/' ),
    281                 array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/?p=1' ),
    282                 array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/wp-admin/' ),
    283                 array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/' ),
    284                 array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/FOO/' ),
    285                 array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/2014/04/23/hello-world/' ),
    286                 array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/sample-page/' ),
    287                 array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/?p=1' ),
    288                 array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/wp-admin/' ),
    289                 array( 'make.wordpress.org/', 'make.wordpress.org/', 'make.wordpress.org', '/' ),
    290                 array( 'make.wordpress.org/foo/', 'make.wordpress.org/', 'make.wordpress.org', '/foo/' ),
    291             );
    292         }
    293 
    294         /**
    295          * @ticket 27884
    296          */
    297         public function test_multisite_bootstrap_additional_path_segments() {
    298             global $current_blog;
    299 
    300             $expected = array(
    301                 'network_id' => self::$network_ids['wordpress.org/'],
    302                 'site_id'    => self::$site_ids['wordpress.org/foo/bar/'],
    303             );
    304             add_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
    305             ms_load_current_site_and_network( 'wordpress.org', '/foo/bar/' );
    306             $actual = array(
    307                 'network_id' => $current_blog->site_id,
    308                 'site_id'    => $current_blog->blog_id,
    309             );
    310             remove_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
    311             ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
    312 
    313             $this->assertEqualSetsWithIndex( $expected, $actual );
    314         }
    315 
    316         /**
    317          * @ticket 37053
    318          */
    319         public function test_get_site_by_path_returns_wp_site() {
    320             add_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10, 3 );
    321 
    322             $site = get_site_by_path( 'example.com', '/foo/' );
    323 
    324             remove_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10 );
    325 
    326             $this->assertInstanceOf( 'WP_Site', $site );
    327         }
    328 
    329         public function filter_path_segments_to_two() {
    330             return 2;
    331         }
    332 
    333         public function filter_pre_get_site_by_path( $site, $domain, $path ) {
    334             $site          = new stdClass();
    335             $site->blog_id = 100;
    336             $site->domain  = $domain;
    337             $site->path    = $path;
    338             $site->site_id = 1;
    339 
    340             return $site;
    341         }
    342     }
    343 
    344 endif;
     121     * @param string $expected_key The array key associated with expected data for the test.
     122     * @param string $domain       The requested domain.
     123     * @param string $path         The requested path.
     124     * @param string $message      The message to pass for failed tests.
     125     */
     126    public function test_get_network_by_path( $expected_key, $domain, $path, $message ) {
     127        $network = get_network_by_path( $domain, $path );
     128        $this->assertSame( self::$network_ids[ $expected_key ], $network->id, $message );
     129    }
     130
     131    public function data_get_network_by_path() {
     132        return array(
     133            array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ),
     134            array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A missing path on a top level domain should find the correct network.' ),
     135            array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A missing path should find the correct network.' ),
     136            array( 'wordpress.org/one/', 'www.wordpress.org', '/one/', 'Should find the path despite the www.' ),
     137            array( 'wordpress.org/one/', 'wordpress.org', '/one/page/', 'A request with two path segments should find the correct network.' ),
     138            array( 'wordpress.org/one/b/', 'wordpress.org', '/one/b/', 'A request with two valid path segments should find the correct network.' ),
     139            array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should not find path because domains do not match.' ),
     140            array( 'wordpress.net/three/', 'wordpress.net', '/three/', 'A network can have a path.' ),
     141            array( 'www.wordpress.net/two/', 'www.wordpress.net', '/two/', 'A www network with a path can coexist with a non-www network.' ),
     142            array( 'wordpress.net/', 'site1.wordpress.net', '/notapath/', 'An invalid subdomain should find the top level network domain.' ),
     143            array( 'wordpress.net/', 'site1.wordpress.net', '/three/', 'An invalid subdomain and path should find the top level network domain.' ),
     144            array( 'wordpress.net/', 'x.y.wordpress.net', '/', 'An invalid two level subdomain should find the top level network domain.' ),
     145        );
     146    }
     147
     148    /**
     149     * @ticket 37217
     150     * @dataProvider data_get_network_by_path_with_zero_path_segments
     151     *
     152     * @param string $expected_key The array key associated with expected data for the test.
     153     * @param string $domain       The requested domain.
     154     * @param string $path         The requested path.
     155     * @param string $message      The message to pass for failed tests.
     156     */
     157    public function test_get_network_by_path_with_zero_path_segments( $expected_key, $domain, $path, $message ) {
     158        add_filter( 'network_by_path_segments_count', '__return_zero' );
     159
     160        $network = get_network_by_path( $domain, $path );
     161
     162        remove_filter( 'network_by_path_segments_count', '__return_zero' );
     163
     164        $this->assertSame( self::$network_ids[ $expected_key ], $network->id, $message );
     165    }
     166
     167    public function data_get_network_by_path_with_zero_path_segments() {
     168        return array(
     169            array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ),
     170            array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A network matching a top level domain should be found regardless of path.' ),
     171            array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A network matching a domain should be found regardless of path.' ),
     172            array( 'wordpress.org/', 'www.wordpress.org', '/one/', 'Should find the network despite the www and regardless of path.' ),
     173            array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should find the network with the corresponding top level domain regardless of path.' ),
     174            array( 'www.wordpress.net/', 'www.wordpress.net', '/two/', 'A www network can coexist with a non-www network.' ),
     175            array( 'make.wordpress.org/', 'make.wordpress.org', '/notapath/', 'A subdomain network should be found regardless of path.' ),
     176            array( 'wordpress.net/', 'x.y.wordpress.net', '/', 'An invalid two level subdomain should find the top level network domain.' ),
     177        );
     178    }
     179
     180    /**
     181     * Even if a matching network is available, it should not match if the the filtered
     182     * value for network path segments is fewer than the number of paths passed.
     183     */
     184    public function test_get_network_by_path_with_forced_single_path_segment_returns_single_path_network() {
     185        add_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
     186        $network = get_network_by_path( 'wordpress.org', '/one/b/' );
     187        remove_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
     188
     189        $this->assertSame( self::$network_ids['wordpress.org/one/'], $network->id );
     190    }
     191
     192    public function filter_network_path_segments() {
     193        return 1;
     194    }
     195
     196    /**
     197     * @ticket 27003
     198     * @ticket 27927
     199     * @dataProvider data_get_site_by_path
     200     *
     201     * @param string $expected_key The array key associated with expected data for the test.
     202     * @param string $domain       The requested domain.
     203     * @param string $path         The requested path.
     204     * @param int    $segments     Optional. Number of segments to use in `get_site_by_path()`.
     205     */
     206    public function test_get_site_by_path( $expected_key, $domain, $path, $segments = null ) {
     207        $site = get_site_by_path( $domain, $path, $segments );
     208
     209        if ( $expected_key ) {
     210            $this->assertEquals( self::$site_ids[ $expected_key ], $site->blog_id );
     211        } else {
     212            $this->assertFalse( $site );
     213        }
     214    }
     215
     216    public function data_get_site_by_path() {
     217        return array(
     218            array( 'wordpress.org/', 'wordpress.org', '/notapath/' ),
     219            array( 'wordpress.org/', 'www.wordpress.org', '/notapath/' ),
     220            array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/' ),
     221            array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/' ),
     222            array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 3 ),
     223            array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 3 ),
     224            array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 2 ),
     225            array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 2 ),
     226            array( 'wordpress.org/foo/', 'wordpress.org', '/foo/bar/baz/', 1 ),
     227            array( 'wordpress.org/foo/', 'www.wordpress.org', '/foo/bar/baz/', 1 ),
     228            array( 'wordpress.org/', 'wordpress.org', '/', 0 ),
     229            array( 'wordpress.org/', 'www.wordpress.org', '/', 0 ),
     230            array( 'make.wordpress.org/foo/', 'make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
     231            array( 'make.wordpress.org/foo/', 'www.make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
     232            array( 'www.w.org/', 'www.w.org', '/', 0 ),
     233            array( 'www.w.org/', 'www.w.org', '/notapath' ),
     234            array( 'www.w.org/foo/bar/', 'www.w.org', '/foo/bar/baz/' ),
     235            array( 'www.w.org/foo/', 'www.w.org', '/foo/bar/baz/', 1 ),
     236
     237            // A site installed with www will not be found by the root domain.
     238            array( false, 'w.org', '/' ),
     239            array( false, 'w.org', '/notapath/' ),
     240            array( false, 'w.org', '/foo/bar/baz/' ),
     241            array( false, 'w.org', '/foo/bar/baz/', 1 ),
     242
     243            // A site will not be found by its root domain when an invalid subdomain is requested.
     244            array( false, 'invalid.wordpress.org', '/' ),
     245            array( false, 'invalid.wordpress.org', '/foo/bar/' ),
     246        );
     247    }
     248
     249    /**
     250     * @ticket 27884
     251     * @dataProvider data_multisite_bootstrap
     252     *
     253     * @param string $site_key    The array key associated with the expected site for the test.
     254     * @param string $network_key The array key associated with the expected network for the test.
     255     * @param string $domain      The requested domain.
     256     * @param string $path        The requested path.
     257     */
     258    public function test_multisite_bootstrap( $site_key, $network_key, $domain, $path ) {
     259        global $current_blog;
     260
     261        $expected = array(
     262            'network_id' => self::$network_ids[ $network_key ],
     263            'site_id'    => self::$site_ids[ $site_key ],
     264        );
     265
     266        ms_load_current_site_and_network( $domain, $path );
     267
     268        $actual = array(
     269            'network_id' => $current_blog->site_id,
     270            'site_id'    => $current_blog->blog_id,
     271        );
     272
     273        ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
     274
     275        $this->assertEqualSetsWithIndex( $expected, $actual );
     276    }
     277
     278    public function data_multisite_bootstrap() {
     279        return array(
     280            array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/' ),
     281            array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/2014/04/23/hello-world/' ),
     282            array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/sample-page/' ),
     283            array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/?p=1' ),
     284            array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/wp-admin/' ),
     285            array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/' ),
     286            array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/FOO/' ),
     287            array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/2014/04/23/hello-world/' ),
     288            array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/sample-page/' ),
     289            array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/?p=1' ),
     290            array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/wp-admin/' ),
     291            array( 'make.wordpress.org/', 'make.wordpress.org/', 'make.wordpress.org', '/' ),
     292            array( 'make.wordpress.org/foo/', 'make.wordpress.org/', 'make.wordpress.org', '/foo/' ),
     293        );
     294    }
     295
     296    /**
     297     * @ticket 27884
     298     */
     299    public function test_multisite_bootstrap_additional_path_segments() {
     300        global $current_blog;
     301
     302        $expected = array(
     303            'network_id' => self::$network_ids['wordpress.org/'],
     304            'site_id'    => self::$site_ids['wordpress.org/foo/bar/'],
     305        );
     306
     307        add_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
     308        ms_load_current_site_and_network( 'wordpress.org', '/foo/bar/' );
     309
     310        $actual = array(
     311            'network_id' => $current_blog->site_id,
     312            'site_id'    => $current_blog->blog_id,
     313        );
     314
     315        remove_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
     316        ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
     317
     318        $this->assertEqualSetsWithIndex( $expected, $actual );
     319    }
     320
     321    /**
     322     * @ticket 37053
     323     */
     324    public function test_get_site_by_path_returns_wp_site() {
     325        add_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10, 3 );
     326
     327        $site = get_site_by_path( 'example.com', '/foo/' );
     328
     329        remove_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10 );
     330
     331        $this->assertInstanceOf( 'WP_Site', $site );
     332    }
     333
     334    public function filter_path_segments_to_two() {
     335        return 2;
     336    }
     337
     338    public function filter_pre_get_site_by_path( $site, $domain, $path ) {
     339        $site          = new stdClass();
     340        $site->blog_id = 100;
     341        $site->domain  = $domain;
     342        $site->path    = $path;
     343        $site->site_id = 1;
     344
     345        return $site;
     346    }
     347}
  • trunk/tests/phpunit/tests/multisite/cleanDirsizeCache.php

    r54304 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    5     /**
    6      * Tests specific to the directory size caching in multisite.
    7      *
    8      * @ticket 19879
    9      * @group multisite
    10      */
    11     class Tests_Multisite_CleanDirsizeCache extends WP_UnitTestCase {
    12 
    13         /**
    14          * Test whether dirsize_cache values are used correctly with a more complex dirsize cache mock.
    15          *
    16          * @ticket 19879
    17          */
    18         public function test_get_dirsize_cache_in_recurse_dirsize_mock() {
    19             $blog_id = self::factory()->blog->create();
    20             switch_to_blog( $blog_id );
    21 
    22             /*
    23              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    24              * or if the `src` directory already contains a directory with site content, then the initial
    25              * expectation will be polluted. We create sites until an empty one is available.
    26              */
    27             while ( 0 !== get_space_used() ) {
    28                 restore_current_blog();
    29                 $blog_id = self::factory()->blog->create();
    30                 switch_to_blog( $blog_id );
    31             }
    32 
    33             // Clear the dirsize cache.
    34             delete_transient( 'dirsize_cache' );
    35 
    36             // Set the dirsize cache to our mock.
    37             set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
    38 
    39             $upload_dir = wp_upload_dir();
    40 
    41             // Check recurse_dirsize() against the mock. The cache should match.
    42             $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
    43             $this->assertSame( 22, recurse_dirsize( $upload_dir['basedir'] . '/2/2' ) );
    44             $this->assertSame( 2, recurse_dirsize( $upload_dir['basedir'] . '/2' ) );
    45             $this->assertSame( 11, recurse_dirsize( $upload_dir['basedir'] . '/1/1' ) );
    46             $this->assertSame( 12, recurse_dirsize( $upload_dir['basedir'] . '/1/2' ) );
    47             $this->assertSame( 13, recurse_dirsize( $upload_dir['basedir'] . '/1/3' ) );
    48             $this->assertSame( 1, recurse_dirsize( $upload_dir['basedir'] . '/1' ) );
    49             $this->assertSame( 42, recurse_dirsize( $upload_dir['basedir'] . '/custom_directory' ) );
    50 
    51             // No cache match, upload directory should be empty and return 0.
    52             $this->assertSame( 0, recurse_dirsize( $upload_dir['basedir'] ) );
    53 
    54             // No cache match on non existing directory should return false.
    55             $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
    56 
    57             // Cleanup.
    58             $this->remove_added_uploads();
    59             restore_current_blog();
    60         }
    61 
    62         /**
    63          * Test whether the dirsize_cache invalidation works given a file path as input.
    64          *
    65          * @ticket 19879
    66          */
    67         public function test_clean_dirsize_cache_file_input_mock() {
    68             $blog_id = self::factory()->blog->create();
    69             switch_to_blog( $blog_id );
    70 
    71             /*
    72              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    73              * or if the `src` directory already contains a directory with site content, then the initial
    74              * expectation will be polluted. We create sites until an empty one is available.
    75              */
    76             while ( 0 !== get_space_used() ) {
    77                 restore_current_blog();
    78                 $blog_id = self::factory()->blog->create();
    79                 switch_to_blog( $blog_id );
    80             }
    81 
    82             $upload_dir       = wp_upload_dir();
    83             $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] );
    84 
    85             // Clear the dirsize cache.
    86             delete_transient( 'dirsize_cache' );
    87 
    88             // Set the dirsize cache to our mock.
    89             set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
    90 
    91             $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
    92             $this->assertArrayHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
    93             $this->assertArrayHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
    94 
    95             // Invalidation should also respect the directory tree up.
    96             // Should work fine with path to directory OR file.
    97             clean_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' );
    98 
    99             $this->assertArrayNotHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
    100             $this->assertArrayNotHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
    101 
    102             // Other cache paths should not be invalidated.
    103             $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
    104 
    105             // Cleanup.
    106             $this->remove_added_uploads();
    107             restore_current_blog();
    108         }
    109 
    110         /**
    111          * Test whether the dirsize_cache invalidation works given a directory path as input.
    112          *
    113          * @ticket 19879
    114          */
    115         public function test_clean_dirsize_cache_folder_input_mock() {
    116             $blog_id = self::factory()->blog->create();
    117             switch_to_blog( $blog_id );
    118 
    119             /*
    120              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    121              * or if the `src` directory already contains a directory with site content, then the initial
    122              * expectation will be polluted. We create sites until an empty one is available.
    123              */
    124             while ( 0 !== get_space_used() ) {
    125                 restore_current_blog();
    126                 $blog_id = self::factory()->blog->create();
    127                 switch_to_blog( $blog_id );
    128             }
    129 
    130             $upload_dir       = wp_upload_dir();
    131             $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] );
    132 
    133             // Clear the dirsize cache.
    134             delete_transient( 'dirsize_cache' );
    135 
    136             // Set the dirsize cache to our mock.
    137             set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
    138 
    139             $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
    140             $this->assertArrayHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
    141             $this->assertArrayHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
    142 
    143             // Invalidation should also respect the directory tree up.
    144             // Should work fine with path to directory OR file.
    145             clean_dirsize_cache( $upload_dir['basedir'] . '/2/1' );
    146 
    147             $this->assertArrayNotHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
    148             $this->assertArrayNotHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
    149 
    150             // Other cache paths should not be invalidated.
    151             $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
    152 
    153             // Cleanup.
    154             $this->remove_added_uploads();
    155             restore_current_blog();
    156         }
    157 
    158         /**
    159          * Test whether dirsize_cache values are used correctly with a simple real upload.
    160          *
    161          * @ticket 19879
    162          */
    163         public function test_get_dirsize_cache_in_recurse_dirsize_upload() {
    164             $blog_id = self::factory()->blog->create();
    165             switch_to_blog( $blog_id );
    166 
    167             /*
    168              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    169              * or if the `src` directory already contains a directory with site content, then the initial
    170              * expectation will be polluted. We create sites until an empty one is available.
    171              */
    172             while ( 0 !== get_space_used() ) {
    173                 restore_current_blog();
    174                 $blog_id = self::factory()->blog->create();
    175                 switch_to_blog( $blog_id );
    176             }
    177 
    178             // Clear the dirsize cache.
    179             delete_transient( 'dirsize_cache' );
    180 
    181             $upload_dir = wp_upload_dir();
    182 
    183             $this->assertSame( 0, recurse_dirsize( $upload_dir['path'] ) );
    184 
    185             // Upload a file to the new site using wp_upload_bits().
    186             $filename = __FUNCTION__ . '.jpg';
    187             $contents = __FUNCTION__ . '_contents';
    188             $file     = wp_upload_bits( $filename, null, $contents );
    189 
    190             $calc_size = recurse_dirsize( $upload_dir['path'] );
    191             $size      = filesize( $file['file'] );
    192             $this->assertSame( $size, $calc_size );
    193 
    194             // `dirsize_cache` should now be filled after upload and recurse_dirsize() call.
    195             $cache_path = untrailingslashit( $upload_dir['path'] );
    196             $this->assertIsArray( get_transient( 'dirsize_cache' ) );
    197             $this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] );
    198 
    199             // Cleanup.
    200             $this->remove_added_uploads();
    201             restore_current_blog();
    202         }
    203 
    204         /**
    205          * Test whether the filter to calculate space for an existing directory works as expected.
    206          *
    207          * @ticket 19879
    208          */
    209         public function test_pre_recurse_dirsize_filter() {
    210             add_filter( 'pre_recurse_dirsize', array( $this, 'filter_pre_recurse_dirsize' ) );
    211 
    212             $upload_dir = wp_upload_dir();
    213             $this->assertSame( 1042, recurse_dirsize( $upload_dir['path'] ) );
    214 
    215             remove_filter( 'pre_recurse_dirsize', array( $this, 'filter_pre_recurse_dirsize' ) );
    216         }
    217 
    218         public function filter_pre_recurse_dirsize() {
    219             return 1042;
    220         }
    221 
    222         private function get_mock_dirsize_cache_for_site( $site_id ) {
    223             $prefix = wp_upload_dir()['basedir'];
    224 
    225             return array(
    226                 "$prefix/2/2"              => 22,
    227                 "$prefix/2/1"              => 21,
    228                 "$prefix/2"                => 2,
    229                 "$prefix/1/3"              => 13,
    230                 "$prefix/1/2"              => 12,
    231                 "$prefix/1/1"              => 11,
    232                 "$prefix/1"                => 1,
    233                 "$prefix/custom_directory" => 42,
    234             );
    235         }
    236 
    237         /*
    238          * Test that 5.6+ gracefully handles the old 5.5 transient structure.
    239          *
    240          * @ticket 51913
    241          */
    242         public function test_5_5_transient_structure_compat() {
    243             $blog_id = self::factory()->blog->create();
    244             switch_to_blog( $blog_id );
    245 
    246             /*
    247              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    248              * or if the `src` directory already contains a directory with site content, then the initial
    249              * expectation will be polluted. We create sites until an empty one is available.
    250              */
    251             while ( 0 !== get_space_used() ) {
    252                 restore_current_blog();
    253                 $blog_id = self::factory()->blog->create();
    254                 switch_to_blog( $blog_id );
    255             }
    256 
    257             // Clear the dirsize cache.
    258             delete_transient( 'dirsize_cache' );
    259 
    260             // Set the dirsize cache to our mock.
    261             set_transient( 'dirsize_cache', $this->get_mock_5_5_dirsize_cache( $blog_id ) );
    262 
    263             $upload_dir = wp_upload_dir();
    264 
    265             /*
    266              * The cached size should be ignored, because it's in the old format. The function
    267              * will try to fetch a live value, but in this case the folder doesn't actually
    268              * exist on disk, so the function should fail.
    269              */
    270             $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
    271 
    272             /*
    273              * Now that it's confirmed that old cached values aren't being returned, create the
    274              * folder on disk, so that the the rest of the function can be tested.
    275              */
    276             wp_mkdir_p( $upload_dir['basedir'] . '/2/1' );
    277             $filename = $upload_dir['basedir'] . '/2/1/this-needs-to-exist.txt';
    278             file_put_contents( $filename, 'this file is 21 bytes' );
    279 
    280             // Clear the dirsize cache.
    281             delete_transient( 'dirsize_cache' );
    282 
    283             // Set the dirsize cache to our mock.
    284             set_transient( 'dirsize_cache', $this->get_mock_5_5_dirsize_cache( $blog_id ) );
    285 
    286             /*
    287              * Now that the folder exists, the old cached value should be overwritten
    288              * with the size, using the current format.
    289              */
    290             $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
    291             $this->assertSame( 21, get_transient( 'dirsize_cache' )[ $upload_dir['basedir'] . '/2/1' ] );
    292 
    293             // No cache match on non existing directory should return false.
    294             $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
    295 
    296             // Cleanup.
    297             $this->remove_added_uploads();
    298             rmdir( $upload_dir['basedir'] . '/2/1' );
    299 
    300             restore_current_blog();
    301         }
    302 
    303         private function get_mock_5_5_dirsize_cache( $site_id ) {
    304             $prefix = untrailingslashit( wp_upload_dir()['basedir'] );
    305 
    306             return array(
    307                 "$prefix/2/2"              => array( 'size' => 22 ),
    308                 "$prefix/2/1"              => array( 'size' => 21 ),
    309                 "$prefix/2"                => array( 'size' => 2 ),
    310                 "$prefix/1/3"              => array( 'size' => 13 ),
    311                 "$prefix/1/2"              => array( 'size' => 12 ),
    312                 "$prefix/1/1"              => array( 'size' => 11 ),
    313                 "$prefix/1"                => array( 'size' => 1 ),
    314                 "$prefix/custom_directory" => array( 'size' => 42 ),
    315             );
    316         }
    317     }
    318 
    319 endif;
     3/**
     4 * Tests specific to the directory size caching in multisite.
     5 *
     6 * @ticket 19879
     7 * @group ms-required
     8 * @group multisite
     9 */
     10class Tests_Multisite_CleanDirsizeCache extends WP_UnitTestCase {
     11
     12    /**
     13     * Test whether dirsize_cache values are used correctly with a more complex dirsize cache mock.
     14     *
     15     * @ticket 19879
     16     */
     17    public function test_get_dirsize_cache_in_recurse_dirsize_mock() {
     18        $blog_id = self::factory()->blog->create();
     19        switch_to_blog( $blog_id );
     20
     21        /*
     22         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     23         * or if the `src` directory already contains a directory with site content, then the initial
     24         * expectation will be polluted. We create sites until an empty one is available.
     25         */
     26        while ( 0 !== get_space_used() ) {
     27            restore_current_blog();
     28            $blog_id = self::factory()->blog->create();
     29            switch_to_blog( $blog_id );
     30        }
     31
     32        // Clear the dirsize cache.
     33        delete_transient( 'dirsize_cache' );
     34
     35        // Set the dirsize cache to our mock.
     36        set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
     37
     38        $upload_dir = wp_upload_dir();
     39
     40        // Check recurse_dirsize() against the mock. The cache should match.
     41        $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
     42        $this->assertSame( 22, recurse_dirsize( $upload_dir['basedir'] . '/2/2' ) );
     43        $this->assertSame( 2, recurse_dirsize( $upload_dir['basedir'] . '/2' ) );
     44        $this->assertSame( 11, recurse_dirsize( $upload_dir['basedir'] . '/1/1' ) );
     45        $this->assertSame( 12, recurse_dirsize( $upload_dir['basedir'] . '/1/2' ) );
     46        $this->assertSame( 13, recurse_dirsize( $upload_dir['basedir'] . '/1/3' ) );
     47        $this->assertSame( 1, recurse_dirsize( $upload_dir['basedir'] . '/1' ) );
     48        $this->assertSame( 42, recurse_dirsize( $upload_dir['basedir'] . '/custom_directory' ) );
     49
     50        // No cache match, upload directory should be empty and return 0.
     51        $this->assertSame( 0, recurse_dirsize( $upload_dir['basedir'] ) );
     52
     53        // No cache match on non existing directory should return false.
     54        $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
     55
     56        // Cleanup.
     57        $this->remove_added_uploads();
     58        restore_current_blog();
     59    }
     60
     61    /**
     62     * Test whether the dirsize_cache invalidation works given a file path as input.
     63     *
     64     * @ticket 19879
     65     */
     66    public function test_clean_dirsize_cache_file_input_mock() {
     67        $blog_id = self::factory()->blog->create();
     68        switch_to_blog( $blog_id );
     69
     70        /*
     71         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     72         * or if the `src` directory already contains a directory with site content, then the initial
     73         * expectation will be polluted. We create sites until an empty one is available.
     74         */
     75        while ( 0 !== get_space_used() ) {
     76            restore_current_blog();
     77            $blog_id = self::factory()->blog->create();
     78            switch_to_blog( $blog_id );
     79        }
     80
     81        $upload_dir       = wp_upload_dir();
     82        $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] );
     83
     84        // Clear the dirsize cache.
     85        delete_transient( 'dirsize_cache' );
     86
     87        // Set the dirsize cache to our mock.
     88        set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
     89
     90        $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
     91        $this->assertArrayHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
     92        $this->assertArrayHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
     93
     94        // Invalidation should also respect the directory tree up.
     95        // Should work fine with path to directory OR file.
     96        clean_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' );
     97
     98        $this->assertArrayNotHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
     99        $this->assertArrayNotHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
     100
     101        // Other cache paths should not be invalidated.
     102        $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
     103
     104        // Cleanup.
     105        $this->remove_added_uploads();
     106        restore_current_blog();
     107    }
     108
     109    /**
     110     * Test whether the dirsize_cache invalidation works given a directory path as input.
     111     *
     112     * @ticket 19879
     113     */
     114    public function test_clean_dirsize_cache_folder_input_mock() {
     115        $blog_id = self::factory()->blog->create();
     116        switch_to_blog( $blog_id );
     117
     118        /*
     119         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     120         * or if the `src` directory already contains a directory with site content, then the initial
     121         * expectation will be polluted. We create sites until an empty one is available.
     122         */
     123        while ( 0 !== get_space_used() ) {
     124            restore_current_blog();
     125            $blog_id = self::factory()->blog->create();
     126            switch_to_blog( $blog_id );
     127        }
     128
     129        $upload_dir       = wp_upload_dir();
     130        $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] );
     131
     132        // Clear the dirsize cache.
     133        delete_transient( 'dirsize_cache' );
     134
     135        // Set the dirsize cache to our mock.
     136        set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
     137
     138        $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
     139        $this->assertArrayHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
     140        $this->assertArrayHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
     141
     142        // Invalidation should also respect the directory tree up.
     143        // Should work fine with path to directory OR file.
     144        clean_dirsize_cache( $upload_dir['basedir'] . '/2/1' );
     145
     146        $this->assertArrayNotHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
     147        $this->assertArrayNotHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
     148
     149        // Other cache paths should not be invalidated.
     150        $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
     151
     152        // Cleanup.
     153        $this->remove_added_uploads();
     154        restore_current_blog();
     155    }
     156
     157    /**
     158     * Test whether dirsize_cache values are used correctly with a simple real upload.
     159     *
     160     * @ticket 19879
     161     */
     162    public function test_get_dirsize_cache_in_recurse_dirsize_upload() {
     163        $blog_id = self::factory()->blog->create();
     164        switch_to_blog( $blog_id );
     165
     166        /*
     167         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     168         * or if the `src` directory already contains a directory with site content, then the initial
     169         * expectation will be polluted. We create sites until an empty one is available.
     170         */
     171        while ( 0 !== get_space_used() ) {
     172            restore_current_blog();
     173            $blog_id = self::factory()->blog->create();
     174            switch_to_blog( $blog_id );
     175        }
     176
     177        // Clear the dirsize cache.
     178        delete_transient( 'dirsize_cache' );
     179
     180        $upload_dir = wp_upload_dir();
     181
     182        $this->assertSame( 0, recurse_dirsize( $upload_dir['path'] ) );
     183
     184        // Upload a file to the new site using wp_upload_bits().
     185        $filename = __FUNCTION__ . '.jpg';
     186        $contents = __FUNCTION__ . '_contents';
     187        $file     = wp_upload_bits( $filename, null, $contents );
     188
     189        $calc_size = recurse_dirsize( $upload_dir['path'] );
     190        $size      = filesize( $file['file'] );
     191        $this->assertSame( $size, $calc_size );
     192
     193        // `dirsize_cache` should now be filled after upload and recurse_dirsize() call.
     194        $cache_path = untrailingslashit( $upload_dir['path'] );
     195        $this->assertIsArray( get_transient( 'dirsize_cache' ) );
     196        $this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] );
     197
     198        // Cleanup.
     199        $this->remove_added_uploads();
     200        restore_current_blog();
     201    }
     202
     203    /**
     204     * Test whether the filter to calculate space for an existing directory works as expected.
     205     *
     206     * @ticket 19879
     207     */
     208    public function test_pre_recurse_dirsize_filter() {
     209        add_filter( 'pre_recurse_dirsize', array( $this, 'filter_pre_recurse_dirsize' ) );
     210
     211        $upload_dir = wp_upload_dir();
     212        $this->assertSame( 1042, recurse_dirsize( $upload_dir['path'] ) );
     213
     214        remove_filter( 'pre_recurse_dirsize', array( $this, 'filter_pre_recurse_dirsize' ) );
     215    }
     216
     217    public function filter_pre_recurse_dirsize() {
     218        return 1042;
     219    }
     220
     221    private function get_mock_dirsize_cache_for_site( $site_id ) {
     222        $prefix = wp_upload_dir()['basedir'];
     223
     224        return array(
     225            "$prefix/2/2"              => 22,
     226            "$prefix/2/1"              => 21,
     227            "$prefix/2"                => 2,
     228            "$prefix/1/3"              => 13,
     229            "$prefix/1/2"              => 12,
     230            "$prefix/1/1"              => 11,
     231            "$prefix/1"                => 1,
     232            "$prefix/custom_directory" => 42,
     233        );
     234    }
     235
     236    /*
     237     * Test that 5.6+ gracefully handles the old 5.5 transient structure.
     238     *
     239     * @ticket 51913
     240     */
     241    public function test_5_5_transient_structure_compat() {
     242        $blog_id = self::factory()->blog->create();
     243        switch_to_blog( $blog_id );
     244
     245        /*
     246         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     247         * or if the `src` directory already contains a directory with site content, then the initial
     248         * expectation will be polluted. We create sites until an empty one is available.
     249         */
     250        while ( 0 !== get_space_used() ) {
     251            restore_current_blog();
     252            $blog_id = self::factory()->blog->create();
     253            switch_to_blog( $blog_id );
     254        }
     255
     256        // Clear the dirsize cache.
     257        delete_transient( 'dirsize_cache' );
     258
     259        // Set the dirsize cache to our mock.
     260        set_transient( 'dirsize_cache', $this->get_mock_5_5_dirsize_cache( $blog_id ) );
     261
     262        $upload_dir = wp_upload_dir();
     263
     264        /*
     265         * The cached size should be ignored, because it's in the old format. The function
     266         * will try to fetch a live value, but in this case the folder doesn't actually
     267         * exist on disk, so the function should fail.
     268         */
     269        $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
     270
     271        /*
     272         * Now that it's confirmed that old cached values aren't being returned, create the
     273         * folder on disk, so that the the rest of the function can be tested.
     274         */
     275        wp_mkdir_p( $upload_dir['basedir'] . '/2/1' );
     276        $filename = $upload_dir['basedir'] . '/2/1/this-needs-to-exist.txt';
     277        file_put_contents( $filename, 'this file is 21 bytes' );
     278
     279        // Clear the dirsize cache.
     280        delete_transient( 'dirsize_cache' );
     281
     282        // Set the dirsize cache to our mock.
     283        set_transient( 'dirsize_cache', $this->get_mock_5_5_dirsize_cache( $blog_id ) );
     284
     285        /*
     286         * Now that the folder exists, the old cached value should be overwritten
     287         * with the size, using the current format.
     288         */
     289        $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
     290        $this->assertSame( 21, get_transient( 'dirsize_cache' )[ $upload_dir['basedir'] . '/2/1' ] );
     291
     292        // No cache match on non existing directory should return false.
     293        $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
     294
     295        // Cleanup.
     296        $this->remove_added_uploads();
     297        rmdir( $upload_dir['basedir'] . '/2/1' );
     298
     299        restore_current_blog();
     300    }
     301
     302    private function get_mock_5_5_dirsize_cache( $site_id ) {
     303        $prefix = untrailingslashit( wp_upload_dir()['basedir'] );
     304
     305        return array(
     306            "$prefix/2/2"              => array( 'size' => 22 ),
     307            "$prefix/2/1"              => array( 'size' => 21 ),
     308            "$prefix/2"                => array( 'size' => 2 ),
     309            "$prefix/1/3"              => array( 'size' => 13 ),
     310            "$prefix/1/2"              => array( 'size' => 12 ),
     311            "$prefix/1/1"              => array( 'size' => 11 ),
     312            "$prefix/1"                => array( 'size' => 1 ),
     313            "$prefix/custom_directory" => array( 'size' => 42 ),
     314        );
     315    }
     316}
  • trunk/tests/phpunit/tests/multisite/forceSslContent.php

    r60147 r60148  
    66 *
    77 * @group functions
     8 * @group ms-required
    89 * @group multisite
    9  * @group ms-required
    1010 *
    1111 * @covers ::force_ssl_content
  • trunk/tests/phpunit/tests/multisite/getBlogDetails.php

    r51860 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @ticket 29845
     5 * @group ms-required
     6 * @group ms-site
     7 * @group multisite
     8 */
     9class Tests_Multisite_GetBlogDetails extends WP_UnitTestCase {
     10
     11    protected static $network_ids;
     12    protected static $site_ids;
     13
     14    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     15        self::$site_ids = array(
     16            WP_TESTS_DOMAIN . '/foo/'      => array(
     17                'domain' => WP_TESTS_DOMAIN,
     18                'path'   => '/foo/',
     19            ),
     20            'foo.' . WP_TESTS_DOMAIN . '/' => array(
     21                'domain' => 'foo.' . WP_TESTS_DOMAIN,
     22                'path'   => '/',
     23            ),
     24            'wordpress.org/'               => array(
     25                'domain' => 'wordpress.org',
     26                'path'   => '/',
     27            ),
     28        );
     29
     30        foreach ( self::$site_ids as &$id ) {
     31            $id = $factory->blog->create( $id );
     32        }
     33        unset( $id );
     34    }
     35
     36    public static function wpTearDownAfterClass() {
     37        foreach ( self::$site_ids as $id ) {
     38            wp_delete_site( $id );
     39        }
     40
     41        wp_update_network_site_counts();
     42    }
     43
     44    public function test_get_blog_details_with_no_arguments_returns_current_site() {
     45        $site = get_blog_details();
     46        $this->assertEquals( get_current_blog_id(), $site->blog_id );
     47    }
     48
     49    public function test_get_blog_details_with_site_name_string_subdirectory() {
     50        if ( is_subdomain_install() ) {
     51            $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' );
     52        }
     53
     54        $site = get_blog_details( 'foo' );
     55        $this->assertEquals( self::$site_ids[ WP_TESTS_DOMAIN . '/foo/' ], $site->blog_id );
     56    }
     57
     58    public function test_get_blog_details_with_site_name_string_subdomain() {
     59        if ( ! is_subdomain_install() ) {
     60            $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' );
     61        }
     62
     63        $site = get_blog_details( 'foo' );
     64        $this->assertEquals( self::$site_ids[ 'foo.' . WP_TESTS_DOMAIN . '/' ], $site->blog_id );
     65    }
     66
     67    public function test_get_blog_details_with_invalid_site_name_string() {
     68        $site = get_blog_details( 'invalid' );
     69        $this->assertFalse( $site );
     70    }
     71
     72    public function test_get_blog_details_with_site_id_int() {
     73        $site = get_blog_details( self::$site_ids['wordpress.org/'] );
     74        $this->assertEquals( self::$site_ids['wordpress.org/'], $site->blog_id );
     75    }
     76
     77    public function test_get_blog_details_with_invalid_site_id_int() {
     78        $site = get_blog_details( 99999 );
     79        $this->assertFalse( $site );
     80    }
     81
     82    public function test_get_blog_details_with_blog_id_in_fields() {
     83        $site = get_blog_details( array( 'blog_id' => self::$site_ids['wordpress.org/'] ) );
     84        $this->assertEquals( self::$site_ids['wordpress.org/'], $site->blog_id );
     85    }
     86
     87    public function test_get_blog_details_with_invalid_blog_id_in_fields() {
     88        $site = get_blog_details( array( 'blog_id' => 88888 ) );
     89        $this->assertFalse( $site );
     90    }
     91
     92    public function test_get_blog_details_with_domain_and_path_in_fields() {
     93        $site = get_blog_details(
     94            array(
     95                'domain' => 'wordpress.org',
     96                'path'   => '/',
     97            )
     98        );
     99        $this->assertEquals( self::$site_ids['wordpress.org/'], $site->blog_id );
     100    }
     101
     102    public function test_get_blog_details_with_domain_and_invalid_path_in_fields() {
     103        $site = get_blog_details(
     104            array(
     105                'domain' => 'wordpress.org',
     106                'path'   => '/zxy/',
     107            )
     108        );
     109        $this->assertFalse( $site );
     110    }
     111
     112    public function test_get_blog_details_with_path_and_invalid_domain_in_fields() {
     113        $site = get_blog_details(
     114            array(
     115                'domain' => 'invalid.org',
     116                'path'   => '/foo/',
     117            )
     118        );
     119        $this->assertFalse( $site );
     120    }
     121
     122    public function test_get_blog_details_with_only_domain_in_fields_subdomain() {
     123        if ( ! is_subdomain_install() ) {
     124            $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' );
     125        }
     126
     127        $site = get_blog_details( array( 'domain' => 'wordpress.org' ) );
     128        $this->assertSame( self::$site_ids['wordpress.org/'], $site->blog_id );
     129    }
     130
     131    public function test_get_blog_details_with_only_domain_in_fields_subdirectory() {
     132        if ( is_subdomain_install() ) {
     133            $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' );
     134        }
     135
     136        $site = get_blog_details( array( 'domain' => 'wordpress.org' ) );
     137        $this->assertFalse( $site );
     138    }
     139
     140    public function test_get_blog_details_with_only_path_in_fields() {
     141        $site = get_blog_details( array( 'path' => '/foo/' ) );
     142        $this->assertFalse( $site );
     143    }
    4144
    5145    /**
    6      * @ticket 29845
    7      * @group ms-site
    8      * @group multisite
     146     * @ticket 50391
    9147     */
    10     class Tests_Multisite_GetBlogDetails extends WP_UnitTestCase {
    11         protected static $network_ids;
    12         protected static $site_ids;
    13 
    14         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    15             self::$site_ids = array(
    16                 WP_TESTS_DOMAIN . '/foo/'      => array(
    17                     'domain' => WP_TESTS_DOMAIN,
    18                     'path'   => '/foo/',
    19                 ),
    20                 'foo.' . WP_TESTS_DOMAIN . '/' => array(
    21                     'domain' => 'foo.' . WP_TESTS_DOMAIN,
    22                     'path'   => '/',
    23                 ),
    24                 'wordpress.org/'               => array(
    25                     'domain' => 'wordpress.org',
    26                     'path'   => '/',
    27                 ),
    28             );
    29 
    30             foreach ( self::$site_ids as &$id ) {
    31                 $id = $factory->blog->create( $id );
    32             }
    33             unset( $id );
    34         }
    35 
    36         public static function wpTearDownAfterClass() {
    37             foreach ( self::$site_ids as $id ) {
    38                 wp_delete_site( $id );
    39             }
    40 
    41             wp_update_network_site_counts();
    42         }
    43 
    44         public function test_get_blog_details_with_no_arguments_returns_current_site() {
    45             $site = get_blog_details();
    46             $this->assertEquals( get_current_blog_id(), $site->blog_id );
    47         }
    48 
    49         public function test_get_blog_details_with_site_name_string_subdirectory() {
    50             if ( is_subdomain_install() ) {
    51                 $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' );
    52             }
    53 
    54             $site = get_blog_details( 'foo' );
    55             $this->assertEquals( self::$site_ids[ WP_TESTS_DOMAIN . '/foo/' ], $site->blog_id );
    56         }
    57 
    58         public function test_get_blog_details_with_site_name_string_subdomain() {
    59             if ( ! is_subdomain_install() ) {
    60                 $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' );
    61             }
    62 
    63             $site = get_blog_details( 'foo' );
    64             $this->assertEquals( self::$site_ids[ 'foo.' . WP_TESTS_DOMAIN . '/' ], $site->blog_id );
    65         }
    66 
    67         public function test_get_blog_details_with_invalid_site_name_string() {
    68             $site = get_blog_details( 'invalid' );
    69             $this->assertFalse( $site );
    70         }
    71 
    72         public function test_get_blog_details_with_site_id_int() {
    73             $site = get_blog_details( self::$site_ids['wordpress.org/'] );
    74             $this->assertEquals( self::$site_ids['wordpress.org/'], $site->blog_id );
    75         }
    76 
    77         public function test_get_blog_details_with_invalid_site_id_int() {
    78             $site = get_blog_details( 99999 );
    79             $this->assertFalse( $site );
    80         }
    81 
    82         public function test_get_blog_details_with_blog_id_in_fields() {
    83             $site = get_blog_details( array( 'blog_id' => self::$site_ids['wordpress.org/'] ) );
    84             $this->assertEquals( self::$site_ids['wordpress.org/'], $site->blog_id );
    85         }
    86 
    87         public function test_get_blog_details_with_invalid_blog_id_in_fields() {
    88             $site = get_blog_details( array( 'blog_id' => 88888 ) );
    89             $this->assertFalse( $site );
    90         }
    91 
    92         public function test_get_blog_details_with_domain_and_path_in_fields() {
    93             $site = get_blog_details(
     148    public function test_get_blog_details_does_not_switch_to_current_blog() {
     149        $count = did_action( 'switch_blog' );
     150
     151        get_blog_details();
     152        $this->assertSame( $count, did_action( 'switch_blog' ) );
     153    }
     154
     155    /**
     156     * @dataProvider data_get_all
     157     *
     158     * @ticket 40228
     159     */
     160    public function test_get_blog_details_get_object_vars( $get_all ) {
     161        $site = get_blog_details(
     162            array(
     163                'domain' => 'wordpress.org',
     164                'path'   => '/',
     165            ),
     166            $get_all
     167        );
     168
     169        $result = array_keys( get_object_vars( $site ) );
     170
     171        $this->assertSameSets( $this->get_fields( $get_all ), $result );
     172    }
     173
     174    /**
     175     * @dataProvider data_get_all
     176     *
     177     * @ticket 40228
     178     */
     179    public function test_get_blog_details_iterate_over_result( $get_all ) {
     180        $site = get_blog_details(
     181            array(
     182                'domain' => 'wordpress.org',
     183                'path'   => '/',
     184            ),
     185            $get_all
     186        );
     187
     188        $result = array();
     189        foreach ( $site as $key => $value ) {
     190            $result[] = $key;
     191        }
     192
     193        $this->assertSameSets( $this->get_fields( $get_all ), $result );
     194    }
     195
     196    public function data_get_all() {
     197        return array(
     198            array( false ),
     199            array( true ),
     200        );
     201    }
     202
     203    protected function get_fields( $all = false ) {
     204        $fields = array(
     205            'blog_id',
     206            'domain',
     207            'path',
     208            'site_id',
     209            'registered',
     210            'last_updated',
     211            'public',
     212            'archived',
     213            'mature',
     214            'spam',
     215            'deleted',
     216            'lang_id',
     217        );
     218
     219        if ( $all ) {
     220            $fields = array_merge(
     221                $fields,
    94222                array(
    95                     'domain' => 'wordpress.org',
    96                     'path'   => '/',
     223                    'blogname',
     224                    'siteurl',
     225                    'post_count',
     226                    'home',
    97227                )
    98228            );
    99             $this->assertEquals( self::$site_ids['wordpress.org/'], $site->blog_id );
    100         }
    101 
    102         public function test_get_blog_details_with_domain_and_invalid_path_in_fields() {
    103             $site = get_blog_details(
    104                 array(
    105                     'domain' => 'wordpress.org',
    106                     'path'   => '/zxy/',
    107                 )
    108             );
    109             $this->assertFalse( $site );
    110         }
    111 
    112         public function test_get_blog_details_with_path_and_invalid_domain_in_fields() {
    113             $site = get_blog_details(
    114                 array(
    115                     'domain' => 'invalid.org',
    116                     'path'   => '/foo/',
    117                 )
    118             );
    119             $this->assertFalse( $site );
    120         }
    121 
    122         public function test_get_blog_details_with_only_domain_in_fields_subdomain() {
    123             if ( ! is_subdomain_install() ) {
    124                 $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' );
    125             }
    126 
    127             $site = get_blog_details( array( 'domain' => 'wordpress.org' ) );
    128             $this->assertSame( self::$site_ids['wordpress.org/'], $site->blog_id );
    129         }
    130 
    131         public function test_get_blog_details_with_only_domain_in_fields_subdirectory() {
    132             if ( is_subdomain_install() ) {
    133                 $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' );
    134             }
    135 
    136             $site = get_blog_details( array( 'domain' => 'wordpress.org' ) );
    137             $this->assertFalse( $site );
    138         }
    139 
    140         public function test_get_blog_details_with_only_path_in_fields() {
    141             $site = get_blog_details( array( 'path' => '/foo/' ) );
    142             $this->assertFalse( $site );
    143         }
    144 
    145         /**
    146          * @ticket 50391
    147          */
    148         public function test_get_blog_details_does_not_switch_to_current_blog() {
    149             $count = did_action( 'switch_blog' );
    150 
    151             get_blog_details();
    152             $this->assertSame( $count, did_action( 'switch_blog' ) );
    153         }
    154 
    155         /**
    156          * @dataProvider data_get_all
    157          *
    158          * @ticket 40228
    159          */
    160         public function test_get_blog_details_get_object_vars( $get_all ) {
    161             $site = get_blog_details(
    162                 array(
    163                     'domain' => 'wordpress.org',
    164                     'path'   => '/',
    165                 ),
    166                 $get_all
    167             );
    168 
    169             $result = array_keys( get_object_vars( $site ) );
    170 
    171             $this->assertSameSets( $this->get_fields( $get_all ), $result );
    172         }
    173 
    174         /**
    175          * @dataProvider data_get_all
    176          *
    177          * @ticket 40228
    178          */
    179         public function test_get_blog_details_iterate_over_result( $get_all ) {
    180             $site = get_blog_details(
    181                 array(
    182                     'domain' => 'wordpress.org',
    183                     'path'   => '/',
    184                 ),
    185                 $get_all
    186             );
    187 
    188             $result = array();
    189             foreach ( $site as $key => $value ) {
    190                 $result[] = $key;
    191             }
    192 
    193             $this->assertSameSets( $this->get_fields( $get_all ), $result );
    194         }
    195 
    196         public function data_get_all() {
    197             return array(
    198                 array( false ),
    199                 array( true ),
    200             );
    201         }
    202 
    203         protected function get_fields( $all = false ) {
    204             $fields = array(
    205                 'blog_id',
    206                 'domain',
    207                 'path',
    208                 'site_id',
    209                 'registered',
    210                 'last_updated',
    211                 'public',
    212                 'archived',
    213                 'mature',
    214                 'spam',
    215                 'deleted',
    216                 'lang_id',
    217             );
    218 
    219             if ( $all ) {
    220                 $fields = array_merge(
    221                     $fields,
    222                     array(
    223                         'blogname',
    224                         'siteurl',
    225                         'post_count',
    226                         'home',
    227                     )
    228                 );
    229             }
    230 
    231             return $fields;
    232         }
    233     }
    234 
    235 endif;
     229        }
     230
     231        return $fields;
     232    }
     233}
  • trunk/tests/phpunit/tests/multisite/getIdFromBlogname.php

    r56547 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4     /**
    5      * Test get_id_from_blogname() in multisite.
    6      *
    7      * @group blogname
    8      * @group ms-site
    9      * @group multisite
    10      */
    11     class Tests_Multisite_GetIdFromBlogname extends WP_UnitTestCase {
    12         protected static $network_ids;
    13         protected static $site_ids;
     3/**
     4 * Test get_id_from_blogname() in multisite.
     5 *
     6 * @group blogname
     7 * @group ms-required
     8 * @group ms-site
     9 * @group multisite
     10 */
     11class Tests_Multisite_GetIdFromBlogname extends WP_UnitTestCase {
    1412
    15         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    16             self::$network_ids = array(
    17                 'wordpress.org/'     => array(
    18                     'domain' => 'wordpress.org',
    19                     'path'   => '/',
    20                 ),
    21                 'www.wordpress.net/' => array(
    22                     'domain' => 'www.wordpress.net',
    23                     'path'   => '/',
    24                 ),
    25             );
     13    protected static $network_ids;
     14    protected static $site_ids;
    2615
    27             foreach ( self::$network_ids as &$id ) {
    28                 $id = $factory->network->create( $id );
    29             }
    30             unset( $id );
     16    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     17        self::$network_ids = array(
     18            'wordpress.org/'     => array(
     19                'domain' => 'wordpress.org',
     20                'path'   => '/',
     21            ),
     22            'www.wordpress.net/' => array(
     23                'domain' => 'www.wordpress.net',
     24                'path'   => '/',
     25            ),
     26        );
    3127
    32             self::$site_ids = array(
    33                 'wordpress.org/'         => array(
    34                     'domain'     => 'wordpress.org',
    35                     'path'       => '/',
    36                     'network_id' => self::$network_ids['wordpress.org/'],
    37                 ),
    38                 'foo.wordpress.org/'     => array(
    39                     'domain'     => 'foo.wordpress.org',
    40                     'path'       => '/',
    41                     'network_id' => self::$network_ids['wordpress.org/'],
    42                 ),
    43                 'wordpress.org/foo/'     => array(
    44                     'domain'     => 'wordpress.org',
    45                     'path'       => '/foo/',
    46                     'network_id' => self::$network_ids['wordpress.org/'],
    47                 ),
    48                 'www.wordpress.net/'     => array(
    49                     'domain'     => 'www.wordpress.net',
    50                     'path'       => '/',
    51                     'network_id' => self::$network_ids['www.wordpress.net/'],
    52                 ),
    53                 'foo.wordpress.net/'     => array(
    54                     'domain'     => 'foo.wordpress.net',
    55                     'path'       => '/',
    56                     'network_id' => self::$network_ids['www.wordpress.net/'],
    57                 ),
    58                 'www.wordpress.net/foo/' => array(
    59                     'domain'     => 'www.wordpress.net',
    60                     'path'       => '/foo/',
    61                     'network_id' => self::$network_ids['www.wordpress.net/'],
    62                 ),
    63             );
     28        foreach ( self::$network_ids as &$id ) {
     29            $id = $factory->network->create( $id );
     30        }
     31        unset( $id );
    6432
    65             foreach ( self::$site_ids as &$id ) {
    66                 $id = $factory->blog->create( $id );
    67             }
    68             unset( $id );
     33        self::$site_ids = array(
     34            'wordpress.org/'         => array(
     35                'domain'     => 'wordpress.org',
     36                'path'       => '/',
     37                'network_id' => self::$network_ids['wordpress.org/'],
     38            ),
     39            'foo.wordpress.org/'     => array(
     40                'domain'     => 'foo.wordpress.org',
     41                'path'       => '/',
     42                'network_id' => self::$network_ids['wordpress.org/'],
     43            ),
     44            'wordpress.org/foo/'     => array(
     45                'domain'     => 'wordpress.org',
     46                'path'       => '/foo/',
     47                'network_id' => self::$network_ids['wordpress.org/'],
     48            ),
     49            'www.wordpress.net/'     => array(
     50                'domain'     => 'www.wordpress.net',
     51                'path'       => '/',
     52                'network_id' => self::$network_ids['www.wordpress.net/'],
     53            ),
     54            'foo.wordpress.net/'     => array(
     55                'domain'     => 'foo.wordpress.net',
     56                'path'       => '/',
     57                'network_id' => self::$network_ids['www.wordpress.net/'],
     58            ),
     59            'www.wordpress.net/foo/' => array(
     60                'domain'     => 'www.wordpress.net',
     61                'path'       => '/foo/',
     62                'network_id' => self::$network_ids['www.wordpress.net/'],
     63            ),
     64        );
     65
     66        foreach ( self::$site_ids as &$id ) {
     67            $id = $factory->blog->create( $id );
     68        }
     69        unset( $id );
     70    }
     71
     72    public static function wpTearDownAfterClass() {
     73        global $wpdb;
     74
     75        foreach ( self::$site_ids as $id ) {
     76            wp_delete_site( $id );
    6977        }
    7078
    71         public static function wpTearDownAfterClass() {
    72             global $wpdb;
    73 
    74             foreach ( self::$site_ids as $id ) {
    75                 wp_delete_site( $id );
    76             }
    77 
    78             foreach ( self::$network_ids as $id ) {
    79                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    80                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    81             }
    82 
    83             wp_update_network_site_counts();
     79        foreach ( self::$network_ids as $id ) {
     80            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     81            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    8482        }
    8583
    86         /**
    87          * @ticket 34450
    88          */
    89         public function test_get_id_from_blogname_no_www() {
    90             global $current_site;
     84        wp_update_network_site_counts();
     85    }
    9186
    92             $original_network = $current_site;
    93             $current_site     = get_network( self::$network_ids['wordpress.org/'] );
     87    /**
     88     * @ticket 34450
     89     */
     90    public function test_get_id_from_blogname_no_www() {
     91        global $current_site;
    9492
    95             if ( is_subdomain_install() ) {
    96                 $expected = self::$site_ids['foo.wordpress.org/'];
    97             } else {
    98                 $expected = self::$site_ids['wordpress.org/foo/'];
    99             }
     93        $original_network = $current_site;
     94        $current_site     = get_network( self::$network_ids['wordpress.org/'] );
    10095
    101             $result       = get_id_from_blogname( 'foo' );
    102             $current_site = $original_network;
    103 
    104             $this->assertSame( $expected, $result );
     96        if ( is_subdomain_install() ) {
     97            $expected = self::$site_ids['foo.wordpress.org/'];
     98        } else {
     99            $expected = self::$site_ids['wordpress.org/foo/'];
    105100        }
    106101
    107         /**
    108          * @ticket 34450
    109          */
    110         public function test_get_id_from_blogname_www() {
    111             global $current_site;
     102        $result       = get_id_from_blogname( 'foo' );
     103        $current_site = $original_network;
    112104
    113             $original_network = $current_site;
    114             $current_site     = get_network( self::$network_ids['www.wordpress.net/'] );
     105        $this->assertSame( $expected, $result );
     106    }
    115107
    116             if ( is_subdomain_install() ) {
    117                 $expected = self::$site_ids['foo.wordpress.net/'];
    118             } else {
    119                 $expected = self::$site_ids['www.wordpress.net/foo/'];
    120             }
     108    /**
     109     * @ticket 34450
     110     */
     111    public function test_get_id_from_blogname_www() {
     112        global $current_site;
    121113
    122             $result       = get_id_from_blogname( 'foo' );
    123             $current_site = $original_network;
     114        $original_network = $current_site;
     115        $current_site     = get_network( self::$network_ids['www.wordpress.net/'] );
    124116
    125             $this->assertSame( $expected, $result );
     117        if ( is_subdomain_install() ) {
     118            $expected = self::$site_ids['foo.wordpress.net/'];
     119        } else {
     120            $expected = self::$site_ids['www.wordpress.net/foo/'];
    126121        }
    127122
    128         public function test_get_id_from_blogname_invalid_slug() {
    129             global $current_site;
     123        $result       = get_id_from_blogname( 'foo' );
     124        $current_site = $original_network;
    130125
    131             $original_network = $current_site;
    132             $current_site     = get_network( self::$network_ids['wordpress.org/'] );
    133 
    134             $result       = get_id_from_blogname( 'bar' );
    135             $current_site = $original_network;
    136 
    137             $this->assertNull( $result );
    138         }
     126        $this->assertSame( $expected, $result );
    139127    }
    140128
    141 endif;
     129    public function test_get_id_from_blogname_invalid_slug() {
     130        global $current_site;
     131
     132        $original_network = $current_site;
     133        $current_site     = get_network( self::$network_ids['wordpress.org/'] );
     134
     135        $result       = get_id_from_blogname( 'bar' );
     136        $current_site = $original_network;
     137
     138        $this->assertNull( $result );
     139    }
     140}
  • trunk/tests/phpunit/tests/multisite/getMainSiteId.php

    r54256 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Tests for the get_main_site_id() function.
     5 *
     6 * @group ms-required
     7 * @group ms-site
     8 * @group multisite
     9 */
     10class Tests_Multisite_GetMainSiteId extends WP_UnitTestCase {
     11
     12    protected static $network_ids;
     13    protected static $site_ids;
     14
     15    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     16        self::$network_ids = array(
     17            'wordpress.org/' => array(
     18                'domain' => 'wordpress.org',
     19                'path'   => '/',
     20            ),
     21            'wp.org/'        => array(
     22                'domain' => 'wp.org',
     23                'path'   => '/',
     24            ), // A network with no sites.
     25        );
     26
     27        foreach ( self::$network_ids as &$id ) {
     28            $id = $factory->network->create( $id );
     29        }
     30        unset( $id );
     31
     32        self::$site_ids = array(
     33            'www.w.org/'         => array(
     34                'domain' => 'www.w.org',
     35                'path'   => '/',
     36            ),
     37            'wordpress.org/'     => array(
     38                'domain'     => 'wordpress.org',
     39                'path'       => '/',
     40                'network_id' => self::$network_ids['wordpress.org/'],
     41            ),
     42            'wordpress.org/foo/' => array(
     43                'domain'     => 'wordpress.org',
     44                'path'       => '/foo/',
     45                'network_id' => self::$network_ids['wordpress.org/'],
     46            ),
     47        );
     48
     49        foreach ( self::$site_ids as &$id ) {
     50            $id = $factory->blog->create( $id );
     51        }
     52        unset( $id );
     53    }
     54
     55    public static function wpTearDownAfterClass() {
     56        foreach ( self::$site_ids as $id ) {
     57            wp_delete_site( $id );
     58        }
     59
     60        global $wpdb;
     61
     62        foreach ( self::$network_ids as $id ) {
     63            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     64            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     65        }
     66
     67        wp_update_network_site_counts();
     68    }
    469
    570    /**
    6      * Tests for the get_main_site_id() function.
    7      *
    8      * @group ms-site
    9      * @group multisite
     71     * @ticket 29684
    1072     */
    11     class Tests_Multisite_GetMainSiteId extends WP_UnitTestCase {
    12         protected static $network_ids;
    13         protected static $site_ids;
     73    public function test_get_main_site_id_on_main_site_returns_self() {
     74        $this->assertSame( get_current_blog_id(), get_main_site_id() );
     75    }
    1476
    15         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    16             self::$network_ids = array(
    17                 'wordpress.org/' => array(
    18                     'domain' => 'wordpress.org',
    19                     'path'   => '/',
    20                 ),
    21                 'wp.org/'        => array(
    22                     'domain' => 'wp.org',
    23                     'path'   => '/',
    24                 ), // A network with no sites.
    25             );
     77    /**
     78     * @ticket 29684
     79     */
     80    public function test_get_main_site_id_returns_main_site_in_switched_context() {
     81        $main_site_id  = get_current_blog_id();
     82        $other_site_id = self::$site_ids['www.w.org/'];
    2683
    27             foreach ( self::$network_ids as &$id ) {
    28                 $id = $factory->network->create( $id );
    29             }
    30             unset( $id );
     84        switch_to_blog( $other_site_id );
     85        $result = get_main_site_id();
     86        restore_current_blog();
    3187
    32             self::$site_ids = array(
    33                 'www.w.org/'         => array(
    34                     'domain' => 'www.w.org',
    35                     'path'   => '/',
    36                 ),
    37                 'wordpress.org/'     => array(
    38                     'domain'     => 'wordpress.org',
    39                     'path'       => '/',
    40                     'network_id' => self::$network_ids['wordpress.org/'],
    41                 ),
    42                 'wordpress.org/foo/' => array(
    43                     'domain'     => 'wordpress.org',
    44                     'path'       => '/foo/',
    45                     'network_id' => self::$network_ids['wordpress.org/'],
    46                 ),
    47             );
     88        $this->assertSame( $main_site_id, $result );
     89    }
    4890
    49             foreach ( self::$site_ids as &$id ) {
    50                 $id = $factory->blog->create( $id );
    51             }
    52             unset( $id );
    53         }
     91    /**
     92     * @ticket 55802
     93     */
     94    public function test_get_main_site_id_with_different_network_cache_id() {
     95        $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ), 'Main blog id needs to match blog id of wordpress.org/' );
     96        $this->assertSame( self::$site_ids['wordpress.org/'], (int) get_network_option( self::$network_ids['wordpress.org/'], 'main_site' ), 'Network option needs to match blog id of wordpress.org/' );
    5497
    55         public static function wpTearDownAfterClass() {
    56             foreach ( self::$site_ids as $id ) {
    57                 wp_delete_site( $id );
    58             }
     98        $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ), 'Main blog id should not be found' );
     99        $this->assertSame( 0, (int) get_network_option( self::$network_ids['wp.org/'], 'main_site' ), 'Network option should not be found' );
     100    }
    59101
    60             global $wpdb;
     102    /**
     103     * @ticket 29684
     104     */
     105    public function test_get_main_site_id_with_different_network_returns_correct_id() {
     106        $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) );
     107    }
    61108
    62             foreach ( self::$network_ids as $id ) {
    63                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    64                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    65             }
     109    /**
     110     * @ticket 29684
     111     */
     112    public function test_get_main_site_id_on_network_without_site_returns_0() {
     113        $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) );
     114    }
    66115
    67             wp_update_network_site_counts();
    68         }
     116    /**
     117     * @ticket 29684
     118     */
     119    public function test_get_main_site_id_on_invalid_network_returns_0() {
     120        $this->assertSame( 0, get_main_site_id( 333 ) );
     121    }
    69122
    70         /**
    71         * @ticket 29684
    72         */
    73         public function test_get_main_site_id_on_main_site_returns_self() {
    74             $this->assertSame( get_current_blog_id(), get_main_site_id() );
    75         }
     123    /**
     124    * @ticket 29684
     125    */
     126    public function test_get_main_site_id_filtered() {
     127        add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) );
     128        $result = get_main_site_id();
    76129
    77         /**
    78          * @ticket 29684
    79          */
    80         public function test_get_main_site_id_returns_main_site_in_switched_context() {
    81             $main_site_id  = get_current_blog_id();
    82             $other_site_id = self::$site_ids['www.w.org/'];
     130        $this->assertSame( 333, $result );
     131    }
    83132
    84             switch_to_blog( $other_site_id );
    85             $result = get_main_site_id();
    86             restore_current_blog();
     133    public function filter_get_main_site_id() {
     134        return 333;
     135    }
    87136
    88             $this->assertSame( $main_site_id, $result );
    89         }
     137    /**
     138     * @ticket 29684
     139     */
     140    public function test_get_main_site_id_filtered_depending_on_network() {
     141        add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id_depending_on_network' ), 10, 2 );
     142        $result = get_main_site_id( self::$network_ids['wordpress.org/'] );
    90143
    91         /**
    92          * @ticket 55802
    93          */
    94         public function test_get_main_site_id_with_different_network_cache_id() {
    95             $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ), 'Main blog id needs to match blog id of wordpress.org/' );
    96             $this->assertSame( self::$site_ids['wordpress.org/'], (int) get_network_option( self::$network_ids['wordpress.org/'], 'main_site' ), 'Network option needs to match blog id of wordpress.org/' );
     144        $this->assertSame( 333, $result );
     145    }
    97146
    98             $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ), 'Main blog id should not be found' );
    99             $this->assertSame( 0, (int) get_network_option( self::$network_ids['wp.org/'], 'main_site' ), 'Network option should not be found' );
    100         }
    101 
    102         /**
    103          * @ticket 29684
    104          */
    105         public function test_get_main_site_id_with_different_network_returns_correct_id() {
    106             $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) );
    107         }
    108 
    109         /**
    110          * @ticket 29684
    111          */
    112         public function test_get_main_site_id_on_network_without_site_returns_0() {
    113             $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) );
    114         }
    115 
    116         /**
    117          * @ticket 29684
    118          */
    119         public function test_get_main_site_id_on_invalid_network_returns_0() {
    120             $this->assertSame( 0, get_main_site_id( 333 ) );
    121         }
    122 
    123         /**
    124          * @ticket 29684
    125          */
    126         public function test_get_main_site_id_filtered() {
    127             add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) );
    128             $result = get_main_site_id();
    129 
    130             $this->assertSame( 333, $result );
    131         }
    132 
    133         public function filter_get_main_site_id() {
     147    public function filter_get_main_site_id_depending_on_network( $main_site_id, $network ) {
     148        // Override main site ID for a specific network for the test.
     149        if ( $network->id === (int) self::$network_ids['wordpress.org/'] ) {
    134150            return 333;
    135151        }
    136152
    137         /**
    138          * @ticket 29684
    139          */
    140         public function test_get_main_site_id_filtered_depending_on_network() {
    141             add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id_depending_on_network' ), 10, 2 );
    142             $result = get_main_site_id( self::$network_ids['wordpress.org/'] );
    143 
    144             $this->assertSame( 333, $result );
    145         }
    146 
    147         public function filter_get_main_site_id_depending_on_network( $main_site_id, $network ) {
    148             // Override main site ID for a specific network for the test.
    149             if ( $network->id === (int) self::$network_ids['wordpress.org/'] ) {
    150                 return 333;
    151             }
    152 
    153             return $main_site_id;
    154         }
    155 
    156         /**
    157          * @ticket 41936
    158          */
    159         public function test_get_main_site_id_with_property_value() {
    160             global $current_site;
    161 
    162             $original_main_site_id = $current_site->blog_id;
    163             $current_site->blog_id = '123';
    164 
    165             $result = get_main_site_id();
    166 
    167             $current_site->blog_id = $original_main_site_id;
    168 
    169             $this->assertSame( 123, $result );
    170         }
    171 
    172         /**
    173          * @ticket 41936
    174          */
    175         public function test_get_main_site_id_filtered_with_property_value() {
    176             global $current_site;
    177 
    178             $original_main_site_id = $current_site->blog_id;
    179             $current_site->blog_id = '123';
    180 
    181             add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) );
    182             $result = get_main_site_id();
    183 
    184             $current_site->blog_id = $original_main_site_id;
    185 
    186             $this->assertSame( 333, $result );
    187         }
     153        return $main_site_id;
    188154    }
    189155
    190 endif;
     156    /**
     157     * @ticket 41936
     158     */
     159    public function test_get_main_site_id_with_property_value() {
     160        global $current_site;
     161
     162        $original_main_site_id = $current_site->blog_id;
     163        $current_site->blog_id = '123';
     164
     165        $result = get_main_site_id();
     166
     167        $current_site->blog_id = $original_main_site_id;
     168
     169        $this->assertSame( 123, $result );
     170    }
     171
     172    /**
     173     * @ticket 41936
     174     */
     175    public function test_get_main_site_id_filtered_with_property_value() {
     176        global $current_site;
     177
     178        $original_main_site_id = $current_site->blog_id;
     179        $current_site->blog_id = '123';
     180
     181        add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) );
     182        $result = get_main_site_id();
     183
     184        $current_site->blog_id = $original_main_site_id;
     185
     186        $this->assertSame( 333, $result );
     187    }
     188}
  • trunk/tests/phpunit/tests/multisite/getSite.php

    r56547 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4     /**
    5      * Test get_site() wrapper of WP_Site in multisite.
    6      *
    7      * @group ms-site
    8      * @group multisite
    9      */
    10     class Tests_Multisite_GetSite extends WP_UnitTestCase {
    11         protected static $site_ids;
     3/**
     4 * Test get_site() wrapper of WP_Site in multisite.
     5 *
     6 * @group ms-required
     7 * @group ms-site
     8 * @group multisite
     9 */
     10class Tests_Multisite_GetSite extends WP_UnitTestCase {
    1211
    13         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    14             self::$site_ids = array(
    15                 'wordpress.org/'         => array(
    16                     'domain' => 'wordpress.org',
    17                     'path'   => '/',
    18                 ),
    19                 'wordpress.org/foo/'     => array(
    20                     'domain' => 'wordpress.org',
    21                     'path'   => '/foo/',
    22                 ),
    23                 'wordpress.org/foo/bar/' => array(
    24                     'domain' => 'wordpress.org',
    25                     'path'   => '/foo/bar/',
    26                 ),
    27             );
     12    protected static $site_ids;
    2813
    29             foreach ( self::$site_ids as &$id ) {
    30                 $id = $factory->blog->create( $id );
    31             }
    32             unset( $id );
     14    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     15        self::$site_ids = array(
     16            'wordpress.org/'         => array(
     17                'domain' => 'wordpress.org',
     18                'path'   => '/',
     19            ),
     20            'wordpress.org/foo/'     => array(
     21                'domain' => 'wordpress.org',
     22                'path'   => '/foo/',
     23            ),
     24            'wordpress.org/foo/bar/' => array(
     25                'domain' => 'wordpress.org',
     26                'path'   => '/foo/bar/',
     27            ),
     28        );
     29
     30        foreach ( self::$site_ids as &$id ) {
     31            $id = $factory->blog->create( $id );
     32        }
     33        unset( $id );
     34    }
     35
     36    public static function wpTearDownAfterClass() {
     37        foreach ( self::$site_ids as $id ) {
     38            wp_delete_site( $id );
    3339        }
    3440
    35         public static function wpTearDownAfterClass() {
    36             foreach ( self::$site_ids as $id ) {
    37                 wp_delete_site( $id );
    38             }
    39 
    40             wp_update_network_site_counts();
    41         }
    42 
    43         public function test_get_site_in_switched_state_returns_switched_site() {
    44             switch_to_blog( self::$site_ids['wordpress.org/foo/'] );
    45             $site = get_site();
    46             restore_current_blog();
    47 
    48             $this->assertSame( self::$site_ids['wordpress.org/foo/'], $site->id );
    49         }
     41        wp_update_network_site_counts();
    5042    }
    5143
    52 endif;
     44    public function test_get_site_in_switched_state_returns_switched_site() {
     45        switch_to_blog( self::$site_ids['wordpress.org/foo/'] );
     46        $site = get_site();
     47        restore_current_blog();
     48
     49        $this->assertSame( self::$site_ids['wordpress.org/foo/'], $site->id );
     50    }
     51}
  • trunk/tests/phpunit/tests/multisite/getSpaceAllowed.php

    r51869 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Tests specific to `get_space_allowed()` in multisite.
     5 *
     6 * @group ms-required
     7 * @group multisite
     8 */
     9class Tests_Multisite_GetSpaceAllowed extends WP_UnitTestCase {
    410
    511    /**
    6      * Tests specific to `get_space_allowed()` in multisite.
    7      *
    8      * @group multisite
     12     * When no option exists for the site or the network, a fallback of
     13     * 100 is expected.
    914     */
    10     class Tests_Multisite_GetSpaceAllowed extends WP_UnitTestCase {
     15    public function test_get_space_allowed_default() {
     16        delete_option( 'blog_upload_space' );
     17        delete_site_option( 'blog_upload_space' );
    1118
    12         /**
    13          * When no option exists for the site or the network, a fallback of
    14          * 100 is expected.
    15          */
    16         public function test_get_space_allowed_default() {
    17             delete_option( 'blog_upload_space' );
    18             delete_site_option( 'blog_upload_space' );
    19 
    20             $this->assertSame( 100, get_space_allowed() );
    21         }
    22 
    23         /**
    24          * If an individual site's option is not available, the default network
    25          * level option is used as a fallback.
    26          */
    27         public function test_get_space_allowed_no_site_option_fallback_to_network_option() {
    28             delete_site_option( 'blog_upload_space' );
    29             update_site_option( 'blog_upload_space', 200 );
    30 
    31             $this->assertSame( 200, get_space_allowed() );
    32         }
    33 
    34         /**
    35          * @dataProvider data_blog_upload_space
    36          *
    37          * @param mixed $site_option    Option to assign to the site's `blog_upload_space`.
    38          * @param mixed $network_option Option to assign to the network's `blog_upload_space`.
    39          * @param int   $expected       Expected return value.
    40          */
    41         public function test_get_space_allowed( $site_option, $network_option, $expected ) {
    42             update_option( 'blog_upload_space', $site_option );
    43             update_site_option( 'blog_upload_space', $network_option );
    44 
    45             $this->assertSame( $expected, get_space_allowed() );
    46         }
    47 
    48         public function data_blog_upload_space() {
    49             return array(
    50                 // A valid site option will be preferred over a network option.
    51                 array( 111, 200, 111 ),
    52                 array( -1, 200, -1 ),
    53                 array( 222, 0, 222 ),
    54 
    55                 // Non-numeric site options should result in a fallback to the network option.
    56                 array( '', 333, 333 ),
    57                 array( false, 444, 444 ),
    58                 array( 'NAN', 555, 555 ),
    59                 array( false, -10, -10 ),
    60 
    61                 // If neither network or site options are valid, fallback to the default.
    62                 array( false, false, 100 ),
    63                 array( 'NAN', 'NAN', 100 ),
    64 
    65                 // These effectively disable uploads.
    66                 array( 0, 666, 0 ),
    67                 array( false, 0, 0 ),
    68                 array( 'NAN', 0, 0 ),
    69             );
    70         }
    71 
    72         public function test_get_space_allowed_filtered() {
    73             update_option( 'blog_upload_space', 777 );
    74             update_site_option( 'blog_upload_space', 888 );
    75 
    76             add_filter( 'get_space_allowed', array( $this, '_filter_space_allowed' ) );
    77             $space_allowed = get_space_allowed();
    78             remove_filter( 'get_space_allowed', array( $this, '_filter_space_allowed' ) );
    79 
    80             $this->assertSame( 999, $space_allowed );
    81         }
    82 
    83         public function _filter_space_allowed() {
    84             return 999;
    85         }
     19        $this->assertSame( 100, get_space_allowed() );
    8620    }
    8721
    88 endif;
     22    /**
     23     * If an individual site's option is not available, the default network
     24     * level option is used as a fallback.
     25     */
     26    public function test_get_space_allowed_no_site_option_fallback_to_network_option() {
     27        delete_site_option( 'blog_upload_space' );
     28        update_site_option( 'blog_upload_space', 200 );
     29
     30        $this->assertSame( 200, get_space_allowed() );
     31    }
     32
     33    /**
     34     * @dataProvider data_blog_upload_space
     35     *
     36     * @param mixed $site_option    Option to assign to the site's `blog_upload_space`.
     37     * @param mixed $network_option Option to assign to the network's `blog_upload_space`.
     38     * @param int   $expected       Expected return value.
     39     */
     40    public function test_get_space_allowed( $site_option, $network_option, $expected ) {
     41        update_option( 'blog_upload_space', $site_option );
     42        update_site_option( 'blog_upload_space', $network_option );
     43
     44        $this->assertSame( $expected, get_space_allowed() );
     45    }
     46
     47    public function data_blog_upload_space() {
     48        return array(
     49            // A valid site option will be preferred over a network option.
     50            array( 111, 200, 111 ),
     51            array( -1, 200, -1 ),
     52            array( 222, 0, 222 ),
     53
     54            // Non-numeric site options should result in a fallback to the network option.
     55            array( '', 333, 333 ),
     56            array( false, 444, 444 ),
     57            array( 'NAN', 555, 555 ),
     58            array( false, -10, -10 ),
     59
     60            // If neither network or site options are valid, fallback to the default.
     61            array( false, false, 100 ),
     62            array( 'NAN', 'NAN', 100 ),
     63
     64            // These effectively disable uploads.
     65            array( 0, 666, 0 ),
     66            array( false, 0, 0 ),
     67            array( 'NAN', 0, 0 ),
     68        );
     69    }
     70
     71    public function test_get_space_allowed_filtered() {
     72        update_option( 'blog_upload_space', 777 );
     73        update_site_option( 'blog_upload_space', 888 );
     74
     75        add_filter( 'get_space_allowed', array( $this, '_filter_space_allowed' ) );
     76        $space_allowed = get_space_allowed();
     77        remove_filter( 'get_space_allowed', array( $this, '_filter_space_allowed' ) );
     78
     79        $this->assertSame( 999, $space_allowed );
     80    }
     81
     82    public function _filter_space_allowed() {
     83        return 999;
     84    }
     85}
  • trunk/tests/phpunit/tests/multisite/getSpaceUsed.php

    r52010 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group multisite
     6 * @covers ::get_space_used
     7 */
     8class Tests_Multisite_GetSpaceUsed extends WP_UnitTestCase {
     9
     10    public function test_get_space_used_switched_site() {
     11        $blog_id = self::factory()->blog->create();
     12        switch_to_blog( $blog_id );
     13
     14        // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
     15        // src directory already contains a content directory with site content, then the initial expectation
     16        // will be polluted. We create sites until an empty one is available.
     17        while ( 0 !== get_space_used() ) {
     18            restore_current_blog();
     19            $blog_id = self::factory()->blog->create();
     20            switch_to_blog( $blog_id );
     21        }
     22
     23        // Upload a file to the new site.
     24        $filename = __FUNCTION__ . '.jpg';
     25        $contents = __FUNCTION__ . '_contents';
     26        $file     = wp_upload_bits( $filename, null, $contents );
     27
     28        // get_space_used() is measures in MB, get the size of the new file in MB.
     29        $size = filesize( $file['file'] ) / 1024 / 1024;
     30
     31        delete_transient( 'dirsize_cache' );
     32
     33        $this->assertSame( $size, get_space_used() );
     34        $upload_dir = wp_upload_dir();
     35        $this->remove_added_uploads();
     36        $this->delete_folders( $upload_dir['basedir'] );
     37        restore_current_blog();
     38    }
    439
    540    /**
    6      * @group multisite
    7      * @covers ::get_space_used
     41     * Directories of sub sites on a network should not count against the same spaced used total for
     42     * the main site.
    843     */
    9     class Tests_Multisite_GetSpaceUsed extends WP_UnitTestCase {
     44    public function test_get_space_used_main_site() {
     45        $space_used = get_space_used();
    1046
    11         public function test_get_space_used_switched_site() {
     47        $blog_id = self::factory()->blog->create();
     48        switch_to_blog( $blog_id );
     49
     50        // We don't rely on an initial value of 0 for space used, but should have a clean space available
     51        // so that we can remove any uploaded files and directories without concern of a conflict with
     52        // existing content directories in src.
     53        while ( 0 !== get_space_used() ) {
     54            restore_current_blog();
    1255            $blog_id = self::factory()->blog->create();
    1356            switch_to_blog( $blog_id );
    14 
    15             // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
    16             // src directory already contains a content directory with site content, then the initial expectation
    17             // will be polluted. We create sites until an empty one is available.
    18             while ( 0 !== get_space_used() ) {
    19                 restore_current_blog();
    20                 $blog_id = self::factory()->blog->create();
    21                 switch_to_blog( $blog_id );
    22             }
    23 
    24             // Upload a file to the new site.
    25             $filename = __FUNCTION__ . '.jpg';
    26             $contents = __FUNCTION__ . '_contents';
    27             $file     = wp_upload_bits( $filename, null, $contents );
    28 
    29             // get_space_used() is measures in MB, get the size of the new file in MB.
    30             $size = filesize( $file['file'] ) / 1024 / 1024;
    31 
    32             delete_transient( 'dirsize_cache' );
    33 
    34             $this->assertSame( $size, get_space_used() );
    35             $upload_dir = wp_upload_dir();
    36             $this->remove_added_uploads();
    37             $this->delete_folders( $upload_dir['basedir'] );
    38             restore_current_blog();
    3957        }
    4058
    41         /**
    42          * Directories of sub sites on a network should not count against the same spaced used total for
    43          * the main site.
    44          */
    45         public function test_get_space_used_main_site() {
    46             $space_used = get_space_used();
     59        // Upload a file to the new site.
     60        $filename = __FUNCTION__ . '.jpg';
     61        $contents = __FUNCTION__ . '_contents';
     62        wp_upload_bits( $filename, null, $contents );
    4763
    48             $blog_id = self::factory()->blog->create();
    49             switch_to_blog( $blog_id );
     64        restore_current_blog();
    5065
    51             // We don't rely on an initial value of 0 for space used, but should have a clean space available
    52             // so that we can remove any uploaded files and directories without concern of a conflict with
    53             // existing content directories in src.
    54             while ( 0 !== get_space_used() ) {
    55                 restore_current_blog();
    56                 $blog_id = self::factory()->blog->create();
    57                 switch_to_blog( $blog_id );
    58             }
     66        delete_transient( 'dirsize_cache' );
    5967
    60             // Upload a file to the new site.
    61             $filename = __FUNCTION__ . '.jpg';
    62             $contents = __FUNCTION__ . '_contents';
    63             wp_upload_bits( $filename, null, $contents );
     68        $this->assertSame( $space_used, get_space_used() );
    6469
    65             restore_current_blog();
     70        // Switch back to the new site to remove the uploaded file.
     71        switch_to_blog( $blog_id );
     72        $upload_dir = wp_upload_dir();
     73        $this->remove_added_uploads();
     74        $this->delete_folders( $upload_dir['basedir'] );
     75        restore_current_blog();
     76    }
    6677
    67             delete_transient( 'dirsize_cache' );
     78    public function test_get_space_used_pre_get_spaced_used_filter() {
     79        add_filter( 'pre_get_space_used', array( $this, 'filter_space_used' ) );
    6880
    69             $this->assertSame( $space_used, get_space_used() );
     81        $this->assertSame( 300, get_space_used() );
    7082
    71             // Switch back to the new site to remove the uploaded file.
    72             switch_to_blog( $blog_id );
    73             $upload_dir = wp_upload_dir();
    74             $this->remove_added_uploads();
    75             $this->delete_folders( $upload_dir['basedir'] );
    76             restore_current_blog();
    77         }
     83        remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used' ) );
     84    }
    7885
    79         public function test_get_space_used_pre_get_spaced_used_filter() {
    80             add_filter( 'pre_get_space_used', array( $this, 'filter_space_used' ) );
    81 
    82             $this->assertSame( 300, get_space_used() );
    83 
    84             remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used' ) );
    85         }
    86 
    87         public function filter_space_used() {
    88             return 300;
    89         }
     86    public function filter_space_used() {
     87        return 300;
    9088    }
    91 endif;
     89}
  • trunk/tests/phpunit/tests/multisite/isEmailAddressUnsafe.php

    r46586 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group multisite
     6 */
     7class Tests_Multisite_IsEmailAddressUnsafe extends WP_UnitTestCase {
     8
     9    public function test_string_domain_list_should_be_split_on_line_breaks() {
     10        update_site_option( 'banned_email_domains', "foo.com\nbar.org\nbaz.gov" );
     11        $this->assertTrue( is_email_address_unsafe( 'foo@bar.org' ) );
     12        $this->assertFalse( is_email_address_unsafe( 'foo@example.org' ) );
     13    }
    414
    515    /**
    6      * @group multisite
     16     * @dataProvider data_unsafe
     17     * @ticket 25046
     18     * @ticket 21570
    719     */
    8     class Tests_Multisite_IsEmailAddressUnsafe extends WP_UnitTestCase {
    9         public function test_string_domain_list_should_be_split_on_line_breaks() {
    10             update_site_option( 'banned_email_domains', "foo.com\nbar.org\nbaz.gov" );
    11             $this->assertTrue( is_email_address_unsafe( 'foo@bar.org' ) );
    12             $this->assertFalse( is_email_address_unsafe( 'foo@example.org' ) );
    13         }
    14 
    15         /**
    16          * @dataProvider data_unsafe
    17          * @ticket 25046
    18          * @ticket 21570
    19          */
    20         public function test_unsafe_emails( $banned, $email ) {
    21             update_site_option( 'banned_email_domains', $banned );
    22             $this->assertTrue( is_email_address_unsafe( $email ) );
    23         }
    24 
    25         /**
    26          * @dataProvider data_safe
    27          * @ticket 25046
    28          * @ticket 21570
    29          */
    30         public function test_safe_emails( $banned, $email ) {
    31             update_site_option( 'banned_email_domains', $banned );
    32             $this->assertFalse( is_email_address_unsafe( $email ) );
    33         }
    34 
    35         public function data_unsafe() {
    36             return array(
    37                 // 25046
    38                 'case_insensitive_1' => array(
    39                     array( 'baR.com' ),
    40                     'test@Bar.com',
    41                 ),
    42                 'case_insensitive_2' => array(
    43                     array( 'baR.com' ),
    44                     'tEst@bar.com',
    45                 ),
    46                 'case_insensitive_3' => array(
    47                     array( 'barfoo.COM' ),
    48                     'test@barFoo.com',
    49                 ),
    50                 'case_insensitive_4' => array(
    51                     array( 'baR.com' ),
    52                     'tEst@foo.bar.com',
    53                 ),
    54                 'case_insensitive_5' => array(
    55                     array( 'BAZ.com' ),
    56                     'test@baz.Com',
    57                 ),
    58 
    59                 // 21570
    60                 array(
    61                     array( 'bar.com', 'foo.co' ),
    62                     'test@bar.com',
    63                 ),
    64                 'subdomain_1'        => array(
    65                     array( 'bar.com', 'foo.co' ),
    66                     'test@foo.bar.com',
    67                 ),
    68                 array(
    69                     array( 'bar.com', 'foo.co' ),
    70                     'test@foo.co',
    71                 ),
    72                 'subdomain_2'        => array(
    73                     array( 'bar.com', 'foo.co' ),
    74                     'test@subdomain.foo.co',
    75                 ),
    76             );
    77         }
    78 
    79         public function data_safe() {
    80             return array(
    81                 // 25046
    82                 array(
    83                     array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
    84                     'test@Foobar.com',
    85                 ),
    86                 array(
    87                     array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
    88                     'test@Foo-bar.com',
    89                 ),
    90                 array(
    91                     array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
    92                     'tEst@foobar.com',
    93                 ),
    94                 array(
    95                     array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
    96                     'test@Subdomain.Foo.com',
    97                 ),
    98                 array(
    99                     array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
    100                     'test@feeBAz.com',
    101                 ),
    102 
    103                 // 21570
    104                 array(
    105                     array( 'bar.com', 'foo.co' ),
    106                     'test@foobar.com',
    107                 ),
    108                 array(
    109                     array( 'bar.com', 'foo.co' ),
    110                     'test@foo-bar.com',
    111                 ),
    112                 array(
    113                     array( 'bar.com', 'foo.co' ),
    114                     'test@foo.com',
    115                 ),
    116                 array(
    117                     array( 'bar.com', 'foo.co' ),
    118                     'test@subdomain.foo.com',
    119                 ),
    120             );
    121         }
    122 
    123         public function test_email_with_only_top_level_domain_returns_safe() {
    124             update_site_option( 'banned_email_domains', 'bar.com' );
    125             $safe = is_email_address_unsafe( 'email@localhost' );
    126             delete_site_option( 'banned_email_domains' );
    127 
    128             $this->assertFalse( $safe );
    129         }
    130 
    131         public function test_invalid_email_without_domain_returns_safe() {
    132             update_site_option( 'banned_email_domains', 'bar.com' );
    133             $safe = is_email_address_unsafe( 'invalid-email' );
    134             delete_site_option( 'bar.com' );
    135 
    136             $this->assertFalse( $safe );
    137         }
     20    public function test_unsafe_emails( $banned, $email ) {
     21        update_site_option( 'banned_email_domains', $banned );
     22        $this->assertTrue( is_email_address_unsafe( $email ) );
    13823    }
    13924
    140 endif;
     25    /**
     26     * @dataProvider data_safe
     27     * @ticket 25046
     28     * @ticket 21570
     29     */
     30    public function test_safe_emails( $banned, $email ) {
     31        update_site_option( 'banned_email_domains', $banned );
     32        $this->assertFalse( is_email_address_unsafe( $email ) );
     33    }
     34
     35    public function data_unsafe() {
     36        return array(
     37            // 25046
     38            'case_insensitive_1' => array(
     39                array( 'baR.com' ),
     40                'test@Bar.com',
     41            ),
     42            'case_insensitive_2' => array(
     43                array( 'baR.com' ),
     44                'tEst@bar.com',
     45            ),
     46            'case_insensitive_3' => array(
     47                array( 'barfoo.COM' ),
     48                'test@barFoo.com',
     49            ),
     50            'case_insensitive_4' => array(
     51                array( 'baR.com' ),
     52                'tEst@foo.bar.com',
     53            ),
     54            'case_insensitive_5' => array(
     55                array( 'BAZ.com' ),
     56                'test@baz.Com',
     57            ),
     58
     59            // 21570
     60            array(
     61                array( 'bar.com', 'foo.co' ),
     62                'test@bar.com',
     63            ),
     64            'subdomain_1'        => array(
     65                array( 'bar.com', 'foo.co' ),
     66                'test@foo.bar.com',
     67            ),
     68            array(
     69                array( 'bar.com', 'foo.co' ),
     70                'test@foo.co',
     71            ),
     72            'subdomain_2'        => array(
     73                array( 'bar.com', 'foo.co' ),
     74                'test@subdomain.foo.co',
     75            ),
     76        );
     77    }
     78
     79    public function data_safe() {
     80        return array(
     81            // 25046
     82            array(
     83                array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
     84                'test@Foobar.com',
     85            ),
     86            array(
     87                array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
     88                'test@Foo-bar.com',
     89            ),
     90            array(
     91                array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
     92                'tEst@foobar.com',
     93            ),
     94            array(
     95                array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
     96                'test@Subdomain.Foo.com',
     97            ),
     98            array(
     99                array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ),
     100                'test@feeBAz.com',
     101            ),
     102
     103            // 21570
     104            array(
     105                array( 'bar.com', 'foo.co' ),
     106                'test@foobar.com',
     107            ),
     108            array(
     109                array( 'bar.com', 'foo.co' ),
     110                'test@foo-bar.com',
     111            ),
     112            array(
     113                array( 'bar.com', 'foo.co' ),
     114                'test@foo.com',
     115            ),
     116            array(
     117                array( 'bar.com', 'foo.co' ),
     118                'test@subdomain.foo.com',
     119            ),
     120        );
     121    }
     122
     123    public function test_email_with_only_top_level_domain_returns_safe() {
     124        update_site_option( 'banned_email_domains', 'bar.com' );
     125        $safe = is_email_address_unsafe( 'email@localhost' );
     126        delete_site_option( 'banned_email_domains' );
     127
     128        $this->assertFalse( $safe );
     129    }
     130
     131    public function test_invalid_email_without_domain_returns_safe() {
     132        update_site_option( 'banned_email_domains', 'bar.com' );
     133        $safe = is_email_address_unsafe( 'invalid-email' );
     134        delete_site_option( 'bar.com' );
     135
     136        $this->assertFalse( $safe );
     137    }
     138}
  • trunk/tests/phpunit/tests/multisite/isUploadSpaceAvailable.php

    r52010 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Tests specific to `is_upload_space_available()` in multisite.
     5 *
     6 * These tests filter `pre_get_space_used` so that we can ignore the local
     7 * environment. Tests for `get_space_used()` are handled elsewhere.
     8 *
     9 * @group ms-required
     10 * @group multisite
     11 */
     12class Tests_Multisite_IsUploadSpaceAvailable extends WP_UnitTestCase {
     13
     14    public function set_up() {
     15        parent::set_up();
     16        update_site_option( 'upload_space_check_disabled', false );
     17    }
    418
    519    /**
    6      * Tests specific to `is_upload_space_available()` in multisite.
    7      *
    8      * These tests filter `pre_get_space_used` so that we can ignore the local
    9      * environment. Tests for `get_space_used()` are handled elsewhere.
    10      *
    11      * @group multisite
     20     * A default of 100MB is used when no `blog_upload_space` option
     21     * exists at the site or network level.
    1222     */
    13     class Tests_Multisite_IsUploadSpaceAvailable extends WP_UnitTestCase {
     23    public function test_is_upload_space_available_default() {
     24        delete_option( 'blog_upload_space' );
     25        delete_site_option( 'blog_upload_space' );
    1426
    15         public function set_up() {
    16             parent::set_up();
    17             update_site_option( 'upload_space_check_disabled', false );
    18         }
     27        add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
     28        $available = is_upload_space_available();
     29        remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
    1930
    20         /**
    21          * A default of 100MB is used when no `blog_upload_space` option
    22          * exists at the site or network level.
    23          */
    24         public function test_is_upload_space_available_default() {
    25             delete_option( 'blog_upload_space' );
    26             delete_site_option( 'blog_upload_space' );
    27 
    28             add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
    29             $available = is_upload_space_available();
    30             remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
    31 
    32             $this->assertTrue( $available );
    33         }
    34 
    35         public function test_is_upload_space_available_check_disabled() {
    36             update_site_option( 'blog_upload_space', 10 );
    37             update_site_option( 'upload_space_check_disabled', true );
    38 
    39             add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_large' ) );
    40             $available = is_upload_space_available();
    41             remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_large' ) );
    42 
    43             $this->assertTrue( $available );
    44         }
    45 
    46         public function test_is_upload_space_available_space_used_is_less_then_allowed() {
    47             update_option( 'blog_upload_space', 350 );
    48 
    49             add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
    50             $available = is_upload_space_available();
    51             remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
    52 
    53             $this->assertTrue( $available );
    54         }
    55 
    56         public function test_is_upload_space_available_space_used_is_more_than_allowed() {
    57             update_option( 'blog_upload_space', 350 );
    58 
    59             add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_large' ) );
    60             $available = is_upload_space_available();
    61             remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_large' ) );
    62 
    63             $this->assertFalse( $available );
    64         }
    65 
    66         /**
    67          * More comprehensive testing a 0 condition is handled in the tests
    68          * for `get_space_allowed()`. We cover one scenario here.
    69          */
    70         public function test_is_upload_space_available_upload_space_0_defaults_to_100() {
    71             update_option( 'blog_upload_space', 0 );
    72 
    73             add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
    74             $available = is_upload_space_available();
    75             remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
    76 
    77             $this->assertFalse( $available );
    78         }
    79 
    80         public function test_is_upload_space_available_upload_space_negative() {
    81             update_site_option( 'blog_upload_space', -1 );
    82 
    83             add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
    84             $available = is_upload_space_available();
    85             remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
    86 
    87             $this->assertFalse( $available );
    88         }
    89 
    90         public function filter_space_used_large() {
    91             return 10000000;
    92         }
    93 
    94         public function filter_space_used_small() {
    95             return 10;
    96         }
     31        $this->assertTrue( $available );
    9732    }
    9833
    99 endif;
     34    public function test_is_upload_space_available_check_disabled() {
     35        update_site_option( 'blog_upload_space', 10 );
     36        update_site_option( 'upload_space_check_disabled', true );
     37
     38        add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_large' ) );
     39        $available = is_upload_space_available();
     40        remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_large' ) );
     41
     42        $this->assertTrue( $available );
     43    }
     44
     45    public function test_is_upload_space_available_space_used_is_less_then_allowed() {
     46        update_option( 'blog_upload_space', 350 );
     47
     48        add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
     49        $available = is_upload_space_available();
     50        remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
     51
     52        $this->assertTrue( $available );
     53    }
     54
     55    public function test_is_upload_space_available_space_used_is_more_than_allowed() {
     56        update_option( 'blog_upload_space', 350 );
     57
     58        add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_large' ) );
     59        $available = is_upload_space_available();
     60        remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_large' ) );
     61
     62        $this->assertFalse( $available );
     63    }
     64
     65    /**
     66     * More comprehensive testing a 0 condition is handled in the tests
     67     * for `get_space_allowed()`. We cover one scenario here.
     68     */
     69    public function test_is_upload_space_available_upload_space_0_defaults_to_100() {
     70        update_option( 'blog_upload_space', 0 );
     71
     72        add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
     73        $available = is_upload_space_available();
     74        remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
     75
     76        $this->assertFalse( $available );
     77    }
     78
     79    public function test_is_upload_space_available_upload_space_negative() {
     80        update_site_option( 'blog_upload_space', -1 );
     81
     82        add_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
     83        $available = is_upload_space_available();
     84        remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used_small' ) );
     85
     86        $this->assertFalse( $available );
     87    }
     88
     89    public function filter_space_used_large() {
     90        return 10000000;
     91    }
     92
     93    public function filter_space_used_small() {
     94        return 10;
     95    }
     96}
  • trunk/tests/phpunit/tests/multisite/msFilesRewriting.php

    r54864 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Tests specific to the ms_files_rewriting option in multisite.
     5 *
     6 * The ms-files group tag must be used for these tests to run as the constants
     7 * set in ms_upload_constants() conflict with a non ms-files configuration.
     8 *
     9 * @group ms-files
     10 * @group ms-required
     11 * @group multisite
     12 */
     13class Tests_Multisite_msFilesRewriting extends WP_UnitTestCase {
     14
     15    public function set_up() {
     16        parent::set_up();
     17        update_site_option( 'ms_files_rewriting', 1 );
     18        ms_upload_constants();
     19    }
     20
     21    public function test_switch_upload_dir() {
     22        $this->assertTrue( is_main_site() );
     23
     24        $site = get_current_site();
     25        $date = date_format( date_create( 'now' ), 'Y/m' );
     26
     27        $user_id  = self::factory()->user->create( array( 'role' => 'administrator' ) );
     28        $blog_id2 = self::factory()->blog->create( array( 'user_id' => $user_id ) );
     29        $info     = wp_upload_dir();
     30        $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] );
     31        $this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] );
     32        $this->assertSame( '/' . $date, $info['subdir'] );
     33        $this->assertFalse( $info['error'] );
     34
     35        switch_to_blog( $blog_id2 );
     36        $info2 = wp_upload_dir();
     37        $this->assertNotEquals( $info, $info2 );
     38        $this->assertSame( get_option( 'siteurl' ) . '/wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . $date, $info2['url'] );
     39        $this->assertSame( ABSPATH . 'wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . $date, $info2['path'] );
     40        $this->assertSame( '/' . $date, $info2['subdir'] );
     41        $this->assertFalse( $info2['error'] );
     42        restore_current_blog();
     43    }
    444
    545    /**
    6      * Tests specific to the ms_files_rewriting option in multisite.
    7      *
    8      * The ms-files group tag must be used for these tests to run as the constants
    9      * set in ms_upload_constants() conflict with a non ms-files configuration.
    10      *
    11      * @group ms-files
    12      * @group multisite
     46     * When a site is deleted with wpmu_delete_blog(), only the files associated with
     47     * that site should be removed. When wpmu_delete_blog() is run a second time, nothing
     48     * should change with upload directories.
    1349     */
    14     class Tests_Multisite_msFilesRewriting extends WP_UnitTestCase {
     50    public function test_upload_directories_after_multiple_wpmu_delete_blog_with_ms_files() {
     51        $filename = __FUNCTION__ . '.jpg';
     52        $contents = __FUNCTION__ . '_contents';
    1553
    16         public function set_up() {
    17             parent::set_up();
    18             update_site_option( 'ms_files_rewriting', 1 );
    19             ms_upload_constants();
    20         }
     54        // Upload a file to the main site on the network.
     55        $file1 = wp_upload_bits( $filename, null, $contents );
    2156
    22         public function test_switch_upload_dir() {
    23             $this->assertTrue( is_main_site() );
     57        $blog_id = self::factory()->blog->create();
    2458
    25             $site = get_current_site();
    26             $date = date_format( date_create( 'now' ), 'Y/m' );
     59        switch_to_blog( $blog_id );
     60        $file2 = wp_upload_bits( $filename, null, $contents );
     61        restore_current_blog();
    2762
    28             $user_id  = self::factory()->user->create( array( 'role' => 'administrator' ) );
    29             $blog_id2 = self::factory()->blog->create( array( 'user_id' => $user_id ) );
    30             $info     = wp_upload_dir();
    31             $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] );
    32             $this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] );
    33             $this->assertSame( '/' . $date, $info['subdir'] );
    34             $this->assertFalse( $info['error'] );
     63        wpmu_delete_blog( $blog_id, true );
    3564
    36             switch_to_blog( $blog_id2 );
    37             $info2 = wp_upload_dir();
    38             $this->assertNotEquals( $info, $info2 );
    39             $this->assertSame( get_option( 'siteurl' ) . '/wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . $date, $info2['url'] );
    40             $this->assertSame( ABSPATH . 'wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . $date, $info2['path'] );
    41             $this->assertSame( '/' . $date, $info2['subdir'] );
    42             $this->assertFalse( $info2['error'] );
    43             restore_current_blog();
    44         }
     65        // The file on the main site should still exist. The file on the deleted site should not.
     66        $this->assertFileExists( $file1['file'] );
     67        $this->assertFileDoesNotExist( $file2['file'] );
    4568
    46         /**
    47          * When a site is deleted with wpmu_delete_blog(), only the files associated with
    48          * that site should be removed. When wpmu_delete_blog() is run a second time, nothing
    49          * should change with upload directories.
    50          */
    51         public function test_upload_directories_after_multiple_wpmu_delete_blog_with_ms_files() {
    52             $filename = __FUNCTION__ . '.jpg';
    53             $contents = __FUNCTION__ . '_contents';
     69        wpmu_delete_blog( $blog_id, true );
    5470
    55             // Upload a file to the main site on the network.
    56             $file1 = wp_upload_bits( $filename, null, $contents );
     71        // The file on the main site should still exist. The file on the deleted site should not.
     72        $this->assertFileExists( $file1['file'] );
     73        $this->assertFileDoesNotExist( $file2['file'] );
    5774
    58             $blog_id = self::factory()->blog->create();
    59 
    60             switch_to_blog( $blog_id );
    61             $file2 = wp_upload_bits( $filename, null, $contents );
    62             restore_current_blog();
    63 
    64             wpmu_delete_blog( $blog_id, true );
    65 
    66             // The file on the main site should still exist. The file on the deleted site should not.
    67             $this->assertFileExists( $file1['file'] );
    68             $this->assertFileDoesNotExist( $file2['file'] );
    69 
    70             wpmu_delete_blog( $blog_id, true );
    71 
    72             // The file on the main site should still exist. The file on the deleted site should not.
    73             $this->assertFileExists( $file1['file'] );
    74             $this->assertFileDoesNotExist( $file2['file'] );
    75 
    76             unlink( $file1['file'] );
    77         }
     75        unlink( $file1['file'] );
    7876    }
    79 
    80 endif;
     77}
  • trunk/tests/phpunit/tests/multisite/network.php

    r59021 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    5     /**
    6      * Tests specific to networks in multisite.
    7      *
    8      * @group ms-network
    9      * @group multisite
    10      */
    11     class Tests_Multisite_Network extends WP_UnitTestCase {
    12         protected $plugin_hook_count = 0;
    13 
    14         protected static $different_network_id;
    15         protected static $different_site_ids = array();
    16 
    17         public function tear_down() {
    18             global $current_site;
    19             $current_site->id = 1;
    20             parent::tear_down();
    21         }
    22 
    23         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    24             self::$different_network_id = $factory->network->create(
    25                 array(
    26                     'domain' => 'wordpress.org',
    27                     'path'   => '/',
    28                 )
    29             );
    30 
    31             $sites = array(
    32                 array(
    33                     'domain'     => 'wordpress.org',
    34                     'path'       => '/',
    35                     'network_id' => self::$different_network_id,
    36                 ),
    37                 array(
    38                     'domain'     => 'wordpress.org',
    39                     'path'       => '/foo/',
    40                     'network_id' => self::$different_network_id,
    41                 ),
    42                 array(
    43                     'domain'     => 'wordpress.org',
    44                     'path'       => '/bar/',
    45                     'network_id' => self::$different_network_id,
    46                 ),
    47             );
    48 
    49             foreach ( $sites as $site ) {
    50                 self::$different_site_ids[] = $factory->blog->create( $site );
    51             }
    52         }
    53 
    54         public static function wpTearDownAfterClass() {
    55             global $wpdb;
    56 
    57             foreach ( self::$different_site_ids as $id ) {
    58                 wp_delete_site( $id );
    59             }
    60 
    61             $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$different_network_id ) );
    62             $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$different_network_id ) );
    63 
    64             wp_update_network_site_counts();
    65         }
    66 
    67         /**
    68          * By default, only one network exists and has a network ID of 1.
    69          */
    70         public function test_get_main_network_id_default() {
    71             $this->assertSame( 1, get_main_network_id() );
    72         }
    73 
    74         /**
    75          * If a second network is created, network ID 1 should still be returned
    76          * as the main network ID.
    77          */
    78         public function test_get_main_network_id_two_networks() {
    79             self::factory()->network->create();
    80 
    81             $this->assertSame( 1, get_main_network_id() );
    82         }
    83 
    84         /**
    85          * When the `$current_site` global is populated with another network, the
    86          * main network should still return as 1.
    87          */
    88         public function test_get_main_network_id_after_network_switch() {
    89             global $current_site;
    90 
    91             $id = self::factory()->network->create();
    92 
    93             $current_site->id = (int) $id;
    94 
    95             $this->assertSame( 1, get_main_network_id() );
    96         }
    97 
    98         /**
    99          * When the first network is removed, the next should return as the main
    100          * network ID.
    101          *
    102          * @todo In the future, we'll have a smarter way of deleting a network. For now,
    103          * fake the process with UPDATE queries.
    104          */
    105         public function test_get_main_network_id_after_network_delete() {
    106             global $wpdb, $current_site;
    107 
    108             $temp_id = self::$different_network_id + 1;
    109 
    110             $current_site->id = (int) self::$different_network_id;
    111             $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=%d WHERE id=1", $temp_id ) );
    112             $main_network_id = get_main_network_id();
    113             $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=1 WHERE id=%d", $temp_id ) );
    114 
    115             $this->assertSame( self::$different_network_id, $main_network_id );
    116         }
    117 
    118         public function test_get_main_network_id_filtered() {
    119             add_filter( 'get_main_network_id', array( $this, 'get_main_network_id' ) );
    120             $this->assertSame( 3, get_main_network_id() );
    121             remove_filter( 'get_main_network_id', array( $this, 'get_main_network_id' ) );
    122         }
    123 
    124         public function get_main_network_id() {
    125             return 3;
    126         }
    127 
    128         /**
    129          * Tests that the `WP_Network::$id` property is an integer.
    130          *
    131          * @ticket 37050
    132          *
    133          * @covers WP_Network::__get
    134          */
    135         public function test_wp_network_object_id_property_is_int() {
    136             $id = self::factory()->network->create();
    137 
    138             $network = WP_Network::get_instance( $id );
    139 
    140             $this->assertSame( (int) $id, $network->id );
    141         }
    142 
    143         /**
    144          * Tests that the `WP_Network::$id` property is stored as an integer.
    145          *
    146          * Uses reflection to access the private property.
    147          * Differs from using the public getter method, which casts to an integer.
    148          *
    149          * @ticket 62035
    150          *
    151          * @covers WP_Network::__construct
    152          */
    153         public function test_wp_network_object_id_property_stored_as_int() {
    154             $id = self::factory()->network->create();
    155 
    156             $network = WP_Network::get_instance( $id );
    157 
    158             $reflection = new ReflectionObject( $network );
    159             $property   = $reflection->getProperty( 'id' );
    160             $property->setAccessible( true );
    161 
    162             $this->assertSame( (int) $id, $property->getValue( $network ) );
    163         }
    164 
    165         /**
    166          * Tests that the `WP_Network::$blog_id` property is a string.
    167          *
    168          * @ticket 62035
    169          *
    170          * @covers WP_Network::__get
    171          */
    172         public function test_wp_network_object_blog_id_property_is_int() {
    173             $id = self::factory()->network->create();
    174 
    175             $network = WP_Network::get_instance( $id );
    176 
    177             $this->assertIsString( $network->blog_id );
    178         }
    179 
    180         /**
    181          * Tests that the `WP_Network::$blog_id` property is stored as a string.
    182          *
    183          * Uses reflection to access the private property.
    184          * Differs from using the public getter method, which casts to a string.
    185          *
    186          * @ticket 62035
    187          *
    188          * @covers WP_Network::__construct
    189          */
    190         public function test_wp_network_object_blog_id_property_stored_as_string() {
    191             $id = self::factory()->network->create();
    192 
    193             $network = WP_Network::get_instance( $id );
    194 
    195             $reflection = new ReflectionObject( $network );
    196             $property   = $reflection->getProperty( 'blog_id' );
    197             $property->setAccessible( true );
    198 
    199             $this->assertIsString( $property->getValue( $network ) );
    200         }
    201 
    202         /**
    203          * @ticket 22917
    204          */
    205         public function test_get_blog_count_no_filter_applied() {
    206             wp_update_network_counts();
    207             $site_count_start = get_blog_count();
    208 
    209             $site_ids = self::factory()->blog->create_many( 1 );
    210             $actual   = (int) get_blog_count(); // Count only updated when cron runs, so should be unchanged.
    211 
    212             foreach ( $site_ids as $site_id ) {
    213                 wp_delete_site( $site_id );
    214             }
    215             wp_update_network_counts();
    216 
    217             $this->assertSame( $site_count_start + 1, $actual );
    218         }
    219 
    220         /**
    221          * @ticket 22917
    222          */
    223         public function test_get_blog_count_enable_live_network_counts_false() {
    224             wp_update_network_counts();
    225             $site_count_start = get_blog_count();
    226 
    227             add_filter( 'enable_live_network_counts', '__return_false' );
    228             $site_ids = self::factory()->blog->create_many( 1 );
    229             $actual   = (int) get_blog_count(); // Count only updated when cron runs, so should be unchanged.
    230             remove_filter( 'enable_live_network_counts', '__return_false' );
    231 
    232             foreach ( $site_ids as $site_id ) {
    233                 wp_delete_site( $site_id );
    234             }
    235             wp_update_network_counts();
    236 
    237             $this->assertEquals( $site_count_start, $actual );
    238         }
    239 
    240         /**
    241          * @ticket 22917
    242          */
    243         public function test_get_blog_count_enabled_live_network_counts_true() {
    244             wp_update_network_counts();
    245             $site_count_start = get_blog_count();
    246 
    247             add_filter( 'enable_live_network_counts', '__return_true' );
    248             $site_ids = self::factory()->blog->create_many( 1 );
    249             $actual   = get_blog_count();
    250             remove_filter( 'enable_live_network_counts', '__return_true' );
    251 
    252             foreach ( $site_ids as $site_id ) {
    253                 wp_delete_site( $site_id );
    254             }
    255             wp_update_network_counts();
    256 
    257             $this->assertSame( $site_count_start + 1, $actual );
    258         }
    259 
    260         /**
    261          * @ticket 37865
    262          */
    263         public function test_get_blog_count_on_different_network() {
    264             wp_update_network_site_counts( self::$different_network_id );
    265 
    266             $site_count = get_blog_count( self::$different_network_id );
    267 
    268             $this->assertEquals( count( self::$different_site_ids ), $site_count );
    269         }
    270 
    271         public function test_active_network_plugins() {
    272             $path = 'hello.php';
    273 
    274             // Local activate, should be invisible for the network.
    275             activate_plugin( $path ); // Enable the plugin for the current site.
    276             $active_plugins = wp_get_active_network_plugins();
    277             $this->assertSame( array(), $active_plugins );
    278 
    279             add_action( 'deactivated_plugin', array( $this, 'helper_deactivate_hook' ) );
    280 
    281             // Activate the plugin sitewide.
    282             activate_plugin( $path, '', true ); // Enable the plugin for all sites in the network.
    283             $active_plugins = wp_get_active_network_plugins();
    284             $this->assertSame( array( WP_PLUGIN_DIR . '/hello.php' ), $active_plugins );
    285 
    286             // Deactivate the plugin.
    287             deactivate_plugins( $path );
    288             $active_plugins = wp_get_active_network_plugins();
    289             $this->assertSame( array(), $active_plugins );
    290 
    291             $this->assertSame( 1, $this->plugin_hook_count ); // Testing actions and silent mode.
    292 
    293             activate_plugin( $path, '', true ); // Enable the plugin for all sites in the network.
    294             deactivate_plugins( $path, true );  // Silent mode.
    295 
    296             $this->assertSame( 1, $this->plugin_hook_count ); // Testing actions and silent mode.
    297         }
    298 
    299         /**
    300          * @ticket 28651
    301          */
    302         public function test_duplicate_network_active_plugin() {
    303             $path = 'hello.php';
    304             $mock = new MockAction();
    305             add_action( 'activate_' . $path, array( $mock, 'action' ) );
    306 
    307             // Should activate on the first try.
    308             activate_plugin( $path, '', true ); // Enable the plugin for all sites in the network.
    309             $active_plugins = wp_get_active_network_plugins();
    310             $this->assertCount( 1, $active_plugins );
    311             $this->assertSame( 1, $mock->get_call_count() );
    312 
    313             // Should do nothing on the second try.
    314             activate_plugin( $path, '', true ); // Enable the plugin for all sites in the network.
    315             $active_plugins = wp_get_active_network_plugins();
    316             $this->assertCount( 1, $active_plugins );
    317             $this->assertSame( 1, $mock->get_call_count() );
    318 
    319             remove_action( 'activate_' . $path, array( $mock, 'action' ) );
    320         }
    321 
    322         public function test_is_plugin_active_for_network_true() {
    323             activate_plugin( 'hello.php', '', true );
    324             $this->assertTrue( is_plugin_active_for_network( 'hello.php' ) );
    325         }
    326 
    327         public function test_is_plugin_active_for_network_false() {
    328             deactivate_plugins( 'hello.php', false, true );
    329             $this->assertFalse( is_plugin_active_for_network( 'hello.php' ) );
    330         }
    331 
    332         public function helper_deactivate_hook() {
    333             ++$this->plugin_hook_count;
    334         }
    335 
    336         public function test_wp_schedule_update_network_counts() {
    337             $this->assertFalse( wp_next_scheduled( 'update_network_counts' ) );
    338 
    339             // We can't use wp_schedule_update_network_counts() because WP_INSTALLING is set.
    340             wp_schedule_event( time(), 'twicedaily', 'update_network_counts' );
    341 
    342             $this->assertIsInt( wp_next_scheduled( 'update_network_counts' ) );
    343         }
    344 
    345         /**
    346          * @expectedDeprecated get_dashboard_blog
    347          */
    348         public function test_get_dashboard_blog() {
    349             // If there is no dashboard blog set, current blog is used.
    350             $dashboard_blog = get_dashboard_blog();
    351             $this->assertEquals( 1, $dashboard_blog->blog_id );
    352 
    353             $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
    354             $blog_id = self::factory()->blog->create( array( 'user_id' => $user_id ) );
    355             $this->assertIsInt( $blog_id );
    356 
    357             // Set the dashboard blog to another one.
    358             update_site_option( 'dashboard_blog', $blog_id );
    359             $dashboard_blog = get_dashboard_blog();
    360             $this->assertEquals( $blog_id, $dashboard_blog->blog_id );
    361         }
    362 
    363         /**
    364          * @ticket 37528
    365          */
    366         public function test_wp_update_network_site_counts() {
    367             update_network_option( null, 'blog_count', 40 );
    368 
    369             $expected = get_sites(
    370                 array(
    371                     'network_id' => get_current_network_id(),
    372                     'spam'       => 0,
    373                     'deleted'    => 0,
    374                     'archived'   => 0,
    375                     'count'      => true,
    376                 )
    377             );
    378 
    379             wp_update_network_site_counts();
    380 
    381             $result = get_blog_count();
    382             $this->assertSame( $expected, $result );
    383         }
    384 
    385         /**
    386          * @ticket 37528
    387          */
    388         public function test_wp_update_network_site_counts_on_different_network() {
    389             update_network_option( self::$different_network_id, 'blog_count', 40 );
    390 
    391             wp_update_network_site_counts( self::$different_network_id );
    392 
    393             $result = get_blog_count( self::$different_network_id );
    394             $this->assertSame( 3, $result );
    395         }
    396 
    397         /**
    398          * @ticket 40349
    399          */
    400         public function test_wp_update_network_user_counts() {
    401             global $wpdb;
    402 
    403             update_network_option( null, 'user_count', 40 );
    404 
    405             $expected = (int) $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    406 
    407             wp_update_network_user_counts();
    408 
    409             $result = get_user_count();
    410             $this->assertSame( $expected, $result );
    411         }
    412 
    413         /**
    414          * @ticket 40349
    415          */
    416         public function test_wp_update_network_user_counts_on_different_network() {
    417             global $wpdb;
    418 
    419             update_network_option( self::$different_network_id, 'user_count', 40 );
    420 
    421             $expected = (int) $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    422 
    423             wp_update_network_user_counts( self::$different_network_id );
    424 
    425             $result = get_user_count( self::$different_network_id );
    426             $this->assertSame( $expected, $result );
    427         }
    428 
    429         /**
    430          * @ticket 40386
    431          */
    432         public function test_wp_update_network_counts() {
    433             delete_network_option( null, 'blog_count' );
    434             delete_network_option( null, 'user_count' );
    435 
    436             wp_update_network_counts();
    437 
    438             $site_count = (int) get_blog_count();
    439             $user_count = (int) get_user_count();
    440 
    441             $this->assertGreaterThan( 0, $site_count );
    442             $this->assertGreaterThan( 0, $user_count );
    443         }
    444 
    445         /**
    446          * @ticket 40386
    447          */
    448         public function test_wp_update_network_counts_on_different_network() {
    449             delete_network_option( self::$different_network_id, 'blog_count' );
    450             delete_network_option( self::$different_network_id, 'user_count' );
    451 
    452             wp_update_network_counts( self::$different_network_id );
    453 
    454             $site_count = (int) get_blog_count( self::$different_network_id );
    455             $user_count = (int) get_user_count( self::$different_network_id );
    456 
    457             $this->assertGreaterThan( 0, $site_count );
    458             $this->assertGreaterThan( 0, $user_count );
    459         }
    460 
    461         /**
    462          * Test the default behavior of upload_size_limit_filter.
    463          * If any default option is changed, the function returns the min value between the
    464          * parameter passed and the `fileupload_maxk` site option (1500Kb by default)
    465          *
    466          * @ticket 55926
    467          */
    468         public function test_upload_size_limit_filter() {
    469             $return = upload_size_limit_filter( 1499 * KB_IN_BYTES );
    470             $this->assertSame( 1499 * KB_IN_BYTES, $return );
    471             $return = upload_size_limit_filter( 1501 * KB_IN_BYTES );
    472             $this->assertSame( 1500 * KB_IN_BYTES, $return );
    473         }
    474 
    475         /**
    476          * Test if upload_size_limit_filter behaves as expected when the `fileupload_maxk` is 0 or an empty string.
    477          *
    478          * @ticket 55926
    479          * @dataProvider data_upload_size_limit_filter_empty_fileupload_maxk
    480          */
    481         public function test_upload_size_limit_filter_empty_fileupload_maxk( $callable_set_fileupload_maxk ) {
    482             add_filter( 'site_option_fileupload_maxk', $callable_set_fileupload_maxk );
    483             $return = upload_size_limit_filter( 1500 );
    484             $this->assertSame( 0, $return );
    485         }
    486 
    487         /**
    488          * @ticket 55926
    489          */
    490         public function data_upload_size_limit_filter_empty_fileupload_maxk() {
    491             return array(
    492                 array( '__return_zero' ),
    493                 array( '__return_empty_string' ),
    494             );
    495         }
    496 
    497         /**
    498          * When upload_space_check is enabled, the space allowed is also considered by `upload_size_limit_filter`.
    499          *
    500          * @ticket 55926
    501          */
    502         public function test_upload_size_limit_filter_when_upload_space_check_enabled() {
    503             add_filter( 'get_space_allowed', '__return_zero' );
    504             add_filter( 'site_option_upload_space_check_disabled', '__return_false' );
    505             $return = upload_size_limit_filter( 100 );
    506             $this->assertSame( 0, $return );
    507         }
    508 
    509         /**
    510          * @ticket 40489
    511          * @dataProvider data_wp_is_large_network
    512          */
    513         public function test_wp_is_large_network( $using, $count, $expected, $different_network ) {
    514             $network_id     = $different_network ? self::$different_network_id : null;
    515             $network_option = 'users' === $using ? 'user_count' : 'blog_count';
    516 
    517             update_network_option( $network_id, $network_option, $count );
    518 
    519             $result = wp_is_large_network( $using, $network_id );
    520             if ( $expected ) {
    521                 $this->assertTrue( $result );
    522             } else {
    523                 $this->assertFalse( $result );
    524             }
    525         }
    526 
    527         public function data_wp_is_large_network() {
    528             return array(
    529                 array( 'sites', 10000, false, false ),
    530                 array( 'sites', 10001, true, false ),
    531                 array( 'users', 10000, false, false ),
    532                 array( 'users', 10001, true, false ),
    533                 array( 'sites', 10000, false, true ),
    534                 array( 'sites', 10001, true, true ),
    535                 array( 'users', 10000, false, true ),
    536                 array( 'users', 10001, true, true ),
    537             );
    538         }
    539 
    540         /**
    541          * @ticket 40489
    542          * @dataProvider data_wp_is_large_network_filtered_by_component
    543          */
    544         public function test_wp_is_large_network_filtered_by_component( $using, $count, $expected, $different_network ) {
    545             $network_id     = $different_network ? self::$different_network_id : null;
    546             $network_option = 'users' === $using ? 'user_count' : 'blog_count';
    547 
    548             update_network_option( $network_id, $network_option, $count );
    549 
    550             add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10, 3 );
    551             $result = wp_is_large_network( $using, $network_id );
    552             remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10 );
    553 
    554             if ( $expected ) {
    555                 $this->assertTrue( $result );
    556             } else {
    557                 $this->assertFalse( $result );
    558             }
    559         }
    560 
    561         public function data_wp_is_large_network_filtered_by_component() {
    562             return array(
    563                 array( 'sites', 10000, false, false ),
    564                 array( 'sites', 10001, true, false ),
    565                 array( 'users', 1000, false, false ),
    566                 array( 'users', 1001, true, false ),
    567                 array( 'sites', 10000, false, true ),
    568                 array( 'sites', 10001, true, true ),
    569                 array( 'users', 1000, false, true ),
    570                 array( 'users', 1001, true, true ),
    571             );
    572         }
    573 
    574         public function filter_wp_is_large_network_for_users( $is_large_network, $using, $count ) {
    575             if ( 'users' === $using ) {
    576                 return $count > 1000;
    577             }
    578 
    579             return $is_large_network;
    580         }
    581 
    582         /**
    583          * @ticket 40489
    584          * @dataProvider data_wp_is_large_network_filtered_by_network
    585          */
    586         public function test_wp_is_large_network_filtered_by_network( $using, $count, $expected, $different_network ) {
    587             $network_id     = $different_network ? self::$different_network_id : null;
    588             $network_option = 'users' === $using ? 'user_count' : 'blog_count';
    589 
    590             update_network_option( $network_id, $network_option, $count );
    591 
    592             add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10, 4 );
    593             $result = wp_is_large_network( $using, $network_id );
    594             remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10 );
    595 
    596             if ( $expected ) {
    597                 $this->assertTrue( $result );
    598             } else {
    599                 $this->assertFalse( $result );
    600             }
    601         }
    602 
    603         public function data_wp_is_large_network_filtered_by_network() {
    604             return array(
    605                 array( 'sites', 10000, false, false ),
    606                 array( 'sites', 10001, true, false ),
    607                 array( 'users', 10000, false, false ),
    608                 array( 'users', 10001, true, false ),
    609                 array( 'sites', 1000, false, true ),
    610                 array( 'sites', 1001, true, true ),
    611                 array( 'users', 1000, false, true ),
    612                 array( 'users', 1001, true, true ),
    613             );
    614         }
    615 
    616         public function filter_wp_is_large_network_on_different_network( $is_large_network, $using, $count, $network_id ) {
    617             if ( $network_id === (int) self::$different_network_id ) {
    618                 return $count > 1000;
    619             }
    620 
    621             return $is_large_network;
    622         }
    623 
    624         /**
    625          * @ticket 38699
    626          */
    627         public function test_wpmu_create_blog_updates_correct_network_site_count() {
    628             global $wpdb;
    629 
    630             $original_count = get_blog_count( self::$different_network_id );
    631 
    632             $suppress = $wpdb->suppress_errors();
    633             $site_id  = wpmu_create_blog( 'example.org', '/', '', 1, array(), self::$different_network_id );
    634             $wpdb->suppress_errors( $suppress );
    635 
    636             $result = get_blog_count( self::$different_network_id );
    637 
    638             wpmu_delete_blog( $site_id, true );
    639 
    640             $this->assertSame( $original_count + 1, $result );
    641         }
    642 
    643         /**
    644          * @ticket 29684
    645          */
    646         public function test_network_blog_id_set() {
    647             $network = get_network( self::$different_network_id );
    648 
    649             $this->assertSame( (string) self::$different_site_ids[0], $network->blog_id );
    650         }
    651 
    652         /**
    653          * @ticket 42251
    654          */
    655         public function test_get_network_not_found_cache() {
    656             $new_network_id = $this->_get_next_network_id();
    657             $this->assertNull( get_network( $new_network_id ) );
    658 
    659             $num_queries = get_num_queries();
    660             $this->assertNull( get_network( $new_network_id ) );
    661             $this->assertSame( $num_queries, get_num_queries() );
    662         }
    663 
    664         /**
    665          * @ticket 42251
    666          */
    667         public function test_get_network_not_found_cache_clear() {
    668             $new_network_id = $this->_get_next_network_id();
    669             $this->assertNull( get_network( $new_network_id ) );
    670 
    671             $new_network = self::factory()->network->create_and_get();
    672 
    673             // Double-check we got the ID of the new network correct.
    674             $this->assertSame( $new_network_id, $new_network->id );
    675 
    676             // Verify that if we fetch the network now, it's no longer false.
    677             $fetched_network = get_network( $new_network_id );
    678             $this->assertInstanceOf( 'WP_Network', $fetched_network );
    679             $this->assertSame( $new_network_id, $fetched_network->id );
    680         }
    681 
    682         /**
    683          * Gets the ID of the site with the highest ID.
    684          * @return int
    685          */
    686         protected function _get_next_network_id() {
    687             global $wpdb;
    688             // Create an extra network, just to make sure we know the ID of the following one.
    689             static::factory()->network->create();
    690             return (int) $wpdb->get_var( 'SELECT id FROM ' . $wpdb->site . ' ORDER BY id DESC LIMIT 1' ) + 1;
    691         }
    692     }
    693 
    694 endif;
     3/**
     4 * Tests specific to networks in multisite.
     5 *
     6 * @group ms-network
     7 * @group ms-required
     8 * @group multisite
     9 */
     10class Tests_Multisite_Network extends WP_UnitTestCase {
     11
     12    protected $plugin_hook_count = 0;
     13
     14    protected static $different_network_id;
     15    protected static $different_site_ids = array();
     16
     17    public function tear_down() {
     18        global $current_site;
     19        $current_site->id = 1;
     20        parent::tear_down();
     21    }
     22
     23    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     24        self::$different_network_id = $factory->network->create(
     25            array(
     26                'domain' => 'wordpress.org',
     27                'path'   => '/',
     28            )
     29        );
     30
     31        $sites = array(
     32            array(
     33                'domain'     => 'wordpress.org',
     34                'path'       => '/',
     35                'network_id' => self::$different_network_id,
     36            ),
     37            array(
     38                'domain'     => 'wordpress.org',
     39                'path'       => '/foo/',
     40                'network_id' => self::$different_network_id,
     41            ),
     42            array(
     43                'domain'     => 'wordpress.org',
     44                'path'       => '/bar/',
     45                'network_id' => self::$different_network_id,
     46            ),
     47        );
     48
     49        foreach ( $sites as $site ) {
     50            self::$different_site_ids[] = $factory->blog->create( $site );
     51        }
     52    }
     53
     54    public static function wpTearDownAfterClass() {
     55        global $wpdb;
     56
     57        foreach ( self::$different_site_ids as $id ) {
     58            wp_delete_site( $id );
     59        }
     60
     61        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$different_network_id ) );
     62        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$different_network_id ) );
     63
     64        wp_update_network_site_counts();
     65    }
     66
     67    /**
     68     * By default, only one network exists and has a network ID of 1.
     69     */
     70    public function test_get_main_network_id_default() {
     71        $this->assertSame( 1, get_main_network_id() );
     72    }
     73
     74    /**
     75     * If a second network is created, network ID 1 should still be returned
     76     * as the main network ID.
     77     */
     78    public function test_get_main_network_id_two_networks() {
     79        self::factory()->network->create();
     80
     81        $this->assertSame( 1, get_main_network_id() );
     82    }
     83
     84    /**
     85     * When the `$current_site` global is populated with another network, the
     86     * main network should still return as 1.
     87     */
     88    public function test_get_main_network_id_after_network_switch() {
     89        global $current_site;
     90
     91        $id = self::factory()->network->create();
     92
     93        $current_site->id = (int) $id;
     94
     95        $this->assertSame( 1, get_main_network_id() );
     96    }
     97
     98    /**
     99     * When the first network is removed, the next should return as the main
     100     * network ID.
     101     *
     102     * @todo In the future, we'll have a smarter way of deleting a network. For now,
     103     * fake the process with UPDATE queries.
     104     */
     105    public function test_get_main_network_id_after_network_delete() {
     106        global $wpdb, $current_site;
     107
     108        $temp_id = self::$different_network_id + 1;
     109
     110        $current_site->id = (int) self::$different_network_id;
     111        $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=%d WHERE id=1", $temp_id ) );
     112        $main_network_id = get_main_network_id();
     113        $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=1 WHERE id=%d", $temp_id ) );
     114
     115        $this->assertSame( self::$different_network_id, $main_network_id );
     116    }
     117
     118    public function test_get_main_network_id_filtered() {
     119        add_filter( 'get_main_network_id', array( $this, 'get_main_network_id' ) );
     120        $this->assertSame( 3, get_main_network_id() );
     121        remove_filter( 'get_main_network_id', array( $this, 'get_main_network_id' ) );
     122    }
     123
     124    public function get_main_network_id() {
     125        return 3;
     126    }
     127
     128    /**
     129     * Tests that the `WP_Network::$id` property is an integer.
     130     *
     131     * @ticket 37050
     132     *
     133     * @covers WP_Network::__get
     134     */
     135    public function test_wp_network_object_id_property_is_int() {
     136        $id = self::factory()->network->create();
     137
     138        $network = WP_Network::get_instance( $id );
     139
     140        $this->assertSame( (int) $id, $network->id );
     141    }
     142
     143    /**
     144     * Tests that the `WP_Network::$id` property is stored as an integer.
     145     *
     146     * Uses reflection to access the private property.
     147     * Differs from using the public getter method, which casts to an integer.
     148     *
     149     * @ticket 62035
     150     *
     151     * @covers WP_Network::__construct
     152     */
     153    public function test_wp_network_object_id_property_stored_as_int() {
     154        $id = self::factory()->network->create();
     155
     156        $network = WP_Network::get_instance( $id );
     157
     158        $reflection = new ReflectionObject( $network );
     159        $property   = $reflection->getProperty( 'id' );
     160        $property->setAccessible( true );
     161
     162        $this->assertSame( (int) $id, $property->getValue( $network ) );
     163    }
     164
     165    /**
     166     * Tests that the `WP_Network::$blog_id` property is a string.
     167     *
     168     * @ticket 62035
     169     *
     170     * @covers WP_Network::__get
     171     */
     172    public function test_wp_network_object_blog_id_property_is_int() {
     173        $id = self::factory()->network->create();
     174
     175        $network = WP_Network::get_instance( $id );
     176
     177        $this->assertIsString( $network->blog_id );
     178    }
     179
     180    /**
     181     * Tests that the `WP_Network::$blog_id` property is stored as a string.
     182     *
     183     * Uses reflection to access the private property.
     184     * Differs from using the public getter method, which casts to a string.
     185     *
     186     * @ticket 62035
     187     *
     188     * @covers WP_Network::__construct
     189     */
     190    public function test_wp_network_object_blog_id_property_stored_as_string() {
     191        $id = self::factory()->network->create();
     192
     193        $network = WP_Network::get_instance( $id );
     194
     195        $reflection = new ReflectionObject( $network );
     196        $property   = $reflection->getProperty( 'blog_id' );
     197        $property->setAccessible( true );
     198
     199        $this->assertIsString( $property->getValue( $network ) );
     200    }
     201
     202    /**
     203     * @ticket 22917
     204     */
     205    public function test_get_blog_count_no_filter_applied() {
     206        wp_update_network_counts();
     207        $site_count_start = get_blog_count();
     208
     209        $site_ids = self::factory()->blog->create_many( 1 );
     210        $actual   = (int) get_blog_count(); // Count only updated when cron runs, so should be unchanged.
     211
     212        foreach ( $site_ids as $site_id ) {
     213            wp_delete_site( $site_id );
     214        }
     215        wp_update_network_counts();
     216
     217        $this->assertSame( $site_count_start + 1, $actual );
     218    }
     219
     220    /**
     221     * @ticket 22917
     222     */
     223    public function test_get_blog_count_enable_live_network_counts_false() {
     224        wp_update_network_counts();
     225        $site_count_start = get_blog_count();
     226
     227        add_filter( 'enable_live_network_counts', '__return_false' );
     228        $site_ids = self::factory()->blog->create_many( 1 );
     229        $actual   = (int) get_blog_count(); // Count only updated when cron runs, so should be unchanged.
     230        remove_filter( 'enable_live_network_counts', '__return_false' );
     231
     232        foreach ( $site_ids as $site_id ) {
     233            wp_delete_site( $site_id );
     234        }
     235        wp_update_network_counts();
     236
     237        $this->assertEquals( $site_count_start, $actual );
     238    }
     239
     240    /**
     241     * @ticket 22917
     242     */
     243    public function test_get_blog_count_enabled_live_network_counts_true() {
     244        wp_update_network_counts();
     245        $site_count_start = get_blog_count();
     246
     247        add_filter( 'enable_live_network_counts', '__return_true' );
     248        $site_ids = self::factory()->blog->create_many( 1 );
     249        $actual   = get_blog_count();
     250        remove_filter( 'enable_live_network_counts', '__return_true' );
     251
     252        foreach ( $site_ids as $site_id ) {
     253            wp_delete_site( $site_id );
     254        }
     255        wp_update_network_counts();
     256
     257        $this->assertSame( $site_count_start + 1, $actual );
     258    }
     259
     260    /**
     261     * @ticket 37865
     262     */
     263    public function test_get_blog_count_on_different_network() {
     264        wp_update_network_site_counts( self::$different_network_id );
     265
     266        $site_count = get_blog_count( self::$different_network_id );
     267
     268        $this->assertEquals( count( self::$different_site_ids ), $site_count );
     269    }
     270
     271    public function test_active_network_plugins() {
     272        $path = 'hello.php';
     273
     274        // Local activate, should be invisible for the network.
     275        activate_plugin( $path ); // Enable the plugin for the current site.
     276        $active_plugins = wp_get_active_network_plugins();
     277        $this->assertSame( array(), $active_plugins );
     278
     279        add_action( 'deactivated_plugin', array( $this, 'helper_deactivate_hook' ) );
     280
     281        // Activate the plugin sitewide.
     282        activate_plugin( $path, '', true ); // Enable the plugin for all sites in the network.
     283        $active_plugins = wp_get_active_network_plugins();
     284        $this->assertSame( array( WP_PLUGIN_DIR . '/hello.php' ), $active_plugins );
     285
     286        // Deactivate the plugin.
     287        deactivate_plugins( $path );
     288        $active_plugins = wp_get_active_network_plugins();
     289        $this->assertSame( array(), $active_plugins );
     290
     291        $this->assertSame( 1, $this->plugin_hook_count ); // Testing actions and silent mode.
     292
     293        activate_plugin( $path, '', true ); // Enable the plugin for all sites in the network.
     294        deactivate_plugins( $path, true );  // Silent mode.
     295
     296        $this->assertSame( 1, $this->plugin_hook_count ); // Testing actions and silent mode.
     297    }
     298
     299    /**
     300     * @ticket 28651
     301     */
     302    public function test_duplicate_network_active_plugin() {
     303        $path = 'hello.php';
     304        $mock = new MockAction();
     305        add_action( 'activate_' . $path, array( $mock, 'action' ) );
     306
     307        // Should activate on the first try.
     308        activate_plugin( $path, '', true ); // Enable the plugin for all sites in the network.
     309        $active_plugins = wp_get_active_network_plugins();
     310        $this->assertCount( 1, $active_plugins );
     311        $this->assertSame( 1, $mock->get_call_count() );
     312
     313        // Should do nothing on the second try.
     314        activate_plugin( $path, '', true ); // Enable the plugin for all sites in the network.
     315        $active_plugins = wp_get_active_network_plugins();
     316        $this->assertCount( 1, $active_plugins );
     317        $this->assertSame( 1, $mock->get_call_count() );
     318
     319        remove_action( 'activate_' . $path, array( $mock, 'action' ) );
     320    }
     321
     322    public function test_is_plugin_active_for_network_true() {
     323        activate_plugin( 'hello.php', '', true );
     324        $this->assertTrue( is_plugin_active_for_network( 'hello.php' ) );
     325    }
     326
     327    public function test_is_plugin_active_for_network_false() {
     328        deactivate_plugins( 'hello.php', false, true );
     329        $this->assertFalse( is_plugin_active_for_network( 'hello.php' ) );
     330    }
     331
     332    public function helper_deactivate_hook() {
     333        ++$this->plugin_hook_count;
     334    }
     335
     336    public function test_wp_schedule_update_network_counts() {
     337        $this->assertFalse( wp_next_scheduled( 'update_network_counts' ) );
     338
     339        // We can't use wp_schedule_update_network_counts() because WP_INSTALLING is set.
     340        wp_schedule_event( time(), 'twicedaily', 'update_network_counts' );
     341
     342        $this->assertIsInt( wp_next_scheduled( 'update_network_counts' ) );
     343    }
     344
     345    /**
     346     * @expectedDeprecated get_dashboard_blog
     347     */
     348    public function test_get_dashboard_blog() {
     349        // If there is no dashboard blog set, current blog is used.
     350        $dashboard_blog = get_dashboard_blog();
     351        $this->assertEquals( 1, $dashboard_blog->blog_id );
     352
     353        $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     354        $blog_id = self::factory()->blog->create( array( 'user_id' => $user_id ) );
     355        $this->assertIsInt( $blog_id );
     356
     357        // Set the dashboard blog to another one.
     358        update_site_option( 'dashboard_blog', $blog_id );
     359        $dashboard_blog = get_dashboard_blog();
     360        $this->assertEquals( $blog_id, $dashboard_blog->blog_id );
     361    }
     362
     363    /**
     364     * @ticket 37528
     365     */
     366    public function test_wp_update_network_site_counts() {
     367        update_network_option( null, 'blog_count', 40 );
     368
     369        $expected = get_sites(
     370            array(
     371                'network_id' => get_current_network_id(),
     372                'spam'       => 0,
     373                'deleted'    => 0,
     374                'archived'   => 0,
     375                'count'      => true,
     376            )
     377        );
     378
     379        wp_update_network_site_counts();
     380
     381        $result = get_blog_count();
     382        $this->assertSame( $expected, $result );
     383    }
     384
     385    /**
     386     * @ticket 37528
     387     */
     388    public function test_wp_update_network_site_counts_on_different_network() {
     389        update_network_option( self::$different_network_id, 'blog_count', 40 );
     390
     391        wp_update_network_site_counts( self::$different_network_id );
     392
     393        $result = get_blog_count( self::$different_network_id );
     394        $this->assertSame( 3, $result );
     395    }
     396
     397    /**
     398     * @ticket 40349
     399     */
     400    public function test_wp_update_network_user_counts() {
     401        global $wpdb;
     402
     403        update_network_option( null, 'user_count', 40 );
     404
     405        $expected = (int) $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
     406
     407        wp_update_network_user_counts();
     408
     409        $result = get_user_count();
     410        $this->assertSame( $expected, $result );
     411    }
     412
     413    /**
     414     * @ticket 40349
     415     */
     416    public function test_wp_update_network_user_counts_on_different_network() {
     417        global $wpdb;
     418
     419        update_network_option( self::$different_network_id, 'user_count', 40 );
     420
     421        $expected = (int) $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
     422
     423        wp_update_network_user_counts( self::$different_network_id );
     424
     425        $result = get_user_count( self::$different_network_id );
     426        $this->assertSame( $expected, $result );
     427    }
     428
     429    /**
     430     * @ticket 40386
     431     */
     432    public function test_wp_update_network_counts() {
     433        delete_network_option( null, 'blog_count' );
     434        delete_network_option( null, 'user_count' );
     435
     436        wp_update_network_counts();
     437
     438        $site_count = (int) get_blog_count();
     439        $user_count = (int) get_user_count();
     440
     441        $this->assertGreaterThan( 0, $site_count );
     442        $this->assertGreaterThan( 0, $user_count );
     443    }
     444
     445    /**
     446     * @ticket 40386
     447     */
     448    public function test_wp_update_network_counts_on_different_network() {
     449        delete_network_option( self::$different_network_id, 'blog_count' );
     450        delete_network_option( self::$different_network_id, 'user_count' );
     451
     452        wp_update_network_counts( self::$different_network_id );
     453
     454        $site_count = (int) get_blog_count( self::$different_network_id );
     455        $user_count = (int) get_user_count( self::$different_network_id );
     456
     457        $this->assertGreaterThan( 0, $site_count );
     458        $this->assertGreaterThan( 0, $user_count );
     459    }
     460
     461    /**
     462     * Test the default behavior of upload_size_limit_filter.
     463     * If any default option is changed, the function returns the min value between the
     464     * parameter passed and the `fileupload_maxk` site option (1500Kb by default)
     465     *
     466     * @ticket 55926
     467     */
     468    public function test_upload_size_limit_filter() {
     469        $return = upload_size_limit_filter( 1499 * KB_IN_BYTES );
     470        $this->assertSame( 1499 * KB_IN_BYTES, $return );
     471        $return = upload_size_limit_filter( 1501 * KB_IN_BYTES );
     472        $this->assertSame( 1500 * KB_IN_BYTES, $return );
     473    }
     474
     475    /**
     476     * Test if upload_size_limit_filter behaves as expected when the `fileupload_maxk` is 0 or an empty string.
     477     *
     478     * @ticket 55926
     479     * @dataProvider data_upload_size_limit_filter_empty_fileupload_maxk
     480     */
     481    public function test_upload_size_limit_filter_empty_fileupload_maxk( $callable_set_fileupload_maxk ) {
     482        add_filter( 'site_option_fileupload_maxk', $callable_set_fileupload_maxk );
     483        $return = upload_size_limit_filter( 1500 );
     484        $this->assertSame( 0, $return );
     485    }
     486
     487    /**
     488     * @ticket 55926
     489     */
     490    public function data_upload_size_limit_filter_empty_fileupload_maxk() {
     491        return array(
     492            array( '__return_zero' ),
     493            array( '__return_empty_string' ),
     494        );
     495    }
     496
     497    /**
     498     * When upload_space_check is enabled, the space allowed is also considered by `upload_size_limit_filter`.
     499     *
     500     * @ticket 55926
     501     */
     502    public function test_upload_size_limit_filter_when_upload_space_check_enabled() {
     503        add_filter( 'get_space_allowed', '__return_zero' );
     504        add_filter( 'site_option_upload_space_check_disabled', '__return_false' );
     505        $return = upload_size_limit_filter( 100 );
     506        $this->assertSame( 0, $return );
     507    }
     508
     509    /**
     510     * @ticket 40489
     511     * @dataProvider data_wp_is_large_network
     512     */
     513    public function test_wp_is_large_network( $using, $count, $expected, $different_network ) {
     514        $network_id     = $different_network ? self::$different_network_id : null;
     515        $network_option = 'users' === $using ? 'user_count' : 'blog_count';
     516
     517        update_network_option( $network_id, $network_option, $count );
     518
     519        $result = wp_is_large_network( $using, $network_id );
     520        if ( $expected ) {
     521            $this->assertTrue( $result );
     522        } else {
     523            $this->assertFalse( $result );
     524        }
     525    }
     526
     527    public function data_wp_is_large_network() {
     528        return array(
     529            array( 'sites', 10000, false, false ),
     530            array( 'sites', 10001, true, false ),
     531            array( 'users', 10000, false, false ),
     532            array( 'users', 10001, true, false ),
     533            array( 'sites', 10000, false, true ),
     534            array( 'sites', 10001, true, true ),
     535            array( 'users', 10000, false, true ),
     536            array( 'users', 10001, true, true ),
     537        );
     538    }
     539
     540    /**
     541     * @ticket 40489
     542     * @dataProvider data_wp_is_large_network_filtered_by_component
     543     */
     544    public function test_wp_is_large_network_filtered_by_component( $using, $count, $expected, $different_network ) {
     545        $network_id     = $different_network ? self::$different_network_id : null;
     546        $network_option = 'users' === $using ? 'user_count' : 'blog_count';
     547
     548        update_network_option( $network_id, $network_option, $count );
     549
     550        add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10, 3 );
     551        $result = wp_is_large_network( $using, $network_id );
     552        remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10 );
     553
     554        if ( $expected ) {
     555            $this->assertTrue( $result );
     556        } else {
     557            $this->assertFalse( $result );
     558        }
     559    }
     560
     561    public function data_wp_is_large_network_filtered_by_component() {
     562        return array(
     563            array( 'sites', 10000, false, false ),
     564            array( 'sites', 10001, true, false ),
     565            array( 'users', 1000, false, false ),
     566            array( 'users', 1001, true, false ),
     567            array( 'sites', 10000, false, true ),
     568            array( 'sites', 10001, true, true ),
     569            array( 'users', 1000, false, true ),
     570            array( 'users', 1001, true, true ),
     571        );
     572    }
     573
     574    public function filter_wp_is_large_network_for_users( $is_large_network, $using, $count ) {
     575        if ( 'users' === $using ) {
     576            return $count > 1000;
     577        }
     578
     579        return $is_large_network;
     580    }
     581
     582    /**
     583     * @ticket 40489
     584     * @dataProvider data_wp_is_large_network_filtered_by_network
     585     */
     586    public function test_wp_is_large_network_filtered_by_network( $using, $count, $expected, $different_network ) {
     587        $network_id     = $different_network ? self::$different_network_id : null;
     588        $network_option = 'users' === $using ? 'user_count' : 'blog_count';
     589
     590        update_network_option( $network_id, $network_option, $count );
     591
     592        add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10, 4 );
     593        $result = wp_is_large_network( $using, $network_id );
     594        remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10 );
     595
     596        if ( $expected ) {
     597            $this->assertTrue( $result );
     598        } else {
     599            $this->assertFalse( $result );
     600        }
     601    }
     602
     603    public function data_wp_is_large_network_filtered_by_network() {
     604        return array(
     605            array( 'sites', 10000, false, false ),
     606            array( 'sites', 10001, true, false ),
     607            array( 'users', 10000, false, false ),
     608            array( 'users', 10001, true, false ),
     609            array( 'sites', 1000, false, true ),
     610            array( 'sites', 1001, true, true ),
     611            array( 'users', 1000, false, true ),
     612            array( 'users', 1001, true, true ),
     613        );
     614    }
     615
     616    public function filter_wp_is_large_network_on_different_network( $is_large_network, $using, $count, $network_id ) {
     617        if ( $network_id === (int) self::$different_network_id ) {
     618            return $count > 1000;
     619        }
     620
     621        return $is_large_network;
     622    }
     623
     624    /**
     625     * @ticket 38699
     626     */
     627    public function test_wpmu_create_blog_updates_correct_network_site_count() {
     628        global $wpdb;
     629
     630        $original_count = get_blog_count( self::$different_network_id );
     631
     632        $suppress = $wpdb->suppress_errors();
     633        $site_id  = wpmu_create_blog( 'example.org', '/', '', 1, array(), self::$different_network_id );
     634        $wpdb->suppress_errors( $suppress );
     635
     636        $result = get_blog_count( self::$different_network_id );
     637
     638        wpmu_delete_blog( $site_id, true );
     639
     640        $this->assertSame( $original_count + 1, $result );
     641    }
     642
     643    /**
     644     * @ticket 29684
     645     */
     646    public function test_network_blog_id_set() {
     647        $network = get_network( self::$different_network_id );
     648
     649        $this->assertSame( (string) self::$different_site_ids[0], $network->blog_id );
     650    }
     651
     652    /**
     653     * @ticket 42251
     654     */
     655    public function test_get_network_not_found_cache() {
     656        $new_network_id = $this->_get_next_network_id();
     657        $this->assertNull( get_network( $new_network_id ) );
     658
     659        $num_queries = get_num_queries();
     660        $this->assertNull( get_network( $new_network_id ) );
     661        $this->assertSame( $num_queries, get_num_queries() );
     662    }
     663
     664    /**
     665     * @ticket 42251
     666     */
     667    public function test_get_network_not_found_cache_clear() {
     668        $new_network_id = $this->_get_next_network_id();
     669        $this->assertNull( get_network( $new_network_id ) );
     670
     671        $new_network = self::factory()->network->create_and_get();
     672
     673        // Double-check we got the ID of the new network correct.
     674        $this->assertSame( $new_network_id, $new_network->id );
     675
     676        // Verify that if we fetch the network now, it's no longer false.
     677        $fetched_network = get_network( $new_network_id );
     678        $this->assertInstanceOf( 'WP_Network', $fetched_network );
     679        $this->assertSame( $new_network_id, $fetched_network->id );
     680    }
     681
     682    /**
     683     * Gets the ID of the site with the highest ID.
     684     * @return int
     685     */
     686    protected function _get_next_network_id() {
     687        global $wpdb;
     688        // Create an extra network, just to make sure we know the ID of the following one.
     689        static::factory()->network->create();
     690        return (int) $wpdb->get_var( 'SELECT id FROM ' . $wpdb->site . ' ORDER BY id DESC LIMIT 1' ) + 1;
     691    }
     692}
  • trunk/tests/phpunit/tests/multisite/site.php

    r58235 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    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 $site_status_hooks       = array();
    14         protected $wp_initialize_site_args = array();
    15         protected $wp_initialize_site_meta = array();
    16         protected static $network_ids;
    17         protected static $site_ids;
    18         protected static $uninitialized_site_id;
    19 
    20         public function set_up() {
    21             global $wpdb;
    22             parent::set_up();
    23             $this->suppress = $wpdb->suppress_errors();
     3/**
     4 * Tests specific to sites in multisite.
     5 *
     6 * @group ms-required
     7 * @group ms-site
     8 * @group multisite
     9 */
     10class Tests_Multisite_Site extends WP_UnitTestCase {
     11    protected $suppress                = false;
     12    protected $site_status_hooks       = array();
     13    protected $wp_initialize_site_args = array();
     14    protected $wp_initialize_site_meta = array();
     15    protected static $network_ids;
     16    protected static $site_ids;
     17    protected static $uninitialized_site_id;
     18
     19    public function set_up() {
     20        global $wpdb;
     21        parent::set_up();
     22        $this->suppress = $wpdb->suppress_errors();
     23    }
     24
     25    public function tear_down() {
     26        global $wpdb;
     27        $wpdb->suppress_errors( $this->suppress );
     28        parent::tear_down();
     29    }
     30
     31    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     32        self::$network_ids = array(
     33            'make.wordpress.org/' => array(
     34                'domain' => 'make.wordpress.org',
     35                'path'   => '/',
     36            ),
     37        );
     38
     39        foreach ( self::$network_ids as &$id ) {
     40            $id = $factory->network->create( $id );
    2441        }
    25 
    26         public function tear_down() {
    27             global $wpdb;
    28             $wpdb->suppress_errors( $this->suppress );
    29             parent::tear_down();
     42        unset( $id );
     43
     44        self::$site_ids = array(
     45            'make.wordpress.org/'     => array(
     46                'domain'     => 'make.wordpress.org',
     47                'path'       => '/',
     48                'network_id' => self::$network_ids['make.wordpress.org/'],
     49            ),
     50            'make.wordpress.org/foo/' => array(
     51                'domain'     => 'make.wordpress.org',
     52                'path'       => '/foo/',
     53                'network_id' => self::$network_ids['make.wordpress.org/'],
     54            ),
     55        );
     56
     57        foreach ( self::$site_ids as &$id ) {
     58            $id = $factory->blog->create( $id );
    3059        }
    31 
    32         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    33             self::$network_ids = array(
    34                 'make.wordpress.org/' => array(
    35                     'domain' => 'make.wordpress.org',
    36                     'path'   => '/',
    37                 ),
    38             );
    39 
    40             foreach ( self::$network_ids as &$id ) {
    41                 $id = $factory->network->create( $id );
    42             }
    43             unset( $id );
    44 
    45             self::$site_ids = array(
    46                 'make.wordpress.org/'     => array(
    47                     'domain'     => 'make.wordpress.org',
    48                     'path'       => '/',
    49                     'network_id' => self::$network_ids['make.wordpress.org/'],
    50                 ),
    51                 'make.wordpress.org/foo/' => array(
    52                     'domain'     => 'make.wordpress.org',
    53                     'path'       => '/foo/',
    54                     'network_id' => self::$network_ids['make.wordpress.org/'],
    55                 ),
    56             );
    57 
    58             foreach ( self::$site_ids as &$id ) {
    59                 $id = $factory->blog->create( $id );
    60             }
    61             unset( $id );
    62 
    63             remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
    64             self::$uninitialized_site_id = wp_insert_site(
    65                 array(
    66                     'domain'     => 'uninitialized.org',
    67                     'path'       => '/',
    68                     'network_id' => self::$network_ids['make.wordpress.org/'],
    69                 )
    70             );
    71             add_action( 'wp_initialize_site', 'wp_initialize_site', 10, 2 );
     60        unset( $id );
     61
     62        remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
     63        self::$uninitialized_site_id = wp_insert_site(
     64            array(
     65                'domain'     => 'uninitialized.org',
     66                'path'       => '/',
     67                'network_id' => self::$network_ids['make.wordpress.org/'],
     68            )
     69        );
     70        add_action( 'wp_initialize_site', 'wp_initialize_site', 10, 2 );
     71    }
     72
     73    public static function wpTearDownAfterClass() {
     74        global $wpdb;
     75
     76        remove_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10 );
     77        wp_delete_site( self::$uninitialized_site_id );
     78        add_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10, 1 );
     79
     80        foreach ( self::$site_ids as $id ) {
     81            wp_delete_site( $id );
    7282        }
    7383
    74         public static function wpTearDownAfterClass() {
    75             global $wpdb;
    76 
    77             remove_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10 );
    78             wp_delete_site( self::$uninitialized_site_id );
    79             add_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10, 1 );
    80 
    81             foreach ( self::$site_ids as $id ) {
    82                 wp_delete_site( $id );
    83             }
    84 
    85             foreach ( self::$network_ids as $id ) {
    86                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    87                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     84        foreach ( self::$network_ids as $id ) {
     85            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     86            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     87        }
     88    }
     89
     90    public function test_switch_restore_blog() {
     91        global $_wp_switched_stack, $wpdb;
     92
     93        $this->assertSame( array(), $_wp_switched_stack );
     94        $this->assertFalse( ms_is_switched() );
     95        $current_blog_id = get_current_blog_id();
     96        $this->assertIsInt( $current_blog_id );
     97
     98        wp_cache_set( 'switch-test', $current_blog_id, 'switch-test' );
     99        $this->assertSame( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     100
     101        $blog_id = self::factory()->blog->create();
     102
     103        $cap_key = wp_get_current_user()->cap_key;
     104        switch_to_blog( $blog_id );
     105        $this->assertNotEquals( $cap_key, wp_get_current_user()->cap_key );
     106        $this->assertSame( array( $current_blog_id ), $_wp_switched_stack );
     107        $this->assertTrue( ms_is_switched() );
     108        $this->assertSame( $blog_id, $wpdb->blogid );
     109        $this->assertFalse( wp_cache_get( 'switch-test', 'switch-test' ) );
     110        wp_cache_set( 'switch-test', $blog_id, 'switch-test' );
     111        $this->assertSame( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     112
     113        switch_to_blog( $blog_id );
     114        $this->assertSame( array( $current_blog_id, $blog_id ), $_wp_switched_stack );
     115        $this->assertTrue( ms_is_switched() );
     116        $this->assertSame( $blog_id, $wpdb->blogid );
     117        $this->assertSame( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     118
     119        restore_current_blog();
     120        $this->assertSame( array( $current_blog_id ), $_wp_switched_stack );
     121        $this->assertTrue( ms_is_switched() );
     122        $this->assertSame( $blog_id, $wpdb->blogid );
     123        $this->assertSame( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     124
     125        restore_current_blog();
     126        $this->assertSame( $cap_key, wp_get_current_user()->cap_key );
     127        $this->assertSame( $current_blog_id, get_current_blog_id() );
     128        $this->assertSame( array(), $_wp_switched_stack );
     129        $this->assertFalse( ms_is_switched() );
     130        $this->assertSame( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
     131
     132        $this->assertFalse( restore_current_blog() );
     133    }
     134
     135    /**
     136     * Test the cache keys and database tables setup through the creation of a site.
     137     */
     138    public function test_created_site_details() {
     139        global $wpdb;
     140
     141        $blog_id = self::factory()->blog->create();
     142
     143        $this->assertIsInt( $blog_id );
     144        $prefix = $wpdb->get_blog_prefix( $blog_id );
     145
     146        // $get_all = false, only retrieve details from the blogs table.
     147        $details = get_blog_details( $blog_id, false );
     148
     149        // Combine domain and path for a site specific cache key.
     150        $key = md5( $details->domain . $details->path );
     151
     152        $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     153
     154        // get_blogaddress_by_name().
     155        $this->assertSame( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
     156
     157        // These are empty until get_blog_details() is called with $get_all = true.
     158        $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
     159        $this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
     160
     161        // $get_all = true, populate the full blog-details cache and the blog slug lookup cache.
     162        $details = get_blog_details( $blog_id, true );
     163        $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
     164        $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
     165
     166        // Check existence of each database table for the created site.
     167        foreach ( $wpdb->tables( 'blog', false ) as $table ) {
     168            $suppress = $wpdb->suppress_errors();
     169
     170            // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
     171            $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
     172
     173            $wpdb->suppress_errors( $suppress );
     174
     175            // The table should exist.
     176            $this->assertNotEmpty( $table_fields );
     177
     178            // And the table should not be empty, unless commentmeta, termmeta, or links.
     179            // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
     180            $result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
     181
     182            if ( 'commentmeta' === $table || 'termmeta' === $table || 'links' === $table ) {
     183                $this->assertEmpty( $result );
     184            } else {
     185                $this->assertNotEmpty( $result );
    88186            }
    89187        }
    90188
    91         public function test_switch_restore_blog() {
    92             global $_wp_switched_stack, $wpdb;
    93 
    94             $this->assertSame( array(), $_wp_switched_stack );
    95             $this->assertFalse( ms_is_switched() );
    96             $current_blog_id = get_current_blog_id();
    97             $this->assertIsInt( $current_blog_id );
    98 
    99             wp_cache_set( 'switch-test', $current_blog_id, 'switch-test' );
    100             $this->assertSame( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    101 
    102             $blog_id = self::factory()->blog->create();
    103 
    104             $cap_key = wp_get_current_user()->cap_key;
    105             switch_to_blog( $blog_id );
    106             $this->assertNotEquals( $cap_key, wp_get_current_user()->cap_key );
    107             $this->assertSame( array( $current_blog_id ), $_wp_switched_stack );
    108             $this->assertTrue( ms_is_switched() );
    109             $this->assertSame( $blog_id, $wpdb->blogid );
    110             $this->assertFalse( wp_cache_get( 'switch-test', 'switch-test' ) );
    111             wp_cache_set( 'switch-test', $blog_id, 'switch-test' );
    112             $this->assertSame( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    113 
    114             switch_to_blog( $blog_id );
    115             $this->assertSame( array( $current_blog_id, $blog_id ), $_wp_switched_stack );
    116             $this->assertTrue( ms_is_switched() );
    117             $this->assertSame( $blog_id, $wpdb->blogid );
    118             $this->assertSame( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    119 
    120             restore_current_blog();
    121             $this->assertSame( array( $current_blog_id ), $_wp_switched_stack );
    122             $this->assertTrue( ms_is_switched() );
    123             $this->assertSame( $blog_id, $wpdb->blogid );
    124             $this->assertSame( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    125 
    126             restore_current_blog();
    127             $this->assertSame( $cap_key, wp_get_current_user()->cap_key );
    128             $this->assertSame( $current_blog_id, get_current_blog_id() );
    129             $this->assertSame( array(), $_wp_switched_stack );
    130             $this->assertFalse( ms_is_switched() );
    131             $this->assertSame( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    132 
    133             $this->assertFalse( restore_current_blog() );
     189        // Update the blog count cache to use get_blog_count().
     190        wp_update_network_counts();
     191        $this->assertSame( 2, (int) get_blog_count() );
     192    }
     193
     194    public function test_site_caches_should_invalidate_when_invalidation_is_not_suspended() {
     195        $site_id = self::factory()->blog->create();
     196
     197        $details = get_site( $site_id );
     198
     199        $suspend = wp_suspend_cache_invalidation( false );
     200        update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
     201        $new_details = get_site( $site_id );
     202        wp_suspend_cache_invalidation( $suspend );
     203
     204        $this->assertNotEquals( $details->path, $new_details->path );
     205    }
     206
     207    public function test_site_caches_should_not_invalidate_when_invalidation_is_suspended() {
     208        $site_id = self::factory()->blog->create();
     209
     210        $details = get_site( $site_id );
     211
     212        $suspend = wp_suspend_cache_invalidation();
     213        update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
     214        $new_details = get_site( $site_id );
     215        wp_suspend_cache_invalidation( $suspend );
     216
     217        $this->assertSame( $details->path, $new_details->path );
     218    }
     219
     220    /**
     221     * When a site is flagged as 'deleted', its data should be cleared from cache.
     222     */
     223    public function test_data_in_cache_after_wpmu_delete_blog_drop_false() {
     224        $blog_id = self::factory()->blog->create();
     225
     226        $details = get_blog_details( $blog_id, false );
     227        $key     = md5( $details->domain . $details->path );
     228
     229        // Delete the site without forcing a table drop.
     230        wpmu_delete_blog( $blog_id, false );
     231
     232        $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
     233        $this->assertFalse( wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     234        $this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
     235        $this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
     236    }
     237
     238    /**
     239     * When a site is flagged as 'deleted', its data should remain in the database.
     240     */
     241    public function test_data_in_tables_after_wpmu_delete_blog_drop_false() {
     242        global $wpdb;
     243
     244        $blog_id = self::factory()->blog->create();
     245
     246        // Delete the site without forcing a table drop.
     247        wpmu_delete_blog( $blog_id, false );
     248
     249        $prefix = $wpdb->get_blog_prefix( $blog_id );
     250        foreach ( $wpdb->tables( 'blog', false ) as $table ) {
     251            $suppress = $wpdb->suppress_errors();
     252
     253            // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
     254            $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
     255
     256            $wpdb->suppress_errors( $suppress );
     257            $this->assertNotEmpty( $table_fields, $prefix . $table );
    134258        }
    135 
    136         /**
    137          * Test the cache keys and database tables setup through the creation of a site.
    138          */
    139         public function test_created_site_details() {
    140             global $wpdb;
    141 
    142             $blog_id = self::factory()->blog->create();
    143 
    144             $this->assertIsInt( $blog_id );
    145             $prefix = $wpdb->get_blog_prefix( $blog_id );
    146 
    147             // $get_all = false, only retrieve details from the blogs table.
    148             $details = get_blog_details( $blog_id, false );
    149 
    150             // Combine domain and path for a site specific cache key.
    151             $key = md5( $details->domain . $details->path );
    152 
    153             $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    154 
    155             // get_blogaddress_by_name().
    156             $this->assertSame( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
    157 
    158             // These are empty until get_blog_details() is called with $get_all = true.
    159             $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
    160             $this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
    161 
    162             // $get_all = true, populate the full blog-details cache and the blog slug lookup cache.
    163             $details = get_blog_details( $blog_id, true );
    164             $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
    165             $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
    166 
    167             // Check existence of each database table for the created site.
    168             foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    169                 $suppress = $wpdb->suppress_errors();
    170 
    171                 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    172                 $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    173 
    174                 $wpdb->suppress_errors( $suppress );
    175 
    176                 // The table should exist.
    177                 $this->assertNotEmpty( $table_fields );
    178 
    179                 // And the table should not be empty, unless commentmeta, termmeta, or links.
    180                 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    181                 $result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
    182 
    183                 if ( 'commentmeta' === $table || 'termmeta' === $table || 'links' === $table ) {
    184                     $this->assertEmpty( $result );
    185                 } else {
    186                     $this->assertNotEmpty( $result );
    187                 }
    188             }
    189 
    190             // Update the blog count cache to use get_blog_count().
    191             wp_update_network_counts();
    192             $this->assertSame( 2, (int) get_blog_count() );
     259    }
     260
     261    /**
     262     * When a site is fully deleted, its data should be cleared from cache.
     263     */
     264    public function test_data_in_cache_after_wpmu_delete_blog_drop_true() {
     265        $blog_id = self::factory()->blog->create();
     266
     267        $details = get_blog_details( $blog_id, false );
     268        $key     = md5( $details->domain . $details->path );
     269
     270        // Delete the site and force a table drop.
     271        wpmu_delete_blog( $blog_id, true );
     272
     273        $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
     274        $this->assertFalse( wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     275        $this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
     276        $this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
     277    }
     278
     279    /**
     280     * When a site is fully deleted, its data should be removed from the database.
     281     */
     282    public function test_data_in_tables_after_wpmu_delete_blog_drop_true() {
     283        global $wpdb;
     284
     285        $blog_id = self::factory()->blog->create();
     286
     287        // Delete the site and force a table drop.
     288        wpmu_delete_blog( $blog_id, true );
     289
     290        $prefix = $wpdb->get_blog_prefix( $blog_id );
     291        foreach ( $wpdb->tables( 'blog', false ) as $table ) {
     292            $suppress = $wpdb->suppress_errors();
     293
     294            // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
     295            $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
     296
     297            $wpdb->suppress_errors( $suppress );
     298            $this->assertEmpty( $table_fields );
    193299        }
    194 
    195         public function test_site_caches_should_invalidate_when_invalidation_is_not_suspended() {
    196             $site_id = self::factory()->blog->create();
    197 
    198             $details = get_site( $site_id );
    199 
    200             $suspend = wp_suspend_cache_invalidation( false );
    201             update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
    202             $new_details = get_site( $site_id );
    203             wp_suspend_cache_invalidation( $suspend );
    204 
    205             $this->assertNotEquals( $details->path, $new_details->path );
     300    }
     301
     302    /**
     303     * When the main site of a network is fully deleted, its data should be cleared from cache.
     304     */
     305    public function test_data_in_cache_after_wpmu_delete_blog_main_site_drop_true() {
     306        $blog_id = 1; // The main site in our test suite has an ID of 1.
     307
     308        $details = get_blog_details( $blog_id, false );
     309        $key     = md5( $details->domain . $details->path );
     310
     311        // Delete the site and force a table drop.
     312        wpmu_delete_blog( $blog_id, true );
     313
     314        $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
     315        $this->assertFalse( wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     316        $this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
     317        $this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
     318    }
     319
     320    /**
     321     * When the main site of a network is fully deleted, its data should remain in the database.
     322     */
     323    public function test_data_in_tables_after_wpmu_delete_blog_main_site_drop_true() {
     324        global $wpdb;
     325
     326        $blog_id = 1; // The main site in our test suite has an ID of 1.
     327
     328        // Delete the site and force a table drop.
     329        wpmu_delete_blog( $blog_id, true );
     330
     331        $prefix = $wpdb->get_blog_prefix( $blog_id );
     332        foreach ( $wpdb->tables( 'blog', false ) as $table ) {
     333            $suppress = $wpdb->suppress_errors();
     334
     335            // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
     336            $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
     337
     338            $wpdb->suppress_errors( $suppress );
     339            $this->assertNotEmpty( $table_fields, $prefix . $table );
    206340        }
    207 
    208         public function test_site_caches_should_not_invalidate_when_invalidation_is_suspended() {
    209             $site_id = self::factory()->blog->create();
    210 
    211             $details = get_site( $site_id );
    212 
    213             $suspend = wp_suspend_cache_invalidation();
    214             update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
    215             $new_details = get_site( $site_id );
    216             wp_suspend_cache_invalidation( $suspend );
    217 
    218             $this->assertSame( $details->path, $new_details->path );
     341    }
     342
     343    /**
     344     * The site count of a network should change when a site is flagged as 'deleted'.
     345     */
     346    public function test_network_count_after_wpmu_delete_blog_drop_false() {
     347        $blog_id = self::factory()->blog->create();
     348
     349        // Delete the site without forcing a table drop.
     350        wpmu_delete_blog( $blog_id, false );
     351
     352        // Update the blog count cache to use get_blog_count().
     353        wp_update_network_counts();
     354        $this->assertSame( 1, get_blog_count() );
     355    }
     356
     357    /**
     358     * The site count of a network should change when a site is fully deleted.
     359     */
     360    public function test_blog_count_after_wpmu_delete_blog_drop_true() {
     361        $blog_id = self::factory()->blog->create();
     362
     363        // Delete the site and force a table drop.
     364        wpmu_delete_blog( $blog_id, true );
     365
     366        // Update the blog count cache to use get_blog_count().
     367        wp_update_network_counts();
     368        $this->assertSame( 1, get_blog_count() );
     369    }
     370
     371    /**
     372     * When a site is deleted with wpmu_delete_blog(), only the files associated with
     373     * that site should be removed. When wpmu_delete_blog() is run a second time, nothing
     374     * should change with upload directories.
     375     */
     376    public function test_upload_directories_after_multiple_wpmu_delete_blog() {
     377        $filename = __FUNCTION__ . '.jpg';
     378        $contents = __FUNCTION__ . '_contents';
     379
     380        // Upload a file to the main site on the network.
     381        $file1 = wp_upload_bits( $filename, null, $contents );
     382
     383        $blog_id = self::factory()->blog->create();
     384
     385        switch_to_blog( $blog_id );
     386        $file2 = wp_upload_bits( $filename, null, $contents );
     387        restore_current_blog();
     388
     389        wpmu_delete_blog( $blog_id, true );
     390
     391        // The file on the main site should still exist. The file on the deleted site should not.
     392        $this->assertFileExists( $file1['file'] );
     393        $this->assertFileDoesNotExist( $file2['file'] );
     394
     395        wpmu_delete_blog( $blog_id, true );
     396
     397        // The file on the main site should still exist. The file on the deleted site should not.
     398        $this->assertFileExists( $file1['file'] );
     399        $this->assertFileDoesNotExist( $file2['file'] );
     400
     401        unlink( $file1['file'] );
     402    }
     403
     404    public function test_wpmu_update_blogs_date() {
     405        global $wpdb;
     406
     407        wpmu_update_blogs_date();
     408
     409        $blog         = get_site( get_current_blog_id() );
     410        $current_time = time();
     411
     412        // Compare the update time with the current time, allow delta < 2.
     413        $this->assertEqualsWithDelta( $current_time, strtotime( $blog->last_updated ), 2, 'The dates should be equal' );
     414    }
     415
     416    /**
     417     * Test cached data for a site that does not exist and then again after it exists.
     418     *
     419     * @ticket 23405
     420     */
     421    public function test_get_blog_details_when_site_does_not_exist() {
     422        // Create an unused site so that we can then assume an invalid site ID.
     423        $blog_id = self::factory()->blog->create();
     424        ++$blog_id;
     425
     426        // Prime the cache for an invalid site.
     427        get_blog_details( $blog_id );
     428
     429        // When the cache is primed with an invalid site, the value is set to -1.
     430        $this->assertSame( -1, wp_cache_get( $blog_id, 'blog-details' ) );
     431
     432        // Create a site in the invalid site's place.
     433        self::factory()->blog->create();
     434
     435        // When a new site is created, its cache is cleared through refresh_blog_details.
     436        $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
     437
     438        $blog = get_blog_details( $blog_id );
     439
     440        // When the cache is refreshed, it should now equal the site data.
     441        $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
     442    }
     443
     444    /**
     445     * @ticket 26410
     446     */
     447    public function test_blog_details_cache_invalidation() {
     448        update_option( 'blogname', 'foo' );
     449        $details = get_site( get_current_blog_id() );
     450        $this->assertSame( 'foo', $details->blogname );
     451
     452        update_option( 'blogname', 'bar' );
     453        $details = get_site( get_current_blog_id() );
     454        $this->assertSame( 'bar', $details->blogname );
     455    }
     456
     457    /**
     458     * Test the original and cached responses for a created and then deleted site when
     459     * the blog ID is requested through get_blog_id_from_url().
     460     */
     461    public function test_get_blog_id_from_url() {
     462        $blog_id = self::factory()->blog->create();
     463        $details = get_site( $blog_id );
     464        $key     = md5( $details->domain . $details->path );
     465
     466        // Test the original response and cached response for the newly created site.
     467        $this->assertSame( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
     468        $this->assertSame( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
     469    }
     470
     471    /**
     472     * Test the case insensitivity of the site lookup.
     473     */
     474    public function test_get_blog_id_from_url_is_case_insensitive() {
     475        $blog_id = self::factory()->blog->create(
     476            array(
     477                'domain' => 'example.com',
     478                'path'   => '/xyz',
     479            )
     480        );
     481        $details = get_site( $blog_id );
     482
     483        $this->assertSame( $blog_id, get_blog_id_from_url( strtoupper( $details->domain ), strtoupper( $details->path ) ) );
     484    }
     485
     486    /**
     487     * Test the first and cached responses for a site that does not exist.
     488     */
     489    public function test_get_blog_id_from_url_that_does_not_exist() {
     490        $blog_id = self::factory()->blog->create( array( 'path' => '/xyz' ) );
     491        $details = get_site( $blog_id );
     492
     493        $this->assertSame( 0, get_blog_id_from_url( $details->domain, 'foo' ) );
     494        $this->assertSame( -1, wp_cache_get( md5( $details->domain . 'foo' ), 'blog-id-cache' ) );
     495    }
     496
     497    /**
     498     * A blog ID is still available if only the `deleted` flag is set for a site. The same
     499     * behavior would be expected if passing `false` explicitly to `wpmu_delete_blog()`.
     500     */
     501    public function test_get_blog_id_from_url_with_deleted_flag() {
     502        $blog_id = self::factory()->blog->create();
     503        $details = get_site( $blog_id );
     504        $key     = md5( $details->domain . $details->path );
     505        wpmu_delete_blog( $blog_id );
     506
     507        $this->assertSame( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
     508        $this->assertSame( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
     509    }
     510
     511    /**
     512     * When deleted with the drop parameter as true, the cache will first be false, then set to
     513     * -1 after an attempt at `get_blog_id_from_url()` is made.
     514     */
     515    public function test_get_blog_id_from_url_after_dropped() {
     516        $blog_id = self::factory()->blog->create();
     517        $details = get_site( $blog_id );
     518        $key     = md5( $details->domain . $details->path );
     519        wpmu_delete_blog( $blog_id, true );
     520
     521        $this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
     522        $this->assertSame( 0, get_blog_id_from_url( $details->domain, $details->path ) );
     523        $this->assertSame( -1, wp_cache_get( $key, 'blog-id-cache' ) );
     524    }
     525
     526    /**
     527     * Test with default parameter of site_id as null.
     528     */
     529    public function test_is_main_site() {
     530        $this->assertTrue( is_main_site() );
     531    }
     532
     533    /**
     534     * Test with a site id of get_current_blog_id(), which should be the same as the
     535     * default parameter tested above.
     536     */
     537    public function test_current_blog_id_is_main_site() {
     538        $this->assertTrue( is_main_site( get_current_blog_id() ) );
     539    }
     540
     541    /**
     542     * Test with a site ID other than the main site to ensure a false response.
     543     */
     544    public function test_is_main_site_is_false_with_other_blog_id() {
     545        $blog_id = self::factory()->blog->create();
     546
     547        $this->assertFalse( is_main_site( $blog_id ) );
     548    }
     549
     550    /**
     551     * Test with no passed ID after switching to another site ID.
     552     */
     553    public function test_is_main_site_is_false_after_switch_to_blog() {
     554        $blog_id = self::factory()->blog->create();
     555        switch_to_blog( $blog_id );
     556
     557        $this->assertFalse( is_main_site() );
     558
     559        restore_current_blog();
     560    }
     561
     562    public function test_switch_upload_dir() {
     563        $this->assertTrue( is_main_site() );
     564
     565        $site = get_current_site();
     566        $date = date_format( date_create( 'now' ), 'Y/m' );
     567
     568        $info = wp_upload_dir();
     569        $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] );
     570        $this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] );
     571        $this->assertSame( '/' . $date, $info['subdir'] );
     572        $this->assertFalse( $info['error'] );
     573
     574        $blog_id = self::factory()->blog->create();
     575
     576        switch_to_blog( $blog_id );
     577        $info = wp_upload_dir();
     578        $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . $date, $info['url'] );
     579        $this->assertSame( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . $date, $info['path'] );
     580        $this->assertSame( '/' . $date, $info['subdir'] );
     581        $this->assertFalse( $info['error'] );
     582        restore_current_blog();
     583
     584        $info = wp_upload_dir();
     585        $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] );
     586        $this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] );
     587        $this->assertSame( '/' . $date, $info['subdir'] );
     588        $this->assertFalse( $info['error'] );
     589    }
     590
     591    /**
     592     * Test the primary purpose of get_blog_post(), to retrieve a post from
     593     * another site on the network.
     594     */
     595    public function test_get_blog_post_from_another_site_on_network() {
     596        $blog_id = self::factory()->blog->create();
     597        $post_id = self::factory()->post->create(); // Create a post on the primary site, ID 1.
     598        $post    = get_post( $post_id );
     599        switch_to_blog( $blog_id );
     600
     601        // The post created and retrieved on the main site should match the one retrieved "remotely".
     602        $this->assertEquals( $post, get_blog_post( 1, $post_id ) );
     603
     604        restore_current_blog();
     605    }
     606
     607    /**
     608     * If get_blog_post() is used on the same site, it should still work.
     609     */
     610    public function test_get_blog_post_from_same_site() {
     611        $post_id = self::factory()->post->create();
     612
     613        $this->assertEquals( get_blog_post( 1, $post_id ), get_post( $post_id ) );
     614    }
     615
     616    /**
     617     * A null response should be returned if an invalid post is requested.
     618     */
     619    public function test_get_blog_post_invalid_returns_null() {
     620        $this->assertNull( get_blog_post( 1, 999999 ) );
     621    }
     622
     623    /**
     624     * Added as a callback to the domain_exists filter to provide manual results for
     625     * the testing of the filter and for a test which does not need the database.
     626     */
     627    public function domain_exists_cb( $exists, $domain, $path, $site_id ) {
     628        if ( 'foo' === $domain && 'bar/' === $path ) {
     629            return 1234;
     630        } else {
     631            return null;
    219632        }
    220 
    221         /**
    222          * When a site is flagged as 'deleted', its data should be cleared from cache.
    223          */
    224         public function test_data_in_cache_after_wpmu_delete_blog_drop_false() {
    225             $blog_id = self::factory()->blog->create();
    226 
    227             $details = get_blog_details( $blog_id, false );
    228             $key     = md5( $details->domain . $details->path );
    229 
    230             // Delete the site without forcing a table drop.
    231             wpmu_delete_blog( $blog_id, false );
    232 
    233             $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
    234             $this->assertFalse( wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    235             $this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
    236             $this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
     633    }
     634
     635    public function test_domain_exists_with_default_site_id() {
     636        $details = get_site( 1 );
     637
     638        $this->assertSame( 1, domain_exists( $details->domain, $details->path ) );
     639    }
     640
     641    public function test_domain_exists_with_specified_site_id() {
     642        $details = get_site( 1 );
     643
     644        $this->assertSame( 1, domain_exists( $details->domain, $details->path, $details->site_id ) );
     645    }
     646
     647    /**
     648     * When the domain is valid, but the resulting site does not belong to the specified network,
     649     * it is marked as not existing.
     650     */
     651    public function test_domain_does_not_exist_with_invalid_site_id() {
     652        $details = get_site( 1 );
     653
     654        $this->assertNull( domain_exists( $details->domain, $details->path, 999 ) );
     655    }
     656
     657    public function test_invalid_domain_does_not_exist_with_default_site_id() {
     658        $this->assertNull( domain_exists( 'foo', 'bar' ) );
     659    }
     660
     661    public function test_domain_filtered_to_exist() {
     662        add_filter( 'domain_exists', array( $this, 'domain_exists_cb' ), 10, 4 );
     663        $exists = domain_exists( 'foo', 'bar' );
     664        remove_filter( 'domain_exists', array( $this, 'domain_exists_cb' ), 10, 4 );
     665        $this->assertSame( 1234, $exists );
     666    }
     667
     668    /**
     669     * When a path is passed to domain_exists, it is immediately trailing slashed. A path
     670     * value with or without the slash should result in the same return value.
     671     */
     672    public function test_slashed_path_in_domain_exists() {
     673        add_filter( 'domain_exists', array( $this, 'domain_exists_cb' ), 10, 4 );
     674        $exists1 = domain_exists( 'foo', 'bar' );
     675        $exists2 = domain_exists( 'foo', 'bar/' );
     676        remove_filter( 'domain_exists', array( $this, 'domain_exists_cb' ), 10, 4 );
     677
     678        // Make sure the same result is returned with or without a trailing slash.
     679        $this->assertSame( $exists1, $exists2 );
     680    }
     681
     682    /**
     683     * Tests returning an address for a given valid ID.
     684     */
     685    public function test_get_blogaddress_by_id_with_valid_id() {
     686        $blogaddress = get_blogaddress_by_id( 1 );
     687        $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/', $blogaddress );
     688    }
     689
     690    /**
     691     * Tests returning an empty string for a non-existing ID.
     692     */
     693    public function test_get_blogaddress_by_id_with_invalid_id() {
     694        $blogaddress = get_blogaddress_by_id( PHP_INT_MAX );
     695        $this->assertSame( '', $blogaddress );
     696    }
     697
     698    /**
     699     * @ticket 14867
     700     */
     701    public function test_get_blogaddress_by_id_scheme_reflects_blog_scheme() {
     702        $blog = self::factory()->blog->create();
     703
     704        $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
     705
     706        update_blog_option( $blog, 'home', set_url_scheme( get_blog_option( $blog, 'home' ), 'https' ) );
     707
     708        $this->assertSame( 'https', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
     709    }
     710
     711    /**
     712     * @ticket 14867
     713     */
     714    public function test_get_blogaddress_by_id_scheme_is_unaffected_by_request() {
     715        $blog = self::factory()->blog->create();
     716
     717        $this->assertFalse( is_ssl() );
     718        $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
     719
     720        $_SERVER['HTTPS'] = 'on';
     721
     722        $is_ssl  = is_ssl();
     723        $address = parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME );
     724
     725        $this->assertTrue( $is_ssl );
     726        $this->assertSame( 'http', $address );
     727    }
     728
     729    /**
     730     * @ticket 33620
     731     * @dataProvider data_new_blog_url_schemes
     732     */
     733    public function test_new_blog_url_schemes( $home_scheme, $siteurl_scheme, $force_ssl_admin ) {
     734        $current_site = get_current_site();
     735
     736        $home    = get_option( 'home' );
     737        $siteurl = get_site_option( 'siteurl' );
     738        $admin   = force_ssl_admin();
     739
     740        // Setup:
     741        update_option( 'home', set_url_scheme( $home, $home_scheme ) );
     742        update_site_option( 'siteurl', set_url_scheme( $siteurl, $siteurl_scheme ) );
     743        force_ssl_admin( $force_ssl_admin );
     744
     745        // Install:
     746        $new = wpmu_create_blog( $current_site->domain, '/new-blog/', 'New Blog', get_current_user_id() );
     747
     748        // Reset:
     749        update_option( 'home', $home );
     750        update_site_option( 'siteurl', $siteurl );
     751        force_ssl_admin( $admin );
     752
     753        // Assert:
     754        $this->assertNotWPError( $new );
     755        $this->assertSame( $home_scheme, parse_url( get_blog_option( $new, 'home' ), PHP_URL_SCHEME ) );
     756        $this->assertSame( $siteurl_scheme, parse_url( get_blog_option( $new, 'siteurl' ), PHP_URL_SCHEME ) );
     757    }
     758
     759    public function data_new_blog_url_schemes() {
     760        return array(
     761            array(
     762                'https',
     763                'https',
     764                false,
     765            ),
     766            array(
     767                'http',
     768                'https',
     769                false,
     770            ),
     771            array(
     772                'https',
     773                'http',
     774                false,
     775            ),
     776            array(
     777                'http',
     778                'http',
     779                false,
     780            ),
     781            array(
     782                'http',
     783                'http',
     784                true,
     785            ),
     786        );
     787    }
     788
     789    /**
     790     * @ticket 36918
     791     */
     792    public function test_new_blog_locale() {
     793        $current_site = get_current_site();
     794
     795        add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     796        update_site_option( 'WPLANG', 'de_DE' );
     797        remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     798
     799        // No locale, use default locale.
     800        add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     801        $blog_id = wpmu_create_blog( $current_site->domain, '/de-de/', 'New Blog', get_current_user_id() );
     802        remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     803
     804        $this->assertNotWPError( $blog_id );
     805        $this->assertSame( 'de_DE', get_blog_option( $blog_id, 'WPLANG' ) );
     806
     807        // Custom locale.
     808        add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     809        $blog_id = wpmu_create_blog( $current_site->domain, '/es-es/', 'New Blog', get_current_user_id(), array( 'WPLANG' => 'es_ES' ) );
     810        remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     811
     812        $this->assertNotWPError( $blog_id );
     813        $this->assertSame( 'es_ES', get_blog_option( $blog_id, 'WPLANG' ) );
     814
     815        // en_US locale.
     816        add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     817        $blog_id = wpmu_create_blog( $current_site->domain, '/en-us/', 'New Blog', get_current_user_id(), array( 'WPLANG' => '' ) );
     818        remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     819
     820        $this->assertNotWPError( $blog_id );
     821        $this->assertSame( '', get_blog_option( $blog_id, 'WPLANG' ) );
     822    }
     823
     824    /**
     825     * @ticket 40503
     826     */
     827    public function test_different_network_language() {
     828        $network = get_network( self::$network_ids['make.wordpress.org/'] );
     829
     830        add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
     831
     832        update_network_option( self::$network_ids['make.wordpress.org/'], 'WPLANG', 'wibble' );
     833        $blog_id = wpmu_create_blog( $network->domain, '/de-de/', 'New Blog', get_current_user_id(), array(), $network->id );
     834
     835        remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
     836
     837        $this->assertSame( get_network_option( self::$network_ids['make.wordpress.org/'], 'WPLANG' ), get_blog_option( $blog_id, 'WPLANG' ) );
     838    }
     839
     840    /**
     841     * Allows to set the WPLANG option to any language.
     842     *
     843     * @param string $value          The sanitized option value.
     844     * @param string $option         The option name.
     845     * @param string $original_value The original value passed to the function.
     846     * @return string The original value.
     847     */
     848    public function filter_allow_unavailable_languages( $value, $option, $original_value ) {
     849        return $original_value;
     850    }
     851
     852    /**
     853     * @ticket 29684
     854     */
     855    public function test_is_main_site_different_network() {
     856        $this->assertTrue( is_main_site( self::$site_ids['make.wordpress.org/'], self::$network_ids['make.wordpress.org/'] ) );
     857    }
     858
     859    /**
     860     * @ticket 29684
     861     */
     862    public function test_is_main_site_different_network_random_site() {
     863        $this->assertFalse( is_main_site( self::$site_ids['make.wordpress.org/foo/'], self::$network_ids['make.wordpress.org/'] ) );
     864    }
     865
     866    /**
     867     * @ticket 40201
     868     * @dataProvider data_get_site_caches
     869     */
     870    public function test_clean_blog_cache( $key, $group ) {
     871        $site = get_site( self::$site_ids['make.wordpress.org/'] );
     872
     873        $replacements = array(
     874            '%blog_id%'         => $site->blog_id,
     875            '%domain%'          => $site->domain,
     876            '%path%'            => $site->path,
     877            '%domain_path_key%' => md5( $site->domain . $site->path ),
     878        );
     879
     880        $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
     881
     882        if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
     883            wp_cache_set( $key, (object) $site->to_array(), $group );
     884        } else {
     885            wp_cache_set( $key, 'something', $group );
    237886        }
    238887
    239         /**
    240          * When a site is flagged as 'deleted', its data should remain in the database.
    241          */
    242         public function test_data_in_tables_after_wpmu_delete_blog_drop_false() {
    243             global $wpdb;
    244 
    245             $blog_id = self::factory()->blog->create();
    246 
    247             // Delete the site without forcing a table drop.
    248             wpmu_delete_blog( $blog_id, false );
    249 
    250             $prefix = $wpdb->get_blog_prefix( $blog_id );
    251             foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    252                 $suppress = $wpdb->suppress_errors();
    253 
    254                 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    255                 $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    256 
    257                 $wpdb->suppress_errors( $suppress );
    258                 $this->assertNotEmpty( $table_fields, $prefix . $table );
     888        clean_blog_cache( $site );
     889        $this->assertFalse( wp_cache_get( $key, $group ) );
     890    }
     891
     892    /**
     893     * @ticket 40201
     894     * @dataProvider data_get_site_caches
     895     */
     896    public function test_clean_blog_cache_with_id( $key, $group ) {
     897        $site = get_site( self::$site_ids['make.wordpress.org/'] );
     898
     899        $replacements = array(
     900            '%blog_id%'         => $site->blog_id,
     901            '%domain%'          => $site->domain,
     902            '%path%'            => $site->path,
     903            '%domain_path_key%' => md5( $site->domain . $site->path ),
     904        );
     905
     906        $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
     907
     908        if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
     909            wp_cache_set( $key, (object) $site->to_array(), $group );
     910        } else {
     911            wp_cache_set( $key, 'something', $group );
     912        }
     913
     914        clean_blog_cache( $site->blog_id );
     915        $this->assertFalse( wp_cache_get( $key, $group ) );
     916    }
     917
     918    /**
     919     * @ticket 40201
     920     */
     921    public function test_clean_blog_cache_resets_last_changed() {
     922        $site = get_site( self::$site_ids['make.wordpress.org/'] );
     923
     924        wp_cache_delete( 'last_changed', 'sites' );
     925
     926        clean_blog_cache( $site );
     927        $this->assertNotFalse( wp_cache_get( 'last_changed', 'sites' ) );
     928    }
     929
     930    /**
     931     * @ticket 40201
     932     */
     933    public function test_clean_blog_cache_fires_action() {
     934        $site = get_site( self::$site_ids['make.wordpress.org/'] );
     935
     936        $old_count = did_action( 'clean_site_cache' );
     937
     938        clean_blog_cache( $site );
     939        $this->assertSame( $old_count + 1, did_action( 'clean_site_cache' ) );
     940    }
     941
     942    /**
     943     * @ticket 40201
     944     */
     945    public function test_clean_blog_cache_bails_on_suspend_cache_invalidation() {
     946        $site = get_site( self::$site_ids['make.wordpress.org/'] );
     947
     948        $old_count = did_action( 'clean_site_cache' );
     949
     950        $suspend = wp_suspend_cache_invalidation();
     951        clean_blog_cache( $site );
     952        wp_suspend_cache_invalidation( $suspend );
     953        $this->assertSame( $old_count, did_action( 'clean_site_cache' ) );
     954    }
     955
     956    /**
     957     * @ticket 40201
     958     */
     959    public function test_clean_blog_cache_bails_on_empty_input() {
     960        $old_count = did_action( 'clean_site_cache' );
     961
     962        clean_blog_cache( null );
     963        $this->assertSame( $old_count, did_action( 'clean_site_cache' ) );
     964    }
     965
     966    /**
     967     * @ticket 40201
     968     */
     969    public function test_clean_blog_cache_bails_on_non_numeric_input() {
     970        $old_count = did_action( 'clean_site_cache' );
     971
     972        clean_blog_cache( 'something' );
     973        $this->assertSame( $old_count, did_action( 'clean_site_cache' ) );
     974    }
     975
     976    /**
     977     * @ticket 40201
     978     */
     979    public function test_clean_blog_cache_works_with_deleted_site() {
     980        $site_id = 12345;
     981
     982        wp_cache_set( $site_id, 'something', 'site-details' );
     983
     984        clean_blog_cache( $site_id );
     985        $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
     986    }
     987
     988    /**
     989     * @ticket 40201
     990     * @dataProvider data_get_site_caches
     991     */
     992    public function test_refresh_blog_details( $key, $group ) {
     993        $site = get_site( self::$site_ids['make.wordpress.org/'] );
     994
     995        $replacements = array(
     996            '%blog_id%'         => $site->blog_id,
     997            '%domain%'          => $site->domain,
     998            '%path%'            => $site->path,
     999            '%domain_path_key%' => md5( $site->domain . $site->path ),
     1000        );
     1001
     1002        $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
     1003
     1004        if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
     1005            wp_cache_set( $key, (object) $site->to_array(), $group );
     1006        } else {
     1007            wp_cache_set( $key, 'something', $group );
     1008        }
     1009
     1010        refresh_blog_details( $site->blog_id );
     1011        $this->assertFalse( wp_cache_get( $key, $group ) );
     1012    }
     1013
     1014    /**
     1015     * @ticket 40201
     1016     */
     1017    public function test_refresh_blog_details_works_with_deleted_site() {
     1018        $site_id = 12345;
     1019
     1020        wp_cache_set( $site_id, 'something', 'site-details' );
     1021
     1022        refresh_blog_details( $site_id );
     1023        $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
     1024    }
     1025
     1026    /**
     1027     * @ticket 40201
     1028     */
     1029    public function test_refresh_blog_details_uses_current_site_as_default() {
     1030        $site_id = get_current_blog_id();
     1031
     1032        wp_cache_set( $site_id, 'something', 'site-details' );
     1033
     1034        refresh_blog_details();
     1035        $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
     1036    }
     1037
     1038    public function data_get_site_caches() {
     1039        return array(
     1040            array( '%blog_id%', 'sites' ),
     1041            array( '%blog_id%', 'site-details' ),
     1042            array( '%blog_id%', 'blog-details' ),
     1043            array( '%blog_id%' . 'short', 'blog-details' ),
     1044            array( '%domain_path_key%', 'blog-lookup' ),
     1045            array( '%domain_path_key%', 'blog-id-cache' ),
     1046        );
     1047    }
     1048
     1049    /**
     1050     * @ticket 40364
     1051     * @dataProvider data_wp_insert_site
     1052     */
     1053    public function test_wp_insert_site( $site_data, $expected_data ) {
     1054        remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
     1055        $site_id = wp_insert_site( $site_data );
     1056
     1057        $this->assertIsInt( $site_id );
     1058
     1059        $site = get_site( $site_id );
     1060        foreach ( $expected_data as $key => $value ) {
     1061            $this->assertEquals( $value, $site->$key );
     1062        }
     1063    }
     1064
     1065    public function data_wp_insert_site() {
     1066        return array(
     1067            array(
     1068                array(
     1069                    'domain' => 'example.com',
     1070                ),
     1071                array(
     1072                    'domain'     => 'example.com',
     1073                    'path'       => '/',
     1074                    'network_id' => 1,
     1075                    'public'     => 1,
     1076                    'archived'   => 0,
     1077                    'mature'     => 0,
     1078                    'spam'       => 0,
     1079                    'deleted'    => 0,
     1080                    'lang_id'    => 0,
     1081                ),
     1082            ),
     1083            array(
     1084                array(
     1085                    'domain'     => 'example.com',
     1086                    'path'       => '/foo',
     1087                    'network_id' => 2,
     1088                ),
     1089                array(
     1090                    'domain'     => 'example.com',
     1091                    'path'       => '/foo/',
     1092                    'network_id' => 2,
     1093                ),
     1094            ),
     1095            array(
     1096                array(
     1097                    'domain'  => 'example.com',
     1098                    'path'    => '/bar/',
     1099                    'site_id' => 2,
     1100                ),
     1101                array(
     1102                    'domain'     => 'example.com',
     1103                    'path'       => '/bar/',
     1104                    'network_id' => 2,
     1105                ),
     1106            ),
     1107            array(
     1108                array(
     1109                    'domain'     => 'example.com',
     1110                    'path'       => '/bar/',
     1111                    'site_id'    => 2,
     1112                    'network_id' => 3,
     1113                ),
     1114                array(
     1115                    'domain'     => 'example.com',
     1116                    'path'       => '/bar/',
     1117                    'network_id' => 3,
     1118                ),
     1119            ),
     1120            array(
     1121                array(
     1122                    'domain'   => 'example.com',
     1123                    'path'     => 'foobar',
     1124                    'public'   => 0,
     1125                    'archived' => 1,
     1126                    'mature'   => 1,
     1127                    'spam'     => 1,
     1128                    'deleted'  => 1,
     1129                    'lang_id'  => 1,
     1130                ),
     1131                array(
     1132                    'domain'   => 'example.com',
     1133                    'path'     => '/foobar/',
     1134                    'public'   => 0,
     1135                    'archived' => 1,
     1136                    'mature'   => 1,
     1137                    'spam'     => 1,
     1138                    'deleted'  => 1,
     1139                    'lang_id'  => 1,
     1140                ),
     1141            ),
     1142            array(
     1143                array(
     1144                    'domain' => 'example.com:8888',
     1145                ),
     1146                array(
     1147                    'domain'     => 'example.com:8888',
     1148                    'path'       => '/',
     1149                    'network_id' => 1,
     1150                    'public'     => 1,
     1151                    'archived'   => 0,
     1152                    'mature'     => 0,
     1153                    'spam'       => 0,
     1154                    'deleted'    => 0,
     1155                    'lang_id'    => 0,
     1156                ),
     1157            ),
     1158        );
     1159    }
     1160
     1161    /**
     1162     * @ticket 50324
     1163     */
     1164    public function test_wp_insert_site_with_clean_site_cache() {
     1165        remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
     1166
     1167        add_action( 'clean_site_cache', array( $this, 'action_database_insert_on_clean_site_cache' ) );
     1168
     1169        $site_id = wp_insert_site(
     1170            array(
     1171                'domain'     => 'valid-domain.com',
     1172                'path'       => '/valid-path/',
     1173                'network_id' => 1,
     1174            )
     1175        );
     1176
     1177        remove_action( 'clean_site_cache', array( $this, 'action_database_insert_on_clean_site_cache' ) );
     1178
     1179        $this->assertIsInt( $site_id );
     1180    }
     1181
     1182    public function action_database_insert_on_clean_site_cache() {
     1183        update_site_option( 'database_write_test.' . time(), true );
     1184    }
     1185
     1186    /**
     1187     * @ticket 40364
     1188     */
     1189    public function test_wp_insert_site_empty_domain() {
     1190        remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
     1191        $site_id = wp_insert_site( array( 'public' => 0 ) );
     1192
     1193        $this->assertWPError( $site_id );
     1194        $this->assertSame( 'site_empty_domain', $site_id->get_error_code() );
     1195    }
     1196
     1197    /**
     1198     * @ticket 40364
     1199     * @dataProvider data_wp_update_site
     1200     */
     1201    public function test_wp_update_site( $site_data, $expected_data ) {
     1202        $site_id = self::factory()->blog->create();
     1203
     1204        $old_site = get_site( $site_id );
     1205
     1206        $result = wp_update_site( $site_id, $site_data );
     1207
     1208        $this->assertSame( $site_id, $result );
     1209
     1210        $new_site = get_site( $site_id );
     1211        foreach ( $new_site->to_array() as $key => $value ) {
     1212            if ( isset( $expected_data[ $key ] ) ) {
     1213                $this->assertEquals( $expected_data[ $key ], $value );
     1214            } elseif ( 'last_updated' === $key ) {
     1215                $this->assertLessThanOrEqual( $value, $old_site->last_updated );
     1216            } else {
     1217                $this->assertSame( $old_site->$key, $value );
    2591218            }
    2601219        }
    261 
    262         /**
    263          * When a site is fully deleted, its data should be cleared from cache.
    264          */
    265         public function test_data_in_cache_after_wpmu_delete_blog_drop_true() {
    266             $blog_id = self::factory()->blog->create();
    267 
    268             $details = get_blog_details( $blog_id, false );
    269             $key     = md5( $details->domain . $details->path );
    270 
    271             // Delete the site and force a table drop.
    272             wpmu_delete_blog( $blog_id, true );
    273 
    274             $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
    275             $this->assertFalse( wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    276             $this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
    277             $this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
     1220    }
     1221
     1222    public function data_wp_update_site() {
     1223        return array(
     1224            array(
     1225                array(
     1226                    'domain'     => 'example.com',
     1227                    'network_id' => 2,
     1228                ),
     1229                array(
     1230                    'domain'  => 'example.com',
     1231                    'site_id' => 2,
     1232                ),
     1233            ),
     1234            array(
     1235                array(
     1236                    'path' => 'foo',
     1237                ),
     1238                array(
     1239                    'path' => '/foo/',
     1240                ),
     1241            ),
     1242            array(
     1243                array(
     1244                    'public'   => 0,
     1245                    'archived' => 1,
     1246                    'mature'   => 1,
     1247                    'spam'     => 1,
     1248                    'deleted'  => 1,
     1249                    'lang_id'  => 1,
     1250                ),
     1251                array(
     1252                    'public'   => 0,
     1253                    'archived' => 1,
     1254                    'mature'   => 1,
     1255                    'spam'     => 1,
     1256                    'deleted'  => 1,
     1257                    'lang_id'  => 1,
     1258                ),
     1259            ),
     1260            array(
     1261                array(
     1262                    'domain'     => 'example.com:8888',
     1263                    'network_id' => 2,
     1264                ),
     1265                array(
     1266                    'domain'  => 'example.com:8888',
     1267                    'site_id' => 2,
     1268                ),
     1269            ),
     1270        );
     1271    }
     1272
     1273    /**
     1274     * @ticket 40364
     1275     */
     1276    public function test_wp_update_site_empty_domain() {
     1277        $site_id = self::factory()->blog->create();
     1278
     1279        $result = wp_update_site( $site_id, array( 'domain' => '' ) );
     1280
     1281        $this->assertWPError( $result );
     1282        $this->assertSame( 'site_empty_domain', $result->get_error_code() );
     1283    }
     1284
     1285    /**
     1286     * @ticket 40364
     1287     */
     1288    public function test_wp_update_site_invalid_id() {
     1289        $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) );
     1290
     1291        $this->assertWPError( $result );
     1292        $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1293    }
     1294
     1295    /**
     1296     * @ticket 40364
     1297     */
     1298    public function test_wp_update_site_cleans_cache() {
     1299        $site_id = self::factory()->blog->create();
     1300        $site1   = get_site( $site_id );
     1301
     1302        $result = wp_update_site( $site_id, array( 'public' => 0 ) );
     1303        $site2  = get_site( $site_id );
     1304
     1305        $result = wp_update_site( $site_id, array( 'public' => 1 ) );
     1306        $site3  = get_site( $site_id );
     1307
     1308        $this->assertSame( '1', $site1->public );
     1309        $this->assertSame( '0', $site2->public );
     1310        $this->assertSame( '1', $site3->public );
     1311    }
     1312
     1313    /**
     1314     * @ticket 40364
     1315     */
     1316    public function test_wp_delete_site() {
     1317        $site_id = self::factory()->blog->create();
     1318
     1319        $site = get_site( $site_id );
     1320
     1321        $result = wp_delete_site( $site_id );
     1322
     1323        $this->assertInstanceOf( 'WP_Site', $result );
     1324        $this->assertSame( $result->to_array(), $site->to_array() );
     1325    }
     1326
     1327    /**
     1328     * @ticket 40364
     1329     */
     1330    public function test_wp_delete_site_invalid_id() {
     1331        $result = wp_delete_site( 444444 );
     1332
     1333        $this->assertWPError( $result );
     1334        $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1335    }
     1336
     1337    /**
     1338     * @ticket 41333
     1339     */
     1340    public function test_wp_delete_site_validate_site_deletion_action() {
     1341        add_action( 'wp_validate_site_deletion', array( $this, 'action_wp_validate_site_deletion_prevent_deletion' ) );
     1342        $result = wp_delete_site( self::$site_ids['make.wordpress.org/'] );
     1343        $this->assertWPError( $result );
     1344        $this->assertSame( 'action_does_not_like_deletion', $result->get_error_code() );
     1345    }
     1346
     1347    public function action_wp_validate_site_deletion_prevent_deletion( $errors ) {
     1348        $errors->add( 'action_does_not_like_deletion', 'You cannot delete this site because the action does not like it.' );
     1349    }
     1350
     1351    /**
     1352     * @ticket 40364
     1353     * @dataProvider data_wp_normalize_site_data
     1354     */
     1355    public function test_wp_normalize_site_data( $data, $expected ) {
     1356        $result = wp_normalize_site_data( $data );
     1357
     1358        $this->assertSameSetsWithIndex( $expected, $result );
     1359    }
     1360
     1361    public function data_wp_normalize_site_data() {
     1362        return array(
     1363            array(
     1364                array(
     1365                    'network_id' => '4',
     1366                ),
     1367                array(
     1368                    'network_id' => 4,
     1369                ),
     1370            ),
     1371            array(
     1372                array(
     1373                    'domain' => 'invalid domain .com',
     1374                    'path'   => 'foo',
     1375                ),
     1376                array(
     1377                    'domain' => 'invaliddomain.com',
     1378                    'path'   => '/foo/',
     1379                ),
     1380            ),
     1381            array(
     1382                array(
     1383                    'domain' => '<yet>/another-invalid-domain.com',
     1384                ),
     1385                array(
     1386                    'domain' => 'yetanother-invalid-domain.com',
     1387                ),
     1388            ),
     1389            array(
     1390                array(
     1391                    'domain' => 'with-port.com:8888',
     1392                ),
     1393                array(
     1394                    'domain' => 'with-port.com:8888',
     1395                ),
     1396            ),
     1397            array(
     1398                array(
     1399                    'domain' => 'subdomain.with-port.com:8888',
     1400                ),
     1401                array(
     1402                    'domain' => 'subdomain.with-port.com:8888',
     1403                ),
     1404            ),
     1405            array(
     1406                array(
     1407                    'path' => '',
     1408                ),
     1409                array(
     1410                    'path' => '/',
     1411                ),
     1412            ),
     1413            array(
     1414                array(
     1415                    'public'   => '0',
     1416                    'archived' => '1',
     1417                    'mature'   => '1',
     1418                    'spam'     => true,
     1419                    'deleted'  => true,
     1420                ),
     1421                array(
     1422                    'public'   => 0,
     1423                    'archived' => 1,
     1424                    'mature'   => 1,
     1425                    'spam'     => 1,
     1426                    'deleted'  => 1,
     1427                ),
     1428            ),
     1429            array(
     1430                array(
     1431                    'registered'   => '',
     1432                    'last_updated' => '',
     1433                ),
     1434                array(),
     1435            ),
     1436            array(
     1437                array(
     1438                    'registered'   => '0000-00-00 00:00:00',
     1439                    'last_updated' => '0000-00-00 00:00:00',
     1440                ),
     1441                array(),
     1442            ),
     1443        );
     1444    }
     1445
     1446    /**
     1447     * @ticket 40364
     1448     * @dataProvider data_wp_validate_site_data
     1449     */
     1450    public function test_wp_validate_site_data( $data, $expected_errors ) {
     1451        $result = new WP_Error();
     1452        wp_validate_site_data( $result, $data );
     1453
     1454        if ( empty( $expected_errors ) ) {
     1455            $this->assertEmpty( $result->errors );
     1456        } else {
     1457            $this->assertSameSets( $expected_errors, array_keys( $result->errors ) );
    2781458        }
    279 
    280         /**
    281          * When a site is fully deleted, its data should be removed from the database.
    282          */
    283         public function test_data_in_tables_after_wpmu_delete_blog_drop_true() {
    284             global $wpdb;
    285 
    286             $blog_id = self::factory()->blog->create();
    287 
    288             // Delete the site and force a table drop.
    289             wpmu_delete_blog( $blog_id, true );
    290 
    291             $prefix = $wpdb->get_blog_prefix( $blog_id );
    292             foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    293                 $suppress = $wpdb->suppress_errors();
    294 
    295                 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    296                 $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    297 
    298                 $wpdb->suppress_errors( $suppress );
    299                 $this->assertEmpty( $table_fields );
    300             }
     1459    }
     1460
     1461    public function data_wp_validate_site_data() {
     1462        $date = current_time( 'mysql', true );
     1463
     1464        return array(
     1465            array(
     1466                array(
     1467                    'domain'       => 'example-site.com',
     1468                    'path'         => '/',
     1469                    'network_id'   => 1,
     1470                    'registered'   => $date,
     1471                    'last_updated' => $date,
     1472                ),
     1473                array(),
     1474            ),
     1475            array(
     1476                array(
     1477                    'path'         => '/',
     1478                    'network_id'   => 1,
     1479                    'registered'   => $date,
     1480                    'last_updated' => $date,
     1481                ),
     1482                array( 'site_empty_domain' ),
     1483            ),
     1484            array(
     1485                array(
     1486                    'domain'       => 'example-site.com',
     1487                    'network_id'   => 1,
     1488                    'registered'   => $date,
     1489                    'last_updated' => $date,
     1490                ),
     1491                array( 'site_empty_path' ),
     1492            ),
     1493            array(
     1494                array(
     1495                    'domain'       => 'example-site.com',
     1496                    'path'         => '/',
     1497                    'registered'   => $date,
     1498                    'last_updated' => $date,
     1499                ),
     1500                array( 'site_empty_network_id' ),
     1501            ),
     1502            array(
     1503                array(
     1504                    'domain'       => get_site()->domain,
     1505                    'path'         => get_site()->path,
     1506                    'network_id'   => get_site()->network_id,
     1507                    'registered'   => $date,
     1508                    'last_updated' => $date,
     1509                ),
     1510                array( 'site_taken' ),
     1511            ),
     1512            array(
     1513                array(
     1514                    'domain'       => 'valid-domain.com',
     1515                    'path'         => '/valid-path/',
     1516                    'network_id'   => 1,
     1517                    'registered'   => '',
     1518                    'last_updated' => $date,
     1519                ),
     1520                array( 'site_empty_registered' ),
     1521            ),
     1522            array(
     1523                array(
     1524                    'domain'       => 'valid-domain.com',
     1525                    'path'         => '/valid-path/',
     1526                    'network_id'   => 1,
     1527                    'registered'   => $date,
     1528                    'last_updated' => '',
     1529                ),
     1530                array( 'site_empty_last_updated' ),
     1531            ),
     1532            array(
     1533                array(
     1534                    'domain'       => 'valid-domain.com',
     1535                    'path'         => '/valid-path/',
     1536                    'network_id'   => 1,
     1537                    'registered'   => '2000-13-32 25:25:61',
     1538                    'last_updated' => $date,
     1539                ),
     1540                array( 'site_invalid_registered' ),
     1541            ),
     1542            array(
     1543                array(
     1544                    'domain'       => 'valid-domain.com',
     1545                    'path'         => '/valid-path/',
     1546                    'network_id'   => 1,
     1547                    'registered'   => $date,
     1548                    'last_updated' => '2000-13-32 25:25:61',
     1549                ),
     1550                array( 'site_invalid_last_updated' ),
     1551            ),
     1552            array(
     1553                array(
     1554                    'domain'       => 'valid-domain.com',
     1555                    'path'         => '/valid-path/',
     1556                    'network_id'   => 1,
     1557                    'registered'   => '0000-00-00 00:00:00',
     1558                    'last_updated' => $date,
     1559                ),
     1560                array(),
     1561            ),
     1562            array(
     1563                array(
     1564                    'domain'       => 'valid-domain.com',
     1565                    'path'         => '/valid-path/',
     1566                    'network_id'   => 1,
     1567                    'registered'   => $date,
     1568                    'last_updated' => '0000-00-00 00:00:00',
     1569                ),
     1570                array(),
     1571            ),
     1572        );
     1573    }
     1574
     1575    /**
     1576     * @ticket 40364
     1577     */
     1578    public function test_site_dates_are_gmt() {
     1579        $first_date = current_time( 'mysql', true );
     1580
     1581        remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
     1582        $site_id = wp_insert_site(
     1583            array(
     1584                'domain'     => 'valid-domain.com',
     1585                'path'       => '/valid-path/',
     1586                'network_id' => 1,
     1587            )
     1588        );
     1589        $this->assertIsInt( $site_id );
     1590
     1591        $site = get_site( $site_id );
     1592        $this->assertEqualsWithDelta( strtotime( $first_date ), strtotime( $site->registered ), 2, 'The dates should be equal' );
     1593        $this->assertEqualsWithDelta( strtotime( $first_date ), strtotime( $site->last_updated ), 2, 'The dates should be equal' );
     1594
     1595        $second_date = current_time( 'mysql', true );
     1596        $site_id     = wp_update_site( $site_id, array() );
     1597        $this->assertIsInt( $site_id );
     1598
     1599        $site = get_site( $site_id );
     1600        $this->assertEqualsWithDelta( strtotime( $first_date ), strtotime( $site->registered ), 2, 'The dates should be equal' );
     1601        $this->assertEqualsWithDelta( strtotime( $second_date ), strtotime( $site->last_updated ), 2, 'The dates should be equal' );
     1602    }
     1603
     1604    /**
     1605     * @ticket 40364
     1606     */
     1607    public function test_wp_delete_site_cleans_cache() {
     1608        $site_id = self::factory()->blog->create();
     1609
     1610        get_site( $site_id );
     1611
     1612        wp_delete_site( $site_id );
     1613
     1614        $this->assertNull( get_site( $site_id ) );
     1615    }
     1616
     1617    /**
     1618     * @ticket 40364
     1619     */
     1620    public function test_wp_update_site_cleans_old_cache_on_domain_change() {
     1621        $old_domain = 'old.wordpress.org';
     1622        $new_domain = 'new.wordpress.org';
     1623
     1624        $site = self::factory()->blog->create_and_get(
     1625            array(
     1626                'domain' => $old_domain,
     1627                'path'   => '/',
     1628            )
     1629        );
     1630
     1631        // Populate the caches.
     1632        get_blog_details(
     1633            array(
     1634                'domain' => $old_domain,
     1635                'path'   => '/',
     1636            )
     1637        );
     1638        get_blog_id_from_url( $old_domain, '/' );
     1639        get_blog_details(
     1640            array(
     1641                'domain' => $new_domain,
     1642                'path'   => '/',
     1643            )
     1644        );
     1645        get_blog_id_from_url( $new_domain, '/' );
     1646
     1647        wp_update_site(
     1648            $site->id,
     1649            array(
     1650                'domain' => $new_domain,
     1651            )
     1652        );
     1653
     1654        $domain_path_key_old = md5( $old_domain . '/' );
     1655        $domain_path_key_new = md5( $new_domain . '/' );
     1656
     1657        // Ensure all respective cache values are empty.
     1658        $result = array(
     1659            wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1660            wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1661            wp_cache_get( 'current_blog_' . $old_domain, 'site-options' ),
     1662            wp_cache_get( 'current_blog_' . $old_domain . '/', 'site-options' ),
     1663            wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1664            wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1665            wp_cache_get( 'current_blog_' . $new_domain, 'site-options' ),
     1666            wp_cache_get( 'current_blog_' . $new_domain . '/', 'site-options' ),
     1667        );
     1668
     1669        $this->assertEmpty( array_filter( $result ) );
     1670    }
     1671
     1672    /**
     1673     * @ticket 40364
     1674     */
     1675    public function test_wp_update_site_cleans_old_cache_on_path_change() {
     1676        $old_path = '/foo/';
     1677        $new_path = '/bar/';
     1678
     1679        $site = self::factory()->blog->create_and_get(
     1680            array(
     1681                'domain' => 'test.wordpress.org',
     1682                'path'   => $old_path,
     1683            )
     1684        );
     1685
     1686        // Populate the caches.
     1687        get_blog_details(
     1688            array(
     1689                'domain' => 'test.wordpress.org',
     1690                'path'   => $old_path,
     1691            )
     1692        );
     1693        get_blog_id_from_url( 'test.wordpress.org', $old_path );
     1694        get_blog_details(
     1695            array(
     1696                'domain' => 'test.wordpress.org',
     1697                'path'   => $new_path,
     1698            )
     1699        );
     1700        get_blog_id_from_url( 'test.wordpress.org', $new_path );
     1701
     1702        wp_update_site(
     1703            $site->id,
     1704            array(
     1705                'path' => $new_path,
     1706            )
     1707        );
     1708
     1709        $domain_path_key_old = md5( 'test.wordpress.org' . $old_path );
     1710        $domain_path_key_new = md5( 'test.wordpress.org' . $new_path );
     1711
     1712        // Ensure all respective cache values are empty.
     1713        $result = array(
     1714            wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1715            wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1716            wp_cache_get( 'current_blog_test.wordpress.org' . $old_path, 'site-options' ),
     1717            wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1718            wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1719            wp_cache_get( 'current_blog_test.wordpress.org' . $new_path, 'site-options' ),
     1720        );
     1721
     1722        $this->assertEmpty( array_filter( $result ) );
     1723    }
     1724
     1725    /**
     1726     * @ticket 40364
     1727     * @dataProvider data_site_status_hook_triggers
     1728     */
     1729    public function test_site_status_hook_triggers( $insert_site_data, $expected_insert_hooks, $update_site_data, $expected_update_hooks ) {
     1730        // First: Insert a site.
     1731        $this->listen_to_site_status_hooks();
     1732
     1733        $site_data = array_merge(
     1734            array(
     1735                'domain' => 'example-site.com',
     1736                'path'   => '/',
     1737            ),
     1738            $insert_site_data
     1739        );
     1740
     1741        $site_id = wp_insert_site( $site_data );
     1742
     1743        $insert_expected = array_fill_keys( $expected_insert_hooks, $site_id );
     1744        $insert_result   = $this->get_listen_to_site_status_hooks_result();
     1745
     1746        // Second: Update that site.
     1747        $this->listen_to_site_status_hooks();
     1748
     1749        wp_update_site( $site_id, $update_site_data );
     1750
     1751        $update_expected = array_fill_keys( $expected_update_hooks, $site_id );
     1752        $update_result   = $this->get_listen_to_site_status_hooks_result();
     1753
     1754        // Check both insert and update results.
     1755        $this->assertSameSetsWithIndex( $insert_expected, $insert_result );
     1756        $this->assertSameSetsWithIndex( $update_expected, $update_result );
     1757    }
     1758
     1759    public function data_site_status_hook_triggers() {
     1760        return array(
     1761            array(
     1762                array(
     1763                    'public'   => 1,
     1764                    'archived' => 1,
     1765                    'mature'   => 1,
     1766                    'spam'     => 1,
     1767                    'deleted'  => 1,
     1768                ),
     1769                array(
     1770                    'archive_blog',
     1771                    'mature_blog',
     1772                    'make_spam_blog',
     1773                    'make_delete_blog',
     1774                ),
     1775                array(
     1776                    'public'   => 0,
     1777                    'archived' => 0,
     1778                    'mature'   => 0,
     1779                    'spam'     => 0,
     1780                    'deleted'  => 0,
     1781                ),
     1782                array(
     1783                    'update_blog_public',
     1784                    'unarchive_blog',
     1785                    'unmature_blog',
     1786                    'make_ham_blog',
     1787                    'make_undelete_blog',
     1788                ),
     1789            ),
     1790            array(
     1791                array(
     1792                    'public'   => 0,
     1793                    'archived' => 0,
     1794                    'mature'   => 0,
     1795                    'spam'     => 0,
     1796                    'deleted'  => 0,
     1797                ),
     1798                array(
     1799                    'update_blog_public',
     1800                ),
     1801                array(
     1802                    'public'   => 1,
     1803                    'archived' => 1,
     1804                    'mature'   => 1,
     1805                    'spam'     => 1,
     1806                    'deleted'  => 1,
     1807                ),
     1808                array(
     1809                    'update_blog_public',
     1810                    'archive_blog',
     1811                    'mature_blog',
     1812                    'make_spam_blog',
     1813                    'make_delete_blog',
     1814                ),
     1815            ),
     1816            array(
     1817                array(
     1818                    'public'   => 0,
     1819                    'archived' => 0,
     1820                    'mature'   => 1,
     1821                    'spam'     => 1,
     1822                    'deleted'  => 1,
     1823                ),
     1824                array(
     1825                    'update_blog_public',
     1826                    'mature_blog',
     1827                    'make_spam_blog',
     1828                    'make_delete_blog',
     1829                ),
     1830                array(
     1831                    'public'   => 0,
     1832                    'archived' => 1,
     1833                    'mature'   => 1,
     1834                    'spam'     => 1,
     1835                    'deleted'  => 0,
     1836                ),
     1837                array(
     1838                    'archive_blog',
     1839                    'make_undelete_blog',
     1840                ),
     1841            ),
     1842        );
     1843    }
     1844
     1845    private function listen_to_site_status_hooks() {
     1846        $this->site_status_hooks = array();
     1847
     1848        $hooknames = array(
     1849            'make_spam_blog',
     1850            'make_ham_blog',
     1851            'mature_blog',
     1852            'unmature_blog',
     1853            'archive_blog',
     1854            'unarchive_blog',
     1855            'make_delete_blog',
     1856            'make_undelete_blog',
     1857            'update_blog_public',
     1858        );
     1859
     1860        foreach ( $hooknames as $hookname ) {
     1861            add_action( $hookname, array( $this, 'action_site_status_hook' ), 10, 1 );
    3011862        }
    302 
    303         /**
    304          * When the main site of a network is fully deleted, its data should be cleared from cache.
    305          */
    306         public function test_data_in_cache_after_wpmu_delete_blog_main_site_drop_true() {
    307             $blog_id = 1; // The main site in our test suite has an ID of 1.
    308 
    309             $details = get_blog_details( $blog_id, false );
    310             $key     = md5( $details->domain . $details->path );
    311 
    312             // Delete the site and force a table drop.
    313             wpmu_delete_blog( $blog_id, true );
    314 
    315             $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
    316             $this->assertFalse( wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    317             $this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
    318             $this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
     1863    }
     1864
     1865    private function get_listen_to_site_status_hooks_result() {
     1866        $hooknames = array(
     1867            'make_spam_blog',
     1868            'make_ham_blog',
     1869            'mature_blog',
     1870            'unmature_blog',
     1871            'archive_blog',
     1872            'unarchive_blog',
     1873            'make_delete_blog',
     1874            'make_undelete_blog',
     1875            'update_blog_public',
     1876        );
     1877
     1878        foreach ( $hooknames as $hookname ) {
     1879            remove_action( $hookname, array( $this, 'action_site_status_hook' ), 10 );
    3191880        }
    3201881
    321         /**
    322          * When the main site of a network is fully deleted, its data should remain in the database.
    323          */
    324         public function test_data_in_tables_after_wpmu_delete_blog_main_site_drop_true() {
    325             global $wpdb;
    326 
    327             $blog_id = 1; // The main site in our test suite has an ID of 1.
    328 
    329             // Delete the site and force a table drop.
    330             wpmu_delete_blog( $blog_id, true );
    331 
    332             $prefix = $wpdb->get_blog_prefix( $blog_id );
    333             foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    334                 $suppress = $wpdb->suppress_errors();
    335 
    336                 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    337                 $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    338 
    339                 $wpdb->suppress_errors( $suppress );
    340                 $this->assertNotEmpty( $table_fields, $prefix . $table );
    341             }
     1882        return $this->site_status_hooks;
     1883    }
     1884
     1885    public function action_site_status_hook( $site_id ) {
     1886        $this->site_status_hooks[ current_action() ] = $site_id;
     1887    }
     1888
     1889    /**
     1890     * @ticket 41333
     1891     * @dataProvider data_wp_initialize_site
     1892     */
     1893    public function test_wp_initialize_site( $args, $expected_options, $expected_meta ) {
     1894        $result = wp_initialize_site( self::$uninitialized_site_id, $args );
     1895
     1896        switch_to_blog( self::$uninitialized_site_id );
     1897
     1898        $options = array();
     1899        foreach ( $expected_options as $option => $value ) {
     1900            $options[ $option ] = get_option( $option );
    3421901        }
    3431902
    344         /**
    345          * The site count of a network should change when a site is flagged as 'deleted'.
    346          */
    347         public function test_network_count_after_wpmu_delete_blog_drop_false() {
    348             $blog_id = self::factory()->blog->create();
    349 
    350             // Delete the site without forcing a table drop.
    351             wpmu_delete_blog( $blog_id, false );
    352 
    353             // Update the blog count cache to use get_blog_count().
    354             wp_update_network_counts();
    355             $this->assertSame( 1, get_blog_count() );
     1903        $meta = array();
     1904        foreach ( $expected_meta as $meta_key => $value ) {
     1905            $meta[ $meta_key ] = get_site_meta( self::$uninitialized_site_id, $meta_key, true );
    3561906        }
    3571907
    358         /**
    359          * The site count of a network should change when a site is fully deleted.
    360          */
    361         public function test_blog_count_after_wpmu_delete_blog_drop_true() {
    362             $blog_id = self::factory()->blog->create();
    363 
    364             // Delete the site and force a table drop.
    365             wpmu_delete_blog( $blog_id, true );
    366 
    367             // Update the blog count cache to use get_blog_count().
    368             wp_update_network_counts();
    369             $this->assertSame( 1, get_blog_count() );
    370         }
    371 
    372         /**
    373          * When a site is deleted with wpmu_delete_blog(), only the files associated with
    374          * that site should be removed. When wpmu_delete_blog() is run a second time, nothing
    375          * should change with upload directories.
    376          */
    377         public function test_upload_directories_after_multiple_wpmu_delete_blog() {
    378             $filename = __FUNCTION__ . '.jpg';
    379             $contents = __FUNCTION__ . '_contents';
    380 
    381             // Upload a file to the main site on the network.
    382             $file1 = wp_upload_bits( $filename, null, $contents );
    383 
    384             $blog_id = self::factory()->blog->create();
    385 
    386             switch_to_blog( $blog_id );
    387             $file2 = wp_upload_bits( $filename, null, $contents );
    388             restore_current_blog();
    389 
    390             wpmu_delete_blog( $blog_id, true );
    391 
    392             // The file on the main site should still exist. The file on the deleted site should not.
    393             $this->assertFileExists( $file1['file'] );
    394             $this->assertFileDoesNotExist( $file2['file'] );
    395 
    396             wpmu_delete_blog( $blog_id, true );
    397 
    398             // The file on the main site should still exist. The file on the deleted site should not.
    399             $this->assertFileExists( $file1['file'] );
    400             $this->assertFileDoesNotExist( $file2['file'] );
    401 
    402             unlink( $file1['file'] );
    403         }
    404 
    405         public function test_wpmu_update_blogs_date() {
    406             global $wpdb;
    407 
    408             wpmu_update_blogs_date();
    409 
    410             $blog         = get_site( get_current_blog_id() );
    411             $current_time = time();
    412 
    413             // Compare the update time with the current time, allow delta < 2.
    414             $this->assertEqualsWithDelta( $current_time, strtotime( $blog->last_updated ), 2, 'The dates should be equal' );
    415         }
    416 
    417         /**
    418          * Test cached data for a site that does not exist and then again after it exists.
    419          *
    420          * @ticket 23405
    421          */
    422         public function test_get_blog_details_when_site_does_not_exist() {
    423             // Create an unused site so that we can then assume an invalid site ID.
    424             $blog_id = self::factory()->blog->create();
    425             ++$blog_id;
    426 
    427             // Prime the cache for an invalid site.
    428             get_blog_details( $blog_id );
    429 
    430             // When the cache is primed with an invalid site, the value is set to -1.
    431             $this->assertSame( -1, wp_cache_get( $blog_id, 'blog-details' ) );
    432 
    433             // Create a site in the invalid site's place.
    434             self::factory()->blog->create();
    435 
    436             // When a new site is created, its cache is cleared through refresh_blog_details.
    437             $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
    438 
    439             $blog = get_blog_details( $blog_id );
    440 
    441             // When the cache is refreshed, it should now equal the site data.
    442             $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
    443         }
    444 
    445         /**
    446          * @ticket 26410
    447          */
    448         public function test_blog_details_cache_invalidation() {
    449             update_option( 'blogname', 'foo' );
    450             $details = get_site( get_current_blog_id() );
    451             $this->assertSame( 'foo', $details->blogname );
    452 
    453             update_option( 'blogname', 'bar' );
    454             $details = get_site( get_current_blog_id() );
    455             $this->assertSame( 'bar', $details->blogname );
    456         }
    457 
    458         /**
    459          * Test the original and cached responses for a created and then deleted site when
    460          * the blog ID is requested through get_blog_id_from_url().
    461          */
    462         public function test_get_blog_id_from_url() {
    463             $blog_id = self::factory()->blog->create();
    464             $details = get_site( $blog_id );
    465             $key     = md5( $details->domain . $details->path );
    466 
    467             // Test the original response and cached response for the newly created site.
    468             $this->assertSame( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
    469             $this->assertSame( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
    470         }
    471 
    472         /**
    473          * Test the case insensitivity of the site lookup.
    474          */
    475         public function test_get_blog_id_from_url_is_case_insensitive() {
    476             $blog_id = self::factory()->blog->create(
    477                 array(
    478                     'domain' => 'example.com',
    479                     'path'   => '/xyz',
    480                 )
    481             );
    482             $details = get_site( $blog_id );
    483 
    484             $this->assertSame( $blog_id, get_blog_id_from_url( strtoupper( $details->domain ), strtoupper( $details->path ) ) );
    485         }
    486 
    487         /**
    488          * Test the first and cached responses for a site that does not exist.
    489          */
    490         public function test_get_blog_id_from_url_that_does_not_exist() {
    491             $blog_id = self::factory()->blog->create( array( 'path' => '/xyz' ) );
    492             $details = get_site( $blog_id );
    493 
    494             $this->assertSame( 0, get_blog_id_from_url( $details->domain, 'foo' ) );
    495             $this->assertSame( -1, wp_cache_get( md5( $details->domain . 'foo' ), 'blog-id-cache' ) );
    496         }
    497 
    498         /**
    499          * A blog ID is still available if only the `deleted` flag is set for a site. The same
    500          * behavior would be expected if passing `false` explicitly to `wpmu_delete_blog()`.
    501          */
    502         public function test_get_blog_id_from_url_with_deleted_flag() {
    503             $blog_id = self::factory()->blog->create();
    504             $details = get_site( $blog_id );
    505             $key     = md5( $details->domain . $details->path );
    506             wpmu_delete_blog( $blog_id );
    507 
    508             $this->assertSame( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
    509             $this->assertSame( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
    510         }
    511 
    512         /**
    513          * When deleted with the drop parameter as true, the cache will first be false, then set to
    514          * -1 after an attempt at `get_blog_id_from_url()` is made.
    515          */
    516         public function test_get_blog_id_from_url_after_dropped() {
    517             $blog_id = self::factory()->blog->create();
    518             $details = get_site( $blog_id );
    519             $key     = md5( $details->domain . $details->path );
    520             wpmu_delete_blog( $blog_id, true );
    521 
    522             $this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
    523             $this->assertSame( 0, get_blog_id_from_url( $details->domain, $details->path ) );
    524             $this->assertSame( -1, wp_cache_get( $key, 'blog-id-cache' ) );
    525         }
    526 
    527         /**
    528          * Test with default parameter of site_id as null.
    529          */
    530         public function test_is_main_site() {
    531             $this->assertTrue( is_main_site() );
    532         }
    533 
    534         /**
    535          * Test with a site id of get_current_blog_id(), which should be the same as the
    536          * default parameter tested above.
    537          */
    538         public function test_current_blog_id_is_main_site() {
    539             $this->assertTrue( is_main_site( get_current_blog_id() ) );
    540         }
    541 
    542         /**
    543          * Test with a site ID other than the main site to ensure a false response.
    544          */
    545         public function test_is_main_site_is_false_with_other_blog_id() {
    546             $blog_id = self::factory()->blog->create();
    547 
    548             $this->assertFalse( is_main_site( $blog_id ) );
    549         }
    550 
    551         /**
    552          * Test with no passed ID after switching to another site ID.
    553          */
    554         public function test_is_main_site_is_false_after_switch_to_blog() {
    555             $blog_id = self::factory()->blog->create();
    556             switch_to_blog( $blog_id );
    557 
    558             $this->assertFalse( is_main_site() );
    559 
    560             restore_current_blog();
    561         }
    562 
    563         public function test_switch_upload_dir() {
    564             $this->assertTrue( is_main_site() );
    565 
    566             $site = get_current_site();
    567             $date = date_format( date_create( 'now' ), 'Y/m' );
    568 
    569             $info = wp_upload_dir();
    570             $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] );
    571             $this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] );
    572             $this->assertSame( '/' . $date, $info['subdir'] );
    573             $this->assertFalse( $info['error'] );
    574 
    575             $blog_id = self::factory()->blog->create();
    576 
    577             switch_to_blog( $blog_id );
    578             $info = wp_upload_dir();
    579             $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . $date, $info['url'] );
    580             $this->assertSame( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . $date, $info['path'] );
    581             $this->assertSame( '/' . $date, $info['subdir'] );
    582             $this->assertFalse( $info['error'] );
    583             restore_current_blog();
    584 
    585             $info = wp_upload_dir();
    586             $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] );
    587             $this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] );
    588             $this->assertSame( '/' . $date, $info['subdir'] );
    589             $this->assertFalse( $info['error'] );
    590         }
    591 
    592         /**
    593          * Test the primary purpose of get_blog_post(), to retrieve a post from
    594          * another site on the network.
    595          */
    596         public function test_get_blog_post_from_another_site_on_network() {
    597             $blog_id = self::factory()->blog->create();
    598             $post_id = self::factory()->post->create(); // Create a post on the primary site, ID 1.
    599             $post    = get_post( $post_id );
    600             switch_to_blog( $blog_id );
    601 
    602             // The post created and retrieved on the main site should match the one retrieved "remotely".
    603             $this->assertEquals( $post, get_blog_post( 1, $post_id ) );
    604 
    605             restore_current_blog();
    606         }
    607 
    608         /**
    609          * If get_blog_post() is used on the same site, it should still work.
    610          */
    611         public function test_get_blog_post_from_same_site() {
    612             $post_id = self::factory()->post->create();
    613 
    614             $this->assertEquals( get_blog_post( 1, $post_id ), get_post( $post_id ) );
    615         }
    616 
    617         /**
    618          * A null response should be returned if an invalid post is requested.
    619          */
    620         public function test_get_blog_post_invalid_returns_null() {
    621             $this->assertNull( get_blog_post( 1, 999999 ) );
    622         }
    623 
    624         /**
    625          * Added as a callback to the domain_exists filter to provide manual results for
    626          * the testing of the filter and for a test which does not need the database.
    627          */
    628         public function domain_exists_cb( $exists, $domain, $path, $site_id ) {
    629             if ( 'foo' === $domain && 'bar/' === $path ) {
    630                 return 1234;
    631             } else {
    632                 return null;
    633             }
    634         }
    635 
    636         public function test_domain_exists_with_default_site_id() {
    637             $details = get_site( 1 );
    638 
    639             $this->assertSame( 1, domain_exists( $details->domain, $details->path ) );
    640         }
    641 
    642         public function test_domain_exists_with_specified_site_id() {
    643             $details = get_site( 1 );
    644 
    645             $this->assertSame( 1, domain_exists( $details->domain, $details->path, $details->site_id ) );
    646         }
    647 
    648         /**
    649          * When the domain is valid, but the resulting site does not belong to the specified network,
    650          * it is marked as not existing.
    651          */
    652         public function test_domain_does_not_exist_with_invalid_site_id() {
    653             $details = get_site( 1 );
    654 
    655             $this->assertNull( domain_exists( $details->domain, $details->path, 999 ) );
    656         }
    657 
    658         public function test_invalid_domain_does_not_exist_with_default_site_id() {
    659             $this->assertNull( domain_exists( 'foo', 'bar' ) );
    660         }
    661 
    662         public function test_domain_filtered_to_exist() {
    663             add_filter( 'domain_exists', array( $this, 'domain_exists_cb' ), 10, 4 );
    664             $exists = domain_exists( 'foo', 'bar' );
    665             remove_filter( 'domain_exists', array( $this, 'domain_exists_cb' ), 10, 4 );
    666             $this->assertSame( 1234, $exists );
    667         }
    668 
    669         /**
    670          * When a path is passed to domain_exists, it is immediately trailing slashed. A path
    671          * value with or without the slash should result in the same return value.
    672          */
    673         public function test_slashed_path_in_domain_exists() {
    674             add_filter( 'domain_exists', array( $this, 'domain_exists_cb' ), 10, 4 );
    675             $exists1 = domain_exists( 'foo', 'bar' );
    676             $exists2 = domain_exists( 'foo', 'bar/' );
    677             remove_filter( 'domain_exists', array( $this, 'domain_exists_cb' ), 10, 4 );
    678 
    679             // Make sure the same result is returned with or without a trailing slash.
    680             $this->assertSame( $exists1, $exists2 );
    681         }
    682 
    683         /**
    684          * Tests returning an address for a given valid ID.
    685          */
    686         public function test_get_blogaddress_by_id_with_valid_id() {
    687             $blogaddress = get_blogaddress_by_id( 1 );
    688             $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/', $blogaddress );
    689         }
    690 
    691         /**
    692          * Tests returning an empty string for a non-existing ID.
    693          */
    694         public function test_get_blogaddress_by_id_with_invalid_id() {
    695             $blogaddress = get_blogaddress_by_id( PHP_INT_MAX );
    696             $this->assertSame( '', $blogaddress );
    697         }
    698 
    699         /**
    700          * @ticket 14867
    701          */
    702         public function test_get_blogaddress_by_id_scheme_reflects_blog_scheme() {
    703             $blog = self::factory()->blog->create();
    704 
    705             $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
    706 
    707             update_blog_option( $blog, 'home', set_url_scheme( get_blog_option( $blog, 'home' ), 'https' ) );
    708 
    709             $this->assertSame( 'https', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
    710         }
    711 
    712         /**
    713          * @ticket 14867
    714          */
    715         public function test_get_blogaddress_by_id_scheme_is_unaffected_by_request() {
    716             $blog = self::factory()->blog->create();
    717 
    718             $this->assertFalse( is_ssl() );
    719             $this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
    720 
    721             $_SERVER['HTTPS'] = 'on';
    722 
    723             $is_ssl  = is_ssl();
    724             $address = parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME );
    725 
    726             $this->assertTrue( $is_ssl );
    727             $this->assertSame( 'http', $address );
    728         }
    729 
    730         /**
    731          * @ticket 33620
    732          * @dataProvider data_new_blog_url_schemes
    733          */
    734         public function test_new_blog_url_schemes( $home_scheme, $siteurl_scheme, $force_ssl_admin ) {
    735             $current_site = get_current_site();
    736 
    737             $home    = get_option( 'home' );
    738             $siteurl = get_site_option( 'siteurl' );
    739             $admin   = force_ssl_admin();
    740 
    741             // Setup:
    742             update_option( 'home', set_url_scheme( $home, $home_scheme ) );
    743             update_site_option( 'siteurl', set_url_scheme( $siteurl, $siteurl_scheme ) );
    744             force_ssl_admin( $force_ssl_admin );
    745 
    746             // Install:
    747             $new = wpmu_create_blog( $current_site->domain, '/new-blog/', 'New Blog', get_current_user_id() );
    748 
    749             // Reset:
    750             update_option( 'home', $home );
    751             update_site_option( 'siteurl', $siteurl );
    752             force_ssl_admin( $admin );
    753 
    754             // Assert:
    755             $this->assertNotWPError( $new );
    756             $this->assertSame( $home_scheme, parse_url( get_blog_option( $new, 'home' ), PHP_URL_SCHEME ) );
    757             $this->assertSame( $siteurl_scheme, parse_url( get_blog_option( $new, 'siteurl' ), PHP_URL_SCHEME ) );
    758         }
    759 
    760         public function data_new_blog_url_schemes() {
    761             return array(
    762                 array(
    763                     'https',
    764                     'https',
    765                     false,
    766                 ),
    767                 array(
    768                     'http',
    769                     'https',
    770                     false,
    771                 ),
    772                 array(
    773                     'https',
    774                     'http',
    775                     false,
    776                 ),
    777                 array(
    778                     'http',
    779                     'http',
    780                     false,
    781                 ),
    782                 array(
    783                     'http',
    784                     'http',
    785                     true,
    786                 ),
    787             );
    788         }
    789 
    790         /**
    791          * @ticket 36918
    792          */
    793         public function test_new_blog_locale() {
    794             $current_site = get_current_site();
    795 
    796             add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    797             update_site_option( 'WPLANG', 'de_DE' );
    798             remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    799 
    800             // No locale, use default locale.
    801             add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    802             $blog_id = wpmu_create_blog( $current_site->domain, '/de-de/', 'New Blog', get_current_user_id() );
    803             remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    804 
    805             $this->assertNotWPError( $blog_id );
    806             $this->assertSame( 'de_DE', get_blog_option( $blog_id, 'WPLANG' ) );
    807 
    808             // Custom locale.
    809             add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    810             $blog_id = wpmu_create_blog( $current_site->domain, '/es-es/', 'New Blog', get_current_user_id(), array( 'WPLANG' => 'es_ES' ) );
    811             remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    812 
    813             $this->assertNotWPError( $blog_id );
    814             $this->assertSame( 'es_ES', get_blog_option( $blog_id, 'WPLANG' ) );
    815 
    816             // en_US locale.
    817             add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    818             $blog_id = wpmu_create_blog( $current_site->domain, '/en-us/', 'New Blog', get_current_user_id(), array( 'WPLANG' => '' ) );
    819             remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    820 
    821             $this->assertNotWPError( $blog_id );
    822             $this->assertSame( '', get_blog_option( $blog_id, 'WPLANG' ) );
    823         }
    824 
    825         /**
    826          * @ticket 40503
    827          */
    828         public function test_different_network_language() {
    829             $network = get_network( self::$network_ids['make.wordpress.org/'] );
    830 
    831             add_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10, 3 );
    832 
    833             update_network_option( self::$network_ids['make.wordpress.org/'], 'WPLANG', 'wibble' );
    834             $blog_id = wpmu_create_blog( $network->domain, '/de-de/', 'New Blog', get_current_user_id(), array(), $network->id );
    835 
    836             remove_filter( 'sanitize_option_WPLANG', array( $this, 'filter_allow_unavailable_languages' ), 10 );
    837 
    838             $this->assertSame( get_network_option( self::$network_ids['make.wordpress.org/'], 'WPLANG' ), get_blog_option( $blog_id, 'WPLANG' ) );
    839         }
    840 
    841         /**
    842          * Allows to set the WPLANG option to any language.
    843          *
    844          * @param string $value          The sanitized option value.
    845          * @param string $option         The option name.
    846          * @param string $original_value The original value passed to the function.
    847          * @return string The original value.
    848          */
    849         public function filter_allow_unavailable_languages( $value, $option, $original_value ) {
    850             return $original_value;
    851         }
    852 
    853         /**
    854          * @ticket 29684
    855          */
    856         public function test_is_main_site_different_network() {
    857             $this->assertTrue( is_main_site( self::$site_ids['make.wordpress.org/'], self::$network_ids['make.wordpress.org/'] ) );
    858         }
    859 
    860         /**
    861          * @ticket 29684
    862          */
    863         public function test_is_main_site_different_network_random_site() {
    864             $this->assertFalse( is_main_site( self::$site_ids['make.wordpress.org/foo/'], self::$network_ids['make.wordpress.org/'] ) );
    865         }
    866 
    867         /**
    868          * @ticket 40201
    869          * @dataProvider data_get_site_caches
    870          */
    871         public function test_clean_blog_cache( $key, $group ) {
    872             $site = get_site( self::$site_ids['make.wordpress.org/'] );
    873 
    874             $replacements = array(
    875                 '%blog_id%'         => $site->blog_id,
    876                 '%domain%'          => $site->domain,
    877                 '%path%'            => $site->path,
    878                 '%domain_path_key%' => md5( $site->domain . $site->path ),
    879             );
    880 
    881             $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
    882 
    883             if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
    884                 wp_cache_set( $key, (object) $site->to_array(), $group );
    885             } else {
    886                 wp_cache_set( $key, 'something', $group );
    887             }
    888 
    889             clean_blog_cache( $site );
    890             $this->assertFalse( wp_cache_get( $key, $group ) );
    891         }
    892 
    893         /**
    894          * @ticket 40201
    895          * @dataProvider data_get_site_caches
    896          */
    897         public function test_clean_blog_cache_with_id( $key, $group ) {
    898             $site = get_site( self::$site_ids['make.wordpress.org/'] );
    899 
    900             $replacements = array(
    901                 '%blog_id%'         => $site->blog_id,
    902                 '%domain%'          => $site->domain,
    903                 '%path%'            => $site->path,
    904                 '%domain_path_key%' => md5( $site->domain . $site->path ),
    905             );
    906 
    907             $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
    908 
    909             if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
    910                 wp_cache_set( $key, (object) $site->to_array(), $group );
    911             } else {
    912                 wp_cache_set( $key, 'something', $group );
    913             }
    914 
    915             clean_blog_cache( $site->blog_id );
    916             $this->assertFalse( wp_cache_get( $key, $group ) );
    917         }
    918 
    919         /**
    920          * @ticket 40201
    921          */
    922         public function test_clean_blog_cache_resets_last_changed() {
    923             $site = get_site( self::$site_ids['make.wordpress.org/'] );
    924 
    925             wp_cache_delete( 'last_changed', 'sites' );
    926 
    927             clean_blog_cache( $site );
    928             $this->assertNotFalse( wp_cache_get( 'last_changed', 'sites' ) );
    929         }
    930 
    931         /**
    932          * @ticket 40201
    933          */
    934         public function test_clean_blog_cache_fires_action() {
    935             $site = get_site( self::$site_ids['make.wordpress.org/'] );
    936 
    937             $old_count = did_action( 'clean_site_cache' );
    938 
    939             clean_blog_cache( $site );
    940             $this->assertSame( $old_count + 1, did_action( 'clean_site_cache' ) );
    941         }
    942 
    943         /**
    944          * @ticket 40201
    945          */
    946         public function test_clean_blog_cache_bails_on_suspend_cache_invalidation() {
    947             $site = get_site( self::$site_ids['make.wordpress.org/'] );
    948 
    949             $old_count = did_action( 'clean_site_cache' );
    950 
    951             $suspend = wp_suspend_cache_invalidation();
    952             clean_blog_cache( $site );
    953             wp_suspend_cache_invalidation( $suspend );
    954             $this->assertSame( $old_count, did_action( 'clean_site_cache' ) );
    955         }
    956 
    957         /**
    958          * @ticket 40201
    959          */
    960         public function test_clean_blog_cache_bails_on_empty_input() {
    961             $old_count = did_action( 'clean_site_cache' );
    962 
    963             clean_blog_cache( null );
    964             $this->assertSame( $old_count, did_action( 'clean_site_cache' ) );
    965         }
    966 
    967         /**
    968          * @ticket 40201
    969          */
    970         public function test_clean_blog_cache_bails_on_non_numeric_input() {
    971             $old_count = did_action( 'clean_site_cache' );
    972 
    973             clean_blog_cache( 'something' );
    974             $this->assertSame( $old_count, did_action( 'clean_site_cache' ) );
    975         }
    976 
    977         /**
    978          * @ticket 40201
    979          */
    980         public function test_clean_blog_cache_works_with_deleted_site() {
    981             $site_id = 12345;
    982 
    983             wp_cache_set( $site_id, 'something', 'site-details' );
    984 
    985             clean_blog_cache( $site_id );
    986             $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
    987         }
    988 
    989         /**
    990          * @ticket 40201
    991          * @dataProvider data_get_site_caches
    992          */
    993         public function test_refresh_blog_details( $key, $group ) {
    994             $site = get_site( self::$site_ids['make.wordpress.org/'] );
    995 
    996             $replacements = array(
    997                 '%blog_id%'         => $site->blog_id,
    998                 '%domain%'          => $site->domain,
    999                 '%path%'            => $site->path,
    1000                 '%domain_path_key%' => md5( $site->domain . $site->path ),
    1001             );
    1002 
    1003             $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
    1004 
    1005             if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
    1006                 wp_cache_set( $key, (object) $site->to_array(), $group );
    1007             } else {
    1008                 wp_cache_set( $key, 'something', $group );
    1009             }
    1010 
    1011             refresh_blog_details( $site->blog_id );
    1012             $this->assertFalse( wp_cache_get( $key, $group ) );
    1013         }
    1014 
    1015         /**
    1016          * @ticket 40201
    1017          */
    1018         public function test_refresh_blog_details_works_with_deleted_site() {
    1019             $site_id = 12345;
    1020 
    1021             wp_cache_set( $site_id, 'something', 'site-details' );
    1022 
    1023             refresh_blog_details( $site_id );
    1024             $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
    1025         }
    1026 
    1027         /**
    1028          * @ticket 40201
    1029          */
    1030         public function test_refresh_blog_details_uses_current_site_as_default() {
    1031             $site_id = get_current_blog_id();
    1032 
    1033             wp_cache_set( $site_id, 'something', 'site-details' );
    1034 
    1035             refresh_blog_details();
    1036             $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
    1037         }
    1038 
    1039         public function data_get_site_caches() {
    1040             return array(
    1041                 array( '%blog_id%', 'sites' ),
    1042                 array( '%blog_id%', 'site-details' ),
    1043                 array( '%blog_id%', 'blog-details' ),
    1044                 array( '%blog_id%' . 'short', 'blog-details' ),
    1045                 array( '%domain_path_key%', 'blog-lookup' ),
    1046                 array( '%domain_path_key%', 'blog-id-cache' ),
    1047             );
    1048         }
    1049 
    1050         /**
    1051          * @ticket 40364
    1052          * @dataProvider data_wp_insert_site
    1053          */
    1054         public function test_wp_insert_site( $site_data, $expected_data ) {
    1055             remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
    1056             $site_id = wp_insert_site( $site_data );
    1057 
    1058             $this->assertIsInt( $site_id );
    1059 
    1060             $site = get_site( $site_id );
    1061             foreach ( $expected_data as $key => $value ) {
    1062                 $this->assertEquals( $value, $site->$key );
    1063             }
    1064         }
    1065 
    1066         public function data_wp_insert_site() {
    1067             return array(
    1068                 array(
    1069                     array(
    1070                         'domain' => 'example.com',
    1071                     ),
    1072                     array(
    1073                         'domain'     => 'example.com',
    1074                         'path'       => '/',
    1075                         'network_id' => 1,
    1076                         'public'     => 1,
    1077                         'archived'   => 0,
    1078                         'mature'     => 0,
    1079                         'spam'       => 0,
    1080                         'deleted'    => 0,
    1081                         'lang_id'    => 0,
    1082                     ),
    1083                 ),
    1084                 array(
    1085                     array(
    1086                         'domain'     => 'example.com',
    1087                         'path'       => '/foo',
    1088                         'network_id' => 2,
    1089                     ),
    1090                     array(
    1091                         'domain'     => 'example.com',
    1092                         'path'       => '/foo/',
    1093                         'network_id' => 2,
    1094                     ),
    1095                 ),
    1096                 array(
    1097                     array(
    1098                         'domain'  => 'example.com',
    1099                         'path'    => '/bar/',
    1100                         'site_id' => 2,
    1101                     ),
    1102                     array(
    1103                         'domain'     => 'example.com',
    1104                         'path'       => '/bar/',
    1105                         'network_id' => 2,
    1106                     ),
    1107                 ),
    1108                 array(
    1109                     array(
    1110                         'domain'     => 'example.com',
    1111                         'path'       => '/bar/',
    1112                         'site_id'    => 2,
    1113                         'network_id' => 3,
    1114                     ),
    1115                     array(
    1116                         'domain'     => 'example.com',
    1117                         'path'       => '/bar/',
    1118                         'network_id' => 3,
    1119                     ),
    1120                 ),
    1121                 array(
    1122                     array(
    1123                         'domain'   => 'example.com',
    1124                         'path'     => 'foobar',
    1125                         'public'   => 0,
    1126                         'archived' => 1,
    1127                         'mature'   => 1,
    1128                         'spam'     => 1,
    1129                         'deleted'  => 1,
    1130                         'lang_id'  => 1,
    1131                     ),
    1132                     array(
    1133                         'domain'   => 'example.com',
    1134                         'path'     => '/foobar/',
    1135                         'public'   => 0,
    1136                         'archived' => 1,
    1137                         'mature'   => 1,
    1138                         'spam'     => 1,
    1139                         'deleted'  => 1,
    1140                         'lang_id'  => 1,
    1141                     ),
    1142                 ),
    1143                 array(
    1144                     array(
    1145                         'domain' => 'example.com:8888',
    1146                     ),
    1147                     array(
    1148                         'domain'     => 'example.com:8888',
    1149                         'path'       => '/',
    1150                         'network_id' => 1,
    1151                         'public'     => 1,
    1152                         'archived'   => 0,
    1153                         'mature'     => 0,
    1154                         'spam'       => 0,
    1155                         'deleted'    => 0,
    1156                         'lang_id'    => 0,
    1157                     ),
    1158                 ),
    1159             );
    1160         }
    1161 
    1162         /**
    1163          * @ticket 50324
    1164          */
    1165         public function test_wp_insert_site_with_clean_site_cache() {
    1166             remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
    1167 
    1168             add_action( 'clean_site_cache', array( $this, 'action_database_insert_on_clean_site_cache' ) );
    1169 
    1170             $site_id = wp_insert_site(
    1171                 array(
    1172                     'domain'     => 'valid-domain.com',
    1173                     'path'       => '/valid-path/',
    1174                     'network_id' => 1,
    1175                 )
    1176             );
    1177 
    1178             remove_action( 'clean_site_cache', array( $this, 'action_database_insert_on_clean_site_cache' ) );
    1179 
    1180             $this->assertIsInt( $site_id );
    1181         }
    1182 
    1183         public function action_database_insert_on_clean_site_cache() {
    1184             update_site_option( 'database_write_test.' . time(), true );
    1185         }
    1186 
    1187         /**
    1188          * @ticket 40364
    1189          */
    1190         public function test_wp_insert_site_empty_domain() {
    1191             remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
    1192             $site_id = wp_insert_site( array( 'public' => 0 ) );
    1193 
    1194             $this->assertWPError( $site_id );
    1195             $this->assertSame( 'site_empty_domain', $site_id->get_error_code() );
    1196         }
    1197 
    1198         /**
    1199          * @ticket 40364
    1200          * @dataProvider data_wp_update_site
    1201          */
    1202         public function test_wp_update_site( $site_data, $expected_data ) {
    1203             $site_id = self::factory()->blog->create();
    1204 
    1205             $old_site = get_site( $site_id );
    1206 
    1207             $result = wp_update_site( $site_id, $site_data );
    1208 
    1209             $this->assertSame( $site_id, $result );
    1210 
    1211             $new_site = get_site( $site_id );
    1212             foreach ( $new_site->to_array() as $key => $value ) {
    1213                 if ( isset( $expected_data[ $key ] ) ) {
    1214                     $this->assertEquals( $expected_data[ $key ], $value );
    1215                 } elseif ( 'last_updated' === $key ) {
    1216                     $this->assertLessThanOrEqual( $value, $old_site->last_updated );
    1217                 } else {
    1218                     $this->assertSame( $old_site->$key, $value );
    1219                 }
    1220             }
    1221         }
    1222 
    1223         public function data_wp_update_site() {
    1224             return array(
    1225                 array(
    1226                     array(
    1227                         'domain'     => 'example.com',
    1228                         'network_id' => 2,
    1229                     ),
    1230                     array(
    1231                         'domain'  => 'example.com',
    1232                         'site_id' => 2,
    1233                     ),
    1234                 ),
    1235                 array(
    1236                     array(
    1237                         'path' => 'foo',
    1238                     ),
    1239                     array(
    1240                         'path' => '/foo/',
    1241                     ),
    1242                 ),
    1243                 array(
    1244                     array(
    1245                         'public'   => 0,
    1246                         'archived' => 1,
    1247                         'mature'   => 1,
    1248                         'spam'     => 1,
    1249                         'deleted'  => 1,
    1250                         'lang_id'  => 1,
    1251                     ),
    1252                     array(
    1253                         'public'   => 0,
    1254                         'archived' => 1,
    1255                         'mature'   => 1,
    1256                         'spam'     => 1,
    1257                         'deleted'  => 1,
    1258                         'lang_id'  => 1,
    1259                     ),
    1260                 ),
    1261                 array(
    1262                     array(
    1263                         'domain'     => 'example.com:8888',
    1264                         'network_id' => 2,
    1265                     ),
    1266                     array(
    1267                         'domain'  => 'example.com:8888',
    1268                         'site_id' => 2,
    1269                     ),
    1270                 ),
    1271             );
    1272         }
    1273 
    1274         /**
    1275          * @ticket 40364
    1276          */
    1277         public function test_wp_update_site_empty_domain() {
    1278             $site_id = self::factory()->blog->create();
    1279 
    1280             $result = wp_update_site( $site_id, array( 'domain' => '' ) );
    1281 
    1282             $this->assertWPError( $result );
    1283             $this->assertSame( 'site_empty_domain', $result->get_error_code() );
    1284         }
    1285 
    1286         /**
    1287          * @ticket 40364
    1288          */
    1289         public function test_wp_update_site_invalid_id() {
    1290             $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) );
    1291 
    1292             $this->assertWPError( $result );
    1293             $this->assertSame( 'site_not_exist', $result->get_error_code() );
    1294         }
    1295 
    1296         /**
    1297          * @ticket 40364
    1298          */
    1299         public function test_wp_update_site_cleans_cache() {
    1300             $site_id = self::factory()->blog->create();
    1301             $site1   = get_site( $site_id );
    1302 
    1303             $result = wp_update_site( $site_id, array( 'public' => 0 ) );
    1304             $site2  = get_site( $site_id );
    1305 
    1306             $result = wp_update_site( $site_id, array( 'public' => 1 ) );
    1307             $site3  = get_site( $site_id );
    1308 
    1309             $this->assertSame( '1', $site1->public );
    1310             $this->assertSame( '0', $site2->public );
    1311             $this->assertSame( '1', $site3->public );
    1312         }
    1313 
    1314         /**
    1315          * @ticket 40364
    1316          */
    1317         public function test_wp_delete_site() {
    1318             $site_id = self::factory()->blog->create();
    1319 
    1320             $site = get_site( $site_id );
    1321 
    1322             $result = wp_delete_site( $site_id );
    1323 
    1324             $this->assertInstanceOf( 'WP_Site', $result );
    1325             $this->assertSame( $result->to_array(), $site->to_array() );
    1326         }
    1327 
    1328         /**
    1329          * @ticket 40364
    1330          */
    1331         public function test_wp_delete_site_invalid_id() {
    1332             $result = wp_delete_site( 444444 );
    1333 
    1334             $this->assertWPError( $result );
    1335             $this->assertSame( 'site_not_exist', $result->get_error_code() );
    1336         }
    1337 
    1338         /**
    1339          * @ticket 41333
    1340          */
    1341         public function test_wp_delete_site_validate_site_deletion_action() {
    1342             add_action( 'wp_validate_site_deletion', array( $this, 'action_wp_validate_site_deletion_prevent_deletion' ) );
    1343             $result = wp_delete_site( self::$site_ids['make.wordpress.org/'] );
    1344             $this->assertWPError( $result );
    1345             $this->assertSame( 'action_does_not_like_deletion', $result->get_error_code() );
    1346         }
    1347 
    1348         public function action_wp_validate_site_deletion_prevent_deletion( $errors ) {
    1349             $errors->add( 'action_does_not_like_deletion', 'You cannot delete this site because the action does not like it.' );
    1350         }
    1351 
    1352         /**
    1353          * @ticket 40364
    1354          * @dataProvider data_wp_normalize_site_data
    1355          */
    1356         public function test_wp_normalize_site_data( $data, $expected ) {
    1357             $result = wp_normalize_site_data( $data );
    1358 
    1359             $this->assertSameSetsWithIndex( $expected, $result );
    1360         }
    1361 
    1362         public function data_wp_normalize_site_data() {
    1363             return array(
    1364                 array(
    1365                     array(
    1366                         'network_id' => '4',
    1367                     ),
    1368                     array(
    1369                         'network_id' => 4,
    1370                     ),
    1371                 ),
    1372                 array(
    1373                     array(
    1374                         'domain' => 'invalid domain .com',
    1375                         'path'   => 'foo',
    1376                     ),
    1377                     array(
    1378                         'domain' => 'invaliddomain.com',
    1379                         'path'   => '/foo/',
    1380                     ),
    1381                 ),
    1382                 array(
    1383                     array(
    1384                         'domain' => '<yet>/another-invalid-domain.com',
    1385                     ),
    1386                     array(
    1387                         'domain' => 'yetanother-invalid-domain.com',
    1388                     ),
    1389                 ),
    1390                 array(
    1391                     array(
    1392                         'domain' => 'with-port.com:8888',
    1393                     ),
    1394                     array(
    1395                         'domain' => 'with-port.com:8888',
    1396                     ),
    1397                 ),
    1398                 array(
    1399                     array(
    1400                         'domain' => 'subdomain.with-port.com:8888',
    1401                     ),
    1402                     array(
    1403                         'domain' => 'subdomain.with-port.com:8888',
    1404                     ),
    1405                 ),
    1406                 array(
    1407                     array(
    1408                         'path' => '',
    1409                     ),
    1410                     array(
    1411                         'path' => '/',
    1412                     ),
    1413                 ),
    1414                 array(
    1415                     array(
    1416                         'public'   => '0',
    1417                         'archived' => '1',
    1418                         'mature'   => '1',
    1419                         'spam'     => true,
    1420                         'deleted'  => true,
    1421                     ),
    1422                     array(
    1423                         'public'   => 0,
    1424                         'archived' => 1,
    1425                         'mature'   => 1,
    1426                         'spam'     => 1,
    1427                         'deleted'  => 1,
    1428                     ),
    1429                 ),
    1430                 array(
    1431                     array(
    1432                         'registered'   => '',
    1433                         'last_updated' => '',
    1434                     ),
    1435                     array(),
    1436                 ),
    1437                 array(
    1438                     array(
    1439                         'registered'   => '0000-00-00 00:00:00',
    1440                         'last_updated' => '0000-00-00 00:00:00',
    1441                     ),
    1442                     array(),
    1443                 ),
    1444             );
    1445         }
    1446 
    1447         /**
    1448          * @ticket 40364
    1449          * @dataProvider data_wp_validate_site_data
    1450          */
    1451         public function test_wp_validate_site_data( $data, $expected_errors ) {
    1452             $result = new WP_Error();
    1453             wp_validate_site_data( $result, $data );
    1454 
    1455             if ( empty( $expected_errors ) ) {
    1456                 $this->assertEmpty( $result->errors );
    1457             } else {
    1458                 $this->assertSameSets( $expected_errors, array_keys( $result->errors ) );
    1459             }
    1460         }
    1461 
    1462         public function data_wp_validate_site_data() {
    1463             $date = current_time( 'mysql', true );
    1464 
    1465             return array(
    1466                 array(
    1467                     array(
    1468                         'domain'       => 'example-site.com',
    1469                         'path'         => '/',
    1470                         'network_id'   => 1,
    1471                         'registered'   => $date,
    1472                         'last_updated' => $date,
    1473                     ),
    1474                     array(),
    1475                 ),
    1476                 array(
    1477                     array(
    1478                         'path'         => '/',
    1479                         'network_id'   => 1,
    1480                         'registered'   => $date,
    1481                         'last_updated' => $date,
    1482                     ),
    1483                     array( 'site_empty_domain' ),
    1484                 ),
    1485                 array(
    1486                     array(
    1487                         'domain'       => 'example-site.com',
    1488                         'network_id'   => 1,
    1489                         'registered'   => $date,
    1490                         'last_updated' => $date,
    1491                     ),
    1492                     array( 'site_empty_path' ),
    1493                 ),
    1494                 array(
    1495                     array(
    1496                         'domain'       => 'example-site.com',
    1497                         'path'         => '/',
    1498                         'registered'   => $date,
    1499                         'last_updated' => $date,
    1500                     ),
    1501                     array( 'site_empty_network_id' ),
    1502                 ),
    1503                 array(
    1504                     array(
    1505                         'domain'       => get_site()->domain,
    1506                         'path'         => get_site()->path,
    1507                         'network_id'   => get_site()->network_id,
    1508                         'registered'   => $date,
    1509                         'last_updated' => $date,
    1510                     ),
    1511                     array( 'site_taken' ),
    1512                 ),
    1513                 array(
    1514                     array(
    1515                         'domain'       => 'valid-domain.com',
    1516                         'path'         => '/valid-path/',
    1517                         'network_id'   => 1,
    1518                         'registered'   => '',
    1519                         'last_updated' => $date,
    1520                     ),
    1521                     array( 'site_empty_registered' ),
    1522                 ),
    1523                 array(
    1524                     array(
    1525                         'domain'       => 'valid-domain.com',
    1526                         'path'         => '/valid-path/',
    1527                         'network_id'   => 1,
    1528                         'registered'   => $date,
    1529                         'last_updated' => '',
    1530                     ),
    1531                     array( 'site_empty_last_updated' ),
    1532                 ),
    1533                 array(
    1534                     array(
    1535                         'domain'       => 'valid-domain.com',
    1536                         'path'         => '/valid-path/',
    1537                         'network_id'   => 1,
    1538                         'registered'   => '2000-13-32 25:25:61',
    1539                         'last_updated' => $date,
    1540                     ),
    1541                     array( 'site_invalid_registered' ),
    1542                 ),
    1543                 array(
    1544                     array(
    1545                         'domain'       => 'valid-domain.com',
    1546                         'path'         => '/valid-path/',
    1547                         'network_id'   => 1,
    1548                         'registered'   => $date,
    1549                         'last_updated' => '2000-13-32 25:25:61',
    1550                     ),
    1551                     array( 'site_invalid_last_updated' ),
    1552                 ),
    1553                 array(
    1554                     array(
    1555                         'domain'       => 'valid-domain.com',
    1556                         'path'         => '/valid-path/',
    1557                         'network_id'   => 1,
    1558                         'registered'   => '0000-00-00 00:00:00',
    1559                         'last_updated' => $date,
    1560                     ),
    1561                     array(),
    1562                 ),
    1563                 array(
    1564                     array(
    1565                         'domain'       => 'valid-domain.com',
    1566                         'path'         => '/valid-path/',
    1567                         'network_id'   => 1,
    1568                         'registered'   => $date,
    1569                         'last_updated' => '0000-00-00 00:00:00',
    1570                     ),
    1571                     array(),
    1572                 ),
    1573             );
    1574         }
    1575 
    1576         /**
    1577          * @ticket 40364
    1578          */
    1579         public function test_site_dates_are_gmt() {
    1580             $first_date = current_time( 'mysql', true );
    1581 
    1582             remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
    1583             $site_id = wp_insert_site(
    1584                 array(
    1585                     'domain'     => 'valid-domain.com',
    1586                     'path'       => '/valid-path/',
    1587                     'network_id' => 1,
    1588                 )
    1589             );
    1590             $this->assertIsInt( $site_id );
    1591 
    1592             $site = get_site( $site_id );
    1593             $this->assertEqualsWithDelta( strtotime( $first_date ), strtotime( $site->registered ), 2, 'The dates should be equal' );
    1594             $this->assertEqualsWithDelta( strtotime( $first_date ), strtotime( $site->last_updated ), 2, 'The dates should be equal' );
    1595 
    1596             $second_date = current_time( 'mysql', true );
    1597             $site_id     = wp_update_site( $site_id, array() );
    1598             $this->assertIsInt( $site_id );
    1599 
    1600             $site = get_site( $site_id );
    1601             $this->assertEqualsWithDelta( strtotime( $first_date ), strtotime( $site->registered ), 2, 'The dates should be equal' );
    1602             $this->assertEqualsWithDelta( strtotime( $second_date ), strtotime( $site->last_updated ), 2, 'The dates should be equal' );
    1603         }
    1604 
    1605         /**
    1606          * @ticket 40364
    1607          */
    1608         public function test_wp_delete_site_cleans_cache() {
    1609             $site_id = self::factory()->blog->create();
    1610 
    1611             get_site( $site_id );
    1612 
    1613             wp_delete_site( $site_id );
    1614 
    1615             $this->assertNull( get_site( $site_id ) );
    1616         }
    1617 
    1618         /**
    1619          * @ticket 40364
    1620          */
    1621         public function test_wp_update_site_cleans_old_cache_on_domain_change() {
    1622             $old_domain = 'old.wordpress.org';
    1623             $new_domain = 'new.wordpress.org';
    1624 
    1625             $site = self::factory()->blog->create_and_get(
    1626                 array(
    1627                     'domain' => $old_domain,
    1628                     'path'   => '/',
    1629                 )
    1630             );
    1631 
    1632             // Populate the caches.
    1633             get_blog_details(
    1634                 array(
    1635                     'domain' => $old_domain,
    1636                     'path'   => '/',
    1637                 )
    1638             );
    1639             get_blog_id_from_url( $old_domain, '/' );
    1640             get_blog_details(
    1641                 array(
    1642                     'domain' => $new_domain,
    1643                     'path'   => '/',
    1644                 )
    1645             );
    1646             get_blog_id_from_url( $new_domain, '/' );
    1647 
    1648             wp_update_site(
    1649                 $site->id,
    1650                 array(
    1651                     'domain' => $new_domain,
    1652                 )
    1653             );
    1654 
    1655             $domain_path_key_old = md5( $old_domain . '/' );
    1656             $domain_path_key_new = md5( $new_domain . '/' );
    1657 
    1658             // Ensure all respective cache values are empty.
    1659             $result = array(
    1660                 wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
    1661                 wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
    1662                 wp_cache_get( 'current_blog_' . $old_domain, 'site-options' ),
    1663                 wp_cache_get( 'current_blog_' . $old_domain . '/', 'site-options' ),
    1664                 wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
    1665                 wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
    1666                 wp_cache_get( 'current_blog_' . $new_domain, 'site-options' ),
    1667                 wp_cache_get( 'current_blog_' . $new_domain . '/', 'site-options' ),
    1668             );
    1669 
    1670             $this->assertEmpty( array_filter( $result ) );
    1671         }
    1672 
    1673         /**
    1674          * @ticket 40364
    1675          */
    1676         public function test_wp_update_site_cleans_old_cache_on_path_change() {
    1677             $old_path = '/foo/';
    1678             $new_path = '/bar/';
    1679 
    1680             $site = self::factory()->blog->create_and_get(
    1681                 array(
    1682                     'domain' => 'test.wordpress.org',
    1683                     'path'   => $old_path,
    1684                 )
    1685             );
    1686 
    1687             // Populate the caches.
    1688             get_blog_details(
    1689                 array(
    1690                     'domain' => 'test.wordpress.org',
    1691                     'path'   => $old_path,
    1692                 )
    1693             );
    1694             get_blog_id_from_url( 'test.wordpress.org', $old_path );
    1695             get_blog_details(
    1696                 array(
    1697                     'domain' => 'test.wordpress.org',
    1698                     'path'   => $new_path,
    1699                 )
    1700             );
    1701             get_blog_id_from_url( 'test.wordpress.org', $new_path );
    1702 
    1703             wp_update_site(
    1704                 $site->id,
    1705                 array(
    1706                     'path' => $new_path,
    1707                 )
    1708             );
    1709 
    1710             $domain_path_key_old = md5( 'test.wordpress.org' . $old_path );
    1711             $domain_path_key_new = md5( 'test.wordpress.org' . $new_path );
    1712 
    1713             // Ensure all respective cache values are empty.
    1714             $result = array(
    1715                 wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
    1716                 wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
    1717                 wp_cache_get( 'current_blog_test.wordpress.org' . $old_path, 'site-options' ),
    1718                 wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
    1719                 wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
    1720                 wp_cache_get( 'current_blog_test.wordpress.org' . $new_path, 'site-options' ),
    1721             );
    1722 
    1723             $this->assertEmpty( array_filter( $result ) );
    1724         }
    1725 
    1726         /**
    1727          * @ticket 40364
    1728          * @dataProvider data_site_status_hook_triggers
    1729          */
    1730         public function test_site_status_hook_triggers( $insert_site_data, $expected_insert_hooks, $update_site_data, $expected_update_hooks ) {
    1731             // First: Insert a site.
    1732             $this->listen_to_site_status_hooks();
    1733 
    1734             $site_data = array_merge(
    1735                 array(
    1736                     'domain' => 'example-site.com',
    1737                     'path'   => '/',
    1738                 ),
    1739                 $insert_site_data
    1740             );
    1741 
    1742             $site_id = wp_insert_site( $site_data );
    1743 
    1744             $insert_expected = array_fill_keys( $expected_insert_hooks, $site_id );
    1745             $insert_result   = $this->get_listen_to_site_status_hooks_result();
    1746 
    1747             // Second: Update that site.
    1748             $this->listen_to_site_status_hooks();
    1749 
    1750             wp_update_site( $site_id, $update_site_data );
    1751 
    1752             $update_expected = array_fill_keys( $expected_update_hooks, $site_id );
    1753             $update_result   = $this->get_listen_to_site_status_hooks_result();
    1754 
    1755             // Check both insert and update results.
    1756             $this->assertSameSetsWithIndex( $insert_expected, $insert_result );
    1757             $this->assertSameSetsWithIndex( $update_expected, $update_result );
    1758         }
    1759 
    1760         public function data_site_status_hook_triggers() {
    1761             return array(
    1762                 array(
    1763                     array(
    1764                         'public'   => 1,
    1765                         'archived' => 1,
    1766                         'mature'   => 1,
    1767                         'spam'     => 1,
    1768                         'deleted'  => 1,
    1769                     ),
    1770                     array(
    1771                         'archive_blog',
    1772                         'mature_blog',
    1773                         'make_spam_blog',
    1774                         'make_delete_blog',
    1775                     ),
    1776                     array(
    1777                         'public'   => 0,
    1778                         'archived' => 0,
    1779                         'mature'   => 0,
    1780                         'spam'     => 0,
    1781                         'deleted'  => 0,
    1782                     ),
    1783                     array(
    1784                         'update_blog_public',
    1785                         'unarchive_blog',
    1786                         'unmature_blog',
    1787                         'make_ham_blog',
    1788                         'make_undelete_blog',
    1789                     ),
    1790                 ),
    1791                 array(
    1792                     array(
    1793                         'public'   => 0,
    1794                         'archived' => 0,
    1795                         'mature'   => 0,
    1796                         'spam'     => 0,
    1797                         'deleted'  => 0,
    1798                     ),
    1799                     array(
    1800                         'update_blog_public',
    1801                     ),
    1802                     array(
    1803                         'public'   => 1,
    1804                         'archived' => 1,
    1805                         'mature'   => 1,
    1806                         'spam'     => 1,
    1807                         'deleted'  => 1,
    1808                     ),
    1809                     array(
    1810                         'update_blog_public',
    1811                         'archive_blog',
    1812                         'mature_blog',
    1813                         'make_spam_blog',
    1814                         'make_delete_blog',
    1815                     ),
    1816                 ),
    1817                 array(
    1818                     array(
    1819                         'public'   => 0,
    1820                         'archived' => 0,
    1821                         'mature'   => 1,
    1822                         'spam'     => 1,
    1823                         'deleted'  => 1,
    1824                     ),
    1825                     array(
    1826                         'update_blog_public',
    1827                         'mature_blog',
    1828                         'make_spam_blog',
    1829                         'make_delete_blog',
    1830                     ),
    1831                     array(
    1832                         'public'   => 0,
    1833                         'archived' => 1,
    1834                         'mature'   => 1,
    1835                         'spam'     => 1,
    1836                         'deleted'  => 0,
    1837                     ),
    1838                     array(
    1839                         'archive_blog',
    1840                         'make_undelete_blog',
    1841                     ),
    1842                 ),
    1843             );
    1844         }
    1845 
    1846         private function listen_to_site_status_hooks() {
    1847             $this->site_status_hooks = array();
    1848 
    1849             $hooknames = array(
    1850                 'make_spam_blog',
    1851                 'make_ham_blog',
    1852                 'mature_blog',
    1853                 'unmature_blog',
    1854                 'archive_blog',
    1855                 'unarchive_blog',
    1856                 'make_delete_blog',
    1857                 'make_undelete_blog',
    1858                 'update_blog_public',
    1859             );
    1860 
    1861             foreach ( $hooknames as $hookname ) {
    1862                 add_action( $hookname, array( $this, 'action_site_status_hook' ), 10, 1 );
    1863             }
    1864         }
    1865 
    1866         private function get_listen_to_site_status_hooks_result() {
    1867             $hooknames = array(
    1868                 'make_spam_blog',
    1869                 'make_ham_blog',
    1870                 'mature_blog',
    1871                 'unmature_blog',
    1872                 'archive_blog',
    1873                 'unarchive_blog',
    1874                 'make_delete_blog',
    1875                 'make_undelete_blog',
    1876                 'update_blog_public',
    1877             );
    1878 
    1879             foreach ( $hooknames as $hookname ) {
    1880                 remove_action( $hookname, array( $this, 'action_site_status_hook' ), 10 );
    1881             }
    1882 
    1883             return $this->site_status_hooks;
    1884         }
    1885 
    1886         public function action_site_status_hook( $site_id ) {
    1887             $this->site_status_hooks[ current_action() ] = $site_id;
    1888         }
    1889 
    1890         /**
    1891          * @ticket 41333
    1892          * @dataProvider data_wp_initialize_site
    1893          */
    1894         public function test_wp_initialize_site( $args, $expected_options, $expected_meta ) {
    1895             $result = wp_initialize_site( self::$uninitialized_site_id, $args );
    1896 
    1897             switch_to_blog( self::$uninitialized_site_id );
    1898 
    1899             $options = array();
    1900             foreach ( $expected_options as $option => $value ) {
    1901                 $options[ $option ] = get_option( $option );
    1902             }
    1903 
    1904             $meta = array();
    1905             foreach ( $expected_meta as $meta_key => $value ) {
    1906                 $meta[ $meta_key ] = get_site_meta( self::$uninitialized_site_id, $meta_key, true );
    1907             }
    1908 
    1909             restore_current_blog();
    1910 
    1911             $initialized = wp_is_site_initialized( self::$uninitialized_site_id );
    1912 
    1913             wp_uninitialize_site( self::$uninitialized_site_id );
    1914 
    1915             $this->assertTrue( $result );
    1916             $this->assertTrue( $initialized );
    1917             $this->assertSame( $expected_options, $options );
    1918             $this->assertSame( $expected_meta, $meta );
    1919         }
    1920 
    1921         public function data_wp_initialize_site() {
    1922             return array(
    1923                 array(
    1924                     array(),
    1925                     array(
    1926                         'home'        => 'http://uninitialized.org',
    1927                         'siteurl'     => 'http://uninitialized.org',
    1928                         'admin_email' => '',
    1929                         'blog_public' => '1',
    1930                     ),
    1931                     array(),
    1932                 ),
    1933                 array(
    1934                     array(
    1935                         'options' => array(
    1936                             'home'    => 'https://uninitialized.org',
    1937                             'siteurl' => 'https://uninitialized.org',
    1938                             'key'     => 'value',
    1939                         ),
    1940                         'meta'    => array(
    1941                             'key1' => 'value1',
    1942                             'key2' => 'value2',
    1943                         ),
    1944                     ),
    1945                     array(
     1908        restore_current_blog();
     1909
     1910        $initialized = wp_is_site_initialized( self::$uninitialized_site_id );
     1911
     1912        wp_uninitialize_site( self::$uninitialized_site_id );
     1913
     1914        $this->assertTrue( $result );
     1915        $this->assertTrue( $initialized );
     1916        $this->assertSame( $expected_options, $options );
     1917        $this->assertSame( $expected_meta, $meta );
     1918    }
     1919
     1920    public function data_wp_initialize_site() {
     1921        return array(
     1922            array(
     1923                array(),
     1924                array(
     1925                    'home'        => 'http://uninitialized.org',
     1926                    'siteurl'     => 'http://uninitialized.org',
     1927                    'admin_email' => '',
     1928                    'blog_public' => '1',
     1929                ),
     1930                array(),
     1931            ),
     1932            array(
     1933                array(
     1934                    'options' => array(
    19461935                        'home'    => 'https://uninitialized.org',
    19471936                        'siteurl' => 'https://uninitialized.org',
    19481937                        'key'     => 'value',
    19491938                    ),
    1950                     array(
     1939                    'meta'    => array(
    19511940                        'key1' => 'value1',
    19521941                        'key2' => 'value2',
    1953                         'key3' => '',
    19541942                    ),
    19551943                ),
    19561944                array(
    1957                     array(
    1958                         'title'   => 'My New Site',
    1959                         'options' => array(
    1960                             'blogdescription' => 'Just My New Site',
    1961                         ),
    1962                     ),
    1963                     array(
    1964                         'blogname'        => 'My New Site',
     1945                    'home'    => 'https://uninitialized.org',
     1946                    'siteurl' => 'https://uninitialized.org',
     1947                    'key'     => 'value',
     1948                ),
     1949                array(
     1950                    'key1' => 'value1',
     1951                    'key2' => 'value2',
     1952                    'key3' => '',
     1953                ),
     1954            ),
     1955            array(
     1956                array(
     1957                    'title'   => 'My New Site',
     1958                    'options' => array(
    19651959                        'blogdescription' => 'Just My New Site',
    19661960                    ),
    1967                     array(),
    1968                 ),
    1969             );
    1970         }
    1971 
    1972         /**
    1973          * @ticket 41333
     1961                ),
     1962                array(
     1963                    'blogname'        => 'My New Site',
     1964                    'blogdescription' => 'Just My New Site',
     1965                ),
     1966                array(),
     1967            ),
     1968        );
     1969    }
     1970
     1971    /**
     1972     * @ticket 41333
     1973     */
     1974    public function test_wp_initialize_site_user_roles() {
     1975        global $wpdb;
     1976
     1977        $result = wp_initialize_site( self::$uninitialized_site_id, array() );
     1978
     1979        switch_to_blog( self::$uninitialized_site_id );
     1980        $table_prefix = $wpdb->get_blog_prefix( self::$uninitialized_site_id );
     1981        $roles        = get_option( $table_prefix . 'user_roles' );
     1982        restore_current_blog();
     1983
     1984        wp_uninitialize_site( self::$uninitialized_site_id );
     1985
     1986        $this->assertTrue( $result );
     1987        $this->assertSameSets(
     1988            array(
     1989                'administrator',
     1990                'editor',
     1991                'author',
     1992                'contributor',
     1993                'subscriber',
     1994            ),
     1995            array_keys( $roles )
     1996        );
     1997    }
     1998
     1999    /**
     2000     * @ticket 41333
     2001     */
     2002    public function test_wp_initialize_site_user_is_admin() {
     2003        $result = wp_initialize_site( self::$uninitialized_site_id, array( 'user_id' => 1 ) );
     2004
     2005        switch_to_blog( self::$uninitialized_site_id );
     2006        $user_is_admin = user_can( 1, 'manage_options' );
     2007        $admin_email   = get_option( 'admin_email' );
     2008        restore_current_blog();
     2009
     2010        wp_uninitialize_site( self::$uninitialized_site_id );
     2011
     2012        $this->assertTrue( $result );
     2013        $this->assertTrue( $user_is_admin );
     2014        $this->assertSame( get_userdata( 1 )->user_email, $admin_email );
     2015    }
     2016
     2017    /**
     2018     * @ticket 41333
     2019     */
     2020    public function test_wp_initialize_site_args_filter() {
     2021        add_filter( 'wp_initialize_site_args', array( $this, 'filter_wp_initialize_site_args' ), 10, 3 );
     2022        $result = wp_initialize_site( self::$uninitialized_site_id, array( 'title' => 'My Site' ) );
     2023
     2024        switch_to_blog( self::$uninitialized_site_id );
     2025        $site_title = get_option( 'blogname' );
     2026        restore_current_blog();
     2027
     2028        wp_uninitialize_site( self::$uninitialized_site_id );
     2029
     2030        $this->assertSame(
     2031            sprintf( 'My Site %1$d in Network %2$d', self::$uninitialized_site_id, get_site( self::$uninitialized_site_id )->network_id ),
     2032            $site_title
     2033        );
     2034    }
     2035
     2036    public function filter_wp_initialize_site_args( $args, $site, $network ) {
     2037        $args['title'] = sprintf( 'My Site %1$d in Network %2$d', $site->id, $network->id );
     2038
     2039        return $args;
     2040    }
     2041
     2042    /**
     2043     * @ticket 41333
     2044     */
     2045    public function test_wp_initialize_site_empty_id() {
     2046        $result = wp_initialize_site( 0 );
     2047        $this->assertWPError( $result );
     2048        $this->assertSame( 'site_empty_id', $result->get_error_code() );
     2049    }
     2050
     2051    /**
     2052     * @ticket 41333
     2053     */
     2054    public function test_wp_initialize_site_invalid_id() {
     2055        $result = wp_initialize_site( 123 );
     2056        $this->assertWPError( $result );
     2057        $this->assertSame( 'site_invalid_id', $result->get_error_code() );
     2058    }
     2059
     2060    /**
     2061     * @ticket 41333
     2062     */
     2063    public function test_wp_initialize_site_already_initialized() {
     2064        $result = wp_initialize_site( get_current_blog_id() );
     2065        $this->assertWPError( $result );
     2066        $this->assertSame( 'site_already_initialized', $result->get_error_code() );
     2067    }
     2068
     2069    /**
     2070     * @ticket 41333
     2071     */
     2072    public function test_wp_uninitialize_site() {
     2073        $site_id = self::factory()->blog->create();
     2074
     2075        $result = wp_uninitialize_site( $site_id );
     2076        $this->assertTrue( $result );
     2077        $this->assertFalse( wp_is_site_initialized( $site_id ) );
     2078    }
     2079
     2080    /**
     2081     * @ticket 41333
     2082     */
     2083    public function test_wp_uninitialize_site_empty_id() {
     2084        $result = wp_uninitialize_site( 0 );
     2085        $this->assertWPError( $result );
     2086        $this->assertSame( 'site_empty_id', $result->get_error_code() );
     2087    }
     2088
     2089    /**
     2090     * @ticket 41333
     2091     */
     2092    public function test_wp_uninitialize_site_invalid_id() {
     2093        $result = wp_uninitialize_site( 123 );
     2094        $this->assertWPError( $result );
     2095        $this->assertSame( 'site_invalid_id', $result->get_error_code() );
     2096    }
     2097
     2098    /**
     2099     * @ticket 41333
     2100     */
     2101    public function test_wp_uninitialize_site_already_uninitialized() {
     2102        $result = wp_uninitialize_site( self::$uninitialized_site_id );
     2103        $this->assertWPError( $result );
     2104        $this->assertSame( 'site_already_uninitialized', $result->get_error_code() );
     2105    }
     2106
     2107    /**
     2108     * @ticket 41333
     2109     */
     2110    public function test_wp_is_site_initialized() {
     2111        $this->assertTrue( wp_is_site_initialized( get_current_blog_id() ) );
     2112        $this->assertFalse( wp_is_site_initialized( self::$uninitialized_site_id ) );
     2113    }
     2114
     2115    /**
     2116     * @ticket 41333
     2117     */
     2118    public function test_wp_is_site_initialized_prefilter() {
     2119        add_filter( 'pre_wp_is_site_initialized', '__return_false' );
     2120        $this->assertFalse( wp_is_site_initialized( get_current_blog_id() ) );
     2121
     2122        add_filter( 'pre_wp_is_site_initialized', '__return_true' );
     2123        $this->assertTrue( wp_is_site_initialized( self::$uninitialized_site_id ) );
     2124    }
     2125
     2126    /**
     2127     * @ticket 41333
     2128     */
     2129    public function test_wp_insert_site_forwards_args_to_wp_initialize_site() {
     2130        $args = array(
     2131            'user_id' => 1,
     2132            'title'   => 'My Site',
     2133            'options' => array( 'option1' => 'value1' ),
     2134            'meta'    => array( 'meta1' => 'value1' ),
     2135        );
     2136
     2137        add_filter( 'wp_initialize_site_args', array( $this, 'filter_wp_initialize_site_args_catch_args' ) );
     2138        $site_id = wp_insert_site(
     2139            array_merge(
     2140                array(
     2141                    'domain' => 'testsite.org',
     2142                    'path'   => '/',
     2143                ),
     2144                $args
     2145            )
     2146        );
     2147
     2148        $passed_args                   = $this->wp_initialize_site_args;
     2149        $this->wp_initialize_site_args = null;
     2150
     2151        $this->assertSameSetsWithIndex( $args, $passed_args );
     2152    }
     2153
     2154    public function filter_wp_initialize_site_args_catch_args( $args ) {
     2155        $this->wp_initialize_site_args = $args;
     2156
     2157        return $args;
     2158    }
     2159
     2160    /**
     2161     * @ticket 46125
     2162     */
     2163    public function test_wpmu_create_blog_cache_cleanup_backward_compatible() {
     2164        add_action( 'populate_options', array( $this, 'populate_options_callback' ) );
     2165
     2166        $blog_id = wpmu_create_blog( 'testsite1.example.org', '/test', 'test', 1, array( 'public' => 1 ), 2 );
     2167
     2168        /*
     2169         * Should not hit blog_details cache initialized in $this->populate_options_callback triggered during
     2170         * populate_options callback's call of get_blog_details.
    19742171         */
    1975         public function test_wp_initialize_site_user_roles() {
    1976             global $wpdb;
    1977 
    1978             $result = wp_initialize_site( self::$uninitialized_site_id, array() );
    1979 
    1980             switch_to_blog( self::$uninitialized_site_id );
    1981             $table_prefix = $wpdb->get_blog_prefix( self::$uninitialized_site_id );
    1982             $roles        = get_option( $table_prefix . 'user_roles' );
    1983             restore_current_blog();
    1984 
    1985             wp_uninitialize_site( self::$uninitialized_site_id );
    1986 
    1987             $this->assertTrue( $result );
    1988             $this->assertSameSets(
    1989                 array(
    1990                     'administrator',
    1991                     'editor',
    1992                     'author',
    1993                     'contributor',
    1994                     'subscriber',
    1995                 ),
    1996                 array_keys( $roles )
    1997             );
    1998         }
    1999 
    2000         /**
    2001          * @ticket 41333
    2002          */
    2003         public function test_wp_initialize_site_user_is_admin() {
    2004             $result = wp_initialize_site( self::$uninitialized_site_id, array( 'user_id' => 1 ) );
    2005 
    2006             switch_to_blog( self::$uninitialized_site_id );
    2007             $user_is_admin = user_can( 1, 'manage_options' );
    2008             $admin_email   = get_option( 'admin_email' );
    2009             restore_current_blog();
    2010 
    2011             wp_uninitialize_site( self::$uninitialized_site_id );
    2012 
    2013             $this->assertTrue( $result );
    2014             $this->assertTrue( $user_is_admin );
    2015             $this->assertSame( get_userdata( 1 )->user_email, $admin_email );
    2016         }
    2017 
    2018         /**
    2019          * @ticket 41333
    2020          */
    2021         public function test_wp_initialize_site_args_filter() {
    2022             add_filter( 'wp_initialize_site_args', array( $this, 'filter_wp_initialize_site_args' ), 10, 3 );
    2023             $result = wp_initialize_site( self::$uninitialized_site_id, array( 'title' => 'My Site' ) );
    2024 
    2025             switch_to_blog( self::$uninitialized_site_id );
    2026             $site_title = get_option( 'blogname' );
    2027             restore_current_blog();
    2028 
    2029             wp_uninitialize_site( self::$uninitialized_site_id );
    2030 
    2031             $this->assertSame(
    2032                 sprintf( 'My Site %1$d in Network %2$d', self::$uninitialized_site_id, get_site( self::$uninitialized_site_id )->network_id ),
    2033                 $site_title
    2034             );
    2035         }
    2036 
    2037         public function filter_wp_initialize_site_args( $args, $site, $network ) {
    2038             $args['title'] = sprintf( 'My Site %1$d in Network %2$d', $site->id, $network->id );
    2039 
    2040             return $args;
    2041         }
    2042 
    2043         /**
    2044          * @ticket 41333
    2045          */
    2046         public function test_wp_initialize_site_empty_id() {
    2047             $result = wp_initialize_site( 0 );
    2048             $this->assertWPError( $result );
    2049             $this->assertSame( 'site_empty_id', $result->get_error_code() );
    2050         }
    2051 
    2052         /**
    2053          * @ticket 41333
    2054          */
    2055         public function test_wp_initialize_site_invalid_id() {
    2056             $result = wp_initialize_site( 123 );
    2057             $this->assertWPError( $result );
    2058             $this->assertSame( 'site_invalid_id', $result->get_error_code() );
    2059         }
    2060 
    2061         /**
    2062          * @ticket 41333
    2063          */
    2064         public function test_wp_initialize_site_already_initialized() {
    2065             $result = wp_initialize_site( get_current_blog_id() );
    2066             $this->assertWPError( $result );
    2067             $this->assertSame( 'site_already_initialized', $result->get_error_code() );
    2068         }
    2069 
    2070         /**
    2071          * @ticket 41333
    2072          */
    2073         public function test_wp_uninitialize_site() {
    2074             $site_id = self::factory()->blog->create();
    2075 
    2076             $result = wp_uninitialize_site( $site_id );
    2077             $this->assertTrue( $result );
    2078             $this->assertFalse( wp_is_site_initialized( $site_id ) );
    2079         }
    2080 
    2081         /**
    2082          * @ticket 41333
    2083          */
    2084         public function test_wp_uninitialize_site_empty_id() {
    2085             $result = wp_uninitialize_site( 0 );
    2086             $this->assertWPError( $result );
    2087             $this->assertSame( 'site_empty_id', $result->get_error_code() );
    2088         }
    2089 
    2090         /**
    2091          * @ticket 41333
    2092          */
    2093         public function test_wp_uninitialize_site_invalid_id() {
    2094             $result = wp_uninitialize_site( 123 );
    2095             $this->assertWPError( $result );
    2096             $this->assertSame( 'site_invalid_id', $result->get_error_code() );
    2097         }
    2098 
    2099         /**
    2100          * @ticket 41333
    2101          */
    2102         public function test_wp_uninitialize_site_already_uninitialized() {
    2103             $result = wp_uninitialize_site( self::$uninitialized_site_id );
    2104             $this->assertWPError( $result );
    2105             $this->assertSame( 'site_already_uninitialized', $result->get_error_code() );
    2106         }
    2107 
    2108         /**
    2109          * @ticket 41333
    2110          */
    2111         public function test_wp_is_site_initialized() {
    2112             $this->assertTrue( wp_is_site_initialized( get_current_blog_id() ) );
    2113             $this->assertFalse( wp_is_site_initialized( self::$uninitialized_site_id ) );
    2114         }
    2115 
    2116         /**
    2117          * @ticket 41333
    2118          */
    2119         public function test_wp_is_site_initialized_prefilter() {
    2120             add_filter( 'pre_wp_is_site_initialized', '__return_false' );
    2121             $this->assertFalse( wp_is_site_initialized( get_current_blog_id() ) );
    2122 
    2123             add_filter( 'pre_wp_is_site_initialized', '__return_true' );
    2124             $this->assertTrue( wp_is_site_initialized( self::$uninitialized_site_id ) );
    2125         }
    2126 
    2127         /**
    2128          * @ticket 41333
    2129          */
    2130         public function test_wp_insert_site_forwards_args_to_wp_initialize_site() {
    2131             $args = array(
    2132                 'user_id' => 1,
    2133                 'title'   => 'My Site',
    2134                 'options' => array( 'option1' => 'value1' ),
    2135                 'meta'    => array( 'meta1' => 'value1' ),
    2136             );
    2137 
    2138             add_filter( 'wp_initialize_site_args', array( $this, 'filter_wp_initialize_site_args_catch_args' ) );
    2139             $site_id = wp_insert_site(
    2140                 array_merge(
    2141                     array(
    2142                         'domain' => 'testsite.org',
    2143                         'path'   => '/',
    2144                     ),
    2145                     $args
    2146                 )
    2147             );
    2148 
    2149             $passed_args                   = $this->wp_initialize_site_args;
    2150             $this->wp_initialize_site_args = null;
    2151 
    2152             $this->assertSameSetsWithIndex( $args, $passed_args );
    2153         }
    2154 
    2155         public function filter_wp_initialize_site_args_catch_args( $args ) {
    2156             $this->wp_initialize_site_args = $args;
    2157 
    2158             return $args;
    2159         }
    2160 
    2161         /**
    2162          * @ticket 46125
    2163          */
    2164         public function test_wpmu_create_blog_cache_cleanup_backward_compatible() {
    2165             add_action( 'populate_options', array( $this, 'populate_options_callback' ) );
    2166 
    2167             $blog_id = wpmu_create_blog( 'testsite1.example.org', '/test', 'test', 1, array( 'public' => 1 ), 2 );
    2168 
    2169             /*
    2170              * Should not hit blog_details cache initialized in $this->populate_options_callback triggered during
    2171              * populate_options callback's call of get_blog_details.
    2172              */
    2173             $this->assertSame( 'http://testsite1.example.org/test', get_blog_details( $blog_id )->siteurl );
    2174             $this->assertSame( 'http://testsite1.example.org/test', get_site( $blog_id )->siteurl );
    2175 
    2176             remove_action( 'populate_options', array( $this, 'populate_options_callback' ) );
    2177         }
    2178 
    2179         /**
    2180          * Populate options callback to warm cache for blog-details / site-details cache group
    2181          */
    2182         public function populate_options_callback() {
    2183             // Cache blog details.
    2184             $blog_id = get_current_blog_id();
    2185             get_blog_details( $blog_id );
    2186             get_site( $blog_id )->siteurl;
    2187             // Set siteurl.
    2188             update_option( 'siteurl', 'http://testsite1.example.org/test' );
    2189         }
    2190 
    2191         /**
    2192          * Tests whether all expected meta are provided in deprecated `wpmu_new_blog` action.
    2193          *
    2194          * @dataProvider data_wpmu_new_blog_action_backward_compatible
    2195          *
    2196          * @ticket 46351
    2197          */
    2198         public function test_wpmu_new_blog_action_backward_compatible( $meta, $expected_meta ) {
    2199             // We are testing deprecated hook. Register it to expected deprecated notices.
    2200             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    2201             add_action( 'wpmu_new_blog', array( $this, 'wpmu_new_blog_callback' ), 10, 6 );
    2202 
    2203             wpmu_create_blog( 'testsite1.example.org', '/new-blog/', 'New Blog', get_current_user_id(), $meta, 1 );
    2204 
    2205             $this->assertSameSetsWithIndex( $expected_meta, $this->wp_initialize_site_meta );
    2206 
    2207             $this->wp_initialize_site_meta = array();
    2208         }
    2209 
    2210         /**
    2211          * @ticket 42251
    2212          */
    2213         public function test_get_site_not_found_cache() {
    2214             $new_site_id = $this->_get_next_site_id();
    2215             $this->assertNull( get_site( $new_site_id ) );
    2216 
    2217             $num_queries = get_num_queries();
    2218             $this->assertNull( get_site( $new_site_id ) );
    2219             $this->assertSame( $num_queries, get_num_queries() );
    2220         }
    2221 
    2222         /**
    2223          * @ticket 42251
    2224          */
    2225         public function test_get_site_not_found_cache_clear() {
    2226             $new_site_id = $this->_get_next_site_id();
    2227             $this->assertNull( get_site( $new_site_id ) );
    2228 
    2229             $new_site = self::factory()->blog->create_and_get();
    2230 
    2231             // Double-check we got the ID of the new site correct.
    2232             $this->assertEquals( $new_site_id, $new_site->blog_id );
    2233 
    2234             // Verify that if we fetch the site now, it's no longer false.
    2235             $fetched_site = get_site( $new_site_id );
    2236             $this->assertInstanceOf( 'WP_Site', $fetched_site );
    2237             $this->assertEquals( $new_site_id, $fetched_site->blog_id );
    2238         }
    2239 
    2240         /**
    2241          * Gets the ID of the next site that will get inserted
    2242          * @return int
    2243          */
    2244         protected function _get_next_site_id() {
    2245             global $wpdb;
    2246             // Create an entry.
    2247             static::factory()->blog->create();
    2248             // Get the ID after it.
    2249             return (int) $wpdb->get_var( 'SELECT blog_id FROM ' . $wpdb->blogs . ' ORDER BY blog_ID DESC LIMIT 1' ) + 1;
    2250         }
    2251 
    2252         /**
    2253          * Capture the $meta value passed to the wpmu_new_blog action and compare it.
    2254          */
    2255         public function wpmu_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
    2256             $this->wp_initialize_site_meta = $meta;
    2257         }
    2258 
    2259         public function data_wpmu_new_blog_action_backward_compatible() {
    2260             return array(
    2261                 'default values' => array(
    2262                     array(),
    2263                     array(
    2264                         'public' => 0, // `public` is one of the default metas in `wpmu_create_blog()' function prior to WordPress 5.1.0.
    2265                         'WPLANG' => 'en_US', // WPLANG is another default meta in `wpmu_create_blog()` function prior to WordPress 5.1.0.
    2266                     ),
    2267                 ),
    2268                 'public site'    => array(
    2269                     array(
    2270                         'public' => 1,
    2271                     ),
    2272                     array(
    2273                         'public' => 1,
    2274                         'WPLANG' => 'en_US',
    2275                     ),
    2276                 ),
    2277                 'allowed_keys'   => array(
    2278                     array(
    2279                         'public'   => -1,
    2280                         'archived' => 0,
    2281                         'mature'   => 0,
    2282                         'spam'     => 0,
    2283                         'deleted'  => 0,
    2284                         'lang_id'  => 11,
    2285 
    2286                     ),
    2287                     array(
    2288                         'public'   => -1,
    2289                         'WPLANG'   => 'en_US',
    2290                         'archived' => 0,
    2291                         'mature'   => 0,
    2292                         'spam'     => 0,
    2293                         'deleted'  => 0,
    2294                         'lang_id'  => 11,
    2295                     ),
    2296                 ),
    2297                 'extra meta key' => array(
    2298                     array(
    2299                         'foo' => 'bar',
    2300                     ),
    2301                     array(
    2302                         'public' => 0,
    2303                         'foo'    => 'bar',
    2304                         'WPLANG' => 'en_US',
    2305                     ),
    2306                 ),
    2307             );
    2308         }
    2309     }
    2310 
    2311 endif;
     2172        $this->assertSame( 'http://testsite1.example.org/test', get_blog_details( $blog_id )->siteurl );
     2173        $this->assertSame( 'http://testsite1.example.org/test', get_site( $blog_id )->siteurl );
     2174
     2175        remove_action( 'populate_options', array( $this, 'populate_options_callback' ) );
     2176    }
     2177
     2178    /**
     2179     * Populate options callback to warm cache for blog-details / site-details cache group
     2180     */
     2181    public function populate_options_callback() {
     2182        // Cache blog details.
     2183        $blog_id = get_current_blog_id();
     2184        get_blog_details( $blog_id );
     2185        get_site( $blog_id )->siteurl;
     2186        // Set siteurl.
     2187        update_option( 'siteurl', 'http://testsite1.example.org/test' );
     2188    }
     2189
     2190    /**
     2191     * Tests whether all expected meta are provided in deprecated `wpmu_new_blog` action.
     2192     *
     2193     * @dataProvider data_wpmu_new_blog_action_backward_compatible
     2194     *
     2195     * @ticket 46351
     2196     */
     2197    public function test_wpmu_new_blog_action_backward_compatible( $meta, $expected_meta ) {
     2198        // We are testing deprecated hook. Register it to expected deprecated notices.
     2199        $this->setExpectedDeprecated( 'wpmu_new_blog' );
     2200        add_action( 'wpmu_new_blog', array( $this, 'wpmu_new_blog_callback' ), 10, 6 );
     2201
     2202        wpmu_create_blog( 'testsite1.example.org', '/new-blog/', 'New Blog', get_current_user_id(), $meta, 1 );
     2203
     2204        $this->assertSameSetsWithIndex( $expected_meta, $this->wp_initialize_site_meta );
     2205
     2206        $this->wp_initialize_site_meta = array();
     2207    }
     2208
     2209    /**
     2210     * @ticket 42251
     2211     */
     2212    public function test_get_site_not_found_cache() {
     2213        $new_site_id = $this->_get_next_site_id();
     2214        $this->assertNull( get_site( $new_site_id ) );
     2215
     2216        $num_queries = get_num_queries();
     2217        $this->assertNull( get_site( $new_site_id ) );
     2218        $this->assertSame( $num_queries, get_num_queries() );
     2219    }
     2220
     2221    /**
     2222     * @ticket 42251
     2223     */
     2224    public function test_get_site_not_found_cache_clear() {
     2225        $new_site_id = $this->_get_next_site_id();
     2226        $this->assertNull( get_site( $new_site_id ) );
     2227
     2228        $new_site = self::factory()->blog->create_and_get();
     2229
     2230        // Double-check we got the ID of the new site correct.
     2231        $this->assertEquals( $new_site_id, $new_site->blog_id );
     2232
     2233        // Verify that if we fetch the site now, it's no longer false.
     2234        $fetched_site = get_site( $new_site_id );
     2235        $this->assertInstanceOf( 'WP_Site', $fetched_site );
     2236        $this->assertEquals( $new_site_id, $fetched_site->blog_id );
     2237    }
     2238
     2239    /**
     2240     * Gets the ID of the next site that will get inserted
     2241     * @return int
     2242     */
     2243    protected function _get_next_site_id() {
     2244        global $wpdb;
     2245        // Create an entry.
     2246        static::factory()->blog->create();
     2247        // Get the ID after it.
     2248        return (int) $wpdb->get_var( 'SELECT blog_id FROM ' . $wpdb->blogs . ' ORDER BY blog_ID DESC LIMIT 1' ) + 1;
     2249    }
     2250
     2251    /**
     2252     * Capture the $meta value passed to the wpmu_new_blog action and compare it.
     2253     */
     2254    public function wpmu_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
     2255        $this->wp_initialize_site_meta = $meta;
     2256    }
     2257
     2258    public function data_wpmu_new_blog_action_backward_compatible() {
     2259        return array(
     2260            'default values' => array(
     2261                array(),
     2262                array(
     2263                    'public' => 0, // `public` is one of the default metas in `wpmu_create_blog()' function prior to WordPress 5.1.0.
     2264                    'WPLANG' => 'en_US', // WPLANG is another default meta in `wpmu_create_blog()` function prior to WordPress 5.1.0.
     2265                ),
     2266            ),
     2267            'public site'    => array(
     2268                array(
     2269                    'public' => 1,
     2270                ),
     2271                array(
     2272                    'public' => 1,
     2273                    'WPLANG' => 'en_US',
     2274                ),
     2275            ),
     2276            'allowed_keys'   => array(
     2277                array(
     2278                    'public'   => -1,
     2279                    'archived' => 0,
     2280                    'mature'   => 0,
     2281                    'spam'     => 0,
     2282                    'deleted'  => 0,
     2283                    'lang_id'  => 11,
     2284
     2285                ),
     2286                array(
     2287                    'public'   => -1,
     2288                    'WPLANG'   => 'en_US',
     2289                    'archived' => 0,
     2290                    'mature'   => 0,
     2291                    'spam'     => 0,
     2292                    'deleted'  => 0,
     2293                    'lang_id'  => 11,
     2294                ),
     2295            ),
     2296            'extra meta key' => array(
     2297                array(
     2298                    'foo' => 'bar',
     2299                ),
     2300                array(
     2301                    'public' => 0,
     2302                    'foo'    => 'bar',
     2303                    'WPLANG' => 'en_US',
     2304                ),
     2305            ),
     2306        );
     2307    }
     2308}
  • trunk/tests/phpunit/tests/multisite/siteDetails.php

    r51860 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Test 'site_details' functionality.
     5 *
     6 * @group ms-required
     7 * @group ms-site
     8 * @group multisite
     9 */
     10class Tests_Multisite_SiteDetails extends WP_UnitTestCase {
     11
    412    /**
    5      * Test 'site_details' functionality.
     13     * @dataProvider data_allowed_options
    614     *
    7      * @group ms-site
    8      * @group multisite
     15     * @ticket 40063
    916     */
    10     class Tests_Multisite_SiteDetails extends WP_UnitTestCase {
    11         /**
    12          * @dataProvider data_allowed_options
    13          *
    14          * @ticket 40063
    15          */
    16         public function test_update_allowed_option_deletes_site_details_cache( $allowed_option, $temporary_value ) {
    17             $site = get_site();
     17    public function test_update_allowed_option_deletes_site_details_cache( $allowed_option, $temporary_value ) {
     18        $site = get_site();
    1819
    19             $original_value = $site->$allowed_option;
    20             update_option( $allowed_option, $temporary_value );
     20        $original_value = $site->$allowed_option;
     21        update_option( $allowed_option, $temporary_value );
    2122
    22             $cached_result = wp_cache_get( $site->id, 'site-details' );
     23        $cached_result = wp_cache_get( $site->id, 'site-details' );
    2324
    24             /* Reset to original value. */
    25             update_option( $allowed_option, $original_value );
     25        /* Reset to original value. */
     26        update_option( $allowed_option, $original_value );
    2627
    27             $this->assertFalse( $cached_result );
    28         }
    29 
    30         /**
    31          * @dataProvider data_allowed_options
    32          *
    33          * @ticket 40063
    34          */
    35         public function test_update_allowed_option_deletes_blog_details_cache( $allowed_option, $temporary_value ) {
    36             $blog_details = get_blog_details();
    37 
    38             $original_value = $blog_details->$allowed_option;
    39             update_option( $allowed_option, $temporary_value );
    40 
    41             $cached_result = wp_cache_get( $blog_details->id, 'blog-details' );
    42 
    43             /* Reset to original value. */
    44             update_option( $allowed_option, $original_value );
    45 
    46             $this->assertFalse( $cached_result );
    47         }
    48 
    49         /**
    50          * @dataProvider data_allowed_options
    51          *
    52          * @ticket 40063
    53          */
    54         public function test_update_allowed_option_does_not_delete_site_cache( $allowed_option, $temporary_value ) {
    55             $site = get_site();
    56 
    57             $original_value = $site->$allowed_option;
    58             update_option( $allowed_option, $temporary_value );
    59 
    60             $cached_result = wp_cache_get( $site->id, 'sites' );
    61 
    62             /* Reset to original value. */
    63             update_option( $allowed_option, $original_value );
    64 
    65             $this->assertNotFalse( $cached_result );
    66         }
    67 
    68         /**
    69          * @dataProvider data_allowed_options
    70          *
    71          * @ticket 40063
    72          */
    73         public function test_update_allowed_option_does_not_delete_short_blog_details_cache( $allowed_option, $temporary_value ) {
    74             $blog_details = get_blog_details( null, false );
    75 
    76             $original_value = get_option( $allowed_option );
    77             update_option( $allowed_option, $temporary_value );
    78 
    79             $cached_result = wp_cache_get( $blog_details->id . 'short', 'blog-details' );
    80 
    81             /* Reset to original value. */
    82             update_option( $allowed_option, $original_value );
    83 
    84             $this->assertNotFalse( $cached_result );
    85         }
    86 
    87         /**
    88          * @dataProvider data_allowed_options
    89          *
    90          * @ticket 40063
    91          */
    92         public function test_update_allowed_option_does_not_update_sites_last_changed( $allowed_option, $temporary_value ) {
    93             $last_changed = wp_cache_get_last_changed( 'sites' );
    94 
    95             $original_value = get_option( $allowed_option );
    96             update_option( $allowed_option, $temporary_value );
    97 
    98             $new_last_changed = wp_cache_get_last_changed( 'sites' );
    99 
    100             /* Reset to original value. */
    101             update_option( $allowed_option, $original_value );
    102 
    103             $this->assertSame( $new_last_changed, $last_changed );
    104         }
    105 
    106         public function data_allowed_options() {
    107             return array(
    108                 array( 'blogname', 'Custom Site' ),
    109                 array( 'home', 'http://custom-site-url.org' ),
    110                 array( 'siteurl', 'http://custom-site-url.org' ),
    111                 array( 'post_count', '4' ),
    112             );
    113         }
    114 
    115         /**
    116          * @ticket 40063
    117          */
    118         public function test_update_random_blog_option_does_not_delete_cache() {
    119             $site = get_site();
    120 
    121             update_option( 'foobar_option', 'foobar_value' );
    122             $cached_result = wp_cache_get( $site->id, 'sites' );
    123 
    124             delete_option( 'foobar_option' );
    125 
    126             $this->assertNotFalse( $cached_result );
    127         }
    128 
    129         /**
    130          * @ticket 40247
    131          */
    132         public function test_site_details_cached_including_false_values() {
    133             $id = self::factory()->blog->create();
    134 
    135             $site = get_site( $id );
    136 
    137             // Trigger retrieving site details (post_count is not set on new sites).
    138             $post_count = $site->post_count;
    139 
    140             $cached_details = wp_cache_get( $site->id, 'site-details' );
    141 
    142             wp_delete_site( $id );
    143             wp_update_network_site_counts();
    144 
    145             $this->assertNotFalse( $cached_details );
    146         }
    147 
    148         public function test_site_details_filter_with_blogname() {
    149             add_filter( 'site_details', array( $this, '_filter_site_details_blogname' ) );
    150             $site     = get_site();
    151             $blogname = $site->blogname;
    152             remove_filter( 'site_details', array( $this, '_filter_site_details_blogname' ) );
    153 
    154             $this->assertSame( 'Foo Bar', $blogname );
    155         }
    156 
    157         public function _filter_site_details_blogname( $details ) {
    158             $details->blogname = 'Foo Bar';
    159             return $details;
    160         }
    161 
    162         /**
    163          * @ticket 40458
    164          */
    165         public function test_site_details_filter_with_custom_value_isetter() {
    166             add_filter( 'site_details', array( $this, '_filter_site_details_custom_value' ) );
    167             $site               = get_site();
    168             $custom_value_isset = isset( $site->custom_value );
    169             remove_filter( 'site_details', array( $this, '_filter_site_details_custom_value' ) );
    170 
    171             $this->assertTrue( $custom_value_isset );
    172         }
    173 
    174         /**
    175          * @ticket 40458
    176          */
    177         public function test_site_details_filter_with_custom_value_getter() {
    178             add_filter( 'site_details', array( $this, '_filter_site_details_custom_value' ) );
    179             $site         = get_site();
    180             $custom_value = $site->custom_value;
    181             remove_filter( 'site_details', array( $this, '_filter_site_details_custom_value' ) );
    182 
    183             $this->assertSame( 'foo', $custom_value );
    184         }
    185 
    186         public function _filter_site_details_custom_value( $details ) {
    187             $details->custom_value = 'foo';
    188             return $details;
    189         }
     28        $this->assertFalse( $cached_result );
    19029    }
    19130
    192 endif;
     31    /**
     32     * @dataProvider data_allowed_options
     33     *
     34     * @ticket 40063
     35     */
     36    public function test_update_allowed_option_deletes_blog_details_cache( $allowed_option, $temporary_value ) {
     37        $blog_details = get_blog_details();
     38
     39        $original_value = $blog_details->$allowed_option;
     40        update_option( $allowed_option, $temporary_value );
     41
     42        $cached_result = wp_cache_get( $blog_details->id, 'blog-details' );
     43
     44        /* Reset to original value. */
     45        update_option( $allowed_option, $original_value );
     46
     47        $this->assertFalse( $cached_result );
     48    }
     49
     50    /**
     51     * @dataProvider data_allowed_options
     52     *
     53     * @ticket 40063
     54     */
     55    public function test_update_allowed_option_does_not_delete_site_cache( $allowed_option, $temporary_value ) {
     56        $site = get_site();
     57
     58        $original_value = $site->$allowed_option;
     59        update_option( $allowed_option, $temporary_value );
     60
     61        $cached_result = wp_cache_get( $site->id, 'sites' );
     62
     63        /* Reset to original value. */
     64        update_option( $allowed_option, $original_value );
     65
     66        $this->assertNotFalse( $cached_result );
     67    }
     68
     69    /**
     70     * @dataProvider data_allowed_options
     71     *
     72     * @ticket 40063
     73     */
     74    public function test_update_allowed_option_does_not_delete_short_blog_details_cache( $allowed_option, $temporary_value ) {
     75        $blog_details = get_blog_details( null, false );
     76
     77        $original_value = get_option( $allowed_option );
     78        update_option( $allowed_option, $temporary_value );
     79
     80        $cached_result = wp_cache_get( $blog_details->id . 'short', 'blog-details' );
     81
     82        /* Reset to original value. */
     83        update_option( $allowed_option, $original_value );
     84
     85        $this->assertNotFalse( $cached_result );
     86    }
     87
     88    /**
     89     * @dataProvider data_allowed_options
     90     *
     91     * @ticket 40063
     92     */
     93    public function test_update_allowed_option_does_not_update_sites_last_changed( $allowed_option, $temporary_value ) {
     94        $last_changed = wp_cache_get_last_changed( 'sites' );
     95
     96        $original_value = get_option( $allowed_option );
     97        update_option( $allowed_option, $temporary_value );
     98
     99        $new_last_changed = wp_cache_get_last_changed( 'sites' );
     100
     101        /* Reset to original value. */
     102        update_option( $allowed_option, $original_value );
     103
     104        $this->assertSame( $new_last_changed, $last_changed );
     105    }
     106
     107    public function data_allowed_options() {
     108        return array(
     109            array( 'blogname', 'Custom Site' ),
     110            array( 'home', 'http://custom-site-url.org' ),
     111            array( 'siteurl', 'http://custom-site-url.org' ),
     112            array( 'post_count', '4' ),
     113        );
     114    }
     115
     116    /**
     117     * @ticket 40063
     118     */
     119    public function test_update_random_blog_option_does_not_delete_cache() {
     120        $site = get_site();
     121
     122        update_option( 'foobar_option', 'foobar_value' );
     123        $cached_result = wp_cache_get( $site->id, 'sites' );
     124
     125        delete_option( 'foobar_option' );
     126
     127        $this->assertNotFalse( $cached_result );
     128    }
     129
     130    /**
     131     * @ticket 40247
     132     */
     133    public function test_site_details_cached_including_false_values() {
     134        $id = self::factory()->blog->create();
     135
     136        $site = get_site( $id );
     137
     138        // Trigger retrieving site details (post_count is not set on new sites).
     139        $post_count = $site->post_count;
     140
     141        $cached_details = wp_cache_get( $site->id, 'site-details' );
     142
     143        wp_delete_site( $id );
     144        wp_update_network_site_counts();
     145
     146        $this->assertNotFalse( $cached_details );
     147    }
     148
     149    public function test_site_details_filter_with_blogname() {
     150        add_filter( 'site_details', array( $this, '_filter_site_details_blogname' ) );
     151        $site     = get_site();
     152        $blogname = $site->blogname;
     153        remove_filter( 'site_details', array( $this, '_filter_site_details_blogname' ) );
     154
     155        $this->assertSame( 'Foo Bar', $blogname );
     156    }
     157
     158    public function _filter_site_details_blogname( $details ) {
     159        $details->blogname = 'Foo Bar';
     160        return $details;
     161    }
     162
     163    /**
     164     * @ticket 40458
     165     */
     166    public function test_site_details_filter_with_custom_value_isetter() {
     167        add_filter( 'site_details', array( $this, '_filter_site_details_custom_value' ) );
     168        $site               = get_site();
     169        $custom_value_isset = isset( $site->custom_value );
     170        remove_filter( 'site_details', array( $this, '_filter_site_details_custom_value' ) );
     171
     172        $this->assertTrue( $custom_value_isset );
     173    }
     174
     175    /**
     176     * @ticket 40458
     177     */
     178    public function test_site_details_filter_with_custom_value_getter() {
     179        add_filter( 'site_details', array( $this, '_filter_site_details_custom_value' ) );
     180        $site         = get_site();
     181        $custom_value = $site->custom_value;
     182        remove_filter( 'site_details', array( $this, '_filter_site_details_custom_value' ) );
     183
     184        $this->assertSame( 'foo', $custom_value );
     185    }
     186
     187    public function _filter_site_details_custom_value( $details ) {
     188        $details->custom_value = 'foo';
     189        return $details;
     190    }
     191}
  • trunk/tests/phpunit/tests/multisite/siteMeta.php

    r55747 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group ms-site
     6 * @group multisite
     7 * @group meta
     8 * @ticket 37923
     9 */
     10class Tests_Multisite_SiteMeta extends WP_UnitTestCase {
     11
     12    protected static $site_id;
     13    protected static $site_id2;
     14    protected static $flag_was_set;
     15
     16    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     17        self::$site_id  = $factory->blog->create(
     18            array(
     19                'domain' => 'wordpress.org',
     20                'path'   => '/',
     21            )
     22        );
     23        self::$site_id2 = $factory->blog->create(
     24            array(
     25                'domain' => 'wordpress.org',
     26                'path'   => '/foo/',
     27            )
     28        );
     29
     30        // Populate the main network flag as necessary.
     31        self::$flag_was_set = true;
     32        if ( false === get_network_option( get_main_network_id(), 'site_meta_supported', false ) ) {
     33            self::$flag_was_set = false;
     34            is_site_meta_supported();
     35        }
     36    }
     37
     38    public static function wpTearDownAfterClass() {
     39        // Delete the possibly previously populated main network flag.
     40        if ( ! self::$flag_was_set ) {
     41            delete_network_option( get_main_network_id(), 'site_meta_supported' );
     42        }
     43
     44        wp_delete_site( self::$site_id );
     45        wp_delete_site( self::$site_id2 );
     46
     47        wp_update_network_site_counts();
     48    }
     49
     50    public function test_is_site_meta_supported() {
     51        $this->assertTrue( is_site_meta_supported() );
     52    }
     53
     54    public function test_is_site_meta_supported_filtered() {
     55        add_filter( 'pre_site_option_site_meta_supported', '__return_zero' );
     56        $this->assertFalse( is_site_meta_supported() );
     57    }
     58
     59    public function test_add() {
     60        if ( ! is_site_meta_supported() ) {
     61            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     62        }
     63
     64        $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) );
     65        $this->assertSame( 'bar', get_site_meta( self::$site_id, 'foo', true ) );
     66    }
     67
     68    public function test_add_unique() {
     69        if ( ! is_site_meta_supported() ) {
     70            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     71        }
     72
     73        $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) );
     74        $this->assertFalse( add_site_meta( self::$site_id, 'foo', 'bar', true ) );
     75    }
     76
     77    public function test_delete() {
     78        if ( ! is_site_meta_supported() ) {
     79            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     80        }
     81
     82        add_site_meta( self::$site_id, 'foo', 'bar' );
     83
     84        $this->assertTrue( delete_site_meta( self::$site_id, 'foo' ) );
     85        $this->assertEmpty( get_site_meta( self::$site_id, 'foo', true ) );
     86    }
     87
     88    public function test_delete_with_invalid_meta_key_should_return_false() {
     89        if ( ! is_site_meta_supported() ) {
     90            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     91        }
     92
     93        $this->assertFalse( delete_site_meta( self::$site_id, 'foo' ) );
     94    }
     95
     96    public function test_delete_should_respect_meta_value() {
     97        if ( ! is_site_meta_supported() ) {
     98            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     99        }
     100
     101        add_site_meta( self::$site_id, 'foo', 'bar' );
     102        add_site_meta( self::$site_id, 'foo', 'baz' );
     103
     104        $this->assertTrue( delete_site_meta( self::$site_id, 'foo', 'bar' ) );
     105
     106        $metas = get_site_meta( self::$site_id, 'foo' );
     107        $this->assertSame( array( 'baz' ), $metas );
     108    }
     109
     110    public function test_get_with_no_key_should_fetch_all_keys() {
     111        if ( ! is_site_meta_supported() ) {
     112            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     113        }
     114
     115        add_site_meta( self::$site_id, 'foo', 'bar' );
     116        add_site_meta( self::$site_id, 'foo1', 'baz' );
     117
     118        $found    = get_site_meta( self::$site_id );
     119        $expected = array(
     120            'foo'  => array( 'bar' ),
     121            'foo1' => array( 'baz' ),
     122        );
     123
     124        $this->assertSameSets( $expected, $found );
     125    }
     126
     127    public function test_get_with_key_should_fetch_all_for_key() {
     128        if ( ! is_site_meta_supported() ) {
     129            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     130        }
     131
     132        add_site_meta( self::$site_id, 'foo', 'bar' );
     133        add_site_meta( self::$site_id, 'foo', 'baz' );
     134        add_site_meta( self::$site_id, 'foo1', 'baz' );
     135
     136        $found    = get_site_meta( self::$site_id, 'foo' );
     137        $expected = array( 'bar', 'baz' );
     138
     139        $this->assertSameSets( $expected, $found );
     140    }
     141
     142    public function test_get_should_respect_single_true() {
     143        if ( ! is_site_meta_supported() ) {
     144            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     145        }
     146
     147        add_site_meta( self::$site_id, 'foo', 'bar' );
     148        add_site_meta( self::$site_id, 'foo', 'baz' );
     149
     150        $found = get_site_meta( self::$site_id, 'foo', true );
     151        $this->assertSame( 'bar', $found );
     152    }
     153
     154    public function test_update_should_pass_to_add_when_no_value_exists_for_key() {
     155        if ( ! is_site_meta_supported() ) {
     156            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     157        }
     158
     159        $actual = update_site_meta( self::$site_id, 'foo', 'bar' );
     160        $this->assertIsInt( $actual );
     161        $this->assertNotEmpty( $actual );
     162
     163        $meta = get_site_meta( self::$site_id, 'foo', true );
     164        $this->assertSame( 'bar', $meta );
     165    }
     166
     167    public function test_update_should_return_true_when_updating_existing_value_for_key() {
     168        if ( ! is_site_meta_supported() ) {
     169            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     170        }
     171
     172        add_site_meta( self::$site_id, 'foo', 'bar' );
     173
     174        $actual = update_site_meta( self::$site_id, 'foo', 'baz' );
     175        $this->assertTrue( $actual );
     176
     177        $meta = get_site_meta( self::$site_id, 'foo', true );
     178        $this->assertSame( 'baz', $meta );
     179    }
     180
     181    public function test_delete_by_key() {
     182        if ( ! is_site_meta_supported() ) {
     183            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     184        }
     185
     186        add_site_meta( self::$site_id, 'unique_delete_by_key', 'value', true );
     187        add_site_meta( self::$site_id2, 'unique_delete_by_key', 'value', true );
     188
     189        $this->assertSame( 'value', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) );
     190        $this->assertSame( 'value', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) );
     191
     192        $this->assertTrue( delete_site_meta_by_key( 'unique_delete_by_key' ) );
     193
     194        $this->assertSame( '', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) );
     195        $this->assertSame( '', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) );
     196    }
     197
     198    public function test_site_meta_should_be_deleted_when_site_is_deleted() {
     199        if ( ! is_site_meta_supported() ) {
     200            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     201        }
     202
     203        $site_id = self::factory()->blog->create(
     204            array(
     205                'domain' => 'foo.org',
     206                'path'   => '/',
     207            )
     208        );
     209
     210        add_site_meta( $site_id, 'foo', 'bar' );
     211        add_site_meta( $site_id, 'foo1', 'bar' );
     212
     213        $this->assertSame( 'bar', get_site_meta( $site_id, 'foo', true ) );
     214        $this->assertSame( 'bar', get_site_meta( $site_id, 'foo1', true ) );
     215
     216        wp_delete_site( $site_id );
     217
     218        $this->assertSame( '', get_site_meta( $site_id, 'foo', true ) );
     219        $this->assertSame( '', get_site_meta( $site_id, 'foo1', true ) );
     220    }
     221
     222    public function test_update_site_meta_cache() {
     223        if ( ! is_site_meta_supported() ) {
     224            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     225        }
     226
     227        update_site_meta( self::$site_id, 'foo', 'bar' );
     228        update_sitemeta_cache( array( self::$site_id ) );
     229
     230        $num_queries = get_num_queries();
     231        get_site_meta( self::$site_id, 'foo', true );
     232        $this->assertSame( $num_queries, get_num_queries() );
     233    }
     234
     235    public function test_query_update_site_meta_cache_true() {
     236        if ( ! is_site_meta_supported() ) {
     237            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     238        }
     239
     240        update_site_meta( self::$site_id, 'foo', 'bar' );
     241
     242        // Do not include 'update_site_meta_cache' as true as its the default.
     243        new WP_Site_Query(
     244            array(
     245                'ID' => self::$site_id,
     246            )
     247        );
     248
     249        $num_queries = get_num_queries();
     250        get_site_meta( self::$site_id, 'foo', true );
     251        $this->assertSame( 1, get_num_queries() - $num_queries );
     252    }
    4253
    5254    /**
    6      * @group ms-site
    7      * @group multisite
    8      * @group meta
    9      * @ticket 37923
     255     * @ticket 58185
    10256     */
    11     class Tests_Multisite_SiteMeta extends WP_UnitTestCase {
    12         protected static $site_id;
    13         protected static $site_id2;
    14         protected static $flag_was_set;
    15 
    16         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    17             self::$site_id  = $factory->blog->create(
    18                 array(
    19                     'domain' => 'wordpress.org',
    20                     'path'   => '/',
    21                 )
    22             );
    23             self::$site_id2 = $factory->blog->create(
    24                 array(
    25                     'domain' => 'wordpress.org',
    26                     'path'   => '/foo/',
    27                 )
    28             );
    29 
    30             // Populate the main network flag as necessary.
    31             self::$flag_was_set = true;
    32             if ( false === get_network_option( get_main_network_id(), 'site_meta_supported', false ) ) {
    33                 self::$flag_was_set = false;
    34                 is_site_meta_supported();
    35             }
    36         }
    37 
    38         public static function wpTearDownAfterClass() {
    39             // Delete the possibly previously populated main network flag.
    40             if ( ! self::$flag_was_set ) {
    41                 delete_network_option( get_main_network_id(), 'site_meta_supported' );
    42             }
    43 
    44             wp_delete_site( self::$site_id );
    45             wp_delete_site( self::$site_id2 );
    46 
    47             wp_update_network_site_counts();
    48         }
    49 
    50         public function test_is_site_meta_supported() {
    51             $this->assertTrue( is_site_meta_supported() );
    52         }
    53 
    54         public function test_is_site_meta_supported_filtered() {
    55             add_filter( 'pre_site_option_site_meta_supported', '__return_zero' );
    56             $this->assertFalse( is_site_meta_supported() );
    57         }
    58 
    59         public function test_add() {
    60             if ( ! is_site_meta_supported() ) {
    61                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    62             }
    63 
    64             $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) );
    65             $this->assertSame( 'bar', get_site_meta( self::$site_id, 'foo', true ) );
    66         }
    67 
    68         public function test_add_unique() {
    69             if ( ! is_site_meta_supported() ) {
    70                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    71             }
    72 
    73             $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) );
    74             $this->assertFalse( add_site_meta( self::$site_id, 'foo', 'bar', true ) );
    75         }
    76 
    77         public function test_delete() {
    78             if ( ! is_site_meta_supported() ) {
    79                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    80             }
    81 
    82             add_site_meta( self::$site_id, 'foo', 'bar' );
    83 
    84             $this->assertTrue( delete_site_meta( self::$site_id, 'foo' ) );
    85             $this->assertEmpty( get_site_meta( self::$site_id, 'foo', true ) );
    86         }
    87 
    88         public function test_delete_with_invalid_meta_key_should_return_false() {
    89             if ( ! is_site_meta_supported() ) {
    90                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    91             }
    92 
    93             $this->assertFalse( delete_site_meta( self::$site_id, 'foo' ) );
    94         }
    95 
    96         public function test_delete_should_respect_meta_value() {
    97             if ( ! is_site_meta_supported() ) {
    98                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    99             }
    100 
    101             add_site_meta( self::$site_id, 'foo', 'bar' );
    102             add_site_meta( self::$site_id, 'foo', 'baz' );
    103 
    104             $this->assertTrue( delete_site_meta( self::$site_id, 'foo', 'bar' ) );
    105 
    106             $metas = get_site_meta( self::$site_id, 'foo' );
    107             $this->assertSame( array( 'baz' ), $metas );
    108         }
    109 
    110         public function test_get_with_no_key_should_fetch_all_keys() {
    111             if ( ! is_site_meta_supported() ) {
    112                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    113             }
    114 
    115             add_site_meta( self::$site_id, 'foo', 'bar' );
    116             add_site_meta( self::$site_id, 'foo1', 'baz' );
    117 
    118             $found    = get_site_meta( self::$site_id );
    119             $expected = array(
    120                 'foo'  => array( 'bar' ),
    121                 'foo1' => array( 'baz' ),
    122             );
    123 
    124             $this->assertSameSets( $expected, $found );
    125         }
    126 
    127         public function test_get_with_key_should_fetch_all_for_key() {
    128             if ( ! is_site_meta_supported() ) {
    129                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    130             }
    131 
    132             add_site_meta( self::$site_id, 'foo', 'bar' );
    133             add_site_meta( self::$site_id, 'foo', 'baz' );
    134             add_site_meta( self::$site_id, 'foo1', 'baz' );
    135 
    136             $found    = get_site_meta( self::$site_id, 'foo' );
    137             $expected = array( 'bar', 'baz' );
    138 
    139             $this->assertSameSets( $expected, $found );
    140         }
    141 
    142         public function test_get_should_respect_single_true() {
    143             if ( ! is_site_meta_supported() ) {
    144                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    145             }
    146 
    147             add_site_meta( self::$site_id, 'foo', 'bar' );
    148             add_site_meta( self::$site_id, 'foo', 'baz' );
    149 
    150             $found = get_site_meta( self::$site_id, 'foo', true );
    151             $this->assertSame( 'bar', $found );
    152         }
    153 
    154         public function test_update_should_pass_to_add_when_no_value_exists_for_key() {
    155             if ( ! is_site_meta_supported() ) {
    156                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    157             }
    158 
    159             $actual = update_site_meta( self::$site_id, 'foo', 'bar' );
    160             $this->assertIsInt( $actual );
    161             $this->assertNotEmpty( $actual );
    162 
    163             $meta = get_site_meta( self::$site_id, 'foo', true );
    164             $this->assertSame( 'bar', $meta );
    165         }
    166 
    167         public function test_update_should_return_true_when_updating_existing_value_for_key() {
    168             if ( ! is_site_meta_supported() ) {
    169                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    170             }
    171 
    172             add_site_meta( self::$site_id, 'foo', 'bar' );
    173 
    174             $actual = update_site_meta( self::$site_id, 'foo', 'baz' );
    175             $this->assertTrue( $actual );
    176 
    177             $meta = get_site_meta( self::$site_id, 'foo', true );
    178             $this->assertSame( 'baz', $meta );
    179         }
    180 
    181         public function test_delete_by_key() {
    182             if ( ! is_site_meta_supported() ) {
    183                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    184             }
    185 
    186             add_site_meta( self::$site_id, 'unique_delete_by_key', 'value', true );
    187             add_site_meta( self::$site_id2, 'unique_delete_by_key', 'value', true );
    188 
    189             $this->assertSame( 'value', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) );
    190             $this->assertSame( 'value', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) );
    191 
    192             $this->assertTrue( delete_site_meta_by_key( 'unique_delete_by_key' ) );
    193 
    194             $this->assertSame( '', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) );
    195             $this->assertSame( '', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) );
    196         }
    197 
    198         public function test_site_meta_should_be_deleted_when_site_is_deleted() {
    199             if ( ! is_site_meta_supported() ) {
    200                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    201             }
    202 
    203             $site_id = self::factory()->blog->create(
    204                 array(
    205                     'domain' => 'foo.org',
    206                     'path'   => '/',
    207                 )
    208             );
    209 
    210             add_site_meta( $site_id, 'foo', 'bar' );
    211             add_site_meta( $site_id, 'foo1', 'bar' );
    212 
    213             $this->assertSame( 'bar', get_site_meta( $site_id, 'foo', true ) );
    214             $this->assertSame( 'bar', get_site_meta( $site_id, 'foo1', true ) );
    215 
    216             wp_delete_site( $site_id );
    217 
    218             $this->assertSame( '', get_site_meta( $site_id, 'foo', true ) );
    219             $this->assertSame( '', get_site_meta( $site_id, 'foo1', true ) );
    220         }
    221 
    222         public function test_update_site_meta_cache() {
    223             if ( ! is_site_meta_supported() ) {
    224                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    225             }
    226 
    227             update_site_meta( self::$site_id, 'foo', 'bar' );
    228             update_sitemeta_cache( array( self::$site_id ) );
    229 
    230             $num_queries = get_num_queries();
    231             get_site_meta( self::$site_id, 'foo', true );
    232             $this->assertSame( $num_queries, get_num_queries() );
    233         }
    234 
    235         public function test_query_update_site_meta_cache_true() {
    236             if ( ! is_site_meta_supported() ) {
    237                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    238             }
    239 
    240             update_site_meta( self::$site_id, 'foo', 'bar' );
    241 
    242             // Do not include 'update_site_meta_cache' as true as its the default.
    243             new WP_Site_Query(
    244                 array(
    245                     'ID' => self::$site_id,
    246                 )
    247             );
    248 
    249             $num_queries = get_num_queries();
    250             get_site_meta( self::$site_id, 'foo', true );
    251             $this->assertSame( 1, get_num_queries() - $num_queries );
    252         }
    253 
    254         /**
    255          * @ticket 58185
    256          */
    257         public function test_lazy_load_site_meta() {
    258             if ( ! is_site_meta_supported() ) {
    259                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    260             }
    261 
    262             $filter = new MockAction();
    263             add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 );
    264 
    265             $q = new WP_Site_Query(
    266                 array(
    267                     'ID' => self::$site_id,
    268                 )
    269             );
    270 
    271             $this->assertSameSets( array( (string) self::$site_id ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the first test site' );
    272 
    273             $q = new WP_Site_Query(
    274                 array(
    275                     'ID' => self::$site_id2,
    276                 )
    277             );
    278 
    279             $this->assertSameSets( array( (string) self::$site_id2 ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the second test site' );
    280 
    281             get_site_meta( self::$site_id2 );
    282 
    283             $args     = $filter->get_args();
    284             $first    = reset( $args );
    285             $site_ids = end( $first );
    286             $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two site\'s meta' );
    287         }
    288 
    289         /**
    290          * @ticket 58185
    291          */
    292         public function test_lazy_load_site_meta_fields_id() {
    293             if ( ! is_site_meta_supported() ) {
    294                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    295             }
    296 
    297             $filter = new MockAction();
    298             add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 );
    299 
    300             $q = new WP_Site_Query(
    301                 array(
    302                     'ID'     => self::$site_id,
    303                     'fields' => 'ids',
    304                 )
    305             );
    306 
    307             $this->assertSameSets( array( self::$site_id ), $q->sites, 'Site query should return the first test site' );
    308 
    309             $q = new WP_Site_Query(
    310                 array(
    311                     'ID'     => self::$site_id2,
    312                     'fields' => 'ids',
    313                 )
    314             );
    315 
    316             $this->assertSameSets( array( self::$site_id2 ), $q->sites, 'Site query should return the second test site' );
    317 
    318             get_site_meta( self::$site_id2 );
    319 
    320             $args     = $filter->get_args();
    321             $first    = reset( $args );
    322             $site_ids = end( $first );
    323             $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two sites meta' );
    324         }
    325 
    326         public function test_query_update_site_meta_cache_false() {
    327             if ( ! is_site_meta_supported() ) {
    328                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    329             }
    330 
    331             update_site_meta( self::$site_id, 'foo', 'bar' );
    332 
    333             new WP_Site_Query(
    334                 array(
    335                     'ID'                     => self::$site_id,
    336                     'update_site_meta_cache' => false,
    337                 )
    338             );
    339 
    340             $num_queries = get_num_queries();
    341             get_site_meta( self::$site_id, 'foo', true );
    342             $this->assertSame( 1, get_num_queries() - $num_queries );
    343         }
    344 
    345         /**
    346          * @ticket 40229
    347          */
    348         public function test_add_site_meta_should_bust_get_sites_cache() {
    349             if ( ! is_site_meta_supported() ) {
    350                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    351             }
    352 
    353             add_site_meta( self::$site_id, 'foo', 'bar' );
    354 
    355             // Prime cache.
    356             $found = get_sites(
    357                 array(
    358                     'fields'     => 'ids',
    359                     'meta_query' => array(
    360                         array(
    361                             'key'   => 'foo',
    362                             'value' => 'bar',
    363                         ),
    364                     ),
    365                 )
    366             );
    367 
    368             $this->assertSameSets( array( self::$site_id ), $found );
    369 
    370             add_site_meta( self::$site_id2, 'foo', 'bar' );
    371 
    372             $found = get_sites(
    373                 array(
    374                     'fields'     => 'ids',
    375                     'meta_query' => array(
    376                         array(
    377                             'key'   => 'foo',
    378                             'value' => 'bar',
    379                         ),
    380                     ),
    381                 )
    382             );
    383 
    384             $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found );
    385         }
    386 
    387         /**
    388          * @ticket 40229
    389          */
    390         public function test_update_site_meta_should_bust_get_sites_cache() {
    391             if ( ! is_site_meta_supported() ) {
    392                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    393             }
    394 
    395             add_site_meta( self::$site_id, 'foo', 'bar' );
    396             add_site_meta( self::$site_id2, 'foo', 'baz' );
    397 
    398             // Prime cache.
    399             $found = get_sites(
    400                 array(
    401                     'fields'     => 'ids',
    402                     'meta_query' => array(
    403                         array(
    404                             'key'   => 'foo',
    405                             'value' => 'bar',
    406                         ),
    407                     ),
    408                 )
    409             );
    410 
    411             $this->assertSameSets( array( self::$site_id ), $found );
    412 
    413             update_site_meta( self::$site_id2, 'foo', 'bar' );
    414 
    415             $found = get_sites(
    416                 array(
    417                     'fields'     => 'ids',
    418                     'meta_query' => array(
    419                         array(
    420                             'key'   => 'foo',
    421                             'value' => 'bar',
    422                         ),
    423                     ),
    424                 )
    425             );
    426 
    427             $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found );
    428         }
    429 
    430         /**
    431          * @ticket 40229
    432          */
    433         public function test_delete_site_meta_should_bust_get_sites_cache() {
    434             if ( ! is_site_meta_supported() ) {
    435                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    436             }
    437 
    438             add_site_meta( self::$site_id, 'foo', 'bar' );
    439             add_site_meta( self::$site_id2, 'foo', 'bar' );
    440 
    441             // Prime cache.
    442             $found = get_sites(
    443                 array(
    444                     'fields'     => 'ids',
    445                     'meta_query' => array(
    446                         array(
    447                             'key'   => 'foo',
    448                             'value' => 'bar',
    449                         ),
    450                     ),
    451                 )
    452             );
    453 
    454             $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found );
    455 
    456             delete_site_meta( self::$site_id2, 'foo', 'bar' );
    457 
    458             $found = get_sites(
    459                 array(
    460                     'fields'     => 'ids',
    461                     'meta_query' => array(
    462                         array(
    463                             'key'   => 'foo',
    464                             'value' => 'bar',
    465                         ),
    466                     ),
    467                 )
    468             );
    469 
    470             $this->assertSameSets( array( self::$site_id ), $found );
    471         }
    472     }
    473 
    474 endif;
     257    public function test_lazy_load_site_meta() {
     258        if ( ! is_site_meta_supported() ) {
     259            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     260        }
     261
     262        $filter = new MockAction();
     263        add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     264
     265        $q = new WP_Site_Query(
     266            array(
     267                'ID' => self::$site_id,
     268            )
     269        );
     270
     271        $this->assertSameSets( array( (string) self::$site_id ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the first test site' );
     272
     273        $q = new WP_Site_Query(
     274            array(
     275                'ID' => self::$site_id2,
     276            )
     277        );
     278
     279        $this->assertSameSets( array( (string) self::$site_id2 ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the second test site' );
     280
     281        get_site_meta( self::$site_id2 );
     282
     283        $args     = $filter->get_args();
     284        $first    = reset( $args );
     285        $site_ids = end( $first );
     286        $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two site\'s meta' );
     287    }
     288
     289    /**
     290     * @ticket 58185
     291     */
     292    public function test_lazy_load_site_meta_fields_id() {
     293        if ( ! is_site_meta_supported() ) {
     294            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     295        }
     296
     297        $filter = new MockAction();
     298        add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     299
     300        $q = new WP_Site_Query(
     301            array(
     302                'ID'     => self::$site_id,
     303                'fields' => 'ids',
     304            )
     305        );
     306
     307        $this->assertSameSets( array( self::$site_id ), $q->sites, 'Site query should return the first test site' );
     308
     309        $q = new WP_Site_Query(
     310            array(
     311                'ID'     => self::$site_id2,
     312                'fields' => 'ids',
     313            )
     314        );
     315
     316        $this->assertSameSets( array( self::$site_id2 ), $q->sites, 'Site query should return the second test site' );
     317
     318        get_site_meta( self::$site_id2 );
     319
     320        $args     = $filter->get_args();
     321        $first    = reset( $args );
     322        $site_ids = end( $first );
     323        $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two sites meta' );
     324    }
     325
     326    public function test_query_update_site_meta_cache_false() {
     327        if ( ! is_site_meta_supported() ) {
     328            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     329        }
     330
     331        update_site_meta( self::$site_id, 'foo', 'bar' );
     332
     333        new WP_Site_Query(
     334            array(
     335                'ID'                     => self::$site_id,
     336                'update_site_meta_cache' => false,
     337            )
     338        );
     339
     340        $num_queries = get_num_queries();
     341        get_site_meta( self::$site_id, 'foo', true );
     342        $this->assertSame( 1, get_num_queries() - $num_queries );
     343    }
     344
     345    /**
     346     * @ticket 40229
     347     */
     348    public function test_add_site_meta_should_bust_get_sites_cache() {
     349        if ( ! is_site_meta_supported() ) {
     350            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     351        }
     352
     353        add_site_meta( self::$site_id, 'foo', 'bar' );
     354
     355        // Prime cache.
     356        $found = get_sites(
     357            array(
     358                'fields'     => 'ids',
     359                'meta_query' => array(
     360                    array(
     361                        'key'   => 'foo',
     362                        'value' => 'bar',
     363                    ),
     364                ),
     365            )
     366        );
     367
     368        $this->assertSameSets( array( self::$site_id ), $found );
     369
     370        add_site_meta( self::$site_id2, 'foo', 'bar' );
     371
     372        $found = get_sites(
     373            array(
     374                'fields'     => 'ids',
     375                'meta_query' => array(
     376                    array(
     377                        'key'   => 'foo',
     378                        'value' => 'bar',
     379                    ),
     380                ),
     381            )
     382        );
     383
     384        $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found );
     385    }
     386
     387    /**
     388     * @ticket 40229
     389     */
     390    public function test_update_site_meta_should_bust_get_sites_cache() {
     391        if ( ! is_site_meta_supported() ) {
     392            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     393        }
     394
     395        add_site_meta( self::$site_id, 'foo', 'bar' );
     396        add_site_meta( self::$site_id2, 'foo', 'baz' );
     397
     398        // Prime cache.
     399        $found = get_sites(
     400            array(
     401                'fields'     => 'ids',
     402                'meta_query' => array(
     403                    array(
     404                        'key'   => 'foo',
     405                        'value' => 'bar',
     406                    ),
     407                ),
     408            )
     409        );
     410
     411        $this->assertSameSets( array( self::$site_id ), $found );
     412
     413        update_site_meta( self::$site_id2, 'foo', 'bar' );
     414
     415        $found = get_sites(
     416            array(
     417                'fields'     => 'ids',
     418                'meta_query' => array(
     419                    array(
     420                        'key'   => 'foo',
     421                        'value' => 'bar',
     422                    ),
     423                ),
     424            )
     425        );
     426
     427        $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found );
     428    }
     429
     430    /**
     431     * @ticket 40229
     432     */
     433    public function test_delete_site_meta_should_bust_get_sites_cache() {
     434        if ( ! is_site_meta_supported() ) {
     435            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     436        }
     437
     438        add_site_meta( self::$site_id, 'foo', 'bar' );
     439        add_site_meta( self::$site_id2, 'foo', 'bar' );
     440
     441        // Prime cache.
     442        $found = get_sites(
     443            array(
     444                'fields'     => 'ids',
     445                'meta_query' => array(
     446                    array(
     447                        'key'   => 'foo',
     448                        'value' => 'bar',
     449                    ),
     450                ),
     451            )
     452        );
     453
     454        $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found );
     455
     456        delete_site_meta( self::$site_id2, 'foo', 'bar' );
     457
     458        $found = get_sites(
     459            array(
     460                'fields'     => 'ids',
     461                'meta_query' => array(
     462                    array(
     463                        'key'   => 'foo',
     464                        'value' => 'bar',
     465                    ),
     466                ),
     467            )
     468        );
     469
     470        $this->assertSameSets( array( self::$site_id ), $found );
     471    }
     472}
  • trunk/tests/phpunit/tests/multisite/updateBlogDetails.php

    r56547 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group ms-site
     6 * @group multisite
     7 */
     8class Tests_Multisite_UpdateBlogDetails extends WP_UnitTestCase {
    49
    510    /**
    6      * @group ms-site
    7      * @group multisite
     11     * If `update_blog_details()` is called with any kind of empty arguments, it
     12     * should return false.
    813     */
    9     class Tests_Multisite_UpdateBlogDetails extends WP_UnitTestCase {
     14    public function test_update_blog_details_with_empty_args() {
     15        $result = update_blog_details( 1, array() );
     16        $this->assertFalse( $result );
     17    }
    1018
    11         /**
    12          * If `update_blog_details()` is called with any kind of empty arguments, it
    13          * should return false.
    14          */
    15         public function test_update_blog_details_with_empty_args() {
    16             $result = update_blog_details( 1, array() );
    17             $this->assertFalse( $result );
     19    /**
     20     * If the ID passed is not that of a current site, we should expect false.
     21     */
     22    public function test_update_blog_details_invalid_blog_id() {
     23        $result = update_blog_details( 999, array( 'domain' => 'example.com' ) );
     24        $this->assertFalse( $result );
     25    }
     26
     27    public function test_update_blog_details() {
     28        $blog_id = self::factory()->blog->create();
     29
     30        $result = update_blog_details(
     31            $blog_id,
     32            array(
     33                'domain' => 'example.com',
     34                'path'   => 'my_path/',
     35            )
     36        );
     37
     38        $this->assertTrue( $result );
     39
     40        $blog = get_site( $blog_id );
     41
     42        $this->assertSame( 'example.com', $blog->domain );
     43        $this->assertSame( '/my_path/', $blog->path );
     44        $this->assertSame( '0', $blog->spam );
     45    }
     46
     47    /**
     48     * Test each of the actions that should fire in update_blog_details() depending on
     49     * the flag and flag value being set. Each action should fire once and should not
     50     * fire if a flag is already set for the given flag value.
     51     *
     52     * @param string $flag       The name of the flag being set or unset on a site.
     53     * @param string $flag_value '0' or '1'. The value of the flag being set.
     54     * @param string $action     The hook expected to fire for the flag name and flag combination.
     55     *
     56     * @dataProvider data_flag_hooks
     57     */
     58    public function test_update_blog_details_flag_action( $flag, $flag_value, $hook ) {
     59        $test_action_counter = new MockAction();
     60
     61        $blog_id = self::factory()->blog->create();
     62
     63        // Set an initial value of '1' for the flag when '0' is the flag value being tested.
     64        if ( '0' === $flag_value ) {
     65            update_blog_details( $blog_id, array( $flag => '1' ) );
    1866        }
    1967
    20         /**
    21          * If the ID passed is not that of a current site, we should expect false.
    22          */
    23         public function test_update_blog_details_invalid_blog_id() {
    24             $result = update_blog_details( 999, array( 'domain' => 'example.com' ) );
    25             $this->assertFalse( $result );
    26         }
     68        add_action( $hook, array( $test_action_counter, 'action' ) );
    2769
    28         public function test_update_blog_details() {
    29             $blog_id = self::factory()->blog->create();
     70        update_blog_details( $blog_id, array( $flag => $flag_value ) );
     71        $blog = get_site( $blog_id );
    3072
    31             $result = update_blog_details(
    32                 $blog_id,
    33                 array(
    34                     'domain' => 'example.com',
    35                     'path'   => 'my_path/',
    36                 )
    37             );
     73        $this->assertSame( $flag_value, $blog->{$flag} );
    3874
    39             $this->assertTrue( $result );
     75        // The hook attached to this flag should have fired once during update_blog_details().
     76        $this->assertSame( 1, $test_action_counter->get_call_count() );
    4077
    41             $blog = get_site( $blog_id );
     78        // Update the site to the exact same flag value for this flag.
     79        update_blog_details( $blog_id, array( $flag => $flag_value ) );
    4280
    43             $this->assertSame( 'example.com', $blog->domain );
    44             $this->assertSame( '/my_path/', $blog->path );
    45             $this->assertSame( '0', $blog->spam );
    46         }
     81        // The hook attached to this flag should not have fired again.
     82        $this->assertSame( 1, $test_action_counter->get_call_count() );
     83    }
    4784
    48         /**
    49          * Test each of the actions that should fire in update_blog_details() depending on
    50          * the flag and flag value being set. Each action should fire once and should not
    51          * fire if a flag is already set for the given flag value.
    52          *
    53          * @param string $flag       The name of the flag being set or unset on a site.
    54          * @param string $flag_value '0' or '1'. The value of the flag being set.
    55          * @param string $action     The hook expected to fire for the flag name and flag combination.
    56          *
    57          * @dataProvider data_flag_hooks
    58          */
    59         public function test_update_blog_details_flag_action( $flag, $flag_value, $hook ) {
    60             $test_action_counter = new MockAction();
     85    public function data_flag_hooks() {
     86        return array(
     87            array( 'spam', '0', 'make_ham_blog' ),
     88            array( 'spam', '1', 'make_spam_blog' ),
     89            array( 'archived', '1', 'archive_blog' ),
     90            array( 'archived', '0', 'unarchive_blog' ),
     91            array( 'deleted', '1', 'make_delete_blog' ),
     92            array( 'deleted', '0', 'make_undelete_blog' ),
     93            array( 'mature', '1', 'mature_blog' ),
     94            array( 'mature', '0', 'unmature_blog' ),
     95        );
     96    }
    6197
    62             $blog_id = self::factory()->blog->create();
     98    /**
     99     * When the path for a site is updated with update_blog_details(), the final path
     100     * should have a leading and trailing slash.
     101     *
     102     * @dataProvider data_single_directory_path
     103     */
     104    public function test_update_blog_details_single_directory_path( $path, $expected ) {
     105        update_blog_details( 1, array( 'path' => $path ) );
     106        $site = get_site( 1 );
    63107
    64             // Set an initial value of '1' for the flag when '0' is the flag value being tested.
    65             if ( '0' === $flag_value ) {
    66                 update_blog_details( $blog_id, array( $flag => '1' ) );
    67             }
     108        $this->assertSame( $expected, $site->path );
     109    }
    68110
    69             add_action( $hook, array( $test_action_counter, 'action' ) );
     111    public function data_single_directory_path() {
     112        return array(
     113            array( 'my_path', '/my_path/' ),
     114            array( 'my_path//', '/my_path/' ),
     115            array( '//my_path', '/my_path/' ),
     116            array( 'my_path/', '/my_path/' ),
     117            array( '/my_path', '/my_path/' ),
     118            array( '/my_path/', '/my_path/' ),
    70119
    71             update_blog_details( $blog_id, array( $flag => $flag_value ) );
    72             $blog = get_site( $blog_id );
     120            array( 'multiple/dirs', '/multiple/dirs/' ),
     121            array( '/multiple/dirs', '/multiple/dirs/' ),
     122            array( 'multiple/dirs/', '/multiple/dirs/' ),
     123            array( '/multiple/dirs/', '/multiple/dirs/' ),
    73124
    74             $this->assertSame( $flag_value, $blog->{$flag} );
    75 
    76             // The hook attached to this flag should have fired once during update_blog_details().
    77             $this->assertSame( 1, $test_action_counter->get_call_count() );
    78 
    79             // Update the site to the exact same flag value for this flag.
    80             update_blog_details( $blog_id, array( $flag => $flag_value ) );
    81 
    82             // The hook attached to this flag should not have fired again.
    83             $this->assertSame( 1, $test_action_counter->get_call_count() );
    84         }
    85 
    86         public function data_flag_hooks() {
    87             return array(
    88                 array( 'spam', '0', 'make_ham_blog' ),
    89                 array( 'spam', '1', 'make_spam_blog' ),
    90                 array( 'archived', '1', 'archive_blog' ),
    91                 array( 'archived', '0', 'unarchive_blog' ),
    92                 array( 'deleted', '1', 'make_delete_blog' ),
    93                 array( 'deleted', '0', 'make_undelete_blog' ),
    94                 array( 'mature', '1', 'mature_blog' ),
    95                 array( 'mature', '0', 'unmature_blog' ),
    96             );
    97         }
    98 
    99         /**
    100          * When the path for a site is updated with update_blog_details(), the final path
    101          * should have a leading and trailing slash.
    102          *
    103          * @dataProvider data_single_directory_path
    104          */
    105         public function test_update_blog_details_single_directory_path( $path, $expected ) {
    106             update_blog_details( 1, array( 'path' => $path ) );
    107             $site = get_site( 1 );
    108 
    109             $this->assertSame( $expected, $site->path );
    110         }
    111 
    112         public function data_single_directory_path() {
    113             return array(
    114                 array( 'my_path', '/my_path/' ),
    115                 array( 'my_path//', '/my_path/' ),
    116                 array( '//my_path', '/my_path/' ),
    117                 array( 'my_path/', '/my_path/' ),
    118                 array( '/my_path', '/my_path/' ),
    119                 array( '/my_path/', '/my_path/' ),
    120 
    121                 array( 'multiple/dirs', '/multiple/dirs/' ),
    122                 array( '/multiple/dirs', '/multiple/dirs/' ),
    123                 array( 'multiple/dirs/', '/multiple/dirs/' ),
    124                 array( '/multiple/dirs/', '/multiple/dirs/' ),
    125 
    126                 // update_blog_details() does not resolve multiple slashes in the middle of a path string.
    127                 array( 'multiple///dirs', '/multiple///dirs/' ),
    128             );
    129         }
     125            // update_blog_details() does not resolve multiple slashes in the middle of a path string.
     126            array( 'multiple///dirs', '/multiple///dirs/' ),
     127        );
    130128    }
    131 endif;
     129}
  • trunk/tests/phpunit/tests/multisite/updateBlogStatus.php

    r59964 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group ms-site
     6 * @group multisite
     7 */
     8class Tests_Multisite_UpdateBlogStatus extends WP_UnitTestCase {
    49
    510    /**
    6      * @group ms-site
    7      * @group multisite
     11     * Updating a field returns the same value that was passed.
    812     */
    9     class Tests_Multisite_UpdateBlogStatus extends WP_UnitTestCase {
    10 
    11         /**
    12          * Updating a field returns the same value that was passed.
    13          */
    14         public function test_update_blog_status() {
    15             $result = update_blog_status( 1, 'spam', 0 );
    16             $this->assertSame( 0, $result );
    17         }
    18 
    19         /**
    20          * Updating an invalid field returns the same value that was passed.
    21          */
    22         public function test_update_blog_status_invalid_status() {
    23             $result = update_blog_status( 1, 'doesnotexist', 'invalid' );
    24             $this->assertSame( 'invalid', $result );
    25         }
    26 
    27         public function test_update_blog_status_make_ham_blog_action() {
    28             $test_action_counter = new MockAction();
    29 
    30             $blog_id = self::factory()->blog->create();
    31             update_blog_details( $blog_id, array( 'spam' => 1 ) );
    32 
    33             add_action( 'make_ham_blog', array( $test_action_counter, 'action' ) );
    34             update_blog_status( $blog_id, 'spam', 0 );
    35             $blog = get_site( $blog_id );
    36 
    37             $this->assertSame( '0', $blog->spam );
    38             $this->assertSame( 1, $test_action_counter->get_call_count() );
    39 
    40             // The action should not fire if the status of 'spam' stays the same.
    41             update_blog_status( $blog_id, 'spam', 0 );
    42             $blog = get_site( $blog_id );
    43 
    44             $this->assertSame( '0', $blog->spam );
    45             $this->assertSame( 1, $test_action_counter->get_call_count() );
    46         }
    47 
    48         /**
    49          * @group external-http
    50          */
    51         public function test_content_from_spam_blog_is_not_available() {
    52             $spam_blog_id = self::factory()->blog->create();
    53             switch_to_blog( $spam_blog_id );
    54             $post_data      = array(
    55                 'post_title'   => 'Hello World!',
    56                 'post_content' => 'Hello world content',
    57             );
    58             $post_id        = self::factory()->post->create( $post_data );
    59             $post           = get_post( $post_id );
    60             $spam_permalink = site_url() . '/?p=' . $post->ID;
    61             $spam_embed_url = get_post_embed_url( $post_id );
    62 
    63             restore_current_blog();
    64             $this->assertNotEmpty( $spam_permalink );
    65             $this->assertSame( $post_data['post_title'], $post->post_title );
    66 
    67             update_blog_status( $spam_blog_id, 'spam', 1 );
    68 
    69             $post_id = self::factory()->post->create(
    70                 array(
    71                     'post_content' => "\n $spam_permalink \n",
    72                 )
    73             );
    74             $post    = get_post( $post_id );
    75             $content = apply_filters( 'the_content', $post->post_content );
    76 
    77             $this->assertStringNotContainsString( $post_data['post_title'], $content );
    78             $this->assertStringNotContainsString( "src=\"{$spam_embed_url}#?", $content );
    79         }
    80 
    81         public function test_update_blog_status_make_spam_blog_action() {
    82             $test_action_counter = new MockAction();
    83 
    84             $blog_id = self::factory()->blog->create();
    85 
    86             add_action( 'make_spam_blog', array( $test_action_counter, 'action' ) );
    87             update_blog_status( $blog_id, 'spam', 1 );
    88             $blog = get_site( $blog_id );
    89 
    90             $this->assertSame( '1', $blog->spam );
    91             $this->assertSame( 1, $test_action_counter->get_call_count() );
    92 
    93             // The action should not fire if the status of 'spam' stays the same.
    94             update_blog_status( $blog_id, 'spam', 1 );
    95             $blog = get_site( $blog_id );
    96 
    97             $this->assertSame( '1', $blog->spam );
    98             $this->assertSame( 1, $test_action_counter->get_call_count() );
    99         }
    100 
    101         public function test_update_blog_status_archive_blog_action() {
    102             $test_action_counter = new MockAction();
    103 
    104             $blog_id = self::factory()->blog->create();
    105 
    106             add_action( 'archive_blog', array( $test_action_counter, 'action' ) );
    107             update_blog_status( $blog_id, 'archived', 1 );
    108             $blog = get_site( $blog_id );
    109 
    110             $this->assertSame( '1', $blog->archived );
    111             $this->assertSame( 1, $test_action_counter->get_call_count() );
    112 
    113             // The action should not fire if the status of 'archived' stays the same.
    114             update_blog_status( $blog_id, 'archived', 1 );
    115             $blog = get_site( $blog_id );
    116 
    117             $this->assertSame( '1', $blog->archived );
    118             $this->assertSame( 1, $test_action_counter->get_call_count() );
    119         }
    120 
    121         public function test_update_blog_status_unarchive_blog_action() {
    122             $test_action_counter = new MockAction();
    123 
    124             $blog_id = self::factory()->blog->create();
    125             update_blog_details( $blog_id, array( 'archived' => 1 ) );
    126 
    127             add_action( 'unarchive_blog', array( $test_action_counter, 'action' ) );
    128             update_blog_status( $blog_id, 'archived', 0 );
    129             $blog = get_site( $blog_id );
    130 
    131             $this->assertSame( '0', $blog->archived );
    132             $this->assertSame( 1, $test_action_counter->get_call_count() );
    133 
    134             // The action should not fire if the status of 'archived' stays the same.
    135             update_blog_status( $blog_id, 'archived', 0 );
    136             $blog = get_site( $blog_id );
    137             $this->assertSame( '0', $blog->archived );
    138             $this->assertSame( 1, $test_action_counter->get_call_count() );
    139         }
    140 
    141         public function test_update_blog_status_make_delete_blog_action() {
    142             $test_action_counter = new MockAction();
    143 
    144             $blog_id = self::factory()->blog->create();
    145 
    146             add_action( 'make_delete_blog', array( $test_action_counter, 'action' ) );
    147             update_blog_status( $blog_id, 'deleted', 1 );
    148             $blog = get_site( $blog_id );
    149 
    150             $this->assertSame( '1', $blog->deleted );
    151             $this->assertSame( 1, $test_action_counter->get_call_count() );
    152 
    153             // The action should not fire if the status of 'deleted' stays the same.
    154             update_blog_status( $blog_id, 'deleted', 1 );
    155             $blog = get_site( $blog_id );
    156 
    157             $this->assertSame( '1', $blog->deleted );
    158             $this->assertSame( 1, $test_action_counter->get_call_count() );
    159         }
    160 
    161         public function test_update_blog_status_make_undelete_blog_action() {
    162             $test_action_counter = new MockAction();
    163 
    164             $blog_id = self::factory()->blog->create();
    165             update_blog_details( $blog_id, array( 'deleted' => 1 ) );
    166 
    167             add_action( 'make_undelete_blog', array( $test_action_counter, 'action' ) );
    168             update_blog_status( $blog_id, 'deleted', 0 );
    169             $blog = get_site( $blog_id );
    170 
    171             $this->assertSame( '0', $blog->deleted );
    172             $this->assertSame( 1, $test_action_counter->get_call_count() );
    173 
    174             // The action should not fire if the status of 'deleted' stays the same.
    175             update_blog_status( $blog_id, 'deleted', 0 );
    176             $blog = get_site( $blog_id );
    177 
    178             $this->assertSame( '0', $blog->deleted );
    179             $this->assertSame( 1, $test_action_counter->get_call_count() );
    180         }
    181 
    182         public function test_update_blog_status_mature_blog_action() {
    183             $test_action_counter = new MockAction();
    184 
    185             $blog_id = self::factory()->blog->create();
    186 
    187             add_action( 'mature_blog', array( $test_action_counter, 'action' ) );
    188             update_blog_status( $blog_id, 'mature', 1 );
    189             $blog = get_site( $blog_id );
    190 
    191             $this->assertSame( '1', $blog->mature );
    192             $this->assertSame( 1, $test_action_counter->get_call_count() );
    193 
    194             // The action should not fire if the status of 'mature' stays the same.
    195             update_blog_status( $blog_id, 'mature', 1 );
    196             $blog = get_site( $blog_id );
    197 
    198             $this->assertSame( '1', $blog->mature );
    199             $this->assertSame( 1, $test_action_counter->get_call_count() );
    200         }
    201 
    202         public function test_update_blog_status_unmature_blog_action() {
    203             $test_action_counter = new MockAction();
    204 
    205             $blog_id = self::factory()->blog->create();
    206             update_blog_details( $blog_id, array( 'mature' => 1 ) );
    207 
    208             add_action( 'unmature_blog', array( $test_action_counter, 'action' ) );
    209             update_blog_status( $blog_id, 'mature', 0 );
    210 
    211             $blog = get_site( $blog_id );
    212             $this->assertSame( '0', $blog->mature );
    213             $this->assertSame( 1, $test_action_counter->get_call_count() );
    214 
    215             // The action should not fire if the status of 'mature' stays the same.
    216             update_blog_status( $blog_id, 'mature', 0 );
    217             $blog = get_site( $blog_id );
    218 
    219             $this->assertSame( '0', $blog->mature );
    220             $this->assertSame( 1, $test_action_counter->get_call_count() );
    221         }
    222 
    223         public function test_update_blog_status_update_blog_public_action() {
    224             $test_action_counter = new MockAction();
    225 
    226             $blog_id = self::factory()->blog->create();
    227 
    228             add_action( 'update_blog_public', array( $test_action_counter, 'action' ) );
    229             update_blog_status( $blog_id, 'public', 0 );
    230 
    231             $blog = get_site( $blog_id );
    232             $this->assertSame( '0', $blog->public );
    233             $this->assertSame( 1, $test_action_counter->get_call_count() );
    234 
    235             // The action should not fire if the status of 'mature' stays the same.
    236             update_blog_status( $blog_id, 'public', 0 );
    237             $blog = get_site( $blog_id );
    238 
    239             $this->assertSame( '0', $blog->public );
    240             $this->assertSame( 1, $test_action_counter->get_call_count() );
    241         }
    242     }
    243 
    244 endif;
     13    public function test_update_blog_status() {
     14        $result = update_blog_status( 1, 'spam', 0 );
     15        $this->assertSame( 0, $result );
     16    }
     17
     18    /**
     19     * Updating an invalid field returns the same value that was passed.
     20     */
     21    public function test_update_blog_status_invalid_status() {
     22        $result = update_blog_status( 1, 'doesnotexist', 'invalid' );
     23        $this->assertSame( 'invalid', $result );
     24    }
     25
     26    public function test_update_blog_status_make_ham_blog_action() {
     27        $test_action_counter = new MockAction();
     28
     29        $blog_id = self::factory()->blog->create();
     30        update_blog_details( $blog_id, array( 'spam' => 1 ) );
     31
     32        add_action( 'make_ham_blog', array( $test_action_counter, 'action' ) );
     33        update_blog_status( $blog_id, 'spam', 0 );
     34        $blog = get_site( $blog_id );
     35
     36        $this->assertSame( '0', $blog->spam );
     37        $this->assertSame( 1, $test_action_counter->get_call_count() );
     38
     39        // The action should not fire if the status of 'spam' stays the same.
     40        update_blog_status( $blog_id, 'spam', 0 );
     41        $blog = get_site( $blog_id );
     42
     43        $this->assertSame( '0', $blog->spam );
     44        $this->assertSame( 1, $test_action_counter->get_call_count() );
     45    }
     46
     47    /**
     48     * @group external-http
     49     */
     50    public function test_content_from_spam_blog_is_not_available() {
     51        $spam_blog_id = self::factory()->blog->create();
     52        switch_to_blog( $spam_blog_id );
     53        $post_data      = array(
     54            'post_title'   => 'Hello World!',
     55            'post_content' => 'Hello world content',
     56        );
     57        $post_id        = self::factory()->post->create( $post_data );
     58        $post           = get_post( $post_id );
     59        $spam_permalink = site_url() . '/?p=' . $post->ID;
     60        $spam_embed_url = get_post_embed_url( $post_id );
     61
     62        restore_current_blog();
     63        $this->assertNotEmpty( $spam_permalink );
     64        $this->assertSame( $post_data['post_title'], $post->post_title );
     65
     66        update_blog_status( $spam_blog_id, 'spam', 1 );
     67
     68        $post_id = self::factory()->post->create(
     69            array(
     70                'post_content' => "\n $spam_permalink \n",
     71            )
     72        );
     73        $post    = get_post( $post_id );
     74        $content = apply_filters( 'the_content', $post->post_content );
     75
     76        $this->assertStringNotContainsString( $post_data['post_title'], $content );
     77        $this->assertStringNotContainsString( "src=\"{$spam_embed_url}#?", $content );
     78    }
     79
     80    public function test_update_blog_status_make_spam_blog_action() {
     81        $test_action_counter = new MockAction();
     82
     83        $blog_id = self::factory()->blog->create();
     84
     85        add_action( 'make_spam_blog', array( $test_action_counter, 'action' ) );
     86        update_blog_status( $blog_id, 'spam', 1 );
     87        $blog = get_site( $blog_id );
     88
     89        $this->assertSame( '1', $blog->spam );
     90        $this->assertSame( 1, $test_action_counter->get_call_count() );
     91
     92        // The action should not fire if the status of 'spam' stays the same.
     93        update_blog_status( $blog_id, 'spam', 1 );
     94        $blog = get_site( $blog_id );
     95
     96        $this->assertSame( '1', $blog->spam );
     97        $this->assertSame( 1, $test_action_counter->get_call_count() );
     98    }
     99
     100    public function test_update_blog_status_archive_blog_action() {
     101        $test_action_counter = new MockAction();
     102
     103        $blog_id = self::factory()->blog->create();
     104
     105        add_action( 'archive_blog', array( $test_action_counter, 'action' ) );
     106        update_blog_status( $blog_id, 'archived', 1 );
     107        $blog = get_site( $blog_id );
     108
     109        $this->assertSame( '1', $blog->archived );
     110        $this->assertSame( 1, $test_action_counter->get_call_count() );
     111
     112        // The action should not fire if the status of 'archived' stays the same.
     113        update_blog_status( $blog_id, 'archived', 1 );
     114        $blog = get_site( $blog_id );
     115
     116        $this->assertSame( '1', $blog->archived );
     117        $this->assertSame( 1, $test_action_counter->get_call_count() );
     118    }
     119
     120    public function test_update_blog_status_unarchive_blog_action() {
     121        $test_action_counter = new MockAction();
     122
     123        $blog_id = self::factory()->blog->create();
     124        update_blog_details( $blog_id, array( 'archived' => 1 ) );
     125
     126        add_action( 'unarchive_blog', array( $test_action_counter, 'action' ) );
     127        update_blog_status( $blog_id, 'archived', 0 );
     128        $blog = get_site( $blog_id );
     129
     130        $this->assertSame( '0', $blog->archived );
     131        $this->assertSame( 1, $test_action_counter->get_call_count() );
     132
     133        // The action should not fire if the status of 'archived' stays the same.
     134        update_blog_status( $blog_id, 'archived', 0 );
     135        $blog = get_site( $blog_id );
     136        $this->assertSame( '0', $blog->archived );
     137        $this->assertSame( 1, $test_action_counter->get_call_count() );
     138    }
     139
     140    public function test_update_blog_status_make_delete_blog_action() {
     141        $test_action_counter = new MockAction();
     142
     143        $blog_id = self::factory()->blog->create();
     144
     145        add_action( 'make_delete_blog', array( $test_action_counter, 'action' ) );
     146        update_blog_status( $blog_id, 'deleted', 1 );
     147        $blog = get_site( $blog_id );
     148
     149        $this->assertSame( '1', $blog->deleted );
     150        $this->assertSame( 1, $test_action_counter->get_call_count() );
     151
     152        // The action should not fire if the status of 'deleted' stays the same.
     153        update_blog_status( $blog_id, 'deleted', 1 );
     154        $blog = get_site( $blog_id );
     155
     156        $this->assertSame( '1', $blog->deleted );
     157        $this->assertSame( 1, $test_action_counter->get_call_count() );
     158    }
     159
     160    public function test_update_blog_status_make_undelete_blog_action() {
     161        $test_action_counter = new MockAction();
     162
     163        $blog_id = self::factory()->blog->create();
     164        update_blog_details( $blog_id, array( 'deleted' => 1 ) );
     165
     166        add_action( 'make_undelete_blog', array( $test_action_counter, 'action' ) );
     167        update_blog_status( $blog_id, 'deleted', 0 );
     168        $blog = get_site( $blog_id );
     169
     170        $this->assertSame( '0', $blog->deleted );
     171        $this->assertSame( 1, $test_action_counter->get_call_count() );
     172
     173        // The action should not fire if the status of 'deleted' stays the same.
     174        update_blog_status( $blog_id, 'deleted', 0 );
     175        $blog = get_site( $blog_id );
     176
     177        $this->assertSame( '0', $blog->deleted );
     178        $this->assertSame( 1, $test_action_counter->get_call_count() );
     179    }
     180
     181    public function test_update_blog_status_mature_blog_action() {
     182        $test_action_counter = new MockAction();
     183
     184        $blog_id = self::factory()->blog->create();
     185
     186        add_action( 'mature_blog', array( $test_action_counter, 'action' ) );
     187        update_blog_status( $blog_id, 'mature', 1 );
     188        $blog = get_site( $blog_id );
     189
     190        $this->assertSame( '1', $blog->mature );
     191        $this->assertSame( 1, $test_action_counter->get_call_count() );
     192
     193        // The action should not fire if the status of 'mature' stays the same.
     194        update_blog_status( $blog_id, 'mature', 1 );
     195        $blog = get_site( $blog_id );
     196
     197        $this->assertSame( '1', $blog->mature );
     198        $this->assertSame( 1, $test_action_counter->get_call_count() );
     199    }
     200
     201    public function test_update_blog_status_unmature_blog_action() {
     202        $test_action_counter = new MockAction();
     203
     204        $blog_id = self::factory()->blog->create();
     205        update_blog_details( $blog_id, array( 'mature' => 1 ) );
     206
     207        add_action( 'unmature_blog', array( $test_action_counter, 'action' ) );
     208        update_blog_status( $blog_id, 'mature', 0 );
     209
     210        $blog = get_site( $blog_id );
     211        $this->assertSame( '0', $blog->mature );
     212        $this->assertSame( 1, $test_action_counter->get_call_count() );
     213
     214        // The action should not fire if the status of 'mature' stays the same.
     215        update_blog_status( $blog_id, 'mature', 0 );
     216        $blog = get_site( $blog_id );
     217
     218        $this->assertSame( '0', $blog->mature );
     219        $this->assertSame( 1, $test_action_counter->get_call_count() );
     220    }
     221
     222    public function test_update_blog_status_update_blog_public_action() {
     223        $test_action_counter = new MockAction();
     224
     225        $blog_id = self::factory()->blog->create();
     226
     227        add_action( 'update_blog_public', array( $test_action_counter, 'action' ) );
     228        update_blog_status( $blog_id, 'public', 0 );
     229
     230        $blog = get_site( $blog_id );
     231        $this->assertSame( '0', $blog->public );
     232        $this->assertSame( 1, $test_action_counter->get_call_count() );
     233
     234        // The action should not fire if the status of 'mature' stays the same.
     235        update_blog_status( $blog_id, 'public', 0 );
     236        $blog = get_site( $blog_id );
     237
     238        $this->assertSame( '0', $blog->public );
     239        $this->assertSame( 1, $test_action_counter->get_call_count() );
     240    }
     241}
  • trunk/tests/phpunit/tests/multisite/updatePostsCount.php

    r55419 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Test that update_posts_count() gets called via default filters on multisite.
     5 *
     6 * @group ms-required
     7 * @group ms-site
     8 * @group multisite
     9 *
     10 * @covers ::update_posts_count
     11 */
     12class Tests_Multisite_UpdatePostsCount extends WP_UnitTestCase {
     13
    414    /**
    5      * Test that update_posts_count() gets called via default filters on multisite.
     15     * Tests that posts count is updated correctly when posts are added or deleted.
    616     *
    7      * @group ms-site
    8      * @group multisite
     17     * @ticket 27952
     18     * @ticket 53443
    919     *
    10      * @covers ::update_posts_count
     20     * @covers ::_update_posts_count_on_transition_post_status
     21     * @covers ::_update_posts_count_on_delete
    1122     */
    12     class Tests_Multisite_UpdatePostsCount extends WP_UnitTestCase {
     23    public function test_update_posts_count() {
     24        $blog_id = self::factory()->blog->create();
     25        switch_to_blog( $blog_id );
    1326
    14         /**
    15          * Tests that posts count is updated correctly when posts are added or deleted.
     27        $original_post_count = (int) get_site()->post_count;
     28
     29        $post_id = self::factory()->post->create();
     30
     31        $post_count_after_creating = get_site()->post_count;
     32
     33        wp_delete_post( $post_id, true );
     34
     35        $post_count_after_deleting = get_site()->post_count;
     36
     37        restore_current_blog();
     38
     39        /*
     40         * Check that posts count is updated when a post is created:
     41         * add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 );
    1642         *
    17          * @ticket 27952
    18          * @ticket 53443
     43         * Check that _update_posts_count_on_transition_post_status() is called on that filter,
     44         * which then calls update_posts_count() to update the count.
     45         */
     46        $this->assertSame( $original_post_count + 1, $post_count_after_creating, 'Post count should be incremented by 1.' );
     47
     48        /*
     49         * Check that posts count is updated when a post is deleted:
     50         * add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 );
    1951         *
    20          * @covers ::_update_posts_count_on_transition_post_status
    21          * @covers ::_update_posts_count_on_delete
     52         * Check that _update_posts_count_on_delete() is called on that filter,
     53         * which then calls update_posts_count() to update the count.
    2254         */
    23         public function test_update_posts_count() {
    24             $blog_id = self::factory()->blog->create();
    25             switch_to_blog( $blog_id );
    26 
    27             $original_post_count = (int) get_site()->post_count;
    28 
    29             $post_id = self::factory()->post->create();
    30 
    31             $post_count_after_creating = get_site()->post_count;
    32 
    33             wp_delete_post( $post_id, true );
    34 
    35             $post_count_after_deleting = get_site()->post_count;
    36 
    37             restore_current_blog();
    38 
    39             /*
    40              * Check that posts count is updated when a post is created:
    41              * add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 );
    42              *
    43              * Check that _update_posts_count_on_transition_post_status() is called on that filter,
    44              * which then calls update_posts_count() to update the count.
    45              */
    46             $this->assertSame( $original_post_count + 1, $post_count_after_creating, 'Post count should be incremented by 1.' );
    47 
    48             /*
    49              * Check that posts count is updated when a post is deleted:
    50              * add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 );
    51              *
    52              * Check that _update_posts_count_on_delete() is called on that filter,
    53              * which then calls update_posts_count() to update the count.
    54              */
    55             $this->assertSame( $original_post_count, $post_count_after_deleting, 'Post count should match the original count.' );
    56         }
     55        $this->assertSame( $original_post_count, $post_count_after_deleting, 'Post count should match the original count.' );
    5756    }
    58 
    59 endif;
     57}
  • trunk/tests/phpunit/tests/multisite/uploadIsUserOverQuota.php

    r51869 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Tests specific to `upload_is_user_over_quota()` in multisite.
     5 *
     6 * These tests filter `get_space_allowed` and `pre_get_space_used` in
     7 * most cases as those are tested elsewhere.
     8 *
     9 * @group ms-required
     10 * @group multisite
     11 */
     12class Tests_Multisite_UploadIsUserOverQuota extends WP_UnitTestCase {
     13
     14    public function set_up() {
     15        parent::set_up();
     16        update_site_option( 'upload_space_check_disabled', false );
     17    }
     18
     19    public function test_upload_is_user_over_quota_allowed_0_used_5() {
     20        add_filter( 'get_space_allowed', '__return_zero' );
     21        add_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
     22        $result = upload_is_user_over_quota( false );
     23        remove_filter( 'get_space_allowed', '__return_zero' );
     24        remove_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
     25
     26        $this->assertTrue( $result );
     27    }
     28
     29    public function test_upload_is_user_over_quota_allowed_0_used_0() {
     30        add_filter( 'get_space_allowed', '__return_zero' );
     31        add_filter( 'pre_get_space_used', '__return_zero' );
     32        $result = upload_is_user_over_quota( false );
     33        remove_filter( 'get_space_allowed', '__return_zero' );
     34        remove_filter( 'pre_get_space_used', '__return_zero' );
     35
     36        $this->assertFalse( $result );
     37    }
     38
     39    public function test_upload_is_user_over_quota_allowed_0_used_100() {
     40        add_filter( 'get_space_allowed', '__return_zero' );
     41        add_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
     42        $result = upload_is_user_over_quota( false );
     43        remove_filter( 'get_space_allowed', '__return_zero' );
     44        remove_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
     45
     46        $this->assertTrue( $result );
     47    }
     48
     49    public function test_upload_is_user_over_quota_allowed_100_used_0() {
     50        add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     51        add_filter( 'pre_get_space_used', '__return_zero' );
     52        $result = upload_is_user_over_quota( false );
     53        remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     54        remove_filter( 'pre_get_space_used', '__return_zero' );
     55
     56        $this->assertFalse( $result );
     57    }
     58
     59    public function test_upload_is_user_over_quota_allowed_100_used_100() {
     60        add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     61        add_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
     62        $result = upload_is_user_over_quota( false );
     63        remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     64        remove_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
     65
     66        $this->assertFalse( $result );
     67    }
     68
     69    public function test_upload_is_user_over_quota_allowed_100_used_200() {
     70        add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     71        add_filter( 'pre_get_space_used', array( $this, '_filter_space_200' ) );
     72        $result = upload_is_user_over_quota( false );
     73        remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     74        remove_filter( 'pre_get_space_used', array( $this, '_filter_space_200' ) );
     75
     76        $this->assertTrue( $result );
     77    }
     78
     79    public function test_upload_is_user_over_quota_allowed_negative_used_100() {
     80        add_filter( 'get_space_allowed', array( $this, '_filter_space_negative' ) );
     81        add_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
     82        $result = upload_is_user_over_quota( false );
     83        remove_filter( 'get_space_allowed', array( $this, '_filter_space_negative' ) );
     84        remove_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
     85
     86        $this->assertTrue( $result );
     87    }
    488
    589    /**
    6      * Tests specific to `upload_is_user_over_quota()` in multisite.
    7      *
    8      * These tests filter `get_space_allowed` and `pre_get_space_used` in
    9      * most cases as those are tested elsewhere.
    10      *
    11      * @group multisite
     90     * When the upload space check is disabled, using more than the available
     91     * quota is allowed.
    1292     */
    13     class Tests_Multisite_UploadIsUserOverQuota extends WP_UnitTestCase {
     93    public function test_upload_is_user_over_check_disabled() {
     94        update_site_option( 'upload_space_check_disabled', true );
    1495
    15         public function set_up() {
    16             parent::set_up();
    17             update_site_option( 'upload_space_check_disabled', false );
    18         }
     96        add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     97        add_filter( 'pre_get_space_used', array( $this, '_filter_space_200' ) );
     98        $result = upload_is_user_over_quota( false );
     99        remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     100        remove_filter( 'pre_get_space_used', array( $this, '_filter_space_200' ) );
    19101
    20         public function test_upload_is_user_over_quota_allowed_0_used_5() {
    21             add_filter( 'get_space_allowed', '__return_zero' );
    22             add_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
    23             $result = upload_is_user_over_quota( false );
    24             remove_filter( 'get_space_allowed', '__return_zero' );
    25             remove_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
    26 
    27             $this->assertTrue( $result );
    28         }
    29 
    30         public function test_upload_is_user_over_quota_allowed_0_used_0() {
    31             add_filter( 'get_space_allowed', '__return_zero' );
    32             add_filter( 'pre_get_space_used', '__return_zero' );
    33             $result = upload_is_user_over_quota( false );
    34             remove_filter( 'get_space_allowed', '__return_zero' );
    35             remove_filter( 'pre_get_space_used', '__return_zero' );
    36 
    37             $this->assertFalse( $result );
    38         }
    39 
    40         public function test_upload_is_user_over_quota_allowed_0_used_100() {
    41             add_filter( 'get_space_allowed', '__return_zero' );
    42             add_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
    43             $result = upload_is_user_over_quota( false );
    44             remove_filter( 'get_space_allowed', '__return_zero' );
    45             remove_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
    46 
    47             $this->assertTrue( $result );
    48         }
    49 
    50         public function test_upload_is_user_over_quota_allowed_100_used_0() {
    51             add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
    52             add_filter( 'pre_get_space_used', '__return_zero' );
    53             $result = upload_is_user_over_quota( false );
    54             remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
    55             remove_filter( 'pre_get_space_used', '__return_zero' );
    56 
    57             $this->assertFalse( $result );
    58         }
    59 
    60         public function test_upload_is_user_over_quota_allowed_100_used_100() {
    61             add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
    62             add_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
    63             $result = upload_is_user_over_quota( false );
    64             remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
    65             remove_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
    66 
    67             $this->assertFalse( $result );
    68         }
    69 
    70         public function test_upload_is_user_over_quota_allowed_100_used_200() {
    71             add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
    72             add_filter( 'pre_get_space_used', array( $this, '_filter_space_200' ) );
    73             $result = upload_is_user_over_quota( false );
    74             remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
    75             remove_filter( 'pre_get_space_used', array( $this, '_filter_space_200' ) );
    76 
    77             $this->assertTrue( $result );
    78         }
    79 
    80         public function test_upload_is_user_over_quota_allowed_negative_used_100() {
    81             add_filter( 'get_space_allowed', array( $this, '_filter_space_negative' ) );
    82             add_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
    83             $result = upload_is_user_over_quota( false );
    84             remove_filter( 'get_space_allowed', array( $this, '_filter_space_negative' ) );
    85             remove_filter( 'pre_get_space_used', array( $this, '_filter_space_100' ) );
    86 
    87             $this->assertTrue( $result );
    88         }
    89 
    90         /**
    91          * When the upload space check is disabled, using more than the available
    92          * quota is allowed.
    93          */
    94         public function test_upload_is_user_over_check_disabled() {
    95             update_site_option( 'upload_space_check_disabled', true );
    96 
    97             add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
    98             add_filter( 'pre_get_space_used', array( $this, '_filter_space_200' ) );
    99             $result = upload_is_user_over_quota( false );
    100             remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
    101             remove_filter( 'pre_get_space_used', array( $this, '_filter_space_200' ) );
    102 
    103             $this->assertFalse( $result );
    104         }
    105 
    106         public function _filter_space_5() {
    107             return 5;
    108         }
    109 
    110         public function _filter_space_100() {
    111             return 100;
    112         }
    113 
    114         public function _filter_space_200() {
    115             return 200;
    116         }
    117 
    118         public function _filter_space_negative() {
    119             return -1;
    120         }
     102        $this->assertFalse( $result );
    121103    }
    122104
    123 endif;
     105    public function _filter_space_5() {
     106        return 5;
     107    }
     108
     109    public function _filter_space_100() {
     110        return 100;
     111    }
     112
     113    public function _filter_space_200() {
     114        return 200;
     115    }
     116
     117    public function _filter_space_negative() {
     118        return -1;
     119    }
     120}
  • trunk/tests/phpunit/tests/multisite/wpCountSites.php

    r54726 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group ms-site
     6 * @group multisite
     7 */
     8class Tests_Multisite_wpCountSites extends WP_UnitTestCase {
    49
    510    /**
    6      * @group ms-site
    7      * @group multisite
     11     * @ticket 37392
    812     */
    9     class Tests_Multisite_wpCountSites extends WP_UnitTestCase {
     13    public function test_wp_count_sites() {
     14        // Create a random number of sites with each status.
     15        $site_ids = array(
     16            'public'   => self::factory()->blog->create_many(
     17                random_int( 0, 5 ),
     18                array(
     19                    'public' => 1,
     20                )
     21            ),
     22            'archived' => self::factory()->blog->create_many(
     23                random_int( 0, 5 ),
     24                array(
     25                    'public'   => 0,
     26                    'archived' => 1,
     27                )
     28            ),
     29            'mature'   => self::factory()->blog->create_many(
     30                random_int( 0, 5 ),
     31                array(
     32                    'public' => 0,
     33                    'mature' => 1,
     34                )
     35            ),
     36            'spam'     => self::factory()->blog->create_many(
     37                random_int( 0, 5 ),
     38                array(
     39                    'public' => 0,
     40                    'spam'   => 1,
     41                )
     42            ),
     43            'deleted'  => self::factory()->blog->create_many(
     44                random_int( 0, 5 ),
     45                array(
     46                    'public'  => 0,
     47                    'deleted' => 1,
     48                )
     49            ),
     50        );
    1051
    11         /**
    12          * @ticket 37392
    13          */
    14         public function test_wp_count_sites() {
    15             // Create a random number of sites with each status.
    16             $site_ids = array(
    17                 'public'   => self::factory()->blog->create_many(
    18                     random_int( 0, 5 ),
    19                     array(
    20                         'public' => 1,
    21                     )
    22                 ),
    23                 'archived' => self::factory()->blog->create_many(
    24                     random_int( 0, 5 ),
    25                     array(
    26                         'public'   => 0,
    27                         'archived' => 1,
    28                     )
    29                 ),
    30                 'mature'   => self::factory()->blog->create_many(
    31                     random_int( 0, 5 ),
    32                     array(
    33                         'public' => 0,
    34                         'mature' => 1,
    35                     )
    36                 ),
    37                 'spam'     => self::factory()->blog->create_many(
    38                     random_int( 0, 5 ),
    39                     array(
    40                         'public' => 0,
    41                         'spam'   => 1,
    42                     )
    43                 ),
    44                 'deleted'  => self::factory()->blog->create_many(
    45                     random_int( 0, 5 ),
    46                     array(
    47                         'public'  => 0,
    48                         'deleted' => 1,
    49                     )
    50                 ),
    51             );
     52        $counts = wp_count_sites();
    5253
    53             $counts = wp_count_sites();
     54        $counts_by_status = array_map( 'count', $site_ids );
     55        $expected         = array_merge(
     56            array( 'all' => array_sum( $counts_by_status ) ),
     57            $counts_by_status
     58        );
     59        // Add 1 to all & public for the main site.
     60        $expected['all']    += 1;
     61        $expected['public'] += 1;
    5462
    55             $counts_by_status = array_map( 'count', $site_ids );
    56             $expected         = array_merge(
    57                 array( 'all' => array_sum( $counts_by_status ) ),
    58                 $counts_by_status
    59             );
    60             // Add 1 to all & public for the main site.
    61             $expected['all']    += 1;
    62             $expected['public'] += 1;
    63 
    64             $this->assertSame( $expected, $counts );
    65         }
     63        $this->assertSame( $expected, $counts );
    6664    }
    67 
    68 endif;
     65}
  • trunk/tests/phpunit/tests/multisite/wpGetSites.php

    r51860 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group ms-site
     6 * @group multisite
     7 */
     8class Tests_Multisite_wpGetSites extends WP_UnitTestCase {
     9    protected static $site_ids;
     10
     11    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     12        self::$site_ids = array(
     13            'w.org/'      => array(
     14                'domain'     => 'w.org',
     15                'path'       => '/',
     16                'network_id' => 2,
     17            ),
     18            'wp.org/'     => array(
     19                'domain'     => 'wp.org',
     20                'path'       => '/',
     21                'network_id' => 2,
     22                'public'     => 0,
     23            ),
     24            'wp.org/foo/' => array(
     25                'domain'     => 'wp.org',
     26                'path'       => '/foo/',
     27                'network_id' => 1,
     28                'public'     => 0,
     29            ),
     30            'wp.org/oof/' => array(
     31                'domain' => 'wp.org',
     32                'path'   => '/oof/',
     33            ),
     34        );
     35
     36        foreach ( self::$site_ids as &$id ) {
     37            $id = $factory->blog->create( $id );
     38        }
     39        unset( $id );
     40    }
     41
     42    public static function wpTearDownAfterClass() {
     43        foreach ( self::$site_ids as $id ) {
     44            wp_delete_site( $id );
     45        }
     46
     47        wp_update_network_site_counts();
     48    }
    449
    550    /**
    6      * @group wp-get-site
    7      * @group ms-site
    8      * @group multisite
     51     * @expectedDeprecated wp_get_sites
    952     */
    10     class Tests_Multisite_wpGetSites extends WP_UnitTestCase {
    11         protected static $site_ids;
     53    public function test_wp_get_sites_site_is_expected_array() {
    1254
    13         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    14             self::$site_ids = array(
    15                 'w.org/'      => array(
    16                     'domain'     => 'w.org',
    17                     'path'       => '/',
     55        $keys  = array(
     56            'blog_id',
     57            'site_id',
     58            'domain',
     59            'path',
     60            'registered',
     61            'last_updated',
     62            'public',
     63            'archived',
     64            'mature',
     65            'spam',
     66            'deleted',
     67            'lang_id',
     68        );
     69        $sites = wp_get_sites();
     70
     71        $missing_keys = array_diff_key( array_flip( $keys ), $sites[0] );
     72
     73        $this->assertSame( array(), $missing_keys, 'Keys are missing from site arrays.' );
     74    }
     75
     76    /**
     77     * @expectedDeprecated wp_get_sites
     78     * @dataProvider data_wp_get_sites
     79     *
     80     * @param $expected
     81     * @param $args
     82     * @param $error
     83     */
     84    public function test_wp_get_sites( $expected, $args, $error ) {
     85        $this->assertCount( $expected, wp_get_sites( $args ), $error );
     86    }
     87
     88    /**
     89     * @return array
     90     */
     91    public function data_wp_get_sites() {
     92        return array(
     93            array( 3, array(), 'Default arguments should return all sites from the current network.' ),
     94            array( 0, array( 'network_id' => 999 ), 'No sites should match a query with an invalid network ID.' ),
     95            array( 5, array( 'network_id' => null ), 'A network ID of null should return all sites on all networks.' ),
     96            array( 2, array( 'network_id' => 2 ), 'Only sites on a specified network ID should be returned.' ),
     97            array( 5, array( 'network_id' => array( 1, 2 ) ), 'If multiple network IDs are specified, sites from both should be returned.' ),
     98            array(
     99                3,
     100                array(
     101                    'public'     => 1,
     102                    'network_id' => null,
     103                ),
     104                'Public sites on all networks.',
     105            ),
     106            array(
     107                2,
     108                array(
     109                    'public'     => 0,
     110                    'network_id' => null,
     111                ),
     112                'Non public sites on all networks.',
     113            ),
     114            array(
     115                2,
     116                array(
     117                    'public'     => 1,
     118                    'network_id' => 1,
     119                ),
     120                'Public sites on a single network.',
     121            ),
     122            array(
     123                1,
     124                array(
     125                    'public'     => 1,
    18126                    'network_id' => 2,
    19127                ),
    20                 'wp.org/'     => array(
    21                     'domain'     => 'wp.org',
    22                     'path'       => '/',
    23                     'network_id' => 2,
    24                     'public'     => 0,
     128                'Public sites on a second network.',
     129            ),
     130            array( 2, array( 'limit' => 2 ), 'Provide only a limit argument.' ),
     131            array(
     132                1,
     133                array(
     134                    'limit'  => 2,
     135                    'offset' => 2,
    25136                ),
    26                 'wp.org/foo/' => array(
    27                     'domain'     => 'wp.org',
    28                     'path'       => '/foo/',
    29                     'network_id' => 1,
    30                     'public'     => 0,
    31                 ),
    32                 'wp.org/oof/' => array(
    33                     'domain' => 'wp.org',
    34                     'path'   => '/oof/',
    35                 ),
    36             );
    37 
    38             foreach ( self::$site_ids as &$id ) {
    39                 $id = $factory->blog->create( $id );
    40             }
    41             unset( $id );
    42         }
    43 
    44         public static function wpTearDownAfterClass() {
    45             foreach ( self::$site_ids as $id ) {
    46                 wp_delete_site( $id );
    47             }
    48 
    49             wp_update_network_site_counts();
    50         }
    51 
    52         /**
    53          * @expectedDeprecated wp_get_sites
    54          */
    55         public function test_wp_get_sites_site_is_expected_array() {
    56 
    57             $keys  = array(
    58                 'blog_id',
    59                 'site_id',
    60                 'domain',
    61                 'path',
    62                 'registered',
    63                 'last_updated',
    64                 'public',
    65                 'archived',
    66                 'mature',
    67                 'spam',
    68                 'deleted',
    69                 'lang_id',
    70             );
    71             $sites = wp_get_sites();
    72 
    73             $missing_keys = array_diff_key( array_flip( $keys ), $sites[0] );
    74 
    75             $this->assertSame( array(), $missing_keys, 'Keys are missing from site arrays.' );
    76         }
    77 
    78         /**
    79          * @expectedDeprecated wp_get_sites
    80          * @dataProvider data_wp_get_sites
    81          *
    82          * @param $expected
    83          * @param $args
    84          * @param $error
    85          */
    86         public function test_wp_get_sites( $expected, $args, $error ) {
    87             $this->assertCount( $expected, wp_get_sites( $args ), $error );
    88         }
    89 
    90         /**
    91          * @return array
    92          */
    93         public function data_wp_get_sites() {
    94             return array(
    95                 array( 3, array(), 'Default arguments should return all sites from the current network.' ),
    96                 array( 0, array( 'network_id' => 999 ), 'No sites should match a query with an invalid network ID.' ),
    97                 array( 5, array( 'network_id' => null ), 'A network ID of null should return all sites on all networks.' ),
    98                 array( 2, array( 'network_id' => 2 ), 'Only sites on a specified network ID should be returned.' ),
    99                 array( 5, array( 'network_id' => array( 1, 2 ) ), 'If multiple network IDs are specified, sites from both should be returned.' ),
    100                 array(
    101                     3,
    102                     array(
    103                         'public'     => 1,
    104                         'network_id' => null,
    105                     ),
    106                     'Public sites on all networks.',
    107                 ),
    108                 array(
    109                     2,
    110                     array(
    111                         'public'     => 0,
    112                         'network_id' => null,
    113                     ),
    114                     'Non public sites on all networks.',
    115                 ),
    116                 array(
    117                     2,
    118                     array(
    119                         'public'     => 1,
    120                         'network_id' => 1,
    121                     ),
    122                     'Public sites on a single network.',
    123                 ),
    124                 array(
    125                     1,
    126                     array(
    127                         'public'     => 1,
    128                         'network_id' => 2,
    129                     ),
    130                     'Public sites on a second network.',
    131                 ),
    132                 array( 2, array( 'limit' => 2 ), 'Provide only a limit argument.' ),
    133                 array(
    134                     1,
    135                     array(
    136                         'limit'  => 2,
    137                         'offset' => 2,
    138                     ),
    139                     'Provide both limit and offset arguments.',
    140                 ),
    141                 array( 2, array( 'offset' => 1 ), 'Provide only an offset argument.' ),
    142                 array( 0, array( 'offset' => 20 ), 'Expect 0 sites when using an offset larger than the total number of sites.' ),
    143             );
    144         }
     137                'Provide both limit and offset arguments.',
     138            ),
     139            array( 2, array( 'offset' => 1 ), 'Provide only an offset argument.' ),
     140            array( 0, array( 'offset' => 20 ), 'Expect 0 sites when using an offset larger than the total number of sites.' ),
     141        );
    145142    }
    146 
    147 endif;
     143}
  • trunk/tests/phpunit/tests/multisite/wpInstallDefaults.php

    r54090 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Saving network settings without altering starter content ( first page, post, and comment ) shouldn't affect
     5 * the way it is added to new sites.
     6 *
     7 * @group ms-required
     8 * @group ms-site
     9 * @group multisite
     10 */
     11class Tests_Multisite_wpInstallDefaults extends WP_UnitTestCase {
    412    /**
    5      * Saving network settings without altering starter content ( first page, post, and comment ) shouldn't affect
    6      * the way it is added to new sites.
    7      *
    8      * @group ms-site
    9      * @group multisite
     13     * @ticket 40036
    1014     */
    11     class Tests_Multisite_wpInstallDefaults extends WP_UnitTestCase {
    12         /**
    13          * @ticket 40036
    14          */
    15         public function test_option_should_not_be_empty_by_default() {
    16             $blog_id = self::factory()->blog->create();
     15    public function test_option_should_not_be_empty_by_default() {
     16        $blog_id = self::factory()->blog->create();
    1717
    18             switch_to_blog( $blog_id );
     18        switch_to_blog( $blog_id );
    1919
    20             $first_page    = get_page_by_path( '/sample-page' );
    21             $first_comment = get_comments();
     20        $first_page    = get_page_by_path( '/sample-page' );
     21        $first_comment = get_comments();
    2222
    23             restore_current_blog();
     23        restore_current_blog();
    2424
    25             wp_delete_site( $blog_id );
     25        wp_delete_site( $blog_id );
    2626
    27             $this->assertNotEmpty( $first_page->post_content );
    28             $this->assertNotEmpty( $first_comment[0]->comment_content );
    29         }
    30 
    31         /**
    32          * @ticket 40036
    33          */
    34         public function test_empty_option_should_fall_back_to_default() {
    35             /*
    36              * Update first_page / first_comment options,
    37              * just like what happens when the network settings page is saved
    38              */
    39             update_site_option( 'first_page', '' );
    40             update_site_option( 'first_comment', '' );
    41 
    42             $blog_id = self::factory()->blog->create();
    43 
    44             switch_to_blog( $blog_id );
    45 
    46             $first_page    = get_page_by_path( '/sample-page' );
    47             $first_comment = get_comments();
    48 
    49             restore_current_blog();
    50 
    51             wp_delete_site( $blog_id );
    52 
    53             $this->assertNotEmpty( $first_page->post_content );
    54             $this->assertNotEmpty( $first_comment[0]->comment_content );
    55         }
    56 
    57         /**
    58          * @ticket 40036
    59          */
    60         public function test_non_default_option_values() {
    61             /*
    62              * Update first_page / first_comment options,
    63              * just like what happens when the network settings page is saved
    64              */
    65             update_site_option( 'first_page', 'Some page content' );
    66             update_site_option( 'first_comment', 'Some comment content' );
    67 
    68             $blog_id = self::factory()->blog->create();
    69 
    70             switch_to_blog( $blog_id );
    71 
    72             $first_page    = get_page_by_path( '/sample-page' );
    73             $first_comment = get_comments();
    74 
    75             restore_current_blog();
    76 
    77             wp_delete_site( $blog_id );
    78 
    79             $this->assertSame( 'Some page content', $first_page->post_content );
    80             $this->assertSame( 'Some comment content', $first_comment[0]->comment_content );
    81         }
     27        $this->assertNotEmpty( $first_page->post_content );
     28        $this->assertNotEmpty( $first_comment[0]->comment_content );
    8229    }
    8330
    84 endif;
     31    /**
     32     * @ticket 40036
     33     */
     34    public function test_empty_option_should_fall_back_to_default() {
     35        /*
     36         * Update first_page / first_comment options,
     37         * just like what happens when the network settings page is saved
     38         */
     39        update_site_option( 'first_page', '' );
     40        update_site_option( 'first_comment', '' );
     41
     42        $blog_id = self::factory()->blog->create();
     43
     44        switch_to_blog( $blog_id );
     45
     46        $first_page    = get_page_by_path( '/sample-page' );
     47        $first_comment = get_comments();
     48
     49        restore_current_blog();
     50
     51        wp_delete_site( $blog_id );
     52
     53        $this->assertNotEmpty( $first_page->post_content );
     54        $this->assertNotEmpty( $first_comment[0]->comment_content );
     55    }
     56
     57    /**
     58     * @ticket 40036
     59     */
     60    public function test_non_default_option_values() {
     61        /*
     62         * Update first_page / first_comment options,
     63         * just like what happens when the network settings page is saved
     64         */
     65        update_site_option( 'first_page', 'Some page content' );
     66        update_site_option( 'first_comment', 'Some comment content' );
     67
     68        $blog_id = self::factory()->blog->create();
     69
     70        switch_to_blog( $blog_id );
     71
     72        $first_page    = get_page_by_path( '/sample-page' );
     73        $first_comment = get_comments();
     74
     75        restore_current_blog();
     76
     77        wp_delete_site( $blog_id );
     78
     79        $this->assertSame( 'Some page content', $first_page->post_content );
     80        $this->assertSame( 'Some comment content', $first_comment[0]->comment_content );
     81    }
     82}
  • trunk/tests/phpunit/tests/multisite/wpMsSitesListTable.php

    r54215 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group admin
     5 * @group ms-required
     6 * @group network-admin
     7 */
     8class Tests_Multisite_wpMsSitesListTable extends WP_UnitTestCase {
     9
     10    protected static $site_ids;
    411
    512    /**
    6      * @group admin
    7      * @group network-admin
     13     * @var WP_MS_Sites_List_Table
    814     */
    9     class Tests_Multisite_wpMsSitesListTable extends WP_UnitTestCase {
    10         protected static $site_ids;
    11 
    12         /**
    13          * @var WP_MS_Sites_List_Table
    14          */
    15         public $table = false;
    16 
    17         public function set_up() {
    18             parent::set_up();
    19             $this->table = _get_list_table( 'WP_MS_Sites_List_Table', array( 'screen' => 'ms-sites' ) );
    20         }
    21 
    22         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    23             self::$site_ids = array(
    24                 'wordpress.org/'          => array(
    25                     'domain' => 'wordpress.org',
    26                     'path'   => '/',
    27                 ),
    28                 'wordpress.org/foo/'      => array(
    29                     'domain' => 'wordpress.org',
    30                     'path'   => '/foo/',
    31                 ),
    32                 'wordpress.org/foo/bar/'  => array(
    33                     'domain' => 'wordpress.org',
    34                     'path'   => '/foo/bar/',
    35                 ),
    36                 'wordpress.org/afoo/'     => array(
    37                     'domain' => 'wordpress.org',
    38                     'path'   => '/afoo/',
    39                 ),
    40                 'make.wordpress.org/'     => array(
    41                     'domain' => 'make.wordpress.org',
    42                     'path'   => '/',
    43                 ),
    44                 'make.wordpress.org/foo/' => array(
    45                     'domain' => 'make.wordpress.org',
    46                     'path'   => '/foo/',
    47                 ),
    48                 'www.w.org/'              => array(
    49                     'domain' => 'www.w.org',
    50                     'path'   => '/',
    51                 ),
    52                 'www.w.org/foo/'          => array(
    53                     'domain' => 'www.w.org',
    54                     'path'   => '/foo/',
    55                 ),
    56                 'www.w.org/foo/bar/'      => array(
    57                     'domain' => 'www.w.org',
    58                     'path'   => '/foo/bar/',
    59                 ),
    60                 'test.example.org/'       => array(
    61                     'domain' => 'test.example.org',
    62                     'path'   => '/',
    63                 ),
    64                 'test2.example.org/'      => array(
    65                     'domain' => 'test2.example.org',
    66                     'path'   => '/',
    67                 ),
    68                 'test3.example.org/zig/'  => array(
    69                     'domain' => 'test3.example.org',
    70                     'path'   => '/zig/',
    71                 ),
    72                 'atest.example.org/'      => array(
    73                     'domain' => 'atest.example.org',
    74                     'path'   => '/',
    75                 ),
    76             );
    77 
    78             foreach ( self::$site_ids as &$id ) {
    79                 $id = $factory->blog->create( $id );
    80             }
    81             unset( $id );
    82         }
    83 
    84         public static function wpTearDownAfterClass() {
    85             foreach ( self::$site_ids as $site_id ) {
    86                 wp_delete_site( $site_id );
    87             }
    88         }
    89 
    90         public function test_ms_sites_list_table_default_items() {
    91             $this->table->prepare_items();
    92 
    93             $items = wp_list_pluck( $this->table->items, 'blog_id' );
    94             $items = array_map( 'intval', $items );
    95 
    96             $this->assertSameSets( array( 1 ) + self::$site_ids, $items );
    97         }
    98 
    99         public function test_ms_sites_list_table_subdirectory_path_search_items() {
    100             if ( is_subdomain_install() ) {
    101                 $this->markTestSkipped( 'Path search is not available for subdomain configurations.' );
    102             }
    103 
    104             $_REQUEST['s'] = 'foo';
    105 
    106             $this->table->prepare_items();
    107 
    108             $items = wp_list_pluck( $this->table->items, 'blog_id' );
    109             $items = array_map( 'intval', $items );
    110 
    111             unset( $_REQUEST['s'] );
    112 
    113             $expected = array(
    114                 self::$site_ids['wordpress.org/foo/'],
    115                 self::$site_ids['wordpress.org/foo/bar/'],
    116                 self::$site_ids['wordpress.org/afoo/'],
    117                 self::$site_ids['make.wordpress.org/foo/'],
    118                 self::$site_ids['www.w.org/foo/'],
    119                 self::$site_ids['www.w.org/foo/bar/'],
    120             );
    121 
    122             $this->assertSameSets( $expected, $items );
    123         }
    124 
    125         public function test_ms_sites_list_table_subdirectory_multiple_path_search_items() {
    126             if ( is_subdomain_install() ) {
    127                 $this->markTestSkipped( 'Path search is not available for subdomain configurations.' );
    128             }
    129 
    130             $_REQUEST['s'] = 'foo/bar';
    131 
    132             $this->table->prepare_items();
    133 
    134             $items = wp_list_pluck( $this->table->items, 'blog_id' );
    135             $items = array_map( 'intval', $items );
    136 
    137             unset( $_REQUEST['s'] );
    138 
    139             $expected = array(
    140                 self::$site_ids['wordpress.org/foo/bar/'],
    141                 self::$site_ids['www.w.org/foo/bar/'],
    142             );
    143 
    144             $this->assertSameSets( $expected, $items );
    145         }
    146 
    147         public function test_ms_sites_list_table_invalid_path_search_items() {
    148             $_REQUEST['s'] = 'foobar';
    149 
    150             $this->table->prepare_items();
    151 
    152             $items = wp_list_pluck( $this->table->items, 'blog_id' );
    153             $items = array_map( 'intval', $items );
    154 
    155             unset( $_REQUEST['s'] );
    156 
    157             $this->assertEmpty( $items );
    158         }
    159 
    160         public function test_ms_sites_list_table_subdomain_domain_search_items() {
    161             if ( ! is_subdomain_install() ) {
    162                 $this->markTestSkipped( 'Domain search is not available for subdirectory configurations.' );
    163             }
    164 
    165             $_REQUEST['s'] = 'test';
    166 
    167             $this->table->prepare_items();
    168 
    169             $items = wp_list_pluck( $this->table->items, 'blog_id' );
    170             $items = array_map( 'intval', $items );
    171 
    172             unset( $_REQUEST['s'] );
    173 
    174             $expected = array(
    175                 self::$site_ids['test.example.org/'],
    176                 self::$site_ids['test2.example.org/'],
    177                 self::$site_ids['test3.example.org/zig/'],
    178                 self::$site_ids['atest.example.org/'],
    179             );
    180 
    181             $this->assertSameSets( $expected, $items );
    182         }
    183 
    184         public function test_ms_sites_list_table_subdomain_domain_search_items_with_trailing_wildcard() {
    185             if ( ! is_subdomain_install() ) {
    186                 $this->markTestSkipped( 'Domain search is not available for subdirectory configurations.' );
    187             }
    188 
    189             $_REQUEST['s'] = 'test*';
    190 
    191             $this->table->prepare_items();
    192 
    193             $items = wp_list_pluck( $this->table->items, 'blog_id' );
    194             $items = array_map( 'intval', $items );
    195 
    196             unset( $_REQUEST['s'] );
    197 
    198             $expected = array(
    199                 self::$site_ids['test.example.org/'],
    200                 self::$site_ids['test2.example.org/'],
    201                 self::$site_ids['test3.example.org/zig/'],
    202                 self::$site_ids['atest.example.org/'],
    203             );
    204 
    205             $this->assertSameSets( $expected, $items );
    206         }
    207 
    208         public function test_ms_sites_list_table_subdirectory_path_search_items_with_trailing_wildcard() {
    209             if ( is_subdomain_install() ) {
    210                 $this->markTestSkipped( 'Path search is not available for subdomain configurations.' );
    211             }
    212 
    213             $_REQUEST['s'] = 'fo*';
    214 
    215             $this->table->prepare_items();
    216 
    217             $items = wp_list_pluck( $this->table->items, 'blog_id' );
    218             $items = array_map( 'intval', $items );
    219 
    220             unset( $_REQUEST['s'] );
    221 
    222             $expected = array(
    223                 self::$site_ids['wordpress.org/foo/'],
    224                 self::$site_ids['wordpress.org/foo/bar/'],
    225                 self::$site_ids['wordpress.org/afoo/'],
    226                 self::$site_ids['make.wordpress.org/foo/'],
    227                 self::$site_ids['www.w.org/foo/'],
    228                 self::$site_ids['www.w.org/foo/bar/'],
    229             );
    230 
    231             $this->assertSameSets( $expected, $items );
    232         }
    233 
    234         /**
    235          * @ticket 42066
    236          */
    237         public function test_get_views_should_return_views_by_default() {
    238             $expected = array(
    239                 'all'    => '<a href="sites.php" class="current" aria-current="page">All <span class="count">(14)</span></a>',
    240                 'public' => '<a href="sites.php?status=public">Public <span class="count">(14)</span></a>',
    241             );
    242 
    243             $this->assertSame( $expected, $this->table->get_views() );
    244         }
    245     }
    246 endif;
     15    public $table = false;
     16
     17    public function set_up() {
     18        parent::set_up();
     19        $this->table = _get_list_table( 'WP_MS_Sites_List_Table', array( 'screen' => 'ms-sites' ) );
     20    }
     21
     22    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     23        self::$site_ids = array(
     24            'wordpress.org/'          => array(
     25                'domain' => 'wordpress.org',
     26                'path'   => '/',
     27            ),
     28            'wordpress.org/foo/'      => array(
     29                'domain' => 'wordpress.org',
     30                'path'   => '/foo/',
     31            ),
     32            'wordpress.org/foo/bar/'  => array(
     33                'domain' => 'wordpress.org',
     34                'path'   => '/foo/bar/',
     35            ),
     36            'wordpress.org/afoo/'     => array(
     37                'domain' => 'wordpress.org',
     38                'path'   => '/afoo/',
     39            ),
     40            'make.wordpress.org/'     => array(
     41                'domain' => 'make.wordpress.org',
     42                'path'   => '/',
     43            ),
     44            'make.wordpress.org/foo/' => array(
     45                'domain' => 'make.wordpress.org',
     46                'path'   => '/foo/',
     47            ),
     48            'www.w.org/'              => array(
     49                'domain' => 'www.w.org',
     50                'path'   => '/',
     51            ),
     52            'www.w.org/foo/'          => array(
     53                'domain' => 'www.w.org',
     54                'path'   => '/foo/',
     55            ),
     56            'www.w.org/foo/bar/'      => array(
     57                'domain' => 'www.w.org',
     58                'path'   => '/foo/bar/',
     59            ),
     60            'test.example.org/'       => array(
     61                'domain' => 'test.example.org',
     62                'path'   => '/',
     63            ),
     64            'test2.example.org/'      => array(
     65                'domain' => 'test2.example.org',
     66                'path'   => '/',
     67            ),
     68            'test3.example.org/zig/'  => array(
     69                'domain' => 'test3.example.org',
     70                'path'   => '/zig/',
     71            ),
     72            'atest.example.org/'      => array(
     73                'domain' => 'atest.example.org',
     74                'path'   => '/',
     75            ),
     76        );
     77
     78        foreach ( self::$site_ids as &$id ) {
     79            $id = $factory->blog->create( $id );
     80        }
     81        unset( $id );
     82    }
     83
     84    public static function wpTearDownAfterClass() {
     85        foreach ( self::$site_ids as $site_id ) {
     86            wp_delete_site( $site_id );
     87        }
     88    }
     89
     90    public function test_ms_sites_list_table_default_items() {
     91        $this->table->prepare_items();
     92
     93        $items = wp_list_pluck( $this->table->items, 'blog_id' );
     94        $items = array_map( 'intval', $items );
     95
     96        $this->assertSameSets( array( 1 ) + self::$site_ids, $items );
     97    }
     98
     99    public function test_ms_sites_list_table_subdirectory_path_search_items() {
     100        if ( is_subdomain_install() ) {
     101            $this->markTestSkipped( 'Path search is not available for subdomain configurations.' );
     102        }
     103
     104        $_REQUEST['s'] = 'foo';
     105
     106        $this->table->prepare_items();
     107
     108        $items = wp_list_pluck( $this->table->items, 'blog_id' );
     109        $items = array_map( 'intval', $items );
     110
     111        unset( $_REQUEST['s'] );
     112
     113        $expected = array(
     114            self::$site_ids['wordpress.org/foo/'],
     115            self::$site_ids['wordpress.org/foo/bar/'],
     116            self::$site_ids['wordpress.org/afoo/'],
     117            self::$site_ids['make.wordpress.org/foo/'],
     118            self::$site_ids['www.w.org/foo/'],
     119            self::$site_ids['www.w.org/foo/bar/'],
     120        );
     121
     122        $this->assertSameSets( $expected, $items );
     123    }
     124
     125    public function test_ms_sites_list_table_subdirectory_multiple_path_search_items() {
     126        if ( is_subdomain_install() ) {
     127            $this->markTestSkipped( 'Path search is not available for subdomain configurations.' );
     128        }
     129
     130        $_REQUEST['s'] = 'foo/bar';
     131
     132        $this->table->prepare_items();
     133
     134        $items = wp_list_pluck( $this->table->items, 'blog_id' );
     135        $items = array_map( 'intval', $items );
     136
     137        unset( $_REQUEST['s'] );
     138
     139        $expected = array(
     140            self::$site_ids['wordpress.org/foo/bar/'],
     141            self::$site_ids['www.w.org/foo/bar/'],
     142        );
     143
     144        $this->assertSameSets( $expected, $items );
     145    }
     146
     147    public function test_ms_sites_list_table_invalid_path_search_items() {
     148        $_REQUEST['s'] = 'foobar';
     149
     150        $this->table->prepare_items();
     151
     152        $items = wp_list_pluck( $this->table->items, 'blog_id' );
     153        $items = array_map( 'intval', $items );
     154
     155        unset( $_REQUEST['s'] );
     156
     157        $this->assertEmpty( $items );
     158    }
     159
     160    public function test_ms_sites_list_table_subdomain_domain_search_items() {
     161        if ( ! is_subdomain_install() ) {
     162            $this->markTestSkipped( 'Domain search is not available for subdirectory configurations.' );
     163        }
     164
     165        $_REQUEST['s'] = 'test';
     166
     167        $this->table->prepare_items();
     168
     169        $items = wp_list_pluck( $this->table->items, 'blog_id' );
     170        $items = array_map( 'intval', $items );
     171
     172        unset( $_REQUEST['s'] );
     173
     174        $expected = array(
     175            self::$site_ids['test.example.org/'],
     176            self::$site_ids['test2.example.org/'],
     177            self::$site_ids['test3.example.org/zig/'],
     178            self::$site_ids['atest.example.org/'],
     179        );
     180
     181        $this->assertSameSets( $expected, $items );
     182    }
     183
     184    public function test_ms_sites_list_table_subdomain_domain_search_items_with_trailing_wildcard() {
     185        if ( ! is_subdomain_install() ) {
     186            $this->markTestSkipped( 'Domain search is not available for subdirectory configurations.' );
     187        }
     188
     189        $_REQUEST['s'] = 'test*';
     190
     191        $this->table->prepare_items();
     192
     193        $items = wp_list_pluck( $this->table->items, 'blog_id' );
     194        $items = array_map( 'intval', $items );
     195
     196        unset( $_REQUEST['s'] );
     197
     198        $expected = array(
     199            self::$site_ids['test.example.org/'],
     200            self::$site_ids['test2.example.org/'],
     201            self::$site_ids['test3.example.org/zig/'],
     202            self::$site_ids['atest.example.org/'],
     203        );
     204
     205        $this->assertSameSets( $expected, $items );
     206    }
     207
     208    public function test_ms_sites_list_table_subdirectory_path_search_items_with_trailing_wildcard() {
     209        if ( is_subdomain_install() ) {
     210            $this->markTestSkipped( 'Path search is not available for subdomain configurations.' );
     211        }
     212
     213        $_REQUEST['s'] = 'fo*';
     214
     215        $this->table->prepare_items();
     216
     217        $items = wp_list_pluck( $this->table->items, 'blog_id' );
     218        $items = array_map( 'intval', $items );
     219
     220        unset( $_REQUEST['s'] );
     221
     222        $expected = array(
     223            self::$site_ids['wordpress.org/foo/'],
     224            self::$site_ids['wordpress.org/foo/bar/'],
     225            self::$site_ids['wordpress.org/afoo/'],
     226            self::$site_ids['make.wordpress.org/foo/'],
     227            self::$site_ids['www.w.org/foo/'],
     228            self::$site_ids['www.w.org/foo/bar/'],
     229        );
     230
     231        $this->assertSameSets( $expected, $items );
     232    }
     233
     234    /**
     235     * @ticket 42066
     236     */
     237    public function test_get_views_should_return_views_by_default() {
     238        $expected = array(
     239            'all'    => '<a href="sites.php" class="current" aria-current="page">All <span class="count">(14)</span></a>',
     240            'public' => '<a href="sites.php?status=public">Public <span class="count">(14)</span></a>',
     241        );
     242
     243        $this->assertSame( $expected, $this->table->get_views() );
     244    }
     245}
  • trunk/tests/phpunit/tests/multisite/wpMsThemesListTable.php

    r54215 r60148  
    22
    33/**
     4 * @group admin
    45 * @group ms-required
    5  * @group admin
    66 * @group network-admin
    77 *
  • trunk/tests/phpunit/tests/multisite/wpMsUsersListTable.php

    r58097 r60148  
    33/**
    44 * @group admin
     5 * @group ms-required
    56 * @group network-admin
    6  * @group ms-required
    77 *
    88 * @covers WP_MS_Users_List_Table
  • trunk/tests/phpunit/tests/multisite/wpNetworkQuery.php

    r57750 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    5     /**
    6      * Test network query functionality in multisite.
    7      *
    8      * @group ms-network
    9      * @group ms-network-query
    10      * @group multisite
    11      */
    12     class Tests_Multisite_wpNetworkQuery extends WP_UnitTestCase {
    13         protected static $network_ids;
    14 
    15         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    16             self::$network_ids = array(
    17                 'wordpress.org/'      => array(
    18                     'domain' => 'wordpress.org',
    19                     'path'   => '/',
    20                 ),
    21                 'make.wordpress.org/' => array(
    22                     'domain' => 'make.wordpress.org',
    23                     'path'   => '/',
    24                 ),
    25                 'www.wordpress.net/'  => array(
    26                     'domain' => 'www.wordpress.net',
    27                     'path'   => '/',
    28                 ),
    29                 'www.w.org/foo/'      => array(
    30                     'domain' => 'www.w.org',
    31                     'path'   => '/foo/',
    32                 ),
    33             );
    34 
    35             foreach ( self::$network_ids as &$id ) {
    36                 $id = $factory->network->create( $id );
    37             }
    38             unset( $id );
     3/**
     4 * Test network query functionality in multisite.
     5 *
     6 * @group ms-network
     7 * @group ms-network-query
     8 * @group ms-required
     9 * @group multisite
     10 */
     11class Tests_Multisite_wpNetworkQuery extends WP_UnitTestCase {
     12    protected static $network_ids;
     13
     14    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     15        self::$network_ids = array(
     16            'wordpress.org/'      => array(
     17                'domain' => 'wordpress.org',
     18                'path'   => '/',
     19            ),
     20            'make.wordpress.org/' => array(
     21                'domain' => 'make.wordpress.org',
     22                'path'   => '/',
     23            ),
     24            'www.wordpress.net/'  => array(
     25                'domain' => 'www.wordpress.net',
     26                'path'   => '/',
     27            ),
     28            'www.w.org/foo/'      => array(
     29                'domain' => 'www.w.org',
     30                'path'   => '/foo/',
     31            ),
     32        );
     33
     34        foreach ( self::$network_ids as &$id ) {
     35            $id = $factory->network->create( $id );
    3936        }
    40 
    41         public static function wpTearDownAfterClass() {
    42             global $wpdb;
    43 
    44             foreach ( self::$network_ids as $id ) {
    45                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    46                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    47             }
     37        unset( $id );
     38    }
     39
     40    public static function wpTearDownAfterClass() {
     41        global $wpdb;
     42
     43        foreach ( self::$network_ids as $id ) {
     44            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     45            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    4846        }
    49 
    50         public function test_wp_network_query_by_number() {
    51             $q     = new WP_Network_Query();
    52             $found = $q->query(
    53                 array(
    54                     'fields' => 'ids',
    55                     'number' => 3,
    56                 )
    57             );
    58 
    59             $this->assertCount( 3, $found );
    60         }
    61 
    62         public function test_wp_network_query_by_network__in_with_order() {
    63             $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['make.wordpress.org/'] );
    64 
    65             $q     = new WP_Network_Query();
    66             $found = $q->query(
    67                 array(
    68                     'fields'      => 'ids',
    69                     'network__in' => $expected,
    70                     'order'       => 'ASC',
    71                 )
    72             );
    73 
    74             $this->assertSame( $expected, $found );
    75 
    76             $found = $q->query(
    77                 array(
    78                     'fields'      => 'ids',
    79                     'network__in' => $expected,
    80                     'order'       => 'DESC',
    81                 )
    82             );
    83 
    84             $this->assertSame( array_reverse( $expected ), $found );
    85         }
    86 
    87         public function test_wp_network_query_by_network__in_with_single_id() {
    88             $expected = array( self::$network_ids['wordpress.org/'] );
    89 
    90             $q     = new WP_Network_Query();
    91             $found = $q->query(
    92                 array(
    93                     'fields'      => 'ids',
    94                     'network__in' => $expected,
    95                 )
    96             );
    97 
    98             $this->assertSameSets( $expected, $found );
    99         }
    100 
    101         public function test_wp_network_query_by_network__in_with_multiple_ids() {
    102             $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['www.wordpress.net/'] );
    103 
    104             $q     = new WP_Network_Query();
    105             $found = $q->query(
    106                 array(
    107                     'fields'      => 'ids',
    108                     'network__in' => $expected,
    109                 )
    110             );
    111 
    112             $this->assertSameSets( $expected, $found );
    113         }
    114 
    115         public function test_wp_network_query_by_network__in_and_count_with_multiple_ids() {
    116             $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['make.wordpress.org/'] );
    117 
    118             $q     = new WP_Network_Query();
    119             $found = $q->query(
    120                 array(
    121                     'fields'      => 'ids',
    122                     'count'       => true,
    123                     'network__in' => $expected,
    124                 )
    125             );
    126 
    127             $this->assertSame( 2, $found );
    128         }
    129 
    130         public function test_wp_network_query_by_network__not_in_with_single_id() {
    131             $excluded = array( self::$network_ids['wordpress.org/'] );
    132             $expected = array_diff( self::$network_ids, $excluded );
    133 
    134             // Exclude main network since we don't have control over it here.
    135             $excluded[] = 1;
    136 
    137             $q     = new WP_Network_Query();
    138             $found = $q->query(
    139                 array(
    140                     'fields'          => 'ids',
    141                     'network__not_in' => $excluded,
    142                 )
    143             );
    144 
    145             $this->assertSameSets( $expected, $found );
    146         }
    147 
    148         public function test_wp_network_query_by_network__not_in_with_multiple_ids() {
    149             $excluded = array( self::$network_ids['wordpress.org/'], self::$network_ids['www.w.org/foo/'] );
    150             $expected = array_diff( self::$network_ids, $excluded );
    151 
    152             // Exclude main network since we don't have control over it here.
    153             $excluded[] = 1;
    154 
    155             $q     = new WP_Network_Query();
    156             $found = $q->query(
    157                 array(
    158                     'fields'          => 'ids',
    159                     'network__not_in' => $excluded,
    160                 )
    161             );
    162 
    163             $this->assertSameSets( $expected, $found );
    164         }
    165 
    166         public function test_wp_network_query_by_domain() {
    167             $q     = new WP_Network_Query();
    168             $found = $q->query(
    169                 array(
    170                     'fields' => 'ids',
    171                     'domain' => 'www.w.org',
    172                 )
    173             );
    174 
    175             $expected = array(
    176                 self::$network_ids['www.w.org/foo/'],
    177             );
    178 
    179             $this->assertSameSets( $expected, $found );
    180         }
    181 
    182         public function test_wp_network_query_by_domain__in_with_single_domain() {
    183             $q     = new WP_Network_Query();
    184             $found = $q->query(
    185                 array(
    186                     'fields'     => 'ids',
    187                     'domain__in' => array( 'make.wordpress.org' ),
    188                 )
    189             );
    190 
    191             $expected = array(
    192                 self::$network_ids['make.wordpress.org/'],
    193             );
    194 
    195             $this->assertSameSets( $expected, $found );
    196         }
    197 
    198         public function test_wp_network_query_by_domain__in_with_multiple_domains() {
    199             $q     = new WP_Network_Query();
    200             $found = $q->query(
    201                 array(
    202                     'fields'     => 'ids',
    203                     'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
    204                 )
    205             );
    206 
    207             $expected = array(
    208                 self::$network_ids['wordpress.org/'],
    209                 self::$network_ids['make.wordpress.org/'],
    210             );
    211 
    212             $this->assertSameSets( $expected, $found );
    213         }
    214 
    215         public function test_wp_network_query_by_domain__in_with_multiple_domains_and_number() {
    216             $q     = new WP_Network_Query();
    217             $found = $q->query(
    218                 array(
    219                     'fields'     => 'ids',
    220                     'number'     => 1,
    221                     'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
    222                 )
    223             );
    224 
    225             $expected = array(
    226                 self::$network_ids['wordpress.org/'],
    227             );
    228 
    229             $this->assertSameSets( $expected, $found );
    230         }
    231 
    232         public function test_wp_network_query_by_domain__in_with_multiple_domains_and_number_and_offset() {
    233             $q     = new WP_Network_Query();
    234             $found = $q->query(
    235                 array(
    236                     'fields'     => 'ids',
    237                     'number'     => 1,
    238                     'offset'     => 1,
    239                     'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
    240                 )
    241             );
    242 
    243             $expected = array(
    244                 self::$network_ids['make.wordpress.org/'],
    245             );
    246 
    247             $this->assertSameSets( $expected, $found );
    248         }
    249 
    250         public function test_wp_network_query_by_domain__not_in_with_single_domain() {
    251             $q     = new WP_Network_Query();
    252             $found = $q->query(
    253                 array(
    254                     'fields'         => 'ids',
    255                     'domain__not_in' => array( 'www.w.org' ),
    256                 )
    257             );
    258 
    259             $expected = array(
    260                 get_current_site()->id, // Account for the initial network added by the test suite.
    261                 self::$network_ids['wordpress.org/'],
    262                 self::$network_ids['make.wordpress.org/'],
    263                 self::$network_ids['www.wordpress.net/'],
    264             );
    265 
    266             $this->assertSameSets( $expected, $found );
    267         }
    268 
    269         public function test_wp_network_query_by_domain__not_in_with_multiple_domains() {
    270             $q     = new WP_Network_Query();
    271             $found = $q->query(
    272                 array(
    273                     'fields'         => 'ids',
    274                     'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
    275                 )
    276             );
    277 
    278             $expected = array(
    279                 get_current_site()->id, // Account for the initial network added by the test suite.
    280                 self::$network_ids['make.wordpress.org/'],
    281                 self::$network_ids['www.wordpress.net/'],
    282             );
    283 
    284             $this->assertSameSets( $expected, $found );
    285         }
    286 
    287         public function test_wp_network_query_by_domain__not_in_with_multiple_domains_and_number() {
    288             $q     = new WP_Network_Query();
    289             $found = $q->query(
    290                 array(
    291                     'fields'         => 'ids',
    292                     'number'         => 2,
    293                     'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
    294                 )
    295             );
    296 
    297             $expected = array(
    298                 get_current_site()->id, // Account for the initial network added by the test suite.
    299                 self::$network_ids['make.wordpress.org/'],
    300             );
    301 
    302             $this->assertSameSets( $expected, $found );
    303         }
    304 
    305         public function test_wp_network_query_by_domain__not_in_with_multiple_domains_and_number_and_offset() {
    306             $q     = new WP_Network_Query();
    307             $found = $q->query(
    308                 array(
    309                     'fields'         => 'ids',
    310                     'number'         => 2,
    311                     'offset'         => 1,
    312                     'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
    313                 )
    314             );
    315 
    316             $expected = array(
    317                 self::$network_ids['make.wordpress.org/'],
    318                 self::$network_ids['www.wordpress.net/'],
    319             );
    320 
    321             $this->assertSameSets( $expected, $found );
    322         }
    323 
    324         public function test_wp_network_query_by_path_with_expected_results() {
    325             $q     = new WP_Network_Query();
    326             $found = $q->query(
    327                 array(
    328                     'fields'          => 'ids',
    329                     'path'            => '/',
    330                     'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
    331                 )
    332             );
    333 
    334             $expected = array(
    335                 self::$network_ids['wordpress.org/'],
    336                 self::$network_ids['make.wordpress.org/'],
    337                 self::$network_ids['www.wordpress.net/'],
    338             );
    339 
    340             $this->assertSameSets( $expected, $found );
    341         }
    342 
    343         public function test_wp_network_query_by_path_and_number_and_offset_with_expected_results() {
    344             $q     = new WP_Network_Query();
    345             $found = $q->query(
    346                 array(
    347                     'fields'          => 'ids',
    348                     'number'          => 1,
    349                     'offset'          => 2,
    350                     'path'            => '/',
    351                     'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
    352                 )
    353             );
    354 
    355             $expected = array(
    356                 self::$network_ids['www.wordpress.net/'],
    357             );
    358 
    359             $this->assertSameSets( $expected, $found );
    360         }
    361 
    362         public function test_wp_network_query_by_path_with_no_expected_results() {
    363             $q     = new WP_Network_Query();
    364             $found = $q->query(
    365                 array(
    366                     'fields' => 'ids',
    367                     'path'   => '/bar/',
    368                 )
    369             );
    370 
    371             $this->assertEmpty( $found );
    372         }
    373 
    374         public function test_wp_network_query_by_search_with_text_in_domain() {
    375             $q     = new WP_Network_Query();
    376             $found = $q->query(
    377                 array(
    378                     'fields' => 'ids',
    379                     'search' => 'ww.word',
    380                 )
    381             );
    382 
    383             $expected = array(
    384                 self::$network_ids['www.wordpress.net/'],
    385             );
    386 
    387             $this->assertSameSets( $expected, $found );
    388         }
    389 
    390         public function test_wp_network_query_by_search_with_text_in_path() {
    391             $q     = new WP_Network_Query();
    392             $found = $q->query(
    393                 array(
    394                     'fields' => 'ids',
    395                     'search' => 'foo',
    396                 )
    397             );
    398 
    399             $expected = array(
    400                 self::$network_ids['www.w.org/foo/'],
    401             );
    402 
    403             $this->assertSameSets( $expected, $found );
    404         }
    405 
    406         public function test_wp_network_query_by_path_order_by_domain_desc() {
    407             $q     = new WP_Network_Query();
    408             $found = $q->query(
    409                 array(
    410                     'fields'          => 'ids',
    411                     'path'            => '/',
    412                     'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
    413                     'order'           => 'DESC',
    414                     'orderby'         => 'domain',
    415                 )
    416             );
    417 
    418             $expected = array(
    419                 self::$network_ids['www.wordpress.net/'],
    420                 self::$network_ids['wordpress.org/'],
    421                 self::$network_ids['make.wordpress.org/'],
    422             );
    423 
    424             $this->assertSame( $expected, $found );
    425         }
    426 
    427         /**
    428          * @ticket 41347
    429          */
    430         public function test_wp_network_query_cache_with_different_fields_no_count() {
    431             $q                 = new WP_Network_Query();
    432             $query_1           = $q->query(
    433                 array(
    434                     'fields' => 'all',
    435                     'number' => 3,
    436                     'order'  => 'ASC',
    437                 )
    438             );
    439             $number_of_queries = get_num_queries();
    440 
    441             $query_2 = $q->query(
    442                 array(
    443                     'fields' => 'ids',
    444                     'number' => 3,
    445                     'order'  => 'ASC',
    446                 )
    447             );
    448 
    449             $this->assertSame( $number_of_queries, get_num_queries() );
    450         }
    451 
    452         /**
    453          * @ticket 41347
    454          */
    455         public function test_wp_network_query_cache_with_different_fields_active_count() {
    456             $q = new WP_Network_Query();
    457 
    458             $query_1           = $q->query(
    459                 array(
    460                     'fields' => 'all',
    461                     'number' => 3,
    462                     'order'  => 'ASC',
    463                     'count'  => true,
    464                 )
    465             );
    466             $number_of_queries = get_num_queries();
    467 
    468             $query_2 = $q->query(
    469                 array(
    470                     'fields' => 'ids',
    471                     'number' => 3,
    472                     'order'  => 'ASC',
    473                     'count'  => true,
    474                 )
    475             );
    476             $this->assertSame( $number_of_queries, get_num_queries() );
    477         }
    478 
    479         /**
    480          * @ticket 41347
    481          */
    482         public function test_wp_network_query_cache_with_same_fields_different_count() {
    483             $q = new WP_Network_Query();
    484 
    485             $query_1 = $q->query(
    486                 array(
    487                     'fields' => 'ids',
    488                     'number' => 3,
    489                     'order'  => 'ASC',
    490                 )
    491             );
    492 
    493             $number_of_queries = get_num_queries();
    494 
    495             $query_2 = $q->query(
    496                 array(
    497                     'fields' => 'ids',
    498                     'number' => 3,
    499                     'order'  => 'ASC',
    500                     'count'  => true,
    501                 )
    502             );
    503             $this->assertSame( $number_of_queries + 1, get_num_queries() );
    504         }
    505 
    506         /**
    507          * @ticket 55461
    508          */
    509         public function test_wp_network_query_cache_with_same_fields_same_cache_field() {
    510             $q                 = new WP_Network_Query();
    511             $query_1           = $q->query(
    512                 array(
    513                     'fields'               => 'all',
    514                     'number'               => 3,
    515                     'order'                => 'ASC',
    516                     'update_network_cache' => true,
    517                 )
    518             );
    519             $number_of_queries = get_num_queries();
    520 
    521             $query_2 = $q->query(
    522                 array(
    523                     'fields'               => 'all',
    524                     'number'               => 3,
    525                     'order'                => 'ASC',
    526                     'update_network_cache' => true,
    527                 )
    528             );
    529 
    530             $this->assertSame( $number_of_queries, get_num_queries() );
    531         }
    532 
    533         /**
    534          * @ticket 55461
    535          */
    536         public function test_wp_network_query_cache_with_same_fields_different_cache_field() {
    537             $q                 = new WP_Network_Query();
    538             $query_1           = $q->query(
    539                 array(
    540                     'fields'               => 'all',
    541                     'number'               => 3,
    542                     'order'                => 'ASC',
    543                     'update_network_cache' => true,
    544                 )
    545             );
    546             $number_of_queries = get_num_queries();
    547 
    548             $query_2 = $q->query(
    549                 array(
    550                     'fields'               => 'all',
    551                     'number'               => 3,
    552                     'order'                => 'ASC',
    553                     'update_network_cache' => false,
    554                 )
    555             );
    556 
    557             $this->assertSame( $number_of_queries, get_num_queries() );
    558         }
    559 
    560         /**
    561          * @ticket 45749
    562          * @ticket 47599
    563          */
    564         public function test_networks_pre_query_filter_should_bypass_database_query() {
    565             add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
    566 
    567             $num_queries = get_num_queries();
    568 
    569             $q       = new WP_Network_Query();
    570             $results = $q->query( array() );
    571 
    572             remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
    573 
    574             // Make sure no queries were executed.
    575             $this->assertSame( $num_queries, get_num_queries() );
    576 
    577             // We manually inserted a non-existing site and overrode the results with it.
    578             $this->assertSame( array( 555 ), $results );
    579 
    580             // Make sure manually setting found_networks doesn't get overwritten.
    581             $this->assertSame( 1, $q->found_networks );
    582         }
    583 
    584         public static function filter_networks_pre_query( $networks, $query ) {
    585             $query->found_networks = 1;
    586 
    587             return array( 555 );
    588         }
    589 
    590         /**
    591          * @ticket 51333
    592          */
    593         public function test_networks_pre_query_filter_should_set_networks_property() {
    594             add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10, 2 );
    595 
    596             $q       = new WP_Network_Query();
    597             $results = $q->query( array() );
    598 
    599             remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10 );
    600 
    601             // Make sure the networks property is the same as the results.
    602             $this->assertSame( $results, $q->networks );
    603 
    604             // Make sure the network domain is `wordpress.org`.
    605             $this->assertSame( 'wordpress.org', $q->networks[0]->domain );
    606         }
    607 
    608         public static function filter_networks_pre_query_and_set_networks( $networks, $query ) {
    609             return array( get_network( self::$network_ids['wordpress.org/'] ) );
    610         }
    611 
    612         /**
    613          * @ticket 56841
    614          */
    615         public function test_wp_network_query_does_not_have_leading_whitespace() {
    616             $q = new WP_Network_Query();
    617             $q->query(
    618                 array(
    619                     'fields'               => 'all',
    620                     'number'               => 3,
    621                     'order'                => 'ASC',
    622                     'update_network_cache' => true,
    623                 )
    624             );
    625 
    626             $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
    627         }
    628     }
    629 
    630 endif;
     47    }
     48
     49    public function test_wp_network_query_by_number() {
     50        $q     = new WP_Network_Query();
     51        $found = $q->query(
     52            array(
     53                'fields' => 'ids',
     54                'number' => 3,
     55            )
     56        );
     57
     58        $this->assertCount( 3, $found );
     59    }
     60
     61    public function test_wp_network_query_by_network__in_with_order() {
     62        $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['make.wordpress.org/'] );
     63
     64        $q     = new WP_Network_Query();
     65        $found = $q->query(
     66            array(
     67                'fields'      => 'ids',
     68                'network__in' => $expected,
     69                'order'       => 'ASC',
     70            )
     71        );
     72
     73        $this->assertSame( $expected, $found );
     74
     75        $found = $q->query(
     76            array(
     77                'fields'      => 'ids',
     78                'network__in' => $expected,
     79                'order'       => 'DESC',
     80            )
     81        );
     82
     83        $this->assertSame( array_reverse( $expected ), $found );
     84    }
     85
     86    public function test_wp_network_query_by_network__in_with_single_id() {
     87        $expected = array( self::$network_ids['wordpress.org/'] );
     88
     89        $q     = new WP_Network_Query();
     90        $found = $q->query(
     91            array(
     92                'fields'      => 'ids',
     93                'network__in' => $expected,
     94            )
     95        );
     96
     97        $this->assertSameSets( $expected, $found );
     98    }
     99
     100    public function test_wp_network_query_by_network__in_with_multiple_ids() {
     101        $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['www.wordpress.net/'] );
     102
     103        $q     = new WP_Network_Query();
     104        $found = $q->query(
     105            array(
     106                'fields'      => 'ids',
     107                'network__in' => $expected,
     108            )
     109        );
     110
     111        $this->assertSameSets( $expected, $found );
     112    }
     113
     114    public function test_wp_network_query_by_network__in_and_count_with_multiple_ids() {
     115        $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['make.wordpress.org/'] );
     116
     117        $q     = new WP_Network_Query();
     118        $found = $q->query(
     119            array(
     120                'fields'      => 'ids',
     121                'count'       => true,
     122                'network__in' => $expected,
     123            )
     124        );
     125
     126        $this->assertSame( 2, $found );
     127    }
     128
     129    public function test_wp_network_query_by_network__not_in_with_single_id() {
     130        $excluded = array( self::$network_ids['wordpress.org/'] );
     131        $expected = array_diff( self::$network_ids, $excluded );
     132
     133        // Exclude main network since we don't have control over it here.
     134        $excluded[] = 1;
     135
     136        $q     = new WP_Network_Query();
     137        $found = $q->query(
     138            array(
     139                'fields'          => 'ids',
     140                'network__not_in' => $excluded,
     141            )
     142        );
     143
     144        $this->assertSameSets( $expected, $found );
     145    }
     146
     147    public function test_wp_network_query_by_network__not_in_with_multiple_ids() {
     148        $excluded = array( self::$network_ids['wordpress.org/'], self::$network_ids['www.w.org/foo/'] );
     149        $expected = array_diff( self::$network_ids, $excluded );
     150
     151        // Exclude main network since we don't have control over it here.
     152        $excluded[] = 1;
     153
     154        $q     = new WP_Network_Query();
     155        $found = $q->query(
     156            array(
     157                'fields'          => 'ids',
     158                'network__not_in' => $excluded,
     159            )
     160        );
     161
     162        $this->assertSameSets( $expected, $found );
     163    }
     164
     165    public function test_wp_network_query_by_domain() {
     166        $q     = new WP_Network_Query();
     167        $found = $q->query(
     168            array(
     169                'fields' => 'ids',
     170                'domain' => 'www.w.org',
     171            )
     172        );
     173
     174        $expected = array(
     175            self::$network_ids['www.w.org/foo/'],
     176        );
     177
     178        $this->assertSameSets( $expected, $found );
     179    }
     180
     181    public function test_wp_network_query_by_domain__in_with_single_domain() {
     182        $q     = new WP_Network_Query();
     183        $found = $q->query(
     184            array(
     185                'fields'     => 'ids',
     186                'domain__in' => array( 'make.wordpress.org' ),
     187            )
     188        );
     189
     190        $expected = array(
     191            self::$network_ids['make.wordpress.org/'],
     192        );
     193
     194        $this->assertSameSets( $expected, $found );
     195    }
     196
     197    public function test_wp_network_query_by_domain__in_with_multiple_domains() {
     198        $q     = new WP_Network_Query();
     199        $found = $q->query(
     200            array(
     201                'fields'     => 'ids',
     202                'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
     203            )
     204        );
     205
     206        $expected = array(
     207            self::$network_ids['wordpress.org/'],
     208            self::$network_ids['make.wordpress.org/'],
     209        );
     210
     211        $this->assertSameSets( $expected, $found );
     212    }
     213
     214    public function test_wp_network_query_by_domain__in_with_multiple_domains_and_number() {
     215        $q     = new WP_Network_Query();
     216        $found = $q->query(
     217            array(
     218                'fields'     => 'ids',
     219                'number'     => 1,
     220                'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
     221            )
     222        );
     223
     224        $expected = array(
     225            self::$network_ids['wordpress.org/'],
     226        );
     227
     228        $this->assertSameSets( $expected, $found );
     229    }
     230
     231    public function test_wp_network_query_by_domain__in_with_multiple_domains_and_number_and_offset() {
     232        $q     = new WP_Network_Query();
     233        $found = $q->query(
     234            array(
     235                'fields'     => 'ids',
     236                'number'     => 1,
     237                'offset'     => 1,
     238                'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
     239            )
     240        );
     241
     242        $expected = array(
     243            self::$network_ids['make.wordpress.org/'],
     244        );
     245
     246        $this->assertSameSets( $expected, $found );
     247    }
     248
     249    public function test_wp_network_query_by_domain__not_in_with_single_domain() {
     250        $q     = new WP_Network_Query();
     251        $found = $q->query(
     252            array(
     253                'fields'         => 'ids',
     254                'domain__not_in' => array( 'www.w.org' ),
     255            )
     256        );
     257
     258        $expected = array(
     259            get_current_site()->id, // Account for the initial network added by the test suite.
     260            self::$network_ids['wordpress.org/'],
     261            self::$network_ids['make.wordpress.org/'],
     262            self::$network_ids['www.wordpress.net/'],
     263        );
     264
     265        $this->assertSameSets( $expected, $found );
     266    }
     267
     268    public function test_wp_network_query_by_domain__not_in_with_multiple_domains() {
     269        $q     = new WP_Network_Query();
     270        $found = $q->query(
     271            array(
     272                'fields'         => 'ids',
     273                'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
     274            )
     275        );
     276
     277        $expected = array(
     278            get_current_site()->id, // Account for the initial network added by the test suite.
     279            self::$network_ids['make.wordpress.org/'],
     280            self::$network_ids['www.wordpress.net/'],
     281        );
     282
     283        $this->assertSameSets( $expected, $found );
     284    }
     285
     286    public function test_wp_network_query_by_domain__not_in_with_multiple_domains_and_number() {
     287        $q     = new WP_Network_Query();
     288        $found = $q->query(
     289            array(
     290                'fields'         => 'ids',
     291                'number'         => 2,
     292                'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
     293            )
     294        );
     295
     296        $expected = array(
     297            get_current_site()->id, // Account for the initial network added by the test suite.
     298            self::$network_ids['make.wordpress.org/'],
     299        );
     300
     301        $this->assertSameSets( $expected, $found );
     302    }
     303
     304    public function test_wp_network_query_by_domain__not_in_with_multiple_domains_and_number_and_offset() {
     305        $q     = new WP_Network_Query();
     306        $found = $q->query(
     307            array(
     308                'fields'         => 'ids',
     309                'number'         => 2,
     310                'offset'         => 1,
     311                'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
     312            )
     313        );
     314
     315        $expected = array(
     316            self::$network_ids['make.wordpress.org/'],
     317            self::$network_ids['www.wordpress.net/'],
     318        );
     319
     320        $this->assertSameSets( $expected, $found );
     321    }
     322
     323    public function test_wp_network_query_by_path_with_expected_results() {
     324        $q     = new WP_Network_Query();
     325        $found = $q->query(
     326            array(
     327                'fields'          => 'ids',
     328                'path'            => '/',
     329                'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
     330            )
     331        );
     332
     333        $expected = array(
     334            self::$network_ids['wordpress.org/'],
     335            self::$network_ids['make.wordpress.org/'],
     336            self::$network_ids['www.wordpress.net/'],
     337        );
     338
     339        $this->assertSameSets( $expected, $found );
     340    }
     341
     342    public function test_wp_network_query_by_path_and_number_and_offset_with_expected_results() {
     343        $q     = new WP_Network_Query();
     344        $found = $q->query(
     345            array(
     346                'fields'          => 'ids',
     347                'number'          => 1,
     348                'offset'          => 2,
     349                'path'            => '/',
     350                'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
     351            )
     352        );
     353
     354        $expected = array(
     355            self::$network_ids['www.wordpress.net/'],
     356        );
     357
     358        $this->assertSameSets( $expected, $found );
     359    }
     360
     361    public function test_wp_network_query_by_path_with_no_expected_results() {
     362        $q     = new WP_Network_Query();
     363        $found = $q->query(
     364            array(
     365                'fields' => 'ids',
     366                'path'   => '/bar/',
     367            )
     368        );
     369
     370        $this->assertEmpty( $found );
     371    }
     372
     373    public function test_wp_network_query_by_search_with_text_in_domain() {
     374        $q     = new WP_Network_Query();
     375        $found = $q->query(
     376            array(
     377                'fields' => 'ids',
     378                'search' => 'ww.word',
     379            )
     380        );
     381
     382        $expected = array(
     383            self::$network_ids['www.wordpress.net/'],
     384        );
     385
     386        $this->assertSameSets( $expected, $found );
     387    }
     388
     389    public function test_wp_network_query_by_search_with_text_in_path() {
     390        $q     = new WP_Network_Query();
     391        $found = $q->query(
     392            array(
     393                'fields' => 'ids',
     394                'search' => 'foo',
     395            )
     396        );
     397
     398        $expected = array(
     399            self::$network_ids['www.w.org/foo/'],
     400        );
     401
     402        $this->assertSameSets( $expected, $found );
     403    }
     404
     405    public function test_wp_network_query_by_path_order_by_domain_desc() {
     406        $q     = new WP_Network_Query();
     407        $found = $q->query(
     408            array(
     409                'fields'          => 'ids',
     410                'path'            => '/',
     411                'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
     412                'order'           => 'DESC',
     413                'orderby'         => 'domain',
     414            )
     415        );
     416
     417        $expected = array(
     418            self::$network_ids['www.wordpress.net/'],
     419            self::$network_ids['wordpress.org/'],
     420            self::$network_ids['make.wordpress.org/'],
     421        );
     422
     423        $this->assertSame( $expected, $found );
     424    }
     425
     426    /**
     427     * @ticket 41347
     428     */
     429    public function test_wp_network_query_cache_with_different_fields_no_count() {
     430        $q                 = new WP_Network_Query();
     431        $query_1           = $q->query(
     432            array(
     433                'fields' => 'all',
     434                'number' => 3,
     435                'order'  => 'ASC',
     436            )
     437        );
     438        $number_of_queries = get_num_queries();
     439
     440        $query_2 = $q->query(
     441            array(
     442                'fields' => 'ids',
     443                'number' => 3,
     444                'order'  => 'ASC',
     445            )
     446        );
     447
     448        $this->assertSame( $number_of_queries, get_num_queries() );
     449    }
     450
     451    /**
     452     * @ticket 41347
     453     */
     454    public function test_wp_network_query_cache_with_different_fields_active_count() {
     455        $q = new WP_Network_Query();
     456
     457        $query_1           = $q->query(
     458            array(
     459                'fields' => 'all',
     460                'number' => 3,
     461                'order'  => 'ASC',
     462                'count'  => true,
     463            )
     464        );
     465        $number_of_queries = get_num_queries();
     466
     467        $query_2 = $q->query(
     468            array(
     469                'fields' => 'ids',
     470                'number' => 3,
     471                'order'  => 'ASC',
     472                'count'  => true,
     473            )
     474        );
     475        $this->assertSame( $number_of_queries, get_num_queries() );
     476    }
     477
     478    /**
     479     * @ticket 41347
     480     */
     481    public function test_wp_network_query_cache_with_same_fields_different_count() {
     482        $q = new WP_Network_Query();
     483
     484        $query_1 = $q->query(
     485            array(
     486                'fields' => 'ids',
     487                'number' => 3,
     488                'order'  => 'ASC',
     489            )
     490        );
     491
     492        $number_of_queries = get_num_queries();
     493
     494        $query_2 = $q->query(
     495            array(
     496                'fields' => 'ids',
     497                'number' => 3,
     498                'order'  => 'ASC',
     499                'count'  => true,
     500            )
     501        );
     502        $this->assertSame( $number_of_queries + 1, get_num_queries() );
     503    }
     504
     505    /**
     506     * @ticket 55461
     507     */
     508    public function test_wp_network_query_cache_with_same_fields_same_cache_field() {
     509        $q                 = new WP_Network_Query();
     510        $query_1           = $q->query(
     511            array(
     512                'fields'               => 'all',
     513                'number'               => 3,
     514                'order'                => 'ASC',
     515                'update_network_cache' => true,
     516            )
     517        );
     518        $number_of_queries = get_num_queries();
     519
     520        $query_2 = $q->query(
     521            array(
     522                'fields'               => 'all',
     523                'number'               => 3,
     524                'order'                => 'ASC',
     525                'update_network_cache' => true,
     526            )
     527        );
     528
     529        $this->assertSame( $number_of_queries, get_num_queries() );
     530    }
     531
     532    /**
     533     * @ticket 55461
     534     */
     535    public function test_wp_network_query_cache_with_same_fields_different_cache_field() {
     536        $q                 = new WP_Network_Query();
     537        $query_1           = $q->query(
     538            array(
     539                'fields'               => 'all',
     540                'number'               => 3,
     541                'order'                => 'ASC',
     542                'update_network_cache' => true,
     543            )
     544        );
     545        $number_of_queries = get_num_queries();
     546
     547        $query_2 = $q->query(
     548            array(
     549                'fields'               => 'all',
     550                'number'               => 3,
     551                'order'                => 'ASC',
     552                'update_network_cache' => false,
     553            )
     554        );
     555
     556        $this->assertSame( $number_of_queries, get_num_queries() );
     557    }
     558
     559    /**
     560     * @ticket 45749
     561     * @ticket 47599
     562     */
     563    public function test_networks_pre_query_filter_should_bypass_database_query() {
     564        add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
     565
     566        $num_queries = get_num_queries();
     567
     568        $q       = new WP_Network_Query();
     569        $results = $q->query( array() );
     570
     571        remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
     572
     573        // Make sure no queries were executed.
     574        $this->assertSame( $num_queries, get_num_queries() );
     575
     576        // We manually inserted a non-existing site and overrode the results with it.
     577        $this->assertSame( array( 555 ), $results );
     578
     579        // Make sure manually setting found_networks doesn't get overwritten.
     580        $this->assertSame( 1, $q->found_networks );
     581    }
     582
     583    public static function filter_networks_pre_query( $networks, $query ) {
     584        $query->found_networks = 1;
     585
     586        return array( 555 );
     587    }
     588
     589    /**
     590     * @ticket 51333
     591     */
     592    public function test_networks_pre_query_filter_should_set_networks_property() {
     593        add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10, 2 );
     594
     595        $q       = new WP_Network_Query();
     596        $results = $q->query( array() );
     597
     598        remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10 );
     599
     600        // Make sure the networks property is the same as the results.
     601        $this->assertSame( $results, $q->networks );
     602
     603        // Make sure the network domain is `wordpress.org`.
     604        $this->assertSame( 'wordpress.org', $q->networks[0]->domain );
     605    }
     606
     607    public static function filter_networks_pre_query_and_set_networks( $networks, $query ) {
     608        return array( get_network( self::$network_ids['wordpress.org/'] ) );
     609    }
     610
     611    /**
     612     * @ticket 56841
     613     */
     614    public function test_wp_network_query_does_not_have_leading_whitespace() {
     615        $q = new WP_Network_Query();
     616        $q->query(
     617            array(
     618                'fields'               => 'all',
     619                'number'               => 3,
     620                'order'                => 'ASC',
     621                'update_network_cache' => true,
     622            )
     623        );
     624
     625        $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     626    }
     627}
  • trunk/tests/phpunit/tests/multisite/wpSiteQuery.php

    r57750 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Test site query functionality in multisite.
     5 *
     6 * @group ms-required
     7 * @group ms-site
     8 * @group multisite
     9 */
     10class Tests_Multisite_wpSiteQuery extends WP_UnitTestCase {
     11    protected static $network_ids;
     12    protected static $site_ids;
     13
     14    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     15        self::$network_ids = array(
     16            'wordpress.org/'      => array(
     17                'domain' => 'wordpress.org',
     18                'path'   => '/',
     19            ),
     20            'make.wordpress.org/' => array(
     21                'domain' => 'make.wordpress.org',
     22                'path'   => '/',
     23            ),
     24            'www.wordpress.net/'  => array(
     25                'domain' => 'www.wordpress.net',
     26                'path'   => '/',
     27            ),
     28        );
     29
     30        foreach ( self::$network_ids as &$id ) {
     31            $id = $factory->network->create( $id );
     32        }
     33        unset( $id );
     34
     35        self::$site_ids = array(
     36            'wordpress.org/'          => array(
     37                'domain'     => 'wordpress.org',
     38                'path'       => '/',
     39                'network_id' => self::$network_ids['wordpress.org/'],
     40            ),
     41            'wordpress.org/foo/'      => array(
     42                'domain'     => 'wordpress.org',
     43                'path'       => '/foo/',
     44                'network_id' => self::$network_ids['wordpress.org/'],
     45            ),
     46            'wordpress.org/foo/bar/'  => array(
     47                'domain'     => 'wordpress.org',
     48                'path'       => '/foo/bar/',
     49                'network_id' => self::$network_ids['wordpress.org/'],
     50            ),
     51            'make.wordpress.org/'     => array(
     52                'domain'     => 'make.wordpress.org',
     53                'path'       => '/',
     54                'network_id' => self::$network_ids['make.wordpress.org/'],
     55            ),
     56            'make.wordpress.org/foo/' => array(
     57                'domain'     => 'make.wordpress.org',
     58                'path'       => '/foo/',
     59                'network_id' => self::$network_ids['make.wordpress.org/'],
     60            ),
     61            'www.w.org/'              => array(
     62                'domain' => 'www.w.org',
     63                'path'   => '/',
     64            ),
     65            'www.w.org/foo/'          => array(
     66                'domain' => 'www.w.org',
     67                'path'   => '/foo/',
     68            ),
     69            'www.w.org/foo/bar/'      => array(
     70                'domain' => 'www.w.org',
     71                'path'   => '/foo/bar/',
     72            ),
     73            'www.w.org/make/'         => array(
     74                'domain'  => 'www.w.org',
     75                'path'    => '/make/',
     76                'public'  => 1,
     77                'lang_id' => 1,
     78            ),
     79        );
     80
     81        foreach ( self::$site_ids as &$id ) {
     82            $id = $factory->blog->create( $id );
     83        }
     84        unset( $id );
     85    }
     86
     87    public static function wpTearDownAfterClass() {
     88        global $wpdb;
     89
     90        foreach ( self::$site_ids as $id ) {
     91            wp_delete_site( $id );
     92        }
     93
     94        foreach ( self::$network_ids as $id ) {
     95            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     96            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     97        }
     98
     99        wp_update_network_site_counts();
     100    }
     101
     102    public function test_wp_site_query_by_ID() {
     103        $q     = new WP_Site_Query();
     104        $found = $q->query(
     105            array(
     106                'fields' => 'ids',
     107                'ID'     => self::$site_ids['www.w.org/'],
     108            )
     109        );
     110
     111        $this->assertSameSets( array( self::$site_ids['www.w.org/'] ), $found );
     112    }
     113
     114    public function test_wp_site_query_by_number() {
     115        $q     = new WP_Site_Query();
     116        $found = $q->query(
     117            array(
     118                'fields' => 'ids',
     119                'number' => 3,
     120            )
     121        );
     122
     123        $this->assertCount( 3, $found );
     124    }
     125
     126    public function test_wp_site_query_by_site__in_with_single_id() {
     127        $expected = array( self::$site_ids['wordpress.org/foo/'] );
     128
     129        $q     = new WP_Site_Query();
     130        $found = $q->query(
     131            array(
     132                'fields'   => 'ids',
     133                'site__in' => $expected,
     134            )
     135        );
     136
     137        $this->assertSameSets( $expected, $found );
     138    }
     139
     140    public function test_wp_site_query_by_site__in_with_multiple_ids() {
     141        $expected = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
     142
     143        $q     = new WP_Site_Query();
     144        $found = $q->query(
     145            array(
     146                'fields'   => 'ids',
     147                'site__in' => $expected,
     148            )
     149        );
     150
     151        $this->assertSameSets( $expected, $found );
     152    }
    4153
    5154    /**
    6      * Test site query functionality in multisite.
    7      *
    8      * @group ms-site
    9      * @group multisite
     155     * Test the `count` query var
    10156     */
    11     class Tests_Multisite_wpSiteQuery extends WP_UnitTestCase {
    12         protected static $network_ids;
    13         protected static $site_ids;
    14 
    15         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    16             self::$network_ids = array(
    17                 'wordpress.org/'      => array(
    18                     'domain' => 'wordpress.org',
    19                     'path'   => '/',
    20                 ),
    21                 'make.wordpress.org/' => array(
    22                     'domain' => 'make.wordpress.org',
    23                     'path'   => '/',
    24                 ),
    25                 'www.wordpress.net/'  => array(
    26                     'domain' => 'www.wordpress.net',
    27                     'path'   => '/',
    28                 ),
    29             );
    30 
    31             foreach ( self::$network_ids as &$id ) {
    32                 $id = $factory->network->create( $id );
    33             }
    34             unset( $id );
    35 
    36             self::$site_ids = array(
    37                 'wordpress.org/'          => array(
    38                     'domain'     => 'wordpress.org',
    39                     'path'       => '/',
    40                     'network_id' => self::$network_ids['wordpress.org/'],
    41                 ),
    42                 'wordpress.org/foo/'      => array(
    43                     'domain'     => 'wordpress.org',
    44                     'path'       => '/foo/',
    45                     'network_id' => self::$network_ids['wordpress.org/'],
    46                 ),
    47                 'wordpress.org/foo/bar/'  => array(
    48                     'domain'     => 'wordpress.org',
    49                     'path'       => '/foo/bar/',
    50                     'network_id' => self::$network_ids['wordpress.org/'],
    51                 ),
    52                 'make.wordpress.org/'     => array(
    53                     'domain'     => 'make.wordpress.org',
    54                     'path'       => '/',
    55                     'network_id' => self::$network_ids['make.wordpress.org/'],
    56                 ),
    57                 'make.wordpress.org/foo/' => array(
    58                     'domain'     => 'make.wordpress.org',
    59                     'path'       => '/foo/',
    60                     'network_id' => self::$network_ids['make.wordpress.org/'],
    61                 ),
    62                 'www.w.org/'              => array(
    63                     'domain' => 'www.w.org',
    64                     'path'   => '/',
    65                 ),
    66                 'www.w.org/foo/'          => array(
    67                     'domain' => 'www.w.org',
    68                     'path'   => '/foo/',
    69                 ),
    70                 'www.w.org/foo/bar/'      => array(
    71                     'domain' => 'www.w.org',
    72                     'path'   => '/foo/bar/',
    73                 ),
    74                 'www.w.org/make/'         => array(
    75                     'domain'  => 'www.w.org',
    76                     'path'    => '/make/',
    77                     'public'  => 1,
    78                     'lang_id' => 1,
    79                 ),
    80             );
    81 
    82             foreach ( self::$site_ids as &$id ) {
    83                 $id = $factory->blog->create( $id );
    84             }
    85             unset( $id );
     157    public function test_wp_site_query_by_site__in_and_count_with_multiple_ids() {
     158        $expected = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
     159
     160        $q     = new WP_Site_Query();
     161        $found = $q->query(
     162            array(
     163                'fields'   => 'ids',
     164                'count'    => true,
     165                'site__in' => $expected,
     166            )
     167        );
     168
     169        $this->assertSame( 2, $found );
     170    }
     171
     172    public function test_wp_site_query_by_site__not_in_with_single_id() {
     173        $excluded = array( self::$site_ids['wordpress.org/foo/'] );
     174        $expected = array_diff( self::$site_ids, $excluded );
     175
     176        // Exclude main site since we don't have control over it here.
     177        $excluded[] = 1;
     178
     179        $q     = new WP_Site_Query();
     180        $found = $q->query(
     181            array(
     182                'fields'       => 'ids',
     183                'site__not_in' => $excluded,
     184            )
     185        );
     186
     187        $this->assertSameSets( $expected, $found );
     188    }
     189
     190    public function test_wp_site_query_by_site__not_in_with_multiple_ids() {
     191        $excluded = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
     192        $expected = array_diff( self::$site_ids, $excluded );
     193
     194        // Exclude main site since we don't have control over it here.
     195        $excluded[] = 1;
     196
     197        $q     = new WP_Site_Query();
     198        $found = $q->query(
     199            array(
     200                'fields'       => 'ids',
     201                'site__not_in' => $excluded,
     202            )
     203        );
     204
     205        $this->assertSameSets( $expected, $found );
     206    }
     207
     208    public function test_wp_site_query_by_network_id_with_order() {
     209        $q     = new WP_Site_Query();
     210        $found = $q->query(
     211            array(
     212                'fields'     => 'ids',
     213                'network_id' => self::$network_ids['wordpress.org/'],
     214                'number'     => 3,
     215                'order'      => 'ASC',
     216            )
     217        );
     218
     219        $expected = array(
     220            self::$site_ids['wordpress.org/'],
     221            self::$site_ids['wordpress.org/foo/'],
     222            self::$site_ids['wordpress.org/foo/bar/'],
     223        );
     224
     225        $this->assertSame( $expected, $found );
     226
     227        $found = $q->query(
     228            array(
     229                'fields'     => 'ids',
     230                'network_id' => self::$network_ids['wordpress.org/'],
     231                'number'     => 3,
     232                'order'      => 'DESC',
     233            )
     234        );
     235
     236        $this->assertSame( array_reverse( $expected ), $found );
     237    }
     238
     239    public function test_wp_site_query_by_network_id_with_existing_sites() {
     240        $q     = new WP_Site_Query();
     241        $found = $q->query(
     242            array(
     243                'fields'     => 'ids',
     244                'network_id' => self::$network_ids['make.wordpress.org/'],
     245            )
     246        );
     247
     248        $expected = array(
     249            self::$site_ids['make.wordpress.org/'],
     250            self::$site_ids['make.wordpress.org/foo/'],
     251        );
     252
     253        $this->assertSameSets( $expected, $found );
     254    }
     255
     256    public function test_wp_site_query_by_network_id_with_no_existing_sites() {
     257        $q     = new WP_Site_Query();
     258        $found = $q->query(
     259            array(
     260                'fields'     => 'ids',
     261                'network_id' => self::$network_ids['www.wordpress.net/'],
     262            )
     263        );
     264
     265        $this->assertEmpty( $found );
     266    }
     267
     268    public function test_wp_site_query_by_domain() {
     269        $q     = new WP_Site_Query();
     270        $found = $q->query(
     271            array(
     272                'fields' => 'ids',
     273                'domain' => 'www.w.org',
     274            )
     275        );
     276
     277        $expected = array(
     278            self::$site_ids['www.w.org/'],
     279            self::$site_ids['www.w.org/foo/'],
     280            self::$site_ids['www.w.org/foo/bar/'],
     281            self::$site_ids['www.w.org/make/'],
     282        );
     283
     284        $this->assertSameSets( $expected, $found );
     285    }
     286
     287    public function test_wp_site_query_by_domain_and_offset() {
     288        $q     = new WP_Site_Query();
     289        $found = $q->query(
     290            array(
     291                'fields' => 'ids',
     292                'domain' => 'www.w.org',
     293                'offset' => 1,
     294            )
     295        );
     296
     297        $expected = array(
     298            self::$site_ids['www.w.org/foo/'],
     299            self::$site_ids['www.w.org/foo/bar/'],
     300            self::$site_ids['www.w.org/make/'],
     301        );
     302
     303        $this->assertSameSets( $expected, $found );
     304    }
     305
     306    public function test_wp_site_query_by_domain_and_number_and_offset() {
     307        $q     = new WP_Site_Query();
     308        $found = $q->query(
     309            array(
     310                'fields' => 'ids',
     311                'domain' => 'www.w.org',
     312                'number' => 2,
     313                'offset' => 1,
     314            )
     315        );
     316
     317        $expected = array(
     318            self::$site_ids['www.w.org/foo/'],
     319            self::$site_ids['www.w.org/foo/bar/'],
     320        );
     321
     322        $this->assertSameSets( $expected, $found );
     323    }
     324
     325    public function test_wp_site_query_by_domain__in_with_single_domain() {
     326        $q     = new WP_Site_Query();
     327        $found = $q->query(
     328            array(
     329                'fields'     => 'ids',
     330                'domain__in' => array( 'make.wordpress.org' ),
     331            )
     332        );
     333
     334        $expected = array(
     335            self::$site_ids['make.wordpress.org/'],
     336            self::$site_ids['make.wordpress.org/foo/'],
     337        );
     338
     339        $this->assertSameSets( $expected, $found );
     340    }
     341
     342    public function test_wp_site_query_by_domain__in_with_multiple_domains() {
     343        $q     = new WP_Site_Query();
     344        $found = $q->query(
     345            array(
     346                'fields'     => 'ids',
     347                'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
     348            )
     349        );
     350
     351        $expected = array(
     352            self::$site_ids['wordpress.org/'],
     353            self::$site_ids['wordpress.org/foo/'],
     354            self::$site_ids['wordpress.org/foo/bar/'],
     355            self::$site_ids['make.wordpress.org/'],
     356            self::$site_ids['make.wordpress.org/foo/'],
     357        );
     358
     359        $this->assertSameSets( $expected, $found );
     360    }
     361
     362    public function test_wp_site_query_by_domain__not_in_with_single_domain() {
     363        $q     = new WP_Site_Query();
     364        $found = $q->query(
     365            array(
     366                'fields'         => 'ids',
     367                'domain__not_in' => array( 'www.w.org' ),
     368            )
     369        );
     370
     371        $expected = array(
     372            get_current_blog_id(), // Account for the initial site added by the test suite.
     373            self::$site_ids['wordpress.org/'],
     374            self::$site_ids['wordpress.org/foo/'],
     375            self::$site_ids['wordpress.org/foo/bar/'],
     376            self::$site_ids['make.wordpress.org/'],
     377            self::$site_ids['make.wordpress.org/foo/'],
     378        );
     379
     380        $this->assertSameSets( $expected, $found );
     381    }
     382
     383    public function test_wp_site_query_by_domain__not_in_with_multiple_domains() {
     384        $q     = new WP_Site_Query();
     385        $found = $q->query(
     386            array(
     387                'fields'         => 'ids',
     388                'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
     389            )
     390        );
     391
     392        $expected = array(
     393            get_current_blog_id(), // Account for the initial site added by the test suite.
     394            self::$site_ids['make.wordpress.org/'],
     395            self::$site_ids['make.wordpress.org/foo/'],
     396        );
     397
     398        $this->assertSameSets( $expected, $found );
     399    }
     400
     401    public function test_wp_site_query_by_path_with_expected_results() {
     402        $q     = new WP_Site_Query();
     403        $found = $q->query(
     404            array(
     405                'fields' => 'ids',
     406                'path'   => '/foo/bar/',
     407            )
     408        );
     409
     410        $expected = array(
     411            self::$site_ids['wordpress.org/foo/bar/'],
     412            self::$site_ids['www.w.org/foo/bar/'],
     413        );
     414
     415        $this->assertSameSets( $expected, $found );
     416    }
     417
     418    public function test_wp_site_query_by_path_with_no_expected_results() {
     419        $q     = new WP_Site_Query();
     420        $found = $q->query(
     421            array(
     422                'fields' => 'ids',
     423                'path'   => '/foo/bar/foo/',
     424            )
     425        );
     426
     427        $this->assertEmpty( $found );
     428    }
     429
     430    // archived, mature, spam, deleted, public.
     431
     432    public function test_wp_site_query_by_archived() {
     433        $q     = new WP_Site_Query();
     434        $found = $q->query(
     435            array(
     436                'fields'       => 'ids',
     437                // Exclude main site since we don't have control over it here.
     438                'site__not_in' => array( 1 ),
     439                'archived'     => '0',
     440            )
     441        );
     442
     443        $this->assertSameSets( array_values( self::$site_ids ), $found );
     444    }
     445
     446    public function test_wp_site_query_by_mature() {
     447        $q     = new WP_Site_Query();
     448        $found = $q->query(
     449            array(
     450                'fields'       => 'ids',
     451                // Exclude main site since we don't have control over it here.
     452                'site__not_in' => array( 1 ),
     453                'mature'       => '0',
     454            )
     455        );
     456
     457        $this->assertSameSets( array_values( self::$site_ids ), $found );
     458    }
     459
     460    public function test_wp_site_query_by_spam() {
     461        $q     = new WP_Site_Query();
     462        $found = $q->query(
     463            array(
     464                'fields'       => 'ids',
     465                // Exclude main site since we don't have control over it here.
     466                'site__not_in' => array( 1 ),
     467                'spam'         => '0',
     468            )
     469        );
     470
     471        $this->assertSameSets( array_values( self::$site_ids ), $found );
     472    }
     473
     474    public function test_wp_site_query_by_deleted() {
     475        $q     = new WP_Site_Query();
     476        $found = $q->query(
     477            array(
     478                'fields'       => 'ids',
     479                // Exclude main site since we don't have control over it here.
     480                'site__not_in' => array( 1 ),
     481                'deleted'      => '0',
     482            )
     483        );
     484
     485        $this->assertSameSets( array_values( self::$site_ids ), $found );
     486    }
     487
     488    public function test_wp_site_query_by_deleted_with_no_results() {
     489        $q     = new WP_Site_Query();
     490        $found = $q->query(
     491            array(
     492                'fields'  => 'ids',
     493                'deleted' => '1',
     494            )
     495        );
     496
     497        $this->assertEmpty( $found );
     498    }
     499
     500    public function test_wp_site_query_by_public() {
     501        $q     = new WP_Site_Query();
     502        $found = $q->query(
     503            array(
     504                'fields'       => 'ids',
     505                // Exclude main site since we don't have control over it here.
     506                'site__not_in' => array( 1 ),
     507                'public'       => '1',
     508            )
     509        );
     510
     511        $this->assertSameSets( array_values( self::$site_ids ), $found );
     512    }
     513
     514    public function test_wp_site_query_by_lang_id_with_zero() {
     515        $q     = new WP_Site_Query();
     516        $found = $q->query(
     517            array(
     518                'fields'       => 'ids',
     519                // Exclude main site since we don't have control over it here.
     520                'site__not_in' => array( 1 ),
     521                'lang_id'      => 0,
     522            )
     523        );
     524
     525        $this->assertSameSets( array_diff( array_values( self::$site_ids ), array( self::$site_ids['www.w.org/make/'] ) ), $found );
     526    }
     527
     528    public function test_wp_site_query_by_lang_id() {
     529        $q     = new WP_Site_Query();
     530        $found = $q->query(
     531            array(
     532                'fields'  => 'ids',
     533                'lang_id' => 1,
     534            )
     535        );
     536
     537        $expected = array(
     538            self::$site_ids['www.w.org/make/'],
     539        );
     540
     541        $this->assertSameSets( $expected, $found );
     542    }
     543
     544    public function test_wp_site_query_by_lang_id_with_no_results() {
     545        $q     = new WP_Site_Query();
     546        $found = $q->query(
     547            array(
     548                'fields'  => 'ids',
     549                'lang_id' => 2,
     550            )
     551        );
     552
     553        $this->assertEmpty( $found );
     554    }
     555
     556    public function test_wp_site_query_by_lang__in() {
     557        $q     = new WP_Site_Query();
     558        $found = $q->query(
     559            array(
     560                'fields'   => 'ids',
     561                'lang__in' => array( 1 ),
     562            )
     563        );
     564
     565        $expected = array(
     566            self::$site_ids['www.w.org/make/'],
     567        );
     568
     569        $this->assertSameSets( $expected, $found );
     570    }
     571
     572    public function test_wp_site_query_by_lang__in_with_multiple_ids() {
     573        $q     = new WP_Site_Query();
     574        $found = $q->query(
     575            array(
     576                'fields'       => 'ids',
     577                // Exclude main site since we don't have control over it here.
     578                'site__not_in' => array( 1 ),
     579                'lang__in'     => array( 0, 1 ),
     580            )
     581        );
     582
     583        $this->assertSameSets( array_values( self::$site_ids ), $found );
     584    }
     585
     586    public function test_wp_site_query_by_lang__not_in() {
     587        $q     = new WP_Site_Query();
     588        $found = $q->query(
     589            array(
     590                'fields'       => 'ids',
     591                'lang__not_in' => array( 0 ),
     592            )
     593        );
     594
     595        $expected = array(
     596            self::$site_ids['www.w.org/make/'],
     597        );
     598
     599        $this->assertSameSets( $expected, $found );
     600    }
     601
     602    public function test_wp_site_query_by_lang__not_in_with_multiple_ids() {
     603        $q     = new WP_Site_Query();
     604        $found = $q->query(
     605            array(
     606                'fields'       => 'ids',
     607                'lang__not_in' => array( 0, 1 ),
     608            )
     609        );
     610
     611        $this->assertEmpty( $found );
     612    }
     613
     614    public function test_wp_site_query_by_search_with_text_in_domain() {
     615        $q     = new WP_Site_Query();
     616        $found = $q->query(
     617            array(
     618                'fields' => 'ids',
     619                'search' => 'ke.wordp',
     620            )
     621        );
     622
     623        $expected = array(
     624            self::$site_ids['make.wordpress.org/'],
     625            self::$site_ids['make.wordpress.org/foo/'],
     626        );
     627
     628        $this->assertSameSets( $expected, $found );
     629    }
     630
     631    public function test_wp_site_query_by_search_with_text_in_path() {
     632        $q     = new WP_Site_Query();
     633        $found = $q->query(
     634            array(
     635                'fields' => 'ids',
     636                'search' => 'foo',
     637            )
     638        );
     639
     640        $expected = array(
     641            self::$site_ids['wordpress.org/foo/'],
     642            self::$site_ids['wordpress.org/foo/bar/'],
     643            self::$site_ids['make.wordpress.org/foo/'],
     644            self::$site_ids['www.w.org/foo/'],
     645            self::$site_ids['www.w.org/foo/bar/'],
     646        );
     647
     648        $this->assertSameSets( $expected, $found );
     649    }
     650
     651    public function test_wp_site_query_by_search_with_text_in_path_and_domain() {
     652        $q     = new WP_Site_Query();
     653        $found = $q->query(
     654            array(
     655                'fields' => 'ids',
     656                'search' => 'make',
     657            )
     658        );
     659
     660        $expected = array(
     661            self::$site_ids['make.wordpress.org/'],
     662            self::$site_ids['make.wordpress.org/foo/'],
     663            self::$site_ids['www.w.org/make/'],
     664        );
     665
     666        $this->assertSameSets( $expected, $found );
     667    }
     668
     669    public function test_wp_site_query_by_search_with_text_in_path_and_domain_order_by_domain_desc() {
     670        $q     = new WP_Site_Query();
     671        $found = $q->query(
     672            array(
     673                'fields'  => 'ids',
     674                'search'  => 'make',
     675                'order'   => 'DESC',
     676                'orderby' => 'domain',
     677            )
     678        );
     679
     680        $expected = array(
     681            self::$site_ids['www.w.org/make/'],
     682            self::$site_ids['make.wordpress.org/'],
     683            self::$site_ids['make.wordpress.org/foo/'],
     684        );
     685
     686        $this->assertSame( $expected, $found );
     687    }
     688
     689    public function test_wp_site_query_by_search_with_text_in_path_exclude_domain_from_search() {
     690        $q     = new WP_Site_Query();
     691        $found = $q->query(
     692            array(
     693                'fields'         => 'ids',
     694                'search'         => 'make',
     695                'search_columns' => array( 'path' ),
     696            )
     697        );
     698
     699        $expected = array(
     700            self::$site_ids['www.w.org/make/'],
     701        );
     702
     703        $this->assertSame( $expected, $found );
     704    }
     705
     706    public function test_wp_site_query_by_search_with_text_in_domain_exclude_path_from_search() {
     707        $q     = new WP_Site_Query();
     708        $found = $q->query(
     709            array(
     710                'fields'         => 'ids',
     711                'search'         => 'make',
     712                'search_columns' => array( 'domain' ),
     713            )
     714        );
     715
     716        $expected = array(
     717            self::$site_ids['make.wordpress.org/'],
     718            self::$site_ids['make.wordpress.org/foo/'],
     719        );
     720
     721        $this->assertSame( $expected, $found );
     722    }
     723
     724    public function test_wp_site_query_by_search_with_wildcard_in_text() {
     725        $q     = new WP_Site_Query();
     726        $found = $q->query(
     727            array(
     728                'fields' => 'ids',
     729                'search' => 'm*ke',
     730            )
     731        );
     732
     733        $expected = array(
     734            self::$site_ids['www.w.org/make/'],
     735            self::$site_ids['make.wordpress.org/'],
     736            self::$site_ids['make.wordpress.org/foo/'],
     737        );
     738
     739        $this->assertSameSets( $expected, $found );
     740    }
     741
     742    public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_path_from_search() {
     743        $q     = new WP_Site_Query();
     744        $found = $q->query(
     745            array(
     746                'fields'         => 'ids',
     747                'search'         => 'm*ke',
     748                'search_columns' => array( 'domain' ),
     749            )
     750        );
     751
     752        $expected = array(
     753            self::$site_ids['make.wordpress.org/'],
     754            self::$site_ids['make.wordpress.org/foo/'],
     755        );
     756
     757        $this->assertSameSets( $expected, $found );
     758    }
     759
     760    public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_domain_from_search() {
     761        $q     = new WP_Site_Query();
     762        $found = $q->query(
     763            array(
     764                'fields'         => 'ids',
     765                'search'         => 'm*ke',
     766                'search_columns' => array( 'path' ),
     767            )
     768        );
     769
     770        $expected = array(
     771            self::$site_ids['www.w.org/make/'],
     772        );
     773
     774        $this->assertSameSets( $expected, $found );
     775    }
     776
     777    /**
     778     * @ticket 41197
     779     */
     780    public function test_wp_site_query_cache_with_different_fields_no_count() {
     781        $q                 = new WP_Site_Query();
     782        $query_1           = $q->query(
     783            array(
     784                'fields'     => 'all',
     785                'network_id' => self::$network_ids['wordpress.org/'],
     786                'number'     => 3,
     787                'order'      => 'ASC',
     788            )
     789        );
     790        $number_of_queries = get_num_queries();
     791
     792        $query_2 = $q->query(
     793            array(
     794                'fields'     => 'ids',
     795                'network_id' => self::$network_ids['wordpress.org/'],
     796                'number'     => 3,
     797                'order'      => 'ASC',
     798            )
     799        );
     800
     801        $this->assertSame( $number_of_queries, get_num_queries() );
     802    }
     803
     804    /**
     805     * @ticket 41197
     806     */
     807    public function test_wp_site_query_cache_with_different_fields_active_count() {
     808        $q = new WP_Site_Query();
     809
     810        $query_1           = $q->query(
     811            array(
     812                'fields'     => 'all',
     813                'network_id' => self::$network_ids['wordpress.org/'],
     814                'number'     => 3,
     815                'order'      => 'ASC',
     816                'count'      => true,
     817            )
     818        );
     819        $number_of_queries = get_num_queries();
     820
     821        $query_2 = $q->query(
     822            array(
     823                'fields'     => 'ids',
     824                'network_id' => self::$network_ids['wordpress.org/'],
     825                'number'     => 3,
     826                'order'      => 'ASC',
     827                'count'      => true,
     828            )
     829        );
     830        $this->assertSame( $number_of_queries, get_num_queries() );
     831    }
     832
     833    /**
     834     * @ticket 41197
     835     */
     836    public function test_wp_site_query_cache_with_same_fields_different_count() {
     837        $q = new WP_Site_Query();
     838
     839        $query_1 = $q->query(
     840            array(
     841                'fields'     => 'ids',
     842                'network_id' => self::$network_ids['wordpress.org/'],
     843                'number'     => 3,
     844                'order'      => 'ASC',
     845            )
     846        );
     847
     848        $number_of_queries = get_num_queries();
     849
     850        $query_2 = $q->query(
     851            array(
     852                'fields'     => 'ids',
     853                'network_id' => self::$network_ids['wordpress.org/'],
     854                'number'     => 3,
     855                'order'      => 'ASC',
     856                'count'      => true,
     857            )
     858        );
     859        $this->assertSame( $number_of_queries + 1, get_num_queries() );
     860    }
     861
     862    /**
     863     * @ticket 55462
     864     */
     865    public function test_wp_site_query_cache_with_same_fields_same_cache_fields() {
     866        $q = new WP_Site_Query();
     867
     868        $query_1 = $q->query(
     869            array(
     870                'fields'                 => 'ids',
     871                'network_id'             => self::$network_ids['wordpress.org/'],
     872                'number'                 => 3,
     873                'order'                  => 'ASC',
     874                'update_site_cache'      => true,
     875                'update_site_meta_cache' => true,
     876            )
     877        );
     878
     879        $number_of_queries = get_num_queries();
     880
     881        $query_2 = $q->query(
     882            array(
     883                'fields'                 => 'ids',
     884                'network_id'             => self::$network_ids['wordpress.org/'],
     885                'number'                 => 3,
     886                'order'                  => 'ASC',
     887                'update_site_cache'      => true,
     888                'update_site_meta_cache' => true,
     889            )
     890        );
     891        $this->assertSame( $number_of_queries, get_num_queries() );
     892    }
     893
     894    /**
     895     * @ticket 55462
     896     */
     897    public function test_wp_site_query_cache_with_same_fields_different_cache_fields() {
     898        $q = new WP_Site_Query();
     899
     900        $query_1 = $q->query(
     901            array(
     902                'fields'                 => 'ids',
     903                'network_id'             => self::$network_ids['wordpress.org/'],
     904                'number'                 => 3,
     905                'order'                  => 'ASC',
     906                'update_site_cache'      => true,
     907                'update_site_meta_cache' => true,
     908            )
     909        );
     910
     911        $number_of_queries = get_num_queries();
     912
     913        $query_2 = $q->query(
     914            array(
     915                'fields'                 => 'ids',
     916                'network_id'             => self::$network_ids['wordpress.org/'],
     917                'number'                 => 3,
     918                'order'                  => 'ASC',
     919                'update_site_cache'      => false,
     920                'update_site_meta_cache' => false,
     921            )
     922        );
     923        $this->assertSame( $number_of_queries, get_num_queries() );
     924    }
     925
     926    /**
     927     * @ticket 40229
     928     * @dataProvider data_wp_site_query_meta_query
     929     */
     930    public function test_wp_site_query_meta_query( $query, $expected, $strict ) {
     931        if ( ! is_site_meta_supported() ) {
     932            $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    86933        }
    87934
    88         public static function wpTearDownAfterClass() {
    89             global $wpdb;
    90 
    91             foreach ( self::$site_ids as $id ) {
    92                 wp_delete_site( $id );
    93             }
    94 
    95             foreach ( self::$network_ids as $id ) {
    96                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    97                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    98             }
    99 
    100             wp_update_network_site_counts();
     935        add_site_meta( self::$site_ids['wordpress.org/'], 'foo', 'foo' );
     936        add_site_meta( self::$site_ids['wordpress.org/foo/'], 'foo', 'bar' );
     937        add_site_meta( self::$site_ids['wordpress.org/foo/bar/'], 'foo', 'baz' );
     938        add_site_meta( self::$site_ids['make.wordpress.org/'], 'bar', 'baz' );
     939        add_site_meta( self::$site_ids['wordpress.org/'], 'numberfoo', 1 );
     940        add_site_meta( self::$site_ids['wordpress.org/foo/'], 'numberfoo', 2 );
     941
     942        $query['fields'] = 'ids';
     943
     944        $q     = new WP_Site_Query();
     945        $found = $q->query( $query );
     946
     947        foreach ( $expected as $index => $domain_path ) {
     948            $expected[ $index ] = self::$site_ids[ $domain_path ];
    101949        }
    102950
    103         public function test_wp_site_query_by_ID() {
    104             $q     = new WP_Site_Query();
    105             $found = $q->query(
    106                 array(
    107                     'fields' => 'ids',
    108                     'ID'     => self::$site_ids['www.w.org/'],
    109                 )
    110             );
    111 
    112             $this->assertSameSets( array( self::$site_ids['www.w.org/'] ), $found );
    113         }
    114 
    115         public function test_wp_site_query_by_number() {
    116             $q     = new WP_Site_Query();
    117             $found = $q->query(
    118                 array(
    119                     'fields' => 'ids',
    120                     'number' => 3,
    121                 )
    122             );
    123 
    124             $this->assertCount( 3, $found );
    125         }
    126 
    127         public function test_wp_site_query_by_site__in_with_single_id() {
    128             $expected = array( self::$site_ids['wordpress.org/foo/'] );
    129 
    130             $q     = new WP_Site_Query();
    131             $found = $q->query(
    132                 array(
    133                     'fields'   => 'ids',
    134                     'site__in' => $expected,
    135                 )
    136             );
    137 
     951        if ( $strict ) {
     952            $this->assertSame( $expected, $found );
     953        } else {
    138954            $this->assertSameSets( $expected, $found );
    139955        }
    140 
    141         public function test_wp_site_query_by_site__in_with_multiple_ids() {
    142             $expected = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
    143 
    144             $q     = new WP_Site_Query();
    145             $found = $q->query(
    146                 array(
    147                     'fields'   => 'ids',
    148                     'site__in' => $expected,
    149                 )
    150             );
    151 
    152             $this->assertSameSets( $expected, $found );
    153         }
    154 
    155         /**
    156          * Test the `count` query var
    157          */
    158         public function test_wp_site_query_by_site__in_and_count_with_multiple_ids() {
    159             $expected = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
    160 
    161             $q     = new WP_Site_Query();
    162             $found = $q->query(
    163                 array(
    164                     'fields'   => 'ids',
    165                     'count'    => true,
    166                     'site__in' => $expected,
    167                 )
    168             );
    169 
    170             $this->assertSame( 2, $found );
    171         }
    172 
    173         public function test_wp_site_query_by_site__not_in_with_single_id() {
    174             $excluded = array( self::$site_ids['wordpress.org/foo/'] );
    175             $expected = array_diff( self::$site_ids, $excluded );
    176 
    177             // Exclude main site since we don't have control over it here.
    178             $excluded[] = 1;
    179 
    180             $q     = new WP_Site_Query();
    181             $found = $q->query(
    182                 array(
    183                     'fields'       => 'ids',
    184                     'site__not_in' => $excluded,
    185                 )
    186             );
    187 
    188             $this->assertSameSets( $expected, $found );
    189         }
    190 
    191         public function test_wp_site_query_by_site__not_in_with_multiple_ids() {
    192             $excluded = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
    193             $expected = array_diff( self::$site_ids, $excluded );
    194 
    195             // Exclude main site since we don't have control over it here.
    196             $excluded[] = 1;
    197 
    198             $q     = new WP_Site_Query();
    199             $found = $q->query(
    200                 array(
    201                     'fields'       => 'ids',
    202                     'site__not_in' => $excluded,
    203                 )
    204             );
    205 
    206             $this->assertSameSets( $expected, $found );
    207         }
    208 
    209         public function test_wp_site_query_by_network_id_with_order() {
    210             $q     = new WP_Site_Query();
    211             $found = $q->query(
    212                 array(
    213                     'fields'     => 'ids',
    214                     'network_id' => self::$network_ids['wordpress.org/'],
    215                     'number'     => 3,
    216                     'order'      => 'ASC',
    217                 )
    218             );
    219 
    220             $expected = array(
    221                 self::$site_ids['wordpress.org/'],
    222                 self::$site_ids['wordpress.org/foo/'],
    223                 self::$site_ids['wordpress.org/foo/bar/'],
    224             );
    225 
    226             $this->assertSame( $expected, $found );
    227 
    228             $found = $q->query(
    229                 array(
    230                     'fields'     => 'ids',
    231                     'network_id' => self::$network_ids['wordpress.org/'],
    232                     'number'     => 3,
    233                     'order'      => 'DESC',
    234                 )
    235             );
    236 
    237             $this->assertSame( array_reverse( $expected ), $found );
    238         }
    239 
    240         public function test_wp_site_query_by_network_id_with_existing_sites() {
    241             $q     = new WP_Site_Query();
    242             $found = $q->query(
    243                 array(
    244                     'fields'     => 'ids',
    245                     'network_id' => self::$network_ids['make.wordpress.org/'],
    246                 )
    247             );
    248 
    249             $expected = array(
    250                 self::$site_ids['make.wordpress.org/'],
    251                 self::$site_ids['make.wordpress.org/foo/'],
    252             );
    253 
    254             $this->assertSameSets( $expected, $found );
    255         }
    256 
    257         public function test_wp_site_query_by_network_id_with_no_existing_sites() {
    258             $q     = new WP_Site_Query();
    259             $found = $q->query(
    260                 array(
    261                     'fields'     => 'ids',
    262                     'network_id' => self::$network_ids['www.wordpress.net/'],
    263                 )
    264             );
    265 
    266             $this->assertEmpty( $found );
    267         }
    268 
    269         public function test_wp_site_query_by_domain() {
    270             $q     = new WP_Site_Query();
    271             $found = $q->query(
    272                 array(
    273                     'fields' => 'ids',
    274                     'domain' => 'www.w.org',
    275                 )
    276             );
    277 
    278             $expected = array(
    279                 self::$site_ids['www.w.org/'],
    280                 self::$site_ids['www.w.org/foo/'],
    281                 self::$site_ids['www.w.org/foo/bar/'],
    282                 self::$site_ids['www.w.org/make/'],
    283             );
    284 
    285             $this->assertSameSets( $expected, $found );
    286         }
    287 
    288         public function test_wp_site_query_by_domain_and_offset() {
    289             $q     = new WP_Site_Query();
    290             $found = $q->query(
    291                 array(
    292                     'fields' => 'ids',
    293                     'domain' => 'www.w.org',
    294                     'offset' => 1,
    295                 )
    296             );
    297 
    298             $expected = array(
    299                 self::$site_ids['www.w.org/foo/'],
    300                 self::$site_ids['www.w.org/foo/bar/'],
    301                 self::$site_ids['www.w.org/make/'],
    302             );
    303 
    304             $this->assertSameSets( $expected, $found );
    305         }
    306 
    307         public function test_wp_site_query_by_domain_and_number_and_offset() {
    308             $q     = new WP_Site_Query();
    309             $found = $q->query(
    310                 array(
    311                     'fields' => 'ids',
    312                     'domain' => 'www.w.org',
    313                     'number' => 2,
    314                     'offset' => 1,
    315                 )
    316             );
    317 
    318             $expected = array(
    319                 self::$site_ids['www.w.org/foo/'],
    320                 self::$site_ids['www.w.org/foo/bar/'],
    321             );
    322 
    323             $this->assertSameSets( $expected, $found );
    324         }
    325 
    326         public function test_wp_site_query_by_domain__in_with_single_domain() {
    327             $q     = new WP_Site_Query();
    328             $found = $q->query(
    329                 array(
    330                     'fields'     => 'ids',
    331                     'domain__in' => array( 'make.wordpress.org' ),
    332                 )
    333             );
    334 
    335             $expected = array(
    336                 self::$site_ids['make.wordpress.org/'],
    337                 self::$site_ids['make.wordpress.org/foo/'],
    338             );
    339 
    340             $this->assertSameSets( $expected, $found );
    341         }
    342 
    343         public function test_wp_site_query_by_domain__in_with_multiple_domains() {
    344             $q     = new WP_Site_Query();
    345             $found = $q->query(
    346                 array(
    347                     'fields'     => 'ids',
    348                     'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
    349                 )
    350             );
    351 
    352             $expected = array(
    353                 self::$site_ids['wordpress.org/'],
    354                 self::$site_ids['wordpress.org/foo/'],
    355                 self::$site_ids['wordpress.org/foo/bar/'],
    356                 self::$site_ids['make.wordpress.org/'],
    357                 self::$site_ids['make.wordpress.org/foo/'],
    358             );
    359 
    360             $this->assertSameSets( $expected, $found );
    361         }
    362 
    363         public function test_wp_site_query_by_domain__not_in_with_single_domain() {
    364             $q     = new WP_Site_Query();
    365             $found = $q->query(
    366                 array(
    367                     'fields'         => 'ids',
    368                     'domain__not_in' => array( 'www.w.org' ),
    369                 )
    370             );
    371 
    372             $expected = array(
    373                 get_current_blog_id(), // Account for the initial site added by the test suite.
    374                 self::$site_ids['wordpress.org/'],
    375                 self::$site_ids['wordpress.org/foo/'],
    376                 self::$site_ids['wordpress.org/foo/bar/'],
    377                 self::$site_ids['make.wordpress.org/'],
    378                 self::$site_ids['make.wordpress.org/foo/'],
    379             );
    380 
    381             $this->assertSameSets( $expected, $found );
    382         }
    383 
    384         public function test_wp_site_query_by_domain__not_in_with_multiple_domains() {
    385             $q     = new WP_Site_Query();
    386             $found = $q->query(
    387                 array(
    388                     'fields'         => 'ids',
    389                     'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
    390                 )
    391             );
    392 
    393             $expected = array(
    394                 get_current_blog_id(), // Account for the initial site added by the test suite.
    395                 self::$site_ids['make.wordpress.org/'],
    396                 self::$site_ids['make.wordpress.org/foo/'],
    397             );
    398 
    399             $this->assertSameSets( $expected, $found );
    400         }
    401 
    402         public function test_wp_site_query_by_path_with_expected_results() {
    403             $q     = new WP_Site_Query();
    404             $found = $q->query(
    405                 array(
    406                     'fields' => 'ids',
    407                     'path'   => '/foo/bar/',
    408                 )
    409             );
    410 
    411             $expected = array(
    412                 self::$site_ids['wordpress.org/foo/bar/'],
    413                 self::$site_ids['www.w.org/foo/bar/'],
    414             );
    415 
    416             $this->assertSameSets( $expected, $found );
    417         }
    418 
    419         public function test_wp_site_query_by_path_with_no_expected_results() {
    420             $q     = new WP_Site_Query();
    421             $found = $q->query(
    422                 array(
    423                     'fields' => 'ids',
    424                     'path'   => '/foo/bar/foo/',
    425                 )
    426             );
    427 
    428             $this->assertEmpty( $found );
    429         }
    430 
    431         // archived, mature, spam, deleted, public.
    432 
    433         public function test_wp_site_query_by_archived() {
    434             $q     = new WP_Site_Query();
    435             $found = $q->query(
    436                 array(
    437                     'fields'       => 'ids',
    438                     // Exclude main site since we don't have control over it here.
    439                     'site__not_in' => array( 1 ),
    440                     'archived'     => '0',
    441                 )
    442             );
    443 
    444             $this->assertSameSets( array_values( self::$site_ids ), $found );
    445         }
    446 
    447         public function test_wp_site_query_by_mature() {
    448             $q     = new WP_Site_Query();
    449             $found = $q->query(
    450                 array(
    451                     'fields'       => 'ids',
    452                     // Exclude main site since we don't have control over it here.
    453                     'site__not_in' => array( 1 ),
    454                     'mature'       => '0',
    455                 )
    456             );
    457 
    458             $this->assertSameSets( array_values( self::$site_ids ), $found );
    459         }
    460 
    461         public function test_wp_site_query_by_spam() {
    462             $q     = new WP_Site_Query();
    463             $found = $q->query(
    464                 array(
    465                     'fields'       => 'ids',
    466                     // Exclude main site since we don't have control over it here.
    467                     'site__not_in' => array( 1 ),
    468                     'spam'         => '0',
    469                 )
    470             );
    471 
    472             $this->assertSameSets( array_values( self::$site_ids ), $found );
    473         }
    474 
    475         public function test_wp_site_query_by_deleted() {
    476             $q     = new WP_Site_Query();
    477             $found = $q->query(
    478                 array(
    479                     'fields'       => 'ids',
    480                     // Exclude main site since we don't have control over it here.
    481                     'site__not_in' => array( 1 ),
    482                     'deleted'      => '0',
    483                 )
    484             );
    485 
    486             $this->assertSameSets( array_values( self::$site_ids ), $found );
    487         }
    488 
    489         public function test_wp_site_query_by_deleted_with_no_results() {
    490             $q     = new WP_Site_Query();
    491             $found = $q->query(
    492                 array(
    493                     'fields'  => 'ids',
    494                     'deleted' => '1',
    495                 )
    496             );
    497 
    498             $this->assertEmpty( $found );
    499         }
    500 
    501         public function test_wp_site_query_by_public() {
    502             $q     = new WP_Site_Query();
    503             $found = $q->query(
    504                 array(
    505                     'fields'       => 'ids',
    506                     // Exclude main site since we don't have control over it here.
    507                     'site__not_in' => array( 1 ),
    508                     'public'       => '1',
    509                 )
    510             );
    511 
    512             $this->assertSameSets( array_values( self::$site_ids ), $found );
    513         }
    514 
    515         public function test_wp_site_query_by_lang_id_with_zero() {
    516             $q     = new WP_Site_Query();
    517             $found = $q->query(
    518                 array(
    519                     'fields'       => 'ids',
    520                     // Exclude main site since we don't have control over it here.
    521                     'site__not_in' => array( 1 ),
    522                     'lang_id'      => 0,
    523                 )
    524             );
    525 
    526             $this->assertSameSets( array_diff( array_values( self::$site_ids ), array( self::$site_ids['www.w.org/make/'] ) ), $found );
    527         }
    528 
    529         public function test_wp_site_query_by_lang_id() {
    530             $q     = new WP_Site_Query();
    531             $found = $q->query(
    532                 array(
    533                     'fields'  => 'ids',
    534                     'lang_id' => 1,
    535                 )
    536             );
    537 
    538             $expected = array(
    539                 self::$site_ids['www.w.org/make/'],
    540             );
    541 
    542             $this->assertSameSets( $expected, $found );
    543         }
    544 
    545         public function test_wp_site_query_by_lang_id_with_no_results() {
    546             $q     = new WP_Site_Query();
    547             $found = $q->query(
    548                 array(
    549                     'fields'  => 'ids',
    550                     'lang_id' => 2,
    551                 )
    552             );
    553 
    554             $this->assertEmpty( $found );
    555         }
    556 
    557         public function test_wp_site_query_by_lang__in() {
    558             $q     = new WP_Site_Query();
    559             $found = $q->query(
    560                 array(
    561                     'fields'   => 'ids',
    562                     'lang__in' => array( 1 ),
    563                 )
    564             );
    565 
    566             $expected = array(
    567                 self::$site_ids['www.w.org/make/'],
    568             );
    569 
    570             $this->assertSameSets( $expected, $found );
    571         }
    572 
    573         public function test_wp_site_query_by_lang__in_with_multiple_ids() {
    574             $q     = new WP_Site_Query();
    575             $found = $q->query(
    576                 array(
    577                     'fields'       => 'ids',
    578                     // Exclude main site since we don't have control over it here.
    579                     'site__not_in' => array( 1 ),
    580                     'lang__in'     => array( 0, 1 ),
    581                 )
    582             );
    583 
    584             $this->assertSameSets( array_values( self::$site_ids ), $found );
    585         }
    586 
    587         public function test_wp_site_query_by_lang__not_in() {
    588             $q     = new WP_Site_Query();
    589             $found = $q->query(
    590                 array(
    591                     'fields'       => 'ids',
    592                     'lang__not_in' => array( 0 ),
    593                 )
    594             );
    595 
    596             $expected = array(
    597                 self::$site_ids['www.w.org/make/'],
    598             );
    599 
    600             $this->assertSameSets( $expected, $found );
    601         }
    602 
    603         public function test_wp_site_query_by_lang__not_in_with_multiple_ids() {
    604             $q     = new WP_Site_Query();
    605             $found = $q->query(
    606                 array(
    607                     'fields'       => 'ids',
    608                     'lang__not_in' => array( 0, 1 ),
    609                 )
    610             );
    611 
    612             $this->assertEmpty( $found );
    613         }
    614 
    615         public function test_wp_site_query_by_search_with_text_in_domain() {
    616             $q     = new WP_Site_Query();
    617             $found = $q->query(
    618                 array(
    619                     'fields' => 'ids',
    620                     'search' => 'ke.wordp',
    621                 )
    622             );
    623 
    624             $expected = array(
    625                 self::$site_ids['make.wordpress.org/'],
    626                 self::$site_ids['make.wordpress.org/foo/'],
    627             );
    628 
    629             $this->assertSameSets( $expected, $found );
    630         }
    631 
    632         public function test_wp_site_query_by_search_with_text_in_path() {
    633             $q     = new WP_Site_Query();
    634             $found = $q->query(
    635                 array(
    636                     'fields' => 'ids',
    637                     'search' => 'foo',
    638                 )
    639             );
    640 
    641             $expected = array(
    642                 self::$site_ids['wordpress.org/foo/'],
    643                 self::$site_ids['wordpress.org/foo/bar/'],
    644                 self::$site_ids['make.wordpress.org/foo/'],
    645                 self::$site_ids['www.w.org/foo/'],
    646                 self::$site_ids['www.w.org/foo/bar/'],
    647             );
    648 
    649             $this->assertSameSets( $expected, $found );
    650         }
    651 
    652         public function test_wp_site_query_by_search_with_text_in_path_and_domain() {
    653             $q     = new WP_Site_Query();
    654             $found = $q->query(
    655                 array(
    656                     'fields' => 'ids',
    657                     'search' => 'make',
    658                 )
    659             );
    660 
    661             $expected = array(
    662                 self::$site_ids['make.wordpress.org/'],
    663                 self::$site_ids['make.wordpress.org/foo/'],
    664                 self::$site_ids['www.w.org/make/'],
    665             );
    666 
    667             $this->assertSameSets( $expected, $found );
    668         }
    669 
    670         public function test_wp_site_query_by_search_with_text_in_path_and_domain_order_by_domain_desc() {
    671             $q     = new WP_Site_Query();
    672             $found = $q->query(
    673                 array(
    674                     'fields'  => 'ids',
    675                     'search'  => 'make',
    676                     'order'   => 'DESC',
    677                     'orderby' => 'domain',
    678                 )
    679             );
    680 
    681             $expected = array(
    682                 self::$site_ids['www.w.org/make/'],
    683                 self::$site_ids['make.wordpress.org/'],
    684                 self::$site_ids['make.wordpress.org/foo/'],
    685             );
    686 
    687             $this->assertSame( $expected, $found );
    688         }
    689 
    690         public function test_wp_site_query_by_search_with_text_in_path_exclude_domain_from_search() {
    691             $q     = new WP_Site_Query();
    692             $found = $q->query(
    693                 array(
    694                     'fields'         => 'ids',
    695                     'search'         => 'make',
    696                     'search_columns' => array( 'path' ),
    697                 )
    698             );
    699 
    700             $expected = array(
    701                 self::$site_ids['www.w.org/make/'],
    702             );
    703 
    704             $this->assertSame( $expected, $found );
    705         }
    706 
    707         public function test_wp_site_query_by_search_with_text_in_domain_exclude_path_from_search() {
    708             $q     = new WP_Site_Query();
    709             $found = $q->query(
    710                 array(
    711                     'fields'         => 'ids',
    712                     'search'         => 'make',
    713                     'search_columns' => array( 'domain' ),
    714                 )
    715             );
    716 
    717             $expected = array(
    718                 self::$site_ids['make.wordpress.org/'],
    719                 self::$site_ids['make.wordpress.org/foo/'],
    720             );
    721 
    722             $this->assertSame( $expected, $found );
    723         }
    724 
    725         public function test_wp_site_query_by_search_with_wildcard_in_text() {
    726             $q     = new WP_Site_Query();
    727             $found = $q->query(
    728                 array(
    729                     'fields' => 'ids',
    730                     'search' => 'm*ke',
    731                 )
    732             );
    733 
    734             $expected = array(
    735                 self::$site_ids['www.w.org/make/'],
    736                 self::$site_ids['make.wordpress.org/'],
    737                 self::$site_ids['make.wordpress.org/foo/'],
    738             );
    739 
    740             $this->assertSameSets( $expected, $found );
    741         }
    742 
    743         public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_path_from_search() {
    744             $q     = new WP_Site_Query();
    745             $found = $q->query(
    746                 array(
    747                     'fields'         => 'ids',
    748                     'search'         => 'm*ke',
    749                     'search_columns' => array( 'domain' ),
    750                 )
    751             );
    752 
    753             $expected = array(
    754                 self::$site_ids['make.wordpress.org/'],
    755                 self::$site_ids['make.wordpress.org/foo/'],
    756             );
    757 
    758             $this->assertSameSets( $expected, $found );
    759         }
    760 
    761         public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_domain_from_search() {
    762             $q     = new WP_Site_Query();
    763             $found = $q->query(
    764                 array(
    765                     'fields'         => 'ids',
    766                     'search'         => 'm*ke',
    767                     'search_columns' => array( 'path' ),
    768                 )
    769             );
    770 
    771             $expected = array(
    772                 self::$site_ids['www.w.org/make/'],
    773             );
    774 
    775             $this->assertSameSets( $expected, $found );
    776         }
    777 
    778         /**
    779          * @ticket 41197
    780          */
    781         public function test_wp_site_query_cache_with_different_fields_no_count() {
    782             $q                 = new WP_Site_Query();
    783             $query_1           = $q->query(
    784                 array(
    785                     'fields'     => 'all',
    786                     'network_id' => self::$network_ids['wordpress.org/'],
    787                     'number'     => 3,
    788                     'order'      => 'ASC',
    789                 )
    790             );
    791             $number_of_queries = get_num_queries();
    792 
    793             $query_2 = $q->query(
    794                 array(
    795                     'fields'     => 'ids',
    796                     'network_id' => self::$network_ids['wordpress.org/'],
    797                     'number'     => 3,
    798                     'order'      => 'ASC',
    799                 )
    800             );
    801 
    802             $this->assertSame( $number_of_queries, get_num_queries() );
    803         }
    804 
    805         /**
    806          * @ticket 41197
    807          */
    808         public function test_wp_site_query_cache_with_different_fields_active_count() {
    809             $q = new WP_Site_Query();
    810 
    811             $query_1           = $q->query(
    812                 array(
    813                     'fields'     => 'all',
    814                     'network_id' => self::$network_ids['wordpress.org/'],
    815                     'number'     => 3,
    816                     'order'      => 'ASC',
    817                     'count'      => true,
    818                 )
    819             );
    820             $number_of_queries = get_num_queries();
    821 
    822             $query_2 = $q->query(
    823                 array(
    824                     'fields'     => 'ids',
    825                     'network_id' => self::$network_ids['wordpress.org/'],
    826                     'number'     => 3,
    827                     'order'      => 'ASC',
    828                     'count'      => true,
    829                 )
    830             );
    831             $this->assertSame( $number_of_queries, get_num_queries() );
    832         }
    833 
    834         /**
    835          * @ticket 41197
    836          */
    837         public function test_wp_site_query_cache_with_same_fields_different_count() {
    838             $q = new WP_Site_Query();
    839 
    840             $query_1 = $q->query(
    841                 array(
    842                     'fields'     => 'ids',
    843                     'network_id' => self::$network_ids['wordpress.org/'],
    844                     'number'     => 3,
    845                     'order'      => 'ASC',
    846                 )
    847             );
    848 
    849             $number_of_queries = get_num_queries();
    850 
    851             $query_2 = $q->query(
    852                 array(
    853                     'fields'     => 'ids',
    854                     'network_id' => self::$network_ids['wordpress.org/'],
    855                     'number'     => 3,
    856                     'order'      => 'ASC',
    857                     'count'      => true,
    858                 )
    859             );
    860             $this->assertSame( $number_of_queries + 1, get_num_queries() );
    861         }
    862 
    863         /**
    864          * @ticket 55462
    865          */
    866         public function test_wp_site_query_cache_with_same_fields_same_cache_fields() {
    867             $q = new WP_Site_Query();
    868 
    869             $query_1 = $q->query(
    870                 array(
    871                     'fields'                 => 'ids',
    872                     'network_id'             => self::$network_ids['wordpress.org/'],
    873                     'number'                 => 3,
    874                     'order'                  => 'ASC',
    875                     'update_site_cache'      => true,
    876                     'update_site_meta_cache' => true,
    877                 )
    878             );
    879 
    880             $number_of_queries = get_num_queries();
    881 
    882             $query_2 = $q->query(
    883                 array(
    884                     'fields'                 => 'ids',
    885                     'network_id'             => self::$network_ids['wordpress.org/'],
    886                     'number'                 => 3,
    887                     'order'                  => 'ASC',
    888                     'update_site_cache'      => true,
    889                     'update_site_meta_cache' => true,
    890                 )
    891             );
    892             $this->assertSame( $number_of_queries, get_num_queries() );
    893         }
    894 
    895         /**
    896          * @ticket 55462
    897          */
    898         public function test_wp_site_query_cache_with_same_fields_different_cache_fields() {
    899             $q = new WP_Site_Query();
    900 
    901             $query_1 = $q->query(
    902                 array(
    903                     'fields'                 => 'ids',
    904                     'network_id'             => self::$network_ids['wordpress.org/'],
    905                     'number'                 => 3,
    906                     'order'                  => 'ASC',
    907                     'update_site_cache'      => true,
    908                     'update_site_meta_cache' => true,
    909                 )
    910             );
    911 
    912             $number_of_queries = get_num_queries();
    913 
    914             $query_2 = $q->query(
    915                 array(
    916                     'fields'                 => 'ids',
    917                     'network_id'             => self::$network_ids['wordpress.org/'],
    918                     'number'                 => 3,
    919                     'order'                  => 'ASC',
    920                     'update_site_cache'      => false,
    921                     'update_site_meta_cache' => false,
    922                 )
    923             );
    924             $this->assertSame( $number_of_queries, get_num_queries() );
    925         }
    926 
    927         /**
    928          * @ticket 40229
    929          * @dataProvider data_wp_site_query_meta_query
    930          */
    931         public function test_wp_site_query_meta_query( $query, $expected, $strict ) {
    932             if ( ! is_site_meta_supported() ) {
    933                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    934             }
    935 
    936             add_site_meta( self::$site_ids['wordpress.org/'], 'foo', 'foo' );
    937             add_site_meta( self::$site_ids['wordpress.org/foo/'], 'foo', 'bar' );
    938             add_site_meta( self::$site_ids['wordpress.org/foo/bar/'], 'foo', 'baz' );
    939             add_site_meta( self::$site_ids['make.wordpress.org/'], 'bar', 'baz' );
    940             add_site_meta( self::$site_ids['wordpress.org/'], 'numberfoo', 1 );
    941             add_site_meta( self::$site_ids['wordpress.org/foo/'], 'numberfoo', 2 );
    942 
    943             $query['fields'] = 'ids';
    944 
    945             $q     = new WP_Site_Query();
    946             $found = $q->query( $query );
    947 
    948             foreach ( $expected as $index => $domain_path ) {
    949                 $expected[ $index ] = self::$site_ids[ $domain_path ];
    950             }
    951 
    952             if ( $strict ) {
    953                 $this->assertSame( $expected, $found );
    954             } else {
    955                 $this->assertSameSets( $expected, $found );
    956             }
    957         }
    958 
    959         public function data_wp_site_query_meta_query() {
    960             return array(
    961                 array(
    962                     array(
    963                         'meta_key' => 'foo',
    964                     ),
    965                     array(
    966                         'wordpress.org/',
    967                         'wordpress.org/foo/',
    968                         'wordpress.org/foo/bar/',
    969                     ),
    970                     false,
    971                 ),
    972                 array(
    973                     array(
    974                         'meta_key'   => 'foo',
    975                         'meta_value' => 'bar',
    976                     ),
    977                     array(
    978                         'wordpress.org/foo/',
    979                     ),
    980                     false,
    981                 ),
    982                 array(
    983                     array(
    984                         'meta_key'     => 'foo',
    985                         'meta_value'   => array( 'bar', 'baz' ),
    986                         'meta_compare' => 'IN',
    987                     ),
    988                     array(
    989                         'wordpress.org/foo/',
    990                         'wordpress.org/foo/bar/',
    991                     ),
    992                     false,
    993                 ),
    994                 array(
    995                     array(
    996                         'meta_query' => array(
    997                             array(
    998                                 'key'   => 'foo',
    999                                 'value' => 'bar',
    1000                             ),
    1001                             array(
    1002                                 'key'   => 'numberfoo',
    1003                                 'value' => 2,
    1004                                 'type'  => 'NUMERIC',
    1005                             ),
     956    }
     957
     958    public function data_wp_site_query_meta_query() {
     959        return array(
     960            array(
     961                array(
     962                    'meta_key' => 'foo',
     963                ),
     964                array(
     965                    'wordpress.org/',
     966                    'wordpress.org/foo/',
     967                    'wordpress.org/foo/bar/',
     968                ),
     969                false,
     970            ),
     971            array(
     972                array(
     973                    'meta_key'   => 'foo',
     974                    'meta_value' => 'bar',
     975                ),
     976                array(
     977                    'wordpress.org/foo/',
     978                ),
     979                false,
     980            ),
     981            array(
     982                array(
     983                    'meta_key'     => 'foo',
     984                    'meta_value'   => array( 'bar', 'baz' ),
     985                    'meta_compare' => 'IN',
     986                ),
     987                array(
     988                    'wordpress.org/foo/',
     989                    'wordpress.org/foo/bar/',
     990                ),
     991                false,
     992            ),
     993            array(
     994                array(
     995                    'meta_query' => array(
     996                        array(
     997                            'key'   => 'foo',
     998                            'value' => 'bar',
     999                        ),
     1000                        array(
     1001                            'key'   => 'numberfoo',
     1002                            'value' => 2,
     1003                            'type'  => 'NUMERIC',
    10061004                        ),
    10071005                    ),
    1008                     array(
    1009                         'wordpress.org/foo/',
     1006                ),
     1007                array(
     1008                    'wordpress.org/foo/',
     1009                ),
     1010                false,
     1011            ),
     1012            array(
     1013                array(
     1014                    'meta_key' => 'foo',
     1015                    'orderby'  => 'meta_value',
     1016                    'order'    => 'ASC',
     1017                ),
     1018                array(
     1019                    'wordpress.org/foo/',
     1020                    'wordpress.org/foo/bar/',
     1021                    'wordpress.org/',
     1022                ),
     1023                true,
     1024            ),
     1025            array(
     1026                array(
     1027                    'meta_key' => 'foo',
     1028                    'orderby'  => 'foo',
     1029                    'order'    => 'ASC',
     1030                ),
     1031                array(
     1032                    'wordpress.org/foo/',
     1033                    'wordpress.org/foo/bar/',
     1034                    'wordpress.org/',
     1035                ),
     1036                true,
     1037            ),
     1038            array(
     1039                array(
     1040                    'meta_key' => 'numberfoo',
     1041                    'orderby'  => 'meta_value_num',
     1042                    'order'    => 'DESC',
     1043                ),
     1044                array(
     1045                    'wordpress.org/foo/',
     1046                    'wordpress.org/',
     1047                ),
     1048                true,
     1049            ),
     1050            array(
     1051                array(
     1052                    'meta_query' => array(
     1053                        array(
     1054                            'key'     => 'foo',
     1055                            'value'   => array( 'foo', 'bar' ),
     1056                            'compare' => 'IN',
     1057                        ),
     1058                        array(
     1059                            'key' => 'numberfoo',
     1060                        ),
    10101061                    ),
    1011                     false,
    1012                 ),
    1013                 array(
    1014                     array(
    1015                         'meta_key' => 'foo',
    1016                         'orderby'  => 'meta_value',
    1017                         'order'    => 'ASC',
     1062                    'orderby'    => array( 'meta_value' => 'ASC' ),
     1063                ),
     1064                array(
     1065                    'wordpress.org/foo/',
     1066                    'wordpress.org/',
     1067                ),
     1068                true,
     1069            ),
     1070            array(
     1071                array(
     1072                    'meta_query' => array(
     1073                        array(
     1074                            'key'     => 'foo',
     1075                            'value'   => array( 'foo', 'bar' ),
     1076                            'compare' => 'IN',
     1077                        ),
     1078                        array(
     1079                            'key' => 'numberfoo',
     1080                        ),
    10181081                    ),
    1019                     array(
    1020                         'wordpress.org/foo/',
    1021                         'wordpress.org/foo/bar/',
    1022                         'wordpress.org/',
     1082                    'orderby'    => array( 'foo' => 'ASC' ),
     1083                ),
     1084                array(
     1085                    'wordpress.org/foo/',
     1086                    'wordpress.org/',
     1087                ),
     1088                true,
     1089            ),
     1090            array(
     1091                array(
     1092                    'meta_query' => array(
     1093                        array(
     1094                            'key'     => 'foo',
     1095                            'value'   => array( 'foo', 'bar' ),
     1096                            'compare' => 'IN',
     1097                        ),
     1098                        'my_subquery' => array(
     1099                            'key' => 'numberfoo',
     1100                        ),
    10231101                    ),
    1024                     true,
    1025                 ),
    1026                 array(
    1027                     array(
    1028                         'meta_key' => 'foo',
    1029                         'orderby'  => 'foo',
    1030                         'order'    => 'ASC',
    1031                     ),
    1032                     array(
    1033                         'wordpress.org/foo/',
    1034                         'wordpress.org/foo/bar/',
    1035                         'wordpress.org/',
    1036                     ),
    1037                     true,
    1038                 ),
    1039                 array(
    1040                     array(
    1041                         'meta_key' => 'numberfoo',
    1042                         'orderby'  => 'meta_value_num',
    1043                         'order'    => 'DESC',
    1044                     ),
    1045                     array(
    1046                         'wordpress.org/foo/',
    1047                         'wordpress.org/',
    1048                     ),
    1049                     true,
    1050                 ),
    1051                 array(
    1052                     array(
    1053                         'meta_query' => array(
    1054                             array(
    1055                                 'key'     => 'foo',
    1056                                 'value'   => array( 'foo', 'bar' ),
    1057                                 'compare' => 'IN',
    1058                             ),
    1059                             array(
    1060                                 'key' => 'numberfoo',
    1061                             ),
    1062                         ),
    1063                         'orderby'    => array( 'meta_value' => 'ASC' ),
    1064                     ),
    1065                     array(
    1066                         'wordpress.org/foo/',
    1067                         'wordpress.org/',
    1068                     ),
    1069                     true,
    1070                 ),
    1071                 array(
    1072                     array(
    1073                         'meta_query' => array(
    1074                             array(
    1075                                 'key'     => 'foo',
    1076                                 'value'   => array( 'foo', 'bar' ),
    1077                                 'compare' => 'IN',
    1078                             ),
    1079                             array(
    1080                                 'key' => 'numberfoo',
    1081                             ),
    1082                         ),
    1083                         'orderby'    => array( 'foo' => 'ASC' ),
    1084                     ),
    1085                     array(
    1086                         'wordpress.org/foo/',
    1087                         'wordpress.org/',
    1088                     ),
    1089                     true,
    1090                 ),
    1091                 array(
    1092                     array(
    1093                         'meta_query' => array(
    1094                             array(
    1095                                 'key'     => 'foo',
    1096                                 'value'   => array( 'foo', 'bar' ),
    1097                                 'compare' => 'IN',
    1098                             ),
    1099                             'my_subquery' => array(
    1100                                 'key' => 'numberfoo',
    1101                             ),
    1102                         ),
    1103                         'orderby'    => array( 'my_subquery' => 'DESC' ),
    1104                     ),
    1105                     array(
    1106                         'wordpress.org/foo/',
    1107                         'wordpress.org/',
    1108                     ),
    1109                     true,
    1110                 ),
    1111             );
    1112         }
    1113 
    1114         /**
    1115          * @ticket 45749
    1116          * @ticket 47599
    1117          */
    1118         public function test_sites_pre_query_filter_should_bypass_database_query() {
    1119             add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
    1120 
    1121             $num_queries = get_num_queries();
    1122 
    1123             $q       = new WP_Site_Query();
    1124             $results = $q->query( array() );
    1125 
    1126             remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
    1127 
    1128             // Make sure no queries were executed.
    1129             $this->assertSame( $num_queries, get_num_queries() );
    1130 
    1131             // We manually inserted a non-existing site and overrode the results with it.
    1132             $this->assertSame( array( 555 ), $results );
    1133 
    1134             // Make sure manually setting found_sites doesn't get overwritten.
    1135             $this->assertSame( 1, $q->found_sites );
    1136         }
    1137 
    1138         public static function filter_sites_pre_query( $sites, $query ) {
    1139             $query->found_sites = 1;
    1140 
    1141             return array( 555 );
    1142         }
    1143 
    1144         /**
    1145          * @ticket 51333
    1146          */
    1147         public function test_sites_pre_query_filter_should_set_sites_property() {
    1148             add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10, 2 );
    1149 
    1150             $q       = new WP_Site_Query();
    1151             $results = $q->query( array() );
    1152 
    1153             remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10 );
    1154 
    1155             // Make sure the sites property is the same as the results.
    1156             $this->assertSame( $results, $q->sites );
    1157 
    1158             // Make sure the site domain is `wordpress.org`.
    1159             $this->assertSame( 'wordpress.org', $q->sites[0]->domain );
    1160         }
    1161 
    1162         public static function filter_sites_pre_query_and_set_sites( $sites, $query ) {
    1163             return array( get_site( self::$site_ids['wordpress.org/'] ) );
    1164         }
    1165 
    1166         /**
    1167          * @ticket 56841
    1168          */
    1169         public function test_wp_site_query_does_not_have_leading_whitespace() {
    1170             $q = new WP_Site_Query();
    1171 
    1172             $q->query(
    1173                 array(
    1174                     'fields'                 => 'ids',
    1175                     'network_id'             => self::$network_ids['wordpress.org/'],
    1176                     'number'                 => 3,
    1177                     'order'                  => 'ASC',
    1178                     'update_site_cache'      => true,
    1179                     'update_site_meta_cache' => true,
    1180                 )
    1181             );
    1182 
    1183             $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
    1184         }
    1185     }
    1186 
    1187 endif;
     1102                    'orderby'    => array( 'my_subquery' => 'DESC' ),
     1103                ),
     1104                array(
     1105                    'wordpress.org/foo/',
     1106                    'wordpress.org/',
     1107                ),
     1108                true,
     1109            ),
     1110        );
     1111    }
     1112
     1113    /**
     1114     * @ticket 45749
     1115     * @ticket 47599
     1116     */
     1117    public function test_sites_pre_query_filter_should_bypass_database_query() {
     1118        add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
     1119
     1120        $num_queries = get_num_queries();
     1121
     1122        $q       = new WP_Site_Query();
     1123        $results = $q->query( array() );
     1124
     1125        remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
     1126
     1127        // Make sure no queries were executed.
     1128        $this->assertSame( $num_queries, get_num_queries() );
     1129
     1130        // We manually inserted a non-existing site and overrode the results with it.
     1131        $this->assertSame( array( 555 ), $results );
     1132
     1133        // Make sure manually setting found_sites doesn't get overwritten.
     1134        $this->assertSame( 1, $q->found_sites );
     1135    }
     1136
     1137    public static function filter_sites_pre_query( $sites, $query ) {
     1138        $query->found_sites = 1;
     1139
     1140        return array( 555 );
     1141    }
     1142
     1143    /**
     1144     * @ticket 51333
     1145     */
     1146    public function test_sites_pre_query_filter_should_set_sites_property() {
     1147        add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10, 2 );
     1148
     1149        $q       = new WP_Site_Query();
     1150        $results = $q->query( array() );
     1151
     1152        remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10 );
     1153
     1154        // Make sure the sites property is the same as the results.
     1155        $this->assertSame( $results, $q->sites );
     1156
     1157        // Make sure the site domain is `wordpress.org`.
     1158        $this->assertSame( 'wordpress.org', $q->sites[0]->domain );
     1159    }
     1160
     1161    public static function filter_sites_pre_query_and_set_sites( $sites, $query ) {
     1162        return array( get_site( self::$site_ids['wordpress.org/'] ) );
     1163    }
     1164
     1165    /**
     1166     * @ticket 56841
     1167     */
     1168    public function test_wp_site_query_does_not_have_leading_whitespace() {
     1169        $q = new WP_Site_Query();
     1170
     1171        $q->query(
     1172            array(
     1173                'fields'                 => 'ids',
     1174                'network_id'             => self::$network_ids['wordpress.org/'],
     1175                'number'                 => 3,
     1176                'order'                  => 'ASC',
     1177                'update_site_cache'      => true,
     1178                'update_site_meta_cache' => true,
     1179            )
     1180        );
     1181
     1182        $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     1183    }
     1184}
  • trunk/tests/phpunit/tests/multisite/wpmuLogNewRegistrations.php

    r54726 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group multisite
     6 */
     7class Tests_Multisite_wpmuLogNewRegistrations extends WP_UnitTestCase {
    48
    5     /**
    6      * @group multisite
    7      */
    8     class Tests_Multisite_wpmuLogNewRegistrations extends WP_UnitTestCase {
     9    public function test_wpmu_log_new_registrations() {
     10        global $wpdb;
    911
    10         public function test_wpmu_log_new_registrations() {
    11             global $wpdb;
     12        $user = new WP_User( 1 );
     13        $ip   = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
    1214
    13             $user = new WP_User( 1 );
    14             $ip   = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
     15        wpmu_log_new_registrations( 1, 1 );
    1516
    16             wpmu_log_new_registrations( 1, 1 );
    17 
    18             // Currently there is no wrapper function for the registration_log.
    19             $reg_blog = $wpdb->get_col( $wpdb->prepare( "SELECT email FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.blog_id = 1 AND IP LIKE %s", $ip ) );
    20             $this->assertSame( $user->user_email, $reg_blog[ count( $reg_blog ) - 1 ] );
    21         }
     17        // Currently there is no wrapper function for the registration_log.
     18        $reg_blog = $wpdb->get_col( $wpdb->prepare( "SELECT email FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.blog_id = 1 AND IP LIKE %s", $ip ) );
     19        $this->assertSame( $user->user_email, $reg_blog[ count( $reg_blog ) - 1 ] );
    2220    }
    23 
    24 endif;
     21}
  • trunk/tests/phpunit/tests/multisite/wpmuValidateBlogSignup.php

    r51860 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group multisite
     6 */
     7class Tests_Multisite_wpmuValidateBlogSignup extends WP_UnitTestCase {
     8
     9    protected static $super_admin_id;
     10
     11    protected static $existing_user_login = 'existinguserfoo';
     12    protected static $existing_user_id;
     13
     14    protected static $existing_blog_name = 'existingsitefoo';
     15    protected static $existing_blog_id;
     16
     17    protected $minimum_site_name_length = 4;
     18
     19    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     20        self::$super_admin_id = $factory->user->create();
     21        grant_super_admin( self::$super_admin_id );
     22
     23        self::$existing_user_id = $factory->user->create( array( 'user_login' => self::$existing_user_login ) );
     24
     25        $network = get_network();
     26
     27        if ( is_subdomain_install() ) {
     28            $domain = self::$existing_blog_name . '.' . preg_replace( '|^www\.|', '', $network->domain );
     29            $path   = $network->path;
     30        } else {
     31            $domain = $network->domain;
     32            $path   = $network->path . self::$existing_blog_name . '/';
     33        }
     34
     35        self::$existing_blog_id = $factory->blog->create(
     36            array(
     37                'domain'     => $domain,
     38                'path'       => $path,
     39                'network_id' => $network->id,
     40            )
     41        );
     42    }
     43
     44    public static function wpTearDownAfterClass() {
     45        revoke_super_admin( self::$super_admin_id );
     46        wpmu_delete_user( self::$super_admin_id );
     47
     48        wpmu_delete_user( self::$existing_user_id );
     49
     50        wp_delete_site( self::$existing_blog_id );
     51    }
    452
    553    /**
    6      * @group multisite
     54     * @dataProvider data_validate_blogname
    755     */
    8     class Tests_Multisite_wpmuValidateBlogSignup extends WP_UnitTestCase {
    9         protected static $super_admin_id;
     56    public function test_validate_blogname( $blog_name, $error_message ) {
     57        $result = wpmu_validate_blog_signup( $blog_name, 'Foo Site Title', get_userdata( self::$super_admin_id ) );
     58        $this->assertContains( 'blogname', $result['errors']->get_error_codes(), $error_message );
     59    }
    1060
    11         protected static $existing_user_login = 'existinguserfoo';
    12         protected static $existing_user_id;
     61    public function data_validate_blogname() {
     62        $data = array(
     63            array( '', 'Site names must not be empty.' ),
     64            array( 'foo-hello', 'Site names must not contain hyphens.' ),
     65            array( 'foo_hello', 'Site names must not contain underscores.' ),
     66            array( 'foo hello', 'Site names must not contain spaces.' ),
     67            array( 'FooHello', 'Site names must not contain uppercase letters.' ),
     68            array( '12345678', 'Site names must not consist of numbers only.' ),
     69            array( self::$existing_blog_name, 'Site names must not collide with an existing site name.' ),
     70            array( self::$existing_user_login, 'Site names must not collide with an existing user login.' ),
     71            array( 'foo', 'Site names must at least contain 4 characters.' ),
     72        );
    1373
    14         protected static $existing_blog_name = 'existingsitefoo';
    15         protected static $existing_blog_id;
    16 
    17         protected $minimum_site_name_length = 4;
    18 
    19         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    20             self::$super_admin_id = $factory->user->create();
    21             grant_super_admin( self::$super_admin_id );
    22 
    23             self::$existing_user_id = $factory->user->create( array( 'user_login' => self::$existing_user_login ) );
    24 
    25             $network = get_network();
    26 
    27             if ( is_subdomain_install() ) {
    28                 $domain = self::$existing_blog_name . '.' . preg_replace( '|^www\.|', '', $network->domain );
    29                 $path   = $network->path;
    30             } else {
    31                 $domain = $network->domain;
    32                 $path   = $network->path . self::$existing_blog_name . '/';
    33             }
    34 
    35             self::$existing_blog_id = $factory->blog->create(
    36                 array(
    37                     'domain'     => $domain,
    38                     'path'       => $path,
    39                     'network_id' => $network->id,
    40                 )
    41             );
     74        $illegal_names = get_site_option( 'illegal_names' );
     75        if ( ! empty( $illegal_names ) ) {
     76            $data[] = array( array_shift( $illegal_names ), 'Illegal site names are not allowed.' );
     77        } else {
     78            $data[] = array( 'www', 'Illegal site names are not allowed.' );
    4279        }
    4380
    44         public static function wpTearDownAfterClass() {
    45             revoke_super_admin( self::$super_admin_id );
    46             wpmu_delete_user( self::$super_admin_id );
     81        return $data;
     82    }
    4783
    48             wpmu_delete_user( self::$existing_user_id );
     84    public function test_validate_empty_blog_title() {
     85        $result = wpmu_validate_blog_signup( 'uniqueblogname1234', '', get_userdata( self::$super_admin_id ) );
     86        $this->assertContains( 'blog_title', $result['errors']->get_error_codes(), 'Site titles must not be empty.' );
     87    }
    4988
    50             wp_delete_site( self::$existing_blog_id );
    51         }
     89    public function test_validate_blogname_from_same_existing_user() {
     90        $result = wpmu_validate_blog_signup( self::$existing_user_login, 'Foo Site Title', get_userdata( self::$existing_user_id ) );
     91        $this->assertEmpty( $result['errors']->get_error_codes() );
     92    }
    5293
    53         /**
    54          * @dataProvider data_validate_blogname
    55          */
    56         public function test_validate_blogname( $blog_name, $error_message ) {
    57             $result = wpmu_validate_blog_signup( $blog_name, 'Foo Site Title', get_userdata( self::$super_admin_id ) );
    58             $this->assertContains( 'blogname', $result['errors']->get_error_codes(), $error_message );
    59         }
     94    /**
     95     * @ticket 39676
     96     *
     97     * @dataProvider data_filter_minimum_site_name_length
     98     */
     99    public function test_filter_minimum_site_name_length( $site_name, $minimum_length, $expect_error ) {
     100        $this->minimum_site_name_length = $minimum_length;
     101        add_filter( 'minimum_site_name_length', array( $this, 'filter_minimum_site_name_length' ) );
    60102
    61         public function data_validate_blogname() {
    62             $data = array(
    63                 array( '', 'Site names must not be empty.' ),
    64                 array( 'foo-hello', 'Site names must not contain hyphens.' ),
    65                 array( 'foo_hello', 'Site names must not contain underscores.' ),
    66                 array( 'foo hello', 'Site names must not contain spaces.' ),
    67                 array( 'FooHello', 'Site names must not contain uppercase letters.' ),
    68                 array( '12345678', 'Site names must not consist of numbers only.' ),
    69                 array( self::$existing_blog_name, 'Site names must not collide with an existing site name.' ),
    70                 array( self::$existing_user_login, 'Site names must not collide with an existing user login.' ),
    71                 array( 'foo', 'Site names must at least contain 4 characters.' ),
    72             );
     103        $result = wpmu_validate_blog_signup( $site_name, 'Site Title', get_userdata( self::$super_admin_id ) );
    73104
    74             $illegal_names = get_site_option( 'illegal_names' );
    75             if ( ! empty( $illegal_names ) ) {
    76                 $data[] = array( array_shift( $illegal_names ), 'Illegal site names are not allowed.' );
    77             } else {
    78                 $data[] = array( 'www', 'Illegal site names are not allowed.' );
    79             }
     105        remove_filter( 'minimum_site_name_length', array( $this, 'filter_minimum_site_name_length' ) );
     106        $this->minimum_site_name_length = 4;
    80107
    81             return $data;
    82         }
    83 
    84         public function test_validate_empty_blog_title() {
    85             $result = wpmu_validate_blog_signup( 'uniqueblogname1234', '', get_userdata( self::$super_admin_id ) );
    86             $this->assertContains( 'blog_title', $result['errors']->get_error_codes(), 'Site titles must not be empty.' );
    87         }
    88 
    89         public function test_validate_blogname_from_same_existing_user() {
    90             $result = wpmu_validate_blog_signup( self::$existing_user_login, 'Foo Site Title', get_userdata( self::$existing_user_id ) );
     108        if ( $expect_error ) {
     109            $this->assertContains( 'blogname', $result['errors']->get_error_codes() );
     110        } else {
    91111            $this->assertEmpty( $result['errors']->get_error_codes() );
    92         }
    93 
    94         /**
    95          * @ticket 39676
    96          *
    97          * @dataProvider data_filter_minimum_site_name_length
    98          */
    99         public function test_filter_minimum_site_name_length( $site_name, $minimum_length, $expect_error ) {
    100             $this->minimum_site_name_length = $minimum_length;
    101             add_filter( 'minimum_site_name_length', array( $this, 'filter_minimum_site_name_length' ) );
    102 
    103             $result = wpmu_validate_blog_signup( $site_name, 'Site Title', get_userdata( self::$super_admin_id ) );
    104 
    105             remove_filter( 'minimum_site_name_length', array( $this, 'filter_minimum_site_name_length' ) );
    106             $this->minimum_site_name_length = 4;
    107 
    108             if ( $expect_error ) {
    109                 $this->assertContains( 'blogname', $result['errors']->get_error_codes() );
    110             } else {
    111                 $this->assertEmpty( $result['errors']->get_error_codes() );
    112             }
    113         }
    114 
    115         public function data_filter_minimum_site_name_length() {
    116             return array(
    117                 array( 'fooo', 5, true ),
    118                 array( 'foooo', 5, false ),
    119                 array( 'foo', 4, true ),
    120                 array( 'fooo', 4, false ),
    121                 array( 'fo', 3, true ),
    122                 array( 'foo', 3, false ),
    123             );
    124         }
    125 
    126         public function filter_minimum_site_name_length() {
    127             return $this->minimum_site_name_length;
    128         }
    129 
    130         /**
    131          * @ticket 43667
    132          */
    133         public function test_signup_nonce_check() {
    134             $original_php_self       = $_SERVER['PHP_SELF'];
    135             $_SERVER['PHP_SELF']     = '/wp-signup.php';
    136             $_POST['signup_form_id'] = 'blog-signup-form';
    137             $_POST['_signup_form']   = wp_create_nonce( 'signup_form_' . $_POST['signup_form_id'] );
    138 
    139             $valid               = wpmu_validate_blog_signup( 'my-nonce-site', 'Site Title', get_userdata( self::$super_admin_id ) );
    140             $_SERVER['PHP_SELF'] = $original_php_self;
    141 
    142             $this->assertNotContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
    143         }
    144 
    145         /**
    146          * @ticket 43667
    147          */
    148         public function test_signup_nonce_check_invalid() {
    149             $original_php_self       = $_SERVER['PHP_SELF'];
    150             $_SERVER['PHP_SELF']     = '/wp-signup.php';
    151             $_POST['signup_form_id'] = 'blog-signup-form';
    152             $_POST['_signup_form']   = wp_create_nonce( 'invalid' );
    153 
    154             $valid               = wpmu_validate_blog_signup( 'my-nonce-site', 'Site Title', get_userdata( self::$super_admin_id ) );
    155             $_SERVER['PHP_SELF'] = $original_php_self;
    156 
    157             $this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
    158112        }
    159113    }
    160114
    161 endif;
     115    public function data_filter_minimum_site_name_length() {
     116        return array(
     117            array( 'fooo', 5, true ),
     118            array( 'foooo', 5, false ),
     119            array( 'foo', 4, true ),
     120            array( 'fooo', 4, false ),
     121            array( 'fo', 3, true ),
     122            array( 'foo', 3, false ),
     123        );
     124    }
     125
     126    public function filter_minimum_site_name_length() {
     127        return $this->minimum_site_name_length;
     128    }
     129
     130    /**
     131     * @ticket 43667
     132     */
     133    public function test_signup_nonce_check() {
     134        $original_php_self       = $_SERVER['PHP_SELF'];
     135        $_SERVER['PHP_SELF']     = '/wp-signup.php';
     136        $_POST['signup_form_id'] = 'blog-signup-form';
     137        $_POST['_signup_form']   = wp_create_nonce( 'signup_form_' . $_POST['signup_form_id'] );
     138
     139        $valid               = wpmu_validate_blog_signup( 'my-nonce-site', 'Site Title', get_userdata( self::$super_admin_id ) );
     140        $_SERVER['PHP_SELF'] = $original_php_self;
     141
     142        $this->assertNotContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
     143    }
     144
     145    /**
     146     * @ticket 43667
     147     */
     148    public function test_signup_nonce_check_invalid() {
     149        $original_php_self       = $_SERVER['PHP_SELF'];
     150        $_SERVER['PHP_SELF']     = '/wp-signup.php';
     151        $_POST['signup_form_id'] = 'blog-signup-form';
     152        $_POST['_signup_form']   = wp_create_nonce( 'invalid' );
     153
     154        $valid               = wpmu_validate_blog_signup( 'my-nonce-site', 'Site Title', get_userdata( self::$super_admin_id ) );
     155        $_SERVER['PHP_SELF'] = $original_php_self;
     156
     157        $this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
     158    }
     159}
  • trunk/tests/phpunit/tests/multisite/wpmuValidateUserSignup.php

    r59901 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    5     /**
    6      * @group multisite
    7      */
    8     class Tests_Multisite_wpmuValidateUserSignup extends WP_UnitTestCase {
    9         /**
    10          * @dataProvider data_user_name
    11          */
    12         public function test_user_name( $user_name, $error_message ) {
    13             $v = wpmu_validate_user_signup( $user_name, 'foo@example.com' );
    14             $this->assertContains( 'user_name', $v['errors']->get_error_codes(), $error_message );
    15         }
    16 
    17         public function data_user_name() {
    18             return array(
    19                 array( 'contains spaces', 'User names with spaces are not allowed.' ),
    20                 array( 'ContainsCaps', 'User names with capital letters are not allowed.' ),
    21                 array( 'contains_underscores', 'User names with underscores are not allowed.' ),
    22                 array( 'contains%^*()junk', 'User names with non-alphanumeric characters are not allowed.' ),
    23                 array( '', 'Empty user names are not allowed.' ),
    24                 array( 'foo', 'User names of 3 characters are not allowed.' ),
    25                 array( 'fo', 'User names of 2 characters are not allowed.' ),
    26                 array( 'f', 'User names of 1 characters are not allowed.' ),
    27                 array( 'f', 'User names of 1 characters are not allowed.' ),
    28                 array( '12345', 'User names consisting only of numbers are not allowed.' ),
    29                 array( 'thisusernamecontainsenoughcharacterstobelongerthan60characters', 'User names longer than 60 characters are not allowed.' ),
    30             );
    31         }
    32 
    33         public function test_should_fail_for_illegal_names() {
    34             $illegal = array( 'foo123', 'bar123' );
    35             update_site_option( 'illegal_names', $illegal );
    36 
    37             foreach ( $illegal as $i ) {
    38                 $v = wpmu_validate_user_signup( $i, 'foo@example.com' );
    39                 $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
    40             }
    41         }
    42 
    43         public function test_should_fail_for_unsafe_email_address() {
    44             add_filter( 'is_email_address_unsafe', '__return_true' );
    45             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
    46             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    47             remove_filter( 'is_email_address_unsafe', '__return_true' );
    48         }
    49 
    50         public function test_should_fail_for_invalid_email_address() {
    51             add_filter( 'is_email', '__return_false' );
    52             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
    53             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    54             remove_filter( 'is_email', '__return_false' );
    55         }
    56 
    57         public function test_should_fail_for_emails_from_disallowed_domains() {
    58             $domains = array( 'foo.com', 'bar.org' );
    59             update_site_option( 'limited_email_domains', $domains );
    60 
    61             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
    62             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    63         }
    64 
    65         public function test_should_not_fail_for_emails_from_allowed_domains_with_mixed_case() {
    66             $domains = array( 'foo.com', 'bar.org' );
    67             update_site_option( 'limited_email_domains', $domains );
    68 
    69             $v = wpmu_validate_user_signup( 'foo123', 'foo@BAR.org' );
    70             $this->assertNotContains( 'user_email', $v['errors']->get_error_codes() );
    71         }
    72 
    73         public function test_should_fail_for_existing_user_name() {
    74             $u = self::factory()->user->create( array( 'user_login' => 'foo123' ) );
    75             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     3/**
     4 * @group ms-required
     5 * @group multisite
     6 */
     7class Tests_Multisite_wpmuValidateUserSignup extends WP_UnitTestCase {
     8
     9    /**
     10     * @dataProvider data_user_name
     11     */
     12    public function test_user_name( $user_name, $error_message ) {
     13        $v = wpmu_validate_user_signup( $user_name, 'foo@example.com' );
     14        $this->assertContains( 'user_name', $v['errors']->get_error_codes(), $error_message );
     15    }
     16
     17    public function data_user_name() {
     18        return array(
     19            array( 'contains spaces', 'User names with spaces are not allowed.' ),
     20            array( 'ContainsCaps', 'User names with capital letters are not allowed.' ),
     21            array( 'contains_underscores', 'User names with underscores are not allowed.' ),
     22            array( 'contains%^*()junk', 'User names with non-alphanumeric characters are not allowed.' ),
     23            array( '', 'Empty user names are not allowed.' ),
     24            array( 'foo', 'User names of 3 characters are not allowed.' ),
     25            array( 'fo', 'User names of 2 characters are not allowed.' ),
     26            array( 'f', 'User names of 1 characters are not allowed.' ),
     27            array( 'f', 'User names of 1 characters are not allowed.' ),
     28            array( '12345', 'User names consisting only of numbers are not allowed.' ),
     29            array( 'thisusernamecontainsenoughcharacterstobelongerthan60characters', 'User names longer than 60 characters are not allowed.' ),
     30        );
     31    }
     32
     33    public function test_should_fail_for_illegal_names() {
     34        $illegal = array( 'foo123', 'bar123' );
     35        update_site_option( 'illegal_names', $illegal );
     36
     37        foreach ( $illegal as $i ) {
     38            $v = wpmu_validate_user_signup( $i, 'foo@example.com' );
    7639            $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
    7740        }
    78 
    79         public function test_should_fail_for_existing_user_email() {
    80             $u = self::factory()->user->create( array( 'user_email' => 'foo@example.com' ) );
    81             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
    82             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    83         }
    84 
    85         public function test_should_fail_for_existing_signup_with_same_username() {
    86             // Don't send notifications.
    87             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    88             wpmu_signup_user( 'foo123', 'foo@example.com' );
    89             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    90 
    91             $v = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
    92             $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
    93         }
    94 
    95         public function test_should_not_fail_for_existing_signup_with_same_username_if_signup_is_old() {
    96             // Don't send notifications.
    97             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    98             wpmu_signup_user( 'foo123', 'foo@example.com' );
    99             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    100 
    101             global $wpdb;
    102             $date = gmdate( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) - 60 );
    103             $wpdb->update( $wpdb->signups, array( 'registered' => $date ), array( 'user_login' => 'foo123' ) );
    104 
    105             $v = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
    106             $this->assertNotContains( 'user_name', $v['errors']->get_error_codes() );
    107         }
    108 
    109         public function test_should_fail_for_existing_signup_with_same_email() {
    110             // Don't send notifications.
    111             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    112             wpmu_signup_user( 'foo123', 'foo@example.com' );
    113             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    114 
    115             $v = wpmu_validate_user_signup( 'foo2', 'foo@example.com' );
    116             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    117         }
    118 
    119         public function test_should_not_fail_for_existing_signup_with_same_email_if_signup_is_old() {
    120             // Don't send notifications.
    121             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    122             wpmu_signup_user( 'foo123', 'foo@example.com' );
    123             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    124 
    125             global $wpdb;
    126             $date = gmdate( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) - 60 );
    127             $wpdb->update( $wpdb->signups, array( 'registered' => $date ), array( 'user_login' => 'foo123' ) );
    128 
    129             $v = wpmu_validate_user_signup( 'foo2', 'foo2@example.com' );
    130             $this->assertNotContains( 'user_email', $v['errors']->get_error_codes() );
    131         }
    132 
    133         /**
    134          * @ticket 43232
    135          */
    136         public function test_should_not_fail_for_data_used_by_a_deleted_user() {
    137             global $wpdb;
    138 
    139             // Don't send notifications.
    140             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    141             add_filter( 'wpmu_welcome_user_notification', '__return_false' );
    142 
    143             // Signup, activate and delete new user.
    144             wpmu_signup_user( 'foo123', 'foo@example.com' );
    145             $key  = $wpdb->get_var( "SELECT activation_key FROM $wpdb->signups WHERE user_login = 'foo123'" );
    146             $user = wpmu_activate_signup( $key );
    147             wpmu_delete_user( $user['user_id'] );
    148 
    149             $valid = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
    150 
    151             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    152             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    153 
    154             $this->assertNotContains( 'user_name', $valid['errors']->get_error_codes() );
    155             $this->assertNotContains( 'user_email', $valid['errors']->get_error_codes() );
    156         }
    157 
    158         public function test_invalid_email_address_with_no_banned_domains_results_in_error() {
    159             $valid = wpmu_validate_user_signup( 'validusername', 'invalid-email' );
    160 
    161             $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
    162         }
    163 
    164         public function test_invalid_email_address_with_banned_domains_results_in_error() {
    165             update_site_option( 'banned_email_domains', 'bar.com' );
    166             $valid = wpmu_validate_user_signup( 'validusername', 'invalid-email' );
    167             delete_site_option( 'banned_email_domains' );
    168 
    169             $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
    170         }
    171 
    172         public function test_incomplete_email_address_with_no_banned_domains_results_in_error() {
    173             $valid = wpmu_validate_user_signup( 'validusername', 'incomplete@email' );
    174 
    175             $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
    176         }
    177 
    178         public function test_valid_email_address_matching_banned_domain_results_in_error() {
    179             update_site_option( 'banned_email_domains', 'bar.com' );
    180             $valid = wpmu_validate_user_signup( 'validusername', 'email@bar.com' );
    181             delete_site_option( 'banned_email_domains' );
    182 
    183             $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
    184         }
    185 
    186         public function test_valid_email_address_not_matching_banned_domain_returns_in_success() {
    187             update_site_option( 'banned_email_domains', 'bar.com' );
    188             $valid = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
    189             delete_site_option( 'banned_email_domains' );
    190 
    191             $this->assertNotContains( 'user_email', $valid['errors']->get_error_codes() );
    192         }
    193 
    194         /**
    195          * @ticket 43667
    196          */
    197         public function test_signup_nonce_check() {
    198             $original_php_self       = $_SERVER['PHP_SELF'];
    199             $_SERVER['PHP_SELF']     = '/wp-signup.php';
    200             $_POST['signup_form_id'] = 'user-signup-form';
    201             $_POST['_signup_form']   = wp_create_nonce( 'signup_form_' . $_POST['signup_form_id'] );
    202 
    203             $valid               = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
    204             $_SERVER['PHP_SELF'] = $original_php_self;
    205 
    206             $this->assertNotContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
    207         }
    208 
    209         /**
    210          * @ticket 43667
    211          */
    212         public function test_signup_nonce_check_invalid() {
    213             $original_php_self       = $_SERVER['PHP_SELF'];
    214             $_SERVER['PHP_SELF']     = '/wp-signup.php';
    215             $_POST['signup_form_id'] = 'user-signup-form';
    216             $_POST['_signup_form']   = wp_create_nonce( 'invalid' );
    217 
    218             $valid               = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
    219             $_SERVER['PHP_SELF'] = $original_php_self;
    220 
    221             $this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
    222         }
    223 
    224         /**
    225          * Ensure that wp_ensure_editable_role does not throw an exception when the role is editable.
    226          *
    227          * @ticket 43251
    228          *
    229          * @covers ::wp_ensure_editable_role
    230          */
    231         public function test_wp_ensure_editable_role_allows_editable_roles() {
    232             $role = get_role( 'editor' );
    233             $this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
    234             $this->assertNull( wp_ensure_editable_role( 'editor' ), 'The editor role should be editable.' );
    235         }
    236 
    237         /**
    238          * Ensure that wp_ensure_editable_role throws an exception for non-existent roles.
    239          *
    240          * @ticket 43251
    241          *
    242          * @covers ::wp_ensure_editable_role
    243          */
    244         public function test_wp_ensure_editable_role_does_not_allow_non_existent_role() {
    245             $this->expectException( 'WPDieException' );
    246             $role = get_role( 'non-existent-role' );
    247             $this->assertNotInstanceOf( 'WP_Role', $role, 'The non-existent-role role should not exist.' );
    248             wp_ensure_editable_role( 'non-existent-role' );
    249         }
    250 
    251         /**
    252          * Ensure that wp_ensure_editable_role throws an exception for roles that are not editable.
    253          *
    254          * @ticket 43251
    255          *
    256          * @covers ::wp_ensure_editable_role
    257          */
    258         public function test_wp_ensure_editable_role_does_not_allow_uneditable_roles() {
    259             add_filter(
    260                 'editable_roles',
    261                 function ( $roles ) {
    262                     unset( $roles['editor'] );
    263                     return $roles;
    264                 }
    265             );
    266             $this->expectException( 'WPDieException' );
    267             $role = get_role( 'editor' );
    268             $this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
    269             wp_ensure_editable_role( 'editor' );
    270         }
    271     }
    272 
    273 endif;
     41    }
     42
     43    public function test_should_fail_for_unsafe_email_address() {
     44        add_filter( 'is_email_address_unsafe', '__return_true' );
     45        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     46        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     47        remove_filter( 'is_email_address_unsafe', '__return_true' );
     48    }
     49
     50    public function test_should_fail_for_invalid_email_address() {
     51        add_filter( 'is_email', '__return_false' );
     52        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     53        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     54        remove_filter( 'is_email', '__return_false' );
     55    }
     56
     57    public function test_should_fail_for_emails_from_disallowed_domains() {
     58        $domains = array( 'foo.com', 'bar.org' );
     59        update_site_option( 'limited_email_domains', $domains );
     60
     61        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     62        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     63    }
     64
     65    public function test_should_not_fail_for_emails_from_allowed_domains_with_mixed_case() {
     66        $domains = array( 'foo.com', 'bar.org' );
     67        update_site_option( 'limited_email_domains', $domains );
     68
     69        $v = wpmu_validate_user_signup( 'foo123', 'foo@BAR.org' );
     70        $this->assertNotContains( 'user_email', $v['errors']->get_error_codes() );
     71    }
     72
     73    public function test_should_fail_for_existing_user_name() {
     74        $u = self::factory()->user->create( array( 'user_login' => 'foo123' ) );
     75        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     76        $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
     77    }
     78
     79    public function test_should_fail_for_existing_user_email() {
     80        $u = self::factory()->user->create( array( 'user_email' => 'foo@example.com' ) );
     81        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     82        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     83    }
     84
     85    public function test_should_fail_for_existing_signup_with_same_username() {
     86        // Don't send notifications.
     87        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     88        wpmu_signup_user( 'foo123', 'foo@example.com' );
     89        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     90
     91        $v = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
     92        $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
     93    }
     94
     95    public function test_should_not_fail_for_existing_signup_with_same_username_if_signup_is_old() {
     96        // Don't send notifications.
     97        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     98        wpmu_signup_user( 'foo123', 'foo@example.com' );
     99        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     100
     101        global $wpdb;
     102        $date = gmdate( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) - 60 );
     103        $wpdb->update( $wpdb->signups, array( 'registered' => $date ), array( 'user_login' => 'foo123' ) );
     104
     105        $v = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
     106        $this->assertNotContains( 'user_name', $v['errors']->get_error_codes() );
     107    }
     108
     109    public function test_should_fail_for_existing_signup_with_same_email() {
     110        // Don't send notifications.
     111        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     112        wpmu_signup_user( 'foo123', 'foo@example.com' );
     113        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     114
     115        $v = wpmu_validate_user_signup( 'foo2', 'foo@example.com' );
     116        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     117    }
     118
     119    public function test_should_not_fail_for_existing_signup_with_same_email_if_signup_is_old() {
     120        // Don't send notifications.
     121        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     122        wpmu_signup_user( 'foo123', 'foo@example.com' );
     123        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     124
     125        global $wpdb;
     126        $date = gmdate( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) - 60 );
     127        $wpdb->update( $wpdb->signups, array( 'registered' => $date ), array( 'user_login' => 'foo123' ) );
     128
     129        $v = wpmu_validate_user_signup( 'foo2', 'foo2@example.com' );
     130        $this->assertNotContains( 'user_email', $v['errors']->get_error_codes() );
     131    }
     132
     133    /**
     134     * @ticket 43232
     135     */
     136    public function test_should_not_fail_for_data_used_by_a_deleted_user() {
     137        global $wpdb;
     138
     139        // Don't send notifications.
     140        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     141        add_filter( 'wpmu_welcome_user_notification', '__return_false' );
     142
     143        // Signup, activate and delete new user.
     144        wpmu_signup_user( 'foo123', 'foo@example.com' );
     145        $key  = $wpdb->get_var( "SELECT activation_key FROM $wpdb->signups WHERE user_login = 'foo123'" );
     146        $user = wpmu_activate_signup( $key );
     147        wpmu_delete_user( $user['user_id'] );
     148
     149        $valid = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
     150
     151        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     152        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     153
     154        $this->assertNotContains( 'user_name', $valid['errors']->get_error_codes() );
     155        $this->assertNotContains( 'user_email', $valid['errors']->get_error_codes() );
     156    }
     157
     158    public function test_invalid_email_address_with_no_banned_domains_results_in_error() {
     159        $valid = wpmu_validate_user_signup( 'validusername', 'invalid-email' );
     160
     161        $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
     162    }
     163
     164    public function test_invalid_email_address_with_banned_domains_results_in_error() {
     165        update_site_option( 'banned_email_domains', 'bar.com' );
     166        $valid = wpmu_validate_user_signup( 'validusername', 'invalid-email' );
     167        delete_site_option( 'banned_email_domains' );
     168
     169        $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
     170    }
     171
     172    public function test_incomplete_email_address_with_no_banned_domains_results_in_error() {
     173        $valid = wpmu_validate_user_signup( 'validusername', 'incomplete@email' );
     174
     175        $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
     176    }
     177
     178    public function test_valid_email_address_matching_banned_domain_results_in_error() {
     179        update_site_option( 'banned_email_domains', 'bar.com' );
     180        $valid = wpmu_validate_user_signup( 'validusername', 'email@bar.com' );
     181        delete_site_option( 'banned_email_domains' );
     182
     183        $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
     184    }
     185
     186    public function test_valid_email_address_not_matching_banned_domain_returns_in_success() {
     187        update_site_option( 'banned_email_domains', 'bar.com' );
     188        $valid = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
     189        delete_site_option( 'banned_email_domains' );
     190
     191        $this->assertNotContains( 'user_email', $valid['errors']->get_error_codes() );
     192    }
     193
     194    /**
     195     * @ticket 43667
     196     */
     197    public function test_signup_nonce_check() {
     198        $original_php_self       = $_SERVER['PHP_SELF'];
     199        $_SERVER['PHP_SELF']     = '/wp-signup.php';
     200        $_POST['signup_form_id'] = 'user-signup-form';
     201        $_POST['_signup_form']   = wp_create_nonce( 'signup_form_' . $_POST['signup_form_id'] );
     202
     203        $valid               = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
     204        $_SERVER['PHP_SELF'] = $original_php_self;
     205
     206        $this->assertNotContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
     207    }
     208
     209    /**
     210     * @ticket 43667
     211     */
     212    public function test_signup_nonce_check_invalid() {
     213        $original_php_self       = $_SERVER['PHP_SELF'];
     214        $_SERVER['PHP_SELF']     = '/wp-signup.php';
     215        $_POST['signup_form_id'] = 'user-signup-form';
     216        $_POST['_signup_form']   = wp_create_nonce( 'invalid' );
     217
     218        $valid               = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
     219        $_SERVER['PHP_SELF'] = $original_php_self;
     220
     221        $this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
     222    }
     223
     224    /**
     225     * Ensure that wp_ensure_editable_role does not throw an exception when the role is editable.
     226     *
     227     * @ticket 43251
     228     *
     229     * @covers ::wp_ensure_editable_role
     230     */
     231    public function test_wp_ensure_editable_role_allows_editable_roles() {
     232        $role = get_role( 'editor' );
     233        $this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
     234        $this->assertNull( wp_ensure_editable_role( 'editor' ), 'The editor role should be editable.' );
     235    }
     236
     237    /**
     238     * Ensure that wp_ensure_editable_role throws an exception for non-existent roles.
     239     *
     240     * @ticket 43251
     241     *
     242     * @covers ::wp_ensure_editable_role
     243     */
     244    public function test_wp_ensure_editable_role_does_not_allow_non_existent_role() {
     245        $this->expectException( 'WPDieException' );
     246        $role = get_role( 'non-existent-role' );
     247        $this->assertNotInstanceOf( 'WP_Role', $role, 'The non-existent-role role should not exist.' );
     248        wp_ensure_editable_role( 'non-existent-role' );
     249    }
     250
     251    /**
     252     * Ensure that wp_ensure_editable_role throws an exception for roles that are not editable.
     253     *
     254     * @ticket 43251
     255     *
     256     * @covers ::wp_ensure_editable_role
     257     */
     258    public function test_wp_ensure_editable_role_does_not_allow_uneditable_roles() {
     259        add_filter(
     260            'editable_roles',
     261            function ( $roles ) {
     262                unset( $roles['editor'] );
     263                return $roles;
     264            }
     265        );
     266        $this->expectException( 'WPDieException' );
     267        $role = get_role( 'editor' );
     268        $this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
     269        wp_ensure_editable_role( 'editor' );
     270    }
     271}
  • trunk/tests/phpunit/tests/option/multisite.php

    r54637 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    5     /**
    6      * Tests specific to network and site options in Multisite.
    7      *
    8      * @group option
    9      * @group ms-option
     3/**
     4 * Tests specific to network and site options in Multisite.
     5 *
     6 * @group option
     7 * @group ms-option
     8 * @group ms-required
     9 * @group multisite
     10 */
     11class Tests_Option_Multisite extends WP_UnitTestCase {
     12
     13    /**
     14     * @covers ::get_blog_option
     15     * @covers ::get_option
     16     * @covers ::add_blog_option
     17     * @covers ::update_blog_option
     18     * @covers ::delete_blog_option
     19     */
     20    public function test_from_same_site() {
     21        $key    = __FUNCTION__ . '_1';
     22        $key2   = __FUNCTION__ . '_2';
     23        $value  = __FUNCTION__ . '_val1';
     24        $value2 = __FUNCTION__ . '_val2';
     25
     26        $this->assertFalse( get_blog_option( 1, 'doesnotexist' ) );
     27        $this->assertFalse( get_option( 'doesnotexist' ) );           // Check get_option().
     28
     29        $this->assertTrue( add_blog_option( 1, $key, $value ) );
     30        // Assert all values of $blog_id that means the current or main blog (the same here).
     31        $this->assertSame( $value, get_blog_option( 1, $key ) );
     32        $this->assertSame( $value, get_blog_option( null, $key ) );
     33        $this->assertSame( $value, get_blog_option( '1', $key ) );
     34        $this->assertSame( $value, get_option( $key ) );            // Check get_option().
     35
     36        $this->assertFalse( add_blog_option( 1, $key, $value ) );     // Already exists.
     37        $this->assertFalse( update_blog_option( 1, $key, $value ) );  // Value is the same.
     38        $this->assertTrue( update_blog_option( 1, $key, $value2 ) );
     39        $this->assertSame( $value2, get_blog_option( 1, $key ) );
     40        $this->assertSame( $value2, get_option( $key ) );           // Check get_option().
     41        $this->assertFalse( add_blog_option( 1, $key, $value ) );
     42        $this->assertSame( $value2, get_blog_option( 1, $key ) );
     43        $this->assertSame( $value2, get_option( $key ) );           // Check get_option().
     44
     45        $this->assertTrue( delete_blog_option( 1, $key ) );
     46        $this->assertFalse( get_blog_option( 1, $key ) );
     47        $this->assertFalse( get_option( $key ) );                     // Check get_option().
     48        $this->assertFalse( delete_blog_option( 1, $key ) );
     49        $this->assertTrue( update_blog_option( 1, $key2, $value2 ) );
     50        $this->assertSame( $value2, get_blog_option( 1, $key2 ) );
     51        $this->assertSame( $value2, get_option( $key2 ) );          // Check get_option().
     52        $this->assertTrue( delete_blog_option( 1, $key2 ) );
     53        $this->assertFalse( get_blog_option( 1, $key2 ) );
     54        $this->assertFalse( get_option( $key2 ) );                    // Check get_option().
     55    }
     56
     57    /**
     58     * @covers ::get_blog_option
     59     * @covers ::get_option
     60     * @covers ::add_blog_option
     61     * @covers ::update_blog_option
     62     * @covers ::delete_blog_option
     63     */
     64    public function test_from_same_site_with_null_blog_id() {
     65        $key    = __FUNCTION__ . '_1';
     66        $key2   = __FUNCTION__ . '_2';
     67        $value  = __FUNCTION__ . '_val1';
     68        $value2 = __FUNCTION__ . '_val2';
     69
     70        $this->assertFalse( get_blog_option( null, 'doesnotexist' ) );
     71        $this->assertFalse( get_option( 'doesnotexist' ) );              // Check get_option().
     72
     73        $this->assertTrue( add_blog_option( null, $key, $value ) );
     74        // Assert all values of $blog_id that means the current or main blog (the same here).
     75        $this->assertSame( $value, get_blog_option( null, $key ) );
     76        $this->assertSame( $value, get_blog_option( null, $key ) );
     77        $this->assertSame( $value, get_option( $key ) );               // Check get_option().
     78
     79        $this->assertFalse( add_blog_option( null, $key, $value ) );     // Already exists.
     80        $this->assertFalse( update_blog_option( null, $key, $value ) );  // Value is the same.
     81        $this->assertTrue( update_blog_option( null, $key, $value2 ) );
     82        $this->assertSame( $value2, get_blog_option( null, $key ) );
     83        $this->assertSame( $value2, get_option( $key ) );              // Check get_option().
     84        $this->assertFalse( add_blog_option( null, $key, $value ) );
     85        $this->assertSame( $value2, get_blog_option( null, $key ) );
     86        $this->assertSame( $value2, get_option( $key ) );              // Check get_option().
     87
     88        $this->assertTrue( delete_blog_option( null, $key ) );
     89        $this->assertFalse( get_blog_option( null, $key ) );
     90        $this->assertFalse( get_option( $key ) );                        // Check get_option().
     91        $this->assertFalse( delete_blog_option( null, $key ) );
     92        $this->assertTrue( update_blog_option( null, $key2, $value2 ) );
     93        $this->assertSame( $value2, get_blog_option( null, $key2 ) );
     94        $this->assertSame( $value2, get_option( $key2 ) );             // Check get_option().
     95        $this->assertTrue( delete_blog_option( null, $key2 ) );
     96        $this->assertFalse( get_blog_option( null, $key2 ) );
     97        $this->assertFalse( get_option( $key2 ) );                       // Check get_option().
     98    }
     99
     100    /**
     101     * @covers ::get_blog_option
     102     * @covers ::get_option
     103     * @covers ::add_blog_option
     104     * @covers ::update_blog_option
     105     * @covers ::delete_blog_option
     106     */
     107    public function test_with_another_site() {
     108        $user_id = self::factory()->user->create();
     109        $this->assertIsInt( $user_id );
     110
     111        $blog_id = self::factory()->blog->create(
     112            array(
     113                'user_id' => $user_id,
     114                'public'  => 1,
     115            )
     116        );
     117        $this->assertIsInt( $blog_id );
     118
     119        $key    = __FUNCTION__ . '_key1';
     120        $key2   = __FUNCTION__ . '_key2';
     121        $value  = __FUNCTION__ . '_val1';
     122        $value2 = __FUNCTION__ . '_val2';
     123
     124        $this->assertFalse( get_blog_option( $blog_id, 'doesnotexist' ) );
     125        // $this->assertFalse( get_option( 'doesnotexist' ) );               // Check get_option().
     126
     127        $this->assertTrue( add_blog_option( $blog_id, $key, $value ) );
     128        // Assert all values of $blog_id that means the current or main blog (the same here).
     129        $this->assertSame( $value, get_blog_option( $blog_id, $key ) );
     130        $this->assertSame( $value, get_blog_option( (string) $blog_id, $key ) );
     131        // $this->assertSame( $value, get_option( $key ) );                // Check get_option().
     132
     133        $this->assertFalse( add_blog_option( $blog_id, $key, $value ) );     // Already exists.
     134        $this->assertFalse( update_blog_option( $blog_id, $key, $value ) );  // Value is the same.
     135        $this->assertTrue( update_blog_option( $blog_id, $key, $value2 ) );
     136        $this->assertSame( $value2, get_blog_option( $blog_id, $key ) );
     137        // $this->assertSame( $value2, get_option( $key ) );               // Check get_option().
     138        $this->assertFalse( add_blog_option( $blog_id, $key, $value ) );
     139        $this->assertSame( $value2, get_blog_option( $blog_id, $key ) );
     140        // $this->assertSame( $value2, get_option( $key ) );               // Check get_option().
     141
     142        $this->assertTrue( delete_blog_option( $blog_id, $key ) );
     143        $this->assertFalse( get_blog_option( $blog_id, $key ) );
     144        // $this->assertFalse( get_option( $key ) );                         // Check get_option().
     145        $this->assertFalse( delete_blog_option( $blog_id, $key ) );
     146        $this->assertTrue( update_blog_option( $blog_id, $key2, $value2 ) );
     147        $this->assertSame( $value2, get_blog_option( $blog_id, $key2 ) );
     148        // $this->assertSame( $value2, get_option( $key2 ) );              // Check get_option().
     149        $this->assertTrue( delete_blog_option( $blog_id, $key2 ) );
     150        $this->assertFalse( get_blog_option( $blog_id, $key2 ) );
     151        // $this->assertFalse( get_option( $key2 ) );                        // Check get_option().
     152    }
     153
     154    /**
    10155     * @group multisite
    11      */
    12     class Tests_Option_Multisite extends WP_UnitTestCase {
    13 
    14         /**
    15          * @covers ::get_blog_option
    16          * @covers ::get_option
    17          * @covers ::add_blog_option
    18          * @covers ::update_blog_option
    19          * @covers ::delete_blog_option
    20          */
    21         public function test_from_same_site() {
    22             $key    = __FUNCTION__ . '_1';
    23             $key2   = __FUNCTION__ . '_2';
    24             $value  = __FUNCTION__ . '_val1';
    25             $value2 = __FUNCTION__ . '_val2';
    26 
    27             $this->assertFalse( get_blog_option( 1, 'doesnotexist' ) );
    28             $this->assertFalse( get_option( 'doesnotexist' ) );           // Check get_option().
    29 
    30             $this->assertTrue( add_blog_option( 1, $key, $value ) );
    31             // Assert all values of $blog_id that means the current or main blog (the same here).
    32             $this->assertSame( $value, get_blog_option( 1, $key ) );
    33             $this->assertSame( $value, get_blog_option( null, $key ) );
    34             $this->assertSame( $value, get_blog_option( '1', $key ) );
    35             $this->assertSame( $value, get_option( $key ) );            // Check get_option().
    36 
    37             $this->assertFalse( add_blog_option( 1, $key, $value ) );     // Already exists.
    38             $this->assertFalse( update_blog_option( 1, $key, $value ) );  // Value is the same.
    39             $this->assertTrue( update_blog_option( 1, $key, $value2 ) );
    40             $this->assertSame( $value2, get_blog_option( 1, $key ) );
    41             $this->assertSame( $value2, get_option( $key ) );           // Check get_option().
    42             $this->assertFalse( add_blog_option( 1, $key, $value ) );
    43             $this->assertSame( $value2, get_blog_option( 1, $key ) );
    44             $this->assertSame( $value2, get_option( $key ) );           // Check get_option().
    45 
    46             $this->assertTrue( delete_blog_option( 1, $key ) );
    47             $this->assertFalse( get_blog_option( 1, $key ) );
    48             $this->assertFalse( get_option( $key ) );                     // Check get_option().
    49             $this->assertFalse( delete_blog_option( 1, $key ) );
    50             $this->assertTrue( update_blog_option( 1, $key2, $value2 ) );
    51             $this->assertSame( $value2, get_blog_option( 1, $key2 ) );
    52             $this->assertSame( $value2, get_option( $key2 ) );          // Check get_option().
    53             $this->assertTrue( delete_blog_option( 1, $key2 ) );
    54             $this->assertFalse( get_blog_option( 1, $key2 ) );
    55             $this->assertFalse( get_option( $key2 ) );                    // Check get_option().
    56         }
    57 
    58         /**
    59          * @covers ::get_blog_option
    60          * @covers ::get_option
    61          * @covers ::add_blog_option
    62          * @covers ::update_blog_option
    63          * @covers ::delete_blog_option
    64          */
    65         public function test_from_same_site_with_null_blog_id() {
    66             $key    = __FUNCTION__ . '_1';
    67             $key2   = __FUNCTION__ . '_2';
    68             $value  = __FUNCTION__ . '_val1';
    69             $value2 = __FUNCTION__ . '_val2';
    70 
    71             $this->assertFalse( get_blog_option( null, 'doesnotexist' ) );
    72             $this->assertFalse( get_option( 'doesnotexist' ) );              // Check get_option().
    73 
    74             $this->assertTrue( add_blog_option( null, $key, $value ) );
    75             // Assert all values of $blog_id that means the current or main blog (the same here).
    76             $this->assertSame( $value, get_blog_option( null, $key ) );
    77             $this->assertSame( $value, get_blog_option( null, $key ) );
    78             $this->assertSame( $value, get_option( $key ) );               // Check get_option().
    79 
    80             $this->assertFalse( add_blog_option( null, $key, $value ) );     // Already exists.
    81             $this->assertFalse( update_blog_option( null, $key, $value ) );  // Value is the same.
    82             $this->assertTrue( update_blog_option( null, $key, $value2 ) );
    83             $this->assertSame( $value2, get_blog_option( null, $key ) );
    84             $this->assertSame( $value2, get_option( $key ) );              // Check get_option().
    85             $this->assertFalse( add_blog_option( null, $key, $value ) );
    86             $this->assertSame( $value2, get_blog_option( null, $key ) );
    87             $this->assertSame( $value2, get_option( $key ) );              // Check get_option().
    88 
    89             $this->assertTrue( delete_blog_option( null, $key ) );
    90             $this->assertFalse( get_blog_option( null, $key ) );
    91             $this->assertFalse( get_option( $key ) );                        // Check get_option().
    92             $this->assertFalse( delete_blog_option( null, $key ) );
    93             $this->assertTrue( update_blog_option( null, $key2, $value2 ) );
    94             $this->assertSame( $value2, get_blog_option( null, $key2 ) );
    95             $this->assertSame( $value2, get_option( $key2 ) );             // Check get_option().
    96             $this->assertTrue( delete_blog_option( null, $key2 ) );
    97             $this->assertFalse( get_blog_option( null, $key2 ) );
    98             $this->assertFalse( get_option( $key2 ) );                       // Check get_option().
    99         }
    100 
    101         /**
    102          * @covers ::get_blog_option
    103          * @covers ::get_option
    104          * @covers ::add_blog_option
    105          * @covers ::update_blog_option
    106          * @covers ::delete_blog_option
    107          */
    108         public function test_with_another_site() {
    109             $user_id = self::factory()->user->create();
    110             $this->assertIsInt( $user_id );
    111 
    112             $blog_id = self::factory()->blog->create(
    113                 array(
    114                     'user_id' => $user_id,
    115                     'public'  => 1,
    116                 )
    117             );
    118             $this->assertIsInt( $blog_id );
    119 
    120             $key    = __FUNCTION__ . '_key1';
    121             $key2   = __FUNCTION__ . '_key2';
    122             $value  = __FUNCTION__ . '_val1';
    123             $value2 = __FUNCTION__ . '_val2';
    124 
    125             $this->assertFalse( get_blog_option( $blog_id, 'doesnotexist' ) );
    126             // $this->assertFalse( get_option( 'doesnotexist' ) );               // Check get_option().
    127 
    128             $this->assertTrue( add_blog_option( $blog_id, $key, $value ) );
    129             // Assert all values of $blog_id that means the current or main blog (the same here).
    130             $this->assertSame( $value, get_blog_option( $blog_id, $key ) );
    131             $this->assertSame( $value, get_blog_option( (string) $blog_id, $key ) );
    132             // $this->assertSame( $value, get_option( $key ) );                // Check get_option().
    133 
    134             $this->assertFalse( add_blog_option( $blog_id, $key, $value ) );     // Already exists.
    135             $this->assertFalse( update_blog_option( $blog_id, $key, $value ) );  // Value is the same.
    136             $this->assertTrue( update_blog_option( $blog_id, $key, $value2 ) );
    137             $this->assertSame( $value2, get_blog_option( $blog_id, $key ) );
    138             // $this->assertSame( $value2, get_option( $key ) );               // Check get_option().
    139             $this->assertFalse( add_blog_option( $blog_id, $key, $value ) );
    140             $this->assertSame( $value2, get_blog_option( $blog_id, $key ) );
    141             // $this->assertSame( $value2, get_option( $key ) );               // Check get_option().
    142 
    143             $this->assertTrue( delete_blog_option( $blog_id, $key ) );
    144             $this->assertFalse( get_blog_option( $blog_id, $key ) );
    145             // $this->assertFalse( get_option( $key ) );                         // Check get_option().
    146             $this->assertFalse( delete_blog_option( $blog_id, $key ) );
    147             $this->assertTrue( update_blog_option( $blog_id, $key2, $value2 ) );
    148             $this->assertSame( $value2, get_blog_option( $blog_id, $key2 ) );
    149             // $this->assertSame( $value2, get_option( $key2 ) );              // Check get_option().
    150             $this->assertTrue( delete_blog_option( $blog_id, $key2 ) );
    151             $this->assertFalse( get_blog_option( $blog_id, $key2 ) );
    152             // $this->assertFalse( get_option( $key2 ) );                        // Check get_option().
    153         }
    154 
    155         /**
    156          * @group multisite
    157          *
    158          * @covers ::get_site_option
    159          */
    160         public function test_site_notoptions() {
    161             $network_id     = get_current_network_id();
    162             $notoptions_key = "{$network_id}:notoptions";
    163 
    164             $_notoptions = wp_cache_get( 'notoptions', 'site-options' );
    165             $this->assertEmpty( $_notoptions );
    166             $_notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
    167             $this->assertEmpty( $_notoptions1 );
    168 
    169             get_site_option( 'burrito' );
    170 
    171             $notoptions = wp_cache_get( 'notoptions', 'site-options' );
    172             $this->assertEmpty( $notoptions );
    173             $notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
    174             $this->assertNotEmpty( $notoptions1 );
    175         }
    176 
    177         /**
    178          * @covers ::users_can_register_signup_filter
    179          * @covers ::get_site_option
    180          */
    181         public function test_users_can_register_signup_filter() {
    182 
    183             get_site_option( 'registration' );
    184             $this->assertFalse( users_can_register_signup_filter() );
    185 
    186             update_site_option( 'registration', 'all' );
    187             $this->assertTrue( users_can_register_signup_filter() );
    188 
    189             update_site_option( 'registration', 'user' );
    190             $this->assertTrue( users_can_register_signup_filter() );
    191 
    192             update_site_option( 'registration', 'none' );
    193             $this->assertFalse( users_can_register_signup_filter() );
    194         }
    195 
    196         /**
    197          * @dataProvider data_illegal_names
    198          *
    199          * @covers ::update_site_option
    200          * @covers ::get_site_option
    201          */
    202         public function test_sanitize_network_option_illegal_names( $option_value, $sanitized_option_value ) {
    203             update_site_option( 'illegal_names', $option_value );
    204             $this->assertSame( $sanitized_option_value, get_site_option( 'illegal_names' ) );
    205         }
    206 
    207         public function data_illegal_names() {
    208             return array(
    209                 array( array( '', 'Woo', '' ), array( 'Woo' ) ),
    210                 array( 'foo bar', array( 'foo', 'bar' ) ),
    211                 array( array(), '' ),
    212             );
    213         }
    214 
    215         /**
    216          * @dataProvider data_email_domains
    217          *
    218          * @param $option_value
    219          * @param $sanitized_option_value
    220          *
    221          * @covers ::update_site_option
    222          * @covers ::get_site_option
    223          */
    224         public function test_sanitize_network_option_limited_email_domains( $option_value, $sanitized_option_value ) {
    225             update_site_option( 'limited_email_domains', $option_value );
    226             $this->assertSame( $sanitized_option_value, get_site_option( 'limited_email_domains' ) );
    227         }
    228 
    229         /**
    230          * @dataProvider data_email_domains
    231          *
    232          * @param $option_value
    233          * @param $sanitized_option_value
    234          *
    235          * @covers ::update_site_option
    236          * @covers ::get_site_option
    237          */
    238         public function test_sanitize_network_option_banned_email_domains( $option_value, $sanitized_option_value ) {
    239             update_site_option( 'banned_email_domains', $option_value );
    240             $this->assertSame( $sanitized_option_value, get_site_option( 'banned_email_domains' ) );
    241         }
    242 
    243         public function data_email_domains() {
    244             return array(
    245                 array( array( 'woo', '', 'boo.com', 'foo.net.biz..' ), array( 'woo', 'boo.com' ) ),
    246                 array( "foo\nbar", array( 'foo', 'bar' ) ),
    247                 array( "foo\n\nbar", array( 'foo', 'bar' ) ),
    248                 array( "\nfoo\nbar\n", array( 'foo', 'bar' ) ),
    249                 array( "foo\nfoo.net.biz..", array( 'foo' ) ),
    250                 array( "foo\nfoo.net.biz..\nbar.com", array( 'foo', 'bar.com' ) ),
    251                 array( 'foo.', array( 'foo.' ) ),
    252                 array( '.foo', array( '.foo' ) ),
    253                 array( 'foo^net', '' ),
    254                 array( array(), '' ),
    255             );
    256         }
    257     }
    258 
    259 endif;
     156     *
     157     * @covers ::get_site_option
     158     */
     159    public function test_site_notoptions() {
     160        $network_id     = get_current_network_id();
     161        $notoptions_key = "{$network_id}:notoptions";
     162
     163        $_notoptions = wp_cache_get( 'notoptions', 'site-options' );
     164        $this->assertEmpty( $_notoptions );
     165        $_notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
     166        $this->assertEmpty( $_notoptions1 );
     167
     168        get_site_option( 'burrito' );
     169
     170        $notoptions = wp_cache_get( 'notoptions', 'site-options' );
     171        $this->assertEmpty( $notoptions );
     172        $notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
     173        $this->assertNotEmpty( $notoptions1 );
     174    }
     175
     176    /**
     177     * @covers ::users_can_register_signup_filter
     178     * @covers ::get_site_option
     179     */
     180    public function test_users_can_register_signup_filter() {
     181
     182        get_site_option( 'registration' );
     183        $this->assertFalse( users_can_register_signup_filter() );
     184
     185        update_site_option( 'registration', 'all' );
     186        $this->assertTrue( users_can_register_signup_filter() );
     187
     188        update_site_option( 'registration', 'user' );
     189        $this->assertTrue( users_can_register_signup_filter() );
     190
     191        update_site_option( 'registration', 'none' );
     192        $this->assertFalse( users_can_register_signup_filter() );
     193    }
     194
     195    /**
     196     * @dataProvider data_illegal_names
     197     *
     198     * @covers ::update_site_option
     199     * @covers ::get_site_option
     200     */
     201    public function test_sanitize_network_option_illegal_names( $option_value, $sanitized_option_value ) {
     202        update_site_option( 'illegal_names', $option_value );
     203        $this->assertSame( $sanitized_option_value, get_site_option( 'illegal_names' ) );
     204    }
     205
     206    public function data_illegal_names() {
     207        return array(
     208            array( array( '', 'Woo', '' ), array( 'Woo' ) ),
     209            array( 'foo bar', array( 'foo', 'bar' ) ),
     210            array( array(), '' ),
     211        );
     212    }
     213
     214    /**
     215     * @dataProvider data_email_domains
     216     *
     217     * @param $option_value
     218     * @param $sanitized_option_value
     219     *
     220     * @covers ::update_site_option
     221     * @covers ::get_site_option
     222     */
     223    public function test_sanitize_network_option_limited_email_domains( $option_value, $sanitized_option_value ) {
     224        update_site_option( 'limited_email_domains', $option_value );
     225        $this->assertSame( $sanitized_option_value, get_site_option( 'limited_email_domains' ) );
     226    }
     227
     228    /**
     229     * @dataProvider data_email_domains
     230     *
     231     * @param $option_value
     232     * @param $sanitized_option_value
     233     *
     234     * @covers ::update_site_option
     235     * @covers ::get_site_option
     236     */
     237    public function test_sanitize_network_option_banned_email_domains( $option_value, $sanitized_option_value ) {
     238        update_site_option( 'banned_email_domains', $option_value );
     239        $this->assertSame( $sanitized_option_value, get_site_option( 'banned_email_domains' ) );
     240    }
     241
     242    public function data_email_domains() {
     243        return array(
     244            array( array( 'woo', '', 'boo.com', 'foo.net.biz..' ), array( 'woo', 'boo.com' ) ),
     245            array( "foo\nbar", array( 'foo', 'bar' ) ),
     246            array( "foo\n\nbar", array( 'foo', 'bar' ) ),
     247            array( "\nfoo\nbar\n", array( 'foo', 'bar' ) ),
     248            array( "foo\nfoo.net.biz..", array( 'foo' ) ),
     249            array( "foo\nfoo.net.biz..\nbar.com", array( 'foo', 'bar.com' ) ),
     250            array( 'foo.', array( 'foo.' ) ),
     251            array( '.foo', array( '.foo' ) ),
     252            array( 'foo^net', '' ),
     253            array( array(), '' ),
     254        );
     255    }
     256}
  • trunk/tests/phpunit/tests/theme/wpThemeGetAllowedFilters.php

    r57107 r60148  
    11<?php
    2 if ( is_multisite() ) :
     2
     3/**
     4 * Tests specific to the filtering of `WP_Theme::get_allowed()` and related functions.
     5 *
     6 * @group ms-required
     7 * @group multisite
     8 * @group themes
     9 */
     10class Tests_Theme_wpThemeGetAllowedFilters extends WP_UnitTestCase {
    311    /**
    4      * Tests specific to the filtering of `WP_Theme::get_allowed()` and related functions.
    5      *
    6      * @group themes
    7      * @group multisite
     12     * @var array List of themes allowed before filters are applied.
    813     */
    9     class Tests_Theme_wpThemeGetAllowedFilters extends WP_UnitTestCase {
    10         /**
    11          * @var array List of themes allowed before filters are applied.
    12          */
    13         protected $default_allowed;
     14    protected $default_allowed;
    1415
    15         protected $filter_network_allowed_themes_args;
     16    protected $filter_network_allowed_themes_args;
    1617
    17         public function test_network_allowed_themes_filter_sends_blog_id() {
    18             $blog_id = 1;
     18    public function test_network_allowed_themes_filter_sends_blog_id() {
     19        $blog_id = 1;
    1920
    20             add_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ), 10, 2 );
    21             WP_Theme::get_allowed( $blog_id );
    22             remove_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ) );
     21        add_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ), 10, 2 );
     22        WP_Theme::get_allowed( $blog_id );
     23        remove_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ) );
    2324
    24             $this->assertCount( 2, $this->filter_network_allowed_themes_args );
    25             $this->assertSame( $blog_id, $this->filter_network_allowed_themes_args[1] );
    26         }
     25        $this->assertCount( 2, $this->filter_network_allowed_themes_args );
     26        $this->assertSame( $blog_id, $this->filter_network_allowed_themes_args[1] );
     27    }
    2728
    28         /**
    29         * Test the `allowed_themes` filter, which filters themes allowed on a network.
    30         */
    31         public function test_wp_theme_get_allowed_with_allowed_themes_filter() {
    32             $blog_id = 1;
     29    /**
     30    * Test the `allowed_themes` filter, which filters themes allowed on a network.
     31    */
     32    public function test_wp_theme_get_allowed_with_allowed_themes_filter() {
     33        $blog_id = 1;
    3334
    34             $this->default_allowed = WP_Theme::get_allowed( $blog_id );
     35        $this->default_allowed = WP_Theme::get_allowed( $blog_id );
    3536
    36             add_filter( 'allowed_themes', array( $this, 'filter_allowed_themes' ), 10 );
    37             $allowed = WP_Theme::get_allowed( $blog_id );
    38             remove_filter( 'allowed_themes', array( $this, 'filter_allowed_themes' ), 10 );
     37        add_filter( 'allowed_themes', array( $this, 'filter_allowed_themes' ), 10 );
     38        $allowed = WP_Theme::get_allowed( $blog_id );
     39        remove_filter( 'allowed_themes', array( $this, 'filter_allowed_themes' ), 10 );
    3940
    40             $expected = $this->default_allowed + array( 'allow-on-network' => true );
     41        $expected = $this->default_allowed + array( 'allow-on-network' => true );
    4142
    42             $this->assertSame( $expected, $allowed );
    43         }
     43        $this->assertSame( $expected, $allowed );
     44    }
    4445
    45         /**
    46         * Test the `network_allowed_themes` filter, which filters allowed themes on the network and provides `$blog_id`.
    47         */
    48         public function test_wp_theme_get_allowed_with_network_allowed_themes_filter() {
    49             $blog_id = 1;
     46    /**
     47    * Test the `network_allowed_themes` filter, which filters allowed themes on the network and provides `$blog_id`.
     48    */
     49    public function test_wp_theme_get_allowed_with_network_allowed_themes_filter() {
     50        $blog_id = 1;
    5051
    51             $this->default_allowed = WP_Theme::get_allowed( $blog_id );
     52        $this->default_allowed = WP_Theme::get_allowed( $blog_id );
    5253
    53             add_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ), 10, 2 );
    54             $allowed = WP_Theme::get_allowed( $blog_id );
    55             remove_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ), 10 );
     54        add_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ), 10, 2 );
     55        $allowed = WP_Theme::get_allowed( $blog_id );
     56        remove_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ), 10 );
    5657
    57             $expected = $this->default_allowed + array( 'network-allowed-theme' => true );
     58        $expected = $this->default_allowed + array( 'network-allowed-theme' => true );
    5859
    59             $this->assertSame( $expected, $allowed );
    60         }
     60        $this->assertSame( $expected, $allowed );
     61    }
    6162
    62         /**
    63         * Test the `site_allowed_themes` filter, which filters allowed themes for a site and provides `$blog_id`.
    64         */
    65         public function test_wp_theme_get_allowed_with_site_allowed_themes_filter() {
    66             $blog_id = 1;
     63    /**
     64    * Test the `site_allowed_themes` filter, which filters allowed themes for a site and provides `$blog_id`.
     65    */
     66    public function test_wp_theme_get_allowed_with_site_allowed_themes_filter() {
     67        $blog_id = 1;
    6768
    68             $this->default_allowed = WP_Theme::get_allowed( $blog_id );
     69        $this->default_allowed = WP_Theme::get_allowed( $blog_id );
    6970
    70             add_filter( 'site_allowed_themes', array( $this, 'filter_site_allowed_themes' ), 10, 2 );
    71             $allowed = WP_Theme::get_allowed( $blog_id );
    72             remove_filter( 'site_allowed_themes', array( $this, 'filter_site_allowed_themes' ), 10 );
     71        add_filter( 'site_allowed_themes', array( $this, 'filter_site_allowed_themes' ), 10, 2 );
     72        $allowed = WP_Theme::get_allowed( $blog_id );
     73        remove_filter( 'site_allowed_themes', array( $this, 'filter_site_allowed_themes' ), 10 );
    7374
    74             $expected = $this->default_allowed + array( 'site-allowed-theme' => true );
     75        $expected = $this->default_allowed + array( 'site-allowed-theme' => true );
    7576
    76             $this->assertSame( $expected, $allowed );
    77         }
     77        $this->assertSame( $expected, $allowed );
     78    }
    7879
    79         public function filter_allowed_themes( $allowed_themes ) {
    80             $allowed_themes['allow-on-network'] = true;
     80    public function filter_allowed_themes( $allowed_themes ) {
     81        $allowed_themes['allow-on-network'] = true;
    8182
    82             return $allowed_themes;
    83         }
     83        return $allowed_themes;
     84    }
    8485
    85         public function filter_network_allowed_themes( $allowed_themes, $blog_id ) {
    86             $this->filter_network_allowed_themes_args = func_get_args();
     86    public function filter_network_allowed_themes( $allowed_themes, $blog_id ) {
     87        $this->filter_network_allowed_themes_args = func_get_args();
    8788
    88             $allowed_themes['network-allowed-theme'] = true;
     89        $allowed_themes['network-allowed-theme'] = true;
    8990
    90             return $allowed_themes;
    91         }
     91        return $allowed_themes;
     92    }
    9293
    93         public function filter_site_allowed_themes( $allowed_themes, $blog_id ) {
    94             $allowed_themes['site-allowed-theme'] = true;
     94    public function filter_site_allowed_themes( $allowed_themes, $blog_id ) {
     95        $allowed_themes['site-allowed-theme'] = true;
    9596
    96             return $allowed_themes;
    97         }
     97        return $allowed_themes;
    9898    }
    99 endif;
     99}
  • trunk/tests/phpunit/tests/user/getActiveBlogForUser.php

    r53489 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Tests specific to users in multisite.
     5 *
     6 * @group user
     7 * @group ms-required
     8 * @group ms-user
     9 * @group multisite
     10 */
     11class Tests_User_GetActiveBlogForUser extends WP_UnitTestCase {
     12
     13    public static $user_id = false;
     14
     15    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     16        self::$user_id = $factory->user->create();
     17    }
     18
     19    public static function wpTearDownAfterClass() {
     20        wpmu_delete_user( self::$user_id );
     21
     22        global $wp_rewrite;
     23        $wp_rewrite->init();
     24    }
    425
    526    /**
    6      * Tests specific to users in multisite.
    7      *
    8      * @group user
    9      * @group ms-user
    10      * @group multisite
     27     * @ticket 38355
    1128     */
    12     class Tests_User_GetActiveBlogForUser extends WP_UnitTestCase {
    13         public static $user_id = false;
     29    public function test_get_active_blog_for_user_with_no_sites() {
     30        $current_site_id = get_current_blog_id();
    1431
    15         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    16             self::$user_id = $factory->user->create();
    17         }
     32        remove_user_from_blog( self::$user_id, $current_site_id );
    1833
    19         public static function wpTearDownAfterClass() {
    20             wpmu_delete_user( self::$user_id );
     34        $result = get_active_blog_for_user( self::$user_id );
    2135
    22             global $wp_rewrite;
    23             $wp_rewrite->init();
    24         }
    25 
    26         /**
    27          * @ticket 38355
    28          */
    29         public function test_get_active_blog_for_user_with_no_sites() {
    30             $current_site_id = get_current_blog_id();
    31 
    32             remove_user_from_blog( self::$user_id, $current_site_id );
    33 
    34             $result = get_active_blog_for_user( self::$user_id );
    35 
    36             $this->assertNull( $result );
    37         }
    38 
    39         /**
    40          * @ticket 38355
    41          */
    42         public function test_get_active_blog_for_user_with_primary_site() {
    43             $site_id_one = self::factory()->blog->create( array( 'user_id' => self::$user_id ) );
    44             $site_id_two = self::factory()->blog->create( array( 'user_id' => self::$user_id ) );
    45 
    46             $sites           = get_blogs_of_user( self::$user_id );
    47             $site_ids        = array_keys( $sites );
    48             $primary_site_id = $site_ids[1];
    49 
    50             update_user_meta( self::$user_id, 'primary_blog', $primary_site_id );
    51 
    52             $result = get_active_blog_for_user( self::$user_id );
    53 
    54             wp_delete_site( $site_id_one );
    55             wp_delete_site( $site_id_two );
    56 
    57             $this->assertSame( $primary_site_id, $result->id );
    58         }
    59 
    60         /**
    61          * @ticket 38355
    62          */
    63         public function test_get_active_blog_for_user_without_primary_site() {
    64             $sites           = get_blogs_of_user( self::$user_id );
    65             $site_ids        = array_keys( $sites );
    66             $primary_site_id = $site_ids[0];
    67 
    68             delete_user_meta( self::$user_id, 'primary_blog' );
    69 
    70             $result = get_active_blog_for_user( self::$user_id );
    71 
    72             wp_delete_site( $primary_site_id );
    73 
    74             $this->assertSame( $primary_site_id, $result->id );
    75         }
    76 
    77         /**
    78          * @ticket 38355
    79          */
    80         public function test_get_active_blog_for_user_with_spam_site() {
    81             $current_site_id = get_current_blog_id();
    82 
    83             $site_id = self::factory()->blog->create(
    84                 array(
    85                     'user_id' => self::$user_id,
    86                     'spam'    => 1,
    87                 )
    88             );
    89 
    90             add_user_to_blog( $site_id, self::$user_id, 'subscriber' );
    91             update_user_meta( self::$user_id, 'primary_blog', $site_id );
    92 
    93             $result = get_active_blog_for_user( self::$user_id );
    94 
    95             wp_delete_site( $site_id );
    96 
    97             $this->assertSame( $current_site_id, $result->id );
    98         }
     36        $this->assertNull( $result );
    9937    }
    10038
    101 endif;
     39    /**
     40     * @ticket 38355
     41     */
     42    public function test_get_active_blog_for_user_with_primary_site() {
     43        $site_id_one = self::factory()->blog->create( array( 'user_id' => self::$user_id ) );
     44        $site_id_two = self::factory()->blog->create( array( 'user_id' => self::$user_id ) );
     45
     46        $sites           = get_blogs_of_user( self::$user_id );
     47        $site_ids        = array_keys( $sites );
     48        $primary_site_id = $site_ids[1];
     49
     50        update_user_meta( self::$user_id, 'primary_blog', $primary_site_id );
     51
     52        $result = get_active_blog_for_user( self::$user_id );
     53
     54        wp_delete_site( $site_id_one );
     55        wp_delete_site( $site_id_two );
     56
     57        $this->assertSame( $primary_site_id, $result->id );
     58    }
     59
     60    /**
     61     * @ticket 38355
     62     */
     63    public function test_get_active_blog_for_user_without_primary_site() {
     64        $sites           = get_blogs_of_user( self::$user_id );
     65        $site_ids        = array_keys( $sites );
     66        $primary_site_id = $site_ids[0];
     67
     68        delete_user_meta( self::$user_id, 'primary_blog' );
     69
     70        $result = get_active_blog_for_user( self::$user_id );
     71
     72        wp_delete_site( $primary_site_id );
     73
     74        $this->assertSame( $primary_site_id, $result->id );
     75    }
     76
     77    /**
     78     * @ticket 38355
     79     */
     80    public function test_get_active_blog_for_user_with_spam_site() {
     81        $current_site_id = get_current_blog_id();
     82
     83        $site_id = self::factory()->blog->create(
     84            array(
     85                'user_id' => self::$user_id,
     86                'spam'    => 1,
     87            )
     88        );
     89
     90        add_user_to_blog( $site_id, self::$user_id, 'subscriber' );
     91        update_user_meta( self::$user_id, 'primary_blog', $site_id );
     92
     93        $result = get_active_blog_for_user( self::$user_id );
     94
     95        wp_delete_site( $site_id );
     96
     97        $this->assertSame( $current_site_id, $result->id );
     98    }
     99}
  • trunk/tests/phpunit/tests/user/multisite.php

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