Issues (90)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/test-rest-comments-controller.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Unit tests covering WP_REST_Comments_Controller functionality.
5
 *
6
 * @package WordPress
7
 * @subpackage JSON API
8
 */
9
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase {
10
11
	protected $admin_id;
12
	protected $subscriber_id;
13
14
	protected $post_id;
15
	protected $private_id;
16
17
	protected $approved_id;
18
	protected $hold_id;
19
20
	protected $endpoint;
21
22
	public function setUp() {
23
		parent::setUp();
24
25
		$this->admin_id = $this->factory->user->create( array(
26
			'role' => 'administrator',
27
		));
28
		$this->subscriber_id = $this->factory->user->create( array(
29
			'role' => 'subscriber',
30
		));
31
		$this->author_id = $this->factory->user->create( array(
32
			'role' => 'author',
33
		));
34
35
		$this->post_id = $this->factory->post->create();
36
		$this->private_id = $this->factory->post->create( array(
37
			'post_status' => 'private',
38
		));
39
40
		$this->approved_id = $this->factory->comment->create( array(
41
			'comment_approved' => 1,
42
			'comment_post_ID'  => $this->post_id,
43
			'user_id'          => 0,
44
		));
45
		$this->hold_id = $this->factory->comment->create( array(
46
			'comment_approved' => 0,
47
			'comment_post_ID'  => $this->post_id,
48
			'user_id'          => $this->subscriber_id,
49
		));
50
51
		$this->endpoint = new WP_REST_Comments_Controller;
52
	}
53
54
	public function tearDown() {
55
		parent::tearDown();
56
	}
57
58 View Code Duplication
	public function test_register_routes() {
59
		$routes = $this->server->get_routes();
60
61
		$this->assertArrayHasKey( '/wp/v2/comments', $routes );
62
		$this->assertCount( 2, $routes['/wp/v2/comments'] );
63
		$this->assertArrayHasKey( '/wp/v2/comments/(?P<id>[\d]+)', $routes );
64
		$this->assertCount( 3, $routes['/wp/v2/comments/(?P<id>[\d]+)'] );
65
	}
66
67 View Code Duplication
	public function test_context_param() {
68
		// Collection
69
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
70
		$response = $this->server->dispatch( $request );
71
		$data = $response->get_data();
72
		$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
73
		$this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
74
		// Single
75
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments/' . $this->approved_id );
76
		$response = $this->server->dispatch( $request );
77
		$data = $response->get_data();
78
		$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
79
		$this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
80
	}
81
82 View Code Duplication
	public function test_registered_query_params() {
83
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
84
		$response = $this->server->dispatch( $request );
85
		$data = $response->get_data();
86
		$keys = array_keys( $data['endpoints'][0]['args'] );
87
		sort( $keys );
88
		$this->assertEquals( array(
89
			'after',
90
			'author',
91
			'author_email',
92
			'author_exclude',
93
			'before',
94
			'context',
95
			'exclude',
96
			'include',
97
			'karma',
98
			'offset',
99
			'order',
100
			'orderby',
101
			'page',
102
			'parent',
103
			'parent_exclude',
104
			'per_page',
105
			'post',
106
			'search',
107
			'status',
108
			'type',
109
			), $keys );
110
	}
111
112 View Code Duplication
	public function test_get_items() {
113
		$this->factory->comment->create_post_comments( $this->post_id, 6 );
114
115
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
116
117
		$response = $this->server->dispatch( $request );
118
		$this->assertEquals( 200, $response->get_status() );
119
120
		$comments = $response->get_data();
121
		// We created 6 comments in this method, plus $this->approved_id.
122
		$this->assertCount( 7, $comments );
123
	}
124
125 View Code Duplication
	public function test_get_items_without_private_post_permission() {
126
		wp_set_current_user( 0 );
127
128
		$args = array(
129
			'comment_approved' => 1,
130
			'comment_post_ID'  => $this->private_id,
131
		);
132
		$private_comment = $this->factory->comment->create( $args );
133
134
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
135
136
		$response = $this->server->dispatch( $request );
137
		$this->assertEquals( 200, $response->get_status() );
138
139
		$collection_data = $response->get_data();
140
		$this->assertFalse( in_array( $private_comment, wp_list_pluck( $collection_data, 'id' ) ) );
141
	}
142
143 View Code Duplication
	public function test_get_items_with_private_post_permission() {
144
		wp_set_current_user( $this->admin_id );
145
146
		$args = array(
147
			'comment_approved' => 1,
148
			'comment_post_ID'  => $this->private_id,
149
		);
150
		$private_comment = $this->factory->comment->create( $args );
151
152
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
153
154
		$response = $this->server->dispatch( $request );
155
		$this->assertEquals( 200, $response->get_status() );
156
157
		$collection_data = $response->get_data();
158
		$this->assertTrue( in_array( $private_comment, wp_list_pluck( $collection_data, 'id' ) ) );
159
	}
160
161 View Code Duplication
	public function test_get_items_with_invalid_post() {
162
		wp_set_current_user( 0 );
163
164
		$comment_id = $this->factory->comment->create( array(
165
			'comment_approved' => 1,
166
			'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
167
		));
168
169
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
170
171
		$response = $this->server->dispatch( $request );
172
		$this->assertEquals( 200, $response->get_status() );
173
174
		$collection_data = $response->get_data();
175
		$this->assertFalse( in_array( $comment_id, wp_list_pluck( $collection_data, 'id' ) ) );
176
177
		wp_delete_comment( $comment_id );
178
	}
179
180 View Code Duplication
	public function test_get_items_with_invalid_post_permission() {
181
		wp_set_current_user( $this->admin_id );
182
183
		$comment_id = $this->factory->comment->create( array(
184
			'comment_approved' => 1,
185
			'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
186
		));
187
188
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
189
190
		$response = $this->server->dispatch( $request );
191
		$this->assertEquals( 200, $response->get_status() );
192
193
		$collection_data = $response->get_data();
194
		$this->assertTrue( in_array( $comment_id, wp_list_pluck( $collection_data, 'id' ) ) );
195
196
		wp_delete_comment( $comment_id );
197
	}
198
199 View Code Duplication
	public function test_get_items_no_permission_for_context() {
200
		wp_set_current_user( 0 );
201
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
202
		$request->set_param( 'context', 'edit' );
203
		$response = $this->server->dispatch( $request );
204
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
205
	}
206
207
	public function test_get_items_no_post() {
208
		$this->factory->comment->create_post_comments( 0, 2 );
209
		wp_set_current_user( $this->admin_id );
210
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
211
		$request->set_param( 'post', 0 );
212
		$response = $this->server->dispatch( $request );
213
		$this->assertEquals( 200, $response->get_status() );
214
		$comments = $response->get_data();
215
		$this->assertCount( 2, $comments );
216
	}
217
218 View Code Duplication
	public function test_get_items_no_permission_for_no_post() {
219
		wp_set_current_user( 0 );
220
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
221
		$request->set_param( 'post', 0 );
222
		$response = $this->server->dispatch( $request );
223
		$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
224
	}
225
226 View Code Duplication
	public function test_get_items_edit_context() {
227
		wp_set_current_user( $this->admin_id );
228
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
229
		$request->set_param( 'context', 'edit' );
230
		$response = $this->server->dispatch( $request );
231
		$this->assertEquals( 200, $response->get_status() );
232
	}
233
234 View Code Duplication
	public function test_get_items_for_post() {
235
		$second_post_id = $this->factory->post->create();
236
		$this->factory->comment->create_post_comments( $second_post_id, 2 );
237
238
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
239
		$request->set_query_params( array(
240
			'post' => $second_post_id,
241
		) );
242
243
		$response = $this->server->dispatch( $request );
244
		$this->assertEquals( 200, $response->get_status() );
245
246
		$comments = $response->get_data();
247
		$this->assertCount( 2, $comments );
248
	}
249
250
	public function test_get_items_include_query() {
251
		wp_set_current_user( $this->admin_id );
252
		$args = array(
253
			'comment_approved' => 1,
254
			'comment_post_ID'  => $this->post_id,
255
		);
256
		$id1 = $this->factory->comment->create( $args );
257
		$this->factory->comment->create( $args );
258
		$id3 = $this->factory->comment->create( $args );
259
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
260
		// Order=>asc
261
		$request->set_param( 'order', 'asc' );
262
		$request->set_param( 'include', array( $id3, $id1 ) );
263
		$response = $this->server->dispatch( $request );
264
		$data = $response->get_data();
265
		$this->assertEquals( 2, count( $data ) );
266
		$this->assertEquals( $id1, $data[0]['id'] );
267
		// Orderby=>include
268
		$request->set_param( 'orderby', 'include' );
269
		$response = $this->server->dispatch( $request );
270
		$data = $response->get_data();
271
		$this->assertEquals( 2, count( $data ) );
272
		$this->assertEquals( $id3, $data[0]['id'] );
273
	}
274
275
	public function test_get_items_exclude_query() {
276
		wp_set_current_user( $this->admin_id );
277
		$args = array(
278
			'comment_approved' => 1,
279
			'comment_post_ID'  => $this->post_id,
280
		);
281
		$id1 = $this->factory->comment->create( $args );
282
		$id2 = $this->factory->comment->create( $args );
283
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
284
		$response = $this->server->dispatch( $request );
285
		$data = $response->get_data();
286
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
287
		$this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
288
		$request->set_param( 'exclude', array( $id2 ) );
289
		$response = $this->server->dispatch( $request );
290
		$data = $response->get_data();
291
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
292
		$this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
293
	}
294
295
	public function test_get_items_offset_query() {
296
		wp_set_current_user( $this->admin_id );
297
		$args = array(
298
			'comment_approved' => 1,
299
			'comment_post_ID'  => $this->post_id,
300
		);
301
		$this->factory->comment->create( $args );
302
		$this->factory->comment->create( $args );
303
		$this->factory->comment->create( $args );
304
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
305
		$request->set_param( 'offset', 1 );
306
		$response = $this->server->dispatch( $request );
307
		$this->assertCount( 3, $response->get_data() );
308
		// 'offset' works with 'per_page'
309
		$request->set_param( 'per_page', 2 );
310
		$response = $this->server->dispatch( $request );
311
		$this->assertCount( 2, $response->get_data() );
312
		// 'offset' takes priority over 'page'
313
		$request->set_param( 'page', 3 );
314
		$response = $this->server->dispatch( $request );
315
		$this->assertCount( 2, $response->get_data() );
316
	}
317
318
	public function test_get_items_order_query() {
319
		wp_set_current_user( $this->admin_id );
320
		$args = array(
321
			'comment_approved' => 1,
322
			'comment_post_ID'  => $this->post_id,
323
		);
324
		$this->factory->comment->create( $args );
325
		$this->factory->comment->create( $args );
326
		$id3 = $this->factory->comment->create( $args );
327
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
328
		// order defaults to 'desc'
329
		$response = $this->server->dispatch( $request );
330
		$data = $response->get_data();
331
		$this->assertEquals( $id3, $data[0]['id'] );
332
		// order=>asc
333
		$request->set_param( 'order', 'asc' );
334
		$response = $this->server->dispatch( $request );
335
		$data = $response->get_data();
336
		$this->assertEquals( $this->approved_id, $data[0]['id'] );
337
	}
338
339
	public function test_get_items_private_post_no_permissions() {
340
		wp_set_current_user( 0 );
341
		$post_id = $this->factory->post->create( array( 'post_status' => 'private' ) );
342
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
343
		$request->set_param( 'post', $post_id );
344
		$response = $this->server->dispatch( $request );
345
		$this->assertErrorResponse( 'rest_cannot_read_post', $response, 401 );
346
	}
347
348
	public function test_get_items_author_arg() {
349
		// Authorized
350
		wp_set_current_user( $this->admin_id );
351
		$args = array(
352
			'comment_approved' => 1,
353
			'comment_post_ID'  => $this->post_id,
354
			'user_id'          => $this->author_id,
355
		);
356
		$this->factory->comment->create( $args );
357
		$args['user_id'] = $this->subscriber_id;
358
		$this->factory->comment->create( $args );
359
		unset( $args['user_id'] );
360
		$this->factory->comment->create( $args );
361
362
		// 'author' limits result to 1 of 3
363
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
364
		$request->set_param( 'author', $this->author_id );
365
		$response = $this->server->dispatch( $request );
366
		$this->assertEquals( 200, $response->get_status() );
367
		$comments = $response->get_data();
368
		$this->assertCount( 1, $comments );
369
		// Multiple authors are supported
370
		$request->set_param( 'author', array( $this->author_id, $this->subscriber_id ) );
371
		$response = $this->server->dispatch( $request );
372
		$this->assertEquals( 200, $response->get_status() );
373
		$comments = $response->get_data();
374
		$this->assertCount( 2, $comments );
375
		// Unavailable to unauthenticated; defaults to error
376
		wp_set_current_user( 0 );
377
		$response = $this->server->dispatch( $request );
378
		$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
379
	}
380
381
	public function test_get_items_author_exclude_arg() {
382
		// Authorized
383
		wp_set_current_user( $this->admin_id );
384
		$args = array(
385
			'comment_approved' => 1,
386
			'comment_post_ID'  => $this->post_id,
387
			'user_id'          => $this->author_id,
388
		);
389
		$this->factory->comment->create( $args );
390
		$args['user_id'] = $this->subscriber_id;
391
		$this->factory->comment->create( $args );
392
		unset( $args['user_id'] );
393
		$this->factory->comment->create( $args );
394
395
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
396
		$response = $this->server->dispatch( $request );
397
		$comments = $response->get_data();
398
		$this->assertCount( 4, $comments );
399
400
		// 'author_exclude' limits result to 3 of 4
401
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
402
		$request->set_param( 'author_exclude', $this->author_id );
403
		$response = $this->server->dispatch( $request );
404
		$this->assertEquals( 200, $response->get_status() );
405
		$comments = $response->get_data();
406
		$this->assertCount( 3, $comments );
407
		// 'author_exclude' for both comment authors (2 of 4)
408
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
409
		$request->set_param( 'author_exclude', array( $this->author_id, $this->subscriber_id ) );
410
		$response = $this->server->dispatch( $request );
411
		$this->assertEquals( 200, $response->get_status() );
412
		$comments = $response->get_data();
413
		$this->assertCount( 2, $comments );
414
		// Unavailable to unauthenticated; defaults to error
415
		wp_set_current_user( 0 );
416
		$response = $this->server->dispatch( $request );
417
		$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
418
	}
419
420 View Code Duplication
	public function test_get_items_parent_arg() {
421
		$args = array(
422
			'comment_approved'  => 1,
423
			'comment_post_ID'   => $this->post_id,
424
		);
425
		$parent_id = $this->factory->comment->create( $args );
426
		$parent_id2 = $this->factory->comment->create( $args );
427
		$args['comment_parent'] = $parent_id;
428
		$this->factory->comment->create( $args );
429
		$args['comment_parent'] = $parent_id2;
430
		$this->factory->comment->create( $args );
431
		// All comments in the database
432
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
433
		$response = $this->server->dispatch( $request );
434
		$this->assertCount( 5, $response->get_data() );
435
		// Limit to the parent
436
		$request->set_param( 'parent', $parent_id );
437
		$response = $this->server->dispatch( $request );
438
		$this->assertCount( 1, $response->get_data() );
439
		// Limit to two parents
440
		$request->set_param( 'parent', array( $parent_id, $parent_id2 ) );
441
		$response = $this->server->dispatch( $request );
442
		$this->assertCount( 2, $response->get_data() );
443
	}
444
445 View Code Duplication
	public function test_get_items_parent_exclude_arg() {
446
		$args = array(
447
			'comment_approved'  => 1,
448
			'comment_post_ID'   => $this->post_id,
449
		);
450
		$parent_id = $this->factory->comment->create( $args );
451
		$parent_id2 = $this->factory->comment->create( $args );
452
		$args['comment_parent'] = $parent_id;
453
		$this->factory->comment->create( $args );
454
		$args['comment_parent'] = $parent_id2;
455
		$this->factory->comment->create( $args );
456
		// All comments in the database
457
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
458
		$response = $this->server->dispatch( $request );
459
		$this->assertCount( 5, $response->get_data() );
460
		// Exclude this particular parent
461
		$request->set_param( 'parent_exclude', $parent_id );
462
		$response = $this->server->dispatch( $request );
463
		$this->assertCount( 4, $response->get_data() );
464
		// Exclude both comment parents
465
		$request->set_param( 'parent_exclude', array( $parent_id, $parent_id2 ) );
466
		$response = $this->server->dispatch( $request );
467
		$this->assertCount( 3, $response->get_data() );
468
	}
469
470
	public function test_get_items_search_query() {
471
		wp_set_current_user( $this->admin_id );
472
		$args = array(
473
			'comment_approved' => 1,
474
			'comment_post_ID'  => $this->post_id,
475
			'comment_content'  => 'foo',
476
			'comment_author'   => 'Homer J Simpson',
477
		);
478
		$id1 = $this->factory->comment->create( $args );
479
		$args['comment_content'] = 'bar';
480
		$this->factory->comment->create( $args );
481
		$args['comment_content'] = 'burrito';
482
		$this->factory->comment->create( $args );
483
		// 3 comments, plus 1 created in construct
484
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
485
		$response = $this->server->dispatch( $request );
486
		$this->assertCount( 4, $response->get_data() );
487
		// One matching comments
488
		$request->set_param( 'search', 'foo' );
489
		$response = $this->server->dispatch( $request );
490
		$data = $response->get_data();
491
		$this->assertCount( 1, $data );
492
		$this->assertEquals( $id1, $data[0]['id'] );
493
	}
494
495
	public function test_get_comments_pagination_headers() {
496
		wp_set_current_user( $this->admin_id );
497
		// Start of the index
498
		for ( $i = 0; $i < 49; $i++ ) {
499
			$this->factory->comment->create( array(
500
				'comment_content'   => "Comment {$i}",
501
				'comment_post_ID'   => $this->post_id,
502
				) );
503
		}
504
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
505
		$response = $this->server->dispatch( $request );
506
		$headers = $response->get_headers();
507
		$this->assertEquals( 50, $headers['X-WP-Total'] );
508
		$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
509
		$next_link = add_query_arg( array(
510
			'page'    => 2,
511
			), rest_url( '/wp/v2/comments' ) );
512
		$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
513
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
514
		// 3rd page
515
		$this->factory->comment->create( array(
516
				'comment_content'   => 'Comment 51',
517
				'comment_post_ID'   => $this->post_id,
518
				) );
519
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
520
		$request->set_param( 'page', 3 );
521
		$response = $this->server->dispatch( $request );
522
		$headers = $response->get_headers();
523
		$this->assertEquals( 51, $headers['X-WP-Total'] );
524
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
525
		$prev_link = add_query_arg( array(
526
			'page'    => 2,
527
			), rest_url( '/wp/v2/comments' ) );
528
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
529
		$next_link = add_query_arg( array(
530
			'page'    => 4,
531
			), rest_url( '/wp/v2/comments' ) );
532
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
533
		// Last page
534
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
535
		$request->set_param( 'page', 6 );
536
		$response = $this->server->dispatch( $request );
537
		$headers = $response->get_headers();
538
		$this->assertEquals( 51, $headers['X-WP-Total'] );
539
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
540
		$prev_link = add_query_arg( array(
541
			'page'    => 5,
542
			), rest_url( '/wp/v2/comments' ) );
543
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
544
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
545
		// Out of bounds
546
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
547
		$request->set_param( 'page', 8 );
548
		$response = $this->server->dispatch( $request );
549
		$headers = $response->get_headers();
550
		$this->assertEquals( 51, $headers['X-WP-Total'] );
551
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
552
		$prev_link = add_query_arg( array(
553
			'page'    => 6,
554
			), rest_url( '/wp/v2/comments' ) );
555
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
556
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
557
	}
558
559 View Code Duplication
	public function test_get_comments_invalid_date() {
560
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
561
		$request->set_param( 'after', rand_str() );
562
		$request->set_param( 'before', rand_str() );
563
		$response = $this->server->dispatch( $request );
564
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
565
	}
566
567
	public function test_get_comments_valid_date() {
568
		$comment1 = $this->factory->comment->create( array(
0 ignored issues
show
$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...
569
			'comment_date'    => '2016-01-15T00:00:00Z',
570
			'comment_post_ID' => $this->post_id,
571
		) );
572
		$comment2 = $this->factory->comment->create( array(
573
			'comment_date'    => '2016-01-16T00:00:00Z',
574
			'comment_post_ID' => $this->post_id,
575
		) );
576
		$comment3 = $this->factory->comment->create( array(
0 ignored issues
show
$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...
577
			'comment_date'    => '2016-01-17T00:00:00Z',
578
			'comment_post_ID' => $this->post_id,
579
		) );
580
581
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
582
		$request->set_param( 'after', '2016-01-15T00:00:00Z' );
583
		$request->set_param( 'before', '2016-01-17T00:00:00Z' );
584
		$response = $this->server->dispatch( $request );
585
		$data = $response->get_data();
586
		$this->assertCount( 1, $data );
587
		$this->assertEquals( $comment2, $data[0]['id'] );
588
	}
589
590
	public function test_get_item() {
591
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
592
593
		$response = $this->server->dispatch( $request );
594
		$this->assertEquals( 200, $response->get_status() );
595
596
		$data = $response->get_data();
597
		$this->check_comment_data( $data, 'view', $response->get_links() );
598
	}
599
600 View Code Duplication
	public function test_prepare_item() {
601
		wp_set_current_user( $this->admin_id );
602
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
603
		$request->set_query_params( array(
604
			'context' => 'edit',
605
		) );
606
607
		$response = $this->server->dispatch( $request );
608
		$this->assertEquals( 200, $response->get_status() );
609
610
		$data = $response->get_data();
611
		$this->check_comment_data( $data, 'edit', $response->get_links() );
612
	}
613
614 View Code Duplication
	public function test_get_comment_author_avatar_urls() {
615
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
616
617
		$response = $this->server->dispatch( $request );
618
619
		$data = $response->get_data();
620
		$this->assertArrayHasKey( 24,  $data['author_avatar_urls'] );
621
		$this->assertArrayHasKey( 48,  $data['author_avatar_urls'] );
622
		$this->assertArrayHasKey( 96,  $data['author_avatar_urls'] );
623
624
		$comment = get_comment( $this->approved_id );
625
		/**
626
		 * Ignore the subdomain, since 'get_avatar_url randomly sets the Gravatar
627
		 * server when building the url string.
628
		 */
629
		$this->assertEquals( substr( get_avatar_url( $comment->comment_author_email ), 9 ), substr( $data['author_avatar_urls'][96], 9 ) );
630
	}
631
632
	public function test_get_comment_invalid_id() {
633
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
634
635
		$response = $this->server->dispatch( $request );
636
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
637
	}
638
639 View Code Duplication
	public function test_get_comment_invalid_context() {
640
		wp_set_current_user( 0 );
641
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $this->approved_id ) );
642
		$request->set_param( 'context', 'edit' );
643
		$response = $this->server->dispatch( $request );
644
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
645
	}
646
647
	public function test_get_comment_invalid_post_id() {
648
		wp_set_current_user( 0 );
649
		$comment_id = $this->factory->comment->create( array(
650
			'comment_approved' => 1,
651
			'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
652
		));
653
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
654
655
		$response = $this->server->dispatch( $request );
656
		$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
657
	}
658
659 View Code Duplication
	public function test_get_comment_invalid_post_id_as_admin() {
660
		wp_set_current_user( $this->admin_id );
661
		$comment_id = $this->factory->comment->create( array(
662
			'comment_approved' => 1,
663
			'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
664
		));
665
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
666
667
		$response = $this->server->dispatch( $request );
668
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
669
	}
670
671
	public function test_get_comment_not_approved() {
672
		wp_set_current_user( 0 );
673
674
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
675
676
		$response = $this->server->dispatch( $request );
677
		$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
678
	}
679
680
	public function test_get_comment_not_approved_same_user() {
681
		wp_set_current_user( $this->subscriber_id );
682
683
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
684
685
		$response = $this->server->dispatch( $request );
686
		$this->assertEquals( 200, $response->get_status() );
687
	}
688
689
	public function test_get_comment_with_children_link() {
690
		$comment_id_1 = $this->factory->comment->create( array(
691
			'comment_approved' => 1,
692
			'comment_post_ID'  => $this->post_id,
693
			'user_id'          => $this->subscriber_id,
694
		) );
695
696
		$child_comment = $this->factory->comment->create( array(
0 ignored issues
show
$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...
697
			'comment_approved' => 1,
698
			'comment_parent'   => $comment_id_1,
699
			'comment_post_ID'  => $this->post_id,
700
			'user_id'          => $this->subscriber_id,
701
		) );
702
703
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
704
		$response = $this->server->dispatch( $request );
705
		$this->assertEquals( 200, $response->get_status() );
706
		$this->assertArrayHasKey( 'children', $response->get_links() );
707
	}
708
709
	public function test_get_comment_without_children_link() {
710
		$comment_id_1 = $this->factory->comment->create( array(
711
			'comment_approved' => 1,
712
			'comment_post_ID'  => $this->post_id,
713
			'user_id'          => $this->subscriber_id,
714
		) );
715
716
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
717
		$response = $this->server->dispatch( $request );
718
		$this->assertEquals( 200, $response->get_status() );
719
		$this->assertArrayNotHasKey( 'children', $response->get_links() );
720
	}
721
722
	public function test_create_item() {
723
		wp_set_current_user( 0 );
724
725
		$params = array(
726
			'post'    => $this->post_id,
727
			'author_name'  => 'Comic Book Guy',
728
			'author_email' => '[email protected]',
729
			'author_url'   => 'http://androidsdungeon.com',
730
			'content' => 'Worst Comment Ever!',
731
			'date'    => '2014-11-07T10:14:25',
732
		);
733
734
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
735
		$request->add_header( 'content-type', 'application/json' );
736
		$request->set_body( wp_json_encode( $params ) );
737
738
		$response = $this->server->dispatch( $request );
739
		$this->assertEquals( 201, $response->get_status() );
740
741
		$data = $response->get_data();
742
		$this->check_comment_data( $data, 'view', $response->get_links() );
743
		$this->assertEquals( 'hold', $data['status'] );
744
		$this->assertEquals( '2014-11-07T10:14:25', $data['date'] );
745
		$this->assertEquals( $this->post_id, $data['post'] );
746
	}
747
748 View Code Duplication
	public function test_create_item_invalid_date() {
749
		wp_set_current_user( 0 );
750
751
		$params = array(
752
			'post'         => $this->post_id,
753
			'author_name'  => 'Reverend Lovejoy',
754
			'author_email' => '[email protected]',
755
			'author_url'   => 'http://timothylovejoy.jr',
756
			'content'      => "It\'s all over\, people! We don\'t have a prayer!",
757
			'date'         => rand_str(),
758
		);
759
760
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
761
		$request->add_header( 'content-type', 'application/json' );
762
		$request->set_body( wp_json_encode( $params ) );
763
764
		$response = $this->server->dispatch( $request );
765
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
766
	}
767
768
	public function test_create_item_assign_different_user() {
769
		$subscriber_id = $this->factory->user->create( array(
770
			'role' => 'subscriber',
771
			'user_email' => '[email protected]',
772
		));
773
774
		wp_set_current_user( $this->admin_id );
775
		$params = array(
776
			'post'    => $this->post_id,
777
			'author_name'  => 'Comic Book Guy',
778
			'author_email' => '[email protected]',
779
			'author_url'   => 'http://androidsdungeon.com',
780
			'author' => $subscriber_id,
781
			'content' => 'Worst Comment Ever!',
782
			'date'    => '2014-11-07T10:14:25',
783
		);
784
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
785
		$request->add_header( 'content-type', 'application/json' );
786
		$request->set_body( wp_json_encode( $params ) );
787
		$response = $this->server->dispatch( $request );
788
		$this->assertEquals( 201, $response->get_status() );
789
790
		$data = $response->get_data();
791
		$this->assertEquals( $subscriber_id, $data['author'] );
792
		$this->assertEquals( '127.0.0.1', $data['author_ip'] );
793
	}
794
795
	public function test_create_comment_without_type() {
796
		$post_id = $this->factory->post->create();
797
		wp_set_current_user( $this->admin_id );
798
799
		$params = array(
800
			'post'    => $post_id,
801
			'author'       => $this->admin_id,
802
			'author_name'  => 'Comic Book Guy',
803
			'author_email' => '[email protected]',
804
			'author_url'   => 'http://androidsdungeon.com',
805
			'content' => 'Worst Comment Ever!',
806
			'date'    => '2014-11-07T10:14:25',
807
		);
808
809
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
810
		$request->add_header( 'content-type', 'application/json' );
811
		$request->set_body( wp_json_encode( $params ) );
812
813
		$response = $this->server->dispatch( $request );
814
		$this->assertEquals( 201, $response->get_status() );
815
816
		$data = $response->get_data();
817
		$this->assertEquals( 'comment', $data['type'] );
818
819
		$comment_id = $data['id'];
820
821
		// Make sure the new comment is present in the collection.
822
		$collection = new WP_REST_Request( 'GET', '/wp/v2/comments' );
823
		$collection->set_param( 'post', $post_id );
824
		$collection_response = $this->server->dispatch( $collection );
825
		$collection_data = $collection_response->get_data();
826
		$this->assertEquals( $comment_id, $collection_data[0]['id'] );
827
	}
828
829
	public function test_create_item_current_user() {
830
		$user_id = $this->factory->user->create( array(
831
			'role' => 'subscriber',
832
			'user_email' => '[email protected]',
833
			'first_name' => 'Lyle',
834
			'last_name' => 'Lanley',
835
			'display_name' => 'Lyle Lanley',
836
			'user_url' => 'http://simpsons.wikia.com/wiki/Lyle_Lanley',
837
		));
838
839
		wp_set_current_user( $user_id );
840
841
		$params = array(
842
			'post' => $this->post_id,
843
			'content' => "Well sir, there's nothing on earth like a genuine, bona fide, electrified, six-car Monorail!",
844
		);
845
846
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
847
		$request->add_header( 'content-type', 'application/json' );
848
		$request->set_body( wp_json_encode( $params ) );
849
		$response = $this->server->dispatch( $request );
850
851
		$this->assertEquals( 201, $response->get_status() );
852
		$data = $response->get_data();
853
		$this->assertEquals( $user_id, $data['author'] );
854
855
		// Check author data matches
856
		$author = get_user_by( 'id', $user_id );
857
		$comment = get_comment( $data['id'] );
858
		$this->assertEquals( $author->display_name, $comment->comment_author );
859
		$this->assertEquals( $author->user_email, $comment->comment_author_email );
860
		$this->assertEquals( $author->user_url, $comment->comment_author_url );
861
	}
862
863
	public function test_create_comment_other_user() {
864
		wp_set_current_user( $this->admin_id );
865
866
		$params = array(
867
			'post'    => $this->post_id,
868
			'author_name'  => 'Homer Jay Simpson',
869
			'author_email' => '[email protected]',
870
			'author_url'   => 'http://compuglobalhypermeganet.com',
871
			'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
872
			'author'    => 0,
873
		);
874
875
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
876
		$request->add_header( 'content-type', 'application/json' );
877
		$request->set_body( wp_json_encode( $params ) );
878
		$response = $this->server->dispatch( $request );
879
880
		$this->assertEquals( 201, $response->get_status() );
881
		$data = $response->get_data();
882
		$this->assertEquals( 0, $data['author'] );
883
	}
884
885 View Code Duplication
	public function test_create_comment_other_user_without_permission() {
886
		wp_set_current_user( $this->subscriber_id );
887
888
		$params = array(
889
			'post'         => $this->post_id,
890
			'author_name'  => 'Homer Jay Simpson',
891
			'author_email' => '[email protected]',
892
			'author_url'   => 'http://compuglobalhypermeganet.com',
893
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
894
			'author'       => $this->admin_id,
895
		);
896
897
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
898
		$request->add_header( 'content-type', 'application/json' );
899
		$request->set_body( wp_json_encode( $params ) );
900
		$response = $this->server->dispatch( $request );
901
902
		$this->assertErrorResponse( 'rest_comment_invalid_author', $response, 403 );
903
	}
904
905 View Code Duplication
	public function test_create_comment_karma_without_permission() {
906
		wp_set_current_user( $this->subscriber_id );
907
908
		$params = array(
909
			'post'         => $this->post_id,
910
			'author_name'  => 'Homer Jay Simpson',
911
			'author_email' => '[email protected]',
912
			'author_url'   => 'http://compuglobalhypermeganet.com',
913
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
914
			'author'       => $this->subscriber_id,
915
			'karma'        => 100,
916
		);
917
918
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
919
		$request->add_header( 'content-type', 'application/json' );
920
		$request->set_body( wp_json_encode( $params ) );
921
		$response = $this->server->dispatch( $request );
922
923
		$this->assertErrorResponse( 'rest_comment_invalid_karma', $response, 403 );
924
	}
925
926 View Code Duplication
	public function test_create_comment_status_without_permission() {
927
		wp_set_current_user( $this->subscriber_id );
928
929
		$params = array(
930
			'post'         => $this->post_id,
931
			'author_name'  => 'Homer Jay Simpson',
932
			'author_email' => '[email protected]',
933
			'author_url'   => 'http://compuglobalhypermeganet.com',
934
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
935
			'author'       => $this->subscriber_id,
936
			'status'        => 'approved',
937
		);
938
939
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
940
		$request->add_header( 'content-type', 'application/json' );
941
		$request->set_body( wp_json_encode( $params ) );
942
		$response = $this->server->dispatch( $request );
943
944
		$this->assertErrorResponse( 'rest_comment_invalid_status', $response, 403 );
945
	}
946
947
	public function test_create_comment_with_status_and_IP() {
948
		$post_id = $this->factory->post->create();
949
		wp_set_current_user( $this->admin_id );
950
951
		$params = array(
952
			'post'         => $post_id,
953
			'author_name'  => 'Comic Book Guy',
954
			'author_email' => '[email protected]',
955
			'author_ip'    => '139.130.4.5',
956
			'author_url'   => 'http://androidsdungeon.com',
957
			'content'      => 'Worst Comment Ever!',
958
			'status'       => 'approved',
959
		);
960
961
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
962
		$request->add_header( 'content-type', 'application/json' );
963
		$request->set_body( wp_json_encode( $params ) );
964
965
		$response = $this->server->dispatch( $request );
966
		$this->assertEquals( 201, $response->get_status() );
967
968
		$data = $response->get_data();
969
		$this->assertEquals( 'approved', $data['status'] );
970
		$this->assertEquals( '139.130.4.5', $data['author_ip'] );
971
	}
972
973 View Code Duplication
	public function test_create_comment_invalid_author_IP() {
974
		wp_set_current_user( $this->admin_id );
975
976
		$params = array(
977
			'author_name'  => 'Comic Book Guy',
978
			'author_email' => '[email protected]',
979
			'author_url'   => 'http://androidsdungeon.com',
980
			'author_ip'    => '867.5309',
981
			'content'      => 'Worst Comment Ever!',
982
			'status'       => 'approved',
983
		);
984
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
985
		$request->add_header( 'content-type', 'application/json' );
986
		$request->set_body( wp_json_encode( $params ) );
987
988
		$response = $this->server->dispatch( $request );
989
990
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
991
	}
992
993 View Code Duplication
	public function test_create_comment_no_post_id() {
994
		wp_set_current_user( $this->admin_id );
995
996
		$params = array(
997
			'author_name'  => 'Comic Book Guy',
998
			'author_email' => '[email protected]',
999
			'author_url'   => 'http://androidsdungeon.com',
1000
			'content'      => 'Worst Comment Ever!',
1001
			'status'       => 'approved',
1002
		);
1003
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1004
		$request->add_header( 'content-type', 'application/json' );
1005
		$request->set_body( wp_json_encode( $params ) );
1006
1007
		$response = $this->server->dispatch( $request );
1008
		$this->assertEquals( 201, $response->get_status() );
1009
	}
1010
1011 View Code Duplication
	public function test_create_comment_no_post_id_no_permission() {
1012
		wp_set_current_user( $this->subscriber_id );
1013
1014
		$params = array(
1015
			'author_name'  => 'Homer Jay Simpson',
1016
			'author_email' => '[email protected]',
1017
			'author_url'   => 'http://compuglobalhypermeganet.com',
1018
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1019
			'author'       => $this->subscriber_id,
1020
		);
1021
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1022
		$request->add_header( 'content-type', 'application/json' );
1023
		$request->set_body( wp_json_encode( $params ) );
1024
1025
		$response = $this->server->dispatch( $request );
1026
1027
		$this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
1028
	}
1029
1030 View Code Duplication
	public function test_create_comment_private_post_invalide_permission() {
1031
		wp_set_current_user( $this->subscriber_id );
1032
1033
		$params = array(
1034
			'post'         => $this->private_id,
1035
			'author_name'  => 'Homer Jay Simpson',
1036
			'author_email' => '[email protected]',
1037
			'author_url'   => 'http://compuglobalhypermeganet.com',
1038
			'content'      => 'I\’d be a vegetarian if bacon grew on trees.',
1039
			'author'       => $this->subscriber_id,
1040
		);
1041
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1042
		$request->add_header( 'content-type', 'application/json' );
1043
		$request->set_body( wp_json_encode( $params ) );
1044
1045
		$response = $this->server->dispatch( $request );
1046
1047
		$this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 );
1048
	}
1049
1050
	public function test_create_item_duplicate() {
1051
		$this->markTestSkipped( 'Needs to be revisited after wp_die handling is added' );
1052
		$this->factory->comment->create(
1053
			array(
1054
				'comment_post_ID'      => $this->post_id,
1055
				'comment_author'       => 'Guy N. Cognito',
1056
				'comment_author_email' => '[email protected]',
1057
				'comment_content'      => 'Homer? Who is Homer? My name is Guy N. Cognito.',
1058
			)
1059
		);
1060
		wp_set_current_user( 0 );
1061
1062
		$params = array(
1063
			'post'    => $this->post_id,
1064
			'author_name'  => 'Guy N. Cognito',
1065
			'author_email' => '[email protected]',
1066
			'content' => 'Homer? Who is Homer? My name is Guy N. Cognito.',
1067
		);
1068
1069
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1070
		$request->add_header( 'content-type', 'application/json' );
1071
		$request->set_body( wp_json_encode( $params ) );
1072
		$response = $this->server->dispatch( $request );
1073
1074
		$this->assertEquals( 409, $response->get_status() );
1075
	}
1076
1077
	public function test_create_comment_closed() {
1078
		$post_id = $this->factory->post->create( array(
1079
			'comment_status' => 'closed',
1080
		));
1081
		wp_set_current_user( 0 );
1082
1083
		$params = array(
1084
			'post'      => $post_id,
1085
		);
1086
1087
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1088
		$request->add_header( 'content-type', 'application/json' );
1089
		$request->set_body( wp_json_encode( $params ) );
1090
		$response = $this->server->dispatch( $request );
1091
1092
		$this->assertEquals( 403, $response->get_status() );
1093
	}
1094
1095 View Code Duplication
	public function test_create_comment_require_login() {
1096
		wp_set_current_user( 0 );
1097
		update_option( 'comment_registration', 1 );
1098
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1099
		$request->set_param( 'post', $this->post_id );
1100
		$response = $this->server->dispatch( $request );
1101
		$this->assertEquals( 401, $response->get_status() );
1102
		$data = $response->get_data();
1103
		$this->assertEquals( 'rest_comment_login_required', $data['code'] );
1104
	}
1105
1106
	public function test_create_comment_two_times() {
1107
1108
		$this->markTestSkipped( 'Needs to be revisited after wp_die handling is added' );
1109
1110
		wp_set_current_user( 0 );
1111
1112
		$params = array(
1113
			'post'    => $this->post_id,
1114
			'author_name'  => 'Comic Book Guy',
1115
			'author_email' => '[email protected]',
1116
			'author_url'   => 'http://androidsdungeon.com',
1117
			'content' => 'Worst Comment Ever!',
1118
		);
1119
1120
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1121
		$request->add_header( 'content-type', 'application/json' );
1122
		$request->set_body( wp_json_encode( $params ) );
1123
1124
		$response = $this->server->dispatch( $request );
1125
		$this->assertEquals( 201, $response->get_status() );
1126
1127
		$params = array(
1128
			'post'    => $this->post_id,
1129
			'author_name'  => 'Comic Book Guy',
1130
			'author_email' => '[email protected]',
1131
			'author_url'   => 'http://androidsdungeon.com',
1132
			'content'      => 'Shakes fist at sky',
1133
		);
1134
1135
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1136
		$request->add_header( 'content-type', 'application/json' );
1137
		$request->set_body( wp_json_encode( $params ) );
1138
1139
		$response = $this->server->dispatch( $request );
1140
		$this->assertEquals( 400, $response->get_status() );
1141
	}
1142
1143
	public function test_update_item() {
1144
		$post_id = $this->factory->post->create();
1145
1146
		wp_set_current_user( $this->admin_id );
1147
1148
		$params = array(
1149
			'content'      => "Disco Stu doesn't advertise.",
1150
			'author'       => $this->subscriber_id,
1151
			'author_name'  => 'Disco Stu',
1152
			'author_url'   => 'http://stusdisco.com',
1153
			'author_email' => '[email protected]',
1154
			'author_ip'    => '4.4.4.4',
1155
			'date'         => '2014-11-07T10:14:25',
1156
			'karma'        => 100,
1157
			'post'         => $post_id,
1158
		);
1159
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1160
		$request->add_header( 'content-type', 'application/json' );
1161
		$request->set_body( wp_json_encode( $params ) );
1162
1163
		$response = $this->server->dispatch( $request );
1164
		$this->assertEquals( 200, $response->get_status() );
1165
1166
		$comment = $response->get_data();
1167
		$updated = get_comment( $this->approved_id );
1168
		$this->assertEquals( $params['content'], $comment['content']['raw'] );
1169
		$this->assertEquals( $params['author'], $comment['author'] );
1170
		$this->assertEquals( $params['author_name'], $comment['author_name'] );
1171
		$this->assertEquals( $params['author_url'], $comment['author_url'] );
1172
		$this->assertEquals( $params['author_email'], $comment['author_email'] );
1173
		$this->assertEquals( $params['author_ip'], $comment['author_ip'] );
1174
		$this->assertEquals( $params['post'], $comment['post'] );
1175
		$this->assertEquals( $params['karma'], $comment['karma'] );
1176
1177
		$this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
1178
		$this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
1179
	}
1180
1181
	public function test_update_comment_status() {
1182
		wp_set_current_user( $this->admin_id );
1183
1184
		$comment_id = $this->factory->comment->create( array(
1185
			'comment_approved' => 0,
1186
			'comment_post_ID'  => $this->post_id,
1187
		));
1188
1189
		$params = array(
1190
			'status' => 'approve',
1191
		);
1192
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1193
		$request->add_header( 'content-type', 'application/json' );
1194
		$request->set_body( wp_json_encode( $params ) );
1195
1196
		$response = $this->server->dispatch( $request );
1197
		$this->assertEquals( 200, $response->get_status() );
1198
1199
		$comment = $response->get_data();
1200
		$updated = get_comment( $comment_id );
1201
		$this->assertEquals( 'approved', $comment['status'] );
1202
		$this->assertEquals( 1, $updated->comment_approved );
1203
	}
1204
1205
	public function test_update_comment_field_does_not_use_default_values() {
1206
		wp_set_current_user( $this->admin_id );
1207
1208
		$comment_id = $this->factory->comment->create( array(
1209
			'comment_approved' => 0,
1210
			'comment_post_ID'  => $this->post_id,
1211
			'comment_content'  => 'some content',
1212
		));
1213
1214
		$params = array(
1215
			'status' => 'approve',
1216
		);
1217
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1218
		$request->add_header( 'content-type', 'application/json' );
1219
		$request->set_body( wp_json_encode( $params ) );
1220
1221
		$response = $this->server->dispatch( $request );
1222
		$this->assertEquals( 200, $response->get_status() );
1223
1224
		$comment = $response->get_data();
1225
		$updated = get_comment( $comment_id );
1226
		$this->assertEquals( 'approved', $comment['status'] );
1227
		$this->assertEquals( 1, $updated->comment_approved );
1228
		$this->assertEquals( 'some content', $updated->comment_content );
1229
	}
1230
1231
	public function test_update_comment_date_gmt() {
1232
		wp_set_current_user( $this->admin_id );
1233
1234
		$params = array(
1235
			'date_gmt' => '2015-05-07T10:14:25',
1236
		);
1237
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1238
		$request->add_header( 'content-type', 'application/json' );
1239
		$request->set_body( wp_json_encode( $params ) );
1240
1241
		$response = $this->server->dispatch( $request );
1242
		$this->assertEquals( 200, $response->get_status() );
1243
1244
		$comment = $response->get_data();
1245
		$updated = get_comment( $this->approved_id );
1246
		$this->assertEquals( $params['date_gmt'], $comment['date_gmt'] );
1247
		$this->assertEquals( $params['date_gmt'], mysql_to_rfc3339( $updated->comment_date_gmt ) );
1248
	}
1249
1250 View Code Duplication
	public function test_update_comment_invalid_type() {
1251
		wp_set_current_user( $this->admin_id );
1252
1253
		$params = array(
1254
			'type' => 'trackback',
1255
		);
1256
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1257
		$request->add_header( 'content-type', 'application/json' );
1258
		$request->set_body( wp_json_encode( $params ) );
1259
1260
		$response = $this->server->dispatch( $request );
1261
		$this->assertErrorResponse( 'rest_comment_invalid_type', $response, 404 );
1262
	}
1263
1264 View Code Duplication
	public function test_update_item_invalid_date() {
1265
		wp_set_current_user( $this->admin_id );
1266
1267
		$params = array(
1268
			'content' => rand_str(),
1269
			'date'    => rand_str(),
1270
		);
1271
1272
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1273
		$request->add_header( 'content-type', 'application/json' );
1274
		$request->set_body( wp_json_encode( $params ) );
1275
1276
		$response = $this->server->dispatch( $request );
1277
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1278
	}
1279
1280 View Code Duplication
	public function test_update_item_invalid_date_gmt() {
1281
		wp_set_current_user( $this->admin_id );
1282
1283
		$params = array(
1284
			'content'  => rand_str(),
1285
			'date_gmt' => rand_str(),
1286
		);
1287
1288
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1289
		$request->add_header( 'content-type', 'application/json' );
1290
		$request->set_body( wp_json_encode( $params ) );
1291
1292
		$response = $this->server->dispatch( $request );
1293
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1294
	}
1295
1296 View Code Duplication
	public function test_update_comment_invalid_id() {
1297
		wp_set_current_user( 0 );
1298
1299
		$params = array(
1300
			'content' => 'Oh, they have the internet on computers now!',
1301
		);
1302
		$request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
1303
		$request->add_header( 'content-type', 'application/json' );
1304
		$request->set_body( wp_json_encode( $params ) );
1305
1306
		$response = $this->server->dispatch( $request );
1307
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
1308
	}
1309
1310 View Code Duplication
	public function test_update_comment_invalid_permission() {
1311
		wp_set_current_user( 0 );
1312
1313
		$params = array(
1314
			'content' => 'Disco Stu likes disco music.',
1315
		);
1316
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
1317
		$request->add_header( 'content-type', 'application/json' );
1318
		$request->set_body( wp_json_encode( $params ) );
1319
1320
		$response = $this->server->dispatch( $request );
1321
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
1322
	}
1323
1324
	public function test_update_comment_private_post_invalid_permission() {
1325
		$private_comment_id = $this->factory->comment->create( array(
1326
			'comment_approved' => 1,
1327
			'comment_post_ID'  => $this->private_id,
1328
			'user_id'          => 0,
1329
		));
1330
1331
		wp_set_current_user( $this->subscriber_id );
1332
1333
		$params = array(
1334
			'content' => 'Disco Stu likes disco music.',
1335
		);
1336
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $private_comment_id ) );
1337
		$request->add_header( 'content-type', 'application/json' );
1338
		$request->set_body( wp_json_encode( $params ) );
1339
1340
		$response = $this->server->dispatch( $request );
1341
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
1342
	}
1343
1344
	public function test_update_comment_with_children_link() {
1345
		wp_set_current_user( $this->admin_id );
1346
		$comment_id_1 = $this->factory->comment->create( array(
1347
			'comment_approved' => 1,
1348
			'comment_post_ID'  => $this->post_id,
1349
			'user_id'          => $this->subscriber_id,
1350
		) );
1351
1352
		$child_comment = $this->factory->comment->create( array(
1353
			'comment_approved' => 1,
1354
			'comment_post_ID'  => $this->post_id,
1355
			'user_id'          => $this->subscriber_id,
1356
		) );
1357
1358
		// Check if comment 1 does not have the child link.
1359
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
1360
		$response = $this->server->dispatch( $request );
1361
		$this->assertEquals( 200, $response->get_status() );
1362
		$this->assertArrayNotHasKey( 'children', $response->get_links() );
1363
1364
		// Change the comment parent.
1365
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%s', $child_comment ) );
1366
		$request->set_param( 'parent', $comment_id_1 );
1367
		$response = $this->server->dispatch( $request );
1368
		$this->assertEquals( 200, $response->get_status() );
1369
1370
		// Check if comment 1 now has the child link.
1371
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
1372
		$response = $this->server->dispatch( $request );
1373
		$this->assertEquals( 200, $response->get_status() );
1374
		$this->assertArrayHasKey( 'children', $response->get_links() );
1375
	}
1376
1377 View Code Duplication
	public function test_delete_item() {
1378
		wp_set_current_user( $this->admin_id );
1379
1380
		$comment_id = $this->factory->comment->create( array(
1381
			'comment_approved' => 1,
1382
			'comment_post_ID'  => $this->post_id,
1383
			'user_id'          => $this->subscriber_id,
1384
		));
1385
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1386
1387
		$response = $this->server->dispatch( $request );
1388
		$this->assertEquals( 200, $response->get_status() );
1389
		$data = $response->get_data();
1390
		$this->assertEquals( $this->post_id, $data['post'] );
1391
	}
1392
1393 View Code Duplication
	public function test_delete_item_skip_trash() {
1394
		wp_set_current_user( $this->admin_id );
1395
1396
		$comment_id = $this->factory->comment->create( array(
1397
			'comment_approved' => 1,
1398
			'comment_post_ID'  => $this->post_id,
1399
			'user_id'          => $this->subscriber_id,
1400
		));
1401
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1402
		$request['force'] = true;
1403
1404
		$response = $this->server->dispatch( $request );
1405
		$this->assertEquals( 200, $response->get_status() );
1406
		$data = $response->get_data();
1407
		$this->assertEquals( $this->post_id, $data['post'] );
1408
	}
1409
1410 View Code Duplication
	public function test_delete_item_already_trashed() {
1411
		wp_set_current_user( $this->admin_id );
1412
1413
		$comment_id = $this->factory->comment->create( array(
1414
			'comment_approved' => 1,
1415
			'comment_post_ID'  => $this->post_id,
1416
			'user_id'          => $this->subscriber_id,
1417
		));
1418
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1419
		$response = $this->server->dispatch( $request );
1420
		$this->assertEquals( 200, $response->get_status() );
1421
		$data = $response->get_data();
1422
		$response = $this->server->dispatch( $request );
1423
		$this->assertErrorResponse( 'rest_already_trashed', $response, 410 );
1424
	}
1425
1426 View Code Duplication
	public function test_delete_comment_invalid_id() {
1427
		wp_set_current_user( $this->admin_id );
1428
1429
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
1430
1431
		$response = $this->server->dispatch( $request );
1432
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
1433
	}
1434
1435
	public function test_delete_comment_without_permission() {
1436
		wp_set_current_user( $this->subscriber_id );
1437
1438
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1439
1440
		$response = $this->server->dispatch( $request );
1441
		$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
1442
	}
1443
1444
	public function test_delete_child_comment_link() {
1445
		wp_set_current_user( $this->admin_id );
1446
		$comment_id_1 = $this->factory->comment->create( array(
1447
			'comment_approved' => 1,
1448
			'comment_post_ID'  => $this->post_id,
1449
			'user_id'          => $this->subscriber_id,
1450
		) );
1451
1452
		$child_comment = $this->factory->comment->create( array(
1453
			'comment_approved' => 1,
1454
			'comment_parent'   => $comment_id_1,
1455
			'comment_post_ID'  => $this->post_id,
1456
			'user_id'          => $this->subscriber_id,
1457
		) );
1458
1459
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%s', $child_comment ) );
1460
		$response = $this->server->dispatch( $request );
1461
		$this->assertEquals( 200, $response->get_status() );
1462
1463
		// Verify children link is gone.
1464
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
1465
		$response = $this->server->dispatch( $request );
1466
		$this->assertEquals( 200, $response->get_status() );
1467
		$this->assertArrayNotHasKey( 'children', $response->get_links() );
1468
	}
1469
1470 View Code Duplication
	public function test_get_item_schema() {
1471
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
1472
		$response = $this->server->dispatch( $request );
1473
		$data = $response->get_data();
1474
		$properties = $data['schema']['properties'];
1475
		$this->assertEquals( 17, count( $properties ) );
1476
		$this->assertArrayHasKey( 'id', $properties );
1477
		$this->assertArrayHasKey( 'author', $properties );
1478
		$this->assertArrayHasKey( 'author_avatar_urls', $properties );
1479
		$this->assertArrayHasKey( 'author_email', $properties );
1480
		$this->assertArrayHasKey( 'author_ip', $properties );
1481
		$this->assertArrayHasKey( 'author_name', $properties );
1482
		$this->assertArrayHasKey( 'author_url', $properties );
1483
		$this->assertArrayHasKey( 'author_user_agent', $properties );
1484
		$this->assertArrayHasKey( 'content', $properties );
1485
		$this->assertArrayHasKey( 'date', $properties );
1486
		$this->assertArrayHasKey( 'date_gmt', $properties );
1487
		$this->assertArrayHasKey( 'karma', $properties );
1488
		$this->assertArrayHasKey( 'link', $properties );
1489
		$this->assertArrayHasKey( 'parent', $properties );
1490
		$this->assertArrayHasKey( 'post', $properties );
1491
		$this->assertArrayHasKey( 'status', $properties );
1492
		$this->assertArrayHasKey( 'type', $properties );
1493
	}
1494
1495 View Code Duplication
	public function test_get_item_schema_show_avatar() {
1496
		update_option( 'show_avatars', false );
1497
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
1498
		$response = $this->server->dispatch( $request );
1499
		$data = $response->get_data();
1500
		$properties = $data['schema']['properties'];
1501
1502
		$this->assertArrayNotHasKey( 'author_avatar_urls', $properties );
1503
	}
1504
1505
	public function test_get_additional_field_registration() {
1506
1507
		$schema = array(
1508
			'type'        => 'integer',
1509
			'description' => 'Some integer of mine',
1510
			'enum'        => array( 1, 2, 3, 4 ),
1511
			'context'     => array( 'view', 'edit' ),
1512
		);
1513
1514
		register_rest_field( 'comment', 'my_custom_int', array(
1515
			'schema'          => $schema,
1516
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1517
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1518
		) );
1519
1520
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
1521
1522
		$response = $this->server->dispatch( $request );
1523
		$data = $response->get_data();
1524
1525
		$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
1526
		$this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
1527
1528
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $this->approved_id );
1529
1530
		$response = $this->server->dispatch( $request );
1531
		$this->assertArrayHasKey( 'my_custom_int', $response->data );
1532
1533
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments/' . $this->approved_id );
1534
		$request->set_body_params(array(
1535
			'my_custom_int' => 123,
1536
			'content' => 'abc',
1537
		));
1538
1539
		wp_set_current_user( 1 );
1540
		$this->server->dispatch( $request );
1541
		$this->assertEquals( 123, get_comment_meta( $this->approved_id, 'my_custom_int', true ) );
1542
1543
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1544
		$request->set_body_params(array(
1545
			'my_custom_int' => 123,
1546
			'title' => 'hello',
1547
			'post' => $this->post_id,
1548
		));
1549
1550
		$response = $this->server->dispatch( $request );
1551
1552
		$this->assertEquals( 123, $response->data['my_custom_int'] );
1553
1554
		global $wp_rest_additional_fields;
1555
		$wp_rest_additional_fields = array();
1556
	}
1557
1558
	public function test_additional_field_update_errors() {
1559
		$schema = array(
1560
			'type'        => 'integer',
1561
			'description' => 'Some integer of mine',
1562
			'enum'        => array( 1, 2, 3, 4 ),
1563
			'context'     => array( 'view', 'edit' ),
1564
		);
1565
1566
		register_rest_field( 'comment', 'my_custom_int', array(
1567
			'schema'          => $schema,
1568
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1569
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1570
		) );
1571
1572
		wp_set_current_user( $this->admin_id );
1573
1574
		// Check for error on update.
1575
		$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1576
		$request->set_body_params(array(
1577
			'my_custom_int' => 'returnError',
1578
			'content' => 'abc',
1579
		));
1580
1581
		$response = $this->server->dispatch( $request );
1582
1583
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1584
1585
		global $wp_rest_additional_fields;
1586
		$wp_rest_additional_fields = array();
1587
	}
1588
1589
	public function additional_field_get_callback( $object ) {
1590
		return get_comment_meta( $object['id'], 'my_custom_int', true );
1591
	}
1592
1593
	public function additional_field_update_callback( $value, $comment ) {
1594
		if ( 'returnError' === $value ) {
1595
			return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
1596
		}
1597
		update_comment_meta( $comment->comment_ID, 'my_custom_int', $value );
1598
	}
1599
1600
	protected function check_comment_data( $data, $context, $links ) {
1601
		$comment = get_comment( $data['id'] );
1602
1603
		$this->assertEquals( $comment->comment_ID, $data['id'] );
1604
		$this->assertEquals( $comment->comment_post_ID, $data['post'] );
1605
		$this->assertEquals( $comment->comment_parent, $data['parent'] );
1606
		$this->assertEquals( $comment->user_id, $data['author'] );
1607
		$this->assertEquals( $comment->comment_author, $data['author_name'] );
1608
		$this->assertEquals( $comment->comment_author_url, $data['author_url'] );
1609
		$this->assertEquals( wpautop( $comment->comment_content ), $data['content']['rendered'] );
1610
		$this->assertEquals( mysql_to_rfc3339( $comment->comment_date ), $data['date'] );
1611
		$this->assertEquals( mysql_to_rfc3339( $comment->comment_date_gmt ), $data['date_gmt'] );
1612
		$this->assertEquals( get_comment_link( $comment ), $data['link'] );
1613
		$this->assertContains( 'author_avatar_urls', $data );
1614
		$this->assertEqualSets( array(
1615
			'self',
1616
			'collection',
1617
			'up',
1618
		), array_keys( $links ) );
1619
1620
		if ( 'edit' === $context ) {
1621
			$this->assertEquals( $comment->comment_author_email, $data['author_email'] );
1622
			$this->assertEquals( $comment->comment_author_IP, $data['author_ip'] );
1623
			$this->assertEquals( $comment->comment_agent, $data['author_user_agent'] );
1624
			$this->assertEquals( $comment->comment_content, $data['content']['raw'] );
1625
			$this->assertEquals( $comment->comment_karma, $data['karma'] );
1626
		}
1627
1628
		if ( 'edit' !== $context ) {
1629
			$this->assertArrayNotHasKey( 'author_email', $data );
1630
			$this->assertArrayNotHasKey( 'author_ip', $data );
1631
			$this->assertArrayNotHasKey( 'author_user_agent', $data );
1632
			$this->assertArrayNotHasKey( 'raw', $data['content'] );
1633
			$this->assertArrayNotHasKey( 'karma', $data );
1634
		}
1635
	}
1636
}
1637