forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultisite.php
More file actions
461 lines (351 loc) · 13.5 KB
/
multisite.php
File metadata and controls
461 lines (351 loc) · 13.5 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
<?php
if ( is_multisite() ) :
/**
* Tests specific to users in multisite.
*
* @group user
* @group ms-user
* @group multisite
*/
class Tests_Multisite_User extends WP_UnitTestCase {
protected $suppress = false;
function setUp() {
global $wpdb;
parent::setUp();
$this->suppress = $wpdb->suppress_errors();
}
function tearDown() {
global $wpdb;
$wpdb->suppress_errors( $this->suppress );
parent::tearDown();
}
function test_remove_user_from_blog() {
$user1 = self::factory()->user->create_and_get();
$user2 = self::factory()->user->create_and_get();
$post_id = self::factory()->post->create( array( 'post_author' => $user1->ID ) );
remove_user_from_blog( $user1->ID, 1, $user2->ID );
$post = get_post( $post_id );
$this->assertNotEquals( $user1->ID, $post->post_author );
$this->assertEquals( $user2->ID, $post->post_author );
}
/**
* Test the returned data from get_blogs_of_user()
*/
function test_get_blogs_of_user() {
$user1_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
// Maintain a list of 6 total sites and include the primary network site.
$blog_ids = self::factory()->blog->create_many( 5, array( 'user_id' => $user1_id ) );
$blog_ids = array_merge( array( 1 ), $blog_ids );
// All sites are new and not marked as spam, archived, or deleted.
$blog_ids_of_user = array_keys( get_blogs_of_user( $user1_id ) );
// User should be a member of the created sites and the network's initial site.
$this->assertEquals( $blog_ids, $blog_ids_of_user );
$this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[0] ) );
$this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[2] ) );
$this->assertTrue( remove_user_from_blog( $user1_id, $blog_ids[4] ) );
unset( $blog_ids[0] );
unset( $blog_ids[2] );
unset( $blog_ids[4] );
sort( $blog_ids );
$blogs_of_user = get_blogs_of_user( $user1_id, false );
// The user should still be a member of all remaining sites.
$blog_ids_of_user = array_keys( $blogs_of_user );
$this->assertEquals( $blog_ids, $blog_ids_of_user );
// Each site retrieved should match the expected structure.
foreach ( $blogs_of_user as $blog_id => $blog ) {
$this->assertEquals( $blog_id, $blog->userblog_id );
$this->assertTrue( isset( $blog->userblog_id ) );
$this->assertTrue( isset( $blog->blogname ) );
$this->assertTrue( isset( $blog->domain ) );
$this->assertTrue( isset( $blog->path ) );
$this->assertTrue( isset( $blog->site_id ) );
$this->assertTrue( isset( $blog->siteurl ) );
$this->assertTrue( isset( $blog->archived ) );
$this->assertTrue( isset( $blog->spam ) );
$this->assertTrue( isset( $blog->deleted ) );
}
// Mark each remaining site as spam, archived, and deleted.
update_blog_details( $blog_ids[0], array( 'spam' => 1 ) );
update_blog_details( $blog_ids[1], array( 'archived' => 1 ) );
update_blog_details( $blog_ids[2], array( 'deleted' => 1 ) );
// Passing true as the second parameter should retrieve ALL sites, even if marked.
$blogs_of_user = get_blogs_of_user( $user1_id, true );
$blog_ids_of_user = array_keys( $blogs_of_user );
$this->assertEquals( $blog_ids, $blog_ids_of_user );
// Check if sites are flagged as expected.
$this->assertEquals( 1, $blogs_of_user[ $blog_ids[0] ]->spam );
$this->assertEquals( 1, $blogs_of_user[ $blog_ids[1] ]->archived );
$this->assertEquals( 1, $blogs_of_user[ $blog_ids[2] ]->deleted );
unset( $blog_ids[0] );
unset( $blog_ids[1] );
unset( $blog_ids[2] );
sort( $blog_ids );
// Passing false (the default) as the second parameter should retrieve only good sites.
$blog_ids_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
$this->assertEquals( $blog_ids, $blog_ids_of_user );
}
/**
* @expectedDeprecated is_blog_user
*/
function test_is_blog_user() {
global $wpdb;
$user1_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
$old_current = get_current_user_id();
wp_set_current_user( $user1_id );
$this->assertTrue( is_blog_user() );
$this->assertTrue( is_blog_user( get_current_blog_id() ) );
$blog_ids = array();
$blog_ids = self::factory()->blog->create_many( 1 );
foreach ( $blog_ids as $blog_id ) {
$this->assertInternalType( 'int', $blog_id );
$this->assertTrue( is_blog_user( $blog_id ) );
$this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
$this->assertFalse( is_blog_user( $blog_id ) );
}
wp_set_current_user( $old_current );
}
function test_is_user_member_of_blog() {
global $wpdb;
$user1_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
$user2_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
$old_current = get_current_user_id();
$this->assertSame( 0, $old_current );
// test for "get current user" when not logged in
$this->assertFalse( is_user_member_of_blog() );
wp_set_current_user( $user1_id );
$site_id = get_current_blog_id();
$this->assertTrue( is_user_member_of_blog() );
$this->assertTrue( is_user_member_of_blog( 0, 0 ) );
$this->assertTrue( is_user_member_of_blog( 0, $site_id ) );
$this->assertTrue( is_user_member_of_blog( $user1_id ) );
$this->assertTrue( is_user_member_of_blog( $user1_id, $site_id ) );
$blog_ids = self::factory()->blog->create_many( 1 );
foreach ( $blog_ids as $blog_id ) {
$this->assertInternalType( 'int', $blog_id );
// Current user gets added to new blogs
$this->assertTrue( is_user_member_of_blog( $user1_id, $blog_id ) );
// Other users should not
$this->assertFalse( is_user_member_of_blog( $user2_id, $blog_id ) );
switch_to_blog( $blog_id );
$this->assertTrue( is_user_member_of_blog( $user1_id ) );
$this->assertFalse( is_user_member_of_blog( $user2_id ) );
// Remove user 1 from blog
$this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
// Add user 2 to blog
$this->assertTrue( add_user_to_blog( $blog_id, $user2_id, 'subscriber' ) );
$this->assertFalse( is_user_member_of_blog( $user1_id ) );
$this->assertTrue( is_user_member_of_blog( $user2_id ) );
restore_current_blog();
$this->assertFalse( is_user_member_of_blog( $user1_id, $blog_id ) );
$this->assertTrue( is_user_member_of_blog( $user2_id, $blog_id ) );
}
wpmu_delete_user( $user1_id );
$user = new WP_User( $user1_id );
$this->assertFalse( $user->exists() );
$this->assertFalse( is_user_member_of_blog( $user1_id ) );
wp_set_current_user( $old_current );
}
/**
* @ticket 23192
*/
function test_is_user_spammy() {
$user_id = self::factory()->user->create(
array(
'role' => 'author',
'user_login' => 'testuser1',
)
);
$spam_username = (string) $user_id;
$spam_user_id = self::factory()->user->create(
array(
'role' => 'author',
'user_login' => $spam_username,
)
);
update_user_status( $spam_user_id, 'spam', '1' );
$this->assertTrue( is_user_spammy( $spam_username ) );
$this->assertFalse( is_user_spammy( 'testuser1' ) );
}
/**
* @ticket 20601
*/
function test_user_member_of_blog() {
global $wp_rewrite;
self::factory()->blog->create();
$user_id = self::factory()->user->create();
self::factory()->blog->create( array( 'user_id' => $user_id ) );
$blogs = get_blogs_of_user( $user_id );
$this->assertCount( 2, $blogs );
$first = reset( $blogs )->userblog_id;
remove_user_from_blog( $user_id, $first );
$blogs = get_blogs_of_user( $user_id );
$second = reset( $blogs )->userblog_id;
$this->assertCount( 1, $blogs );
switch_to_blog( $first );
$wp_rewrite->init();
$this->go_to( get_author_posts_url( $user_id ) );
$this->assertQueryTrue( 'is_404' );
switch_to_blog( $second );
$wp_rewrite->init();
$this->go_to( get_author_posts_url( $user_id ) );
$this->assertQueryTrue( 'is_author', 'is_archive' );
add_user_to_blog( $first, $user_id, 'administrator' );
$blogs = get_blogs_of_user( $user_id );
$this->assertCount( 2, $blogs );
switch_to_blog( $first );
$wp_rewrite->init();
$this->go_to( get_author_posts_url( $user_id ) );
$this->assertQueryTrue( 'is_author', 'is_archive' );
}
function test_revoked_super_admin_can_be_deleted() {
if ( isset( $GLOBALS['super_admins'] ) ) {
$old_global = $GLOBALS['super_admins'];
unset( $GLOBALS['super_admins'] );
}
$user_id = self::factory()->user->create();
grant_super_admin( $user_id );
revoke_super_admin( $user_id );
$this->assertTrue( wpmu_delete_user( $user_id ) );
if ( isset( $old_global ) ) {
$GLOBALS['super_admins'] = $old_global;
}
}
function test_revoked_super_admin_is_deleted() {
if ( isset( $GLOBALS['super_admins'] ) ) {
$old_global = $GLOBALS['super_admins'];
unset( $GLOBALS['super_admins'] );
}
$user_id = self::factory()->user->create();
grant_super_admin( $user_id );
revoke_super_admin( $user_id );
wpmu_delete_user( $user_id );
$user = new WP_User( $user_id );
$this->assertFalse( $user->exists(), 'WP_User->exists' );
if ( isset( $old_global ) ) {
$GLOBALS['super_admins'] = $old_global;
}
}
function test_super_admin_cannot_be_deleted() {
if ( isset( $GLOBALS['super_admins'] ) ) {
$old_global = $GLOBALS['super_admins'];
unset( $GLOBALS['super_admins'] );
}
$user_id = self::factory()->user->create();
grant_super_admin( $user_id );
$this->assertFalse( wpmu_delete_user( $user_id ) );
if ( isset( $old_global ) ) {
$GLOBALS['super_admins'] = $old_global;
}
}
/**
* @ticket 27205
*/
function test_granting_super_admins() {
if ( isset( $GLOBALS['super_admins'] ) ) {
$old_global = $GLOBALS['super_admins'];
unset( $GLOBALS['super_admins'] );
}
$user_id = self::factory()->user->create();
$this->assertFalse( is_super_admin( $user_id ) );
$this->assertFalse( revoke_super_admin( $user_id ) );
$this->assertTrue( grant_super_admin( $user_id ) );
$this->assertTrue( is_super_admin( $user_id ) );
$this->assertFalse( grant_super_admin( $user_id ) );
$this->assertTrue( revoke_super_admin( $user_id ) );
// None of these operations should set the $super_admins global.
$this->assertFalse( isset( $GLOBALS['super_admins'] ) );
// Try with two users.
$second_user = self::factory()->user->create();
$this->assertTrue( grant_super_admin( $user_id ) );
$this->assertTrue( grant_super_admin( $second_user ) );
$this->assertTrue( is_super_admin( $second_user ) );
$this->assertTrue( is_super_admin( $user_id ) );
$this->assertTrue( revoke_super_admin( $user_id ) );
$this->assertTrue( revoke_super_admin( $second_user ) );
if ( isset( $old_global ) ) {
$GLOBALS['super_admins'] = $old_global;
}
}
public function test_numeric_string_user_id() {
$u = self::factory()->user->create();
$u_string = (string) $u;
$this->assertTrue( wpmu_delete_user( $u_string ) );
$this->assertFalse( get_user_by( 'id', $u ) );
}
/**
* @ticket 33800
*/
public function test_should_return_false_for_non_numeric_string_user_id() {
$this->assertFalse( wpmu_delete_user( 'abcde' ) );
}
/**
* @ticket 33800
*/
public function test_should_return_false_for_object_user_id() {
$u_obj = self::factory()->user->create_and_get();
$this->assertFalse( wpmu_delete_user( $u_obj ) );
$this->assertEquals( $u_obj->ID, username_exists( $u_obj->user_login ) );
}
/**
* @ticket 38356
*/
public function test_add_user_to_blog_subscriber() {
$site_id = self::factory()->blog->create();
$user_id = self::factory()->user->create();
add_user_to_blog( $site_id, $user_id, 'subscriber' );
switch_to_blog( $site_id );
$user = get_user_by( 'id', $user_id );
restore_current_blog();
wpmu_delete_blog( $site_id );
wpmu_delete_user( $user_id );
$this->assertContains( 'subscriber', $user->roles );
}
/**
* @ticket 38356
*/
public function test_add_user_to_blog_invalid_user() {
$site_id = self::factory()->blog->create();
$result = add_user_to_blog( 73622, $site_id, 'subscriber' );
wpmu_delete_blog( $site_id );
$this->assertWPError( $result );
}
/**
* @ticket 41101
*/
public function test_should_fail_can_add_user_to_blog_filter() {
$site_id = self::factory()->blog->create();
$user_id = self::factory()->user->create();
add_filter( 'can_add_user_to_blog', '__return_false' );
$result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
$this->assertWPError( $result );
}
/**
* @ticket 41101
*/
public function test_should_succeed_can_add_user_to_blog_filter() {
$site_id = self::factory()->blog->create();
$user_id = self::factory()->user->create();
add_filter( 'can_add_user_to_blog', '__return_true' );
$result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
$this->assertTrue( $result );
}
/**
* @ticket 23016
*/
public function test_wp_roles_global_is_reset() {
global $wp_roles;
$role = 'test_global_is_reset';
$role_name = 'Test Global Is Reset';
$blog_id = self::factory()->blog->create();
$wp_roles->add_role( $role, $role_name, array() );
$this->assertNotEmpty( $wp_roles->get_role( $role ) );
switch_to_blog( $blog_id );
$this->assertEmpty( $wp_roles->get_role( $role ) );
$wp_roles->add_role( $role, $role_name, array() );
$this->assertNotEmpty( $wp_roles->get_role( $role ) );
restore_current_blog();
$this->assertNotEmpty( $wp_roles->get_role( $role ) );
$wp_roles->remove_role( $role );
}
}
endif;