Completed
Pull Request — master (#2)
by Stephen
13:19
created

WP_Test_REST_Comments_Controller   D

Complexity

Total Complexity 151

Size/Duplication

Total Lines 2908
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 2908
rs 4.4102
c 0
b 0
f 0
wmc 151
lcom 2
cbo 7

139 Methods

Rating   Name   Duplication   Size   Complexity  
A wpSetUpBeforeClass() 0 48 1
A wpTearDownAfterClass() 0 15 1
A setUp() 0 7 2
A test_register_routes() 0 8 1
A test_context_param() 0 14 1
B test_registered_query_params() 0 29 1
A test_get_items() 0 12 1
A test_get_items_with_password() 0 19 1
A test_get_items_with_password_without_post() 0 17 1
A test_get_items_with_password_with_multiple_post() 0 15 1
A test_get_password_items_without_edit_post_permission() 0 17 1
A test_get_password_items_with_edit_post_permission() 0 17 1
A test_get_items_without_private_post_permission() 0 17 1
A test_get_items_with_private_post_permission() 0 17 1
A test_get_items_with_invalid_post() 0 18 1
A test_get_items_with_invalid_post_permission() 0 18 1
A test_get_items_no_permission_for_context() 0 7 1
A test_get_items_no_post() 0 10 1
A test_get_items_no_permission_for_no_post() 0 7 1
A test_get_items_edit_context() 0 7 1
A test_get_items_for_post() 0 15 1
B test_get_items_include_query() 0 33 1
B test_get_items_exclude_query() 0 24 1
B test_get_items_offset_query() 0 26 1
B test_get_items_order_query() 0 24 1
A test_get_items_private_post_no_permissions() 0 8 1
B test_get_items_author_arg() 0 37 1
B test_get_items_author_exclude_arg() 0 44 1
B test_get_items_parent_arg() 0 28 1
B test_get_items_parent_exclude_arg() 0 28 1
B test_get_items_search_query() 0 24 1
A test_get_comments_pagination_headers() 0 63 2
A test_get_comments_invalid_date() 0 7 1
A test_get_comments_valid_date() 0 22 1
A test_get_item() 0 9 1
A test_prepare_item() 0 13 1
A test_get_comment_author_avatar_urls() 0 17 1
A test_get_comment_invalid_id() 0 6 1
A test_get_comment_invalid_context() 0 7 1
A test_get_comment_invalid_post_id() 0 11 1
A test_get_comment_invalid_post_id_as_admin() 0 11 1
A test_get_comment_not_approved() 0 8 1
A test_get_comment_not_approved_same_user() 0 8 1
A test_get_comment_with_children_link() 0 19 1
A test_get_comment_without_children_link() 0 12 1
A test_get_comment_with_password_without_edit_post_permission() 0 11 1
A test_get_comment_with_password_with_valid_password() 0 15 1
B test_create_item() 0 25 1
B comment_dates_provider() 0 44 1
B test_create_comment_date() 0 29 3
B test_create_item_using_accepted_content_raw_value() 0 24 1
A test_create_item_error_from_filter() 0 21 1
A return_premade_error() 0 3 1
A test_create_comment_missing_required_author_name() 0 18 1
A test_create_comment_empty_required_author_name() 0 19 1
A test_create_comment_missing_required_author_email() 0 17 1
A test_create_comment_empty_required_author_email() 0 18 1
A test_create_comment_author_email_too_short() 0 20 1
A test_create_item_invalid_no_content() 0 22 1
A test_create_item_invalid_date() 0 19 1
B test_create_item_assign_different_user() 0 26 1
B test_create_comment_without_type() 0 33 1
A test_create_comment_with_invalid_type() 0 22 1
A test_create_comment_invalid_email() 0 21 1
B test_create_item_current_user() 0 33 1
B test_create_comment_other_user() 0 24 1
A test_create_comment_other_user_without_permission() 0 19 1
A test_create_comment_invalid_post() 0 19 1
A test_create_comment_status_without_permission() 0 20 1
B test_create_comment_with_status_IP_and_user_agent() 0 27 1
B test_create_comment_user_agent_header() 0 24 1
A test_create_comment_author_ip() 0 20 1
A test_create_comment_invalid_author_IP() 0 19 1
A test_create_comment_author_ip_no_permission() 0 16 1
A test_create_comment_author_ip_defaults_to_remote_addr() 0 18 1
A test_create_comment_no_post_id() 0 18 1
A test_create_comment_no_post_id_no_permission() 0 17 1
A test_create_comment_invalid_post_id() 0 19 1
A test_create_comment_draft_post() 0 19 1
A test_create_comment_trash_post() 0 19 1
A test_create_comment_private_post_invalid_permission() 0 19 1
A test_create_comment_password_post_invalid_permission() 0 18 1
B test_create_item_duplicate() 0 25 1
A test_create_comment_closed() 0 17 1
A test_create_comment_require_login() 0 11 1
A test_create_item_invalid_author() 0 16 1
A test_create_item_pull_author_info() 0 22 1
B test_create_comment_two_times() 0 33 1
A anonymous_comments_callback_null() 0 4 1
A test_allow_anonymous_comments_null() 0 21 1
A test_create_comment_author_name_too_long() 0 19 1
A test_create_comment_author_email_too_long() 0 19 1
A test_create_comment_author_url_too_long() 0 19 1
A test_create_comment_content_too_long() 0 19 1
A test_create_comment_without_password() 0 18 1
A test_create_comment_with_password() 0 18 1
B test_update_item() 0 35 1
B test_update_comment_date() 0 29 3
A test_update_item_no_content() 0 18 1
A test_update_item_no_change() 0 15 1
A test_update_comment_status() 0 23 1
B test_update_comment_field_does_not_use_default_values() 0 25 1
A test_update_comment_date_gmt() 0 19 1
A test_update_comment_author_email_only() 0 17 1
A test_update_comment_empty_author_name() 0 18 1
A test_update_comment_author_name_only() 0 17 1
A test_update_comment_empty_author_email() 0 18 1
A test_update_comment_author_email_too_short() 0 19 1
A test_update_comment_invalid_type() 0 13 1
A test_update_comment_with_raw_property() 0 20 1
A test_update_item_invalid_date() 0 15 1
A test_update_item_invalid_date_gmt() 0 15 1
A test_update_comment_invalid_id() 0 13 1
A test_update_comment_invalid_post_id() 0 9 1
A test_update_comment_invalid_permission() 0 13 1
A test_update_comment_private_post_invalid_permission() 0 19 1
B test_update_comment_with_children_link() 0 33 1
A test_update_comment_author_name_too_long() 0 15 1
A test_update_comment_author_email_too_long() 0 15 1
A test_update_comment_author_url_too_long() 0 15 1
A test_update_comment_content_too_long() 0 14 1
A verify_comment_roundtrip() 0 50 3
A test_comment_roundtrip_as_editor() 0 16 1
B test_comment_roundtrip_as_editor_unfiltered_html() 0 32 2
A test_comment_roundtrip_as_superadmin() 0 16 1
A test_comment_roundtrip_as_superadmin_unfiltered_html() 0 16 1
A test_delete_item() 0 17 1
A test_delete_item_skip_trash() 0 17 1
A test_delete_item_already_trashed() 0 15 1
A test_delete_comment_invalid_id() 0 8 1
A test_delete_comment_without_permission() 0 8 1
B test_delete_child_comment_link() 0 25 1
B test_get_item_schema() 0 30 1
A test_get_item_schema_show_avatar() 0 9 1
A test_get_additional_field_registration() 0 53 1
B test_additional_field_update_errors() 0 30 1
A additional_field_get_callback() 0 3 1
A additional_field_update_callback() 0 6 2
B check_comment_data() 0 34 3

How to fix   Complexity   

Complex Class

Complex classes like WP_Test_REST_Comments_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WP_Test_REST_Comments_Controller, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Unit tests covering WP_REST_Comments_Controller functionality.
4
 *
5
 * @package WordPress
6
 * @subpackage REST API
7
 */
8
9
/**
10
 * @group restapi
11
 */
12
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase {
13
	protected static $superadmin_id;
14
	protected static $admin_id;
15
	protected static $editor_id;
16
	protected static $subscriber_id;
17
	protected static $author_id;
18
19
	protected static $post_id;
20
	protected static $password_id;
21
	protected static $private_id;
22
	protected static $draft_id;
23
	protected static $trash_id;
24
	protected static $approved_id;
25
	protected static $hold_id;
26
27
	protected $endpoint;
28
29
	public static function wpSetUpBeforeClass( $factory ) {
30
		self::$superadmin_id = $factory->user->create( array(
31
			'role'       => 'administrator',
32
			'user_login' => 'superadmin',
33
		) );
34
		self::$admin_id = $factory->user->create( array(
35
			'role' => 'administrator',
36
		) );
37
		self::$editor_id = $factory->user->create( array(
38
			'role' => 'editor',
39
		) );
40
		self::$subscriber_id = $factory->user->create( array(
41
			'role' => 'subscriber',
42
		) );
43
		self::$author_id = $factory->user->create( array(
44
			'role'         => 'author',
45
			'display_name' => 'Sea Captain',
46
			'first_name'   => 'Horatio',
47
			'last_name'    => 'McCallister',
48
			'user_email'   => '[email protected]',
49
			'user_url'     => 'http://thefryingdutchman.com',
50
		) );
51
52
		self::$post_id = $factory->post->create();
53
		self::$private_id = $factory->post->create( array(
54
			'post_status' => 'private',
55
		) );
56
		self::$password_id = $factory->post->create( array(
57
			'post_password'    => 'toomanysecrets',
58
		) );
59
		self::$draft_id = $factory->post->create( array(
60
			'post_status' => 'draft',
61
		) );
62
		self::$trash_id = $factory->post->create( array(
63
			'post_status' => 'trash',
64
		) );
65
66
		self::$approved_id = $factory->comment->create( array(
67
			'comment_approved' => 1,
68
			'comment_post_ID'  => self::$post_id,
69
			'user_id'          => 0,
70
		) );
71
		self::$hold_id = $factory->comment->create( array(
72
			'comment_approved' => 0,
73
			'comment_post_ID'  => self::$post_id,
74
			'user_id'          => self::$subscriber_id,
75
		) );
76
	}
77
78
	public static function wpTearDownAfterClass() {
79
		self::delete_user( self::$superadmin_id );
80
		self::delete_user( self::$admin_id );
81
		self::delete_user( self::$editor_id );
82
		self::delete_user( self::$subscriber_id );
83
		self::delete_user( self::$author_id );
84
85
		wp_delete_post( self::$post_id, true );
86
		wp_delete_post( self::$private_id, true );
87
		wp_delete_post( self::$password_id, true );
88
		wp_delete_post( self::$draft_id, true );
89
		wp_delete_post( self::$trash_id, true );
90
		wp_delete_post( self::$approved_id, true );
91
		wp_delete_post( self::$hold_id, true );
92
	}
93
94
	public function setUp() {
95
		parent::setUp();
96
		$this->endpoint = new WP_REST_Comments_Controller;
97
		if ( is_multisite() ) {
98
			update_site_option( 'site_admins', array( 'superadmin' ) );
99
		}
100
	}
101
102
	public function test_register_routes() {
103
		$routes = $this->server->get_routes();
104
105
		$this->assertArrayHasKey( '/wp/v2/comments', $routes );
106
		$this->assertCount( 2, $routes['/wp/v2/comments'] );
107
		$this->assertArrayHasKey( '/wp/v2/comments/(?P<id>[\d]+)', $routes );
108
		$this->assertCount( 3, $routes['/wp/v2/comments/(?P<id>[\d]+)'] );
109
	}
110
111
	public function test_context_param() {
112
		// Collection
113
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
114
		$response = $this->server->dispatch( $request );
115
		$data = $response->get_data();
116
		$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
117
		$this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
118
		// Single
119
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments/' . self::$approved_id );
120
		$response = $this->server->dispatch( $request );
121
		$data = $response->get_data();
122
		$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
123
		$this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
124
	}
125
126
	public function test_registered_query_params() {
127
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
128
		$response = $this->server->dispatch( $request );
129
		$data = $response->get_data();
130
		$keys = array_keys( $data['endpoints'][0]['args'] );
131
		sort( $keys );
132
		$this->assertEquals( array(
133
			'after',
134
			'author',
135
			'author_email',
136
			'author_exclude',
137
			'before',
138
			'context',
139
			'exclude',
140
			'include',
141
			'offset',
142
			'order',
143
			'orderby',
144
			'page',
145
			'parent',
146
			'parent_exclude',
147
			'password',
148
			'per_page',
149
			'post',
150
			'search',
151
			'status',
152
			'type',
153
			), $keys );
154
	}
155
156
	public function test_get_items() {
157
		$this->factory->comment->create_post_comments( self::$post_id, 6 );
158
159
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
160
161
		$response = $this->server->dispatch( $request );
162
		$this->assertEquals( 200, $response->get_status() );
163
164
		$comments = $response->get_data();
165
		// We created 6 comments in this method, plus self::$approved_id.
166
		$this->assertCount( 7, $comments );
167
	}
168
169
	/**
170
	 * @ticket 38692
171
	 */
172
	public function test_get_items_with_password() {
173
		wp_set_current_user( 0 );
174
175
		$args = array(
176
			'comment_approved' => 1,
177
			'comment_post_ID'  => self::$password_id,
178
		);
179
		$password_comment = $this->factory->comment->create( $args );
180
181
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
182
		$request->set_param( 'password', 'toomanysecrets' );
183
		$request->set_param( 'post', self::$password_id );
184
185
		$response = $this->server->dispatch( $request );
186
		$this->assertEquals( 200, $response->get_status() );
187
188
		$collection_data = $response->get_data();
189
		$this->assertTrue( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
190
	}
191
192
	/**
193
	 * @ticket 38692
194
	 */
195
	public function test_get_items_with_password_without_post() {
196
		wp_set_current_user( 0 );
197
		$args = array(
198
			'comment_approved' => 1,
199
			'comment_post_ID'  => self::$password_id,
200
		);
201
		$password_comment = $this->factory->comment->create( $args );
202
203
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
204
		$request->set_param( 'password', 'toomanysecrets' );
205
206
		$response = $this->server->dispatch( $request );
207
		$this->assertEquals( 200, $response->get_status() );
208
209
		$collection_data = $response->get_data();
210
		$this->assertFalse( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
211
	}
212
213
	/**
214
	 * @ticket 38692
215
	 */
216
	public function test_get_items_with_password_with_multiple_post() {
217
		wp_set_current_user( 0 );
218
		$args = array(
219
			'comment_approved' => 1,
220
			'comment_post_ID'  => self::$password_id,
221
		);
222
		$password_comment = $this->factory->comment->create( $args );
0 ignored issues
show
Unused Code introduced by
$password_comment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
223
224
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
225
		$request->set_param( 'password', 'toomanysecrets' );
226
		$request->set_param( 'post', array( self::$password_id, self::$post_id ) );
227
228
		$response = $this->server->dispatch( $request );
229
		$this->assertErrorResponse( 'rest_cannot_read_post', $response, 401 );
230
	}
231
232
	public function test_get_password_items_without_edit_post_permission() {
233
		wp_set_current_user( 0 );
234
235
		$args = array(
236
			'comment_approved' => 1,
237
			'comment_post_ID'  => self::$password_id,
238
		);
239
		$password_comment = $this->factory->comment->create( $args );
240
241
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
242
243
		$response = $this->server->dispatch( $request );
244
		$this->assertEquals( 200, $response->get_status() );
245
246
		$collection_data = $response->get_data();
247
		$this->assertFalse( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
248
	}
249
250
	public function test_get_password_items_with_edit_post_permission() {
251
		wp_set_current_user( self::$admin_id );
252
253
		$args = array(
254
			'comment_approved' => 1,
255
			'comment_post_ID'  => self::$password_id,
256
		);
257
		$password_comment = $this->factory->comment->create( $args );
258
259
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
260
261
		$response = $this->server->dispatch( $request );
262
		$this->assertEquals( 200, $response->get_status() );
263
264
		$collection_data = $response->get_data();
265
		$this->assertTrue( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
266
	}
267
268
	public function test_get_items_without_private_post_permission() {
269
		wp_set_current_user( 0 );
270
271
		$args = array(
272
			'comment_approved' => 1,
273
			'comment_post_ID'  => self::$private_id,
274
		);
275
		$private_comment = $this->factory->comment->create( $args );
276
277
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
278
279
		$response = $this->server->dispatch( $request );
280
		$this->assertEquals( 200, $response->get_status() );
281
282
		$collection_data = $response->get_data();
283
		$this->assertFalse( in_array( $private_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
284
	}
285
286
	public function test_get_items_with_private_post_permission() {
287
		wp_set_current_user( self::$admin_id );
288
289
		$args = array(
290
			'comment_approved' => 1,
291
			'comment_post_ID'  => self::$private_id,
292
		);
293
		$private_comment = $this->factory->comment->create( $args );
294
295
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
296
297
		$response = $this->server->dispatch( $request );
298
		$this->assertEquals( 200, $response->get_status() );
299
300
		$collection_data = $response->get_data();
301
		$this->assertTrue( in_array( $private_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
302
	}
303
304
	public function test_get_items_with_invalid_post() {
305
		wp_set_current_user( 0 );
306
307
		$comment_id = $this->factory->comment->create( array(
308
			'comment_approved' => 1,
309
			'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
310
		));
311
312
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
313
314
		$response = $this->server->dispatch( $request );
315
		$this->assertEquals( 200, $response->get_status() );
316
317
		$collection_data = $response->get_data();
318
		$this->assertFalse( in_array( $comment_id, wp_list_pluck( $collection_data, 'id' ), true ) );
319
320
		wp_delete_comment( $comment_id );
321
	}
322
323
	public function test_get_items_with_invalid_post_permission() {
324
		wp_set_current_user( self::$admin_id );
325
326
		$comment_id = $this->factory->comment->create( array(
327
			'comment_approved' => 1,
328
			'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
329
		));
330
331
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
332
333
		$response = $this->server->dispatch( $request );
334
		$this->assertEquals( 200, $response->get_status() );
335
336
		$collection_data = $response->get_data();
337
		$this->assertTrue( in_array( $comment_id, wp_list_pluck( $collection_data, 'id' ), true ) );
338
339
		wp_delete_comment( $comment_id );
340
	}
341
342
	public function test_get_items_no_permission_for_context() {
343
		wp_set_current_user( 0 );
344
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
345
		$request->set_param( 'context', 'edit' );
346
		$response = $this->server->dispatch( $request );
347
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
348
	}
349
350
	public function test_get_items_no_post() {
351
		$this->factory->comment->create_post_comments( 0, 2 );
352
		wp_set_current_user( self::$admin_id );
353
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
354
		$request->set_param( 'post', 0 );
355
		$response = $this->server->dispatch( $request );
356
		$this->assertEquals( 200, $response->get_status() );
357
		$comments = $response->get_data();
358
		$this->assertCount( 2, $comments );
359
	}
360
361
	public function test_get_items_no_permission_for_no_post() {
362
		wp_set_current_user( 0 );
363
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
364
		$request->set_param( 'post', 0 );
365
		$response = $this->server->dispatch( $request );
366
		$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
367
	}
368
369
	public function test_get_items_edit_context() {
370
		wp_set_current_user( self::$admin_id );
371
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
372
		$request->set_param( 'context', 'edit' );
373
		$response = $this->server->dispatch( $request );
374
		$this->assertEquals( 200, $response->get_status() );
375
	}
376
377
	public function test_get_items_for_post() {
378
		$second_post_id = $this->factory->post->create();
379
		$this->factory->comment->create_post_comments( $second_post_id, 2 );
380
381
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
382
		$request->set_query_params( array(
383
			'post' => $second_post_id,
384
		) );
385
386
		$response = $this->server->dispatch( $request );
387
		$this->assertEquals( 200, $response->get_status() );
388
389
		$comments = $response->get_data();
390
		$this->assertCount( 2, $comments );
391
	}
392
393
	public function test_get_items_include_query() {
394
		wp_set_current_user( self::$admin_id );
395
		$args = array(
396
			'comment_approved' => 1,
397
			'comment_post_ID'  => self::$post_id,
398
		);
399
		$id1 = $this->factory->comment->create( $args );
400
		$this->factory->comment->create( $args );
401
		$id3 = $this->factory->comment->create( $args );
402
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
403
		// Order=>asc
404
		$request->set_param( 'order', 'asc' );
405
		$request->set_param( 'include', array( $id3, $id1 ) );
406
		$response = $this->server->dispatch( $request );
407
		$data = $response->get_data();
408
		$this->assertEquals( 2, count( $data ) );
409
		$this->assertEquals( $id1, $data[0]['id'] );
410
		// Orderby=>include
411
		$request->set_param( 'orderby', 'include' );
412
		$response = $this->server->dispatch( $request );
413
		$data = $response->get_data();
414
		$this->assertEquals( 2, count( $data ) );
415
		$this->assertEquals( $id3, $data[0]['id'] );
416
		// Orderby=>invalid should fail.
417
		$request->set_param( 'orderby', 'invalid' );
418
		$response = $this->server->dispatch( $request );
419
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
420
		// fails on invalid id.
421
		$request->set_param( 'orderby', array( 'include' ) );
422
		$request->set_param( 'include', array( 'invalid' ) );
423
		$response = $this->server->dispatch( $request );
424
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
425
	}
426
427
	public function test_get_items_exclude_query() {
428
		wp_set_current_user( self::$admin_id );
429
		$args = array(
430
			'comment_approved' => 1,
431
			'comment_post_ID'  => self::$post_id,
432
		);
433
		$id1 = $this->factory->comment->create( $args );
434
		$id2 = $this->factory->comment->create( $args );
435
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
436
		$response = $this->server->dispatch( $request );
437
		$data = $response->get_data();
438
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
439
		$this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
440
		$request->set_param( 'exclude', array( $id2 ) );
441
		$response = $this->server->dispatch( $request );
442
		$data = $response->get_data();
443
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
444
		$this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
445
446
		// fails on invalid id.
447
		$request->set_param( 'exclude', array( 'invalid' ) );
448
		$response = $this->server->dispatch( $request );
449
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
450
	}
451
452
	public function test_get_items_offset_query() {
453
		wp_set_current_user( self::$admin_id );
454
		$args = array(
455
			'comment_approved' => 1,
456
			'comment_post_ID'  => self::$post_id,
457
		);
458
		$this->factory->comment->create( $args );
459
		$this->factory->comment->create( $args );
460
		$this->factory->comment->create( $args );
461
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
462
		$request->set_param( 'offset', 1 );
463
		$response = $this->server->dispatch( $request );
464
		$this->assertCount( 3, $response->get_data() );
465
		// 'offset' works with 'per_page'
466
		$request->set_param( 'per_page', 2 );
467
		$response = $this->server->dispatch( $request );
468
		$this->assertCount( 2, $response->get_data() );
469
		// 'offset' takes priority over 'page'
470
		$request->set_param( 'page', 3 );
471
		$response = $this->server->dispatch( $request );
472
		$this->assertCount( 2, $response->get_data() );
473
		// 'offset' with invalid value errors.
474
		$request->set_param( 'offset', 'moreplease' );
475
		$response = $this->server->dispatch( $request );
476
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
477
	}
478
479
	public function test_get_items_order_query() {
480
		wp_set_current_user( self::$admin_id );
481
		$args = array(
482
			'comment_approved' => 1,
483
			'comment_post_ID'  => self::$post_id,
484
		);
485
		$this->factory->comment->create( $args );
486
		$this->factory->comment->create( $args );
487
		$id3 = $this->factory->comment->create( $args );
488
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
489
		// order defaults to 'desc'
490
		$response = $this->server->dispatch( $request );
491
		$data = $response->get_data();
492
		$this->assertEquals( $id3, $data[0]['id'] );
493
		// order=>asc
494
		$request->set_param( 'order', 'asc' );
495
		$response = $this->server->dispatch( $request );
496
		$data = $response->get_data();
497
		$this->assertEquals( self::$approved_id, $data[0]['id'] );
498
		// order=>asc,id should fail
499
		$request->set_param( 'order', 'asc,id' );
500
		$response = $this->server->dispatch( $request );
501
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
502
	}
503
504
	public function test_get_items_private_post_no_permissions() {
505
		wp_set_current_user( 0 );
506
		$post_id = $this->factory->post->create( array( 'post_status' => 'private' ) );
507
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
508
		$request->set_param( 'post', $post_id );
509
		$response = $this->server->dispatch( $request );
510
		$this->assertErrorResponse( 'rest_cannot_read_post', $response, 401 );
511
	}
512
513
	public function test_get_items_author_arg() {
514
		// Authorized
515
		wp_set_current_user( self::$admin_id );
516
		$args = array(
517
			'comment_approved' => 1,
518
			'comment_post_ID'  => self::$post_id,
519
			'user_id'          => self::$author_id,
520
		);
521
		$this->factory->comment->create( $args );
522
		$args['user_id'] = self::$subscriber_id;
523
		$this->factory->comment->create( $args );
524
		unset( $args['user_id'] );
525
		$this->factory->comment->create( $args );
526
527
		// 'author' limits result to 1 of 3
528
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
529
		$request->set_param( 'author', self::$author_id );
530
		$response = $this->server->dispatch( $request );
531
		$this->assertEquals( 200, $response->get_status() );
532
		$comments = $response->get_data();
533
		$this->assertCount( 1, $comments );
534
		// Multiple authors are supported
535
		$request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) );
536
		$response = $this->server->dispatch( $request );
537
		$this->assertEquals( 200, $response->get_status() );
538
		$comments = $response->get_data();
539
		$this->assertCount( 2, $comments );
540
		// Invalid author param errors
541
		$request->set_param( 'author', 'skippy' );
542
		$response = $this->server->dispatch( $request );
543
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
544
		// Unavailable to unauthenticated; defaults to error
545
		wp_set_current_user( 0 );
546
		$request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) );
547
		$response = $this->server->dispatch( $request );
548
		$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
549
	}
550
551
	public function test_get_items_author_exclude_arg() {
552
		// Authorized
553
		wp_set_current_user( self::$admin_id );
554
		$args = array(
555
			'comment_approved' => 1,
556
			'comment_post_ID'  => self::$post_id,
557
			'user_id'          => self::$author_id,
558
		);
559
		$this->factory->comment->create( $args );
560
		$args['user_id'] = self::$subscriber_id;
561
		$this->factory->comment->create( $args );
562
		unset( $args['user_id'] );
563
		$this->factory->comment->create( $args );
564
565
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
566
		$response = $this->server->dispatch( $request );
567
		$comments = $response->get_data();
568
		$this->assertCount( 4, $comments );
569
570
		// 'author_exclude' limits result to 3 of 4
571
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
572
		$request->set_param( 'author_exclude', self::$author_id );
573
		$response = $this->server->dispatch( $request );
574
		$this->assertEquals( 200, $response->get_status() );
575
		$comments = $response->get_data();
576
		$this->assertCount( 3, $comments );
577
		// 'author_exclude' for both comment authors (2 of 4)
578
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
579
		$request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) );
580
		$response = $this->server->dispatch( $request );
581
		$this->assertEquals( 200, $response->get_status() );
582
		$comments = $response->get_data();
583
		$this->assertCount( 2, $comments );
584
		// 'author_exclude' for both invalid author
585
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
586
		$request->set_param( 'author_exclude', 'skippy' );
587
		$response = $this->server->dispatch( $request );
588
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
589
		// Unavailable to unauthenticated; defaults to error
590
		wp_set_current_user( 0 );
591
		$request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) );
592
		$response = $this->server->dispatch( $request );
593
		$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
594
	}
595
596
	public function test_get_items_parent_arg() {
597
		$args = array(
598
			'comment_approved'  => 1,
599
			'comment_post_ID'   => self::$post_id,
600
		);
601
		$parent_id = $this->factory->comment->create( $args );
602
		$parent_id2 = $this->factory->comment->create( $args );
603
		$args['comment_parent'] = $parent_id;
604
		$this->factory->comment->create( $args );
605
		$args['comment_parent'] = $parent_id2;
606
		$this->factory->comment->create( $args );
607
		// All comments in the database
608
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
609
		$response = $this->server->dispatch( $request );
610
		$this->assertCount( 5, $response->get_data() );
611
		// Limit to the parent
612
		$request->set_param( 'parent', $parent_id );
613
		$response = $this->server->dispatch( $request );
614
		$this->assertCount( 1, $response->get_data() );
615
		// Limit to two parents
616
		$request->set_param( 'parent', array( $parent_id, $parent_id2 ) );
617
		$response = $this->server->dispatch( $request );
618
		$this->assertCount( 2, $response->get_data() );
619
		// Invalid parent should error
620
		$request->set_param( 'parent', 'invalid' );
621
		$response = $this->server->dispatch( $request );
622
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
623
	}
624
625
	public function test_get_items_parent_exclude_arg() {
626
		$args = array(
627
			'comment_approved'  => 1,
628
			'comment_post_ID'   => self::$post_id,
629
		);
630
		$parent_id = $this->factory->comment->create( $args );
631
		$parent_id2 = $this->factory->comment->create( $args );
632
		$args['comment_parent'] = $parent_id;
633
		$this->factory->comment->create( $args );
634
		$args['comment_parent'] = $parent_id2;
635
		$this->factory->comment->create( $args );
636
		// All comments in the database
637
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
638
		$response = $this->server->dispatch( $request );
639
		$this->assertCount( 5, $response->get_data() );
640
		// Exclude this particular parent
641
		$request->set_param( 'parent_exclude', $parent_id );
642
		$response = $this->server->dispatch( $request );
643
		$this->assertCount( 4, $response->get_data() );
644
		// Exclude both comment parents
645
		$request->set_param( 'parent_exclude', array( $parent_id, $parent_id2 ) );
646
		$response = $this->server->dispatch( $request );
647
		$this->assertCount( 3, $response->get_data() );
648
		// Invalid parent id should error
649
		$request->set_param( 'parent_exclude', 'invalid' );
650
		$response = $this->server->dispatch( $request );
651
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
652
	}
653
654
	public function test_get_items_search_query() {
655
		wp_set_current_user( self::$admin_id );
656
		$args = array(
657
			'comment_approved' => 1,
658
			'comment_post_ID'  => self::$post_id,
659
			'comment_content'  => 'foo',
660
			'comment_author'   => 'Homer J Simpson',
661
		);
662
		$id1 = $this->factory->comment->create( $args );
663
		$args['comment_content'] = 'bar';
664
		$this->factory->comment->create( $args );
665
		$args['comment_content'] = 'burrito';
666
		$this->factory->comment->create( $args );
667
		// 3 comments, plus 1 created in construct
668
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
669
		$response = $this->server->dispatch( $request );
670
		$this->assertCount( 4, $response->get_data() );
671
		// One matching comments
672
		$request->set_param( 'search', 'foo' );
673
		$response = $this->server->dispatch( $request );
674
		$data = $response->get_data();
675
		$this->assertCount( 1, $data );
676
		$this->assertEquals( $id1, $data[0]['id'] );
677
	}
678
679
	public function test_get_comments_pagination_headers() {
680
		wp_set_current_user( self::$admin_id );
681
		// Start of the index
682
		for ( $i = 0; $i < 49; $i++ ) {
683
			$this->factory->comment->create( array(
684
				'comment_content'   => "Comment {$i}",
685
				'comment_post_ID'   => self::$post_id,
686
				) );
687
		}
688
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
689
		$response = $this->server->dispatch( $request );
690
		$headers = $response->get_headers();
691
		$this->assertEquals( 50, $headers['X-WP-Total'] );
692
		$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
693
		$next_link = add_query_arg( array(
694
			'page'    => 2,
695
			), rest_url( '/wp/v2/comments' ) );
696
		$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
697
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
698
		// 3rd page
699
		$this->factory->comment->create( array(
700
				'comment_content'   => 'Comment 51',
701
				'comment_post_ID'   => self::$post_id,
702
				) );
703
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
704
		$request->set_param( 'page', 3 );
705
		$response = $this->server->dispatch( $request );
706
		$headers = $response->get_headers();
707
		$this->assertEquals( 51, $headers['X-WP-Total'] );
708
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
709
		$prev_link = add_query_arg( array(
710
			'page'    => 2,
711
			), rest_url( '/wp/v2/comments' ) );
712
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
713
		$next_link = add_query_arg( array(
714
			'page'    => 4,
715
			), rest_url( '/wp/v2/comments' ) );
716
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
717
		// Last page
718
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
719
		$request->set_param( 'page', 6 );
720
		$response = $this->server->dispatch( $request );
721
		$headers = $response->get_headers();
722
		$this->assertEquals( 51, $headers['X-WP-Total'] );
723
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
724
		$prev_link = add_query_arg( array(
725
			'page'    => 5,
726
			), rest_url( '/wp/v2/comments' ) );
727
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
728
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
729
		// Out of bounds
730
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
731
		$request->set_param( 'page', 8 );
732
		$response = $this->server->dispatch( $request );
733
		$headers = $response->get_headers();
734
		$this->assertEquals( 51, $headers['X-WP-Total'] );
735
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
736
		$prev_link = add_query_arg( array(
737
			'page'    => 6,
738
			), rest_url( '/wp/v2/comments' ) );
739
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
740
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
741
	}
742
743
	public function test_get_comments_invalid_date() {
744
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
745
		$request->set_param( 'after', rand_str() );
746
		$request->set_param( 'before', rand_str() );
747
		$response = $this->server->dispatch( $request );
748
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
749
	}
750
751
	public function test_get_comments_valid_date() {
752
		$comment1 = $this->factory->comment->create( array(
0 ignored issues
show
Unused Code introduced by
$comment1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
753
			'comment_date'    => '2016-01-15T00:00:00Z',
754
			'comment_post_ID' => self::$post_id,
755
		) );
756
		$comment2 = $this->factory->comment->create( array(
757
			'comment_date'    => '2016-01-16T00:00:00Z',
758
			'comment_post_ID' => self::$post_id,
759
		) );
760
		$comment3 = $this->factory->comment->create( array(
0 ignored issues
show
Unused Code introduced by
$comment3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
761
			'comment_date'    => '2016-01-17T00:00:00Z',
762
			'comment_post_ID' => self::$post_id,
763
		) );
764
765
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
766
		$request->set_param( 'after', '2016-01-15T00:00:00Z' );
767
		$request->set_param( 'before', '2016-01-17T00:00:00Z' );
768
		$response = $this->server->dispatch( $request );
769
		$data = $response->get_data();
770
		$this->assertCount( 1, $data );
771
		$this->assertEquals( $comment2, $data[0]['id'] );
772
	}
773
774
	public function test_get_item() {
775
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
776
777
		$response = $this->server->dispatch( $request );
778
		$this->assertEquals( 200, $response->get_status() );
779
780
		$data = $response->get_data();
781
		$this->check_comment_data( $data, 'view', $response->get_links() );
782
	}
783
784
	public function test_prepare_item() {
785
		wp_set_current_user( self::$admin_id );
786
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
787
		$request->set_query_params( array(
788
			'context' => 'edit',
789
		) );
790
791
		$response = $this->server->dispatch( $request );
792
		$this->assertEquals( 200, $response->get_status() );
793
794
		$data = $response->get_data();
795
		$this->check_comment_data( $data, 'edit', $response->get_links() );
796
	}
797
798
	public function test_get_comment_author_avatar_urls() {
799
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
800
801
		$response = $this->server->dispatch( $request );
802
803
		$data = $response->get_data();
804
		$this->assertArrayHasKey( 24,  $data['author_avatar_urls'] );
805
		$this->assertArrayHasKey( 48,  $data['author_avatar_urls'] );
806
		$this->assertArrayHasKey( 96,  $data['author_avatar_urls'] );
807
808
		$comment = get_comment( self::$approved_id );
809
		/**
810
		 * Ignore the subdomain, since 'get_avatar_url randomly sets the Gravatar
811
		 * server when building the url string.
812
		 */
813
		$this->assertEquals( substr( get_avatar_url( $comment->comment_author_email ), 9 ), substr( $data['author_avatar_urls'][96], 9 ) );
814
	}
815
816
	public function test_get_comment_invalid_id() {
817
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
818
819
		$response = $this->server->dispatch( $request );
820
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
821
	}
822
823
	public function test_get_comment_invalid_context() {
824
		wp_set_current_user( 0 );
825
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', self::$approved_id ) );
826
		$request->set_param( 'context', 'edit' );
827
		$response = $this->server->dispatch( $request );
828
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
829
	}
830
831
	public function test_get_comment_invalid_post_id() {
832
		wp_set_current_user( 0 );
833
		$comment_id = $this->factory->comment->create( array(
834
			'comment_approved' => 1,
835
			'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
836
		));
837
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
838
839
		$response = $this->server->dispatch( $request );
840
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
841
	}
842
843
	public function test_get_comment_invalid_post_id_as_admin() {
844
		wp_set_current_user( self::$admin_id );
845
		$comment_id = $this->factory->comment->create( array(
846
			'comment_approved' => 1,
847
			'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
848
		));
849
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
850
851
		$response = $this->server->dispatch( $request );
852
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
853
	}
854
855
	public function test_get_comment_not_approved() {
856
		wp_set_current_user( 0 );
857
858
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
859
860
		$response = $this->server->dispatch( $request );
861
		$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
862
	}
863
864
	public function test_get_comment_not_approved_same_user() {
865
		wp_set_current_user( self::$admin_id );
866
867
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
868
869
		$response = $this->server->dispatch( $request );
870
		$this->assertEquals( 200, $response->get_status() );
871
	}
872
873
	public function test_get_comment_with_children_link() {
874
		$comment_id_1 = $this->factory->comment->create( array(
875
			'comment_approved' => 1,
876
			'comment_post_ID'  => self::$post_id,
877
			'user_id'          => self::$subscriber_id,
878
		) );
879
880
		$child_comment = $this->factory->comment->create( array(
0 ignored issues
show
Unused Code introduced by
$child_comment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
881
			'comment_approved' => 1,
882
			'comment_parent'   => $comment_id_1,
883
			'comment_post_ID'  => self::$post_id,
884
			'user_id'          => self::$subscriber_id,
885
		) );
886
887
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
888
		$response = $this->server->dispatch( $request );
889
		$this->assertEquals( 200, $response->get_status() );
890
		$this->assertArrayHasKey( 'children', $response->get_links() );
891
	}
892
893
	public function test_get_comment_without_children_link() {
894
		$comment_id_1 = $this->factory->comment->create( array(
895
			'comment_approved' => 1,
896
			'comment_post_ID'  => self::$post_id,
897
			'user_id'          => self::$subscriber_id,
898
		) );
899
900
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
901
		$response = $this->server->dispatch( $request );
902
		$this->assertEquals( 200, $response->get_status() );
903
		$this->assertArrayNotHasKey( 'children', $response->get_links() );
904
	}
905
906
	public function test_get_comment_with_password_without_edit_post_permission() {
907
		wp_set_current_user( self::$subscriber_id );
908
		$args = array(
909
			'comment_approved' => 1,
910
			'comment_post_ID'  => self::$password_id,
911
		);
912
		$password_comment = $this->factory->comment->create( $args );
913
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) );
914
		$response = $this->server->dispatch( $request );
915
		$this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
916
	}
917
918
	/**
919
	 * @ticket 38692
920
	 */
921
	public function test_get_comment_with_password_with_valid_password() {
922
		wp_set_current_user( self::$subscriber_id );
923
924
		$args = array(
925
			'comment_approved' => 1,
926
			'comment_post_ID'  => self::$password_id,
927
		);
928
		$password_comment = $this->factory->comment->create( $args );
929
930
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) );
931
		$request->set_param( 'password', 'toomanysecrets' );
932
933
		$response = $this->server->dispatch( $request );
934
		$this->assertEquals( 200, $response->get_status() );
935
	}
936
937
	public function test_create_item() {
938
		wp_set_current_user( self::$admin_id );
939
940
		$params = array(
941
			'post'    => self::$post_id,
942
			'author_name'  => 'Comic Book Guy',
943
			'author_email' => '[email protected]',
944
			'author_url'   => 'http://androidsdungeon.com',
945
			'content' => 'Worst Comment Ever!',
946
			'date'    => '2014-11-07T10:14:25',
947
		);
948
949
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
950
		$request->add_header( 'content-type', 'application/json' );
951
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
952
953
		$response = $this->server->dispatch( $request );
954
		$this->assertEquals( 201, $response->get_status() );
955
956
		$data = $response->get_data();
957
		$this->check_comment_data( $data, 'edit', $response->get_links() );
958
		$this->assertEquals( 'hold', $data['status'] );
959
		$this->assertEquals( '2014-11-07T10:14:25', $data['date'] );
960
		$this->assertEquals( self::$post_id, $data['post'] );
961
	}
962
963
	public function comment_dates_provider() {
964
		return array(
965
			'set date without timezone' => array(
966
				'params'   => array(
967
					'timezone_string' => 'America/New_York',
968
					'date'            => '2016-12-12T14:00:00',
969
				),
970
				'results' => array(
971
					'date'            => '2016-12-12T14:00:00',
972
					'date_gmt'        => '2016-12-12T19:00:00',
973
				),
974
			),
975
			'set date_gmt without timezone' => array(
976
				'params'   => array(
977
					'timezone_string' => 'America/New_York',
978
					'date_gmt'        => '2016-12-12T19:00:00',
979
				),
980
				'results' => array(
981
					'date'            => '2016-12-12T14:00:00',
982
					'date_gmt'        => '2016-12-12T19:00:00',
983
				),
984
			),
985
			'set date with timezone' => array(
986
				'params'   => array(
987
					'timezone_string' => 'America/New_York',
988
					'date'            => '2016-12-12T18:00:00-01:00',
989
				),
990
				'results' => array(
991
					'date'            => '2016-12-12T14:00:00',
992
					'date_gmt'        => '2016-12-12T19:00:00',
993
				),
994
			),
995
			'set date_gmt with timezone' => array(
996
				'params'   => array(
997
					'timezone_string' => 'America/New_York',
998
					'date_gmt'        => '2016-12-12T18:00:00-01:00',
999
				),
1000
				'results' => array(
1001
					'date'            => '2016-12-12T14:00:00',
1002
					'date_gmt'        => '2016-12-12T19:00:00',
1003
				),
1004
			),
1005
		);
1006
	}
1007
1008
	/**
1009
	 * @dataProvider comment_dates_provider
1010
	 */
1011
	public function test_create_comment_date( $params, $results ) {
1012
		wp_set_current_user( self::$admin_id );
1013
		update_option( 'timezone_string', $params['timezone_string'] );
1014
1015
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1016
		$request->set_param( 'content', 'not empty' );
1017
		$request->set_param( 'post', self::$post_id );
1018
		if ( isset( $params['date'] ) ) {
1019
			$request->set_param( 'date', $params['date'] );
1020
		}
1021
		if ( isset( $params['date_gmt'] ) ) {
1022
			$request->set_param( 'date_gmt', $params['date_gmt'] );
1023
		}
1024
		$response = $this->server->dispatch( $request );
1025
1026
		update_option( 'timezone_string', '' );
1027
1028
		$this->assertEquals( 201, $response->get_status() );
1029
		$data = $response->get_data();
1030
		$comment = get_comment( $data['id'] );
1031
1032
		$this->assertEquals( $results['date'], $data['date'] );
1033
		$comment_date = str_replace( 'T', ' ', $results['date'] );
1034
		$this->assertEquals( $comment_date, $comment->comment_date );
1035
1036
		$this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
1037
		$comment_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
1038
		$this->assertEquals( $comment_date_gmt, $comment->comment_date_gmt );
1039
	}
1040
1041
	public function test_create_item_using_accepted_content_raw_value() {
1042
		wp_set_current_user( self::$admin_id );
1043
1044
		$params = array(
1045
			'post'         => self::$post_id,
1046
			'author_name'  => 'Reverend Lovejoy',
1047
			'author_email' => '[email protected]',
1048
			'author_url'   => 'http://timothylovejoy.jr',
1049
			'content'      => array(
1050
				'raw' => 'Once something has been approved by the government, it\'s no longer immoral.',
1051
			),
1052
		);
1053
1054
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1055
		$request->add_header( 'content-type', 'application/json' );
1056
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1057
1058
		$response = $this->server->dispatch( $request );
1059
		$this->assertEquals( 201, $response->get_status() );
1060
1061
		$data = $response->get_data();
1062
		$new_comment = get_comment( $data['id'] );
1063
		$this->assertEquals( $params['content']['raw'], $new_comment->comment_content );
1064
	}
1065
1066
	public function test_create_item_error_from_filter() {
1067
		add_filter( 'rest_pre_insert_comment', array( $this, 'return_premade_error' ) );
1068
		wp_set_current_user( self::$admin_id );
1069
1070
		$params = array(
1071
			'post'         => self::$post_id,
1072
			'author_name'  => 'Homer Jay Simpson',
1073
			'author_email' => '[email protected]',
1074
			'content'      => array(
1075
				'raw' => 'Aw, he loves beer. Here, little fella.'
1076
			),
1077
		);
1078
1079
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1080
		$request->add_header( 'content-type', 'application/json' );
1081
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1082
1083
		$response = $this->server->dispatch( $request );
1084
1085
		$this->assertErrorResponse( 'test_rest_premade_error', $response, 418 );
1086
	}
1087
1088
	public function return_premade_error() {
1089
		return new WP_Error( 'test_rest_premade_error', "I'm sorry, I thought he was a party robot.", array( 'status' => 418 ) );
1090
	}
1091
1092
	public function test_create_comment_missing_required_author_name() {
1093
		add_filter( 'rest_allow_anonymous_comments', '__return_true' );
1094
		update_option( 'require_name_email', 1 );
1095
1096
		$params = array(
1097
			'post'         => self::$post_id,
1098
			'author_email' => '[email protected]',
1099
			'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
1100
		);
1101
1102
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1103
		$request->add_header( 'content-type', 'application/json' );
1104
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1105
1106
		$response = $this->server->dispatch( $request );
1107
1108
		$this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 );
1109
	}
1110
1111
	public function test_create_comment_empty_required_author_name() {
1112
		add_filter( 'rest_allow_anonymous_comments', '__return_true' );
1113
		update_option( 'require_name_email', 1 );
1114
1115
		$params = array(
1116
			'author_name'  => '',
1117
			'author_email' => '[email protected]',
1118
			'post'         => self::$post_id,
1119
			'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
1120
		);
1121
1122
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1123
		$request->add_header( 'content-type', 'application/json' );
1124
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1125
1126
		$response = $this->server->dispatch( $request );
1127
1128
		$this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 );
1129
	}
1130
1131
	public function test_create_comment_missing_required_author_email() {
1132
		wp_set_current_user( self::$admin_id );
1133
		update_option( 'require_name_email', 1 );
1134
1135
		$params = array(
1136
			'post'        => self::$post_id,
1137
			'author_name' => 'Edna Krabappel',
1138
			'content'     => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
1139
		);
1140
1141
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1142
		$request->add_header( 'content-type', 'application/json' );
1143
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1144
1145
		$response = $this->server->dispatch( $request );
1146
		$this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 );
1147
	}
1148
1149
	public function test_create_comment_empty_required_author_email() {
1150
		wp_set_current_user( self::$admin_id );
1151
		update_option( 'require_name_email', 1 );
1152
1153
		$params = array(
1154
			'post'         => self::$post_id,
1155
			'author_name'  => 'Edna Krabappel',
1156
			'author_email' => '',
1157
			'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
1158
		);
1159
1160
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1161
		$request->add_header( 'content-type', 'application/json' );
1162
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1163
1164
		$response = $this->server->dispatch( $request );
1165
		$this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 );
1166
	}
1167
1168
	public function test_create_comment_author_email_too_short() {
1169
		wp_set_current_user( self::$admin_id );
1170
1171
		$params = array(
1172
			'post'         => self::$post_id,
1173
			'author_name'  => 'Homer J. Simpson',
1174
			'author_email' => 'a@b',
1175
			'content'      => 'in this house, we obey the laws of thermodynamics!',
1176
		);
1177
1178
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1179
		$request->add_header( 'content-type', 'application/json' );
1180
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1181
		$response = $this->server->dispatch( $request );
1182
1183
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1184
1185
		$data = $response->get_data();
1186
		$this->assertArrayHasKey( 'author_email', $data['data']['params'] );
1187
	}
1188
1189
	public function test_create_item_invalid_no_content() {
1190
		wp_set_current_user( self::$admin_id );
1191
1192
		$params = array(
1193
			'post'         => self::$post_id,
1194
			'author_name'  => 'Reverend Lovejoy',
1195
			'author_email' => '[email protected]',
1196
			'author_url'   => 'http://timothylovejoy.jr',
1197
		);
1198
1199
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1200
		$request->add_header( 'content-type', 'application/json' );
1201
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1202
1203
		$response = $this->server->dispatch( $request );
1204
		$this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
1205
1206
		$params['content'] = '';
1207
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1208
		$response = $this->server->dispatch( $request );
1209
		$this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
1210
	}
1211
1212
	public function test_create_item_invalid_date() {
1213
		wp_set_current_user( self::$admin_id );
1214
1215
		$params = array(
1216
			'post'         => self::$post_id,
1217
			'author_name'  => 'Reverend Lovejoy',
1218
			'author_email' => '[email protected]',
1219
			'author_url'   => 'http://timothylovejoy.jr',
1220
			'content'      => 'It\'s all over\, people! We don\'t have a prayer!',
1221
			'date'         => rand_str(),
1222
		);
1223
1224
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1225
		$request->add_header( 'content-type', 'application/json' );
1226
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1227
1228
		$response = $this->server->dispatch( $request );
1229
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1230
	}
1231
1232
1233
	public function test_create_item_assign_different_user() {
1234
		$subscriber_id = $this->factory->user->create( array(
1235
			'role' => 'subscriber',
1236
			'user_email' => '[email protected]',
1237
		));
1238
1239
		wp_set_current_user( self::$admin_id );
1240
		$params = array(
1241
			'post'    => self::$post_id,
1242
			'author_name'  => 'Comic Book Guy',
1243
			'author_email' => '[email protected]',
1244
			'author_url'   => 'http://androidsdungeon.com',
1245
			'author' => $subscriber_id,
1246
			'content' => 'Worst Comment Ever!',
1247
			'date'    => '2014-11-07T10:14:25',
1248
		);
1249
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1250
		$request->add_header( 'content-type', 'application/json' );
1251
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1252
		$response = $this->server->dispatch( $request );
1253
		$this->assertEquals( 201, $response->get_status() );
1254
1255
		$data = $response->get_data();
1256
		$this->assertEquals( $subscriber_id, $data['author'] );
1257
		$this->assertEquals( '127.0.0.1', $data['author_ip'] );
1258
	}
1259
1260
	public function test_create_comment_without_type() {
1261
		$post_id = $this->factory->post->create();
1262
		wp_set_current_user( self::$admin_id );
1263
1264
		$params = array(
1265
			'post'    => $post_id,
1266
			'author'       => self::$admin_id,
1267
			'author_name'  => 'Comic Book Guy',
1268
			'author_email' => '[email protected]',
1269
			'author_url'   => 'http://androidsdungeon.com',
1270
			'content' => 'Worst Comment Ever!',
1271
			'date'    => '2014-11-07T10:14:25',
1272
		);
1273
1274
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1275
		$request->add_header( 'content-type', 'application/json' );
1276
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1277
1278
		$response = $this->server->dispatch( $request );
1279
		$this->assertEquals( 201, $response->get_status() );
1280
1281
		$data = $response->get_data();
1282
		$this->assertEquals( 'comment', $data['type'] );
1283
1284
		$comment_id = $data['id'];
1285
1286
		// Make sure the new comment is present in the collection.
1287
		$collection = new WP_REST_Request( 'GET', '/wp/v2/comments' );
1288
		$collection->set_param( 'post', $post_id );
1289
		$collection_response = $this->server->dispatch( $collection );
1290
		$collection_data = $collection_response->get_data();
1291
		$this->assertEquals( $comment_id, $collection_data[0]['id'] );
1292
	}
1293
1294
	/**
1295
	 * @ticket 38820
1296
	 */
1297
	public function test_create_comment_with_invalid_type() {
1298
		$post_id = $this->factory->post->create();
1299
		wp_set_current_user( self::$admin_id );
1300
1301
		$params = array(
1302
			'post'    => $post_id,
1303
			'author'       => self::$admin_id,
1304
			'author_name'  => 'Comic Book Guy',
1305
			'author_email' => '[email protected]',
1306
			'author_url'   => 'http://androidsdungeon.com',
1307
			'content' => 'Worst Comment Ever!',
1308
			'date'    => '2014-11-07T10:14:25',
1309
			'type' => 'foo',
1310
		);
1311
1312
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1313
		$request->add_header( 'content-type', 'application/json' );
1314
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1315
1316
		$response = $this->server->dispatch( $request );
1317
		$this->assertErrorResponse( 'rest_invalid_comment_type', $response, 400 );
1318
	}
1319
1320
	public function test_create_comment_invalid_email() {
1321
		$post_id = $this->factory->post->create();
1322
		wp_set_current_user( self::$admin_id );
1323
1324
		$params = array(
1325
			'post'	=> $post_id,
1326
			'author'	   => self::$admin_id,
1327
			'author_name'  => 'Comic Book Guy',
1328
			'author_email' => 'hello:)',
1329
			'author_url'   => 'http://androidsdungeon.com',
1330
			'content' => 'Worst Comment Ever!',
1331
			'date'	=> '2014-11-07T10:14:25',
1332
		);
1333
1334
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1335
		$request->add_header( 'content-type', 'application/json' );
1336
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1337
1338
		$response = $this->server->dispatch( $request );
1339
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1340
	}
1341
1342
	public function test_create_item_current_user() {
1343
		$user_id = $this->factory->user->create( array(
1344
			'role' => 'subscriber',
1345
			'user_email' => '[email protected]',
1346
			'first_name' => 'Lyle',
1347
			'last_name' => 'Lanley',
1348
			'display_name' => 'Lyle Lanley',
1349
			'user_url' => 'http://simpsons.wikia.com/wiki/Lyle_Lanley',
1350
		));
1351
1352
		wp_set_current_user( $user_id );
1353
1354
		$params = array(
1355
			'post' => self::$post_id,
1356
			'content' => "Well sir, there's nothing on earth like a genuine, bona fide, electrified, six-car Monorail!",
1357
		);
1358
1359
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1360
		$request->add_header( 'content-type', 'application/json' );
1361
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1362
		$response = $this->server->dispatch( $request );
1363
1364
		$this->assertEquals( 201, $response->get_status() );
1365
		$data = $response->get_data();
1366
		$this->assertEquals( $user_id, $data['author'] );
1367
1368
		// Check author data matches
1369
		$author = get_user_by( 'id', $user_id );
1370
		$comment = get_comment( $data['id'] );
1371
		$this->assertEquals( $author->display_name, $comment->comment_author );
1372
		$this->assertEquals( $author->user_email, $comment->comment_author_email );
1373
		$this->assertEquals( $author->user_url, $comment->comment_author_url );
1374
	}
1375
1376
	public function test_create_comment_other_user() {
1377
		wp_set_current_user( self::$admin_id );
1378
1379
		$params = array(
1380
			'post'    => self::$post_id,
1381
			'author_name'  => 'Homer Jay Simpson',
1382
			'author_email' => '[email protected]',
1383
			'author_url'   => 'http://compuglobalhypermeganet.com',
1384
			'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1385
			'author'    => self::$subscriber_id,
1386
		);
1387
1388
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1389
		$request->add_header( 'content-type', 'application/json' );
1390
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1391
		$response = $this->server->dispatch( $request );
1392
1393
		$this->assertEquals( 201, $response->get_status() );
1394
		$data = $response->get_data();
1395
		$this->assertEquals( self::$subscriber_id, $data['author'] );
1396
		$this->assertEquals( 'Homer Jay Simpson', $data['author_name'] );
1397
		$this->assertEquals( '[email protected]', $data['author_email'] );
1398
		$this->assertEquals( 'http://compuglobalhypermeganet.com', $data['author_url'] );
1399
	}
1400
1401
	public function test_create_comment_other_user_without_permission() {
1402
		wp_set_current_user( self::$subscriber_id );
1403
1404
		$params = array(
1405
			'post'         => self::$post_id,
1406
			'author_name'  => 'Homer Jay Simpson',
1407
			'author_email' => '[email protected]',
1408
			'author_url'   => 'http://compuglobalhypermeganet.com',
1409
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1410
			'author'       => self::$admin_id,
1411
		);
1412
1413
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1414
		$request->add_header( 'content-type', 'application/json' );
1415
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1416
		$response = $this->server->dispatch( $request );
1417
1418
		$this->assertErrorResponse( 'rest_comment_invalid_author', $response, 403 );
1419
	}
1420
1421
	public function test_create_comment_invalid_post() {
1422
		wp_set_current_user( self::$subscriber_id );
1423
1424
		$params = array(
1425
			'post'		   => 'some-slug',
1426
			'author_name'  => 'Homer Jay Simpson',
1427
			'author_email' => '[email protected]',
1428
			'author_url'   => 'http://compuglobalhypermeganet.com',
1429
			'content'	   => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1430
			'author'	   => self::$subscriber_id,
1431
		);
1432
1433
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1434
		$request->add_header( 'content-type', 'application/json' );
1435
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1436
		$response = $this->server->dispatch( $request );
1437
1438
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1439
	}
1440
1441
	public function test_create_comment_status_without_permission() {
1442
		wp_set_current_user( self::$subscriber_id );
1443
1444
		$params = array(
1445
			'post'         => self::$post_id,
1446
			'author_name'  => 'Homer Jay Simpson',
1447
			'author_email' => '[email protected]',
1448
			'author_url'   => 'http://compuglobalhypermeganet.com',
1449
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1450
			'author'       => self::$subscriber_id,
1451
			'status'        => 'approved',
1452
		);
1453
1454
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1455
		$request->add_header( 'content-type', 'application/json' );
1456
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1457
		$response = $this->server->dispatch( $request );
1458
1459
		$this->assertErrorResponse( 'rest_comment_invalid_status', $response, 403 );
1460
	}
1461
1462
	public function test_create_comment_with_status_IP_and_user_agent() {
1463
		$post_id = $this->factory->post->create();
1464
		wp_set_current_user( self::$admin_id );
1465
1466
		$params = array(
1467
			'post'         => $post_id,
1468
			'author_name'  => 'Comic Book Guy',
1469
			'author_email' => '[email protected]',
1470
			'author_ip'    => '139.130.4.5',
1471
			'author_url'   => 'http://androidsdungeon.com',
1472
			'author_user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
1473
			'content'      => 'Worst Comment Ever!',
1474
			'status'       => 'approved',
1475
		);
1476
1477
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1478
		$request->add_header( 'content-type', 'application/json' );
1479
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1480
1481
		$response = $this->server->dispatch( $request );
1482
		$this->assertEquals( 201, $response->get_status() );
1483
1484
		$data = $response->get_data();
1485
		$this->assertEquals( 'approved', $data['status'] );
1486
		$this->assertEquals( '139.130.4.5', $data['author_ip'] );
1487
		$this->assertEquals( 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', $data['author_user_agent'] );
1488
	}
1489
1490
	public function test_create_comment_user_agent_header() {
1491
		wp_set_current_user( self::$admin_id );
1492
1493
		$params = array(
1494
			'post'         => self::$post_id,
1495
			'author_name'  => 'Homer Jay Simpson',
1496
			'author_email' => '[email protected]',
1497
			'author_url'   => 'http://compuglobalhypermeganet.com',
1498
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1499
		);
1500
1501
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1502
		$request->add_header( 'content-type', 'application/json' );
1503
		$request->add_header( 'user_agent', 'Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)' );
1504
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1505
1506
		$response = $this->server->dispatch( $request );
1507
		$this->assertEquals( 201, $response->get_status() );
1508
1509
		$data = $response->get_data();
1510
1511
		$new_comment = get_comment( $data['id'] );
1512
		$this->assertEquals( 'Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)', $new_comment->comment_agent );
1513
	}
1514
1515
	public function test_create_comment_author_ip() {
1516
		wp_set_current_user( self::$admin_id );
1517
1518
		$params = array(
1519
			'post'         => self::$post_id,
1520
			'author_name'  => 'Comic Book Guy',
1521
			'author_email' => '[email protected]',
1522
			'author_url'   => 'http://androidsdungeon.com',
1523
			'author_ip'    => '127.0.0.3',
1524
			'content'      => 'Worst Comment Ever!',
1525
			'status'       => 'approved',
1526
		);
1527
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1528
		$request->add_header( 'content-type', 'application/json' );
1529
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1530
		$response = $this->server->dispatch( $request );
1531
		$data = $response->get_data();
1532
		$new_comment = get_comment( $data['id'] );
1533
		$this->assertEquals( '127.0.0.3', $new_comment->comment_author_IP );
1534
	}
1535
1536
	public function test_create_comment_invalid_author_IP() {
1537
		wp_set_current_user( self::$admin_id );
1538
1539
		$params = array(
1540
			'post'         => self::$post_id,
1541
			'author_name'  => 'Comic Book Guy',
1542
			'author_email' => '[email protected]',
1543
			'author_url'   => 'http://androidsdungeon.com',
1544
			'author_ip'    => '867.5309',
1545
			'content'      => 'Worst Comment Ever!',
1546
			'status'       => 'approved',
1547
		);
1548
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1549
		$request->add_header( 'content-type', 'application/json' );
1550
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1551
1552
		$response = $this->server->dispatch( $request );
1553
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1554
	}
1555
1556
	public function test_create_comment_author_ip_no_permission() {
1557
		wp_set_current_user( self::$subscriber_id );
1558
		$params = array(
1559
			'author_name'  => 'Comic Book Guy',
1560
			'author_email' => '[email protected]',
1561
			'author_url'   => 'http://androidsdungeon.com',
1562
			'author_ip'    => '10.0.10.1',
1563
			'content'      => 'Worst Comment Ever!',
1564
			'status'       => 'approved',
1565
		);
1566
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1567
		$request->add_header( 'content-type', 'application/json' );
1568
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1569
		$response = $this->server->dispatch( $request );
1570
		$this->assertErrorResponse( 'rest_comment_invalid_author_ip', $response, 403 );
1571
	}
1572
1573
	public function test_create_comment_author_ip_defaults_to_remote_addr() {
1574
		wp_set_current_user( self::$admin_id );
1575
		$_SERVER['REMOTE_ADDR'] = '127.0.0.2';
1576
		$params = array(
1577
			'post'         => self::$post_id,
1578
			'author_name'  => 'Comic Book Guy',
1579
			'author_email' => '[email protected]',
1580
			'author_url'   => 'http://androidsdungeon.com',
1581
			'content'      => 'Worst Comment Ever!',
1582
		);
1583
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1584
		$request->add_header( 'content-type', 'application/json' );
1585
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1586
		$response = $this->server->dispatch( $request );
1587
		$data = $response->get_data();
1588
		$new_comment = get_comment( $data['id'] );
1589
		$this->assertEquals( '127.0.0.2', $new_comment->comment_author_IP );
1590
	}
1591
1592
	public function test_create_comment_no_post_id() {
1593
		wp_set_current_user( self::$admin_id );
1594
1595
		$params = array(
1596
			'author_name'  => 'Comic Book Guy',
1597
			'author_email' => '[email protected]',
1598
			'author_url'   => 'http://androidsdungeon.com',
1599
			'content'      => 'Worst Comment Ever!',
1600
			'status'       => 'approved',
1601
		);
1602
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1603
		$request->add_header( 'content-type', 'application/json' );
1604
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1605
1606
		$response = $this->server->dispatch( $request );
1607
1608
		$this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
1609
	}
1610
1611
	public function test_create_comment_no_post_id_no_permission() {
1612
		wp_set_current_user( self::$subscriber_id );
1613
1614
		$params = array(
1615
			'author_name'  => 'Homer Jay Simpson',
1616
			'author_email' => '[email protected]',
1617
			'author_url'   => 'http://compuglobalhypermeganet.com',
1618
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1619
			'author'       => self::$subscriber_id,
1620
		);
1621
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1622
		$request->add_header( 'content-type', 'application/json' );
1623
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1624
1625
		$response = $this->server->dispatch( $request );
1626
		$this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
1627
	}
1628
1629
	public function test_create_comment_invalid_post_id() {
1630
		wp_set_current_user( self::$admin_id );
1631
1632
		$params = array(
1633
			'author_name'  => 'Homer Jay Simpson',
1634
			'author_email' => '[email protected]',
1635
			'author_url'   => 'http://compuglobalhypermeganet.com',
1636
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1637
			'status'       => 'approved',
1638
			'post'         => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
1639
		);
1640
1641
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1642
		$request->add_header( 'content-type', 'application/json' );
1643
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1644
1645
		$response = $this->server->dispatch( $request );
1646
		$this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
1647
	}
1648
1649
	public function test_create_comment_draft_post() {
1650
		wp_set_current_user( self::$subscriber_id );
1651
1652
		$params = array(
1653
			'post'         => self::$draft_id,
1654
			'author_name'  => 'Ishmael',
1655
			'author_email' => '[email protected]',
1656
			'author_url'   => 'https://en.wikipedia.org/wiki/Herman_Melville',
1657
			'content'      => 'Call me Ishmael.',
1658
			'author'       => self::$subscriber_id,
1659
		);
1660
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1661
		$request->add_header( 'content-type', 'application/json' );
1662
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1663
1664
		$response = $this->server->dispatch( $request );
1665
1666
		$this->assertErrorResponse( 'rest_comment_draft_post', $response, 403 );
1667
	}
1668
1669
	public function test_create_comment_trash_post() {
1670
		wp_set_current_user( self::$subscriber_id );
1671
1672
		$params = array(
1673
			'post'         => self::$trash_id,
1674
			'author_name'  => 'Ishmael',
1675
			'author_email' => '[email protected]',
1676
			'author_url'   => 'https://en.wikipedia.org/wiki/Herman_Melville',
1677
			'content'      => 'Call me Ishmael.',
1678
			'author'       => self::$subscriber_id,
1679
		);
1680
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1681
		$request->add_header( 'content-type', 'application/json' );
1682
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1683
1684
		$response = $this->server->dispatch( $request );
1685
1686
		$this->assertErrorResponse( 'rest_comment_trash_post', $response, 403 );
1687
	}
1688
1689
	public function test_create_comment_private_post_invalid_permission() {
1690
		wp_set_current_user( self::$subscriber_id );
1691
1692
		$params = array(
1693
			'post'         => self::$private_id,
1694
			'author_name'  => 'Homer Jay Simpson',
1695
			'author_email' => '[email protected]',
1696
			'author_url'   => 'http://compuglobalhypermeganet.com',
1697
			'content'      => 'I\’d be a vegetarian if bacon grew on trees.',
1698
			'author'       => self::$subscriber_id,
1699
		);
1700
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1701
		$request->add_header( 'content-type', 'application/json' );
1702
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1703
1704
		$response = $this->server->dispatch( $request );
1705
1706
		$this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 );
1707
	}
1708
1709
	public function test_create_comment_password_post_invalid_permission() {
1710
		wp_set_current_user( self::$subscriber_id );
1711
1712
		$params = array(
1713
			'post'         => self::$password_id,
1714
			'author_name'  => 'Homer Jay Simpson',
1715
			'author_email' => '[email protected]',
1716
			'author_url'   => 'http://compuglobalhypermeganet.com',
1717
			'content'      => 'I\’d be a vegetarian if bacon grew on trees.',
1718
			'author'       => self::$subscriber_id,
1719
		);
1720
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1721
		$request->add_header( 'content-type', 'application/json' );
1722
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1723
1724
		$response = $this->server->dispatch( $request );
1725
		$this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 );
1726
	}
1727
1728
	public function test_create_item_duplicate() {
1729
		wp_set_current_user( self::$subscriber_id );
1730
		$this->factory->comment->create(
1731
			array(
1732
				'comment_post_ID'      => self::$post_id,
1733
				'comment_author'       => 'Guy N. Cognito',
1734
				'comment_author_email' => '[email protected]',
1735
				'comment_content'      => 'Homer? Who is Homer? My name is Guy N. Cognito.',
1736
			)
1737
		);
1738
1739
		$params = array(
1740
			'post'    => self::$post_id,
1741
			'author_name'  => 'Guy N. Cognito',
1742
			'author_email' => '[email protected]',
1743
			'content' => 'Homer? Who is Homer? My name is Guy N. Cognito.',
1744
		);
1745
1746
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1747
		$request->add_header( 'content-type', 'application/json' );
1748
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1749
		$response = $this->server->dispatch( $request );
1750
1751
		$this->assertEquals( 409, $response->get_status() );
1752
	}
1753
1754
	public function test_create_comment_closed() {
1755
		$post_id = $this->factory->post->create( array(
1756
			'comment_status' => 'closed',
1757
		));
1758
		wp_set_current_user( self::$subscriber_id );
1759
1760
		$params = array(
1761
			'post'      => $post_id,
1762
		);
1763
1764
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1765
		$request->add_header( 'content-type', 'application/json' );
1766
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1767
		$response = $this->server->dispatch( $request );
1768
1769
		$this->assertEquals( 403, $response->get_status() );
1770
	}
1771
1772
	public function test_create_comment_require_login() {
1773
		wp_set_current_user( 0 );
1774
		update_option( 'comment_registration', 1 );
1775
		add_filter( 'rest_allow_anonymous_comments', '__return_true' );
1776
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1777
		$request->set_param( 'post', self::$post_id );
1778
		$response = $this->server->dispatch( $request );
1779
		$this->assertEquals( 401, $response->get_status() );
1780
		$data = $response->get_data();
1781
		$this->assertEquals( 'rest_comment_login_required', $data['code'] );
1782
	}
1783
1784
	public function test_create_item_invalid_author() {
1785
		wp_set_current_user( self::$admin_id );
1786
1787
		$params = array(
1788
			'post'         => self::$post_id,
1789
			'author'       => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
1790
			'content'      => 'It\'s all over\, people! We don\'t have a prayer!',
1791
		);
1792
1793
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1794
		$request->add_header( 'content-type', 'application/json' );
1795
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1796
1797
		$response = $this->server->dispatch( $request );
1798
		$this->assertErrorResponse( 'rest_comment_author_invalid', $response, 400 );
1799
	}
1800
1801
	public function test_create_item_pull_author_info() {
1802
		wp_set_current_user( self::$admin_id );
1803
1804
		$author = new WP_User( self::$author_id );
0 ignored issues
show
Unused Code introduced by
$author is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1805
		$params = array(
1806
			'post'         => self::$post_id,
1807
			'author'       => self::$author_id,
1808
			'content'      => 'It\'s all over\, people! We don\'t have a prayer!',
1809
		);
1810
1811
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1812
		$request->add_header( 'content-type', 'application/json' );
1813
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1814
1815
		$response = $this->server->dispatch( $request );
1816
1817
		$result = $response->get_data();
1818
		$this->assertSame( self::$author_id, $result['author'] );
1819
		$this->assertSame( 'Sea Captain', $result['author_name'] );
1820
		$this->assertSame( '[email protected]', $result['author_email'] );
1821
		$this->assertSame( 'http://thefryingdutchman.com', $result['author_url'] );
1822
	}
1823
1824
	public function test_create_comment_two_times() {
1825
		add_filter( 'rest_allow_anonymous_comments', '__return_true' );
1826
1827
		$params = array(
1828
			'post'    => self::$post_id,
1829
			'author_name'  => 'Comic Book Guy',
1830
			'author_email' => '[email protected]',
1831
			'author_url'   => 'http://androidsdungeon.com',
1832
			'content' => 'Worst Comment Ever!',
1833
		);
1834
1835
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1836
		$request->add_header( 'content-type', 'application/json' );
1837
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1838
1839
		$response = $this->server->dispatch( $request );
1840
		$this->assertEquals( 201, $response->get_status() );
1841
1842
		$params = array(
1843
			'post'    => self::$post_id,
1844
			'author_name'  => 'Comic Book Guy',
1845
			'author_email' => '[email protected]',
1846
			'author_url'   => 'http://androidsdungeon.com',
1847
			'content'      => 'Shakes fist at sky',
1848
		);
1849
1850
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1851
		$request->add_header( 'content-type', 'application/json' );
1852
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1853
1854
		$response = $this->server->dispatch( $request );
1855
		$this->assertEquals( 400, $response->get_status() );
1856
	}
1857
1858
	public function anonymous_comments_callback_null() {
1859
		// I'm a plugin developer who forgot to include a return value for some
1860
		// code path in my 'rest_allow_anonymous_comments' filter.
1861
	}
1862
1863
	public function test_allow_anonymous_comments_null() {
1864
		add_filter( 'rest_allow_anonymous_comments', array( $this, 'anonymous_comments_callback_null' ), 10, 2 );
1865
1866
		$params = array(
1867
			'post'         => self::$post_id,
1868
			'author_name'  => 'Comic Book Guy',
1869
			'author_email' => '[email protected]',
1870
			'author_url'   => 'http://androidsdungeon.com',
1871
			'content'      => 'Worst Comment Ever!',
1872
		);
1873
1874
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1875
		$request->add_header( 'content-type', 'application/json' );
1876
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1877
1878
		$response = $this->server->dispatch( $request );
1879
1880
		remove_filter( 'rest_allow_anonymous_comments', array( $this, 'anonymous_comments_callback_null' ), 10, 2 );
1881
1882
		$this->assertErrorResponse( 'rest_comment_login_required', $response, 401 );
1883
	}
1884
1885
	/**
1886
	 * @ticket 38477
1887
	 */
1888
	public function test_create_comment_author_name_too_long() {
1889
		wp_set_current_user( self::$subscriber_id );
1890
1891
		$params = array(
1892
			'post'         => self::$post_id,
1893
			'author_name'  => rand_long_str( 246 ),
1894
			'author_email' => '[email protected]',
1895
			'author_url'   => 'http://jazz.gingivitis.com',
1896
			'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
1897
			'date'         => '1995-04-30T10:22:00',
1898
		);
1899
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1900
1901
		$request->add_header( 'content-type', 'application/json' );
1902
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1903
		$response = $this->server->dispatch( $request );
1904
1905
		$this->assertErrorResponse( 'comment_author_column_length', $response, 400 );
1906
	}
1907
1908
	/**
1909
	 * @ticket 38477
1910
	 */
1911
	public function test_create_comment_author_email_too_long() {
1912
		wp_set_current_user( self::$subscriber_id );
1913
1914
		$params = array(
1915
			'post'         => self::$post_id,
1916
			'author_name'  => 'Bleeding Gums Murphy',
1917
			'author_email' => 'murphy@' . rand_long_str( 190 ) . '.com',
1918
			'author_url'   => 'http://jazz.gingivitis.com',
1919
			'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
1920
			'date'         => '1995-04-30T10:22:00',
1921
		);
1922
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1923
1924
		$request->add_header( 'content-type', 'application/json' );
1925
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1926
		$response = $this->server->dispatch( $request );
1927
1928
		$this->assertErrorResponse( 'comment_author_email_column_length', $response, 400 );
1929
	}
1930
1931
	/**
1932
	 * @ticket 38477
1933
	 */
1934
	public function test_create_comment_author_url_too_long() {
1935
		wp_set_current_user( self::$subscriber_id );
1936
1937
		$params = array(
1938
			'post'         => self::$post_id,
1939
			'author_name'  => 'Bleeding Gums Murphy',
1940
			'author_email' => '[email protected]',
1941
			'author_url'   => 'http://jazz.' . rand_long_str( 185 ) . '.com',
1942
			'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
1943
			'date'         => '1995-04-30T10:22:00',
1944
		);
1945
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1946
1947
		$request->add_header( 'content-type', 'application/json' );
1948
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1949
		$response = $this->server->dispatch( $request );
1950
1951
		$this->assertErrorResponse( 'comment_author_url_column_length', $response, 400 );
1952
	}
1953
1954
	/**
1955
	 * @ticket 38477
1956
	 */
1957
	public function test_create_comment_content_too_long() {
1958
		wp_set_current_user( self::$subscriber_id );
1959
1960
		$params = array(
1961
			'post'         => self::$post_id,
1962
			'author_name'  => 'Bleeding Gums Murphy',
1963
			'author_email' => '[email protected]',
1964
			'author_url'   => 'http://jazz.gingivitis.com',
1965
			'content'      => rand_long_str( 66525 ),
1966
			'date'         => '1995-04-30T10:22:00',
1967
		);
1968
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1969
1970
		$request->add_header( 'content-type', 'application/json' );
1971
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1972
		$response = $this->server->dispatch( $request );
1973
1974
		$this->assertErrorResponse( 'comment_content_column_length', $response, 400 );
1975
	}
1976
1977
	public function test_create_comment_without_password() {
1978
		wp_set_current_user( self::$subscriber_id );
1979
1980
		$params = array(
1981
			'post'         => self::$password_id,
1982
			'author_name'  => 'Bleeding Gums Murphy',
1983
			'author_email' => '[email protected]',
1984
			'author_url'   => 'http://jazz.gingivitis.com',
1985
			'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
1986
		);
1987
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1988
1989
		$request->add_header( 'content-type', 'application/json' );
1990
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1991
		$response = $this->server->dispatch( $request );
1992
1993
		$this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 );
1994
	}
1995
1996
	public function test_create_comment_with_password() {
1997
		add_filter( 'rest_allow_anonymous_comments', '__return_true' );
1998
1999
		$params = array(
2000
			'post'         => self::$password_id,
2001
			'author_name'  => 'Bleeding Gums Murphy',
2002
			'author_email' => '[email protected]',
2003
			'author_url'   => 'http://jazz.gingivitis.com',
2004
			'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
2005
			'password'     => 'toomanysecrets',
2006
		);
2007
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
2008
2009
		$request->add_header( 'content-type', 'application/json' );
2010
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2011
		$response = $this->server->dispatch( $request );
2012
		$this->assertEquals( 201, $response->get_status() );
2013
	}
2014
2015
	public function test_update_item() {
2016
		$post_id = $this->factory->post->create();
2017
2018
		wp_set_current_user( self::$admin_id );
2019
2020
		$params = array(
2021
			'author'       => self::$subscriber_id,
2022
			'author_name'  => 'Disco Stu',
2023
			'author_url'   => 'http://stusdisco.com',
2024
			'author_email' => '[email protected]',
2025
			'author_ip'    => '4.4.4.4',
2026
			'content'      => 'Testing.',
2027
			'date'         => '2014-11-07T10:14:25',
2028
			'post'         => $post_id,
2029
		);
2030
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2031
		$request->add_header( 'content-type', 'application/json' );
2032
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2033
2034
		$response = $this->server->dispatch( $request );
2035
		$this->assertEquals( 200, $response->get_status() );
2036
2037
		$comment = $response->get_data();
2038
		$updated = get_comment( self::$approved_id );
2039
		$this->assertEquals( $params['content'], $comment['content']['raw'] );
2040
		$this->assertEquals( $params['author'], $comment['author'] );
2041
		$this->assertEquals( $params['author_name'], $comment['author_name'] );
2042
		$this->assertEquals( $params['author_url'], $comment['author_url'] );
2043
		$this->assertEquals( $params['author_email'], $comment['author_email'] );
2044
		$this->assertEquals( $params['author_ip'], $comment['author_ip'] );
2045
		$this->assertEquals( $params['post'], $comment['post'] );
2046
2047
		$this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
2048
		$this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
2049
	}
2050
2051
	/**
2052
	 * @dataProvider comment_dates_provider
2053
	 */
2054
	public function test_update_comment_date( $params, $results ) {
2055
		wp_set_current_user( self::$editor_id );
2056
		update_option( 'timezone_string', $params['timezone_string'] );
2057
2058
		$comment_id = $this->factory->comment->create();
2059
2060
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
2061
		if ( isset( $params['date'] ) ) {
2062
			$request->set_param( 'date', $params['date'] );
2063
		}
2064
		if ( isset( $params['date_gmt'] ) ) {
2065
			$request->set_param( 'date_gmt', $params['date_gmt'] );
2066
		}
2067
		$response = $this->server->dispatch( $request );
2068
2069
		update_option( 'timezone_string', '' );
2070
2071
		$this->assertEquals( 200, $response->get_status() );
2072
		$data = $response->get_data();
2073
		$comment = get_comment( $data['id'] );
2074
2075
		$this->assertEquals( $results['date'], $data['date'] );
2076
		$comment_date = str_replace( 'T', ' ', $results['date'] );
2077
		$this->assertEquals( $comment_date, $comment->comment_date );
2078
2079
		$this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
2080
		$comment_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
2081
		$this->assertEquals( $comment_date_gmt, $comment->comment_date_gmt );
2082
	}
2083
2084
	public function test_update_item_no_content() {
2085
		$post_id = $this->factory->post->create();
0 ignored issues
show
Unused Code introduced by
$post_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2086
2087
		wp_set_current_user( self::$admin_id );
2088
2089
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2090
		$request->set_param( 'author_email', '[email protected]' );
2091
2092
		// Sending a request without content is fine.
2093
		$response = $this->server->dispatch( $request );
2094
		$this->assertEquals( 200, $response->get_status() );
2095
2096
		// Sending a request with empty comment is not fine.
2097
		$request->set_param( 'author_email', '[email protected]' );
2098
		$request->set_param( 'content', '' );
2099
		$response = $this->server->dispatch( $request );
2100
		$this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
2101
	}
2102
2103
	public function test_update_item_no_change() {
2104
		$comment = get_comment( self::$approved_id );
2105
2106
		wp_set_current_user( self::$admin_id );
2107
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2108
		$request->set_param( 'post', $comment->comment_post_ID );
2109
2110
		// Run twice to make sure that the update still succeeds even if no DB
2111
		// rows are updated.
2112
		$response = $this->server->dispatch( $request );
2113
		$this->assertEquals( 200, $response->get_status() );
2114
2115
		$response = $this->server->dispatch( $request );
2116
		$this->assertEquals( 200, $response->get_status() );
2117
	}
2118
2119
	public function test_update_comment_status() {
2120
		wp_set_current_user( self::$admin_id );
2121
2122
		$comment_id = $this->factory->comment->create( array(
2123
			'comment_approved' => 0,
2124
			'comment_post_ID'  => self::$post_id,
2125
		));
2126
2127
		$params = array(
2128
			'status' => 'approve',
2129
		);
2130
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
2131
		$request->add_header( 'content-type', 'application/json' );
2132
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2133
2134
		$response = $this->server->dispatch( $request );
2135
		$this->assertEquals( 200, $response->get_status() );
2136
2137
		$comment = $response->get_data();
2138
		$updated = get_comment( $comment_id );
2139
		$this->assertEquals( 'approved', $comment['status'] );
2140
		$this->assertEquals( 1, $updated->comment_approved );
2141
	}
2142
2143
	public function test_update_comment_field_does_not_use_default_values() {
2144
		wp_set_current_user( self::$admin_id );
2145
2146
		$comment_id = $this->factory->comment->create( array(
2147
			'comment_approved' => 0,
2148
			'comment_post_ID'  => self::$post_id,
2149
			'comment_content'  => 'some content',
2150
		));
2151
2152
		$params = array(
2153
			'status' => 'approve',
2154
		);
2155
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
2156
		$request->add_header( 'content-type', 'application/json' );
2157
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2158
2159
		$response = $this->server->dispatch( $request );
2160
		$this->assertEquals( 200, $response->get_status() );
2161
2162
		$comment = $response->get_data();
2163
		$updated = get_comment( $comment_id );
2164
		$this->assertEquals( 'approved', $comment['status'] );
2165
		$this->assertEquals( 1, $updated->comment_approved );
2166
		$this->assertEquals( 'some content', $updated->comment_content );
2167
	}
2168
2169
	public function test_update_comment_date_gmt() {
2170
		wp_set_current_user( self::$admin_id );
2171
2172
		$params = array(
2173
			'date_gmt' => '2015-05-07T10:14:25',
2174
			'content'  => 'I\'ll be deep in the cold, cold ground before I recognize Missouri.',
2175
		);
2176
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2177
		$request->add_header( 'content-type', 'application/json' );
2178
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2179
2180
		$response = $this->server->dispatch( $request );
2181
		$this->assertEquals( 200, $response->get_status() );
2182
2183
		$comment = $response->get_data();
2184
		$updated = get_comment( self::$approved_id );
2185
		$this->assertEquals( $params['date_gmt'], $comment['date_gmt'] );
2186
		$this->assertEquals( $params['date_gmt'], mysql_to_rfc3339( $updated->comment_date_gmt ) );
2187
	}
2188
2189
	public function test_update_comment_author_email_only() {
2190
		wp_set_current_user( self::$editor_id );
2191
		update_option( 'require_name_email', 1 );
2192
2193
		$params = array(
2194
			'post'         => self::$post_id,
2195
			'author_email' => '[email protected]',
2196
			'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
2197
		);
2198
2199
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2200
		$request->add_header( 'content-type', 'application/json' );
2201
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2202
2203
		$response = $this->server->dispatch( $request );
2204
		$this->assertEquals( 200, $response->get_status() );
2205
	}
2206
2207
	public function test_update_comment_empty_author_name() {
2208
		wp_set_current_user( self::$editor_id );
2209
		update_option( 'require_name_email', 1 );
2210
2211
		$params = array(
2212
			'author_name'  => '',
2213
			'author_email' => '[email protected]',
2214
			'post'         => self::$post_id,
2215
			'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
2216
		);
2217
2218
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2219
		$request->add_header( 'content-type', 'application/json' );
2220
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2221
2222
		$response = $this->server->dispatch( $request );
2223
		$this->assertEquals( 200, $response->get_status() );
2224
	}
2225
2226
	public function test_update_comment_author_name_only() {
2227
		wp_set_current_user( self::$admin_id );
2228
		update_option( 'require_name_email', 1 );
2229
2230
		$params = array(
2231
			'post'        => self::$post_id,
2232
			'author_name' => 'Edna Krabappel',
2233
			'content'     => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
2234
		);
2235
2236
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2237
		$request->add_header( 'content-type', 'application/json' );
2238
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2239
2240
		$response = $this->server->dispatch( $request );
2241
		$this->assertEquals( 200, $response->get_status() );
2242
	}
2243
2244
	public function test_update_comment_empty_author_email() {
2245
		wp_set_current_user( self::$admin_id );
2246
		update_option( 'require_name_email', 1 );
2247
2248
		$params = array(
2249
			'post'         => self::$post_id,
2250
			'author_name'  => 'Edna Krabappel',
2251
			'author_email' => '',
2252
			'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
2253
		);
2254
2255
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2256
		$request->add_header( 'content-type', 'application/json' );
2257
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2258
2259
		$response = $this->server->dispatch( $request );
2260
		$this->assertEquals( 200, $response->get_status() );
2261
	}
2262
2263
	public function test_update_comment_author_email_too_short() {
2264
		wp_set_current_user( self::$admin_id );
2265
2266
		$params = array(
2267
			'post'         => self::$post_id,
2268
			'author_name'  => 'Homer J. Simpson',
2269
			'author_email' => 'a@b',
2270
			'content'      => 'in this house, we obey the laws of thermodynamics!',
2271
		);
2272
2273
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2274
		$request->add_header( 'content-type', 'application/json' );
2275
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2276
		$response = $this->server->dispatch( $request );
2277
2278
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
2279
		$data = $response->get_data();
2280
		$this->assertArrayHasKey( 'author_email', $data['data']['params'] );
2281
	}
2282
2283
	public function test_update_comment_invalid_type() {
2284
		wp_set_current_user( self::$admin_id );
2285
2286
		$params = array(
2287
			'type' => 'trackback',
2288
		);
2289
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2290
		$request->add_header( 'content-type', 'application/json' );
2291
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2292
2293
		$response = $this->server->dispatch( $request );
2294
		$this->assertErrorResponse( 'rest_comment_invalid_type', $response, 404 );
2295
	}
2296
2297
	public function test_update_comment_with_raw_property() {
2298
		wp_set_current_user( self::$admin_id );
2299
2300
		$params = array(
2301
			'content' => array(
2302
				'raw' => 'What the heck kind of name is Persephone?',
2303
			),
2304
		);
2305
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2306
		$request->add_header( 'content-type', 'application/json' );
2307
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2308
2309
		$response = $this->server->dispatch( $request );
2310
2311
		$this->assertEquals( 200, $response->get_status() );
2312
2313
		$comment = $response->get_data();
0 ignored issues
show
Unused Code introduced by
$comment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2314
		$updated = get_comment( self::$approved_id );
2315
		$this->assertEquals( $params['content']['raw'], $updated->comment_content );
2316
	}
2317
2318
	public function test_update_item_invalid_date() {
2319
		wp_set_current_user( self::$admin_id );
2320
2321
		$params = array(
2322
			'content' => rand_str(),
2323
			'date'    => rand_str(),
2324
		);
2325
2326
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2327
		$request->add_header( 'content-type', 'application/json' );
2328
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2329
2330
		$response = $this->server->dispatch( $request );
2331
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
2332
	}
2333
2334
	public function test_update_item_invalid_date_gmt() {
2335
		wp_set_current_user( self::$admin_id );
2336
2337
		$params = array(
2338
			'content'  => rand_str(),
2339
			'date_gmt' => rand_str(),
2340
		);
2341
2342
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2343
		$request->add_header( 'content-type', 'application/json' );
2344
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2345
2346
		$response = $this->server->dispatch( $request );
2347
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
2348
	}
2349
2350
	public function test_update_comment_invalid_id() {
2351
		wp_set_current_user( self::$subscriber_id );
2352
2353
		$params = array(
2354
			'content' => 'Oh, they have the internet on computers now!',
2355
		);
2356
		$request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
2357
		$request->add_header( 'content-type', 'application/json' );
2358
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2359
2360
		$response = $this->server->dispatch( $request );
2361
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
2362
	}
2363
2364
	public function test_update_comment_invalid_post_id() {
2365
		wp_set_current_user( self::$admin_id );
2366
2367
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2368
		$request->set_param( 'post', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
2369
2370
		$response = $this->server->dispatch( $request );
2371
		$this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
2372
	}
2373
2374
	public function test_update_comment_invalid_permission() {
2375
		add_filter( 'rest_allow_anonymous_comments', '__return_true' );
2376
2377
		$params = array(
2378
			'content' => 'Disco Stu likes disco music.',
2379
		);
2380
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
2381
		$request->add_header( 'content-type', 'application/json' );
2382
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2383
2384
		$response = $this->server->dispatch( $request );
2385
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
2386
	}
2387
2388
	public function test_update_comment_private_post_invalid_permission() {
2389
		$private_comment_id = $this->factory->comment->create( array(
2390
			'comment_approved' => 1,
2391
			'comment_post_ID'  => self::$private_id,
2392
			'user_id'          => 0,
2393
		));
2394
2395
		wp_set_current_user( self::$subscriber_id );
2396
2397
		$params = array(
2398
			'content' => 'Disco Stu likes disco music.',
2399
		);
2400
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $private_comment_id ) );
2401
		$request->add_header( 'content-type', 'application/json' );
2402
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2403
2404
		$response = $this->server->dispatch( $request );
2405
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
2406
	}
2407
2408
	public function test_update_comment_with_children_link() {
2409
		wp_set_current_user( self::$admin_id );
2410
		$comment_id_1 = $this->factory->comment->create( array(
2411
			'comment_approved' => 1,
2412
			'comment_post_ID'  => self::$post_id,
2413
			'user_id'          => self::$subscriber_id,
2414
		) );
2415
2416
		$child_comment = $this->factory->comment->create( array(
2417
			'comment_approved' => 1,
2418
			'comment_post_ID'  => self::$post_id,
2419
			'user_id'          => self::$subscriber_id,
2420
		) );
2421
2422
		// Check if comment 1 does not have the child link.
2423
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
2424
		$response = $this->server->dispatch( $request );
2425
		$this->assertEquals( 200, $response->get_status() );
2426
		$this->assertArrayNotHasKey( 'children', $response->get_links() );
2427
2428
		// Change the comment parent.
2429
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%s', $child_comment ) );
2430
		$request->set_param( 'parent', $comment_id_1 );
2431
		$request->set_param( 'content', rand_str() );
2432
		$response = $this->server->dispatch( $request );
2433
		$this->assertEquals( 200, $response->get_status() );
2434
2435
		// Check if comment 1 now has the child link.
2436
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
2437
		$response = $this->server->dispatch( $request );
2438
		$this->assertEquals( 200, $response->get_status() );
2439
		$this->assertArrayHasKey( 'children', $response->get_links() );
2440
	}
2441
2442
	/**
2443
	 * @ticket 38477
2444
	 */
2445
	public function test_update_comment_author_name_too_long() {
2446
		wp_set_current_user( self::$admin_id );
2447
2448
		$params = array(
2449
			'author_name' => rand_long_str( 246 ),
2450
			'content'     => 'This isn\'t a saxophone. It\'s an umbrella.',
2451
		);
2452
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2453
2454
		$request->add_header( 'content-type', 'application/json' );
2455
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2456
		$response = $this->server->dispatch( $request );
2457
2458
		$this->assertErrorResponse( 'comment_author_column_length', $response, 400 );
2459
	}
2460
2461
	/**
2462
	 * @ticket 38477
2463
	 */
2464
	public function test_update_comment_author_email_too_long() {
2465
		wp_set_current_user( self::$admin_id );
2466
2467
		$params = array(
2468
			'author_email' => 'murphy@' . rand_long_str( 190 ) . '.com',
2469
			'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
2470
		);
2471
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2472
2473
		$request->add_header( 'content-type', 'application/json' );
2474
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2475
		$response = $this->server->dispatch( $request );
2476
2477
		$this->assertErrorResponse( 'comment_author_email_column_length', $response, 400 );
2478
	}
2479
2480
	/**
2481
	 * @ticket 38477
2482
	 */
2483
	public function test_update_comment_author_url_too_long() {
2484
		wp_set_current_user( self::$admin_id );
2485
2486
		$params = array(
2487
			'author_url' => 'http://jazz.' . rand_long_str( 185 ) . '.com',
2488
			'content'    => 'This isn\'t a saxophone. It\'s an umbrella.',
2489
		);
2490
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2491
2492
		$request->add_header( 'content-type', 'application/json' );
2493
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2494
		$response = $this->server->dispatch( $request );
2495
2496
		$this->assertErrorResponse( 'comment_author_url_column_length', $response, 400 );
2497
	}
2498
2499
	/**
2500
	 * @ticket 38477
2501
	 */
2502
	public function test_update_comment_content_too_long() {
2503
		wp_set_current_user( self::$admin_id );
2504
2505
		$params = array(
2506
			'content' => rand_long_str( 66525 ),
2507
		);
2508
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2509
2510
		$request->add_header( 'content-type', 'application/json' );
2511
		$request->set_body( wp_json_encode( $params ) );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($params) targeting wp_json_encode() can also be of type false; however, WP_REST_Request::set_body() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
2512
		$response = $this->server->dispatch( $request );
2513
2514
		$this->assertErrorResponse( 'comment_content_column_length', $response, 400 );
2515
	}
2516
2517
	public function verify_comment_roundtrip( $input = array(), $expected_output = array() ) {
2518
		// Create the comment
2519
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
2520
		$request->set_param( 'author_email', '[email protected]' );
2521
		$request->set_param( 'post', self::$post_id );
2522
		foreach ( $input as $name => $value ) {
2523
			$request->set_param( $name, $value );
2524
		}
2525
		$response = $this->server->dispatch( $request );
2526
		$this->assertEquals( 201, $response->get_status() );
2527
		$actual_output = $response->get_data();
2528
2529
		// Compare expected API output to actual API output
2530
		$this->assertInternalType( 'array', $actual_output['content'] );
2531
		$this->assertArrayHasKey( 'raw', $actual_output['content'] );
2532
		$this->assertEquals( $expected_output['content']['raw']     , $actual_output['content']['raw'] );
2533
		$this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) );
2534
		$this->assertEquals( $expected_output['author_name']        , $actual_output['author_name'] );
2535
		$this->assertEquals( $expected_output['author_user_agent']  , $actual_output['author_user_agent'] );
2536
2537
		// Compare expected API output to WP internal values
2538
		$comment = get_comment( $actual_output['id'] );
2539
		$this->assertEquals( $expected_output['content']['raw']   , $comment->comment_content );
2540
		$this->assertEquals( $expected_output['author_name']      , $comment->comment_author );
2541
		$this->assertEquals( $expected_output['author_user_agent'], $comment->comment_agent );
2542
2543
		// Update the comment
2544
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $actual_output['id'] ) );
2545
		foreach ( $input as $name => $value ) {
2546
			$request->set_param( $name, $value );
2547
		}
2548
		// FIXME at least one value must change, or update fails
2549
		// See https://core.trac.wordpress.org/ticket/38700
2550
		$request->set_param( 'author_ip', '127.0.0.2' );
2551
		$response = $this->server->dispatch( $request );
2552
		$this->assertEquals( 200, $response->get_status() );
2553
		$actual_output = $response->get_data();
2554
2555
		// Compare expected API output to actual API output
2556
		$this->assertEquals( $expected_output['content']['raw']     , $actual_output['content']['raw'] );
2557
		$this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) );
2558
		$this->assertEquals( $expected_output['author_name']        , $actual_output['author_name'] );
2559
		$this->assertEquals( $expected_output['author_user_agent']  , $actual_output['author_user_agent'] );
2560
2561
		// Compare expected API output to WP internal values
2562
		$comment = get_comment( $actual_output['id'] );
2563
		$this->assertEquals( $expected_output['content']['raw']   , $comment->comment_content );
2564
		$this->assertEquals( $expected_output['author_name']      , $comment->comment_author );
2565
		$this->assertEquals( $expected_output['author_user_agent'], $comment->comment_agent );
2566
	}
2567
2568
	public function test_comment_roundtrip_as_editor() {
2569
		wp_set_current_user( self::$editor_id );
2570
		$this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) );
2571
		$this->verify_comment_roundtrip( array(
2572
			'content'           => '\o/ ¯\_(ツ)_/¯',
2573
			'author_name'       => '\o/ ¯\_(ツ)_/¯',
2574
			'author_user_agent' => '\o/ ¯\_(ツ)_/¯',
2575
		), array(
2576
			'content' => array(
2577
				'raw'      => '\o/ ¯\_(ツ)_/¯',
2578
				'rendered' => '<p>\o/ ¯\_(ツ)_/¯</p>',
2579
			),
2580
			'author_name'       => '\o/ ¯\_(ツ)_/¯',
2581
			'author_user_agent' => '\o/ ¯\_(ツ)_/¯',
2582
		) );
2583
	}
2584
2585
	public function test_comment_roundtrip_as_editor_unfiltered_html() {
2586
		wp_set_current_user( self::$editor_id );
2587
		if ( is_multisite() ) {
2588
			$this->assertFalse( current_user_can( 'unfiltered_html' ) );
2589
			$this->verify_comment_roundtrip( array(
2590
				'content'           => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2591
				'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2592
				'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2593
			), array(
2594
				'content' => array(
2595
					'raw'      => 'div <strong>strong</strong> oh noes',
2596
					'rendered' => '<p>div <strong>strong</strong> oh noes</p>',
2597
				),
2598
				'author_name'       => 'div strong',
2599
				'author_user_agent' => 'div strong',
2600
			) );
2601
		} else {
2602
			$this->assertTrue( current_user_can( 'unfiltered_html' ) );
2603
			$this->verify_comment_roundtrip( array(
2604
				'content'           => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2605
				'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2606
				'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2607
			), array(
2608
				'content' => array(
2609
					'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2610
					'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
2611
				),
2612
				'author_name'       => 'div strong',
2613
				'author_user_agent' => 'div strong',
2614
			) );
2615
		}
2616
	}
2617
2618
	public function test_comment_roundtrip_as_superadmin() {
2619
		wp_set_current_user( self::$superadmin_id );
2620
		$this->assertTrue( current_user_can( 'unfiltered_html' ) );
2621
		$this->verify_comment_roundtrip( array(
2622
			'content'           => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2623
			'author_name'       => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2624
			'author_user_agent' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2625
		), array(
2626
			'content' => array(
2627
				'raw'      => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2628
				'rendered' => '<p>\\\&#038;\\\ &amp; &invalid; < &lt; &amp;lt;' . "\n</p>",
2629
			),
2630
			'author_name'       => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
2631
			'author_user_agent' => '\\\&\\\ &amp; &invalid; &lt; &lt; &amp;lt;',
2632
		) );
2633
	}
2634
2635
	public function test_comment_roundtrip_as_superadmin_unfiltered_html() {
2636
		wp_set_current_user( self::$superadmin_id );
2637
		$this->assertTrue( current_user_can( 'unfiltered_html' ) );
2638
		$this->verify_comment_roundtrip( array(
2639
			'content'           => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2640
			'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2641
			'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2642
		), array(
2643
			'content' => array(
2644
				'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2645
				'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
2646
			),
2647
			'author_name'       => 'div strong',
2648
			'author_user_agent' => 'div strong',
2649
		) );
2650
	}
2651
2652
	public function test_delete_item() {
2653
		wp_set_current_user( self::$admin_id );
2654
2655
		$comment_id = $this->factory->comment->create( array(
2656
			'comment_approved' => 1,
2657
			'comment_post_ID'  => self::$post_id,
2658
			'user_id'          => self::$subscriber_id,
2659
		));
2660
2661
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
2662
		$request->set_param( 'force', 'false' );
2663
		$response = $this->server->dispatch( $request );
2664
		$this->assertEquals( 200, $response->get_status() );
2665
2666
		$data = $response->get_data();
2667
		$this->assertEquals( 'trash', $data['status'] );
2668
	}
2669
2670
	public function test_delete_item_skip_trash() {
2671
		wp_set_current_user( self::$admin_id );
2672
2673
		$comment_id = $this->factory->comment->create( array(
2674
			'comment_approved' => 1,
2675
			'comment_post_ID'  => self::$post_id,
2676
			'user_id'          => self::$subscriber_id,
2677
		));
2678
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
2679
		$request['force'] = true;
2680
2681
		$response = $this->server->dispatch( $request );
2682
		$this->assertEquals( 200, $response->get_status() );
2683
		$data = $response->get_data();
2684
		$this->assertTrue( $data['deleted'] );
2685
		$this->assertNotEmpty( $data['previous']['post'] );
2686
	}
2687
2688
	public function test_delete_item_already_trashed() {
2689
		wp_set_current_user( self::$admin_id );
2690
2691
		$comment_id = $this->factory->comment->create( array(
2692
			'comment_approved' => 1,
2693
			'comment_post_ID'  => self::$post_id,
2694
			'user_id'          => self::$subscriber_id,
2695
		));
2696
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
2697
		$response = $this->server->dispatch( $request );
2698
		$this->assertEquals( 200, $response->get_status() );
2699
		$data = $response->get_data();
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2700
		$response = $this->server->dispatch( $request );
2701
		$this->assertErrorResponse( 'rest_already_trashed', $response, 410 );
2702
	}
2703
2704
	public function test_delete_comment_invalid_id() {
2705
		wp_set_current_user( self::$admin_id );
2706
2707
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
2708
2709
		$response = $this->server->dispatch( $request );
2710
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
2711
	}
2712
2713
	public function test_delete_comment_without_permission() {
2714
		wp_set_current_user( self::$subscriber_id );
2715
2716
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2717
2718
		$response = $this->server->dispatch( $request );
2719
		$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
2720
	}
2721
2722
	public function test_delete_child_comment_link() {
2723
		wp_set_current_user( self::$admin_id );
2724
		$comment_id_1 = $this->factory->comment->create( array(
2725
			'comment_approved' => 1,
2726
			'comment_post_ID'  => self::$post_id,
2727
			'user_id'          => self::$subscriber_id,
2728
		) );
2729
2730
		$child_comment = $this->factory->comment->create( array(
2731
			'comment_approved' => 1,
2732
			'comment_parent'   => $comment_id_1,
2733
			'comment_post_ID'  => self::$post_id,
2734
			'user_id'          => self::$subscriber_id,
2735
		) );
2736
2737
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%s', $child_comment ) );
2738
		$response = $this->server->dispatch( $request );
2739
		$this->assertEquals( 200, $response->get_status() );
2740
2741
		// Verify children link is gone.
2742
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
2743
		$response = $this->server->dispatch( $request );
2744
		$this->assertEquals( 200, $response->get_status() );
2745
		$this->assertArrayNotHasKey( 'children', $response->get_links() );
2746
	}
2747
2748
	public function test_get_item_schema() {
2749
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
2750
		$response = $this->server->dispatch( $request );
2751
		$data = $response->get_data();
2752
		$properties = $data['schema']['properties'];
2753
		$this->assertEquals( 17, count( $properties ) );
2754
		$this->assertArrayHasKey( 'id', $properties );
2755
		$this->assertArrayHasKey( 'author', $properties );
2756
		$this->assertArrayHasKey( 'author_avatar_urls', $properties );
2757
		$this->assertArrayHasKey( 'author_email', $properties );
2758
		$this->assertArrayHasKey( 'author_ip', $properties );
2759
		$this->assertArrayHasKey( 'author_name', $properties );
2760
		$this->assertArrayHasKey( 'author_url', $properties );
2761
		$this->assertArrayHasKey( 'author_user_agent', $properties );
2762
		$this->assertArrayHasKey( 'content', $properties );
2763
		$this->assertArrayHasKey( 'date', $properties );
2764
		$this->assertArrayHasKey( 'date_gmt', $properties );
2765
		$this->assertArrayHasKey( 'link', $properties );
2766
		$this->assertArrayHasKey( 'meta', $properties );
2767
		$this->assertArrayHasKey( 'parent', $properties );
2768
		$this->assertArrayHasKey( 'post', $properties );
2769
		$this->assertArrayHasKey( 'status', $properties );
2770
		$this->assertArrayHasKey( 'type', $properties );
2771
2772
		$this->assertEquals( 0, $properties['parent']['default'] );
2773
		$this->assertEquals( 0, $properties['post']['default'] );
2774
2775
		$this->assertEquals( true, $properties['link']['readonly'] );
2776
		$this->assertEquals( true, $properties['type']['readonly'] );
2777
	}
2778
2779
	public function test_get_item_schema_show_avatar() {
2780
		update_option( 'show_avatars', false );
2781
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
2782
		$response = $this->server->dispatch( $request );
2783
		$data = $response->get_data();
2784
		$properties = $data['schema']['properties'];
2785
2786
		$this->assertArrayNotHasKey( 'author_avatar_urls', $properties );
2787
	}
2788
2789
	public function test_get_additional_field_registration() {
2790
2791
		$schema = array(
2792
			'type'        => 'integer',
2793
			'description' => 'Some integer of mine',
2794
			'enum'        => array( 1, 2, 3, 4 ),
2795
			'context'     => array( 'view', 'edit' ),
2796
		);
2797
2798
		register_rest_field( 'comment', 'my_custom_int', array(
2799
			'schema'          => $schema,
2800
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
2801
			'update_callback' => array( $this, 'additional_field_update_callback' ),
2802
		) );
2803
2804
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
2805
2806
		$response = $this->server->dispatch( $request );
2807
		$data = $response->get_data();
2808
2809
		$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
2810
		$this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
2811
2812
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id );
2813
2814
		$response = $this->server->dispatch( $request );
2815
		$this->assertArrayHasKey( 'my_custom_int', $response->data );
2816
2817
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments/' . self::$approved_id );
2818
		$request->set_body_params(array(
2819
			'my_custom_int' => 123,
2820
			'content'       => 'abc',
2821
		));
2822
2823
		wp_set_current_user( 1 );
2824
		$this->server->dispatch( $request );
2825
		$this->assertEquals( 123, get_comment_meta( self::$approved_id, 'my_custom_int', true ) );
2826
2827
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
2828
		$request->set_body_params(array(
2829
			'my_custom_int' => 123,
2830
			'title'         => 'hello',
2831
			'content'       => 'goodbye',
2832
			'post'          => self::$post_id,
2833
		));
2834
2835
		$response = $this->server->dispatch( $request );
2836
2837
		$this->assertEquals( 123, $response->data['my_custom_int'] );
2838
2839
		global $wp_rest_additional_fields;
2840
		$wp_rest_additional_fields = array();
2841
	}
2842
2843
	public function test_additional_field_update_errors() {
2844
		$schema = array(
2845
			'type'        => 'integer',
2846
			'description' => 'Some integer of mine',
2847
			'enum'        => array( 1, 2, 3, 4 ),
2848
			'context'     => array( 'view', 'edit' ),
2849
		);
2850
2851
		register_rest_field( 'comment', 'my_custom_int', array(
2852
			'schema'          => $schema,
2853
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
2854
			'update_callback' => array( $this, 'additional_field_update_callback' ),
2855
		) );
2856
2857
		wp_set_current_user( self::$admin_id );
2858
2859
		// Check for error on update.
2860
		$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2861
		$request->set_body_params(array(
2862
			'my_custom_int' => 'returnError',
2863
			'content' => 'abc',
2864
		));
2865
2866
		$response = $this->server->dispatch( $request );
2867
2868
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
2869
2870
		global $wp_rest_additional_fields;
2871
		$wp_rest_additional_fields = array();
2872
	}
2873
2874
	public function additional_field_get_callback( $object ) {
2875
		return get_comment_meta( $object['id'], 'my_custom_int', true );
2876
	}
2877
2878
	public function additional_field_update_callback( $value, $comment ) {
2879
		if ( 'returnError' === $value ) {
2880
			return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
2881
		}
2882
		update_comment_meta( $comment->comment_ID, 'my_custom_int', $value );
2883
	}
2884
2885
	protected function check_comment_data( $data, $context, $links ) {
2886
		$comment = get_comment( $data['id'] );
2887
2888
		$this->assertEquals( $comment->comment_ID, $data['id'] );
2889
		$this->assertEquals( $comment->comment_post_ID, $data['post'] );
2890
		$this->assertEquals( $comment->comment_parent, $data['parent'] );
2891
		$this->assertEquals( $comment->user_id, $data['author'] );
2892
		$this->assertEquals( $comment->comment_author, $data['author_name'] );
2893
		$this->assertEquals( $comment->comment_author_url, $data['author_url'] );
2894
		$this->assertEquals( wpautop( $comment->comment_content ), $data['content']['rendered'] );
2895
		$this->assertEquals( mysql_to_rfc3339( $comment->comment_date ), $data['date'] );
2896
		$this->assertEquals( mysql_to_rfc3339( $comment->comment_date_gmt ), $data['date_gmt'] );
2897
		$this->assertEquals( get_comment_link( $comment ), $data['link'] );
2898
		$this->assertContains( 'author_avatar_urls', $data );
2899
		$this->assertEqualSets( array(
2900
			'self',
2901
			'collection',
2902
			'up',
2903
		), array_keys( $links ) );
2904
2905
		if ( 'edit' === $context ) {
2906
			$this->assertEquals( $comment->comment_author_email, $data['author_email'] );
2907
			$this->assertEquals( $comment->comment_author_IP, $data['author_ip'] );
2908
			$this->assertEquals( $comment->comment_agent, $data['author_user_agent'] );
2909
			$this->assertEquals( $comment->comment_content, $data['content']['raw'] );
2910
		}
2911
2912
		if ( 'edit' !== $context ) {
2913
			$this->assertArrayNotHasKey( 'author_email', $data );
2914
			$this->assertArrayNotHasKey( 'author_ip', $data );
2915
			$this->assertArrayNotHasKey( 'author_user_agent', $data );
2916
			$this->assertArrayNotHasKey( 'raw', $data['content'] );
2917
		}
2918
	}
2919
}
2920