Completed
Branch develop (a2474f)
by
unknown
02:30
created

test_get_comment_without_children_link()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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