forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommentForm.php
More file actions
298 lines (239 loc) · 10.6 KB
/
commentForm.php
File metadata and controls
298 lines (239 loc) · 10.6 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
<?php
/**
* @group comment
*
* @covers ::comment_form
*/
class Tests_Comment_CommentForm extends WP_UnitTestCase {
public static $post_id;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create();
}
public function test_default_markup_for_submit_button_and_wrapper() {
$p = self::factory()->post->create();
$args = array(
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
);
$form = get_echo( 'comment_form', array( $args, $p ) );
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
$hidden = get_comment_id_fields( $p );
$this->assertMatchesRegularExpression( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
}
public function test_custom_submit_button() {
$p = self::factory()->post->create();
$args = array(
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
'submit_button' => '<input name="custom-%1$s" type="submit" id="custom-%2$s" class="custom-%3$s" value="custom-%4$s" />',
);
$form = get_echo( 'comment_form', array( $args, $p ) );
$button = '<input name="custom-foo-name" type="submit" id="custom-foo-id" class="custom-foo-class" value="custom-foo-label" />';
$this->assertStringContainsString( $button, $form );
}
public function test_custom_submit_field() {
$p = self::factory()->post->create();
$args = array(
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
'submit_field' => '<p class="my-custom-submit-field">%1$s %2$s</p>',
);
$form = get_echo( 'comment_form', array( $args, $p ) );
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
$hidden = get_comment_id_fields( $p );
$this->assertMatchesRegularExpression( '|<p class="my\-custom\-submit\-field">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
}
/**
* @ticket 32312
*/
public function test_submit_button_and_submit_field_should_fall_back_on_defaults_when_filtered_defaults_do_not_contain_the_keys() {
$p = self::factory()->post->create();
$args = array(
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
);
add_filter( 'comment_form_defaults', array( $this, 'filter_comment_form_defaults' ) );
$form = get_echo( 'comment_form', array( $args, $p ) );
remove_filter( 'comment_form_defaults', array( $this, 'filter_comment_form_defaults' ) );
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
$hidden = get_comment_id_fields( $p );
$this->assertMatchesRegularExpression( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
}
public function filter_comment_form_defaults( $defaults ) {
unset( $defaults['submit_field'] );
unset( $defaults['submit_button'] );
return $defaults;
}
/**
* @ticket 44126
*/
public function test_fields_should_include_cookies_consent() {
$p = self::factory()->post->create();
add_filter( 'option_show_comments_cookies_opt_in', '__return_true' );
$args = array(
'fields' => array(
'author' => 'Hello World!',
),
);
$form = get_echo( 'comment_form', array( $args, $p ) );
remove_filter( 'option_show_comments_cookies_opt_in', '__return_true' );
$this->assertMatchesRegularExpression( '|<p class="comment\-form\-cookies\-consent">.*?</p>|', $form );
}
/**
* @ticket 47975
*/
public function test_aria_describedby_email_notes_should_not_be_added_if_no_email_notes() {
$p = self::factory()->post->create();
$form_with_aria = get_echo( 'comment_form', array( array(), $p ) );
$this->assertStringContainsString( 'aria-describedby="email-notes"', $form_with_aria );
$args = array(
'comment_notes_before' => '',
);
$form_without_aria = get_echo( 'comment_form', array( $args, $p ) );
$this->assertStringNotContainsString( 'aria-describedby="email-notes"', $form_without_aria );
}
/**
* @ticket 32767
*/
public function test_when_thread_comments_enabled() {
update_option( 'thread_comments', true );
$form = get_echo( 'comment_form', array( array(), self::$post_id ) );
$expected = '<a rel="nofollow" id="cancel-comment-reply-link" href="#respond" style="display:none;">Cancel reply</a>';
$this->assertStringContainsString( $expected, $form );
}
/**
* @ticket 32767
*/
public function test_when_thread_comments_disabled() {
delete_option( 'thread_comments' );
$form = get_echo( 'comment_form', array( array(), self::$post_id ) );
$expected = '<a rel="nofollow" id="cancel-comment-reply-link" href="#respond" style="display:none;">Cancel reply</a>';
$this->assertStringNotContainsString( $expected, $form );
}
/**
* @ticket 56243
*/
public function test_comment_form_should_not_display_for_global_post_when_called_with_invalid_id() {
// Go to permalink to ensure global post ID is set.
$this->go_to( get_permalink( self::$post_id ) );
$impossibly_high_post_id = PHP_INT_MAX;
$form = get_echo( 'comment_form', array( array(), $impossibly_high_post_id ) );
$this->assertEmpty( $form );
}
/**
* @ticket 56243
*/
public function test_comment_form_should_display_for_global_post_with_falsey_post_id() {
$post_id = self::$post_id;
$this->go_to( get_permalink( $post_id ) );
$form = get_echo( 'comment_form', array( array(), false ) );
$this->assertNotEmpty( $form );
$post_hidden_field = "<input type='hidden' name='comment_post_ID' value='{$post_id}' id='comment_post_ID' />";
$this->assertStringContainsString( $post_hidden_field, $form );
}
/**
* @ticket 56243
*/
public function test_comment_form_should_display_for_specified_post_when_passed_a_valid_post_id() {
$post_id = self::$post_id;
$form = get_echo( 'comment_form', array( array(), $post_id ) );
$this->assertNotEmpty( $form );
$post_hidden_field = "<input type='hidden' name='comment_post_ID' value='{$post_id}' id='comment_post_ID' />";
$this->assertStringContainsString( $post_hidden_field, $form );
}
/**
* Tests novalidate attribute on the comment form.
*
* @ticket 47595
*/
public function test_comment_form_and_novalidate_attribute() {
$post_id = self::$post_id;
// By default, the novalidate is not emitted.
$form = get_echo( 'comment_form', array( array(), $post_id ) );
$p = new WP_HTML_Tag_Processor( $form );
$this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' );
$this->assertNull( $p->get_attribute( 'novalidate' ), 'Expected FORM to not have novalidate attribute by default.' );
// Opt in to the novalidate attribute by passing an arg to comment_form().
$form = get_echo( 'comment_form', array( array( 'novalidate' => true ), $post_id ) );
$p = new WP_HTML_Tag_Processor( $form );
$this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' );
$this->assertTrue( $p->get_attribute( 'novalidate' ), 'Expected FORM to have the novalidate attribute.' );
// Opt in to the novalidate attribute via the comment_form_defaults filter.
add_filter(
'comment_form_defaults',
static function ( array $defaults ): array {
$defaults['novalidate'] = true;
return $defaults;
}
);
$form = get_echo( 'comment_form', array( array(), $post_id ) );
$p = new WP_HTML_Tag_Processor( $form );
$this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' );
$this->assertTrue( $p->get_attribute( 'novalidate' ), 'Expected FORM to have novalidate attribute.' );
}
/**
* @ticket 16576
*/
public function test_custom_fields_shown_default_fields_hidden_for_logged_in_users() {
$user_id = self::factory()->user->create(
array(
'role' => 'subscriber',
'user_login' => 'testuser',
'user_email' => 'test@example.com',
)
);
wp_set_current_user( $user_id );
$this->assertTrue( is_user_logged_in() );
$args = array(
'fields' => array(
'author' => '<p><label for="author">Name</label><input type="text" name="author" id="author" /></p>',
'email' => '<p><label for="email">Email</label><input type="email" name="email" id="email" /></p>',
'url' => '<p><label for="url">Website</label><input type="url" name="url" id="url" /></p>',
'cookies' => '<p><input type="checkbox" name="wp-comment-cookies-consent" id="wp-comment-cookies-consent" /><label for="wp-comment-cookies-consent">Save my details</label></p>',
'custom_field' => '<p><label for="custom_field">Custom Field</label><input type="text" name="custom_field" id="custom_field" /></p>',
'department' => '<p><label for="department">Department</label><select name="department" id="department"><option value="sales">Sales</option></select></p>',
),
);
$form = get_echo( 'comment_form', array( $args, self::$post_id ) );
// Custom fields should be present
$this->assertStringContainsString( 'name="custom_field"', $form );
$this->assertStringContainsString( 'name="department"', $form );
$this->assertStringContainsString( 'Custom Field', $form );
$this->assertStringContainsString( 'Department', $form );
// Default fields should NOT be present
$this->assertStringNotContainsString( 'name="author"', $form );
$this->assertStringNotContainsString( 'name="email"', $form );
$this->assertStringNotContainsString( 'name="url"', $form );
$this->assertStringNotContainsString( 'wp-comment-cookies-consent', $form );
wp_set_current_user( 0 );
}
/**
* @ticket 16576
*/
public function test_all_fields_displayed_for_non_logged_in_users() {
wp_set_current_user( 0 );
$this->assertFalse( is_user_logged_in() );
$args = array(
'fields' => array(
'author' => '<p><label for="author">Name</label><input type="text" name="author" id="author" /></p>',
'email' => '<p><label for="email">Email</label><input type="email" name="email" id="email" /></p>',
'url' => '<p><label for="url">Website</label><input type="url" name="url" id="url" /></p>',
'custom_field' => '<p><label for="custom_field">Custom Field</label><input type="text" name="custom_field" id="custom_field" /></p>',
),
);
$form = get_echo( 'comment_form', array( $args, self::$post_id ) );
// All fields should be present for non-logged-in users
$this->assertStringContainsString( 'name="author"', $form );
$this->assertStringContainsString( 'name="email"', $form );
$this->assertStringContainsString( 'name="url"', $form );
$this->assertStringContainsString( 'name="custom_field"', $form );
}
}