forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpDeleteUser.php
More file actions
157 lines (127 loc) · 4.62 KB
/
wpDeleteUser.php
File metadata and controls
157 lines (127 loc) · 4.62 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
<?php
/**
* @group user
*/
class Tests_User_WpDeleteUser extends WP_UnitTestCase {
/**
* Test that usermeta cache is cleared after user deletion.
*
* @ticket 19500
*/
function test_get_blogs_of_user() {
// Logged out users don't have blogs.
$this->assertEquals( array(), get_blogs_of_user( 0 ) );
$user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
$blogs = get_blogs_of_user( $user_id );
$this->assertEquals( array( 1 ), array_keys( $blogs ) );
// Non-existent users don't have blogs.
self::delete_user( $user_id );
$user = new WP_User( $user_id );
$this->assertFalse( $user->exists(), 'WP_User->exists' );
$this->assertEquals( array(), get_blogs_of_user( $user_id ) );
}
/**
* Test that usermeta cache is cleared after user deletion.
*
* @ticket 19500
*/
function test_is_user_member_of_blog() {
$old_current = get_current_user_id();
$user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
wp_set_current_user( $user_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, get_current_blog_id() ) );
$this->assertTrue( is_user_member_of_blog( $user_id ) );
$this->assertTrue( is_user_member_of_blog( $user_id, get_current_blog_id() ) );
// Will only remove the user from the current site in multisite; this is desired
// and will achieve the desired effect with is_user_member_of_blog().
wp_delete_user( $user_id );
$this->assertFalse( is_user_member_of_blog( $user_id ) );
$this->assertFalse( is_user_member_of_blog( $user_id, get_current_blog_id() ) );
wp_set_current_user( $old_current );
}
function test_delete_user() {
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
$user = new WP_User( $user_id );
$post = array(
'post_author' => $user_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_type' => 'post',
);
// insert a post and make sure the ID is ok
$post_id = wp_insert_post($post);
$this->assertTrue(is_numeric($post_id));
$this->assertTrue($post_id > 0);
$post = get_post( $post_id );
$this->assertEquals( $post_id, $post->ID );
$post = array(
'post_author' => $user_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_type' => 'nav_menu_item',
);
// insert a post and make sure the ID is ok
$nav_id = wp_insert_post($post);
$this->assertTrue(is_numeric($nav_id));
$this->assertTrue($nav_id > 0);
$post = get_post( $nav_id );
$this->assertEquals( $nav_id, $post->ID );
wp_delete_user( $user_id );
$user = new WP_User( $user_id );
if ( is_multisite() )
$this->assertTrue( $user->exists() );
else
$this->assertFalse( $user->exists() );
$this->assertNotNull( get_post( $post_id ) );
$this->assertEquals( 'trash', get_post( $post_id )->post_status );
// nav_menu_item is delete_with_user = false so the nav post should remain published.
$this->assertNotNull( get_post( $nav_id ) );
$this->assertEquals( 'publish', get_post( $nav_id )->post_status );
wp_delete_post( $nav_id, true );
$this->assertNull( get_post( $nav_id ) );
wp_delete_post( $post_id, true );
$this->assertNull( get_post( $post_id ) );
}
/**
* @ticket 20447
*/
function test_wp_delete_user_reassignment_clears_post_caches() {
$user_id = self::factory()->user->create();
$reassign = self::factory()->user->create();
$post_id = self::factory()->post->create( array( 'post_author' => $user_id ) );
get_post( $post_id ); // Ensure this post is in the cache.
wp_delete_user( $user_id, $reassign );
$post = get_post( $post_id );
$this->assertEquals( $reassign, $post->post_author );
}
public function test_numeric_string_user_id() {
if ( is_multisite() ) {
$this->markTestSkipped( 'wp_delete_user() does not delete user records in Multisite.' );
}
$u = self::factory()->user->create();
$u_string = (string) $u;
$this->assertTrue( wp_delete_user( $u_string ) );
$this->assertFalse( get_user_by( 'id', $u ) );
}
/**
* @group 33800
*/
public function test_should_return_false_for_non_numeric_string_user_id() {
$this->assertFalse( wp_delete_user( 'abcde' ) );
}
/**
* @group 33800
*/
public function test_should_return_false_for_object_user_id() {
if ( is_multisite() ) {
$this->markTestSkipped( 'wp_delete_user() does not delete user records in Multisite.' );
}
$u_obj = self::factory()->user->create_and_get();
$this->assertFalse( wp_delete_user( $u_obj ) );
$this->assertEquals( $u_obj->ID, username_exists( $u_obj->user_login ) );
}
}