forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite.php
More file actions
2544 lines (2112 loc) · 73.3 KB
/
site.php
File metadata and controls
2544 lines (2112 loc) · 73.3 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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( is_multisite() ) :
/**
* Tests specific to sites in multisite.
*
* @group ms-site
* @group multisite
*/
class Tests_Multisite_Site extends WP_UnitTestCase {
protected $suppress = false;
protected $site_status_hooks = array();
protected $wp_initialize_site_args = array();
protected $wp_initialize_site_meta = array();
protected static $network_ids;
protected static $site_ids;
protected static $uninitialized_site_id;
function setUp() {
global $wpdb;
parent::setUp();
$this->suppress = $wpdb->suppress_errors();
}
function tearDown() {
global $wpdb;
$wpdb->suppress_errors( $this->suppress );
parent::tearDown();
}
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$network_ids = array(
'make.wordpress.org/' => array(
'domain' => 'make.wordpress.org',
'path' => '/',
),
);
foreach ( self::$network_ids as &$id ) {
$id = $factory->network->create( $id );
}
unset( $id );
self::$site_ids = array(
'make.wordpress.org/' => array(
'domain' => 'make.wordpress.org',
'path' => '/',
'network_id' => self::$network_ids['make.wordpress.org/'],
),
'make.wordpress.org/foo/' => array(
'domain' => 'make.wordpress.org',
'path' => '/foo/',
'network_id' => self::$network_ids['make.wordpress.org/'],
),
);
foreach ( self::$site_ids as &$id ) {
$id = $factory->blog->create( $id );
}
unset( $id );
remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
self::$uninitialized_site_id = wp_insert_site(
array(
'domain' => 'uninitialized.org',
'path' => '/',
'network_id' => self::$network_ids['make.wordpress.org/'],
)
);
add_action( 'wp_initialize_site', 'wp_initialize_site', 10, 2 );
}
public static function wpTearDownAfterClass() {
global $wpdb;
remove_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10 );
wp_delete_site( self::$uninitialized_site_id );
add_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10, 1 );
foreach ( self::$site_ids as $id ) {
wp_delete_site( $id );
}
foreach ( self::$network_ids as $id ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
}
}
function test_switch_restore_blog() {
global $_wp_switched_stack, $wpdb;
$this->assertSame( array(), $_wp_switched_stack );
$this->assertFalse( ms_is_switched() );
$current_blog_id = get_current_blog_id();
$this->assertIsInt( $current_blog_id );
wp_cache_set( 'switch-test', $current_blog_id, 'switch-test' );
$this->assertSame( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
$blog_id = self::factory()->blog->create();
$cap_key = wp_get_current_user()->cap_key;
switch_to_blog( $blog_id );
$this->assertNotEquals( $cap_key, wp_get_current_user()->cap_key );
$this->assertSame( array( $current_blog_id ), $_wp_switched_stack );
$this->assertTrue( ms_is_switched() );
$this->assertSame( $blog_id, $wpdb->blogid );
$this->assertFalse( wp_cache_get( 'switch-test', 'switch-test' ) );
wp_cache_set( 'switch-test', $blog_id, 'switch-test' );
$this->assertSame( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
switch_to_blog( $blog_id );
$this->assertSame( array( $current_blog_id, $blog_id ), $_wp_switched_stack );
$this->assertTrue( ms_is_switched() );
$this->assertSame( $blog_id, $wpdb->blogid );
$this->assertSame( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
restore_current_blog();
$this->assertSame( array( $current_blog_id ), $_wp_switched_stack );
$this->assertTrue( ms_is_switched() );
$this->assertSame( $blog_id, $wpdb->blogid );
$this->assertSame( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
restore_current_blog();
$this->assertSame( $cap_key, wp_get_current_user()->cap_key );
$this->assertSame( $current_blog_id, get_current_blog_id() );
$this->assertSame( array(), $_wp_switched_stack );
$this->assertFalse( ms_is_switched() );
$this->assertSame( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
$this->assertFalse( restore_current_blog() );
}
/**
* Test the cache keys and database tables setup through the creation of a site.
*/
function test_created_site_details() {
global $wpdb;
$blog_id = self::factory()->blog->create();
$this->assertIsInt( $blog_id );
$prefix = $wpdb->get_blog_prefix( $blog_id );
// $get_all = false, only retrieve details from the blogs table.
$details = get_blog_details( $blog_id, false );
// Combine domain and path for a site specific cache key.
$key = md5( $details->domain . $details->path );
$this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
// get_blogaddress_by_name().
$this->assertSame( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
// These are empty until get_blog_details() is called with $get_all = true.
$this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
$this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
// $get_all = true, populate the full blog-details cache and the blog slug lookup cache.
$details = get_blog_details( $blog_id, true );
$this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
$this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
// Check existence of each database table for the created site.
foreach ( $wpdb->tables( 'blog', false ) as $table ) {
$suppress = $wpdb->suppress_errors();
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
$wpdb->suppress_errors( $suppress );
// The table should exist.
$this->assertNotEmpty( $table_fields );
// And the table should not be empty, unless commentmeta, termmeta, or links.
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
if ( 'commentmeta' === $table || 'termmeta' === $table || 'links' === $table ) {
$this->assertEmpty( $result );
} else {
$this->assertNotEmpty( $result );
}
}
// Update the blog count cache to use get_blog_count().
wp_update_network_counts();
$this->assertSame( 2, (int) get_blog_count() );
}
public function test_site_caches_should_invalidate_when_invalidation_is_not_suspended() {
$site_id = self::factory()->blog->create();
$details = get_site( $site_id );
$suspend = wp_suspend_cache_invalidation( false );
update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
$new_details = get_site( $site_id );
wp_suspend_cache_invalidation( $suspend );
$this->assertNotEquals( $details->path, $new_details->path );
}
public function test_site_caches_should_not_invalidate_when_invalidation_is_suspended() {
$site_id = self::factory()->blog->create();
$details = get_site( $site_id );
$suspend = wp_suspend_cache_invalidation();
update_blog_details( $site_id, array( 'path' => '/a-non-random-test-path/' ) );
$new_details = get_site( $site_id );
wp_suspend_cache_invalidation( $suspend );
$this->assertSame( $details->path, $new_details->path );
}
/**
* When a site is flagged as 'deleted', its data should be cleared from cache.
*/
function test_data_in_cache_after_wpmu_delete_blog_drop_false() {
$blog_id = self::factory()->blog->create();
$details = get_blog_details( $blog_id, false );
$key = md5( $details->domain . $details->path );
// Delete the site without forcing a table drop.
wpmu_delete_blog( $blog_id, false );
$this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
$this->assertFalse( wp_cache_get( $blog_id . 'short', 'blog-details' ) );
$this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
$this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
}
/**
* When a site is flagged as 'deleted', its data should remain in the database.
*/
function test_data_in_tables_after_wpmu_delete_blog_drop_false() {
global $wpdb;
$blog_id = self::factory()->blog->create();
// Delete the site without forcing a table drop.
wpmu_delete_blog( $blog_id, false );
$prefix = $wpdb->get_blog_prefix( $blog_id );
foreach ( $wpdb->tables( 'blog', false ) as $table ) {
$suppress = $wpdb->suppress_errors();
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
$wpdb->suppress_errors( $suppress );
$this->assertNotEmpty( $table_fields, $prefix . $table );
}
}
/**
* When a site is fully deleted, its data should be cleared from cache.
*/
function test_data_in_cache_after_wpmu_delete_blog_drop_true() {
$blog_id = self::factory()->blog->create();
$details = get_blog_details( $blog_id, false );
$key = md5( $details->domain . $details->path );
// Delete the site and force a table drop.
wpmu_delete_blog( $blog_id, true );
$this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
$this->assertFalse( wp_cache_get( $blog_id . 'short', 'blog-details' ) );
$this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
$this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
}
/**
* When a site is fully deleted, its data should be removed from the database.
*/
function test_data_in_tables_after_wpmu_delete_blog_drop_true() {
global $wpdb;
$blog_id = self::factory()->blog->create();
// Delete the site and force a table drop.
wpmu_delete_blog( $blog_id, true );
$prefix = $wpdb->get_blog_prefix( $blog_id );
foreach ( $wpdb->tables( 'blog', false ) as $table ) {
$suppress = $wpdb->suppress_errors();
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
$wpdb->suppress_errors( $suppress );
$this->assertEmpty( $table_fields );
}
}
/**
* When the main site of a network is fully deleted, its data should be cleared from cache.
*/
function test_data_in_cache_after_wpmu_delete_blog_main_site_drop_true() {
$blog_id = 1; // The main site in our test suite has an ID of 1.
$details = get_blog_details( $blog_id, false );
$key = md5( $details->domain . $details->path );
// Delete the site and force a table drop.
wpmu_delete_blog( $blog_id, true );
$this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
$this->assertFalse( wp_cache_get( $blog_id . 'short', 'blog-details' ) );
$this->assertFalse( wp_cache_get( $key, 'blog-lookup' ) );
$this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
}
/**
* When the main site of a network is fully deleted, its data should remain in the database.
*/
function test_data_in_tables_after_wpmu_delete_blog_main_site_drop_true() {
global $wpdb;
$blog_id = 1; // The main site in our test suite has an ID of 1.
// Delete the site and force a table drop.
wpmu_delete_blog( $blog_id, true );
$prefix = $wpdb->get_blog_prefix( $blog_id );
foreach ( $wpdb->tables( 'blog', false ) as $table ) {
$suppress = $wpdb->suppress_errors();
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
$wpdb->suppress_errors( $suppress );
$this->assertNotEmpty( $table_fields, $prefix . $table );
}
}
/**
* The site count of a network should change when a site is flagged as 'deleted'.
*/
function test_network_count_after_wpmu_delete_blog_drop_false() {
$blog_id = self::factory()->blog->create();
// Delete the site without forcing a table drop.
wpmu_delete_blog( $blog_id, false );
// Update the blog count cache to use get_blog_count().
wp_update_network_counts();
$this->assertSame( 1, get_blog_count() );
}
/**
* The site count of a network should change when a site is fully deleted.
*/
function test_blog_count_after_wpmu_delete_blog_drop_true() {
$blog_id = self::factory()->blog->create();
// Delete the site and force a table drop.
wpmu_delete_blog( $blog_id, true );
// Update the blog count cache to use get_blog_count().
wp_update_network_counts();
$this->assertSame( 1, get_blog_count() );
}
/**
* When a site is deleted with wpmu_delete_blog(), only the files associated with
* that site should be removed. When wpmu_delete_blog() is run a second time, nothing
* should change with upload directories.
*/
function test_upload_directories_after_multiple_wpmu_delete_blog() {
$filename = __FUNCTION__ . '.jpg';
$contents = __FUNCTION__ . '_contents';
// Upload a file to the main site on the network.
$file1 = wp_upload_bits( $filename, null, $contents );
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );
$file2 = wp_upload_bits( $filename, null, $contents );
restore_current_blog();
wpmu_delete_blog( $blog_id, true );
// The file on the main site should still exist. The file on the deleted site should not.
$this->assertFileExists( $file1['file'] );
$this->assertFileNotExists( $file2['file'] );
wpmu_delete_blog( $blog_id, true );
// The file on the main site should still exist. The file on the deleted site should not.
$this->assertFileExists( $file1['file'] );
$this->assertFileNotExists( $file2['file'] );
}
function test_wpmu_update_blogs_date() {
global $wpdb;
wpmu_update_blogs_date();
$blog = get_site( get_current_blog_id() );
$current_time = time();
// Compare the update time with the current time, allow delta < 2.
$this->assertEqualsWithDelta( $current_time, strtotime( $blog->last_updated ), 2, 'The dates should be equal' );
}
/**
* Provide a counter to determine that hooks are firing when intended.
*/
function _action_counter_cb() {
global $test_action_counter;
$test_action_counter++;
}
/**
* Test cached data for a site that does not exist and then again after it exists.
*
* @ticket 23405
*/
function test_get_blog_details_when_site_does_not_exist() {
// Create an unused site so that we can then assume an invalid site ID.
$blog_id = self::factory()->blog->create();
$blog_id++;
// Prime the cache for an invalid site.
get_blog_details( $blog_id );
// When the cache is primed with an invalid site, the value is set to -1.
$this->assertSame( -1, wp_cache_get( $blog_id, 'blog-details' ) );
// Create a site in the invalid site's place.
self::factory()->blog->create();
// When a new site is created, its cache is cleared through refresh_blog_details.
$this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
$blog = get_blog_details( $blog_id );
// When the cache is refreshed, it should now equal the site data.
$this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
}
/**
* Updating a field returns the sme value that was passed.
*/
function test_update_blog_status() {
$result = update_blog_status( 1, 'spam', 0 );
$this->assertSame( 0, $result );
}
/**
* Updating an invalid field returns the same value that was passed.
*/
function test_update_blog_status_invalid_status() {
$result = update_blog_status( 1, 'doesnotexist', 'invalid' );
$this->assertSame( 'invalid', $result );
}
function test_update_blog_status_make_ham_blog_action() {
global $test_action_counter;
$test_action_counter = 0;
$blog_id = self::factory()->blog->create();
update_blog_details( $blog_id, array( 'spam' => 1 ) );
add_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 );
update_blog_status( $blog_id, 'spam', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->spam );
$this->assertSame( 1, $test_action_counter );
// The action should not fire if the status of 'spam' stays the same.
update_blog_status( $blog_id, 'spam', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->spam );
$this->assertSame( 1, $test_action_counter );
remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 );
}
function test_content_from_spam_blog_is_not_available() {
$spam_blog_id = self::factory()->blog->create();
switch_to_blog( $spam_blog_id );
$post_data = array(
'post_title' => 'Hello World!',
'post_content' => 'Hello world content',
);
$post_id = self::factory()->post->create( $post_data );
$post = get_post( $post_id );
$spam_permalink = site_url() . '/?p=' . $post->ID;
$spam_embed_url = get_post_embed_url( $post_id );
restore_current_blog();
$this->assertNotEmpty( $spam_permalink );
$this->assertSame( $post_data['post_title'], $post->post_title );
update_blog_status( $spam_blog_id, 'spam', 1 );
$post_id = self::factory()->post->create(
array(
'post_content' => "\n $spam_permalink \n",
)
);
$post = get_post( $post_id );
$content = apply_filters( 'the_content', $post->post_content );
$this->assertStringNotContainsString( $post_data['post_title'], $content );
$this->assertStringNotContainsString( "src=\"{$spam_embed_url}#?", $content );
}
function test_update_blog_status_make_spam_blog_action() {
global $test_action_counter;
$test_action_counter = 0;
$blog_id = self::factory()->blog->create();
add_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 );
update_blog_status( $blog_id, 'spam', 1 );
$blog = get_site( $blog_id );
$this->assertSame( '1', $blog->spam );
$this->assertSame( 1, $test_action_counter );
// The action should not fire if the status of 'spam' stays the same.
update_blog_status( $blog_id, 'spam', 1 );
$blog = get_site( $blog_id );
$this->assertSame( '1', $blog->spam );
$this->assertSame( 1, $test_action_counter );
remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 );
}
function test_update_blog_status_archive_blog_action() {
global $test_action_counter;
$test_action_counter = 0;
$blog_id = self::factory()->blog->create();
add_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 );
update_blog_status( $blog_id, 'archived', 1 );
$blog = get_site( $blog_id );
$this->assertSame( '1', $blog->archived );
$this->assertSame( 1, $test_action_counter );
// The action should not fire if the status of 'archived' stays the same.
update_blog_status( $blog_id, 'archived', 1 );
$blog = get_site( $blog_id );
$this->assertSame( '1', $blog->archived );
$this->assertSame( 1, $test_action_counter );
remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 );
}
function test_update_blog_status_unarchive_blog_action() {
global $test_action_counter;
$test_action_counter = 0;
$blog_id = self::factory()->blog->create();
update_blog_details( $blog_id, array( 'archived' => 1 ) );
add_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 );
update_blog_status( $blog_id, 'archived', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->archived );
$this->assertSame( 1, $test_action_counter );
// The action should not fire if the status of 'archived' stays the same.
update_blog_status( $blog_id, 'archived', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->archived );
$this->assertSame( 1, $test_action_counter );
remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 );
}
function test_update_blog_status_make_delete_blog_action() {
global $test_action_counter;
$test_action_counter = 0;
$blog_id = self::factory()->blog->create();
add_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 );
update_blog_status( $blog_id, 'deleted', 1 );
$blog = get_site( $blog_id );
$this->assertSame( '1', $blog->deleted );
$this->assertSame( 1, $test_action_counter );
// The action should not fire if the status of 'deleted' stays the same.
update_blog_status( $blog_id, 'deleted', 1 );
$blog = get_site( $blog_id );
$this->assertSame( '1', $blog->deleted );
$this->assertSame( 1, $test_action_counter );
remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 );
}
function test_update_blog_status_make_undelete_blog_action() {
global $test_action_counter;
$test_action_counter = 0;
$blog_id = self::factory()->blog->create();
update_blog_details( $blog_id, array( 'deleted' => 1 ) );
add_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 );
update_blog_status( $blog_id, 'deleted', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->deleted );
$this->assertSame( 1, $test_action_counter );
// The action should not fire if the status of 'deleted' stays the same.
update_blog_status( $blog_id, 'deleted', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->deleted );
$this->assertSame( 1, $test_action_counter );
remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 );
}
function test_update_blog_status_mature_blog_action() {
global $test_action_counter;
$test_action_counter = 0;
$blog_id = self::factory()->blog->create();
add_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 );
update_blog_status( $blog_id, 'mature', 1 );
$blog = get_site( $blog_id );
$this->assertSame( '1', $blog->mature );
$this->assertSame( 1, $test_action_counter );
// The action should not fire if the status of 'mature' stays the same.
update_blog_status( $blog_id, 'mature', 1 );
$blog = get_site( $blog_id );
$this->assertSame( '1', $blog->mature );
$this->assertSame( 1, $test_action_counter );
remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 );
}
function test_update_blog_status_unmature_blog_action() {
global $test_action_counter;
$test_action_counter = 0;
$blog_id = self::factory()->blog->create();
update_blog_details( $blog_id, array( 'mature' => 1 ) );
add_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 );
update_blog_status( $blog_id, 'mature', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->mature );
$this->assertSame( 1, $test_action_counter );
// The action should not fire if the status of 'mature' stays the same.
update_blog_status( $blog_id, 'mature', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->mature );
$this->assertSame( 1, $test_action_counter );
remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 );
}
function test_update_blog_status_update_blog_public_action() {
global $test_action_counter;
$test_action_counter = 0;
$blog_id = self::factory()->blog->create();
add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 );
update_blog_status( $blog_id, 'public', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->public );
$this->assertSame( 1, $test_action_counter );
// The action should not fire if the status of 'mature' stays the same.
update_blog_status( $blog_id, 'public', 0 );
$blog = get_site( $blog_id );
$this->assertSame( '0', $blog->public );
$this->assertSame( 1, $test_action_counter );
remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 );
}
/**
* @ticket 27952
*/
function test_posts_count() {
self::factory()->post->create();
$post2 = self::factory()->post->create();
$this->assertSame( 2, get_site()->post_count );
wp_delete_post( $post2 );
$this->assertSame( 1, get_site()->post_count );
}
/**
* @ticket 26410
*/
function test_blog_details_cache_invalidation() {
update_option( 'blogname', 'foo' );
$details = get_site( get_current_blog_id() );
$this->assertSame( 'foo', $details->blogname );
update_option( 'blogname', 'bar' );
$details = get_site( get_current_blog_id() );
$this->assertSame( 'bar', $details->blogname );
}
/**
* Test the original and cached responses for a created and then deleted site when
* the blog ID is requested through get_blog_id_from_url().
*/
function test_get_blog_id_from_url() {
$blog_id = self::factory()->blog->create();
$details = get_site( $blog_id );
$key = md5( $details->domain . $details->path );
// Test the original response and cached response for the newly created site.
$this->assertSame( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
$this->assertSame( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
}
/**
* Test the case insensitivity of the site lookup.
*/
function test_get_blog_id_from_url_is_case_insensitive() {
$blog_id = self::factory()->blog->create(
array(
'domain' => 'example.com',
'path' => '/xyz',
)
);
$details = get_site( $blog_id );
$this->assertSame( $blog_id, get_blog_id_from_url( strtoupper( $details->domain ), strtoupper( $details->path ) ) );
}
/**
* Test the first and cached responses for a site that does not exist.
*/
function test_get_blog_id_from_url_that_does_not_exist() {
$blog_id = self::factory()->blog->create( array( 'path' => '/xyz' ) );
$details = get_site( $blog_id );
$this->assertSame( 0, get_blog_id_from_url( $details->domain, 'foo' ) );
$this->assertSame( -1, wp_cache_get( md5( $details->domain . 'foo' ), 'blog-id-cache' ) );
}
/**
* A blog ID is still available if only the `deleted` flag is set for a site. The same
* behavior would be expected if passing `false` explicitly to `wpmu_delete_blog()`.
*/
function test_get_blog_id_from_url_with_deleted_flag() {
$blog_id = self::factory()->blog->create();
$details = get_site( $blog_id );
$key = md5( $details->domain . $details->path );
wpmu_delete_blog( $blog_id );
$this->assertSame( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
$this->assertSame( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
}
/**
* When deleted with the drop parameter as true, the cache will first be false, then set to
* -1 after an attempt at `get_blog_id_from_url()` is made.
*/
function test_get_blog_id_from_url_after_dropped() {
$blog_id = self::factory()->blog->create();
$details = get_site( $blog_id );
$key = md5( $details->domain . $details->path );
wpmu_delete_blog( $blog_id, true );
$this->assertFalse( wp_cache_get( $key, 'blog-id-cache' ) );
$this->assertSame( 0, get_blog_id_from_url( $details->domain, $details->path ) );
$this->assertSame( -1, wp_cache_get( $key, 'blog-id-cache' ) );
}
/**
* Test with default parameter of site_id as null.
*/
function test_is_main_site() {
$this->assertTrue( is_main_site() );
}
/**
* Test with a site id of get_current_blog_id(), which should be the same as the
* default parameter tested above.
*/
function test_current_blog_id_is_main_site() {
$this->assertTrue( is_main_site( get_current_blog_id() ) );
}
/**
* Test with a site ID other than the main site to ensure a false response.
*/
function test_is_main_site_is_false_with_other_blog_id() {
$blog_id = self::factory()->blog->create();
$this->assertFalse( is_main_site( $blog_id ) );
}
/**
* Test with no passed ID after switching to another site ID.
*/
function test_is_main_site_is_false_after_switch_to_blog() {
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );
$this->assertFalse( is_main_site() );
restore_current_blog();
}
function test_switch_upload_dir() {
$this->assertTrue( is_main_site() );
$site = get_current_site();
$info = wp_upload_dir();
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['url'] );
$this->assertSame( ABSPATH . 'wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['path'] );
$this->assertSame( gmstrftime( '/%Y/%m' ), $info['subdir'] );
$this->assertFalse( $info['error'] );
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );
$info = wp_upload_dir();
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime( '%Y/%m' ), $info['url'] );
$this->assertSame( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime( '%Y/%m' ), $info['path'] );
$this->assertSame( gmstrftime( '/%Y/%m' ), $info['subdir'] );
$this->assertFalse( $info['error'] );
restore_current_blog();
$info = wp_upload_dir();
$this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['url'] );
$this->assertSame( ABSPATH . 'wp-content/uploads/' . gmstrftime( '%Y/%m' ), $info['path'] );
$this->assertSame( gmstrftime( '/%Y/%m' ), $info['subdir'] );
$this->assertFalse( $info['error'] );
}
/**
* Test the primary purpose of get_blog_post(), to retrieve a post from
* another site on the network.
*/
function test_get_blog_post_from_another_site_on_network() {
$blog_id = self::factory()->blog->create();
$post_id = self::factory()->post->create(); // Create a post on the primary site, ID 1.
$post = get_post( $post_id );
switch_to_blog( $blog_id );
// The post created and retrieved on the main site should match the one retrieved "remotely".
$this->assertEquals( $post, get_blog_post( 1, $post_id ) );
restore_current_blog();
}
/**
* If get_blog_post() is used on the same site, it should still work.
*/
function test_get_blog_post_from_same_site() {
$post_id = self::factory()->post->create();
$this->assertEquals( get_blog_post( 1, $post_id ), get_post( $post_id ) );
}
/**
* A null response should be returned if an invalid post is requested.
*/
function test_get_blog_post_invalid_returns_null() {
$this->assertNull( get_blog_post( 1, 999999 ) );
}
/**
* Added as a callback to the domain_exists filter to provide manual results for
* the testing of the filter and for a test which does not need the database.
*/
function _domain_exists_cb( $exists, $domain, $path, $site_id ) {
if ( 'foo' === $domain && 'bar/' === $path ) {
return 1234;
} else {
return null;
}
}
function test_domain_exists_with_default_site_id() {
$details = get_site( 1 );
$this->assertSame( 1, domain_exists( $details->domain, $details->path ) );
}
function test_domain_exists_with_specified_site_id() {
$details = get_site( 1 );
$this->assertSame( 1, domain_exists( $details->domain, $details->path, $details->site_id ) );
}
/**
* When the domain is valid, but the resulting site does not belong to the specified network,
* it is marked as not existing.
*/
function test_domain_does_not_exist_with_invalid_site_id() {
$details = get_site( 1 );
$this->assertNull( domain_exists( $details->domain, $details->path, 999 ) );
}
function test_invalid_domain_does_not_exist_with_default_site_id() {
$this->assertNull( domain_exists( 'foo', 'bar' ) );
}
function test_domain_filtered_to_exist() {
add_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
$exists = domain_exists( 'foo', 'bar' );
remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
$this->assertSame( 1234, $exists );
}
/**
* When a path is passed to domain_exists, it is immediately trailing slashed. A path
* value with or without the slash should result in the same return value.
*/
function test_slashed_path_in_domain_exists() {
add_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
$exists1 = domain_exists( 'foo', 'bar' );
$exists2 = domain_exists( 'foo', 'bar/' );
remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
// Make sure the same result is returned with or without a trailing slash.
$this->assertSame( $exists1, $exists2 );
}
/**
* Tests returning an address for a given valid ID.
*/
function test_get_blogaddress_by_id_with_valid_id() {
$blogaddress = get_blogaddress_by_id( 1 );
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/', $blogaddress );
}
/**
* Tests returning the appropriate response for a invalid id given.
*/
function test_get_blogaddress_by_id_with_invalid_id() {
$blogaddress = get_blogaddress_by_id( 42 );
$this->assertSame( '', $blogaddress );
}
/**
* @ticket 14867
*/
function test_get_blogaddress_by_id_scheme_reflects_blog_scheme() {
$blog = self::factory()->blog->create();
$this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
update_blog_option( $blog, 'home', set_url_scheme( get_blog_option( $blog, 'home' ), 'https' ) );
$this->assertSame( 'https', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
}
/**
* @ticket 14867
*/
function test_get_blogaddress_by_id_scheme_is_unaffected_by_request() {
$blog = self::factory()->blog->create();
$this->assertFalse( is_ssl() );
$this->assertSame( 'http', parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME ) );
$_SERVER['HTTPS'] = 'on';
$is_ssl = is_ssl();
$address = parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME );
$this->assertTrue( $is_ssl );
$this->assertSame( 'http', $address );