forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslashes.php
More file actions
230 lines (199 loc) · 8 KB
/
slashes.php
File metadata and controls
230 lines (199 loc) · 8 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
<?php
/**
* @group user
* @group slashes
* @ticket 21767
*/
class Tests_User_Slashes extends WP_UnitTestCase {
protected static $author_id;
protected static $user_id;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$author_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$user_id = $factory->user->create();
}
function setUp() {
parent::setUp();
wp_set_current_user( self::$author_id );
// It is important to test with both even and odd numbered slashes,
// as KSES does a strip-then-add slashes in some of its function calls.
$this->slash_1 = 'String with 1 slash \\';
$this->slash_2 = 'String with 2 slashes \\\\';
$this->slash_3 = 'String with 3 slashes \\\\\\';
$this->slash_4 = 'String with 4 slashes \\\\\\\\';
$this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
$this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
$this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
}
/**
* Tests the controller function that expects slashed data.
*/
function test_add_user() {
$_POST = array();
$_GET = array();
$_REQUEST = array();
$_POST['user_login'] = 'slash_example_user_1';
$_POST['pass1'] = 'password';
$_POST['pass2'] = 'password';
$_POST['role'] = 'subscriber';
$_POST['email'] = 'user1@example.com';
$_POST['first_name'] = $this->slash_1;
$_POST['last_name'] = $this->slash_3;
$_POST['nickname'] = $this->slash_5;
$_POST['display_name'] = $this->slash_7;
$_POST['description'] = $this->slash_3;
$_POST = add_magic_quotes( $_POST ); // The add_user() function will strip slashes.
$user_id = add_user();
$user = get_user_to_edit( $user_id );
$this->assertSame( $this->slash_1, $user->first_name );
$this->assertSame( $this->slash_3, $user->last_name );
$this->assertSame( $this->slash_5, $user->nickname );
$this->assertSame( $this->slash_7, $user->display_name );
$this->assertSame( $this->slash_3, $user->description );
$_POST = array();
$_GET = array();
$_REQUEST = array();
$_POST['user_login'] = 'slash_example_user_2';
$_POST['pass1'] = 'password';
$_POST['pass2'] = 'password';
$_POST['role'] = 'subscriber';
$_POST['email'] = 'user2@example.com';
$_POST['first_name'] = $this->slash_2;
$_POST['last_name'] = $this->slash_4;
$_POST['nickname'] = $this->slash_6;
$_POST['display_name'] = $this->slash_2;
$_POST['description'] = $this->slash_4;
$_POST = add_magic_quotes( $_POST ); // The add_user() function will strip slashes.
$user_id = add_user();
$user = get_user_to_edit( $user_id );
$this->assertSame( $this->slash_2, $user->first_name );
$this->assertSame( $this->slash_4, $user->last_name );
$this->assertSame( $this->slash_6, $user->nickname );
$this->assertSame( $this->slash_2, $user->display_name );
$this->assertSame( $this->slash_4, $user->description );
}
/**
* Tests the controller function that expects slashed data.
*/
function test_edit_user() {
$user_id = self::$user_id;
$_POST = array();
$_GET = array();
$_REQUEST = array();
$_POST['role'] = 'subscriber';
$_POST['email'] = 'user1@example.com';
$_POST['first_name'] = $this->slash_1;
$_POST['last_name'] = $this->slash_3;
$_POST['nickname'] = $this->slash_5;
$_POST['display_name'] = $this->slash_7;
$_POST['description'] = $this->slash_3;
$_POST = add_magic_quotes( $_POST ); // The edit_user() function will strip slashes.
$user_id = edit_user( $user_id );
$user = get_user_to_edit( $user_id );
$this->assertSame( $this->slash_1, $user->first_name );
$this->assertSame( $this->slash_3, $user->last_name );
$this->assertSame( $this->slash_5, $user->nickname );
$this->assertSame( $this->slash_7, $user->display_name );
$this->assertSame( $this->slash_3, $user->description );
$_POST = array();
$_GET = array();
$_REQUEST = array();
$_POST['role'] = 'subscriber';
$_POST['email'] = 'user2@example.com';
$_POST['first_name'] = $this->slash_2;
$_POST['last_name'] = $this->slash_4;
$_POST['nickname'] = $this->slash_6;
$_POST['display_name'] = $this->slash_2;
$_POST['description'] = $this->slash_4;
$_POST = add_magic_quotes( $_POST ); // The edit_user() function will strip slashes.
$user_id = edit_user( $user_id );
$user = get_user_to_edit( $user_id );
$this->assertSame( $this->slash_2, $user->first_name );
$this->assertSame( $this->slash_4, $user->last_name );
$this->assertSame( $this->slash_6, $user->nickname );
$this->assertSame( $this->slash_2, $user->display_name );
$this->assertSame( $this->slash_4, $user->description );
}
/**
* Tests the model function that expects slashed data.
*/
function test_wp_insert_user() {
$user_id = wp_insert_user(
array(
'user_login' => 'slash_example_user_3',
'role' => 'subscriber',
'email' => 'user3@example.com',
'first_name' => $this->slash_1,
'last_name' => $this->slash_3,
'nickname' => $this->slash_5,
'display_name' => $this->slash_7,
'description' => $this->slash_3,
'user_pass' => '',
)
);
$user = get_user_to_edit( $user_id );
$this->assertSame( wp_unslash( $this->slash_1 ), $user->first_name );
$this->assertSame( wp_unslash( $this->slash_3 ), $user->last_name );
$this->assertSame( wp_unslash( $this->slash_5 ), $user->nickname );
$this->assertSame( wp_unslash( $this->slash_7 ), $user->display_name );
$this->assertSame( wp_unslash( $this->slash_3 ), $user->description );
$user_id = wp_insert_user(
array(
'user_login' => 'slash_example_user_4',
'role' => 'subscriber',
'email' => 'user3@example.com',
'first_name' => $this->slash_2,
'last_name' => $this->slash_4,
'nickname' => $this->slash_6,
'display_name' => $this->slash_2,
'description' => $this->slash_4,
'user_pass' => '',
)
);
$user = get_user_to_edit( $user_id );
$this->assertSame( wp_unslash( $this->slash_2 ), $user->first_name );
$this->assertSame( wp_unslash( $this->slash_4 ), $user->last_name );
$this->assertSame( wp_unslash( $this->slash_6 ), $user->nickname );
$this->assertSame( wp_unslash( $this->slash_2 ), $user->display_name );
$this->assertSame( wp_unslash( $this->slash_4 ), $user->description );
}
/**
* Tests the model function that expects slashed data.
*/
function test_wp_update_user() {
$user_id = self::$user_id;
$user_id = wp_update_user(
array(
'ID' => $user_id,
'role' => 'subscriber',
'first_name' => $this->slash_1,
'last_name' => $this->slash_3,
'nickname' => $this->slash_5,
'display_name' => $this->slash_7,
'description' => $this->slash_3,
)
);
$user = get_user_to_edit( $user_id );
$this->assertSame( wp_unslash( $this->slash_1 ), $user->first_name );
$this->assertSame( wp_unslash( $this->slash_3 ), $user->last_name );
$this->assertSame( wp_unslash( $this->slash_5 ), $user->nickname );
$this->assertSame( wp_unslash( $this->slash_7 ), $user->display_name );
$this->assertSame( wp_unslash( $this->slash_3 ), $user->description );
$user_id = wp_update_user(
array(
'ID' => $user_id,
'role' => 'subscriber',
'first_name' => $this->slash_2,
'last_name' => $this->slash_4,
'nickname' => $this->slash_6,
'display_name' => $this->slash_2,
'description' => $this->slash_4,
)
);
$user = get_user_to_edit( $user_id );
$this->assertSame( wp_unslash( $this->slash_2 ), $user->first_name );
$this->assertSame( wp_unslash( $this->slash_4 ), $user->last_name );
$this->assertSame( wp_unslash( $this->slash_6 ), $user->nickname );
$this->assertSame( wp_unslash( $this->slash_2 ), $user->display_name );
$this->assertSame( wp_unslash( $this->slash_4 ), $user->description );
}
}