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