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