Ticket #29896: 29896.diff
File 29896.diff, 99.3 KB (added by , 6 years ago) |
---|
-
tests/phpunit/tests/ms.php
25 25 $wpdb->suppress_errors( $this->suppress ); 26 26 } 27 27 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 28 /** 43 29 * @ticket 22917 44 30 */ … … 96 82 remove_filter( 'enable_live_network_counts', '__return_true' ); 97 83 } 98 84 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 = false108 $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 #20950112 $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 = true121 $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 cache126 $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 else139 $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 blogs149 foreach ( $blog_ids as $blog_id ) {150 // drop tables for every second blog151 $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 else171 $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_user221 */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 85 function test_active_network_plugins() { 277 86 $path = "hello.php"; 278 87 … … 371 180 $this->assertFalse( users_can_register_signup_filter() ); 372 181 } 373 182 374 /**375 * @expectedDeprecated get_dashboard_blog376 */377 function test_get_dashboard_blog() {378 // if there is no dashboard blog set, current blog is used379 $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 one387 update_site_option( 'dashboard_blog', $blog_id );388 $dashboard_blog = get_dashboard_blog();389 $this->assertEquals( $blog_id, $dashboard_blog->blog_id );390 }391 392 183 function test_wpmu_log_new_registrations() { 393 184 global $wpdb; 394 185 … … 450 241 $this->assertFalse( is_upload_space_available() ); 451 242 } 452 243 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 < 2459 $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 blog475 $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 again530 $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 again545 $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 again560 $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 again575 $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 again590 $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 again605 $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 again620 $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 again635 $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 244 /** 644 * Test fetching a blog that doesn't exist and again after it exists.645 *646 * @ticket 23405647 */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 reset654 // to 1 in between test cases but picks up where it previously left off655 // on the next insert. If 1 is reported, burn a blog create to get656 // 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 again704 $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 again721 $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 again738 $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 again755 $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 again772 $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 again789 $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 again806 $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 again823 $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 again840 $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 again857 $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 good934 $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();942 }943 944 /**945 245 * @ticket 21570 946 246 */ 947 247 function test_aggressiveness_of_is_email_address_unsafe() { … … 1033 333 $this->assertEquals( null, domain_exists( $details->domain, $details->path ) ); 1034 334 } 1035 335 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 336 function test_is_main_site() { 1057 337 $this->assertTrue( is_main_site() ); 1058 338 $this->assertTrue( is_main_site( get_current_blog_id() ) ); … … 1119 399 } 1120 400 1121 401 /** 1122 * @ticket 231921123 */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 145111143 */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_id1149 $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 31158 $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 11166 $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 21169 $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 31172 $this->assertCount( 0, wp_get_sites( array( 'network_id' => 1, 'public' => 0 ) ) );1173 1174 // Test public + network_id = 31175 $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 145111181 */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 specified1187 $this->assertCount( 5, wp_get_sites() );1188 1189 // Expect first 2 sites when using limit1190 $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 21196 $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 sites1199 $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) );1200 }1201 1202 /**1203 * @ticket 270031204 */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 270031251 * @ticket 279271252 */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 206011337 */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 402 * @ticket 27205 1379 403 */ 1380 404 function test_granting_super_admins() { … … 1408 432 $GLOBALS['super_admins'] = $old_global; 1409 433 } 1410 434 } 1411 1412 /**1413 * @ticket 279521414 */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 264101426 */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 298451439 */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 278841515 */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 1600 /**1601 * Reset various globals required for a 'clean' multisite boot.1602 *1603 * The $wpdb and $table_prefix globals are required for ms-settings.php to1604 * load properly.1605 *1606 * @param string $domain HTTP_HOST of the bootstrap request.1607 * @param string $path REQUEST_URI of the boot strap request.1608 */1609 function _setup_host_request( $domain, $path ) {1610 global $current_site, $current_blog, $table_prefix, $wpdb;1611 1612 $table_prefix = WP_TESTS_TABLE_PREFIX;1613 $current_site = $current_blog = null;1614 $_SERVER['HTTP_HOST'] = $domain;1615 $_SERVER['REQUEST_URI'] = $path;1616 1617 include ABSPATH . '/wp-includes/ms-settings.php';1618 }1619 435 } 1620 436 1621 437 endif; -
tests/phpunit/tests/multisite/bootstrap.php
1 <?php 2 3 if ( is_multisite() ) : 4 5 /** 6 * @group ms-bootstrap 7 * @group multisite 8 */ 9 class Tests_Multisite_Bootstrap extends WP_UnitTestCase { 10 protected $suppress = false; 11 12 function setUp() { 13 global $wpdb; 14 parent::setUp(); 15 $this->suppress = $wpdb->suppress_errors(); 16 17 $_SERVER['REMOTE_ADDR'] = ''; 18 } 19 20 function tearDown() { 21 global $wpdb; 22 parent::tearDown(); 23 $wpdb->suppress_errors( $this->suppress ); 24 } 25 26 /** 27 * @ticket 27003 28 */ 29 function test_get_network_by_path() { 30 global $wpdb; 31 32 $ids = array( 33 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), 34 'wordpress.org/one/' => array( 'domain' => 'wordpress.org', 'path' => '/one/' ), 35 'wordpress.net/' => array( 'domain' => 'wordpress.net', 'path' => '/' ), 36 'www.wordpress.net/' => array( 'domain' => 'www.wordpress.net', 'path' => '/' ), 37 'www.wordpress.net/two/' => array( 'domain' => 'www.wordpress.net', 'path' => '/two/' ), 38 'wordpress.net/three/' => array( 'domain' => 'wordpress.net', 'path' => '/three/' ), 39 ); 40 41 foreach ( $ids as &$id ) { 42 $id = $this->factory->network->create( $id ); 43 } 44 unset( $id ); 45 46 $this->assertEquals( $ids['www.wordpress.net/'], 47 get_network_by_path( 'www.wordpress.net', '/notapath/' )->id ); 48 49 $this->assertEquals( $ids['www.wordpress.net/two/'], 50 get_network_by_path( 'www.wordpress.net', '/two/' )->id ); 51 52 // This should find /one/ despite the www. 53 $this->assertEquals( $ids['wordpress.org/one/'], 54 get_network_by_path( 'www.wordpress.org', '/one/' )->id ); 55 56 // This should not find /one/ because the domains don't match. 57 $this->assertEquals( $ids['wordpress.org/'], 58 get_network_by_path( 'site1.wordpress.org', '/one/' )->id ); 59 60 $this->assertEquals( $ids['wordpress.net/three/'], 61 get_network_by_path( 'wordpress.net', '/three/' )->id ); 62 63 $this->assertEquals( $ids['wordpress.net/'], 64 get_network_by_path( 'wordpress.net', '/notapath/' )->id ); 65 66 $this->assertEquals( $ids['wordpress.net/'], 67 get_network_by_path( 'site1.wordpress.net', '/notapath/' )->id ); 68 69 $this->assertEquals( $ids['wordpress.net/'], 70 get_network_by_path( 'site1.wordpress.net', '/three/' )->id ); 71 } 72 73 /** 74 * @ticket 27003 75 * @ticket 27927 76 */ 77 function test_get_site_by_path() { 78 $ids = array( 79 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), 80 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/' ), 81 'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/' ), 82 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/' ), 83 'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/' ), 84 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ), 85 'www.w.org/foo/' => array( 'domain' => 'www.w.org', 'path' => '/foo/' ), 86 'www.w.org/foo/bar/' => array( 'domain' => 'www.w.org', 'path' => '/foo/bar/' ), 87 ); 88 89 foreach ( $ids as &$id ) { 90 $id = $this->factory->blog->create( $id ); 91 } 92 unset( $id ); 93 94 $this->assertEquals( $ids['wordpress.org/'], 95 get_site_by_path( 'wordpress.org', '/notapath/' )->blog_id ); 96 97 $this->assertEquals( $ids['wordpress.org/'], 98 get_site_by_path( 'www.wordpress.org', '/notapath/' )->blog_id ); 99 100 $this->assertEquals( $ids['wordpress.org/foo/bar/'], 101 get_site_by_path( 'wordpress.org', '/foo/bar/baz/' )->blog_id ); 102 103 $this->assertEquals( $ids['wordpress.org/foo/bar/'], 104 get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/' )->blog_id ); 105 106 $this->assertEquals( $ids['wordpress.org/foo/bar/'], 107 get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 3 )->blog_id ); 108 109 $this->assertEquals( $ids['wordpress.org/foo/bar/'], 110 get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 3 )->blog_id ); 111 112 $this->assertEquals( $ids['wordpress.org/foo/bar/'], 113 get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 2 )->blog_id ); 114 115 $this->assertEquals( $ids['wordpress.org/foo/bar/'], 116 get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 2 )->blog_id ); 117 118 $this->assertEquals( $ids['wordpress.org/foo/'], 119 get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 1 )->blog_id ); 120 121 $this->assertEquals( $ids['wordpress.org/foo/'], 122 get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 1 )->blog_id ); 123 124 $this->assertEquals( $ids['wordpress.org/'], 125 get_site_by_path( 'wordpress.org', '/', 0 )->blog_id ); 126 127 $this->assertEquals( $ids['wordpress.org/'], 128 get_site_by_path( 'www.wordpress.org', '/', 0 )->blog_id ); 129 130 $this->assertEquals( $ids['make.wordpress.org/foo/'], 131 get_site_by_path( 'make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id ); 132 133 $this->assertEquals( $ids['make.wordpress.org/foo/'], 134 get_site_by_path( 'www.make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id ); 135 136 $this->assertEquals( $ids['www.w.org/'], 137 get_site_by_path( 'www.w.org', '/', 0 )->blog_id ); 138 139 $this->assertEquals( $ids['www.w.org/'], 140 get_site_by_path( 'www.w.org', '/notapath/' )->blog_id ); 141 142 $this->assertEquals( $ids['www.w.org/foo/bar/'], 143 get_site_by_path( 'www.w.org', '/foo/bar/baz/' )->blog_id ); 144 145 $this->assertEquals( $ids['www.w.org/foo/'], 146 get_site_by_path( 'www.w.org', '/foo/bar/baz/', 1 )->blog_id ); 147 148 // A site installed with www will not be found by the root domain. 149 $this->assertFalse( get_site_by_path( 'w.org', '/' ) ); 150 $this->assertFalse( get_site_by_path( 'w.org', '/notapath/' ) ); 151 $this->assertFalse( get_site_by_path( 'w.org', '/foo/bar/baz/' ) ); 152 $this->assertFalse( get_site_by_path( 'w.org', '/foo/bar/baz/', 1 ) ); 153 154 // A site will not be found by its root domain when an invalid subdomain is requested. 155 $this->assertFalse( get_site_by_path( 'invalid.wordpress.org', '/' ) ); 156 $this->assertFalse( get_site_by_path( 'invalid.wordpress.org', '/foo/bar/' ) ); 157 } 158 159 /** 160 * @ticket 27884 161 */ 162 function test_multisite_bootstrap() { 163 global $current_blog; 164 165 $network_ids = array( 166 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), 167 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/' ), 168 ); 169 170 foreach ( $network_ids as &$id ) { 171 $id = $this->factory->network->create( $id ); 172 } 173 unset( $id ); 174 175 $ids = array( 176 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => $network_ids['wordpress.org/'] ), 177 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => $network_ids['wordpress.org/'] ), 178 'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/', 'site_id' => $network_ids['wordpress.org/'] ), 179 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/', 'site_id' => $network_ids['make.wordpress.org/'] ), 180 'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/', 'site_id' => $network_ids['make.wordpress.org/'] ), 181 ); 182 183 foreach ( $ids as &$id ) { 184 $id = $this->factory->blog->create( $id ); 185 } 186 unset( $id ); 187 188 $this->_setup_host_request( 'wordpress.org', '/' ); 189 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 190 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 191 192 $this->_setup_host_request( 'wordpress.org', '/2014/04/23/hello-world/' ); 193 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 194 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 195 196 $this->_setup_host_request( 'wordpress.org', '/sample-page/' ); 197 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 198 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 199 200 $this->_setup_host_request( 'wordpress.org', '/?p=1' ); 201 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 202 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 203 204 $this->_setup_host_request( 'wordpress.org', '/wp-admin/' ); 205 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 206 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 207 208 $this->_setup_host_request( 'wordpress.org', '/foo/' ); 209 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 210 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 211 212 $this->_setup_host_request( 'wordpress.org', '/FOO/' ); 213 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 214 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 215 216 $this->_setup_host_request( 'wordpress.org', '/foo/2014/04/23/hello-world/' ); 217 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 218 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 219 220 $this->_setup_host_request( 'wordpress.org', '/foo/sample-page/' ); 221 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 222 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 223 224 $this->_setup_host_request( 'wordpress.org', '/foo/?p=1' ); 225 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 226 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 227 228 $this->_setup_host_request( 'wordpress.org', '/foo/wp-admin/' ); 229 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 230 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 231 232 // @todo not currently passing. 233 //$this->_setup_host_request( 'wordpress.org', '/foo/bar/' ); 234 //$this->assertEquals( $ids['wordpress.org/foo/bar/'], $current_blog->blog_id ); 235 //$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 236 237 $this->_setup_host_request( 'make.wordpress.org', '/' ); 238 $this->assertEquals( $ids['make.wordpress.org/'], $current_blog->blog_id ); 239 $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id ); 240 241 $this->_setup_host_request( 'make.wordpress.org', '/foo/' ); 242 $this->assertEquals( $ids['make.wordpress.org/foo/'], $current_blog->blog_id ); 243 $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id ); 244 } 245 246 /** 247 * Reset various globals required for a 'clean' multisite boot. 248 * 249 * The $wpdb and $table_prefix globals are required for ms-settings.php to 250 * load properly. 251 * 252 * @param string $domain HTTP_HOST of the bootstrap request. 253 * @param string $path REQUEST_URI of the boot strap request. 254 */ 255 function _setup_host_request( $domain, $path ) { 256 global $current_site, $current_blog, $table_prefix, $wpdb; 257 258 $table_prefix = WP_TESTS_TABLE_PREFIX; 259 $current_site = $current_blog = null; 260 $_SERVER['HTTP_HOST'] = $domain; 261 $_SERVER['REQUEST_URI'] = $path; 262 263 include ABSPATH . '/wp-includes/ms-settings.php'; 264 } 265 } 266 267 endif; 268 No newline at end of file -
tests/phpunit/tests/multisite/sites.php
1 <?php 2 3 if ( is_multisite() ) : 4 5 /** 6 * @group ms-sites 7 * @group multisite 8 */ 9 class Tests_Multisite_Sites extends WP_UnitTestCase { 10 protected $suppress = false; 11 12 function setUp() { 13 global $wpdb; 14 parent::setUp(); 15 $this->suppress = $wpdb->suppress_errors(); 16 17 $_SERVER['REMOTE_ADDR'] = ''; 18 } 19 20 function tearDown() { 21 global $wpdb; 22 parent::tearDown(); 23 $wpdb->suppress_errors( $this->suppress ); 24 } 25 26 function test_create_and_delete_blog() { 27 global $wpdb; 28 29 $blog_ids = $this->factory->blog->create_many( 4 ); 30 foreach ( $blog_ids as $blog_id ) { 31 $this->assertInternalType( 'int', $blog_id ); 32 $prefix = $wpdb->get_blog_prefix( $blog_id ); 33 34 // $get_all = false 35 $details = get_blog_details( $blog_id, false ); 36 $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); 37 38 // get_id_from_blogname(), see #20950 39 $this->assertEquals( $blog_id, get_id_from_blogname( $details->path ) ); 40 $this->assertEquals( $blog_id, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) ); 41 42 // get_blog_id_from_url() 43 $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) ); 44 $key = md5( $details->domain . $details->path ); 45 $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) ); 46 47 // These are empty until get_blog_details() is called with $get_all = true 48 $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); 49 $key = md5( $details->domain . $details->path ); 50 $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); 51 52 // $get_all = true should propulate the full blog-details cache and the blog slug lookup cache 53 $details = get_blog_details( $blog_id, true ); 54 $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) ); 55 $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) ); 56 57 foreach ( $wpdb->tables( 'blog', false ) as $table ) { 58 $suppress = $wpdb->suppress_errors(); 59 $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" ); 60 $wpdb->suppress_errors( $suppress ); 61 $this->assertNotEmpty( $table_fields ); 62 $result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" ); 63 if ( 'commentmeta' == $table || 'links' == $table ) 64 $this->assertEmpty( $result ); 65 else 66 $this->assertNotEmpty( $result ); 67 } 68 } 69 70 // update the blog count cache to use get_blog_count() 71 wp_update_network_counts(); 72 $this->assertEquals( 4 + 1, (int) get_blog_count() ); 73 74 $drop_tables = false; 75 // delete all blogs 76 foreach ( $blog_ids as $blog_id ) { 77 // drop tables for every second blog 78 $drop_tables = ! $drop_tables; 79 $details = get_blog_details( $blog_id, false ); 80 81 wpmu_delete_blog( $blog_id, $drop_tables ); 82 83 $this->assertEquals( false, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) ); 84 $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); 85 $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); 86 $key = md5( $details->domain . $details->path ); 87 $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); 88 $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); 89 90 $prefix = $wpdb->get_blog_prefix( $blog_id ); 91 foreach ( $wpdb->tables( 'blog', false ) as $table ) { 92 $suppress = $wpdb->suppress_errors(); 93 $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" ); 94 $wpdb->suppress_errors( $suppress ); 95 if ( $drop_tables ) 96 $this->assertEmpty( $table_fields ); 97 else 98 $this->assertNotEmpty( $table_fields, $prefix . $table ); 99 } 100 } 101 102 // update the blog count cache to use get_blog_count() 103 wp_update_network_counts(); 104 $this->assertEquals( 1, get_blog_count() ); 105 } 106 107 /** 108 * @expectedDeprecated get_dashboard_blog 109 */ 110 function test_get_dashboard_blog() { 111 // if there is no dashboard blog set, current blog is used 112 $dashboard_blog = get_dashboard_blog(); 113 $this->assertEquals( 1, $dashboard_blog->blog_id ); 114 115 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 116 $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id ) ); 117 $this->assertInternalType( 'int', $blog_id ); 118 119 // set the dashboard blog to another one 120 update_site_option( 'dashboard_blog', $blog_id ); 121 $dashboard_blog = get_dashboard_blog(); 122 $this->assertEquals( $blog_id, $dashboard_blog->blog_id ); 123 } 124 125 function test_wpmu_update_blogs_date() { 126 global $wpdb; 127 128 wpmu_update_blogs_date(); 129 130 // compare the update time with the current time, allow delta < 2 131 $blog = get_blog_details( $wpdb->blogid ); 132 $current_time = time(); 133 $time_difference = $current_time - strtotime( $blog->last_updated ); 134 $this->assertLessThan( 2, $time_difference ); 135 } 136 137 function test_getters(){ 138 global $current_site; 139 140 $blog_id = get_current_blog_id(); 141 $blog = get_blog_details( $blog_id ); 142 $this->assertEquals( $blog_id, $blog->blog_id ); 143 $this->assertEquals( $current_site->domain, $blog->domain ); 144 $this->assertEquals( '/', $blog->path ); 145 146 // Test defaulting to current blog 147 $this->assertEquals( $blog, get_blog_details() ); 148 149 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 150 $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogname', 'title' => 'Test Title' ) ); 151 $this->assertInternalType( 'int', $blog_id ); 152 153 $this->assertEquals( 'http://' . $current_site->domain . $current_site->path . 'test_blogname/', get_blogaddress_by_name('test_blogname') ); 154 155 $this->assertEquals( $blog_id, get_id_from_blogname('test_blogname') ); 156 } 157 158 function _action_counter_cb( $blog_id ) { 159 global $test_action_counter; 160 $test_action_counter++; 161 } 162 163 function test_update_blog_details() { 164 global $test_action_counter; 165 166 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 167 $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) ); 168 $this->assertInternalType( 'int', $blog_id ); 169 170 $result = update_blog_details( $blog_id, array('domain' => 'example.com', 'path' => 'my_path/') ); 171 $this->assertTrue( $result ); 172 173 $blog = get_blog_details( $blog_id ); 174 $this->assertEquals( 'example.com', $blog->domain ); 175 $this->assertEquals( 'my_path/', $blog->path ); 176 $this->assertEquals( '0', $blog->spam ); 177 178 $result = update_blog_details( $blog_id, array('domain' => 'example2.com','spam' => 1) ); 179 $this->assertTrue( $result ); 180 $blog = get_blog_details( $blog_id ); 181 $this->assertEquals( 'example2.com', $blog->domain ); 182 $this->assertEquals( 'my_path/', $blog->path ); 183 $this->assertEquals( '1', $blog->spam ); 184 185 $result = update_blog_details( $blog_id ); 186 $this->assertFalse( $result ); 187 $blog = get_blog_details( $blog_id ); 188 $this->assertEquals( 'example2.com', $blog->domain ); 189 $this->assertEquals( 'my_path/', $blog->path ); 190 $this->assertEquals( '1', $blog->spam ); 191 192 $test_action_counter = 0; 193 194 add_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 195 $result = update_blog_details( $blog_id, array( 'spam' => 0 ) ); 196 $this->assertTrue( $result ); 197 $blog = get_blog_details( $blog_id ); 198 $this->assertEquals( '0', $blog->spam ); 199 $this->assertEquals( 1, $test_action_counter ); 200 201 // Same again 202 $result = update_blog_details( $blog_id, array( 'spam' => 0 ) ); 203 $this->assertTrue( $result ); 204 $blog = get_blog_details( $blog_id ); 205 $this->assertEquals( '0', $blog->spam ); 206 $this->assertEquals( 1, $test_action_counter ); 207 remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 208 209 add_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 210 $result = update_blog_details( $blog_id, array( 'spam' => 1 ) ); 211 $this->assertTrue( $result ); 212 $blog = get_blog_details( $blog_id ); 213 $this->assertEquals( '1', $blog->spam ); 214 $this->assertEquals( 2, $test_action_counter ); 215 216 // Same again 217 $result = update_blog_details( $blog_id, array( 'spam' => 1 ) ); 218 $this->assertTrue( $result ); 219 $blog = get_blog_details( $blog_id ); 220 $this->assertEquals( '1', $blog->spam ); 221 $this->assertEquals( 2, $test_action_counter ); 222 remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 223 224 add_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 225 $result = update_blog_details( $blog_id, array( 'archived' => 1 ) ); 226 $this->assertTrue( $result ); 227 $blog = get_blog_details( $blog_id ); 228 $this->assertEquals( '1', $blog->archived ); 229 $this->assertEquals( 3, $test_action_counter ); 230 231 // Same again 232 $result = update_blog_details( $blog_id, array( 'archived' => 1 ) ); 233 $this->assertTrue( $result ); 234 $blog = get_blog_details( $blog_id ); 235 $this->assertEquals( '1', $blog->archived ); 236 $this->assertEquals( 3, $test_action_counter ); 237 remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 238 239 add_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 240 $result = update_blog_details( $blog_id, array( 'archived' => 0 ) ); 241 $this->assertTrue( $result ); 242 $blog = get_blog_details( $blog_id ); 243 $this->assertEquals( '0', $blog->archived ); 244 $this->assertEquals( 4, $test_action_counter ); 245 246 // Same again 247 $result = update_blog_details( $blog_id, array( 'archived' => 0 ) ); 248 $this->assertTrue( $result ); 249 $blog = get_blog_details( $blog_id ); 250 $this->assertEquals( '0', $blog->archived ); 251 $this->assertEquals( 4, $test_action_counter ); 252 remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 253 254 add_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 255 $result = update_blog_details( $blog_id, array( 'deleted' => 1 ) ); 256 $this->assertTrue( $result ); 257 $blog = get_blog_details( $blog_id ); 258 $this->assertEquals( '1', $blog->deleted ); 259 $this->assertEquals( 5, $test_action_counter ); 260 261 // Same again 262 $result = update_blog_details( $blog_id, array( 'deleted' => 1 ) ); 263 $this->assertTrue( $result ); 264 $blog = get_blog_details( $blog_id ); 265 $this->assertEquals( '1', $blog->deleted ); 266 $this->assertEquals( 5, $test_action_counter ); 267 remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 268 269 add_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 270 $result = update_blog_details( $blog_id, array( 'deleted' => 0 ) ); 271 $this->assertTrue( $result ); 272 $blog = get_blog_details( $blog_id ); 273 $this->assertEquals( '0', $blog->deleted ); 274 $this->assertEquals( 6, $test_action_counter ); 275 276 // Same again 277 $result = update_blog_details( $blog_id, array( 'deleted' => 0 ) ); 278 $this->assertTrue( $result ); 279 $blog = get_blog_details( $blog_id ); 280 $this->assertEquals( '0', $blog->deleted ); 281 $this->assertEquals( 6, $test_action_counter ); 282 remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 283 284 add_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 285 $result = update_blog_details( $blog_id, array( 'mature' => 1 ) ); 286 $this->assertTrue( $result ); 287 $blog = get_blog_details( $blog_id ); 288 $this->assertEquals( '1', $blog->mature ); 289 $this->assertEquals( 7, $test_action_counter ); 290 291 // Same again 292 $result = update_blog_details( $blog_id, array( 'mature' => 1 ) ); 293 $this->assertTrue( $result ); 294 $blog = get_blog_details( $blog_id ); 295 $this->assertEquals( '1', $blog->mature ); 296 $this->assertEquals( 7, $test_action_counter ); 297 remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 298 299 add_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 300 $result = update_blog_details( $blog_id, array( 'mature' => 0 ) ); 301 $this->assertTrue( $result ); 302 $blog = get_blog_details( $blog_id ); 303 $this->assertEquals( '0', $blog->mature ); 304 $this->assertEquals( 8, $test_action_counter ); 305 306 // Same again 307 $result = update_blog_details( $blog_id, array( 'mature' => 0 ) ); 308 $this->assertTrue( $result ); 309 $blog = get_blog_details( $blog_id ); 310 $this->assertEquals( '0', $blog->mature ); 311 $this->assertEquals( 8, $test_action_counter ); 312 remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 313 } 314 315 /** 316 * Test fetching a blog that doesn't exist and again after it exists. 317 * 318 * @ticket 23405 319 */ 320 function test_get_blog_details_blog_does_not_exist() { 321 global $wpdb; 322 323 $blog_id = $wpdb->get_var( "SELECT MAX(blog_id) FROM $wpdb->blogs" ); 324 325 // An idosyncrancy of the unit tests is that the max blog_id gets reset 326 // to 1 in between test cases but picks up where it previously left off 327 // on the next insert. If 1 is reported, burn a blog create to get 328 // the max counter back in sync. 329 if ( 1 == $blog_id ) { 330 $blog_id = $this->factory->blog->create(); 331 } 332 $blog_id++; 333 334 $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) ); 335 $this->assertFalse( get_blog_details( $blog_id ) ); 336 $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) ); 337 $this->assertFalse( get_blog_details( $blog_id ) ); 338 $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) ); 339 340 $this->assertEquals( $blog_id, $this->factory->blog->create() ); 341 $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) ); 342 343 $blog = get_blog_details( $blog_id ); 344 $this->assertEquals( $blog_id, $blog->blog_id ); 345 $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) ); 346 347 wpmu_delete_blog( $blog_id ); 348 $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) ); 349 $blog->deleted = '1'; 350 $this->assertEQuals( $blog, get_blog_details( $blog_id ) ); 351 $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) ); 352 353 wpmu_delete_blog( $blog_id, true ); 354 $this->assertFalse( get_blog_details( $blog_id ) ); 355 $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) ); 356 } 357 358 function test_update_blog_status() { 359 global $test_action_counter; 360 361 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 362 $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) ); 363 $this->assertInternalType( 'int', $blog_id ); 364 365 $test_action_counter = 0; 366 $count = 1; 367 368 add_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 369 $result = update_blog_status( $blog_id, 'spam', 0 ); 370 $this->assertEquals( 0, $result ); 371 $blog = get_blog_details( $blog_id ); 372 $this->assertEquals( '0', $blog->spam ); 373 $this->assertEquals( $count, $test_action_counter ); 374 375 // Same again 376 $count++; 377 $result = update_blog_status( $blog_id, 'spam', 0 ); 378 $this->assertEquals( 0, $result ); 379 $blog = get_blog_details( $blog_id ); 380 $this->assertEquals( '0', $blog->spam ); 381 $this->assertEquals( $count, $test_action_counter ); 382 remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 383 384 $count++; 385 add_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 386 $result = update_blog_status( $blog_id, 'spam', 1 ); 387 $this->assertEquals( 1, $result ); 388 $blog = get_blog_details( $blog_id ); 389 $this->assertEquals( '1', $blog->spam ); 390 $this->assertEquals( $count, $test_action_counter ); 391 392 // Same again 393 $count++; 394 $result = update_blog_status( $blog_id, 'spam', 1 ); 395 $this->assertEquals( 1, $result ); 396 $blog = get_blog_details( $blog_id ); 397 $this->assertEquals( '1', $blog->spam ); 398 $this->assertEquals( $count, $test_action_counter ); 399 remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 400 401 add_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 402 $count++; 403 $result = update_blog_status( $blog_id, 'archived', 1 ); 404 $this->assertEquals( 1, $result ); 405 $blog = get_blog_details( $blog_id ); 406 $this->assertEquals( '1', $blog->archived ); 407 $this->assertEquals( $count, $test_action_counter ); 408 409 // Same again 410 $count++; 411 $result = update_blog_status( $blog_id, 'archived', 1 ); 412 $this->assertEquals( 1, $result ); 413 $blog = get_blog_details( $blog_id ); 414 $this->assertEquals( '1', $blog->archived ); 415 $this->assertEquals( $count, $test_action_counter ); 416 remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 417 418 add_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 419 $count++; 420 $result = update_blog_status( $blog_id, 'archived', 0 ); 421 $this->assertEquals( 0, $result ); 422 $blog = get_blog_details( $blog_id ); 423 $this->assertEquals( '0', $blog->archived ); 424 $this->assertEquals( $count, $test_action_counter ); 425 426 // Same again 427 $result = update_blog_status( $blog_id, 'archived', 0 ); 428 $count++; 429 $this->assertEquals( 0, $result ); 430 $blog = get_blog_details( $blog_id ); 431 $this->assertEquals( '0', $blog->archived ); 432 $this->assertEquals( $count, $test_action_counter ); 433 remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 434 435 add_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 436 $count++; 437 $result = update_blog_status( $blog_id, 'deleted', 1 ); 438 $this->assertEquals( 1, $result ); 439 $blog = get_blog_details( $blog_id ); 440 $this->assertEquals( '1', $blog->deleted ); 441 $this->assertEquals( $count, $test_action_counter ); 442 443 // Same again 444 $count++; 445 $result = update_blog_status( $blog_id, 'deleted', 1 ); 446 $this->assertEquals( 1, $result ); 447 $blog = get_blog_details( $blog_id ); 448 $this->assertEquals( '1', $blog->deleted ); 449 $this->assertEquals( $count, $test_action_counter ); 450 remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 451 452 add_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 453 $count++; 454 $result = update_blog_status( $blog_id, 'deleted', 0 ); 455 $this->assertEquals( 0, $result ); 456 $blog = get_blog_details( $blog_id ); 457 $this->assertEquals( '0', $blog->deleted ); 458 $this->assertEquals( $count, $test_action_counter ); 459 460 // Same again 461 $count++; 462 $result = update_blog_status( $blog_id, 'deleted', 0 ); 463 $this->assertEquals( 0, $result ); 464 $blog = get_blog_details( $blog_id ); 465 $this->assertEquals( '0', $blog->deleted ); 466 $this->assertEquals( $count, $test_action_counter ); 467 remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 468 469 add_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 470 $count++; 471 $result = update_blog_status( $blog_id, 'mature', 1 ); 472 $this->assertEquals( 1, $result ); 473 $blog = get_blog_details( $blog_id ); 474 $this->assertEquals( '1', $blog->mature ); 475 $this->assertEquals( $count, $test_action_counter ); 476 477 // Same again 478 $count++; 479 $result = update_blog_status( $blog_id, 'mature', 1 ); 480 $this->assertEquals( 1, $result ); 481 $blog = get_blog_details( $blog_id ); 482 $this->assertEquals( '1', $blog->mature ); 483 $this->assertEquals( $count, $test_action_counter ); 484 remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 485 486 add_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 487 $count++; 488 $result = update_blog_status( $blog_id, 'mature', 0 ); 489 $this->assertEquals( 0, $result ); 490 $blog = get_blog_details( $blog_id ); 491 $this->assertEquals( '0', $blog->mature ); 492 $this->assertEquals( $count, $test_action_counter ); 493 494 // Same again 495 $count++; 496 $result = update_blog_status( $blog_id, 'mature', 0 ); 497 $this->assertEquals( 0, $result ); 498 $blog = get_blog_details( $blog_id ); 499 $this->assertEquals( '0', $blog->mature ); 500 $this->assertEquals( $count, $test_action_counter ); 501 remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 ); 502 503 add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 ); 504 $count++; 505 $result = update_blog_status( $blog_id, 'public', 0 ); 506 $this->assertEquals( 0, $result ); 507 $blog = get_blog_details( $blog_id ); 508 $this->assertEquals( '0', $blog->public ); 509 $this->assertEquals( $count, $test_action_counter ); 510 511 // Same again 512 $count++; 513 $result = update_blog_status( $blog_id, 'public', 0 ); 514 $this->assertEquals( 0, $result ); 515 $blog = get_blog_details( $blog_id ); 516 $this->assertEquals( '0', $blog->public ); 517 $this->assertEquals( $count, $test_action_counter ); 518 remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 ); 519 520 add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 ); 521 $count++; 522 $result = update_blog_status( $blog_id, 'public', 1 ); 523 $this->assertEquals( 1, $result ); 524 $blog = get_blog_details( $blog_id ); 525 $this->assertEquals( '1', $blog->public ); 526 $this->assertEquals( $count, $test_action_counter ); 527 528 // Same again 529 $count++; 530 $result = update_blog_status( $blog_id, 'public', 1 ); 531 $this->assertEquals( 1, $result ); 532 $blog = get_blog_details( $blog_id ); 533 $this->assertEquals( '1', $blog->public ); 534 $this->assertEquals( $count, $test_action_counter ); 535 remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 ); 536 537 // Updating a dummy field returns the value passed. Go fig. 538 $result = update_blog_status( $blog_id, 'doesnotexist', 1 ); 539 $this->assertEquals( 1, $result ); 540 } 541 542 function test_switch_restore_blog() { 543 global $_wp_switched_stack, $wpdb; 544 545 $this->assertEquals( array(), $_wp_switched_stack ); 546 $this->assertFalse( ms_is_switched() ); 547 $current_blog_id = get_current_blog_id(); 548 $this->assertInternalType( 'integer', $current_blog_id ); 549 550 wp_cache_set( 'switch-test', $current_blog_id, 'switch-test' ); 551 $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) ); 552 553 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 554 $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) ); 555 556 $cap_key = wp_get_current_user()->cap_key; 557 switch_to_blog( $blog_id ); 558 $this->assertNotEquals( $cap_key, wp_get_current_user()->cap_key ); 559 $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack ); 560 $this->assertTrue( ms_is_switched() ); 561 $this->assertEquals( $blog_id, $wpdb->blogid ); 562 $this->assertFalse( wp_cache_get( 'switch-test', 'switch-test' ) ); 563 wp_cache_set( 'switch-test', $blog_id, 'switch-test' ); 564 $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) ); 565 566 switch_to_blog( $blog_id ); 567 $this->assertEquals( array( $current_blog_id, $blog_id ), $_wp_switched_stack ); 568 $this->assertTrue( ms_is_switched() ); 569 $this->assertEquals( $blog_id, $wpdb->blogid ); 570 $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) ); 571 572 restore_current_blog(); 573 $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack ); 574 $this->assertTrue( ms_is_switched() ); 575 $this->assertEquals( $blog_id, $wpdb->blogid ); 576 $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) ); 577 578 restore_current_blog(); 579 $this->assertEquals( $cap_key, wp_get_current_user()->cap_key ); 580 $this->assertEquals( $current_blog_id, get_current_blog_id() ); 581 $this->assertEquals( array(), $_wp_switched_stack ); 582 $this->assertFalse( ms_is_switched() ); 583 $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) ); 584 585 $this->assertFalse( restore_current_blog() ); 586 } 587 588 function test_get_blog_post() { 589 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 590 $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) ); 591 $current_blog_id = get_current_blog_id(); 592 593 $post_id = $this->factory->post->create(); 594 $this->assertInstanceOf( 'WP_Post', get_post( $post_id ) ); 595 switch_to_blog( $blog_id ); 596 $this->assertNull( get_post( $post_id ) ); 597 $post = get_blog_post( $current_blog_id, $post_id ); 598 $this->assertInstanceOf( 'WP_Post', $post ); 599 $this->assertEquals( $post_id, $post->ID ); 600 restore_current_blog(); 601 602 wp_update_post( array( 'ID' => $post_id, 'post_title' => 'A Different Title' ) ); 603 switch_to_blog( $blog_id ); 604 $post = get_blog_post( $current_blog_id, $post_id ); 605 // Make sure cache is good 606 $this->assertEquals( 'A Different Title', $post->post_title ); 607 608 $post_id2 = $this->factory->post->create(); 609 // Test get_blog_post() with currently active blog ID. 610 $post = get_blog_post( $blog_id, $post_id2 ); 611 $this->assertInstanceOf( 'WP_Post', $post ); 612 $this->assertEquals( $post_id2, $post->ID ); 613 restore_current_blog(); 614 } 615 616 function test_get_blog_id_from_url() { 617 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 618 $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/testdomainexists', 'title' => 'Test Title' ) ); 619 620 $details = get_blog_details( $blog_id, false ); 621 622 $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) ); 623 $key = md5( $details->domain . $details->path ); 624 $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) ); 625 626 $this->assertEquals( 0, get_blog_id_from_url( $details->domain, 'foo' ) ); 627 628 wpmu_delete_blog( $blog_id ); 629 $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) ); 630 wpmu_delete_blog( $blog_id, true ); 631 632 $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); 633 $this->assertEquals( 0, get_blog_id_from_url( $details->domain, $details->path ) ); 634 } 635 636 /** 637 * @ticket 14511 638 */ 639 function test_wp_get_sites() { 640 $this->factory->blog->create_many( 2, array( 'site_id' => 2, 'meta' => array( 'public' => 1 ) ) ); 641 $this->factory->blog->create_many( 3, array( 'site_id' => 3, 'meta' => array( 'public' => 0 ) ) ); 642 643 // Expect no sites when passed an invalid network_id 644 $this->assertCount( 0, wp_get_sites( array( 'network_id' => 0 ) ) ); 645 $this->assertCount( 0, wp_get_sites( array( 'network_id' => 4 ) ) ); 646 647 // Expect 1 site when no network_id is specified - defaults to current network. 648 $this->assertCount( 1, wp_get_sites() ); 649 // Expect 6 sites when network_id = null. 650 $this->assertCount( 6, wp_get_sites( array( 'network_id' => null ) ) ); 651 652 // Expect 1 site with a network_id of 1, 2 for network_id 2, 3 for 3 653 $this->assertCount( 1, wp_get_sites( array( 'network_id' => 1 ) ) ); 654 $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2 ) ) ); 655 $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3 ) ) ); 656 657 // Expect 6 sites when public is null (across all networks) 658 $this->assertCount( 6, wp_get_sites( array( 'public' => null, 'network_id' => null ) ) ); 659 660 // Expect 3 sites when public is 1 661 $this->assertCount( 3, wp_get_sites( array( 'public' => 1, 'network_id' => null ) ) ); 662 663 // Expect 2 sites when public is 1 and network_id is 2 664 $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2, 'public' => 1 ) ) ); 665 666 // Expect no sites when public is set to 0 and network_id is not 3 667 $this->assertCount( 0, wp_get_sites( array( 'network_id' => 1, 'public' => 0 ) ) ); 668 669 // Test public + network_id = 3 670 $this->assertCount( 0, wp_get_sites( array( 'network_id' => 3, 'public' => 1 ) ) ); 671 $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3, 'public' => 0 ) ) ); 672 } 673 674 /** 675 * @ticket 14511 676 */ 677 function test_wp_get_sites_limit_offset() { 678 // Create 4 more sites (in addition to the default one) 679 $this->factory->blog->create_many( 4, array( 'meta' => array( 'public' => 1 ) ) ); 680 681 // Expect all 5 sites when no limit/offset is specified 682 $this->assertCount( 5, wp_get_sites() ); 683 684 // Expect first 2 sites when using limit 685 $this->assertCount( 2, wp_get_sites( array( 'limit' => 2 ) ) ); 686 687 // Expect only the last 3 sites when using offset of 2 (limit will default to 100) 688 $this->assertCount( 3, wp_get_sites( array( 'offset' => 2 ) ) ); 689 690 // Expect only the last 1 site when using offset of 4 and limit of 2 691 $this->assertCount( 1, wp_get_sites( array( 'limit' => 2, 'offset' => 4 ) ) ); 692 693 // Expect 0 sites when using an offset larger than the number of sites 694 $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) ); 695 } 696 697 /** 698 * @ticket 27952 699 */ 700 function test_posts_count() { 701 $this->factory->post->create(); 702 $post2 = $this->factory->post->create(); 703 $this->assertEquals( 2, get_blog_details()->post_count ); 704 705 wp_delete_post( $post2 ); 706 $this->assertEquals( 1, get_blog_details()->post_count ); 707 } 708 709 /** 710 * @ticket 26410 711 */ 712 function test_blog_details_cache_invalidation() { 713 update_option( 'blogname', 'foo' ); 714 $details = get_blog_details( get_current_blog_id() ); 715 $this->assertEquals( 'foo', $details->blogname ); 716 717 update_option( 'blogname', 'bar' ); 718 $details = get_blog_details( get_current_blog_id() ); 719 $this->assertEquals( 'bar', $details->blogname ); 720 } 721 722 /** 723 * @ticket 29845 724 */ 725 function test_get_blog_details() { 726 $network_ids = array( 727 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), 728 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/' ), 729 ); 730 731 foreach ( $network_ids as &$id ) { 732 $id = $this->factory->network->create( $id ); 733 } 734 unset( $id ); 735 736 $ids = array( 737 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'title' => 'Test 1', 'site_id' => $network_ids['wordpress.org/'] ), 738 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'title' => 'Test 2', 'site_id' => $network_ids['wordpress.org/'] ), 739 'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/', 'title' => 'Test 3', 'site_id' => $network_ids['wordpress.org/'] ), 740 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/', 'title' => 'Test 4', 'site_id' => $network_ids['make.wordpress.org/'] ), 741 'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/', 'title' => 'Test 5', 'site_id' => $network_ids['make.wordpress.org/'] ), 742 ); 743 744 foreach ( $ids as &$id ) { 745 $id = $this->factory->blog->create( $id ); 746 } 747 unset( $id ); 748 749 // Retrieve site details by passing only a blog ID. 750 $site = get_blog_details( $ids['wordpress.org/'] ); 751 $this->assertEquals( $ids['wordpress.org/'], $site->blog_id ); 752 $this->assertEquals( 'Test 1', $site->blogname ); 753 754 $site = get_blog_details( $ids['wordpress.org/foo/'] ); 755 $this->assertEquals( $ids['wordpress.org/foo/'], $site->blog_id ); 756 $this->assertEquals( 'Test 2', $site->blogname ); 757 758 $site = get_blog_details( 999 ); 759 $this->assertFalse( $site ); 760 761 // Retrieve site details by passing an array containing blog_id. 762 $site = get_blog_details( array( 'blog_id' => $ids['wordpress.org/foo/bar/'] ) ); 763 $this->assertEquals( $ids['wordpress.org/foo/bar/'], $site->blog_id ); 764 $this->assertEquals( 'Test 3', $site->blogname ); 765 766 $site = get_blog_details( array( 'blog_id' => $ids['make.wordpress.org/'] ) ); 767 $this->assertEquals( $ids['make.wordpress.org/'], $site->blog_id ); 768 $this->assertEquals( 'Test 4', $site->blogname ); 769 770 $site = get_blog_details( array( 'blog_id' => 999 ) ); 771 $this->assertFalse( $site ); 772 773 // Retrieve site details by passing an array containing domain and path. 774 $site = get_blog_details( array( 'domain' => 'wordpress.org', 'path' => '/' ) ); 775 $this->assertEquals( $ids['wordpress.org/'], $site->blog_id ); 776 $this->assertEquals( 'Test 1', $site->blogname ); 777 778 $site = get_blog_details( array( 'domain' => 'wordpress.org', 'path' => '/foo/' ) ); 779 $this->assertEquals( $ids['wordpress.org/foo/'], $site->blog_id ); 780 $this->assertEquals( 'Test 2', $site->blogname ); 781 782 $site = get_blog_details( array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/' ) ); 783 $this->assertEquals( $ids['wordpress.org/foo/bar/'], $site->blog_id ); 784 $this->assertEquals( 'Test 3', $site->blogname ); 785 786 $site = get_blog_details( array( 'domain' => 'make.wordpress.org', 'path' => '/' ) ); 787 $this->assertEquals( $ids['make.wordpress.org/'], $site->blog_id ); 788 $this->assertEquals( 'Test 4', $site->blogname ); 789 790 $site = get_blog_details( array( 'domain' => 'make.wordpress.org', 'path' => '/foo/' ) ); 791 $this->assertEquals( $ids['make.wordpress.org/foo/'], $site->blog_id ); 792 $this->assertEquals( 'Test 5', $site->blogname ); 793 794 $site = get_blog_details( array( 'domain' => 'wordpress.org', 'path' => '/zxy/' ) ); 795 $this->assertFalse( $site ); 796 } 797 } 798 799 endif; 800 No newline at end of file -
tests/phpunit/tests/multisite/users.php
1 <?php 2 3 if ( is_multisite() ) : 4 /** 5 * @group ms-users 6 * @group multisite 7 */ 8 class Tests_Multisite_Users extends WP_UnitTestCase { 9 protected $suppress = false; 10 11 function setUp() { 12 global $wpdb; 13 parent::setUp(); 14 $this->suppress = $wpdb->suppress_errors(); 15 16 $_SERVER['REMOTE_ADDR'] = ''; 17 } 18 19 function tearDown() { 20 global $wpdb; 21 parent::tearDown(); 22 $wpdb->suppress_errors( $this->suppress ); 23 } 24 25 function test_remove_user_from_blog() { 26 $user1 = $this->factory->user->create_and_get(); 27 $user2 = $this->factory->user->create_and_get(); 28 29 $post_id = $this->factory->post->create( array( 'post_author' => $user1->ID ) ); 30 31 remove_user_from_blog( $user1->ID, 1, $user2->ID ); 32 33 $post = get_post( $post_id ); 34 35 $this->assertNotEquals( $user1->ID, $post->post_author ); 36 $this->assertEquals( $user2->ID, $post->post_author ); 37 } 38 39 function test_get_blogs_of_user() { 40 // Logged out users don't have blogs. 41 $this->assertEquals( array(), get_blogs_of_user( 0 ) ); 42 43 $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 44 $blog_ids = $this->factory->blog->create_many( 10, array( 'user_id' => $user1_id ) ); 45 46 foreach ( $blog_ids as $blog_id ) 47 $this->assertInternalType( 'int', $blog_id ); 48 49 $blogs_of_user = array_keys( get_blogs_of_user( $user1_id, false ) ); 50 sort( $blogs_of_user ); 51 $this->assertEquals ( array_merge( array( 1 ), $blog_ids), $blogs_of_user ); 52 53 $this->assertTrue( remove_user_from_blog( $user1_id, 1 ) ); 54 55 $blogs_of_user = array_keys( get_blogs_of_user( $user1_id, false ) ); 56 sort( $blogs_of_user ); 57 $this->assertEquals ( $blog_ids, $blogs_of_user ); 58 59 foreach ( get_blogs_of_user( $user1_id, false ) as $blog ) { 60 $this->assertTrue( isset( $blog->userblog_id ) ); 61 $this->assertTrue( isset( $blog->blogname ) ); 62 $this->assertTrue( isset( $blog->domain ) ); 63 $this->assertTrue( isset( $blog->path ) ); 64 $this->assertTrue( isset( $blog->site_id ) ); 65 $this->assertTrue( isset( $blog->siteurl ) ); 66 $this->assertTrue( isset( $blog->archived ) ); 67 $this->assertTrue( isset( $blog->spam ) ); 68 $this->assertTrue( isset( $blog->deleted ) ); 69 } 70 71 // Non-existent users don't have blogs. 72 wpmu_delete_user( $user1_id ); 73 $user = new WP_User( $user1_id ); 74 $this->assertFalse( $user->exists(), 'WP_User->exists' ); 75 $this->assertEquals( array(), get_blogs_of_user( $user1_id ) ); 76 } 77 78 79 /** 80 * @expectedDeprecated is_blog_user 81 */ 82 function test_is_blog_user() { 83 global $wpdb; 84 85 $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 86 87 $old_current = get_current_user_id(); 88 wp_set_current_user( $user1_id ); 89 90 $this->assertTrue( is_blog_user() ); 91 $this->assertTrue( is_blog_user( $wpdb->blogid ) ); 92 93 $blog_ids = array(); 94 95 $blog_ids = $this->factory->blog->create_many( 5 ); 96 foreach ( $blog_ids as $blog_id ) { 97 $this->assertInternalType( 'int', $blog_id ); 98 $this->assertTrue( is_blog_user( $blog_id ) ); 99 $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) ); 100 $this->assertFalse( is_blog_user( $blog_id ) ); 101 } 102 103 wp_set_current_user( $old_current ); 104 } 105 106 function test_is_user_member_of_blog() { 107 global $wpdb; 108 109 $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 110 111 $old_current = get_current_user_id(); 112 wp_set_current_user( $user1_id ); 113 114 $this->assertTrue( is_user_member_of_blog() ); 115 $this->assertTrue( is_user_member_of_blog( 0, 0 ) ); 116 $this->assertTrue( is_user_member_of_blog( 0, $wpdb->blogid ) ); 117 $this->assertTrue( is_user_member_of_blog( $user1_id ) ); 118 $this->assertTrue( is_user_member_of_blog( $user1_id, $wpdb->blogid ) ); 119 120 $blog_ids = $this->factory->blog->create_many( 5 ); 121 foreach ( $blog_ids as $blog_id ) { 122 $this->assertInternalType( 'int', $blog_id ); 123 $this->assertTrue( is_user_member_of_blog( $user1_id, $blog_id ) ); 124 $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) ); 125 $this->assertFalse( is_user_member_of_blog( $user1_id, $blog_id ) ); 126 } 127 128 wpmu_delete_user( $user1_id ); 129 $user = new WP_User( $user1_id ); 130 $this->assertFalse( $user->exists(), 'WP_User->exists' ); 131 $this->assertFalse( is_user_member_of_blog( $user1_id ), 'is_user_member_of_blog' ); 132 133 wp_set_current_user( $old_current ); 134 } 135 136 /** 137 * @ticket 20601 138 */ 139 function test_user_member_of_blog() { 140 global $wp_rewrite; 141 142 $this->factory->blog->create(); 143 $user_id = $this->factory->user->create(); 144 $this->factory->blog->create( array( 'user_id' => $user_id ) ); 145 146 $blogs = get_blogs_of_user( $user_id ); 147 $this->assertCount( 2, $blogs ); 148 $first = reset( $blogs )->userblog_id; 149 remove_user_from_blog( $user_id, $first ); 150 151 $blogs = get_blogs_of_user( $user_id ); 152 $second = reset( $blogs )->userblog_id; 153 $this->assertCount( 1, $blogs ); 154 155 switch_to_blog( $first ); 156 $wp_rewrite->init(); 157 158 $this->go_to( get_author_posts_url( $user_id ) ); 159 $this->assertQueryTrue( 'is_404' ); 160 161 switch_to_blog( $second ); 162 $wp_rewrite->init(); 163 164 $this->go_to( get_author_posts_url( $user_id ) ); 165 $this->assertQueryTrue( 'is_author', 'is_archive' ); 166 167 add_user_to_blog( $first, $user_id, 'administrator' ); 168 $blogs = get_blogs_of_user( $user_id ); 169 $this->assertCount( 2, $blogs ); 170 171 switch_to_blog( $first ); 172 $wp_rewrite->init(); 173 174 $this->go_to( get_author_posts_url( $user_id ) ); 175 $this->assertQueryTrue( 'is_author', 'is_archive' ); 176 } 177 178 /** 179 * @ticket 23192 180 */ 181 function test_is_user_spammy() { 182 $user_id = $this->factory->user->create( array( 183 'role' => 'author', 184 'user_login' => 'testuser1', 185 ) ); 186 187 $spam_username = (string) $user_id; 188 $spam_user_id = $this->factory->user->create( array( 189 'role' => 'author', 190 'user_login' => $spam_username, 191 ) ); 192 update_user_status( $spam_user_id, 'spam', '1' ); 193 194 $this->assertTrue( is_user_spammy( $spam_username ) ); 195 $this->assertFalse( is_user_spammy( 'testuser1' ) ); 196 } 197 } 198 199 endif; 200 No newline at end of file