Completed
Branch develop (21297d)
by
unknown
02:41
created

test_additional_field_update_errors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 30
rs 8.8571
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_create_item() {
599
		wp_set_current_user( 0 );
600
601
		$params = array(
602
			'post'    => $this->post_id,
603
			'author_name'  => 'Comic Book Guy',
604
			'author_email' => '[email protected]',
605
			'author_url'   => 'http://androidsdungeon.com',
606
			'content' => 'Worst Comment Ever!',
607
			'date'    => '2014-11-07T10:14:25',
608
		);
609
610
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
611
		$request->add_header( 'content-type', 'application/json' );
612
		$request->set_body( wp_json_encode( $params ) );
613
614
		$response = $this->server->dispatch( $request );
615
		$this->assertEquals( 201, $response->get_status() );
616
617
		$data = $response->get_data();
618
		$this->check_comment_data( $data, 'view', $response->get_links() );
619
		$this->assertEquals( 'hold', $data['status'] );
620
		$this->assertEquals( '2014-11-07T10:14:25', $data['date'] );
621
		$this->assertEquals( $this->post_id, $data['post'] );
622
	}
623
624 View Code Duplication
	public function test_create_item_invalid_date() {
625
		wp_set_current_user( 0 );
626
627
		$params = array(
628
			'post'         => $this->post_id,
629
			'author_name'  => 'Reverend Lovejoy',
630
			'author_email' => '[email protected]',
631
			'author_url'   => 'http://timothylovejoy.jr',
632
			'content'      => "It\'s all over\, people! We don\'t have a prayer!",
633
			'date'         => rand_str(),
634
		);
635
636
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
637
		$request->add_header( 'content-type', 'application/json' );
638
		$request->set_body( wp_json_encode( $params ) );
639
640
		$response = $this->server->dispatch( $request );
641
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
642
	}
643
644
	public function test_create_item_assign_different_user() {
645
		$subscriber_id = $this->factory->user->create( array(
646
			'role' => 'subscriber',
647
			'user_email' => '[email protected]',
648
		));
649
650
		wp_set_current_user( $this->admin_id );
651
		$params = array(
652
			'post'    => $this->post_id,
653
			'author_name'  => 'Comic Book Guy',
654
			'author_email' => '[email protected]',
655
			'author_url'   => 'http://androidsdungeon.com',
656
			'author' => $subscriber_id,
657
			'content' => 'Worst Comment Ever!',
658
			'date'    => '2014-11-07T10:14:25',
659
		);
660
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
661
		$request->add_header( 'content-type', 'application/json' );
662
		$request->set_body( wp_json_encode( $params ) );
663
		$response = $this->server->dispatch( $request );
664
		$this->assertEquals( 201, $response->get_status() );
665
666
		$data = $response->get_data();
667
		$this->assertEquals( $subscriber_id, $data['author'] );
668
		$this->assertEquals( '127.0.0.1', $data['author_ip'] );
669
	}
670
671
	public function test_create_comment_without_type() {
672
		$post_id = $this->factory->post->create();
673
		wp_set_current_user( $this->admin_id );
674
675
		$params = array(
676
			'post'    => $post_id,
677
			'author'       => $this->admin_id,
678
			'author_name'  => 'Comic Book Guy',
679
			'author_email' => '[email protected]',
680
			'author_url'   => 'http://androidsdungeon.com',
681
			'content' => 'Worst Comment Ever!',
682
			'date'    => '2014-11-07T10:14:25',
683
		);
684
685
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
686
		$request->add_header( 'content-type', 'application/json' );
687
		$request->set_body( wp_json_encode( $params ) );
688
689
		$response = $this->server->dispatch( $request );
690
		$this->assertEquals( 201, $response->get_status() );
691
692
		$data = $response->get_data();
693
		$this->assertEquals( 'comment', $data['type'] );
694
695
		$comment_id = $data['id'];
696
697
		// Make sure the new comment is present in the collection.
698
		$collection = new WP_REST_Request( 'GET', '/wp/v2/comments' );
699
		$collection->set_param( 'post', $post_id );
700
		$collection_response = $this->server->dispatch( $collection );
701
		$collection_data = $collection_response->get_data();
702
		$this->assertEquals( $comment_id, $collection_data[0]['id'] );
703
	}
704
705
	public function test_create_item_current_user() {
706
		$user_id = $this->factory->user->create( array(
707
			'role' => 'subscriber',
708
			'user_email' => '[email protected]',
709
			'first_name' => 'Lyle',
710
			'last_name' => 'Lanley',
711
			'display_name' => 'Lyle Lanley',
712
			'user_url' => 'http://simpsons.wikia.com/wiki/Lyle_Lanley',
713
		));
714
715
		wp_set_current_user( $user_id );
716
717
		$params = array(
718
			'post' => $this->post_id,
719
			'content' => "Well sir, there's nothing on earth like a genuine, bona fide, electrified, six-car Monorail!",
720
		);
721
722
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
723
		$request->add_header( 'content-type', 'application/json' );
724
		$request->set_body( wp_json_encode( $params ) );
725
		$response = $this->server->dispatch( $request );
726
727
		$this->assertEquals( 201, $response->get_status() );
728
		$data = $response->get_data();
729
		$this->assertEquals( $user_id, $data['author'] );
730
731
		// Check author data matches
732
		$author = get_user_by( 'id', $user_id );
733
		$comment = get_comment( $data['id'] );
734
		$this->assertEquals( $author->display_name, $comment->comment_author );
735
		$this->assertEquals( $author->user_email, $comment->comment_author_email );
736
		$this->assertEquals( $author->user_url, $comment->comment_author_url );
737
	}
738
739
	public function test_create_comment_other_user() {
740
		wp_set_current_user( $this->admin_id );
741
742
		$params = array(
743
			'post'    => $this->post_id,
744
			'author_name'  => 'Homer Jay Simpson',
745
			'author_email' => '[email protected]',
746
			'author_url'   => 'http://compuglobalhypermeganet.com',
747
			'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
748
			'author'    => 0,
749
		);
750
751
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
752
		$request->add_header( 'content-type', 'application/json' );
753
		$request->set_body( wp_json_encode( $params ) );
754
		$response = $this->server->dispatch( $request );
755
756
		$this->assertEquals( 201, $response->get_status() );
757
		$data = $response->get_data();
758
		$this->assertEquals( 0, $data['author'] );
759
	}
760
761 View Code Duplication
	public function test_create_comment_other_user_without_permission() {
762
		wp_set_current_user( $this->subscriber_id );
763
764
		$params = array(
765
			'post'         => $this->post_id,
766
			'author_name'  => 'Homer Jay Simpson',
767
			'author_email' => '[email protected]',
768
			'author_url'   => 'http://compuglobalhypermeganet.com',
769
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
770
			'author'       => $this->admin_id,
771
		);
772
773
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
774
		$request->add_header( 'content-type', 'application/json' );
775
		$request->set_body( wp_json_encode( $params ) );
776
		$response = $this->server->dispatch( $request );
777
778
		$this->assertErrorResponse( 'rest_comment_invalid_author', $response, 403 );
779
	}
780
781 View Code Duplication
	public function test_create_comment_karma_without_permission() {
782
		wp_set_current_user( $this->subscriber_id );
783
784
		$params = array(
785
			'post'         => $this->post_id,
786
			'author_name'  => 'Homer Jay Simpson',
787
			'author_email' => '[email protected]',
788
			'author_url'   => 'http://compuglobalhypermeganet.com',
789
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
790
			'author'       => $this->subscriber_id,
791
			'karma'        => 100,
792
		);
793
794
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
795
		$request->add_header( 'content-type', 'application/json' );
796
		$request->set_body( wp_json_encode( $params ) );
797
		$response = $this->server->dispatch( $request );
798
799
		$this->assertErrorResponse( 'rest_comment_invalid_karma', $response, 403 );
800
	}
801
802 View Code Duplication
	public function test_create_comment_status_without_permission() {
803
		wp_set_current_user( $this->subscriber_id );
804
805
		$params = array(
806
			'post'         => $this->post_id,
807
			'author_name'  => 'Homer Jay Simpson',
808
			'author_email' => '[email protected]',
809
			'author_url'   => 'http://compuglobalhypermeganet.com',
810
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
811
			'author'       => $this->subscriber_id,
812
			'status'        => 'approved',
813
		);
814
815
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
816
		$request->add_header( 'content-type', 'application/json' );
817
		$request->set_body( wp_json_encode( $params ) );
818
		$response = $this->server->dispatch( $request );
819
820
		$this->assertErrorResponse( 'rest_comment_invalid_status', $response, 403 );
821
	}
822
823
	public function test_create_comment_with_status_and_IP() {
824
		$post_id = $this->factory->post->create();
825
		wp_set_current_user( $this->admin_id );
826
827
		$params = array(
828
			'post'         => $post_id,
829
			'author_name'  => 'Comic Book Guy',
830
			'author_email' => '[email protected]',
831
			'author_ip'    => '139.130.4.5',
832
			'author_url'   => 'http://androidsdungeon.com',
833
			'content'      => 'Worst Comment Ever!',
834
			'status'       => 'approved',
835
		);
836
837
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
838
		$request->add_header( 'content-type', 'application/json' );
839
		$request->set_body( wp_json_encode( $params ) );
840
841
		$response = $this->server->dispatch( $request );
842
		$this->assertEquals( 201, $response->get_status() );
843
844
		$data = $response->get_data();
845
		$this->assertEquals( 'approved', $data['status'] );
846
		$this->assertEquals( '139.130.4.5', $data['author_ip'] );
847
	}
848
849 View Code Duplication
	public function test_create_comment_invalid_author_IP() {
850
		wp_set_current_user( $this->admin_id );
851
852
		$params = array(
853
			'author_name'  => 'Comic Book Guy',
854
			'author_email' => '[email protected]',
855
			'author_url'   => 'http://androidsdungeon.com',
856
			'author_ip'    => '867.5309',
857
			'content'      => 'Worst Comment Ever!',
858
			'status'       => 'approved',
859
		);
860
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
861
		$request->add_header( 'content-type', 'application/json' );
862
		$request->set_body( wp_json_encode( $params ) );
863
864
		$response = $this->server->dispatch( $request );
865
866
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
867
	}
868
869 View Code Duplication
	public function test_create_comment_no_post_id() {
870
		wp_set_current_user( $this->admin_id );
871
872
		$params = array(
873
			'author_name'  => 'Comic Book Guy',
874
			'author_email' => '[email protected]',
875
			'author_url'   => 'http://androidsdungeon.com',
876
			'content'      => 'Worst Comment Ever!',
877
			'status'       => 'approved',
878
		);
879
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
880
		$request->add_header( 'content-type', 'application/json' );
881
		$request->set_body( wp_json_encode( $params ) );
882
883
		$response = $this->server->dispatch( $request );
884
		$this->assertEquals( 201, $response->get_status() );
885
	}
886
887
	public function test_create_item_duplicate() {
888
		$this->markTestSkipped( 'Needs to be revisited after wp_die handling is added' );
889
		$this->factory->comment->create(
890
			array(
891
				'comment_post_ID'      => $this->post_id,
892
				'comment_author'       => 'Guy N. Cognito',
893
				'comment_author_email' => '[email protected]',
894
				'comment_content'      => 'Homer? Who is Homer? My name is Guy N. Cognito.',
895
			)
896
		);
897
		wp_set_current_user( 0 );
898
899
		$params = array(
900
			'post'    => $this->post_id,
901
			'author_name'  => 'Guy N. Cognito',
902
			'author_email' => '[email protected]',
903
			'content' => 'Homer? Who is Homer? My name is Guy N. Cognito.',
904
		);
905
906
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
907
		$request->add_header( 'content-type', 'application/json' );
908
		$request->set_body( wp_json_encode( $params ) );
909
		$response = $this->server->dispatch( $request );
910
911
		$this->assertEquals( 409, $response->get_status() );
912
	}
913
914
	public function test_create_comment_closed() {
915
		$post_id = $this->factory->post->create( array(
916
			'comment_status' => 'closed',
917
		));
918
		wp_set_current_user( 0 );
919
920
		$params = array(
921
			'post'      => $post_id,
922
		);
923
924
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
925
		$request->add_header( 'content-type', 'application/json' );
926
		$request->set_body( wp_json_encode( $params ) );
927
		$response = $this->server->dispatch( $request );
928
929
		$this->assertEquals( 403, $response->get_status() );
930
	}
931
932 View Code Duplication
	public function test_create_comment_require_login() {
933
		wp_set_current_user( 0 );
934
		update_option( 'comment_registration', 1 );
935
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
936
		$request->set_param( 'post', $this->post_id );
937
		$response = $this->server->dispatch( $request );
938
		$this->assertEquals( 401, $response->get_status() );
939
		$data = $response->get_data();
940
		$this->assertEquals( 'rest_comment_login_required', $data['code'] );
941
	}
942
943
	public function test_create_comment_two_times() {
944
945
		$this->markTestSkipped( 'Needs to be revisited after wp_die handling is added' );
946
947
		wp_set_current_user( 0 );
948
949
		$params = array(
950
			'post'    => $this->post_id,
951
			'author_name'  => 'Comic Book Guy',
952
			'author_email' => '[email protected]',
953
			'author_url'   => 'http://androidsdungeon.com',
954
			'content' => 'Worst Comment Ever!',
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
961
		$response = $this->server->dispatch( $request );
962
		$this->assertEquals( 201, $response->get_status() );
963
964
		$params = array(
965
			'post'    => $this->post_id,
966
			'author_name'  => 'Comic Book Guy',
967
			'author_email' => '[email protected]',
968
			'author_url'   => 'http://androidsdungeon.com',
969
			'content'      => 'Shakes fist at sky',
970
		);
971
972
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
973
		$request->add_header( 'content-type', 'application/json' );
974
		$request->set_body( wp_json_encode( $params ) );
975
976
		$response = $this->server->dispatch( $request );
977
		$this->assertEquals( 400, $response->get_status() );
978
	}
979
980
	public function test_update_item() {
981
		$post_id = $this->factory->post->create();
982
983
		wp_set_current_user( $this->admin_id );
984
985
		$params = array(
986
			'content'      => "Disco Stu doesn't advertise.",
987
			'author'       => $this->subscriber_id,
988
			'author_name'  => 'Disco Stu',
989
			'author_url'   => 'http://stusdisco.com',
990
			'author_email' => '[email protected]',
991
			'author_ip'    => '4.4.4.4',
992
			'date'         => '2014-11-07T10:14:25',
993
			'karma'        => 100,
994
			'post'         => $post_id,
995
		);
996
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
997
		$request->add_header( 'content-type', 'application/json' );
998
		$request->set_body( wp_json_encode( $params ) );
999
1000
		$response = $this->server->dispatch( $request );
1001
		$this->assertEquals( 200, $response->get_status() );
1002
1003
		$comment = $response->get_data();
1004
		$updated = get_comment( $this->approved_id );
1005
		$this->assertEquals( $params['content'], $comment['content']['raw'] );
1006
		$this->assertEquals( $params['author'], $comment['author'] );
1007
		$this->assertEquals( $params['author_name'], $comment['author_name'] );
1008
		$this->assertEquals( $params['author_url'], $comment['author_url'] );
1009
		$this->assertEquals( $params['author_email'], $comment['author_email'] );
1010
		$this->assertEquals( $params['author_ip'], $comment['author_ip'] );
1011
		$this->assertEquals( $params['post'], $comment['post'] );
1012
		$this->assertEquals( $params['karma'], $comment['karma'] );
1013
1014
		$this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
1015
		$this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
1016
	}
1017
1018
	public function test_update_comment_status() {
1019
		wp_set_current_user( $this->admin_id );
1020
1021
		$comment_id = $this->factory->comment->create( array(
1022
			'comment_approved' => 0,
1023
			'comment_post_ID'  => $this->post_id,
1024
		));
1025
1026
		$params = array(
1027
			'status' => 'approve',
1028
		);
1029
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_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( $comment_id );
1038
		$this->assertEquals( 'approved', $comment['status'] );
1039
		$this->assertEquals( 1, $updated->comment_approved );
1040
	}
1041
1042
	public function test_update_comment_field_does_not_use_default_values() {
1043
		wp_set_current_user( $this->admin_id );
1044
1045
		$comment_id = $this->factory->comment->create( array(
1046
			'comment_approved' => 0,
1047
			'comment_post_ID'  => $this->post_id,
1048
			'comment_content'  => 'some content',
1049
		));
1050
1051
		$params = array(
1052
			'status' => 'approve',
1053
		);
1054
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1055
		$request->add_header( 'content-type', 'application/json' );
1056
		$request->set_body( wp_json_encode( $params ) );
1057
1058
		$response = $this->server->dispatch( $request );
1059
		$this->assertEquals( 200, $response->get_status() );
1060
1061
		$comment = $response->get_data();
1062
		$updated = get_comment( $comment_id );
1063
		$this->assertEquals( 'approved', $comment['status'] );
1064
		$this->assertEquals( 1, $updated->comment_approved );
1065
		$this->assertEquals( 'some content', $updated->comment_content );
1066
	}
1067
1068
	public function test_update_comment_date_gmt() {
1069
		wp_set_current_user( $this->admin_id );
1070
1071
		$params = array(
1072
			'date_gmt' => '2015-05-07T10:14:25',
1073
		);
1074
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1075
		$request->add_header( 'content-type', 'application/json' );
1076
		$request->set_body( wp_json_encode( $params ) );
1077
1078
		$response = $this->server->dispatch( $request );
1079
		$this->assertEquals( 200, $response->get_status() );
1080
1081
		$comment = $response->get_data();
1082
		$updated = get_comment( $this->approved_id );
1083
		$this->assertEquals( $params['date_gmt'], $comment['date_gmt'] );
1084
		$this->assertEquals( $params['date_gmt'], mysql_to_rfc3339( $updated->comment_date_gmt ) );
1085
	}
1086
1087 View Code Duplication
	public function test_update_comment_invalid_type() {
1088
		wp_set_current_user( $this->admin_id );
1089
1090
		$params = array(
1091
			'type' => 'trackback',
1092
		);
1093
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1094
		$request->add_header( 'content-type', 'application/json' );
1095
		$request->set_body( wp_json_encode( $params ) );
1096
1097
		$response = $this->server->dispatch( $request );
1098
		$this->assertErrorResponse( 'rest_comment_invalid_type', $response, 404 );
1099
	}
1100
1101 View Code Duplication
	public function test_update_item_invalid_date() {
1102
		wp_set_current_user( $this->admin_id );
1103
1104
		$params = array(
1105
			'content' => rand_str(),
1106
			'date'    => rand_str(),
1107
		);
1108
1109
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1110
		$request->add_header( 'content-type', 'application/json' );
1111
		$request->set_body( wp_json_encode( $params ) );
1112
1113
		$response = $this->server->dispatch( $request );
1114
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1115
	}
1116
1117 View Code Duplication
	public function test_update_item_invalid_date_gmt() {
1118
		wp_set_current_user( $this->admin_id );
1119
1120
		$params = array(
1121
			'content'  => rand_str(),
1122
			'date_gmt' => rand_str(),
1123
		);
1124
1125
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1126
		$request->add_header( 'content-type', 'application/json' );
1127
		$request->set_body( wp_json_encode( $params ) );
1128
1129
		$response = $this->server->dispatch( $request );
1130
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1131
	}
1132
1133 View Code Duplication
	public function test_update_comment_invalid_id() {
1134
		wp_set_current_user( 0 );
1135
1136
		$params = array(
1137
			'content' => 'Oh, they have the internet on computers now!',
1138
		);
1139
		$request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
1140
		$request->add_header( 'content-type', 'application/json' );
1141
		$request->set_body( wp_json_encode( $params ) );
1142
1143
		$response = $this->server->dispatch( $request );
1144
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
1145
	}
1146
1147 View Code Duplication
	public function test_update_comment_invalid_permission() {
1148
		wp_set_current_user( 0 );
1149
1150
		$params = array(
1151
			'content' => 'Disco Stu likes disco music.',
1152
		);
1153
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
1154
		$request->add_header( 'content-type', 'application/json' );
1155
		$request->set_body( wp_json_encode( $params ) );
1156
1157
		$response = $this->server->dispatch( $request );
1158
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
1159
	}
1160
1161 View Code Duplication
	public function test_delete_item() {
1162
		wp_set_current_user( $this->admin_id );
1163
1164
		$comment_id = $this->factory->comment->create( array(
1165
			'comment_approved' => 1,
1166
			'comment_post_ID'  => $this->post_id,
1167
			'user_id'          => $this->subscriber_id,
1168
		));
1169
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1170
1171
		$response = $this->server->dispatch( $request );
1172
		$this->assertEquals( 200, $response->get_status() );
1173
		$data = $response->get_data();
1174
		$this->assertEquals( $this->post_id, $data['post'] );
1175
	}
1176
1177 View Code Duplication
	public function test_delete_item_skip_trash() {
1178
		wp_set_current_user( $this->admin_id );
1179
1180
		$comment_id = $this->factory->comment->create( array(
1181
			'comment_approved' => 1,
1182
			'comment_post_ID'  => $this->post_id,
1183
			'user_id'          => $this->subscriber_id,
1184
		));
1185
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1186
		$request['force'] = true;
1187
1188
		$response = $this->server->dispatch( $request );
1189
		$this->assertEquals( 200, $response->get_status() );
1190
		$data = $response->get_data();
1191
		$this->assertEquals( $this->post_id, $data['post'] );
1192
	}
1193
1194 View Code Duplication
	public function test_delete_item_already_trashed() {
1195
		wp_set_current_user( $this->admin_id );
1196
1197
		$comment_id = $this->factory->comment->create( array(
1198
			'comment_approved' => 1,
1199
			'comment_post_ID'  => $this->post_id,
1200
			'user_id'          => $this->subscriber_id,
1201
		));
1202
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1203
		$response = $this->server->dispatch( $request );
1204
		$this->assertEquals( 200, $response->get_status() );
1205
		$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...
1206
		$response = $this->server->dispatch( $request );
1207
		$this->assertErrorResponse( 'rest_already_trashed', $response, 410 );
1208
	}
1209
1210 View Code Duplication
	public function test_delete_comment_invalid_id() {
1211
		wp_set_current_user( $this->admin_id );
1212
1213
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
1214
1215
		$response = $this->server->dispatch( $request );
1216
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
1217
	}
1218
1219
	public function test_delete_comment_without_permission() {
1220
		wp_set_current_user( $this->subscriber_id );
1221
1222
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1223
1224
		$response = $this->server->dispatch( $request );
1225
		$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
1226
	}
1227
1228 View Code Duplication
	public function test_get_item_schema() {
1229
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
1230
		$response = $this->server->dispatch( $request );
1231
		$data = $response->get_data();
1232
		$properties = $data['schema']['properties'];
1233
		$this->assertEquals( 17, count( $properties ) );
1234
		$this->assertArrayHasKey( 'id', $properties );
1235
		$this->assertArrayHasKey( 'author', $properties );
1236
		$this->assertArrayHasKey( 'author_avatar_urls', $properties );
1237
		$this->assertArrayHasKey( 'author_email', $properties );
1238
		$this->assertArrayHasKey( 'author_ip', $properties );
1239
		$this->assertArrayHasKey( 'author_name', $properties );
1240
		$this->assertArrayHasKey( 'author_url', $properties );
1241
		$this->assertArrayHasKey( 'author_user_agent', $properties );
1242
		$this->assertArrayHasKey( 'content', $properties );
1243
		$this->assertArrayHasKey( 'date', $properties );
1244
		$this->assertArrayHasKey( 'date_gmt', $properties );
1245
		$this->assertArrayHasKey( 'karma', $properties );
1246
		$this->assertArrayHasKey( 'link', $properties );
1247
		$this->assertArrayHasKey( 'parent', $properties );
1248
		$this->assertArrayHasKey( 'post', $properties );
1249
		$this->assertArrayHasKey( 'status', $properties );
1250
		$this->assertArrayHasKey( 'type', $properties );
1251
	}
1252
1253 View Code Duplication
	public function test_get_item_schema_show_avatar() {
1254
		update_option( 'show_avatars', false );
1255
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
1256
		$response = $this->server->dispatch( $request );
1257
		$data = $response->get_data();
1258
		$properties = $data['schema']['properties'];
1259
1260
		$this->assertArrayNotHasKey( 'author_avatar_urls', $properties );
1261
	}
1262
1263
	public function test_get_additional_field_registration() {
1264
1265
		$schema = array(
1266
			'type'        => 'integer',
1267
			'description' => 'Some integer of mine',
1268
			'enum'        => array( 1, 2, 3, 4 ),
1269
			'context'     => array( 'view', 'edit' ),
1270
		);
1271
1272
		register_rest_field( 'comment', 'my_custom_int', array(
1273
			'schema'          => $schema,
1274
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1275
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1276
		) );
1277
1278
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
1279
1280
		$response = $this->server->dispatch( $request );
1281
		$data = $response->get_data();
1282
1283
		$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
1284
		$this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
1285
1286
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $this->approved_id );
1287
1288
		$response = $this->server->dispatch( $request );
1289
		$this->assertArrayHasKey( 'my_custom_int', $response->data );
1290
1291
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments/' . $this->approved_id );
1292
		$request->set_body_params(array(
1293
			'my_custom_int' => 123,
1294
			'content' => 'abc',
1295
		));
1296
1297
		wp_set_current_user( 1 );
1298
		$this->server->dispatch( $request );
1299
		$this->assertEquals( 123, get_comment_meta( $this->approved_id, 'my_custom_int', true ) );
1300
1301
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1302
		$request->set_body_params(array(
1303
			'my_custom_int' => 123,
1304
			'title' => 'hello',
1305
			'post' => $this->post_id,
1306
		));
1307
1308
		$response = $this->server->dispatch( $request );
1309
1310
		$this->assertEquals( 123, $response->data['my_custom_int'] );
1311
1312
		global $wp_rest_additional_fields;
1313
		$wp_rest_additional_fields = array();
1314
	}
1315
1316
	public function test_additional_field_update_errors() {
1317
		$schema = array(
1318
			'type'        => 'integer',
1319
			'description' => 'Some integer of mine',
1320
			'enum'        => array( 1, 2, 3, 4 ),
1321
			'context'     => array( 'view', 'edit' ),
1322
		);
1323
1324
		register_rest_field( 'comment', 'my_custom_int', array(
1325
			'schema'          => $schema,
1326
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1327
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1328
		) );
1329
1330
		wp_set_current_user( $this->admin_id );
1331
1332
		// Check for error on update.
1333
		$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1334
		$request->set_body_params(array(
1335
			'my_custom_int' => 'returnError',
1336
			'content' => 'abc',
1337
		));
1338
1339
		$response = $this->server->dispatch( $request );
1340
1341
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1342
1343
		global $wp_rest_additional_fields;
1344
		$wp_rest_additional_fields = array();
1345
	}
1346
1347
	public function additional_field_get_callback( $object ) {
1348
		return get_comment_meta( $object['id'], 'my_custom_int', true );
1349
	}
1350
1351
	public function additional_field_update_callback( $value, $comment ) {
1352
		if ( 'returnError' === $value ) {
1353
			return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
1354
		}
1355
		update_comment_meta( $comment->comment_ID, 'my_custom_int', $value );
1356
	}
1357
1358
	protected function check_comment_data( $data, $context, $links ) {
1359
		$comment = get_comment( $data['id'] );
1360
1361
		$this->assertEquals( $comment->comment_ID, $data['id'] );
1362
		$this->assertEquals( $comment->comment_post_ID, $data['post'] );
1363
		$this->assertEquals( $comment->comment_parent, $data['parent'] );
1364
		$this->assertEquals( $comment->user_id, $data['author'] );
1365
		$this->assertEquals( $comment->comment_author, $data['author_name'] );
1366
		$this->assertEquals( $comment->comment_author_url, $data['author_url'] );
1367
		$this->assertEquals( wpautop( $comment->comment_content ), $data['content']['rendered'] );
1368
		$this->assertEquals( mysql_to_rfc3339( $comment->comment_date ), $data['date'] );
1369
		$this->assertEquals( mysql_to_rfc3339( $comment->comment_date_gmt ), $data['date_gmt'] );
1370
		$this->assertEquals( get_comment_link( $comment ), $data['link'] );
1371
		$this->assertContains( 'author_avatar_urls', $data );
1372
		$this->assertEqualSets( array(
1373
			'self',
1374
			'collection',
1375
			'up',
1376
		), array_keys( $links ) );
1377
1378
		if ( 'edit' === $context ) {
1379
			$this->assertEquals( $comment->comment_author_email, $data['author_email'] );
1380
			$this->assertEquals( $comment->comment_author_IP, $data['author_ip'] );
1381
			$this->assertEquals( $comment->comment_agent, $data['author_user_agent'] );
1382
			$this->assertEquals( $comment->comment_content, $data['content']['raw'] );
1383
			$this->assertEquals( $comment->comment_karma, $data['karma'] );
1384
		}
1385
1386
		if ( 'edit' !== $context ) {
1387
			$this->assertArrayNotHasKey( 'author_email', $data );
1388
			$this->assertArrayNotHasKey( 'author_ip', $data );
1389
			$this->assertArrayNotHasKey( 'author_user_agent', $data );
1390
			$this->assertArrayNotHasKey( 'raw', $data['content'] );
1391
			$this->assertArrayNotHasKey( 'karma', $data );
1392
		}
1393
	}
1394
}
1395