Completed
Branch develop (8edf3f)
by
unknown
02:31
created

test_create_comment_with_status_and_IP()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 25
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
		// Orderby=>desc
183
		$request->set_param( 'include', array( $id3, $id1 ) );
184
		$response = $this->server->dispatch( $request );
185
		$data = $response->get_data();
186
		$this->assertEquals( 2, count( $data ) );
187
		$this->assertEquals( $id1, $data[0]['id'] );
188
		// Orderby=>include
189
		$request->set_param( 'orderby', 'include' );
190
		$response = $this->server->dispatch( $request );
191
		$data = $response->get_data();
192
		$this->assertEquals( 2, count( $data ) );
193
		$this->assertEquals( $id3, $data[0]['id'] );
194
	}
195
196
	public function test_get_items_exclude_query() {
197
		wp_set_current_user( $this->admin_id );
198
		$args = array(
199
			'comment_approved' => 1,
200
			'comment_post_ID'  => $this->post_id,
201
		);
202
		$id1 = $this->factory->comment->create( $args );
203
		$id2 = $this->factory->comment->create( $args );
204
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
205
		$response = $this->server->dispatch( $request );
206
		$data = $response->get_data();
207
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
208
		$this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
209
		$request->set_param( 'exclude', array( $id2 ) );
210
		$response = $this->server->dispatch( $request );
211
		$data = $response->get_data();
212
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
213
		$this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
214
	}
215
216
	public function test_get_items_offset_query() {
217
		wp_set_current_user( $this->admin_id );
218
		$args = array(
219
			'comment_approved' => 1,
220
			'comment_post_ID'  => $this->post_id,
221
		);
222
		$this->factory->comment->create( $args );
223
		$this->factory->comment->create( $args );
224
		$this->factory->comment->create( $args );
225
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
226
		$request->set_param( 'offset', 1 );
227
		$response = $this->server->dispatch( $request );
228
		$this->assertCount( 3, $response->get_data() );
229
		// 'offset' works with 'per_page'
230
		$request->set_param( 'per_page', 2 );
231
		$response = $this->server->dispatch( $request );
232
		$this->assertCount( 2, $response->get_data() );
233
		// 'offset' takes priority over 'page'
234
		$request->set_param( 'page', 3 );
235
		$response = $this->server->dispatch( $request );
236
		$this->assertCount( 2, $response->get_data() );
237
	}
238
239
	public function test_get_items_private_post_no_permissions() {
240
		wp_set_current_user( 0 );
241
		$post_id = $this->factory->post->create( array( 'post_status' => 'private' ) );
242
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
243
		$request->set_param( 'post', $post_id );
244
		$response = $this->server->dispatch( $request );
245
		$this->assertErrorResponse( 'rest_cannot_read_post', $response, 401 );
246
	}
247
248
	public function test_get_items_author_arg() {
249
		// Authorized
250
		wp_set_current_user( $this->admin_id );
251
		$args = array(
252
			'comment_approved' => 1,
253
			'comment_post_ID'  => $this->post_id,
254
			'user_id'          => $this->author_id,
255
		);
256
		$this->factory->comment->create( $args );
257
		$args['user_id'] = $this->subscriber_id;
258
		$this->factory->comment->create( $args );
259
		unset( $args['user_id'] );
260
		$this->factory->comment->create( $args );
261
262
		// 'author' limits result to 1 of 3
263
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
264
		$request->set_param( 'author', $this->author_id );
265
		$response = $this->server->dispatch( $request );
266
		$this->assertEquals( 200, $response->get_status() );
267
		$comments = $response->get_data();
268
		$this->assertCount( 1, $comments );
269
		// Multiple authors are supported
270
		$request->set_param( 'author', array( $this->author_id, $this->subscriber_id ) );
271
		$response = $this->server->dispatch( $request );
272
		$this->assertEquals( 200, $response->get_status() );
273
		$comments = $response->get_data();
274
		$this->assertCount( 2, $comments );
275
		// Unavailable to unauthenticated; defaults to error
276
		wp_set_current_user( 0 );
277
		$response = $this->server->dispatch( $request );
278
		$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
279
	}
280
281
	public function test_get_items_author_exclude_arg() {
282
		// Authorized
283
		wp_set_current_user( $this->admin_id );
284
		$args = array(
285
			'comment_approved' => 1,
286
			'comment_post_ID'  => $this->post_id,
287
			'user_id'          => $this->author_id,
288
		);
289
		$this->factory->comment->create( $args );
290
		$args['user_id'] = $this->subscriber_id;
291
		$this->factory->comment->create( $args );
292
		unset( $args['user_id'] );
293
		$this->factory->comment->create( $args );
294
295
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
296
		$response = $this->server->dispatch( $request );
297
		$comments = $response->get_data();
298
		$this->assertCount( 4, $comments );
299
300
		// 'author_exclude' limits result to 3 of 4
301
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
302
		$request->set_param( 'author_exclude', $this->author_id );
303
		$response = $this->server->dispatch( $request );
304
		$this->assertEquals( 200, $response->get_status() );
305
		$comments = $response->get_data();
306
		$this->assertCount( 3, $comments );
307
		// 'author_exclude' for both comment authors (2 of 4)
308
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
309
		$request->set_param( 'author_exclude', array( $this->author_id, $this->subscriber_id ) );
310
		$response = $this->server->dispatch( $request );
311
		$this->assertEquals( 200, $response->get_status() );
312
		$comments = $response->get_data();
313
		$this->assertCount( 2, $comments );
314
		// Unavailable to unauthenticated; defaults to error
315
		wp_set_current_user( 0 );
316
		$response = $this->server->dispatch( $request );
317
		$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
318
	}
319
320 View Code Duplication
	public function test_get_items_parent_arg() {
321
		$args = array(
322
			'comment_approved'  => 1,
323
			'comment_post_ID'   => $this->post_id,
324
		);
325
		$parent_id = $this->factory->comment->create( $args );
326
		$parent_id2 = $this->factory->comment->create( $args );
327
		$args['comment_parent'] = $parent_id;
328
		$this->factory->comment->create( $args );
329
		$args['comment_parent'] = $parent_id2;
330
		$this->factory->comment->create( $args );
331
		// All comments in the database
332
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
333
		$response = $this->server->dispatch( $request );
334
		$this->assertCount( 5, $response->get_data() );
335
		// Limit to the parent
336
		$request->set_param( 'parent', $parent_id );
337
		$response = $this->server->dispatch( $request );
338
		$this->assertCount( 1, $response->get_data() );
339
		// Limit to two parents
340
		$request->set_param( 'parent', array( $parent_id, $parent_id2 ) );
341
		$response = $this->server->dispatch( $request );
342
		$this->assertCount( 2, $response->get_data() );
343
	}
344
345 View Code Duplication
	public function test_get_items_parent_exclude_arg() {
346
		$args = array(
347
			'comment_approved'  => 1,
348
			'comment_post_ID'   => $this->post_id,
349
		);
350
		$parent_id = $this->factory->comment->create( $args );
351
		$parent_id2 = $this->factory->comment->create( $args );
352
		$args['comment_parent'] = $parent_id;
353
		$this->factory->comment->create( $args );
354
		$args['comment_parent'] = $parent_id2;
355
		$this->factory->comment->create( $args );
356
		// All comments in the database
357
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
358
		$response = $this->server->dispatch( $request );
359
		$this->assertCount( 5, $response->get_data() );
360
		// Exclude this particular parent
361
		$request->set_param( 'parent_exclude', $parent_id );
362
		$response = $this->server->dispatch( $request );
363
		$this->assertCount( 4, $response->get_data() );
364
		// Exclude both comment parents
365
		$request->set_param( 'parent_exclude', array( $parent_id, $parent_id2 ) );
366
		$response = $this->server->dispatch( $request );
367
		$this->assertCount( 3, $response->get_data() );
368
	}
369
370
	public function test_get_items_search_query() {
371
		wp_set_current_user( $this->admin_id );
372
		$args = array(
373
			'comment_approved' => 1,
374
			'comment_post_ID'  => $this->post_id,
375
			'comment_content'  => 'foo',
376
			'comment_author'   => 'Homer J Simpson',
377
		);
378
		$id1 = $this->factory->comment->create( $args );
379
		$args['comment_content'] = 'bar';
380
		$this->factory->comment->create( $args );
381
		$args['comment_content'] = 'burrito';
382
		$this->factory->comment->create( $args );
383
		// 3 comments, plus 1 created in construct
384
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
385
		$response = $this->server->dispatch( $request );
386
		$this->assertCount( 4, $response->get_data() );
387
		// One matching comments
388
		$request->set_param( 'search', 'foo' );
389
		$response = $this->server->dispatch( $request );
390
		$data = $response->get_data();
391
		$this->assertCount( 1, $data );
392
		$this->assertEquals( $id1, $data[0]['id'] );
393
	}
394
395
	public function test_get_comments_pagination_headers() {
396
		wp_set_current_user( $this->admin_id );
397
		// Start of the index
398
		for ( $i = 0; $i < 49; $i++ ) {
399
			$this->factory->comment->create( array(
400
				'comment_content'   => "Comment {$i}",
401
				'comment_post_ID'   => $this->post_id,
402
				) );
403
		}
404
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
405
		$response = $this->server->dispatch( $request );
406
		$headers = $response->get_headers();
407
		$this->assertEquals( 50, $headers['X-WP-Total'] );
408
		$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
409
		$next_link = add_query_arg( array(
410
			'page'    => 2,
411
			), rest_url( '/wp/v2/comments' ) );
412
		$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
413
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
414
		// 3rd page
415
		$this->factory->comment->create( array(
416
				'comment_content'   => 'Comment 51',
417
				'comment_post_ID'   => $this->post_id,
418
				) );
419
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
420
		$request->set_param( 'page', 3 );
421
		$response = $this->server->dispatch( $request );
422
		$headers = $response->get_headers();
423
		$this->assertEquals( 51, $headers['X-WP-Total'] );
424
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
425
		$prev_link = add_query_arg( array(
426
			'page'    => 2,
427
			), rest_url( '/wp/v2/comments' ) );
428
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
429
		$next_link = add_query_arg( array(
430
			'page'    => 4,
431
			), rest_url( '/wp/v2/comments' ) );
432
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
433
		// Last page
434
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
435
		$request->set_param( 'page', 6 );
436
		$response = $this->server->dispatch( $request );
437
		$headers = $response->get_headers();
438
		$this->assertEquals( 51, $headers['X-WP-Total'] );
439
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
440
		$prev_link = add_query_arg( array(
441
			'page'    => 5,
442
			), rest_url( '/wp/v2/comments' ) );
443
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
444
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
445
		// Out of bounds
446
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
447
		$request->set_param( 'page', 8 );
448
		$response = $this->server->dispatch( $request );
449
		$headers = $response->get_headers();
450
		$this->assertEquals( 51, $headers['X-WP-Total'] );
451
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
452
		$prev_link = add_query_arg( array(
453
			'page'    => 6,
454
			), rest_url( '/wp/v2/comments' ) );
455
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
456
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
457
	}
458
459 View Code Duplication
	public function test_get_comments_invalid_date() {
460
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
461
		$request->set_param( 'after', rand_str() );
462
		$request->set_param( 'before', rand_str() );
463
		$response = $this->server->dispatch( $request );
464
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
465
	}
466
467
	public function test_get_comments_valid_date() {
468
		$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...
469
			'comment_date'    => '2016-01-15T00:00:00Z',
470
			'comment_post_ID' => $this->post_id,
471
		) );
472
		$comment2 = $this->factory->comment->create( array(
473
			'comment_date'    => '2016-01-16T00:00:00Z',
474
			'comment_post_ID' => $this->post_id,
475
		) );
476
		$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...
477
			'comment_date'    => '2016-01-17T00:00:00Z',
478
			'comment_post_ID' => $this->post_id,
479
		) );
480
481
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
482
		$request->set_param( 'after', '2016-01-15T00:00:00Z' );
483
		$request->set_param( 'before', '2016-01-17T00:00:00Z' );
484
		$response = $this->server->dispatch( $request );
485
		$data = $response->get_data();
486
		$this->assertCount( 1, $data );
487
		$this->assertEquals( $comment2, $data[0]['id'] );
488
	}
489
490
	public function test_get_item() {
491
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
492
493
		$response = $this->server->dispatch( $request );
494
		$this->assertEquals( 200, $response->get_status() );
495
496
		$data = $response->get_data();
497
		$this->check_comment_data( $data, 'view', $response->get_links() );
498
	}
499
500 View Code Duplication
	public function test_prepare_item() {
501
		wp_set_current_user( $this->admin_id );
502
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
503
		$request->set_query_params( array(
504
			'context' => 'edit',
505
		) );
506
507
		$response = $this->server->dispatch( $request );
508
		$this->assertEquals( 200, $response->get_status() );
509
510
		$data = $response->get_data();
511
		$this->check_comment_data( $data, 'edit', $response->get_links() );
512
	}
513
514 View Code Duplication
	public function test_get_comment_author_avatar_urls() {
515
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
516
517
		$response = $this->server->dispatch( $request );
518
519
		$data = $response->get_data();
520
		$this->assertArrayHasKey( 24,  $data['author_avatar_urls'] );
521
		$this->assertArrayHasKey( 48,  $data['author_avatar_urls'] );
522
		$this->assertArrayHasKey( 96,  $data['author_avatar_urls'] );
523
524
		$comment = get_comment( $this->approved_id );
525
		/**
526
		 * Ignore the subdomain, since 'get_avatar_url randomly sets the Gravatar
527
		 * server when building the url string.
528
		 */
529
		$this->assertEquals( substr( get_avatar_url( $comment->comment_author_email ), 9 ), substr( $data['author_avatar_urls'][96], 9 ) );
530
	}
531
532
	public function test_get_comment_invalid_id() {
533
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
534
535
		$response = $this->server->dispatch( $request );
536
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
537
	}
538
539 View Code Duplication
	public function test_get_comment_invalid_context() {
540
		wp_set_current_user( 0 );
541
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $this->approved_id ) );
542
		$request->set_param( 'context', 'edit' );
543
		$response = $this->server->dispatch( $request );
544
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
545
	}
546
547
	public function test_get_comment_invalid_post_id() {
548
		$comment_id = $this->factory->comment->create( array(
549
			'comment_approved' => 1,
550
			'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
551
		));
552
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
553
554
		$response = $this->server->dispatch( $request );
555
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
556
	}
557
558
	public function test_get_comment_not_approved() {
559
		wp_set_current_user( 0 );
560
561
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
562
563
		$response = $this->server->dispatch( $request );
564
		$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
565
	}
566
567
	public function test_get_comment_not_approved_same_user() {
568
		wp_set_current_user( $this->subscriber_id );
569
570
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
571
572
		$response = $this->server->dispatch( $request );
573
		$this->assertEquals( 200, $response->get_status() );
574
	}
575
576
	public function test_create_item() {
577
		wp_set_current_user( 0 );
578
579
		$params = array(
580
			'post'    => $this->post_id,
581
			'author_name'  => 'Comic Book Guy',
582
			'author_email' => '[email protected]',
583
			'author_url'   => 'http://androidsdungeon.com',
584
			'content' => 'Worst Comment Ever!',
585
			'date'    => '2014-11-07T10:14:25',
586
		);
587
588
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
589
		$request->add_header( 'content-type', 'application/json' );
590
		$request->set_body( wp_json_encode( $params ) );
591
592
		$response = $this->server->dispatch( $request );
593
		$this->assertEquals( 201, $response->get_status() );
594
595
		$data = $response->get_data();
596
		$this->check_comment_data( $data, 'view', $response->get_links() );
597
		$this->assertEquals( 'hold', $data['status'] );
598
		$this->assertEquals( '2014-11-07T10:14:25', $data['date'] );
599
		$this->assertEquals( $this->post_id, $data['post'] );
600
	}
601
602 View Code Duplication
	public function test_create_item_invalid_date() {
603
		wp_set_current_user( 0 );
604
605
		$params = array(
606
			'post'         => $this->post_id,
607
			'author_name'  => 'Reverend Lovejoy',
608
			'author_email' => '[email protected]',
609
			'author_url'   => 'http://timothylovejoy.jr',
610
			'content'      => "It\'s all over\, people! We don\'t have a prayer!",
611
			'date'         => rand_str(),
612
		);
613
614
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
615
		$request->add_header( 'content-type', 'application/json' );
616
		$request->set_body( wp_json_encode( $params ) );
617
618
		$response = $this->server->dispatch( $request );
619
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
620
	}
621
622
	public function test_create_item_assign_different_user() {
623
		$subscriber_id = $this->factory->user->create( array(
624
			'role' => 'subscriber',
625
			'user_email' => '[email protected]',
626
		));
627
628
		wp_set_current_user( $this->admin_id );
629
		$params = array(
630
			'post'    => $this->post_id,
631
			'author_name'  => 'Comic Book Guy',
632
			'author_email' => '[email protected]',
633
			'author_url'   => 'http://androidsdungeon.com',
634
			'author' => $subscriber_id,
635
			'content' => 'Worst Comment Ever!',
636
			'date'    => '2014-11-07T10:14:25',
637
		);
638
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
639
		$request->add_header( 'content-type', 'application/json' );
640
		$request->set_body( wp_json_encode( $params ) );
641
		$response = $this->server->dispatch( $request );
642
		$this->assertEquals( 201, $response->get_status() );
643
644
		$data = $response->get_data();
645
		$this->assertEquals( $subscriber_id, $data['author'] );
646
		$this->assertEquals( '127.0.0.1', $data['author_ip'] );
647
	}
648
649
	public function test_create_comment_without_type() {
650
		$post_id = $this->factory->post->create();
651
		wp_set_current_user( $this->admin_id );
652
653
		$params = array(
654
			'post'    => $post_id,
655
			'author'       => $this->admin_id,
656
			'author_name'  => 'Comic Book Guy',
657
			'author_email' => '[email protected]',
658
			'author_url'   => 'http://androidsdungeon.com',
659
			'content' => 'Worst Comment Ever!',
660
			'date'    => '2014-11-07T10:14:25',
661
		);
662
663
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
664
		$request->add_header( 'content-type', 'application/json' );
665
		$request->set_body( wp_json_encode( $params ) );
666
667
		$response = $this->server->dispatch( $request );
668
		$this->assertEquals( 201, $response->get_status() );
669
670
		$data = $response->get_data();
671
		$this->assertEquals( 'comment', $data['type'] );
672
673
		$comment_id = $data['id'];
674
675
		// Make sure the new comment is present in the collection.
676
		$collection = new WP_REST_Request( 'GET', '/wp/v2/comments' );
677
		$collection->set_param( 'post', $post_id );
678
		$collection_response = $this->server->dispatch( $collection );
679
		$collection_data = $collection_response->get_data();
680
		$this->assertEquals( $comment_id, $collection_data[0]['id'] );
681
	}
682
683
	public function test_create_item_current_user() {
684
		$user_id = $this->factory->user->create( array(
685
			'role' => 'subscriber',
686
			'user_email' => '[email protected]',
687
			'first_name' => 'Lyle',
688
			'last_name' => 'Lanley',
689
			'display_name' => 'Lyle Lanley',
690
			'user_url' => 'http://simpsons.wikia.com/wiki/Lyle_Lanley',
691
		));
692
693
		wp_set_current_user( $user_id );
694
695
		$params = array(
696
			'post' => $this->post_id,
697
			'content' => "Well sir, there's nothing on earth like a genuine, bona fide, electrified, six-car Monorail!",
698
		);
699
700
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
701
		$request->add_header( 'content-type', 'application/json' );
702
		$request->set_body( wp_json_encode( $params ) );
703
		$response = $this->server->dispatch( $request );
704
705
		$this->assertEquals( 201, $response->get_status() );
706
		$data = $response->get_data();
707
		$this->assertEquals( $user_id, $data['author'] );
708
709
		// Check author data matches
710
		$author = get_user_by( 'id', $user_id );
711
		$comment = get_comment( $data['id'] );
712
		$this->assertEquals( $author->display_name, $comment->comment_author );
713
		$this->assertEquals( $author->user_email, $comment->comment_author_email );
714
		$this->assertEquals( $author->user_url, $comment->comment_author_url );
715
	}
716
717
	public function test_create_comment_other_user() {
718
		wp_set_current_user( $this->admin_id );
719
720
		$params = array(
721
			'post'    => $this->post_id,
722
			'author_name'  => 'Homer Jay Simpson',
723
			'author_email' => '[email protected]',
724
			'author_url'   => 'http://compuglobalhypermeganet.com',
725
			'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
726
			'author'    => 0,
727
		);
728
729
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
730
		$request->add_header( 'content-type', 'application/json' );
731
		$request->set_body( wp_json_encode( $params ) );
732
		$response = $this->server->dispatch( $request );
733
734
		$this->assertEquals( 201, $response->get_status() );
735
		$data = $response->get_data();
736
		$this->assertEquals( 0, $data['author'] );
737
	}
738
739 View Code Duplication
	public function test_create_comment_other_user_without_permission() {
740
		wp_set_current_user( $this->subscriber_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'       => $this->admin_id,
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->assertErrorResponse( 'rest_comment_invalid_author', $response, 403 );
757
	}
758
759 View Code Duplication
	public function test_create_comment_karma_without_permission() {
760
		wp_set_current_user( $this->subscriber_id );
761
762
		$params = array(
763
			'post'         => $this->post_id,
764
			'author_name'  => 'Homer Jay Simpson',
765
			'author_email' => '[email protected]',
766
			'author_url'   => 'http://compuglobalhypermeganet.com',
767
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
768
			'author'       => $this->subscriber_id,
769
			'karma'        => 100,
770
		);
771
772
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
773
		$request->add_header( 'content-type', 'application/json' );
774
		$request->set_body( wp_json_encode( $params ) );
775
		$response = $this->server->dispatch( $request );
776
777
		$this->assertErrorResponse( 'rest_comment_invalid_karma', $response, 403 );
778
	}
779
780 View Code Duplication
	public function test_create_comment_status_without_permission() {
781
		wp_set_current_user( $this->subscriber_id );
782
783
		$params = array(
784
			'post'         => $this->post_id,
785
			'author_name'  => 'Homer Jay Simpson',
786
			'author_email' => '[email protected]',
787
			'author_url'   => 'http://compuglobalhypermeganet.com',
788
			'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
789
			'author'       => $this->subscriber_id,
790
			'status'        => 'approved',
791
		);
792
793
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
794
		$request->add_header( 'content-type', 'application/json' );
795
		$request->set_body( wp_json_encode( $params ) );
796
		$response = $this->server->dispatch( $request );
797
798
		$this->assertErrorResponse( 'rest_comment_invalid_status', $response, 403 );
799
	}
800
801
	public function test_create_comment_with_status_and_IP() {
802
		$post_id = $this->factory->post->create();
803
		wp_set_current_user( $this->admin_id );
804
805
		$params = array(
806
			'post'         => $post_id,
807
			'author_name'  => 'Comic Book Guy',
808
			'author_email' => '[email protected]',
809
			'author_ip'    => '139.130.4.5',
810
			'author_url'   => 'http://androidsdungeon.com',
811
			'content'      => 'Worst Comment Ever!',
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
819
		$response = $this->server->dispatch( $request );
820
		$this->assertEquals( 201, $response->get_status() );
821
822
		$data = $response->get_data();
823
		$this->assertEquals( 'approved', $data['status'] );
824
		$this->assertEquals( '139.130.4.5', $data['author_ip'] );
825
	}
826
827 View Code Duplication
	public function test_create_comment_invalid_author_IP() {
828
		wp_set_current_user( $this->admin_id );
829
830
		$params = array(
831
			'author_name'  => 'Comic Book Guy',
832
			'author_email' => '[email protected]',
833
			'author_url'   => 'http://androidsdungeon.com',
834
			'author_ip'    => '867.5309',
835
			'content'      => 'Worst Comment Ever!',
836
			'status'       => 'approved',
837
		);
838
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
839
		$request->add_header( 'content-type', 'application/json' );
840
		$request->set_body( wp_json_encode( $params ) );
841
842
		$response = $this->server->dispatch( $request );
843
844
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
845
	}
846
847 View Code Duplication
	public function test_create_comment_no_post_id() {
848
		wp_set_current_user( $this->admin_id );
849
850
		$params = array(
851
			'author_name'  => 'Comic Book Guy',
852
			'author_email' => '[email protected]',
853
			'author_url'   => 'http://androidsdungeon.com',
854
			'content'      => 'Worst Comment Ever!',
855
			'status'       => 'approved',
856
		);
857
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
858
		$request->add_header( 'content-type', 'application/json' );
859
		$request->set_body( wp_json_encode( $params ) );
860
861
		$response = $this->server->dispatch( $request );
862
		$this->assertEquals( 201, $response->get_status() );
863
	}
864
865
	public function test_create_item_duplicate() {
866
		$this->markTestSkipped( 'Needs to be revisited after wp_die handling is added' );
867
		$this->factory->comment->create(
868
			array(
869
				'comment_post_ID'      => $this->post_id,
870
				'comment_author'       => 'Guy N. Cognito',
871
				'comment_author_email' => '[email protected]',
872
				'comment_content'      => 'Homer? Who is Homer? My name is Guy N. Cognito.',
873
			)
874
		);
875
		wp_set_current_user( 0 );
876
877
		$params = array(
878
			'post'    => $this->post_id,
879
			'author_name'  => 'Guy N. Cognito',
880
			'author_email' => '[email protected]',
881
			'content' => 'Homer? Who is Homer? My name is Guy N. Cognito.',
882
		);
883
884
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
885
		$request->add_header( 'content-type', 'application/json' );
886
		$request->set_body( wp_json_encode( $params ) );
887
		$response = $this->server->dispatch( $request );
888
889
		$this->assertEquals( 409, $response->get_status() );
890
	}
891
892
	public function test_create_comment_closed() {
893
		$post_id = $this->factory->post->create( array(
894
			'comment_status' => 'closed',
895
		));
896
		wp_set_current_user( 0 );
897
898
		$params = array(
899
			'post'      => $post_id,
900
		);
901
902
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
903
		$request->add_header( 'content-type', 'application/json' );
904
		$request->set_body( wp_json_encode( $params ) );
905
		$response = $this->server->dispatch( $request );
906
907
		$this->assertEquals( 403, $response->get_status() );
908
	}
909
910 View Code Duplication
	public function test_create_comment_require_login() {
911
		wp_set_current_user( 0 );
912
		update_option( 'comment_registration', 1 );
913
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
914
		$request->set_param( 'post', $this->post_id );
915
		$response = $this->server->dispatch( $request );
916
		$this->assertEquals( 401, $response->get_status() );
917
		$data = $response->get_data();
918
		$this->assertEquals( 'rest_comment_login_required', $data['code'] );
919
	}
920
921
	public function test_create_comment_two_times() {
922
923
		$this->markTestSkipped( 'Needs to be revisited after wp_die handling is added' );
924
925
		wp_set_current_user( 0 );
926
927
		$params = array(
928
			'post'    => $this->post_id,
929
			'author_name'  => 'Comic Book Guy',
930
			'author_email' => '[email protected]',
931
			'author_url'   => 'http://androidsdungeon.com',
932
			'content' => 'Worst Comment Ever!',
933
		);
934
935
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
936
		$request->add_header( 'content-type', 'application/json' );
937
		$request->set_body( wp_json_encode( $params ) );
938
939
		$response = $this->server->dispatch( $request );
940
		$this->assertEquals( 201, $response->get_status() );
941
942
		$params = array(
943
			'post'    => $this->post_id,
944
			'author_name'  => 'Comic Book Guy',
945
			'author_email' => '[email protected]',
946
			'author_url'   => 'http://androidsdungeon.com',
947
			'content'      => 'Shakes fist at sky',
948
		);
949
950
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
951
		$request->add_header( 'content-type', 'application/json' );
952
		$request->set_body( wp_json_encode( $params ) );
953
954
		$response = $this->server->dispatch( $request );
955
		$this->assertEquals( 400, $response->get_status() );
956
	}
957
958
	public function test_update_item() {
959
		$post_id = $this->factory->post->create();
960
961
		wp_set_current_user( $this->admin_id );
962
963
		$params = array(
964
			'content'      => "Disco Stu doesn't advertise.",
965
			'author'       => $this->subscriber_id,
966
			'author_name'  => 'Disco Stu',
967
			'author_url'   => 'http://stusdisco.com',
968
			'author_email' => '[email protected]',
969
			'author_ip'    => '4.4.4.4',
970
			'date'         => '2014-11-07T10:14:25',
971
			'karma'        => 100,
972
			'post'         => $post_id,
973
		);
974
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
975
		$request->add_header( 'content-type', 'application/json' );
976
		$request->set_body( wp_json_encode( $params ) );
977
978
		$response = $this->server->dispatch( $request );
979
		$this->assertEquals( 200, $response->get_status() );
980
981
		$comment = $response->get_data();
982
		$updated = get_comment( $this->approved_id );
983
		$this->assertEquals( $params['content'], $comment['content']['raw'] );
984
		$this->assertEquals( $params['author'], $comment['author'] );
985
		$this->assertEquals( $params['author_name'], $comment['author_name'] );
986
		$this->assertEquals( $params['author_url'], $comment['author_url'] );
987
		$this->assertEquals( $params['author_email'], $comment['author_email'] );
988
		$this->assertEquals( $params['author_ip'], $comment['author_ip'] );
989
		$this->assertEquals( $params['post'], $comment['post'] );
990
		$this->assertEquals( $params['karma'], $comment['karma'] );
991
992
		$this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
993
		$this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
994
	}
995
996
	public function test_update_comment_status() {
997
		wp_set_current_user( $this->admin_id );
998
999
		$comment_id = $this->factory->comment->create( array(
1000
			'comment_approved' => 0,
1001
			'comment_post_ID'  => $this->post_id,
1002
		));
1003
1004
		$params = array(
1005
			'status' => 'approve',
1006
		);
1007
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1008
		$request->add_header( 'content-type', 'application/json' );
1009
		$request->set_body( wp_json_encode( $params ) );
1010
1011
		$response = $this->server->dispatch( $request );
1012
		$this->assertEquals( 200, $response->get_status() );
1013
1014
		$comment = $response->get_data();
1015
		$updated = get_comment( $comment_id );
1016
		$this->assertEquals( 'approved', $comment['status'] );
1017
		$this->assertEquals( 1, $updated->comment_approved );
1018
	}
1019
1020
	public function test_update_comment_field_does_not_use_default_values() {
1021
		wp_set_current_user( $this->admin_id );
1022
1023
		$comment_id = $this->factory->comment->create( array(
1024
			'comment_approved' => 0,
1025
			'comment_post_ID'  => $this->post_id,
1026
			'comment_content'  => 'some content',
1027
		));
1028
1029
		$params = array(
1030
			'status' => 'approve',
1031
		);
1032
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1033
		$request->add_header( 'content-type', 'application/json' );
1034
		$request->set_body( wp_json_encode( $params ) );
1035
1036
		$response = $this->server->dispatch( $request );
1037
		$this->assertEquals( 200, $response->get_status() );
1038
1039
		$comment = $response->get_data();
1040
		$updated = get_comment( $comment_id );
1041
		$this->assertEquals( 'approved', $comment['status'] );
1042
		$this->assertEquals( 1, $updated->comment_approved );
1043
		$this->assertEquals( 'some content', $updated->comment_content );
1044
	}
1045
1046
	public function test_update_comment_date_gmt() {
1047
		wp_set_current_user( $this->admin_id );
1048
1049
		$params = array(
1050
			'date_gmt' => '2015-05-07T10:14:25',
1051
		);
1052
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1053
		$request->add_header( 'content-type', 'application/json' );
1054
		$request->set_body( wp_json_encode( $params ) );
1055
1056
		$response = $this->server->dispatch( $request );
1057
		$this->assertEquals( 200, $response->get_status() );
1058
1059
		$comment = $response->get_data();
1060
		$updated = get_comment( $this->approved_id );
1061
		$this->assertEquals( $params['date_gmt'], $comment['date_gmt'] );
1062
		$this->assertEquals( $params['date_gmt'], mysql_to_rfc3339( $updated->comment_date_gmt ) );
1063
	}
1064
1065 View Code Duplication
	public function test_update_comment_invalid_type() {
1066
		wp_set_current_user( $this->admin_id );
1067
1068
		$params = array(
1069
			'type' => 'trackback',
1070
		);
1071
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1072
		$request->add_header( 'content-type', 'application/json' );
1073
		$request->set_body( wp_json_encode( $params ) );
1074
1075
		$response = $this->server->dispatch( $request );
1076
		$this->assertErrorResponse( 'rest_comment_invalid_type', $response, 404 );
1077
	}
1078
1079 View Code Duplication
	public function test_update_item_invalid_date() {
1080
		wp_set_current_user( $this->admin_id );
1081
1082
		$params = array(
1083
			'content' => rand_str(),
1084
			'date'    => rand_str(),
1085
		);
1086
1087
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_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->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1093
	}
1094
1095 View Code Duplication
	public function test_update_item_invalid_date_gmt() {
1096
		wp_set_current_user( $this->admin_id );
1097
1098
		$params = array(
1099
			'content'  => rand_str(),
1100
			'date_gmt' => rand_str(),
1101
		);
1102
1103
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1104
		$request->add_header( 'content-type', 'application/json' );
1105
		$request->set_body( wp_json_encode( $params ) );
1106
1107
		$response = $this->server->dispatch( $request );
1108
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1109
	}
1110
1111 View Code Duplication
	public function test_update_comment_invalid_id() {
1112
		wp_set_current_user( 0 );
1113
1114
		$params = array(
1115
			'content' => 'Oh, they have the internet on computers now!',
1116
		);
1117
		$request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
1118
		$request->add_header( 'content-type', 'application/json' );
1119
		$request->set_body( wp_json_encode( $params ) );
1120
1121
		$response = $this->server->dispatch( $request );
1122
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
1123
	}
1124
1125 View Code Duplication
	public function test_update_comment_invalid_permission() {
1126
		wp_set_current_user( 0 );
1127
1128
		$params = array(
1129
			'content' => 'Disco Stu likes disco music.',
1130
		);
1131
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
1132
		$request->add_header( 'content-type', 'application/json' );
1133
		$request->set_body( wp_json_encode( $params ) );
1134
1135
		$response = $this->server->dispatch( $request );
1136
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
1137
	}
1138
1139 View Code Duplication
	public function test_delete_item() {
1140
		wp_set_current_user( $this->admin_id );
1141
1142
		$comment_id = $this->factory->comment->create( array(
1143
			'comment_approved' => 1,
1144
			'comment_post_ID'  => $this->post_id,
1145
			'user_id'          => $this->subscriber_id,
1146
		));
1147
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1148
1149
		$response = $this->server->dispatch( $request );
1150
		$this->assertEquals( 200, $response->get_status() );
1151
		$data = $response->get_data();
1152
		$this->assertEquals( $this->post_id, $data['post'] );
1153
	}
1154
1155 View Code Duplication
	public function test_delete_item_skip_trash() {
1156
		wp_set_current_user( $this->admin_id );
1157
1158
		$comment_id = $this->factory->comment->create( array(
1159
			'comment_approved' => 1,
1160
			'comment_post_ID'  => $this->post_id,
1161
			'user_id'          => $this->subscriber_id,
1162
		));
1163
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1164
		$request['force'] = true;
1165
1166
		$response = $this->server->dispatch( $request );
1167
		$this->assertEquals( 200, $response->get_status() );
1168
		$data = $response->get_data();
1169
		$this->assertEquals( $this->post_id, $data['post'] );
1170
	}
1171
1172 View Code Duplication
	public function test_delete_item_already_trashed() {
1173
		wp_set_current_user( $this->admin_id );
1174
1175
		$comment_id = $this->factory->comment->create( array(
1176
			'comment_approved' => 1,
1177
			'comment_post_ID'  => $this->post_id,
1178
			'user_id'          => $this->subscriber_id,
1179
		));
1180
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
1181
		$response = $this->server->dispatch( $request );
1182
		$this->assertEquals( 200, $response->get_status() );
1183
		$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...
1184
		$response = $this->server->dispatch( $request );
1185
		$this->assertErrorResponse( 'rest_already_trashed', $response, 410 );
1186
	}
1187
1188 View Code Duplication
	public function test_delete_comment_invalid_id() {
1189
		wp_set_current_user( $this->admin_id );
1190
1191
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
1192
1193
		$response = $this->server->dispatch( $request );
1194
		$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
1195
	}
1196
1197
	public function test_delete_comment_without_permission() {
1198
		wp_set_current_user( $this->subscriber_id );
1199
1200
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
1201
1202
		$response = $this->server->dispatch( $request );
1203
		$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
1204
	}
1205
1206 View Code Duplication
	public function test_get_item_schema() {
1207
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
1208
		$response = $this->server->dispatch( $request );
1209
		$data = $response->get_data();
1210
		$properties = $data['schema']['properties'];
1211
		$this->assertEquals( 17, count( $properties ) );
1212
		$this->assertArrayHasKey( 'id', $properties );
1213
		$this->assertArrayHasKey( 'author', $properties );
1214
		$this->assertArrayHasKey( 'author_avatar_urls', $properties );
1215
		$this->assertArrayHasKey( 'author_email', $properties );
1216
		$this->assertArrayHasKey( 'author_ip', $properties );
1217
		$this->assertArrayHasKey( 'author_name', $properties );
1218
		$this->assertArrayHasKey( 'author_url', $properties );
1219
		$this->assertArrayHasKey( 'author_user_agent', $properties );
1220
		$this->assertArrayHasKey( 'content', $properties );
1221
		$this->assertArrayHasKey( 'date', $properties );
1222
		$this->assertArrayHasKey( 'date_gmt', $properties );
1223
		$this->assertArrayHasKey( 'karma', $properties );
1224
		$this->assertArrayHasKey( 'link', $properties );
1225
		$this->assertArrayHasKey( 'parent', $properties );
1226
		$this->assertArrayHasKey( 'post', $properties );
1227
		$this->assertArrayHasKey( 'status', $properties );
1228
		$this->assertArrayHasKey( 'type', $properties );
1229
	}
1230
1231 View Code Duplication
	public function test_get_item_schema_show_avatar() {
1232
		update_option( 'show_avatars', false );
1233
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
1234
		$response = $this->server->dispatch( $request );
1235
		$data = $response->get_data();
1236
		$properties = $data['schema']['properties'];
1237
1238
		$this->assertArrayNotHasKey( 'author_avatar_urls', $properties );
1239
	}
1240
1241
	public function test_get_additional_field_registration() {
1242
1243
		$schema = array(
1244
			'type'        => 'integer',
1245
			'description' => 'Some integer of mine',
1246
			'enum'        => array( 1, 2, 3, 4 ),
1247
			'context'     => array( 'view', 'edit' ),
1248
		);
1249
1250
		register_rest_field( 'comment', 'my_custom_int', array(
1251
			'schema'          => $schema,
1252
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1253
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1254
		) );
1255
1256
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
1257
1258
		$response = $this->server->dispatch( $request );
1259
		$data = $response->get_data();
1260
1261
		$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
1262
		$this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
1263
1264
		$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $this->approved_id );
1265
1266
		$response = $this->server->dispatch( $request );
1267
		$this->assertArrayHasKey( 'my_custom_int', $response->data );
1268
1269
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments/' . $this->approved_id );
1270
		$request->set_body_params(array(
1271
			'my_custom_int' => 123,
1272
			'content' => 'abc',
1273
		));
1274
1275
		wp_set_current_user( 1 );
1276
		$this->server->dispatch( $request );
1277
		$this->assertEquals( 123, get_comment_meta( $this->approved_id, 'my_custom_int', true ) );
1278
1279
		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
1280
		$request->set_body_params(array(
1281
			'my_custom_int' => 123,
1282
			'title' => 'hello',
1283
			'post' => $this->post_id,
1284
		));
1285
1286
		$response = $this->server->dispatch( $request );
1287
1288
		$this->assertEquals( 123, $response->data['my_custom_int'] );
1289
1290
		global $wp_rest_additional_fields;
1291
		$wp_rest_additional_fields = array();
1292
	}
1293
1294
	public function additional_field_get_callback( $object ) {
1295
		return get_comment_meta( $object['id'], 'my_custom_int', true );
1296
	}
1297
1298
	public function additional_field_update_callback( $value, $comment ) {
1299
		update_comment_meta( $comment->comment_ID, 'my_custom_int', $value );
1300
	}
1301
1302
	protected function check_comment_data( $data, $context, $links ) {
1303
		$comment = get_comment( $data['id'] );
1304
1305
		$this->assertEquals( $comment->comment_ID, $data['id'] );
1306
		$this->assertEquals( $comment->comment_post_ID, $data['post'] );
1307
		$this->assertEquals( $comment->comment_parent, $data['parent'] );
1308
		$this->assertEquals( $comment->user_id, $data['author'] );
1309
		$this->assertEquals( $comment->comment_author, $data['author_name'] );
1310
		$this->assertEquals( $comment->comment_author_url, $data['author_url'] );
1311
		$this->assertEquals( wpautop( $comment->comment_content ), $data['content']['rendered'] );
1312
		$this->assertEquals( mysql_to_rfc3339( $comment->comment_date ), $data['date'] );
1313
		$this->assertEquals( mysql_to_rfc3339( $comment->comment_date_gmt ), $data['date_gmt'] );
1314
		$this->assertEquals( get_comment_link( $comment ), $data['link'] );
1315
		$this->assertContains( 'author_avatar_urls', $data );
1316
		$this->assertEqualSets( array(
1317
			'self',
1318
			'collection',
1319
			'up',
1320
		), array_keys( $links ) );
1321
1322
		if ( 'edit' === $context ) {
1323
			$this->assertEquals( $comment->comment_author_email, $data['author_email'] );
1324
			$this->assertEquals( $comment->comment_author_IP, $data['author_ip'] );
1325
			$this->assertEquals( $comment->comment_agent, $data['author_user_agent'] );
1326
			$this->assertEquals( $comment->comment_content, $data['content']['raw'] );
1327
			$this->assertEquals( $comment->comment_karma, $data['karma'] );
1328
		}
1329
1330
		if ( 'edit' !== $context ) {
1331
			$this->assertArrayNotHasKey( 'author_email', $data );
1332
			$this->assertArrayNotHasKey( 'author_ip', $data );
1333
			$this->assertArrayNotHasKey( 'author_user_agent', $data );
1334
			$this->assertArrayNotHasKey( 'raw', $data['content'] );
1335
			$this->assertArrayNotHasKey( 'karma', $data );
1336
		}
1337
	}
1338
}
1339