Make WordPress Core

Changeset 29916


Ignore:
Timestamp:
10/16/2014 05:06:22 AM (10 years ago)
Author:
jeremyfelt
Message:

Split and organize multisite unit tests

  • Move ms.php to multisite.php
  • Create multisite.php under directories option/ and user/ to better match existing structure.
  • Create a multisite/ directory containing bootstrap.php, site.php, and network.php for very multisite specific testing.
  • Add unit test groups ms-site, ms-user, ms-option, ms-network, and ms-bootstrap.

Fixes #29896

Location:
trunk/tests/phpunit/tests
Files:
5 added
1 edited
2 moved

Legend:

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

    r29915 r29916  
    88 * @group multisite
    99 */
    10 class Tests_MS extends WP_UnitTestCase {
    11     protected $plugin_hook_count = 0;
     10class Tests_Multisite extends WP_UnitTestCase {
    1211    protected $suppress = false;
    1312
     
    2625    }
    2726
    28     function test_remove_user_from_blog() {
    29         $user1 = $this->factory->user->create_and_get();
    30         $user2 = $this->factory->user->create_and_get();
    31 
    32         $post_id = $this->factory->post->create( array( 'post_author' => $user1->ID ) );
    33 
    34         remove_user_from_blog( $user1->ID, 1, $user2->ID );
    35 
    36         $post = get_post( $post_id );
    37 
    38         $this->assertNotEquals( $user1->ID, $post->post_author );
    39         $this->assertEquals( $user2->ID, $post->post_author );
    40     }
    41 
    42     /**
    43      * @ticket 22917
    44      */
    45     function test_enable_live_network_site_counts_filter() {
    46         $site_count_start = get_blog_count();
    47         // false for large networks by default
    48         add_filter( 'enable_live_network_counts', '__return_false' );
    49         $this->factory->blog->create_many( 4 );
    50 
    51         // count only updated when cron runs, so unchanged
    52         $this->assertEquals( $site_count_start, (int) get_blog_count() );
    53 
    54         add_filter( 'enable_live_network_counts', '__return_true' );
    55         $site_ids = $this->factory->blog->create_many( 4 );
    56 
    57         $this->assertEquals( $site_count_start + 9, (int) get_blog_count() );
    58 
    59         //clean up
    60         remove_filter( 'enable_live_network_counts', '__return_false' );
    61         remove_filter( 'enable_live_network_counts', '__return_true' );
    62         foreach ( $site_ids as $site_id ) {
    63             wpmu_delete_blog( $site_id, true );
    64         }
    65     }
    66 
    67     /**
    68      * @ticket 22917
    69      */
    70     function test_enable_live_network_user_counts_filter() {
    71         // false for large networks by default
    72         add_filter( 'enable_live_network_counts', '__return_false' );
    73 
    74         // Refresh the cache
    75         wp_update_network_counts();
    76         $start_count = get_user_count();
    77 
    78         wpmu_create_user( 'user', 'pass', 'email' );
    79 
    80         // No change, cache not refreshed
    81         $count = get_user_count();
    82 
    83         $this->assertEquals( $start_count, $count );
    84 
    85         wp_update_network_counts();
    86         $start_count = get_user_count();
    87 
    88         add_filter( 'enable_live_network_counts', '__return_true' );
    89 
    90         wpmu_create_user( 'user2', 'pass2', 'email2' );
    91 
    92         $count = get_user_count();
    93         $this->assertEquals( $start_count + 1, $count );
    94 
    95         remove_filter( 'enable_live_network_counts', '__return_false' );
    96         remove_filter( 'enable_live_network_counts', '__return_true' );
    97     }
    98 
    99     function test_create_and_delete_blog() {
    100         global $wpdb;
    101 
    102         $blog_ids = $this->factory->blog->create_many( 4 );
    103         foreach ( $blog_ids as $blog_id ) {
    104             $this->assertInternalType( 'int', $blog_id );
    105             $prefix = $wpdb->get_blog_prefix( $blog_id );
    106 
    107             // $get_all = false
    108             $details = get_blog_details( $blog_id, false );
    109             $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    110 
    111             // get_id_from_blogname(), see #20950
    112             $this->assertEquals( $blog_id, get_id_from_blogname( $details->path ) );
    113             $this->assertEquals( $blog_id, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
    114 
    115             // get_blog_id_from_url()
    116             $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
    117             $key = md5( $details->domain . $details->path );
    118             $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
    119 
    120             // These are empty until get_blog_details() is called with $get_all = true
    121             $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    122             $key = md5( $details->domain . $details->path );
    123             $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
    124 
    125             // $get_all = true should propulate the full blog-details cache and the blog slug lookup cache
    126             $details = get_blog_details( $blog_id, true );
    127             $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
    128             $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
    129 
    130             foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    131                 $suppress = $wpdb->suppress_errors();
    132                 $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    133                 $wpdb->suppress_errors( $suppress );
    134                 $this->assertNotEmpty( $table_fields );
    135                 $result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
    136                 if ( 'commentmeta' == $table || 'links' == $table )
    137                     $this->assertEmpty( $result );
    138                 else
    139                     $this->assertNotEmpty( $result );
    140             }
    141         }
    142 
    143         // update the blog count cache to use get_blog_count()
    144         wp_update_network_counts();
    145         $this->assertEquals( 4 + 1, (int) get_blog_count() );
    146 
    147         $drop_tables = false;
    148         // delete all blogs
    149         foreach ( $blog_ids as $blog_id ) {
    150             // drop tables for every second blog
    151             $drop_tables = ! $drop_tables;
    152             $details = get_blog_details( $blog_id, false );
    153 
    154             wpmu_delete_blog( $blog_id, $drop_tables );
    155 
    156             $this->assertEquals( false, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
    157             $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    158             $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    159             $key = md5( $details->domain . $details->path );
    160             $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
    161             $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    162 
    163             $prefix = $wpdb->get_blog_prefix( $blog_id );
    164             foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    165                 $suppress = $wpdb->suppress_errors();
    166                 $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    167                 $wpdb->suppress_errors( $suppress );
    168                 if ( $drop_tables )
    169                     $this->assertEmpty( $table_fields );
    170                 else
    171                     $this->assertNotEmpty( $table_fields, $prefix . $table );
    172             }
    173         }
    174 
    175         // update the blog count cache to use get_blog_count()
    176         wp_update_network_counts();
    177         $this->assertEquals( 1, get_blog_count() );
    178     }
    179 
    180     function test_get_blogs_of_user() {
    181         // Logged out users don't have blogs.
    182         $this->assertEquals( array(), get_blogs_of_user( 0 ) );
    183 
    184         $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    185         $blog_ids = $this->factory->blog->create_many( 10, array( 'user_id' => $user1_id ) );
    186 
    187         foreach ( $blog_ids as $blog_id )
    188             $this->assertInternalType( 'int', $blog_id );
    189 
    190         $blogs_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
    191         sort( $blogs_of_user );
    192         $this->assertEquals ( array_merge( array( 1 ), $blog_ids), $blogs_of_user );
    193 
    194         $this->assertTrue( remove_user_from_blog( $user1_id, 1 ) );
    195 
    196         $blogs_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
    197         sort( $blogs_of_user );
    198         $this->assertEquals ( $blog_ids, $blogs_of_user );
    199 
    200         foreach ( get_blogs_of_user( $user1_id, false ) as $blog ) {
    201             $this->assertTrue( isset( $blog->userblog_id ) );
    202             $this->assertTrue( isset( $blog->blogname ) );
    203             $this->assertTrue( isset( $blog->domain ) );
    204             $this->assertTrue( isset( $blog->path ) );
    205             $this->assertTrue( isset( $blog->site_id ) );
    206             $this->assertTrue( isset( $blog->siteurl ) );
    207             $this->assertTrue( isset( $blog->archived ) );
    208             $this->assertTrue( isset( $blog->spam ) );
    209             $this->assertTrue( isset( $blog->deleted ) );
    210         }
    211 
    212         // Non-existent users don't have blogs.
    213         wpmu_delete_user( $user1_id );
    214         $user = new WP_User( $user1_id );
    215         $this->assertFalse( $user->exists(), 'WP_User->exists' );
    216         $this->assertEquals( array(), get_blogs_of_user( $user1_id ) );
    217     }
    218 
    219     /**
    220      * @expectedDeprecated is_blog_user
    221      */
    222     function test_is_blog_user() {
    223         global $wpdb;
    224 
    225         $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    226 
    227         $old_current = get_current_user_id();
    228         wp_set_current_user( $user1_id );
    229 
    230         $this->assertTrue( is_blog_user() );
    231         $this->assertTrue( is_blog_user( $wpdb->blogid ) );
    232 
    233         $blog_ids = array();
    234 
    235         $blog_ids = $this->factory->blog->create_many( 5 );
    236         foreach ( $blog_ids as $blog_id ) {
    237             $this->assertInternalType( 'int', $blog_id );
    238             $this->assertTrue( is_blog_user( $blog_id ) );
    239             $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
    240             $this->assertFalse( is_blog_user( $blog_id ) );
    241         }
    242 
    243         wp_set_current_user( $old_current );
    244     }
    245 
    246     function test_is_user_member_of_blog() {
    247         global $wpdb;
    248 
    249         $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    250 
    251         $old_current = get_current_user_id();
    252         wp_set_current_user( $user1_id );
    253 
    254         $this->assertTrue( is_user_member_of_blog() );
    255         $this->assertTrue( is_user_member_of_blog( 0, 0 ) );
    256         $this->assertTrue( is_user_member_of_blog( 0, $wpdb->blogid ) );
    257         $this->assertTrue( is_user_member_of_blog( $user1_id ) );
    258         $this->assertTrue( is_user_member_of_blog( $user1_id, $wpdb->blogid ) );
    259 
    260         $blog_ids = $this->factory->blog->create_many( 5 );
    261         foreach ( $blog_ids as $blog_id ) {
    262             $this->assertInternalType( 'int', $blog_id );
    263             $this->assertTrue( is_user_member_of_blog( $user1_id, $blog_id ) );
    264             $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
    265             $this->assertFalse( is_user_member_of_blog( $user1_id, $blog_id ) );
    266         }
    267 
    268         wpmu_delete_user( $user1_id );
    269         $user = new WP_User( $user1_id );
    270         $this->assertFalse( $user->exists(), 'WP_User->exists' );
    271         $this->assertFalse( is_user_member_of_blog( $user1_id ), 'is_user_member_of_blog' );
    272 
    273         wp_set_current_user( $old_current );
    274     }
    275 
    276     function test_active_network_plugins() {
    277         $path = "hello.php";
    278 
    279         // local activate, should be invisible for the network
    280         activate_plugin($path); // $network_wide = false
    281         $active_plugins = wp_get_active_network_plugins();
    282         $this->assertEquals( Array(), $active_plugins );
    283 
    284         add_action( 'deactivated_plugin', array( $this, '_helper_deactivate_hook' ) );
    285 
    286         // activate the plugin sitewide
    287         activate_plugin($path, '', $network_wide = true);
    288         $active_plugins = wp_get_active_network_plugins();
    289         $this->assertEquals( Array(WP_PLUGIN_DIR . '/hello.php'), $active_plugins );
    290 
    291         //deactivate the plugin
    292         deactivate_plugins($path);
    293         $active_plugins = wp_get_active_network_plugins();
    294         $this->assertEquals( Array(), $active_plugins );
    295 
    296         $this->assertEquals( 1, $this->plugin_hook_count ); // testing actions and silent mode
    297 
    298         activate_plugin($path, '', $network_wide = true);
    299         deactivate_plugins($path, true); // silent
    300 
    301         $this->assertEquals( 1, $this->plugin_hook_count ); // testing actions and silent mode
    302     }
    303 
    304     /**
    305      * @ticket 28651
    306      */
    307     function test_duplicate_network_active_plugin() {
    308         $path = "hello.php";
    309         $mock = new MockAction();
    310         add_action( 'activate_' . $path, array ( $mock, 'action' ) );
    311 
    312         // should activate on the first try
    313         activate_plugin( $path, '', true );
    314         $active_plugins = wp_get_active_network_plugins();
    315         $this->assertCount( 1, $active_plugins );
    316         $this->assertEquals( 1, $mock->get_call_count() );
    317 
    318         // should do nothing on the second try
    319         activate_plugin( $path, '', true );
    320         $active_plugins = wp_get_active_network_plugins();
    321         $this->assertCount( 1, $active_plugins );
    322         $this->assertEquals( 1, $mock->get_call_count() );
    323 
    324         remove_action( 'activate_' . $path, array ( $mock, 'action' ) );
    325     }
    326 
    327     function _helper_deactivate_hook() {
    328         $this->plugin_hook_count++;
    329     }
    330 
    331     function test_get_user_count() {
    332         // Refresh the cache
    333         wp_update_network_counts();
    334         $start_count = get_user_count();
    335 
    336         // Only false for large networks as of 3.7
    337         add_filter( 'enable_live_network_counts', '__return_false' );
    338         $this->factory->user->create( array( 'role' => 'administrator' ) );
    339 
    340         $count = get_user_count(); // No change, cache not refreshed
    341         $this->assertEquals( $start_count, $count );
    342 
    343         wp_update_network_counts(); // Magic happens here
    344 
    345         $count = get_user_count();
    346         $this->assertEquals( $start_count + 1, $count );
    347         remove_filter( 'enable_live_network_counts', '__return_false' );
    348     }
    349 
    350     function test_wp_schedule_update_network_counts() {
    351         $this->assertFalse(wp_next_scheduled('update_network_counts'));
    352 
    353         // We can't use wp_schedule_update_network_counts() because WP_INSTALLING is set
    354         wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
    355 
    356         $this->assertInternalType('int', wp_next_scheduled('update_network_counts'));
    357     }
    358 
    359     function test_users_can_register_signup_filter() {
    360 
    361         $registration = get_site_option('registration');
    362         $this->assertFalse( users_can_register_signup_filter() );
    363 
    364         update_site_option('registration', 'all');
    365         $this->assertTrue( users_can_register_signup_filter() );
    366 
    367         update_site_option('registration', 'user');
    368         $this->assertTrue( users_can_register_signup_filter() );
    369 
    370         update_site_option('registration', 'none');
    371         $this->assertFalse( users_can_register_signup_filter() );
    372     }
    373 
    374     /**
    375      * @expectedDeprecated get_dashboard_blog
    376      */
    377     function test_get_dashboard_blog() {
    378         // if there is no dashboard blog set, current blog is used
    379         $dashboard_blog = get_dashboard_blog();
    380         $this->assertEquals( 1, $dashboard_blog->blog_id );
    381 
    382         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    383         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id ) );
    384         $this->assertInternalType( 'int', $blog_id );
    385 
    386         // set the dashboard blog to another one
    387         update_site_option( 'dashboard_blog', $blog_id );
    388         $dashboard_blog = get_dashboard_blog();
    389         $this->assertEquals( $blog_id, $dashboard_blog->blog_id );
    390     }
    391 
    39227    function test_wpmu_log_new_registrations() {
    39328        global $wpdb;
     
    40136        $reg_blog = $wpdb->get_col( "SELECT email FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.blog_id = 1 AND IP LIKE '" . $ip . "'" );
    40237        $this->assertEquals( $user->user_email, $reg_blog[ count( $reg_blog )-1 ] );
    403     }
    404 
    405     /**
    406      * @ticket 18119
    407      */
    408     function test_upload_is_user_over_quota() {
    409         $default_space_allowed = 100;
    410         $echo = false;
    411 
    412         $this->assertFalse( upload_is_user_over_quota( $echo ) );
    413         $this->assertTrue( is_upload_space_available() );
    414 
    415         update_site_option('upload_space_check_disabled', true);
    416         $this->assertFalse( upload_is_user_over_quota( $echo ) );
    417         $this->assertTrue( is_upload_space_available() );
    418 
    419         update_site_option( 'blog_upload_space', 0 );
    420         $this->assertFalse( upload_is_user_over_quota( $echo ) );
    421         $this->assertEquals( $default_space_allowed, get_space_allowed() );
    422         $this->assertTrue( is_upload_space_available() );
    423 
    424         update_site_option('upload_space_check_disabled', false);
    425         $this->assertFalse( upload_is_user_over_quota( $echo ) );
    426         $this->assertTrue( is_upload_space_available() );
    427 
    428         if ( defined( 'BLOGSUPLOADDIR' ) && ! file_exists( BLOGSUPLOADDIR ) )
    429             $this->markTestSkipped( 'This test is broken when blogs.dir does not exist. ');
    430 
    431         /*
    432         This is broken when blogs.dir does not exist, as get_upload_space_available()
    433         simply returns the value of blog_upload_space (converted to bytes), which would
    434         be negative but still not false. When blogs.dir does exist, < 0 is returned as 0.
    435         */
    436 
    437         update_site_option( 'blog_upload_space', -1 );
    438         $this->assertTrue( upload_is_user_over_quota( $echo ) );
    439         $this->assertEquals( -1, get_space_allowed() );
    440         $this->assertFalse( is_upload_space_available() );
    441 
    442         update_option( 'blog_upload_space', 0 );
    443         $this->assertFalse( upload_is_user_over_quota( $echo ) );
    444         $this->assertEquals( $default_space_allowed, get_space_allowed() );
    445         $this->assertTrue( is_upload_space_available() );
    446 
    447         update_option( 'blog_upload_space', -1 );
    448         $this->assertTrue( upload_is_user_over_quota( $echo ) );
    449         $this->assertEquals( -1, get_space_allowed() );
    450         $this->assertFalse( is_upload_space_available() );
    451     }
    452 
    453     function test_wpmu_update_blogs_date() {
    454         global $wpdb;
    455 
    456         wpmu_update_blogs_date();
    457 
    458         // compare the update time with the current time, allow delta < 2
    459         $blog = get_blog_details( $wpdb->blogid );
    460         $current_time = time();
    461         $time_difference = $current_time - strtotime( $blog->last_updated );
    462         $this->assertLessThan( 2, $time_difference );
    463     }
    464 
    465     function test_getters(){
    466         global $current_site;
    467 
    468         $blog_id = get_current_blog_id();
    469         $blog = get_blog_details( $blog_id );
    470         $this->assertEquals( $blog_id, $blog->blog_id );
    471         $this->assertEquals( $current_site->domain, $blog->domain );
    472         $this->assertEquals( '/', $blog->path );
    473 
    474         // Test defaulting to current blog
    475         $this->assertEquals( $blog, get_blog_details() );
    476 
    477         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    478         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogname', 'title' => 'Test Title' ) );
    479         $this->assertInternalType( 'int', $blog_id );
    480 
    481         $this->assertEquals( 'http://' . $current_site->domain . $current_site->path . 'test_blogname/', get_blogaddress_by_name('test_blogname') );
    482 
    483         $this->assertEquals( $blog_id, get_id_from_blogname('test_blogname') );
    484     }
    485 
    486     function _action_counter_cb( $blog_id ) {
    487         global $test_action_counter;
    488         $test_action_counter++;
    489     }
    490 
    491     function test_update_blog_details() {
    492         global $test_action_counter;
    493 
    494         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    495         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) );
    496         $this->assertInternalType( 'int', $blog_id );
    497 
    498         $result = update_blog_details( $blog_id, array('domain' => 'example.com', 'path' => 'my_path/') );
    499         $this->assertTrue( $result );
    500 
    501         $blog = get_blog_details( $blog_id );
    502         $this->assertEquals( 'example.com', $blog->domain );
    503         $this->assertEquals( 'my_path/', $blog->path );
    504         $this->assertEquals( '0', $blog->spam );
    505 
    506         $result = update_blog_details( $blog_id, array('domain' => 'example2.com','spam' => 1) );
    507         $this->assertTrue( $result );
    508         $blog = get_blog_details( $blog_id );
    509         $this->assertEquals( 'example2.com', $blog->domain );
    510         $this->assertEquals( 'my_path/', $blog->path );
    511         $this->assertEquals( '1', $blog->spam );
    512 
    513         $result = update_blog_details( $blog_id );
    514         $this->assertFalse( $result );
    515         $blog = get_blog_details( $blog_id );
    516         $this->assertEquals( 'example2.com', $blog->domain );
    517         $this->assertEquals( 'my_path/', $blog->path );
    518         $this->assertEquals( '1', $blog->spam );
    519 
    520         $test_action_counter = 0;
    521 
    522         add_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    523         $result = update_blog_details( $blog_id, array( 'spam' => 0 ) );
    524         $this->assertTrue( $result );
    525         $blog = get_blog_details( $blog_id );
    526         $this->assertEquals( '0', $blog->spam );
    527         $this->assertEquals( 1, $test_action_counter );
    528 
    529         // Same again
    530         $result = update_blog_details( $blog_id, array( 'spam' => 0 ) );
    531         $this->assertTrue( $result );
    532         $blog = get_blog_details( $blog_id );
    533         $this->assertEquals( '0', $blog->spam );
    534         $this->assertEquals( 1, $test_action_counter );
    535         remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    536 
    537         add_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    538         $result = update_blog_details( $blog_id, array( 'spam' => 1 ) );
    539         $this->assertTrue( $result );
    540         $blog = get_blog_details( $blog_id );
    541         $this->assertEquals( '1', $blog->spam );
    542         $this->assertEquals( 2, $test_action_counter );
    543 
    544         // Same again
    545         $result = update_blog_details( $blog_id, array( 'spam' => 1 ) );
    546         $this->assertTrue( $result );
    547         $blog = get_blog_details( $blog_id );
    548         $this->assertEquals( '1', $blog->spam );
    549         $this->assertEquals( 2, $test_action_counter );
    550         remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    551 
    552         add_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    553         $result = update_blog_details( $blog_id, array( 'archived' => 1 ) );
    554         $this->assertTrue( $result );
    555         $blog = get_blog_details( $blog_id );
    556         $this->assertEquals( '1', $blog->archived );
    557         $this->assertEquals( 3, $test_action_counter );
    558 
    559         // Same again
    560         $result = update_blog_details( $blog_id, array( 'archived' => 1 ) );
    561         $this->assertTrue( $result );
    562         $blog = get_blog_details( $blog_id );
    563         $this->assertEquals( '1', $blog->archived );
    564         $this->assertEquals( 3, $test_action_counter );
    565         remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    566 
    567         add_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    568         $result = update_blog_details( $blog_id, array( 'archived' => 0 ) );
    569         $this->assertTrue( $result );
    570         $blog = get_blog_details( $blog_id );
    571         $this->assertEquals( '0', $blog->archived );
    572         $this->assertEquals( 4, $test_action_counter );
    573 
    574         // Same again
    575         $result = update_blog_details( $blog_id, array( 'archived' => 0 ) );
    576         $this->assertTrue( $result );
    577         $blog = get_blog_details( $blog_id );
    578         $this->assertEquals( '0', $blog->archived );
    579         $this->assertEquals( 4, $test_action_counter );
    580         remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    581 
    582         add_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    583         $result = update_blog_details( $blog_id, array( 'deleted' => 1 ) );
    584         $this->assertTrue( $result );
    585         $blog = get_blog_details( $blog_id );
    586         $this->assertEquals( '1', $blog->deleted );
    587         $this->assertEquals( 5, $test_action_counter );
    588 
    589         // Same again
    590         $result = update_blog_details( $blog_id, array( 'deleted' => 1 ) );
    591         $this->assertTrue( $result );
    592         $blog = get_blog_details( $blog_id );
    593         $this->assertEquals( '1', $blog->deleted );
    594         $this->assertEquals( 5, $test_action_counter );
    595         remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    596 
    597         add_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    598         $result = update_blog_details( $blog_id, array( 'deleted' => 0 ) );
    599         $this->assertTrue( $result );
    600         $blog = get_blog_details( $blog_id );
    601         $this->assertEquals( '0', $blog->deleted );
    602         $this->assertEquals( 6, $test_action_counter );
    603 
    604         // Same again
    605         $result = update_blog_details( $blog_id, array( 'deleted' => 0 ) );
    606         $this->assertTrue( $result );
    607         $blog = get_blog_details( $blog_id );
    608         $this->assertEquals( '0', $blog->deleted );
    609         $this->assertEquals( 6, $test_action_counter );
    610         remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    611 
    612         add_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    613         $result = update_blog_details( $blog_id, array( 'mature' => 1 ) );
    614         $this->assertTrue( $result );
    615         $blog = get_blog_details( $blog_id );
    616         $this->assertEquals( '1', $blog->mature );
    617         $this->assertEquals( 7, $test_action_counter );
    618 
    619         // Same again
    620         $result = update_blog_details( $blog_id, array( 'mature' => 1 ) );
    621         $this->assertTrue( $result );
    622         $blog = get_blog_details( $blog_id );
    623         $this->assertEquals( '1', $blog->mature );
    624         $this->assertEquals( 7, $test_action_counter );
    625         remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    626 
    627         add_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    628         $result = update_blog_details( $blog_id, array( 'mature' => 0 ) );
    629         $this->assertTrue( $result );
    630         $blog = get_blog_details( $blog_id );
    631         $this->assertEquals( '0', $blog->mature );
    632         $this->assertEquals( 8, $test_action_counter );
    633 
    634         // Same again
    635         $result = update_blog_details( $blog_id, array( 'mature' => 0 ) );
    636         $this->assertTrue( $result );
    637         $blog = get_blog_details( $blog_id );
    638         $this->assertEquals( '0', $blog->mature );
    639         $this->assertEquals( 8, $test_action_counter );
    640         remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    641     }
    642 
    643     /**
    644      * Test fetching a blog that doesn't exist and again after it exists.
    645      *
    646      * @ticket 23405
    647      */
    648     function test_get_blog_details_blog_does_not_exist() {
    649         global $wpdb;
    650 
    651         $blog_id = $wpdb->get_var( "SELECT MAX(blog_id) FROM $wpdb->blogs" );
    652 
    653         // An idosyncrancy of the unit tests is that the max blog_id gets reset
    654         // to 1 in between test cases but picks up where it previously left off
    655         // on the next insert. If 1 is reported, burn a blog create to get
    656         // the max counter back in sync.
    657         if ( 1 == $blog_id ) {
    658             $blog_id = $this->factory->blog->create();
    659         }
    660         $blog_id++;
    661 
    662         $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
    663         $this->assertFalse( get_blog_details( $blog_id ) );
    664         $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
    665         $this->assertFalse( get_blog_details( $blog_id ) );
    666         $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
    667 
    668         $this->assertEquals( $blog_id, $this->factory->blog->create() );
    669         $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' )  );
    670 
    671         $blog = get_blog_details( $blog_id );
    672         $this->assertEquals( $blog_id, $blog->blog_id );
    673         $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
    674 
    675         wpmu_delete_blog( $blog_id );
    676         $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
    677         $blog->deleted = '1';
    678         $this->assertEQuals( $blog, get_blog_details( $blog_id ) );
    679         $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
    680 
    681         wpmu_delete_blog( $blog_id, true );
    682         $this->assertFalse( get_blog_details( $blog_id ) );
    683         $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
    684     }
    685 
    686     function test_update_blog_status() {
    687         global $test_action_counter;
    688 
    689         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    690         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) );
    691         $this->assertInternalType( 'int', $blog_id );
    692 
    693         $test_action_counter = 0;
    694         $count = 1;
    695 
    696         add_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    697         $result = update_blog_status( $blog_id, 'spam', 0 );
    698         $this->assertEquals( 0, $result );
    699         $blog = get_blog_details( $blog_id );
    700         $this->assertEquals( '0', $blog->spam );
    701         $this->assertEquals( $count, $test_action_counter );
    702 
    703         // Same again
    704         $count++;
    705         $result = update_blog_status( $blog_id, 'spam', 0 );
    706         $this->assertEquals( 0, $result );
    707         $blog = get_blog_details( $blog_id );
    708         $this->assertEquals( '0', $blog->spam );
    709         $this->assertEquals( $count, $test_action_counter );
    710         remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    711 
    712         $count++;
    713         add_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    714         $result = update_blog_status( $blog_id, 'spam', 1 );
    715         $this->assertEquals( 1, $result );
    716         $blog = get_blog_details( $blog_id );
    717         $this->assertEquals( '1', $blog->spam );
    718         $this->assertEquals( $count, $test_action_counter );
    719 
    720         // Same again
    721         $count++;
    722         $result = update_blog_status( $blog_id, 'spam', 1 );
    723         $this->assertEquals( 1, $result );
    724         $blog = get_blog_details( $blog_id );
    725         $this->assertEquals( '1', $blog->spam );
    726         $this->assertEquals( $count, $test_action_counter );
    727         remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    728 
    729         add_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    730         $count++;
    731         $result = update_blog_status( $blog_id, 'archived', 1 );
    732         $this->assertEquals( 1, $result );
    733         $blog = get_blog_details( $blog_id );
    734         $this->assertEquals( '1', $blog->archived );
    735         $this->assertEquals( $count, $test_action_counter );
    736 
    737         // Same again
    738         $count++;
    739         $result = update_blog_status( $blog_id, 'archived', 1 );
    740         $this->assertEquals( 1, $result );
    741         $blog = get_blog_details( $blog_id );
    742         $this->assertEquals( '1', $blog->archived );
    743         $this->assertEquals( $count, $test_action_counter );
    744         remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    745 
    746         add_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    747         $count++;
    748         $result = update_blog_status( $blog_id, 'archived', 0 );
    749         $this->assertEquals( 0, $result );
    750         $blog = get_blog_details( $blog_id );
    751         $this->assertEquals( '0', $blog->archived );
    752         $this->assertEquals( $count, $test_action_counter );
    753 
    754         // Same again
    755         $result = update_blog_status( $blog_id, 'archived', 0 );
    756         $count++;
    757         $this->assertEquals( 0, $result );
    758         $blog = get_blog_details( $blog_id );
    759         $this->assertEquals( '0', $blog->archived );
    760         $this->assertEquals( $count, $test_action_counter );
    761         remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    762 
    763         add_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    764         $count++;
    765         $result = update_blog_status( $blog_id, 'deleted', 1 );
    766         $this->assertEquals( 1, $result );
    767         $blog = get_blog_details( $blog_id );
    768         $this->assertEquals( '1', $blog->deleted );
    769         $this->assertEquals( $count, $test_action_counter );
    770 
    771         // Same again
    772         $count++;
    773         $result = update_blog_status( $blog_id, 'deleted', 1 );
    774         $this->assertEquals( 1, $result );
    775         $blog = get_blog_details( $blog_id );
    776         $this->assertEquals( '1', $blog->deleted );
    777         $this->assertEquals( $count, $test_action_counter );
    778         remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    779 
    780         add_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    781         $count++;
    782         $result = update_blog_status( $blog_id, 'deleted', 0 );
    783         $this->assertEquals( 0, $result );
    784         $blog = get_blog_details( $blog_id );
    785         $this->assertEquals( '0', $blog->deleted );
    786         $this->assertEquals( $count, $test_action_counter );
    787 
    788         // Same again
    789         $count++;
    790         $result = update_blog_status( $blog_id, 'deleted', 0 );
    791         $this->assertEquals( 0, $result );
    792         $blog = get_blog_details( $blog_id );
    793         $this->assertEquals( '0', $blog->deleted );
    794         $this->assertEquals( $count, $test_action_counter );
    795         remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    796 
    797         add_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    798         $count++;
    799         $result = update_blog_status( $blog_id, 'mature', 1 );
    800         $this->assertEquals( 1, $result );
    801         $blog = get_blog_details( $blog_id );
    802         $this->assertEquals( '1', $blog->mature );
    803         $this->assertEquals( $count, $test_action_counter );
    804 
    805         // Same again
    806         $count++;
    807         $result = update_blog_status( $blog_id, 'mature', 1 );
    808         $this->assertEquals( 1, $result );
    809         $blog = get_blog_details( $blog_id );
    810         $this->assertEquals( '1', $blog->mature );
    811         $this->assertEquals( $count, $test_action_counter );
    812         remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    813 
    814         add_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    815         $count++;
    816         $result = update_blog_status( $blog_id, 'mature', 0 );
    817         $this->assertEquals( 0, $result );
    818         $blog = get_blog_details( $blog_id );
    819         $this->assertEquals( '0', $blog->mature );
    820         $this->assertEquals( $count, $test_action_counter );
    821 
    822         // Same again
    823         $count++;
    824         $result = update_blog_status( $blog_id, 'mature', 0 );
    825         $this->assertEquals( 0, $result );
    826         $blog = get_blog_details( $blog_id );
    827         $this->assertEquals( '0', $blog->mature );
    828         $this->assertEquals( $count, $test_action_counter );
    829         remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
    830 
    831         add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 );
    832         $count++;
    833         $result = update_blog_status( $blog_id, 'public', 0 );
    834         $this->assertEquals( 0, $result );
    835         $blog = get_blog_details( $blog_id );
    836         $this->assertEquals( '0', $blog->public );
    837         $this->assertEquals( $count, $test_action_counter );
    838 
    839         // Same again
    840         $count++;
    841         $result = update_blog_status( $blog_id, 'public', 0 );
    842         $this->assertEquals( 0, $result );
    843         $blog = get_blog_details( $blog_id );
    844         $this->assertEquals( '0', $blog->public );
    845         $this->assertEquals( $count, $test_action_counter );
    846         remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 );
    847 
    848         add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 );
    849         $count++;
    850         $result = update_blog_status( $blog_id, 'public', 1 );
    851         $this->assertEquals( 1, $result );
    852         $blog = get_blog_details( $blog_id );
    853         $this->assertEquals( '1', $blog->public );
    854         $this->assertEquals( $count, $test_action_counter );
    855 
    856         // Same again
    857         $count++;
    858         $result = update_blog_status( $blog_id, 'public', 1 );
    859         $this->assertEquals( 1, $result );
    860         $blog = get_blog_details( $blog_id );
    861         $this->assertEquals( '1', $blog->public );
    862         $this->assertEquals( $count, $test_action_counter );
    863         remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 );
    864 
    865         // Updating a dummy field returns the value passed. Go fig.
    866         $result = update_blog_status( $blog_id, 'doesnotexist', 1 );
    867         $this->assertEquals( 1, $result );
    868     }
    869 
    870     function test_switch_restore_blog() {
    871         global $_wp_switched_stack, $wpdb;
    872 
    873         $this->assertEquals( array(), $_wp_switched_stack );
    874         $this->assertFalse( ms_is_switched() );
    875         $current_blog_id = get_current_blog_id();
    876         $this->assertInternalType( 'integer', $current_blog_id );
    877 
    878         wp_cache_set( 'switch-test', $current_blog_id, 'switch-test' );
    879         $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    880 
    881         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    882         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) );
    883 
    884         $cap_key = wp_get_current_user()->cap_key;
    885         switch_to_blog( $blog_id );
    886         $this->assertNotEquals( $cap_key, wp_get_current_user()->cap_key );
    887         $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack );
    888         $this->assertTrue( ms_is_switched() );
    889         $this->assertEquals( $blog_id, $wpdb->blogid );
    890         $this->assertFalse( wp_cache_get( 'switch-test', 'switch-test' ) );
    891         wp_cache_set( 'switch-test', $blog_id, 'switch-test' );
    892         $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    893 
    894         switch_to_blog( $blog_id );
    895         $this->assertEquals( array( $current_blog_id, $blog_id ), $_wp_switched_stack );
    896         $this->assertTrue( ms_is_switched() );
    897         $this->assertEquals( $blog_id, $wpdb->blogid );
    898         $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    899 
    900         restore_current_blog();
    901         $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack );
    902         $this->assertTrue( ms_is_switched() );
    903         $this->assertEquals( $blog_id, $wpdb->blogid );
    904         $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    905 
    906         restore_current_blog();
    907         $this->assertEquals( $cap_key, wp_get_current_user()->cap_key );
    908         $this->assertEquals( $current_blog_id, get_current_blog_id() );
    909         $this->assertEquals( array(), $_wp_switched_stack );
    910         $this->assertFalse( ms_is_switched() );
    911         $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
    912 
    913         $this->assertFalse( restore_current_blog() );
    914     }
    915 
    916     function test_get_blog_post() {
    917         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    918         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) );
    919         $current_blog_id = get_current_blog_id();
    920 
    921         $post_id = $this->factory->post->create();
    922         $this->assertInstanceOf( 'WP_Post', get_post( $post_id ) );
    923         switch_to_blog( $blog_id );
    924         $this->assertNull( get_post( $post_id ) );
    925         $post = get_blog_post( $current_blog_id, $post_id );
    926         $this->assertInstanceOf( 'WP_Post', $post );
    927         $this->assertEquals( $post_id, $post->ID );
    928         restore_current_blog();
    929 
    930         wp_update_post( array( 'ID' => $post_id, 'post_title' => 'A Different Title' ) );
    931         switch_to_blog( $blog_id );
    932         $post = get_blog_post( $current_blog_id, $post_id );
    933         // Make sure cache is good
    934         $this->assertEquals( 'A Different Title', $post->post_title );
    935 
    936         $post_id2 = $this->factory->post->create();
    937         // Test get_blog_post() with currently active blog ID.
    938         $post = get_blog_post( $blog_id, $post_id2 );
    939         $this->assertInstanceOf( 'WP_Post', $post );
    940         $this->assertEquals( $post_id2, $post->ID );
    941         restore_current_blog();
    94238    }
    94339
     
    97268
    97369    }
    974     /**
    975      * @ticket 21552
    976      * @ticket 23418
    977      */
    978     function test_sanitize_ms_options() {
    979         update_site_option( 'illegal_names', array( '', 'Woo', '' ) );
    980         update_site_option( 'limited_email_domains', array(  'woo', '', 'boo.com', 'foo.net.biz..'  ) );
    981         update_site_option( 'banned_email_domains', array(  'woo', '', 'boo.com', 'foo.net.biz..'  ) );
    982 
    983         $this->assertEquals( array( 'Woo' ), get_site_option( 'illegal_names' ) );
    984         $this->assertEquals( array( 'woo', 'boo.com' ), get_site_option( 'limited_email_domains' ) );
    985         $this->assertEquals( array( 'woo', 'boo.com' ), get_site_option( 'banned_email_domains' ) );
    986 
    987         update_site_option( 'illegal_names', 'foo bar' );
    988         update_site_option( 'limited_email_domains', "foo\nbar" );
    989         update_site_option( 'banned_email_domains', "foo\nbar" );
    990 
    991         $this->assertEquals( array( 'foo', 'bar' ), get_site_option( 'illegal_names' ) );
    992         $this->assertEquals( array( 'foo', 'bar' ), get_site_option( 'limited_email_domains' ) );
    993         $this->assertEquals( array( 'foo', 'bar' ), get_site_option( 'banned_email_domains' ) );
    994 
    995         foreach ( array( 'illegal_names', 'limited_email_domains', 'banned_email_domains' ) as $option ) {
    996             update_site_option( $option, array() );
    997             $this->assertSame( '', get_site_option( $option ) );
    998         }
    999     }
    1000 
    1001     function _domain_exists_cb( $exists, $domain, $path, $site_id ) {
    1002         if ( 'foo' == $domain && 'bar/' == $path )
    1003             return 1234;
    1004         else
    1005             return null;
    1006     }
    1007 
    1008     function test_domain_exists() {
    1009         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    1010         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/testdomainexists', 'title' => 'Test Title' ) );
    1011 
    1012         $details = get_blog_details( $blog_id, false );
    1013 
    1014         $this->assertEquals( $blog_id, domain_exists( $details->domain, $details->path ) );
    1015         $this->assertEquals( $blog_id, domain_exists( $details->domain, $details->path, $details->site_id ) );
    1016         $this->assertEquals( null, domain_exists( $details->domain, $details->path, 999 ) );
    1017         $this->assertEquals( null, domain_exists( 'foo', 'bar' ) );
    1018 
    1019         add_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
    1020         $this->assertEquals( 1234, domain_exists( 'foo', 'bar' ) );
    1021         $this->assertEquals( null, domain_exists( 'foo', 'baz' ) );
    1022         $this->assertEquals( null, domain_exists( 'bar', 'foo' ) );
    1023 
    1024         // Make sure the same result is returned with or without a trailing slash
    1025         $this->assertEquals( domain_exists( 'foo', 'bar' ), domain_exists( 'foo', 'bar/' ) );
    1026 
    1027         remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
    1028         $this->assertEquals( null, domain_exists( 'foo', 'bar' ) );
    1029 
    1030         wpmu_delete_blog( $blog_id );
    1031         $this->assertEquals( $blog_id, domain_exists( $details->domain, $details->path ) );
    1032         wpmu_delete_blog( $blog_id, true );
    1033         $this->assertEquals( null, domain_exists( $details->domain, $details->path ) );
    1034     }
    1035 
    1036     function test_get_blog_id_from_url() {
    1037         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    1038         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/testdomainexists', 'title' => 'Test Title' ) );
    1039 
    1040         $details = get_blog_details( $blog_id, false );
    1041 
    1042         $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
    1043         $key = md5( $details->domain . $details->path );
    1044         $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
    1045 
    1046         $this->assertEquals( 0, get_blog_id_from_url( $details->domain, 'foo' ) );
    1047 
    1048         wpmu_delete_blog( $blog_id );
    1049         $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
    1050         wpmu_delete_blog( $blog_id, true );
    1051 
    1052         $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    1053         $this->assertEquals( 0, get_blog_id_from_url( $details->domain, $details->path ) );
    1054     }
    1055 
    1056     function test_is_main_site() {
    1057         $this->assertTrue( is_main_site() );
    1058         $this->assertTrue( is_main_site( get_current_blog_id() ) );
    1059 
    1060         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    1061         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id ) );
    1062 
    1063         switch_to_blog( $blog_id  );
    1064         $this->assertFalse( is_main_site( $blog_id ) );
    1065         $this->assertFalse( is_main_site( get_current_blog_id() ) );
    1066         $this->assertFalse( is_main_site() );
    1067 
    1068         restore_current_blog();
    1069     }
    1070 
    1071     function test_switch_upload_dir() {
    1072         $this->assertTrue( is_main_site() );
    1073 
    1074         $site = get_current_site();
    1075 
    1076         $info = wp_upload_dir();
    1077         $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
    1078         $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
    1079         $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
    1080         $this->assertEquals( '', $info['error'] );
    1081 
    1082         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    1083         $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id ) );
    1084 
    1085         switch_to_blog( $blog_id );
    1086         $info = wp_upload_dir();
    1087         $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime('%Y/%m'), $info['url'] );
    1088         $this->assertEquals( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime('%Y/%m'), $info['path'] );
    1089         $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
    1090         $this->assertEquals( '', $info['error'] );
    1091         restore_current_blog();
    1092 
    1093         $info = wp_upload_dir();
    1094         $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
    1095         $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
    1096         $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
    1097         $this->assertEquals( '', $info['error'] );
    1098 
    1099         update_site_option( 'ms_files_rewriting', 1 );
    1100         ms_upload_constants();
    1101 
    1102         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    1103         $blog_id2 = $this->factory->blog->create( array( 'user_id' => $user_id ) );
    1104         $info = wp_upload_dir();
    1105         $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
    1106         $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
    1107         $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
    1108         $this->assertEquals( '', $info['error'] );
    1109 
    1110         switch_to_blog( $blog_id2 );
    1111         $info2 = wp_upload_dir();
    1112         $this->assertNotEquals( $info, $info2 );
    1113         $this->assertEquals( get_option( 'siteurl' )  . '/wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . gmstrftime('%Y/%m'), $info2['url'] );
    1114         $this->assertEquals( ABSPATH . 'wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . gmstrftime('%Y/%m'), $info2['path'] );
    1115         $this->assertEquals( gmstrftime('/%Y/%m'), $info2['subdir'] );
    1116         $this->assertEquals( '', $info2['error'] );
    1117         restore_current_blog();
    1118         update_site_option( 'ms_files_rewriting', 0 );
    1119     }
    1120 
    1121     /**
    1122      * @ticket 23192
    1123      */
    1124     function test_is_user_spammy() {
    1125         $user_id = $this->factory->user->create( array(
    1126             'role' => 'author',
    1127             'user_login' => 'testuser1',
    1128         ) );
    1129 
    1130         $spam_username = (string) $user_id;
    1131         $spam_user_id = $this->factory->user->create( array(
    1132             'role' => 'author',
    1133             'user_login' => $spam_username,
    1134         ) );
    1135         update_user_status( $spam_user_id, 'spam', '1' );
    1136 
    1137         $this->assertTrue( is_user_spammy( $spam_username ) );
    1138         $this->assertFalse( is_user_spammy( 'testuser1' ) );
    1139     }
    1140 
    1141     /**
    1142      * @ticket 14511
    1143      */
    1144     function test_wp_get_sites() {
    1145         $this->factory->blog->create_many( 2, array( 'site_id' => 2, 'meta' => array( 'public' => 1 ) ) );
    1146         $this->factory->blog->create_many( 3, array( 'site_id' => 3, 'meta' => array( 'public' => 0 ) ) );
    1147 
    1148         // Expect no sites when passed an invalid network_id
    1149         $this->assertCount( 0, wp_get_sites( array( 'network_id' => 0 ) ) );
    1150         $this->assertCount( 0, wp_get_sites( array( 'network_id' => 4 ) ) );
    1151 
    1152         // Expect 1 site when no network_id is specified - defaults to current network.
    1153         $this->assertCount( 1, wp_get_sites() );
    1154         // Expect 6 sites when network_id = null.
    1155         $this->assertCount( 6, wp_get_sites( array( 'network_id' => null ) ) );
    1156 
    1157         // Expect 1 site with a network_id of 1, 2 for network_id 2, 3 for 3
    1158         $this->assertCount( 1, wp_get_sites( array( 'network_id' => 1 ) ) );
    1159         $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2 ) ) );
    1160         $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3 ) ) );
    1161 
    1162         // Expect 6 sites when public is null (across all networks)
    1163         $this->assertCount( 6, wp_get_sites( array( 'public' => null, 'network_id' => null ) ) );
    1164 
    1165         // Expect 3 sites when public is 1
    1166         $this->assertCount( 3, wp_get_sites( array( 'public' => 1, 'network_id' => null ) ) );
    1167 
    1168         // Expect 2 sites when public is 1 and network_id is 2
    1169         $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2, 'public' => 1 ) ) );
    1170 
    1171         // Expect no sites when public is set to 0 and network_id is not 3
    1172         $this->assertCount( 0, wp_get_sites( array( 'network_id' => 1, 'public' => 0 ) ) );
    1173 
    1174         // Test public + network_id = 3
    1175         $this->assertCount( 0, wp_get_sites( array( 'network_id' => 3, 'public' => 1 ) ) );
    1176         $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3, 'public' => 0 ) ) );
    1177     }
    1178 
    1179     /**
    1180      * @ticket 14511
    1181      */
    1182     function test_wp_get_sites_limit_offset() {
    1183         // Create 4 more sites (in addition to the default one)
    1184         $this->factory->blog->create_many( 4, array( 'meta' => array( 'public' => 1 ) ) );
    1185 
    1186         // Expect all 5 sites when no limit/offset is specified
    1187         $this->assertCount( 5, wp_get_sites() );
    1188 
    1189         // Expect first 2 sites when using limit
    1190         $this->assertCount( 2, wp_get_sites( array( 'limit' => 2 ) ) );
    1191 
    1192         // Expect only the last 3 sites when using offset of 2 (limit will default to 100)
    1193         $this->assertCount( 3, wp_get_sites( array( 'offset' => 2 ) ) );
    1194 
    1195         // Expect only the last 1 site when using offset of 4 and limit of 2
    1196         $this->assertCount( 1, wp_get_sites( array( 'limit' => 2, 'offset' => 4 ) ) );
    1197 
    1198         // Expect 0 sites when using an offset larger than the number of sites
    1199         $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) );
    1200     }
    1201 
    1202     /**
    1203      * @ticket 27003
    1204      */
    1205     function test_get_network_by_path() {
    1206         global $wpdb;
    1207 
    1208         $ids = array(
    1209             'wordpress.org/'         => array( 'domain' => 'wordpress.org', 'path' => '/' ),
    1210             'wordpress.org/one/'     => array( 'domain' => 'wordpress.org', 'path' => '/one/' ),
    1211             'wordpress.net/'         => array( 'domain' => 'wordpress.net', 'path' => '/' ),
    1212             'www.wordpress.net/'     => array( 'domain' => 'www.wordpress.net', 'path' => '/' ),
    1213             'www.wordpress.net/two/' => array( 'domain' => 'www.wordpress.net', 'path' => '/two/' ),
    1214             'wordpress.net/three/'   => array( 'domain' => 'wordpress.net', 'path' => '/three/' ),
    1215         );
    1216 
    1217         foreach ( $ids as &$id ) {
    1218             $id = $this->factory->network->create( $id );
    1219         }
    1220         unset( $id );
    1221 
    1222         $this->assertEquals( $ids['www.wordpress.net/'],
    1223             get_network_by_path( 'www.wordpress.net', '/notapath/' )->id );
    1224 
    1225         $this->assertEquals( $ids['www.wordpress.net/two/'],
    1226             get_network_by_path( 'www.wordpress.net', '/two/' )->id );
    1227 
    1228         // This should find /one/ despite the www.
    1229         $this->assertEquals( $ids['wordpress.org/one/'],
    1230             get_network_by_path( 'www.wordpress.org', '/one/' )->id );
    1231 
    1232         // This should not find /one/ because the domains don't match.
    1233         $this->assertEquals( $ids['wordpress.org/'],
    1234             get_network_by_path( 'site1.wordpress.org', '/one/' )->id );
    1235 
    1236         $this->assertEquals( $ids['wordpress.net/three/'],
    1237             get_network_by_path( 'wordpress.net', '/three/' )->id );
    1238 
    1239         $this->assertEquals( $ids['wordpress.net/'],
    1240             get_network_by_path( 'wordpress.net', '/notapath/' )->id );
    1241 
    1242         $this->assertEquals( $ids['wordpress.net/'],
    1243             get_network_by_path( 'site1.wordpress.net', '/notapath/' )->id );
    1244 
    1245         $this->assertEquals( $ids['wordpress.net/'],
    1246             get_network_by_path( 'site1.wordpress.net', '/three/' )->id );
    1247     }
    1248 
    1249     /**
    1250      * @ticket 27003
    1251      * @ticket 27927
    1252      */
    1253     function test_get_site_by_path() {
    1254         $ids = array(
    1255             'wordpress.org/'              => array( 'domain' => 'wordpress.org',      'path' => '/' ),
    1256             'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',      'path' => '/foo/' ),
    1257             'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/' ),
    1258             'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
    1259             'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/' ),
    1260             'www.w.org/'                  => array( 'domain' => 'www.w.org',          'path' => '/' ),
    1261             'www.w.org/foo/'              => array( 'domain' => 'www.w.org',          'path' => '/foo/' ),
    1262             'www.w.org/foo/bar/'          => array( 'domain' => 'www.w.org',          'path' => '/foo/bar/' ),
    1263         );
    1264 
    1265         foreach ( $ids as &$id ) {
    1266             $id = $this->factory->blog->create( $id );
    1267         }
    1268         unset( $id );
    1269 
    1270         $this->assertEquals( $ids['wordpress.org/'],
    1271             get_site_by_path( 'wordpress.org', '/notapath/' )->blog_id );
    1272 
    1273         $this->assertEquals( $ids['wordpress.org/'],
    1274             get_site_by_path( 'www.wordpress.org', '/notapath/' )->blog_id );
    1275 
    1276         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
    1277             get_site_by_path( 'wordpress.org', '/foo/bar/baz/' )->blog_id );
    1278 
    1279         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
    1280             get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/' )->blog_id );
    1281 
    1282         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
    1283             get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 3 )->blog_id );
    1284 
    1285         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
    1286             get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 3 )->blog_id );
    1287 
    1288         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
    1289             get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 2 )->blog_id );
    1290 
    1291         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
    1292             get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 2 )->blog_id );
    1293 
    1294         $this->assertEquals( $ids['wordpress.org/foo/'],
    1295             get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 1 )->blog_id );
    1296 
    1297         $this->assertEquals( $ids['wordpress.org/foo/'],
    1298             get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 1 )->blog_id );
    1299 
    1300         $this->assertEquals( $ids['wordpress.org/'],
    1301             get_site_by_path( 'wordpress.org', '/', 0 )->blog_id );
    1302 
    1303         $this->assertEquals( $ids['wordpress.org/'],
    1304             get_site_by_path( 'www.wordpress.org', '/', 0 )->blog_id );
    1305 
    1306         $this->assertEquals( $ids['make.wordpress.org/foo/'],
    1307             get_site_by_path( 'make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id );
    1308 
    1309         $this->assertEquals( $ids['make.wordpress.org/foo/'],
    1310             get_site_by_path( 'www.make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id );
    1311 
    1312         $this->assertEquals( $ids['www.w.org/'],
    1313             get_site_by_path( 'www.w.org', '/', 0 )->blog_id );
    1314 
    1315         $this->assertEquals( $ids['www.w.org/'],
    1316             get_site_by_path( 'www.w.org', '/notapath/' )->blog_id );
    1317 
    1318         $this->assertEquals( $ids['www.w.org/foo/bar/'],
    1319             get_site_by_path( 'www.w.org', '/foo/bar/baz/' )->blog_id );
    1320 
    1321         $this->assertEquals( $ids['www.w.org/foo/'],
    1322             get_site_by_path( 'www.w.org', '/foo/bar/baz/', 1 )->blog_id );
    1323 
    1324         // A site installed with www will not be found by the root domain.
    1325         $this->assertFalse( get_site_by_path( 'w.org', '/' ) );
    1326         $this->assertFalse( get_site_by_path( 'w.org', '/notapath/' ) );
    1327         $this->assertFalse( get_site_by_path( 'w.org', '/foo/bar/baz/' ) );
    1328         $this->assertFalse( get_site_by_path( 'w.org', '/foo/bar/baz/', 1 ) );
    1329 
    1330         // A site will not be found by its root domain when an invalid subdomain is requested.
    1331         $this->assertFalse( get_site_by_path( 'invalid.wordpress.org', '/' ) );
    1332         $this->assertFalse( get_site_by_path( 'invalid.wordpress.org', '/foo/bar/' ) );
    1333     }
    1334 
    1335     /**
    1336      * @ticket 20601
    1337      */
    1338     function test_user_member_of_blog() {
    1339         global $wp_rewrite;
    1340 
    1341         $this->factory->blog->create();
    1342         $user_id = $this->factory->user->create();
    1343         $this->factory->blog->create( array( 'user_id' => $user_id ) );
    1344 
    1345         $blogs = get_blogs_of_user( $user_id );
    1346         $this->assertCount( 2, $blogs );
    1347         $first = reset( $blogs )->userblog_id;
    1348         remove_user_from_blog( $user_id, $first );
    1349 
    1350         $blogs = get_blogs_of_user( $user_id );
    1351         $second = reset( $blogs )->userblog_id;
    1352         $this->assertCount( 1, $blogs );
    1353 
    1354         switch_to_blog( $first );
    1355         $wp_rewrite->init();
    1356 
    1357         $this->go_to( get_author_posts_url( $user_id ) );
    1358         $this->assertQueryTrue( 'is_404' );
    1359 
    1360         switch_to_blog( $second );
    1361         $wp_rewrite->init();
    1362 
    1363         $this->go_to( get_author_posts_url( $user_id ) );
    1364         $this->assertQueryTrue( 'is_author', 'is_archive' );
    1365 
    1366         add_user_to_blog( $first, $user_id, 'administrator' );
    1367         $blogs = get_blogs_of_user( $user_id );
    1368         $this->assertCount( 2, $blogs );
    1369 
    1370         switch_to_blog( $first );
    1371         $wp_rewrite->init();
    1372 
    1373         $this->go_to( get_author_posts_url( $user_id ) );
    1374         $this->assertQueryTrue( 'is_author', 'is_archive' );
    1375     }
    1376 
    1377     /**
    1378      * @ticket 27205
    1379      */
    1380     function test_granting_super_admins() {
    1381         if ( isset( $GLOBALS['super_admins'] ) ) {
    1382             $old_global = $GLOBALS['super_admins'];
    1383             unset( $GLOBALS['super_admins'] );
    1384         }
    1385 
    1386         $user_id = $this->factory->user->create();
    1387 
    1388         $this->assertFalse( is_super_admin( $user_id ) );
    1389         $this->assertFalse( revoke_super_admin( $user_id ) );
    1390         $this->assertTrue( grant_super_admin( $user_id ) );
    1391         $this->assertTrue( is_super_admin( $user_id ) );
    1392         $this->assertFalse( grant_super_admin( $user_id ) );
    1393         $this->assertTrue( revoke_super_admin( $user_id ) );
    1394 
    1395         // None of these operations should set the $super_admins global.
    1396         $this->assertFalse( isset( $GLOBALS['super_admins'] ) );
    1397 
    1398         // Try with two users.
    1399         $second_user = $this->factory->user->create();
    1400         $this->assertTrue( grant_super_admin( $user_id ) );
    1401         $this->assertTrue( grant_super_admin( $second_user ) );
    1402         $this->assertTrue( is_super_admin( $second_user ) );
    1403         $this->assertTrue( is_super_admin( $user_id ) );
    1404         $this->assertTrue( revoke_super_admin( $user_id ) );
    1405         $this->assertTrue( revoke_super_admin( $second_user ) );
    1406 
    1407         if ( isset( $old_global ) ) {
    1408             $GLOBALS['super_admins'] = $old_global;
    1409         }
    1410     }
    1411 
    1412     /**
    1413      * @ticket 27952
    1414      */
    1415     function test_posts_count() {
    1416         $this->factory->post->create();
    1417         $post2 = $this->factory->post->create();
    1418         $this->assertEquals( 2, get_blog_details()->post_count );
    1419 
    1420         wp_delete_post( $post2 );
    1421         $this->assertEquals( 1, get_blog_details()->post_count );
    1422     }
    1423 
    1424     /**
    1425      * @ticket 26410
    1426      */
    1427     function test_blog_details_cache_invalidation() {
    1428         update_option( 'blogname', 'foo' );
    1429         $details = get_blog_details( get_current_blog_id() );
    1430         $this->assertEquals( 'foo', $details->blogname );
    1431 
    1432         update_option( 'blogname', 'bar' );
    1433         $details = get_blog_details( get_current_blog_id() );
    1434         $this->assertEquals( 'bar', $details->blogname );
    1435     }
    1436 
    1437     /**
    1438      * @ticket 29845
    1439      */
    1440     function test_get_blog_details() {
    1441         $network_ids = array(
    1442             'wordpress.org/'         => array( 'domain' => 'wordpress.org', 'path' => '/' ),
    1443             'make.wordpress.org/'    => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
    1444         );
    1445 
    1446         foreach ( $network_ids as &$id ) {
    1447             $id = $this->factory->network->create( $id );
    1448         }
    1449         unset( $id );
    1450 
    1451         $ids = array(
    1452             'wordpress.org/'              => array( 'domain' => 'wordpress.org',      'path' => '/',         'title' => 'Test 1', 'site_id' => $network_ids['wordpress.org/'] ),
    1453             'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',      'path' => '/foo/',     'title' => 'Test 2', 'site_id' => $network_ids['wordpress.org/'] ),
    1454             'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/', 'title' => 'Test 3', 'site_id' => $network_ids['wordpress.org/'] ),
    1455             'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/',         'title' => 'Test 4', 'site_id' => $network_ids['make.wordpress.org/'] ),
    1456             'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/',     'title' => 'Test 5', 'site_id' => $network_ids['make.wordpress.org/'] ),
    1457         );
    1458 
    1459         foreach ( $ids as &$id ) {
    1460             $id = $this->factory->blog->create( $id );
    1461         }
    1462         unset( $id );
    1463 
    1464         // Retrieve site details by passing only a blog ID.
    1465         $site = get_blog_details( $ids['wordpress.org/'] );
    1466         $this->assertEquals( $ids['wordpress.org/'], $site->blog_id );
    1467         $this->assertEquals( 'Test 1', $site->blogname );
    1468 
    1469         $site = get_blog_details( $ids['wordpress.org/foo/'] );
    1470         $this->assertEquals( $ids['wordpress.org/foo/'], $site->blog_id );
    1471         $this->assertEquals( 'Test 2', $site->blogname );
    1472 
    1473         $site = get_blog_details( 999 );
    1474         $this->assertFalse( $site );
    1475 
    1476         // Retrieve site details by passing an array containing blog_id.
    1477         $site = get_blog_details( array( 'blog_id' => $ids['wordpress.org/foo/bar/'] ) );
    1478         $this->assertEquals( $ids['wordpress.org/foo/bar/'], $site->blog_id );
    1479         $this->assertEquals( 'Test 3', $site->blogname );
    1480 
    1481         $site = get_blog_details( array( 'blog_id' => $ids['make.wordpress.org/'] ) );
    1482         $this->assertEquals( $ids['make.wordpress.org/'], $site->blog_id );
    1483         $this->assertEquals( 'Test 4', $site->blogname );
    1484 
    1485         $site = get_blog_details( array( 'blog_id' => 999 ) );
    1486         $this->assertFalse( $site );
    1487 
    1488         // Retrieve site details by passing an array containing domain and path.
    1489         $site = get_blog_details( array( 'domain' => 'wordpress.org', 'path' => '/' ) );
    1490         $this->assertEquals( $ids['wordpress.org/'], $site->blog_id );
    1491         $this->assertEquals( 'Test 1', $site->blogname );
    1492 
    1493         $site = get_blog_details( array( 'domain' => 'wordpress.org', 'path' => '/foo/' ) );
    1494         $this->assertEquals( $ids['wordpress.org/foo/'], $site->blog_id );
    1495         $this->assertEquals( 'Test 2', $site->blogname );
    1496 
    1497         $site = get_blog_details( array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/' ) );
    1498         $this->assertEquals( $ids['wordpress.org/foo/bar/'], $site->blog_id );
    1499         $this->assertEquals( 'Test 3', $site->blogname );
    1500 
    1501         $site = get_blog_details( array( 'domain' => 'make.wordpress.org', 'path' => '/' ) );
    1502         $this->assertEquals( $ids['make.wordpress.org/'], $site->blog_id );
    1503         $this->assertEquals( 'Test 4', $site->blogname );
    1504 
    1505         $site = get_blog_details( array( 'domain' => 'make.wordpress.org', 'path' => '/foo/' ) );
    1506         $this->assertEquals( $ids['make.wordpress.org/foo/'], $site->blog_id );
    1507         $this->assertEquals( 'Test 5', $site->blogname );
    1508 
    1509         $site = get_blog_details( array( 'domain' => 'wordpress.org', 'path' => '/zxy/' ) );
    1510         $this->assertFalse( $site );
    1511     }
    1512 
    1513     /**
    1514      * @ticket 27884
    1515      */
    1516     function test_multisite_bootstrap() {
    1517         global $current_blog;
    1518 
    1519         $network_ids = array(
    1520             'wordpress.org/'         => array( 'domain' => 'wordpress.org', 'path' => '/' ),
    1521             'make.wordpress.org/'    => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
    1522         );
    1523 
    1524         foreach ( $network_ids as &$id ) {
    1525             $id = $this->factory->network->create( $id );
    1526         }
    1527         unset( $id );
    1528 
    1529         $ids = array(
    1530             'wordpress.org/'              => array( 'domain' => 'wordpress.org',      'path' => '/',         'site_id' => $network_ids['wordpress.org/'] ),
    1531             'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',      'path' => '/foo/',     'site_id' => $network_ids['wordpress.org/'] ),
    1532             'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/', 'site_id' => $network_ids['wordpress.org/'] ),
    1533             'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/',         'site_id' => $network_ids['make.wordpress.org/'] ),
    1534             'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/',     'site_id' => $network_ids['make.wordpress.org/'] ),
    1535         );
    1536 
    1537         foreach ( $ids as &$id ) {
    1538             $id = $this->factory->blog->create( $id );
    1539         }
    1540         unset( $id );
    1541 
    1542         $this->_setup_host_request( 'wordpress.org', '/' );
    1543         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    1544         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1545 
    1546         $this->_setup_host_request( 'wordpress.org', '/2014/04/23/hello-world/' );
    1547         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    1548         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1549 
    1550         $this->_setup_host_request( 'wordpress.org', '/sample-page/' );
    1551         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    1552         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1553 
    1554         $this->_setup_host_request( 'wordpress.org', '/?p=1' );
    1555         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    1556         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1557 
    1558         $this->_setup_host_request( 'wordpress.org', '/wp-admin/' );
    1559         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    1560         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1561 
    1562         $this->_setup_host_request( 'wordpress.org', '/foo/' );
    1563         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    1564         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1565 
    1566         $this->_setup_host_request( 'wordpress.org', '/FOO/' );
    1567         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    1568         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1569 
    1570         $this->_setup_host_request( 'wordpress.org', '/foo/2014/04/23/hello-world/' );
    1571         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    1572         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1573 
    1574         $this->_setup_host_request( 'wordpress.org', '/foo/sample-page/' );
    1575         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    1576         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1577 
    1578         $this->_setup_host_request( 'wordpress.org', '/foo/?p=1' );
    1579         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    1580         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1581 
    1582         $this->_setup_host_request( 'wordpress.org', '/foo/wp-admin/' );
    1583         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    1584         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1585 
    1586         // @todo not currently passing.
    1587         //$this->_setup_host_request( 'wordpress.org', '/foo/bar/' );
    1588         //$this->assertEquals( $ids['wordpress.org/foo/bar/'], $current_blog->blog_id );
    1589         //$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    1590 
    1591         $this->_setup_host_request( 'make.wordpress.org', '/' );
    1592         $this->assertEquals( $ids['make.wordpress.org/'], $current_blog->blog_id );
    1593         $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
    1594 
    1595         $this->_setup_host_request( 'make.wordpress.org', '/foo/' );
    1596         $this->assertEquals( $ids['make.wordpress.org/foo/'], $current_blog->blog_id );
    1597         $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
    1598 
    1599         // Request the original tests domain and path to unpollute the stack.
    1600         $this->_setup_host_request( WP_TESTS_DOMAIN, '/' );
    1601     }
    1602 
    1603     /**
    1604      * Reset various globals required for a 'clean' multisite boot.
    1605      *
    1606      * The $wpdb and $table_prefix globals are required for ms-settings.php to
    1607      * load properly.
    1608      *
    1609      * @param string $domain HTTP_HOST of the bootstrap request.
    1610      * @param string $path   REQUEST_URI of the boot strap request.
    1611      */
    1612     function _setup_host_request( $domain, $path ) {
    1613         global $current_site, $current_blog, $table_prefix, $wpdb;
    1614 
    1615         $table_prefix = WP_TESTS_TABLE_PREFIX;
    1616         $current_site = $current_blog = null;
    1617         $_SERVER['HTTP_HOST'] = $domain;
    1618         $_SERVER['REQUEST_URI'] = $path;
    1619 
    1620         include ABSPATH . '/wp-includes/ms-settings.php';
    1621     }
    162270}
    162371
  • trunk/tests/phpunit/tests/option/multisite.php

    r29881 r29916  
    22
    33if ( is_multisite() ) :
     4
    45/**
     6 * Tests specific to network and site options in Multisite.
     7 *
    58 * @group option
     9 * @group ms-option
     10 * @group multisite
    611 */
    7 class Tests_Option_BlogOption extends WP_UnitTestCase {
     12class Tests_Multisite_Option extends WP_UnitTestCase {
    813    protected $suppress = false;
    914
     
    151156        //$this->assertFalse( get_option( $key2 ) ); // check get_option()
    152157    }
     158
     159    /**
     160     * @group multisite
     161     */
     162    function test_site_notoptions() {
     163        global $wpdb;
     164        $notoptions_key = "{$wpdb->siteid}:notoptions";
     165
     166        $_notoptions = wp_cache_get( 'notoptions', 'site-options' );
     167        $this->assertEmpty( $_notoptions );
     168        $_notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
     169        $this->assertEmpty( $_notoptions1 );
     170
     171        get_site_option( 'burrito' );
     172
     173        $notoptions = wp_cache_get( 'notoptions', 'site-options' );
     174        $this->assertEmpty( $notoptions );
     175        $notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
     176        $this->assertNotEmpty( $notoptions1 );
     177    }
     178
     179    function test_users_can_register_signup_filter() {
     180
     181        $registration = get_site_option('registration');
     182        $this->assertFalse( users_can_register_signup_filter() );
     183
     184        update_site_option('registration', 'all');
     185        $this->assertTrue( users_can_register_signup_filter() );
     186
     187        update_site_option('registration', 'user');
     188        $this->assertTrue( users_can_register_signup_filter() );
     189
     190        update_site_option('registration', 'none');
     191        $this->assertFalse( users_can_register_signup_filter() );
     192    }
     193
     194    /**
     195     * @ticket 21552
     196     * @ticket 23418
     197     */
     198    function test_sanitize_ms_options() {
     199        update_site_option( 'illegal_names', array( '', 'Woo', '' ) );
     200        update_site_option( 'limited_email_domains', array(  'woo', '', 'boo.com', 'foo.net.biz..'  ) );
     201        update_site_option( 'banned_email_domains', array(  'woo', '', 'boo.com', 'foo.net.biz..'  ) );
     202
     203        $this->assertEquals( array( 'Woo' ), get_site_option( 'illegal_names' ) );
     204        $this->assertEquals( array( 'woo', 'boo.com' ), get_site_option( 'limited_email_domains' ) );
     205        $this->assertEquals( array( 'woo', 'boo.com' ), get_site_option( 'banned_email_domains' ) );
     206
     207        update_site_option( 'illegal_names', 'foo bar' );
     208        update_site_option( 'limited_email_domains', "foo\nbar" );
     209        update_site_option( 'banned_email_domains', "foo\nbar" );
     210
     211        $this->assertEquals( array( 'foo', 'bar' ), get_site_option( 'illegal_names' ) );
     212        $this->assertEquals( array( 'foo', 'bar' ), get_site_option( 'limited_email_domains' ) );
     213        $this->assertEquals( array( 'foo', 'bar' ), get_site_option( 'banned_email_domains' ) );
     214
     215        foreach ( array( 'illegal_names', 'limited_email_domains', 'banned_email_domains' ) as $option ) {
     216            update_site_option( $option, array() );
     217            $this->assertSame( '', get_site_option( $option ) );
     218        }
     219    }
    153220}
     221
    154222endif;
  • trunk/tests/phpunit/tests/option/siteOption.php

    r26305 r29916  
    9191        $this->assertFalse( get_site_option( $option ) );
    9292    }
    93 
    94     /**
    95      * @group multisite
    96      */
    97     function test_site_notoptions() {
    98         if ( ! is_multisite() ) {
    99             $this->markTestSkipped( 'Should only run in multisite' );
    100         }
    101 
    102         global $wpdb;
    103         $notoptions_key = "{$wpdb->siteid}:notoptions";
    104 
    105         $_notoptions = wp_cache_get( 'notoptions', 'site-options' );
    106         $this->assertEmpty( $_notoptions );
    107         $_notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
    108         $this->assertEmpty( $_notoptions1 );
    109 
    110         get_site_option( 'burrito' );
    111 
    112         $notoptions = wp_cache_get( 'notoptions', 'site-options' );
    113         $this->assertEmpty( $notoptions );
    114         $notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
    115         $this->assertNotEmpty( $notoptions1 );
    116     }
    11793}
Note: See TracChangeset for help on using the changeset viewer.