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