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
197 lines (164 loc) · 5.75 KB
/
slashes.php
File metadata and controls
197 lines (164 loc) · 5.75 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
<?php
/**
* @group comment
* @group slashes
* @ticket 21767
*/
class Tests_Comment_Slashes extends WP_UnitTestCase {
/*
* 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.
*/
const SLASH_1 = 'String with 1 slash \\';
const SLASH_2 = 'String with 2 slashes \\\\';
const SLASH_3 = 'String with 3 slashes \\\\\\';
const SLASH_4 = 'String with 4 slashes \\\\\\\\';
const SLASH_5 = 'String with 5 slashes \\\\\\\\\\';
const SLASH_6 = 'String with 6 slashes \\\\\\\\\\\\';
const SLASH_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
protected static $author_id;
protected static $post_id;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// We need an admin user to bypass comment flood protection.
self::$author_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$post_id = $factory->post->create();
}
public function set_up() {
parent::set_up();
wp_set_current_user( self::$author_id );
}
/**
* Tests the extended model function that expects slashed data.
*
* @covers ::wp_new_comment
*/
public function test_wp_new_comment() {
$post_id = self::$post_id;
// Not testing comment_author_email or comment_author_url
// as slashes are not permitted in that data.
$data = array(
'comment_post_ID' => $post_id,
'comment_author' => self::SLASH_1,
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => self::SLASH_7,
);
$comment_id = wp_new_comment( $data );
$comment = get_comment( $comment_id );
$this->assertSame( wp_unslash( self::SLASH_1 ), $comment->comment_author );
$this->assertSame( wp_unslash( self::SLASH_7 ), $comment->comment_content );
$data = array(
'comment_post_ID' => $post_id,
'comment_author' => self::SLASH_2,
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => self::SLASH_4,
);
$comment_id = wp_new_comment( $data );
$comment = get_comment( $comment_id );
$this->assertSame( wp_unslash( self::SLASH_2 ), $comment->comment_author );
$this->assertSame( wp_unslash( self::SLASH_4 ), $comment->comment_content );
}
/**
* Tests the controller function that expects slashed data.
*
* @covers ::edit_comment
*/
public function test_edit_comment() {
$post_id = self::$post_id;
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
)
);
// Not testing comment_author_email or comment_author_url
// as slashes are not permitted in that data.
$_POST = array();
$_POST['comment_ID'] = $comment_id;
$_POST['comment_status'] = '';
$_POST['newcomment_author'] = self::SLASH_1;
$_POST['newcomment_author_url'] = '';
$_POST['newcomment_author_email'] = '';
$_POST['content'] = self::SLASH_7;
$_POST = add_magic_quotes( $_POST ); // The edit_comment() function will strip slashes.
edit_comment();
$comment = get_comment( $comment_id );
$this->assertSame( self::SLASH_1, $comment->comment_author );
$this->assertSame( self::SLASH_7, $comment->comment_content );
$_POST = array();
$_POST['comment_ID'] = $comment_id;
$_POST['comment_status'] = '';
$_POST['newcomment_author'] = self::SLASH_2;
$_POST['newcomment_author_url'] = '';
$_POST['newcomment_author_email'] = '';
$_POST['content'] = self::SLASH_4;
$_POST = add_magic_quotes( $_POST ); // The edit_comment() function will strip slashes.
edit_comment();
$comment = get_comment( $comment_id );
$this->assertSame( self::SLASH_2, $comment->comment_author );
$this->assertSame( self::SLASH_4, $comment->comment_content );
}
/**
* Tests the model function that expects slashed data.
*
* @covers ::wp_insert_comment
*/
public function test_wp_insert_comment() {
$post_id = self::$post_id;
$comment_id = wp_insert_comment(
array(
'comment_post_ID' => $post_id,
'comment_author' => self::SLASH_1,
'comment_content' => self::SLASH_7,
)
);
$comment = get_comment( $comment_id );
$this->assertSame( wp_unslash( self::SLASH_1 ), $comment->comment_author );
$this->assertSame( wp_unslash( self::SLASH_7 ), $comment->comment_content );
$comment_id = wp_insert_comment(
array(
'comment_post_ID' => $post_id,
'comment_author' => self::SLASH_2,
'comment_content' => self::SLASH_4,
)
);
$comment = get_comment( $comment_id );
$this->assertSame( wp_unslash( self::SLASH_2 ), $comment->comment_author );
$this->assertSame( wp_unslash( self::SLASH_4 ), $comment->comment_content );
}
/**
* Tests the model function that expects slashed data.
*
* @covers ::wp_update_comment
*/
public function test_wp_update_comment() {
$post_id = self::$post_id;
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
)
);
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_author' => self::SLASH_1,
'comment_content' => self::SLASH_7,
)
);
$comment = get_comment( $comment_id );
$this->assertSame( wp_unslash( self::SLASH_1 ), $comment->comment_author );
$this->assertSame( wp_unslash( self::SLASH_7 ), $comment->comment_content );
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_author' => self::SLASH_2,
'comment_content' => self::SLASH_4,
)
);
$comment = get_comment( $comment_id );
$this->assertSame( wp_unslash( self::SLASH_2 ), $comment->comment_author );
$this->assertSame( wp_unslash( self::SLASH_4 ), $comment->comment_content );
}
}