wpSetUpBeforeClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 45
nc 1
nop 1
dl 0
loc 71
rs 9.1369
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Unit tests covering WP_REST_Comments_Controller functionality.
4
 *
5
 * @package    WordPress
6
 * @subpackage REST API
7
 */
8
9
/**
10
 * @group restapi
11
 */
12
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
13
{
14
    protected static $superadmin_id;
15
    protected static $admin_id;
16
    protected static $editor_id;
17
    protected static $subscriber_id;
18
    protected static $author_id;
19
20
    protected static $post_id;
21
    protected static $password_id;
22
    protected static $private_id;
23
    protected static $draft_id;
24
    protected static $trash_id;
25
    protected static $approved_id;
26
    protected static $hold_id;
27
28
    protected $endpoint;
29
30
    public static function wpSetUpBeforeClass( $factory ) 
0 ignored issues
show
Coding Style introduced by
The function name wpSetUpBeforeClass is in camel caps, but expected wp_set_up_before_class instead as per the coding standard.
Loading history...
31
    {
32
        self::$superadmin_id = $factory->user->create(
33
            array(
34
            'role'       => 'administrator',
35
            'user_login' => 'superadmin',
36
            ) 
37
        );
38
        self::$admin_id = $factory->user->create(
39
            array(
40
            'role' => 'administrator',
41
            ) 
42
        );
43
        self::$editor_id = $factory->user->create(
44
            array(
45
            'role' => 'editor',
46
            ) 
47
        );
48
        self::$subscriber_id = $factory->user->create(
49
            array(
50
            'role' => 'subscriber',
51
            ) 
52
        );
53
        self::$author_id = $factory->user->create(
54
            array(
55
            'role'         => 'author',
56
            'display_name' => 'Sea Captain',
57
            'first_name'   => 'Horatio',
58
            'last_name'    => 'McCallister',
59
            'user_email'   => '[email protected]',
60
            'user_url'     => 'http://thefryingdutchman.com',
61
            ) 
62
        );
63
64
        self::$post_id = $factory->post->create();
65
        self::$private_id = $factory->post->create(
66
            array(
67
            'post_status' => 'private',
68
            ) 
69
        );
70
        self::$password_id = $factory->post->create(
71
            array(
72
            'post_password'    => 'toomanysecrets',
73
            ) 
74
        );
75
        self::$draft_id = $factory->post->create(
76
            array(
77
            'post_status' => 'draft',
78
            ) 
79
        );
80
        self::$trash_id = $factory->post->create(
81
            array(
82
            'post_status' => 'trash',
83
            ) 
84
        );
85
86
        self::$approved_id = $factory->comment->create(
87
            array(
88
            'comment_approved' => 1,
89
            'comment_post_ID'  => self::$post_id,
90
            'user_id'          => 0,
91
            ) 
92
        );
93
        self::$hold_id = $factory->comment->create(
94
            array(
95
            'comment_approved' => 0,
96
            'comment_post_ID'  => self::$post_id,
97
            'user_id'          => self::$subscriber_id,
98
            ) 
99
        );
100
    }
101
102
    public static function wpTearDownAfterClass() 
0 ignored issues
show
Coding Style introduced by
The function name wpTearDownAfterClass is in camel caps, but expected wp_tear_down_after_class instead as per the coding standard.
Loading history...
103
    {
104
        self::delete_user(self::$admin_id);
105
        self::delete_user(self::$subscriber_id);
106
        self::delete_user(self::$author_id);
107
108
        wp_delete_post(self::$post_id, true);
109
        wp_delete_post(self::$private_id, true);
110
        wp_delete_post(self::$password_id, true);
111
        wp_delete_post(self::$draft_id, true);
112
        wp_delete_post(self::$trash_id, true);
113
        wp_delete_post(self::$approved_id, true);
114
        wp_delete_post(self::$hold_id, true);
115
    }
116
117
    public function setUp() 
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
118
    {
119
        parent::setUp();
120
        $this->endpoint = new WP_REST_Comments_Controller;
121
        if (is_multisite() ) {
122
            update_site_option('site_admins', array( 'superadmin' ));
123
        }
124
    }
125
126 View Code Duplication
    public function test_register_routes() 
127
    {
128
        $routes = $this->server->get_routes();
129
130
        $this->assertArrayHasKey('/wp/v2/comments', $routes);
131
        $this->assertCount(2, $routes['/wp/v2/comments']);
132
        $this->assertArrayHasKey('/wp/v2/comments/(?P<id>[\d]+)', $routes);
133
        $this->assertCount(3, $routes['/wp/v2/comments/(?P<id>[\d]+)']);
134
    }
135
136 View Code Duplication
    public function test_context_param() 
137
    {
138
        // Collection
139
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/comments');
140
        $response = $this->server->dispatch($request);
141
        $data = $response->get_data();
142
        $this->assertEquals('view', $data['endpoints'][0]['args']['context']['default']);
143
        $this->assertEquals(array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum']);
144
        // Single
145
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/comments/' . self::$approved_id);
146
        $response = $this->server->dispatch($request);
147
        $data = $response->get_data();
148
        $this->assertEquals('view', $data['endpoints'][0]['args']['context']['default']);
149
        $this->assertEquals(array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum']);
150
    }
151
152 View Code Duplication
    public function test_registered_query_params() 
153
    {
154
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/comments');
155
        $response = $this->server->dispatch($request);
156
        $data = $response->get_data();
157
        $keys = array_keys($data['endpoints'][0]['args']);
158
        sort($keys);
159
        $this->assertEquals(
160
            array(
161
            'after',
162
            'author',
163
            'author_email',
164
            'author_exclude',
165
            'before',
166
            'context',
167
            'exclude',
168
            'include',
169
            'offset',
170
            'order',
171
            'orderby',
172
            'page',
173
            'parent',
174
            'parent_exclude',
175
            'password',
176
            'per_page',
177
            'post',
178
            'search',
179
            'status',
180
            'type',
181
            ), $keys 
182
        );
183
    }
184
185
    public function test_get_items() 
186
    {
187
        $this->factory->comment->create_post_comments(self::$post_id, 6);
188
189
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
190
191
        $response = $this->server->dispatch($request);
192
        $this->assertEquals(200, $response->get_status());
193
194
        $comments = $response->get_data();
195
        // We created 6 comments in this method, plus self::$approved_id.
196
        $this->assertCount(7, $comments);
197
    }
198
199
    /**
200
     * @ticket 38692
201
     */
202
    public function test_get_items_with_password() 
203
    {
204
        wp_set_current_user(0);
205
206
        $args = array(
207
         'comment_approved' => 1,
208
         'comment_post_ID'  => self::$password_id,
209
        );
210
        $password_comment = $this->factory->comment->create($args);
211
212
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
213
        $request->set_param('password', 'toomanysecrets');
214
        $request->set_param('post', self::$password_id);
215
216
        $response = $this->server->dispatch($request);
217
        $this->assertEquals(200, $response->get_status());
218
219
        $collection_data = $response->get_data();
220
        $this->assertTrue(in_array($password_comment, wp_list_pluck($collection_data, 'id'), true));
221
    }
222
223
    /**
224
     * @ticket 38692
225
     */
226
    public function test_get_items_with_password_without_post() 
227
    {
228
        wp_set_current_user(0);
229
        $args = array(
230
         'comment_approved' => 1,
231
         'comment_post_ID'  => self::$password_id,
232
        );
233
        $password_comment = $this->factory->comment->create($args);
234
235
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
236
        $request->set_param('password', 'toomanysecrets');
237
238
        $response = $this->server->dispatch($request);
239
        $this->assertEquals(200, $response->get_status());
240
241
        $collection_data = $response->get_data();
242
        $this->assertFalse(in_array($password_comment, wp_list_pluck($collection_data, 'id'), true));
243
    }
244
245
    /**
246
     * @ticket 38692
247
     */
248
    public function test_get_items_with_password_with_multiple_post() 
249
    {
250
        wp_set_current_user(0);
251
        $args = array(
252
         'comment_approved' => 1,
253
         'comment_post_ID'  => self::$password_id,
254
        );
255
        $password_comment = $this->factory->comment->create($args);
256
257
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
258
        $request->set_param('password', 'toomanysecrets');
259
        $request->set_param('post', array( self::$password_id, self::$post_id ));
260
261
        $response = $this->server->dispatch($request);
262
        $this->assertErrorResponse('rest_cannot_read_post', $response, 401);
263
    }
264
265
    public function test_get_password_items_without_edit_post_permission() 
266
    {
267
        wp_set_current_user(0);
268
269
        $args = array(
270
         'comment_approved' => 1,
271
         'comment_post_ID'  => self::$password_id,
272
        );
273
        $password_comment = $this->factory->comment->create($args);
274
275
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
276
277
        $response = $this->server->dispatch($request);
278
        $this->assertEquals(200, $response->get_status());
279
280
        $collection_data = $response->get_data();
281
        $this->assertFalse(in_array($password_comment, wp_list_pluck($collection_data, 'id'), true));
282
    }
283
284
    public function test_get_password_items_with_edit_post_permission() 
285
    {
286
        wp_set_current_user(self::$admin_id);
287
288
        $args = array(
289
         'comment_approved' => 1,
290
         'comment_post_ID'  => self::$password_id,
291
        );
292
        $password_comment = $this->factory->comment->create($args);
293
294
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
295
296
        $response = $this->server->dispatch($request);
297
        $this->assertEquals(200, $response->get_status());
298
299
        $collection_data = $response->get_data();
300
        $this->assertTrue(in_array($password_comment, wp_list_pluck($collection_data, 'id'), true));
301
    }
302
303
    public function test_get_items_without_private_post_permission() 
304
    {
305
        wp_set_current_user(0);
306
307
        $args = array(
308
         'comment_approved' => 1,
309
         'comment_post_ID'  => self::$private_id,
310
        );
311
        $private_comment = $this->factory->comment->create($args);
312
313
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
314
315
        $response = $this->server->dispatch($request);
316
        $this->assertEquals(200, $response->get_status());
317
318
        $collection_data = $response->get_data();
319
        $this->assertFalse(in_array($private_comment, wp_list_pluck($collection_data, 'id'), true));
320
    }
321
322
    public function test_get_items_with_private_post_permission() 
323
    {
324
        wp_set_current_user(self::$admin_id);
325
326
        $args = array(
327
         'comment_approved' => 1,
328
         'comment_post_ID'  => self::$private_id,
329
        );
330
        $private_comment = $this->factory->comment->create($args);
331
332
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
333
334
        $response = $this->server->dispatch($request);
335
        $this->assertEquals(200, $response->get_status());
336
337
        $collection_data = $response->get_data();
338
        $this->assertTrue(in_array($private_comment, wp_list_pluck($collection_data, 'id'), true));
339
    }
340
341
    public function test_get_items_with_invalid_post() 
342
    {
343
        wp_set_current_user(0);
344
345
        $comment_id = $this->factory->comment->create(
346
            array(
347
            'comment_approved' => 1,
348
            'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
349
            )
350
        );
351
352
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
353
354
        $response = $this->server->dispatch($request);
355
        $this->assertEquals(200, $response->get_status());
356
357
        $collection_data = $response->get_data();
358
        $this->assertFalse(in_array($comment_id, wp_list_pluck($collection_data, 'id'), true));
359
360
        wp_delete_comment($comment_id);
361
    }
362
363
    public function test_get_items_with_invalid_post_permission() 
364
    {
365
        wp_set_current_user(self::$admin_id);
366
367
        $comment_id = $this->factory->comment->create(
368
            array(
369
            'comment_approved' => 1,
370
            'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
371
            )
372
        );
373
374
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
375
376
        $response = $this->server->dispatch($request);
377
        $this->assertEquals(200, $response->get_status());
378
379
        $collection_data = $response->get_data();
380
        $this->assertTrue(in_array($comment_id, wp_list_pluck($collection_data, 'id'), true));
381
382
        wp_delete_comment($comment_id);
383
    }
384
385 View Code Duplication
    public function test_get_items_no_permission_for_context() 
386
    {
387
        wp_set_current_user(0);
388
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
389
        $request->set_param('context', 'edit');
390
        $response = $this->server->dispatch($request);
391
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
392
    }
393
394
    public function test_get_items_no_post() 
395
    {
396
        $this->factory->comment->create_post_comments(0, 2);
397
        wp_set_current_user(self::$admin_id);
398
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
399
        $request->set_param('post', 0);
400
        $response = $this->server->dispatch($request);
401
        $this->assertEquals(200, $response->get_status());
402
        $comments = $response->get_data();
403
        $this->assertCount(2, $comments);
404
    }
405
406 View Code Duplication
    public function test_get_items_no_permission_for_no_post() 
407
    {
408
        wp_set_current_user(0);
409
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
410
        $request->set_param('post', 0);
411
        $response = $this->server->dispatch($request);
412
        $this->assertErrorResponse('rest_cannot_read', $response, 401);
413
    }
414
415
    public function test_get_items_edit_context() 
416
    {
417
        wp_set_current_user(self::$admin_id);
418
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
419
        $request->set_param('context', 'edit');
420
        $response = $this->server->dispatch($request);
421
        $this->assertEquals(200, $response->get_status());
422
    }
423
424 View Code Duplication
    public function test_get_items_for_post() 
425
    {
426
        $second_post_id = $this->factory->post->create();
427
        $this->factory->comment->create_post_comments($second_post_id, 2);
428
429
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
430
        $request->set_query_params(
431
            array(
432
            'post' => $second_post_id,
433
            ) 
434
        );
435
436
        $response = $this->server->dispatch($request);
437
        $this->assertEquals(200, $response->get_status());
438
439
        $comments = $response->get_data();
440
        $this->assertCount(2, $comments);
441
    }
442
443
    public function test_get_items_include_query() 
444
    {
445
        wp_set_current_user(self::$admin_id);
446
        $args = array(
447
         'comment_approved' => 1,
448
         'comment_post_ID'  => self::$post_id,
449
        );
450
        $id1 = $this->factory->comment->create($args);
451
        $this->factory->comment->create($args);
452
        $id3 = $this->factory->comment->create($args);
453
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
454
        // Order=>asc
455
        $request->set_param('order', 'asc');
456
        $request->set_param('include', array( $id3, $id1 ));
457
        $response = $this->server->dispatch($request);
458
        $data = $response->get_data();
459
        $this->assertEquals(2, count($data));
460
        $this->assertEquals($id1, $data[0]['id']);
461
        // Orderby=>include
462
        $request->set_param('orderby', 'include');
463
        $response = $this->server->dispatch($request);
464
        $data = $response->get_data();
465
        $this->assertEquals(2, count($data));
466
        $this->assertEquals($id3, $data[0]['id']);
467
        // Orderby=>invalid should fail.
468
        $request->set_param('orderby', 'invalid');
469
        $response = $this->server->dispatch($request);
470
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
471
        // fails on invalid id.
472
        $request->set_param('orderby', array( 'include' ));
473
        $request->set_param('include', array( 'invalid' ));
474
        $response = $this->server->dispatch($request);
475
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
476
    }
477
478
    public function test_get_items_exclude_query() 
479
    {
480
        wp_set_current_user(self::$admin_id);
481
        $args = array(
482
         'comment_approved' => 1,
483
         'comment_post_ID'  => self::$post_id,
484
        );
485
        $id1 = $this->factory->comment->create($args);
486
        $id2 = $this->factory->comment->create($args);
487
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
488
        $response = $this->server->dispatch($request);
489
        $data = $response->get_data();
490
        $this->assertTrue(in_array($id1, wp_list_pluck($data, 'id'), true));
491
        $this->assertTrue(in_array($id2, wp_list_pluck($data, 'id'), true));
492
        $request->set_param('exclude', array( $id2 ));
493
        $response = $this->server->dispatch($request);
494
        $data = $response->get_data();
495
        $this->assertTrue(in_array($id1, wp_list_pluck($data, 'id'), true));
496
        $this->assertFalse(in_array($id2, wp_list_pluck($data, 'id'), true));
497
498
        // fails on invalid id.
499
        $request->set_param('exclude', array( 'invalid' ));
500
        $response = $this->server->dispatch($request);
501
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
502
    }
503
504
    public function test_get_items_offset_query() 
505
    {
506
        wp_set_current_user(self::$admin_id);
507
        $args = array(
508
         'comment_approved' => 1,
509
         'comment_post_ID'  => self::$post_id,
510
        );
511
        $this->factory->comment->create($args);
512
        $this->factory->comment->create($args);
513
        $this->factory->comment->create($args);
514
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
515
        $request->set_param('offset', 1);
516
        $response = $this->server->dispatch($request);
517
        $this->assertCount(3, $response->get_data());
518
        // 'offset' works with 'per_page'
519
        $request->set_param('per_page', 2);
520
        $response = $this->server->dispatch($request);
521
        $this->assertCount(2, $response->get_data());
522
        // 'offset' takes priority over 'page'
523
        $request->set_param('page', 3);
524
        $response = $this->server->dispatch($request);
525
        $this->assertCount(2, $response->get_data());
526
        // 'offset' with invalid value errors.
527
        $request->set_param('offset', 'moreplease');
528
        $response = $this->server->dispatch($request);
529
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
530
    }
531
532
    public function test_get_items_order_query() 
533
    {
534
        wp_set_current_user(self::$admin_id);
535
        $args = array(
536
         'comment_approved' => 1,
537
         'comment_post_ID'  => self::$post_id,
538
        );
539
        $this->factory->comment->create($args);
540
        $this->factory->comment->create($args);
541
        $id3 = $this->factory->comment->create($args);
542
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
543
        // order defaults to 'desc'
544
        $response = $this->server->dispatch($request);
545
        $data = $response->get_data();
546
        $this->assertEquals($id3, $data[0]['id']);
547
        // order=>asc
548
        $request->set_param('order', 'asc');
549
        $response = $this->server->dispatch($request);
550
        $data = $response->get_data();
551
        $this->assertEquals(self::$approved_id, $data[0]['id']);
552
        // order=>asc,id should fail
553
        $request->set_param('order', 'asc,id');
554
        $response = $this->server->dispatch($request);
555
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
556
    }
557
558
    public function test_get_items_private_post_no_permissions() 
559
    {
560
        wp_set_current_user(0);
561
        $post_id = $this->factory->post->create(array( 'post_status' => 'private' ));
562
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
563
        $request->set_param('post', $post_id);
564
        $response = $this->server->dispatch($request);
565
        $this->assertErrorResponse('rest_cannot_read_post', $response, 401);
566
    }
567
568
    public function test_get_items_author_arg() 
569
    {
570
        // Authorized
571
        wp_set_current_user(self::$admin_id);
572
        $args = array(
573
         'comment_approved' => 1,
574
         'comment_post_ID'  => self::$post_id,
575
         'user_id'          => self::$author_id,
576
        );
577
        $this->factory->comment->create($args);
578
        $args['user_id'] = self::$subscriber_id;
579
        $this->factory->comment->create($args);
580
        unset($args['user_id']);
581
        $this->factory->comment->create($args);
582
583
        // 'author' limits result to 1 of 3
584
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
585
        $request->set_param('author', self::$author_id);
586
        $response = $this->server->dispatch($request);
587
        $this->assertEquals(200, $response->get_status());
588
        $comments = $response->get_data();
589
        $this->assertCount(1, $comments);
590
        // Multiple authors are supported
591
        $request->set_param('author', array( self::$author_id, self::$subscriber_id ));
592
        $response = $this->server->dispatch($request);
593
        $this->assertEquals(200, $response->get_status());
594
        $comments = $response->get_data();
595
        $this->assertCount(2, $comments);
596
        // Invalid author param errors
597
        $request->set_param('author', 'skippy');
598
        $response = $this->server->dispatch($request);
599
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
600
        // Unavailable to unauthenticated; defaults to error
601
        wp_set_current_user(0);
602
        $request->set_param('author', array( self::$author_id, self::$subscriber_id ));
603
        $response = $this->server->dispatch($request);
604
        $this->assertErrorResponse('rest_forbidden_param', $response, 401);
605
    }
606
607
    public function test_get_items_author_exclude_arg() 
608
    {
609
        // Authorized
610
        wp_set_current_user(self::$admin_id);
611
        $args = array(
612
         'comment_approved' => 1,
613
         'comment_post_ID'  => self::$post_id,
614
         'user_id'          => self::$author_id,
615
        );
616
        $this->factory->comment->create($args);
617
        $args['user_id'] = self::$subscriber_id;
618
        $this->factory->comment->create($args);
619
        unset($args['user_id']);
620
        $this->factory->comment->create($args);
621
622
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
623
        $response = $this->server->dispatch($request);
624
        $comments = $response->get_data();
625
        $this->assertCount(4, $comments);
626
627
        // 'author_exclude' limits result to 3 of 4
628
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
629
        $request->set_param('author_exclude', self::$author_id);
630
        $response = $this->server->dispatch($request);
631
        $this->assertEquals(200, $response->get_status());
632
        $comments = $response->get_data();
633
        $this->assertCount(3, $comments);
634
        // 'author_exclude' for both comment authors (2 of 4)
635
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
636
        $request->set_param('author_exclude', array( self::$author_id, self::$subscriber_id ));
637
        $response = $this->server->dispatch($request);
638
        $this->assertEquals(200, $response->get_status());
639
        $comments = $response->get_data();
640
        $this->assertCount(2, $comments);
641
        // 'author_exclude' for both invalid author
642
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
643
        $request->set_param('author_exclude', 'skippy');
644
        $response = $this->server->dispatch($request);
645
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
646
        // Unavailable to unauthenticated; defaults to error
647
        wp_set_current_user(0);
648
        $request->set_param('author_exclude', array( self::$author_id, self::$subscriber_id ));
649
        $response = $this->server->dispatch($request);
650
        $this->assertErrorResponse('rest_forbidden_param', $response, 401);
651
    }
652
653 View Code Duplication
    public function test_get_items_parent_arg() 
654
    {
655
        $args = array(
656
         'comment_approved'  => 1,
657
         'comment_post_ID'   => self::$post_id,
658
        );
659
        $parent_id = $this->factory->comment->create($args);
660
        $parent_id2 = $this->factory->comment->create($args);
661
        $args['comment_parent'] = $parent_id;
662
        $this->factory->comment->create($args);
663
        $args['comment_parent'] = $parent_id2;
664
        $this->factory->comment->create($args);
665
        // All comments in the database
666
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
667
        $response = $this->server->dispatch($request);
668
        $this->assertCount(5, $response->get_data());
669
        // Limit to the parent
670
        $request->set_param('parent', $parent_id);
671
        $response = $this->server->dispatch($request);
672
        $this->assertCount(1, $response->get_data());
673
        // Limit to two parents
674
        $request->set_param('parent', array( $parent_id, $parent_id2 ));
675
        $response = $this->server->dispatch($request);
676
        $this->assertCount(2, $response->get_data());
677
        // Invalid parent should error
678
        $request->set_param('parent', 'invalid');
679
        $response = $this->server->dispatch($request);
680
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
681
    }
682
683 View Code Duplication
    public function test_get_items_parent_exclude_arg() 
684
    {
685
        $args = array(
686
         'comment_approved'  => 1,
687
         'comment_post_ID'   => self::$post_id,
688
        );
689
        $parent_id = $this->factory->comment->create($args);
690
        $parent_id2 = $this->factory->comment->create($args);
691
        $args['comment_parent'] = $parent_id;
692
        $this->factory->comment->create($args);
693
        $args['comment_parent'] = $parent_id2;
694
        $this->factory->comment->create($args);
695
        // All comments in the database
696
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
697
        $response = $this->server->dispatch($request);
698
        $this->assertCount(5, $response->get_data());
699
        // Exclude this particular parent
700
        $request->set_param('parent_exclude', $parent_id);
701
        $response = $this->server->dispatch($request);
702
        $this->assertCount(4, $response->get_data());
703
        // Exclude both comment parents
704
        $request->set_param('parent_exclude', array( $parent_id, $parent_id2 ));
705
        $response = $this->server->dispatch($request);
706
        $this->assertCount(3, $response->get_data());
707
        // Invalid parent id should error
708
        $request->set_param('parent_exclude', 'invalid');
709
        $response = $this->server->dispatch($request);
710
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
711
    }
712
713
    public function test_get_items_search_query() 
714
    {
715
        wp_set_current_user(self::$admin_id);
716
        $args = array(
717
         'comment_approved' => 1,
718
         'comment_post_ID'  => self::$post_id,
719
         'comment_content'  => 'foo',
720
         'comment_author'   => 'Homer J Simpson',
721
        );
722
        $id1 = $this->factory->comment->create($args);
723
        $args['comment_content'] = 'bar';
724
        $this->factory->comment->create($args);
725
        $args['comment_content'] = 'burrito';
726
        $this->factory->comment->create($args);
727
        // 3 comments, plus 1 created in construct
728
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
729
        $response = $this->server->dispatch($request);
730
        $this->assertCount(4, $response->get_data());
731
        // One matching comments
732
        $request->set_param('search', 'foo');
733
        $response = $this->server->dispatch($request);
734
        $data = $response->get_data();
735
        $this->assertCount(1, $data);
736
        $this->assertEquals($id1, $data[0]['id']);
737
    }
738
739
    public function test_get_comments_pagination_headers() 
740
    {
741
        wp_set_current_user(self::$admin_id);
742
        // Start of the index
743
        for ( $i = 0; $i < 49; $i++ ) {
744
            $this->factory->comment->create(
745
                array(
746
                'comment_content'   => "Comment {$i}",
747
                'comment_post_ID'   => self::$post_id,
748
                ) 
749
            );
750
        }
751
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
752
        $response = $this->server->dispatch($request);
753
        $headers = $response->get_headers();
754
        $this->assertEquals(50, $headers['X-WP-Total']);
755
        $this->assertEquals(5, $headers['X-WP-TotalPages']);
756
        $next_link = add_query_arg(
757
            array(
758
            'page'    => 2,
759
            ), rest_url('/wp/v2/comments') 
760
        );
761
        $this->assertFalse(stripos($headers['Link'], 'rel="prev"'));
762
        $this->assertContains('<' . $next_link . '>; rel="next"', $headers['Link']);
763
        // 3rd page
764
        $this->factory->comment->create(
765
            array(
766
            'comment_content'   => 'Comment 51',
767
            'comment_post_ID'   => self::$post_id,
768
            ) 
769
        );
770
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
771
        $request->set_param('page', 3);
772
        $response = $this->server->dispatch($request);
773
        $headers = $response->get_headers();
774
        $this->assertEquals(51, $headers['X-WP-Total']);
775
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
776
        $prev_link = add_query_arg(
777
            array(
778
            'page'    => 2,
779
            ), rest_url('/wp/v2/comments') 
780
        );
781
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
782
        $next_link = add_query_arg(
783
            array(
784
            'page'    => 4,
785
            ), rest_url('/wp/v2/comments') 
786
        );
787
        $this->assertContains('<' . $next_link . '>; rel="next"', $headers['Link']);
788
        // Last page
789
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
790
        $request->set_param('page', 6);
791
        $response = $this->server->dispatch($request);
792
        $headers = $response->get_headers();
793
        $this->assertEquals(51, $headers['X-WP-Total']);
794
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
795
        $prev_link = add_query_arg(
796
            array(
797
            'page'    => 5,
798
            ), rest_url('/wp/v2/comments') 
799
        );
800
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
801
        $this->assertFalse(stripos($headers['Link'], 'rel="next"'));
802
        // Out of bounds
803
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
804
        $request->set_param('page', 8);
805
        $response = $this->server->dispatch($request);
806
        $headers = $response->get_headers();
807
        $this->assertEquals(51, $headers['X-WP-Total']);
808
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
809
        $prev_link = add_query_arg(
810
            array(
811
            'page'    => 6,
812
            ), rest_url('/wp/v2/comments') 
813
        );
814
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
815
        $this->assertFalse(stripos($headers['Link'], 'rel="next"'));
816
    }
817
818
    public function test_get_comments_invalid_date() 
819
    {
820
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
821
        $request->set_param('after', rand_str());
822
        $request->set_param('before', rand_str());
823
        $response = $this->server->dispatch($request);
824
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
825
    }
826
827
    public function test_get_comments_valid_date() 
828
    {
829
        $comment1 = $this->factory->comment->create(
830
            array(
831
            'comment_date'    => '2016-01-15T00:00:00Z',
832
            'comment_post_ID' => self::$post_id,
833
            ) 
834
        );
835
        $comment2 = $this->factory->comment->create(
836
            array(
837
            'comment_date'    => '2016-01-16T00:00:00Z',
838
            'comment_post_ID' => self::$post_id,
839
            ) 
840
        );
841
        $comment3 = $this->factory->comment->create(
842
            array(
843
            'comment_date'    => '2016-01-17T00:00:00Z',
844
            'comment_post_ID' => self::$post_id,
845
            ) 
846
        );
847
848
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
849
        $request->set_param('after', '2016-01-15T00:00:00Z');
850
        $request->set_param('before', '2016-01-17T00:00:00Z');
851
        $response = $this->server->dispatch($request);
852
        $data = $response->get_data();
853
        $this->assertCount(1, $data);
854
        $this->assertEquals($comment2, $data[0]['id']);
855
    }
856
857
    public function test_get_item() 
858
    {
859
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%d', self::$approved_id));
860
861
        $response = $this->server->dispatch($request);
862
        $this->assertEquals(200, $response->get_status());
863
864
        $data = $response->get_data();
865
        $this->check_comment_data($data, 'view', $response->get_links());
866
    }
867
868
    public function test_prepare_item() 
869
    {
870
        wp_set_current_user(self::$admin_id);
871
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%d', self::$approved_id));
872
        $request->set_query_params(
873
            array(
874
            'context' => 'edit',
875
            ) 
876
        );
877
878
        $response = $this->server->dispatch($request);
879
        $this->assertEquals(200, $response->get_status());
880
881
        $data = $response->get_data();
882
        $this->check_comment_data($data, 'edit', $response->get_links());
883
    }
884
885 View Code Duplication
    public function test_get_comment_author_avatar_urls() 
886
    {
887
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%d', self::$approved_id));
888
889
        $response = $this->server->dispatch($request);
890
891
        $data = $response->get_data();
892
        $this->assertArrayHasKey(24,  $data['author_avatar_urls']);
893
        $this->assertArrayHasKey(48,  $data['author_avatar_urls']);
894
        $this->assertArrayHasKey(96,  $data['author_avatar_urls']);
895
896
        $comment = get_comment(self::$approved_id);
897
        /**
898
         * Ignore the subdomain, since 'get_avatar_url randomly sets the Gravatar
899
         * server when building the url string.
900
         */
901
        $this->assertEquals(substr(get_avatar_url($comment->comment_author_email), 9), substr($data['author_avatar_urls'][96], 9));
902
    }
903
904 View Code Duplication
    public function test_get_comment_invalid_id() 
905
    {
906
        $request = new WP_REST_Request('GET', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER);
907
908
        $response = $this->server->dispatch($request);
909
        $this->assertErrorResponse('rest_comment_invalid_id', $response, 404);
910
    }
911
912 View Code Duplication
    public function test_get_comment_invalid_context() 
913
    {
914
        wp_set_current_user(0);
915
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%s', self::$approved_id));
916
        $request->set_param('context', 'edit');
917
        $response = $this->server->dispatch($request);
918
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
919
    }
920
921
    public function test_get_comment_invalid_post_id() 
922
    {
923
        wp_set_current_user(0);
924
        $comment_id = $this->factory->comment->create(
925
            array(
926
            'comment_approved' => 1,
927
            'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
928
            )
929
        );
930
        $request = new WP_REST_Request('GET', '/wp/v2/comments/' . $comment_id);
931
932
        $response = $this->server->dispatch($request);
933
        $this->assertErrorResponse('rest_cannot_read', $response, 401);
934
    }
935
936
    public function test_get_comment_invalid_post_id_as_admin() 
937
    {
938
        wp_set_current_user(self::$admin_id);
939
        $comment_id = $this->factory->comment->create(
940
            array(
941
            'comment_approved' => 1,
942
            'comment_post_ID'  => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
943
            )
944
        );
945
        $request = new WP_REST_Request('GET', '/wp/v2/comments/' . $comment_id);
946
947
        $response = $this->server->dispatch($request);
948
        $this->assertErrorResponse('rest_post_invalid_id', $response, 404);
949
    }
950
951 View Code Duplication
    public function test_get_comment_not_approved() 
952
    {
953
        wp_set_current_user(0);
954
955
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%d', self::$hold_id));
956
957
        $response = $this->server->dispatch($request);
958
        $this->assertErrorResponse('rest_cannot_read', $response, 401);
959
    }
960
961 View Code Duplication
    public function test_get_comment_not_approved_same_user() 
962
    {
963
        wp_set_current_user(self::$admin_id);
964
965
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%d', self::$hold_id));
966
967
        $response = $this->server->dispatch($request);
968
        $this->assertEquals(200, $response->get_status());
969
    }
970
971
    public function test_get_comment_with_children_link() 
972
    {
973
        $comment_id_1 = $this->factory->comment->create(
974
            array(
975
            'comment_approved' => 1,
976
            'comment_post_ID'  => self::$post_id,
977
            'user_id'          => self::$subscriber_id,
978
            ) 
979
        );
980
981
        $child_comment = $this->factory->comment->create(
982
            array(
983
            'comment_approved' => 1,
984
            'comment_parent'   => $comment_id_1,
985
            'comment_post_ID'  => self::$post_id,
986
            'user_id'          => self::$subscriber_id,
987
            ) 
988
        );
989
990
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%s', $comment_id_1));
991
        $response = $this->server->dispatch($request);
992
        $this->assertEquals(200, $response->get_status());
993
        $this->assertArrayHasKey('children', $response->get_links());
994
    }
995
996
    public function test_get_comment_without_children_link() 
997
    {
998
        $comment_id_1 = $this->factory->comment->create(
999
            array(
1000
            'comment_approved' => 1,
1001
            'comment_post_ID'  => self::$post_id,
1002
            'user_id'          => self::$subscriber_id,
1003
            ) 
1004
        );
1005
1006
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%s', $comment_id_1));
1007
        $response = $this->server->dispatch($request);
1008
        $this->assertEquals(200, $response->get_status());
1009
        $this->assertArrayNotHasKey('children', $response->get_links());
1010
    }
1011
1012
    public function test_get_comment_with_password_without_edit_post_permission() 
1013
    {
1014
        wp_set_current_user(self::$subscriber_id);
1015
        $args = array(
1016
         'comment_approved' => 1,
1017
         'comment_post_ID'  => self::$password_id,
1018
        );
1019
        $password_comment = $this->factory->comment->create($args);
1020
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%s', $password_comment));
1021
        $response = $this->server->dispatch($request);
1022
        $this->assertErrorResponse('rest_cannot_read', $response, 403);
1023
    }
1024
1025
    /**
1026
     * @ticket 38692
1027
     */
1028
    public function test_get_comment_with_password_with_valid_password() 
1029
    {
1030
        wp_set_current_user(self::$subscriber_id);
1031
1032
        $args = array(
1033
         'comment_approved' => 1,
1034
         'comment_post_ID'  => self::$password_id,
1035
        );
1036
        $password_comment = $this->factory->comment->create($args);
1037
1038
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%s', $password_comment));
1039
        $request->set_param('password', 'toomanysecrets');
1040
1041
        $response = $this->server->dispatch($request);
1042
        $this->assertEquals(200, $response->get_status());
1043
    }
1044
1045 View Code Duplication
    public function test_create_item() 
1046
    {
1047
        wp_set_current_user(self::$admin_id);
1048
1049
        $params = array(
1050
         'post'    => self::$post_id,
1051
         'author_name'  => 'Comic Book Guy',
1052
         'author_email' => '[email protected]',
1053
         'author_url'   => 'http://androidsdungeon.com',
1054
         'content' => 'Worst Comment Ever!',
1055
         'date'    => '2014-11-07T10:14:25',
1056
        );
1057
1058
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1059
        $request->add_header('content-type', 'application/json');
1060
        $request->set_body(wp_json_encode($params));
1061
1062
        $response = $this->server->dispatch($request);
1063
        $this->assertEquals(201, $response->get_status());
1064
1065
        $data = $response->get_data();
1066
        $this->check_comment_data($data, 'edit', $response->get_links());
1067
        $this->assertEquals('hold', $data['status']);
1068
        $this->assertEquals('2014-11-07T10:14:25', $data['date']);
1069
        $this->assertEquals(self::$post_id, $data['post']);
1070
    }
1071
1072
    public function test_create_item_using_accepted_content_raw_value() 
1073
    {
1074
        wp_set_current_user(self::$admin_id);
1075
1076
        $params = array(
1077
         'post'         => self::$post_id,
1078
         'author_name'  => 'Reverend Lovejoy',
1079
         'author_email' => '[email protected]',
1080
         'author_url'   => 'http://timothylovejoy.jr',
1081
         'content'      => array(
1082
          'raw' => 'Once something has been approved by the government, it\'s no longer immoral.',
1083
         ),
1084
        );
1085
1086
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1087
        $request->add_header('content-type', 'application/json');
1088
        $request->set_body(wp_json_encode($params));
1089
1090
        $response = $this->server->dispatch($request);
1091
        $this->assertEquals(201, $response->get_status());
1092
1093
        $data = $response->get_data();
1094
        $new_comment = get_comment($data['id']);
1095
        $this->assertEquals($params['content']['raw'], $new_comment->comment_content);
1096
    }
1097
1098
    public function test_create_comment_missing_required_author_name() 
1099
    {
1100
        add_filter('rest_allow_anonymous_comments', '__return_true');
1101
        update_option('require_name_email', 1);
1102
1103
        $params = array(
1104
         'post'         => self::$post_id,
1105
         'author_email' => '[email protected]',
1106
         'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
1107
        );
1108
1109
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1110
        $request->add_header('content-type', 'application/json');
1111
        $request->set_body(wp_json_encode($params));
1112
1113
        $response = $this->server->dispatch($request);
1114
1115
        $this->assertErrorResponse('rest_comment_author_data_required', $response, 400);
1116
    }
1117
1118
    public function test_create_comment_empty_required_author_name() 
1119
    {
1120
        add_filter('rest_allow_anonymous_comments', '__return_true');
1121
        update_option('require_name_email', 1);
1122
1123
        $params = array(
1124
         'author_name'  => '',
1125
         'author_email' => '[email protected]',
1126
         'post'         => self::$post_id,
1127
         'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
1128
        );
1129
1130
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1131
        $request->add_header('content-type', 'application/json');
1132
        $request->set_body(wp_json_encode($params));
1133
1134
        $response = $this->server->dispatch($request);
1135
1136
        $this->assertErrorResponse('rest_comment_author_data_required', $response, 400);
1137
    }
1138
1139
    public function test_create_comment_missing_required_author_email() 
1140
    {
1141
        wp_set_current_user(self::$admin_id);
1142
        update_option('require_name_email', 1);
1143
1144
        $params = array(
1145
         'post'        => self::$post_id,
1146
         'author_name' => 'Edna Krabappel',
1147
         'content'     => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
1148
        );
1149
1150
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1151
        $request->add_header('content-type', 'application/json');
1152
        $request->set_body(wp_json_encode($params));
1153
1154
        $response = $this->server->dispatch($request);
1155
        $this->assertErrorResponse('rest_comment_author_data_required', $response, 400);
1156
    }
1157
1158
    public function test_create_comment_empty_required_author_email() 
1159
    {
1160
        wp_set_current_user(self::$admin_id);
1161
        update_option('require_name_email', 1);
1162
1163
        $params = array(
1164
         'post'         => self::$post_id,
1165
         'author_name'  => 'Edna Krabappel',
1166
         'author_email' => '',
1167
         'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
1168
        );
1169
1170
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1171
        $request->add_header('content-type', 'application/json');
1172
        $request->set_body(wp_json_encode($params));
1173
1174
        $response = $this->server->dispatch($request);
1175
        $this->assertErrorResponse('rest_comment_author_data_required', $response, 400);
1176
    }
1177
1178 View Code Duplication
    public function test_create_comment_author_email_too_short() 
1179
    {
1180
        wp_set_current_user(self::$admin_id);
1181
1182
        $params = array(
1183
         'post'         => self::$post_id,
1184
         'author_name'  => 'Homer J. Simpson',
1185
         'author_email' => 'a@b',
1186
         'content'      => 'in this house, we obey the laws of thermodynamics!',
1187
        );
1188
1189
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1190
        $request->add_header('content-type', 'application/json');
1191
        $request->set_body(wp_json_encode($params));
1192
        $response = $this->server->dispatch($request);
1193
1194
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1195
1196
        $data = $response->get_data();
1197
        $this->assertArrayHasKey('author_email', $data['data']['params']);
1198
    }
1199
1200
    public function test_create_item_invalid_no_content() 
1201
    {
1202
        wp_set_current_user(self::$admin_id);
1203
1204
        $params = array(
1205
         'post'         => self::$post_id,
1206
         'author_name'  => 'Reverend Lovejoy',
1207
         'author_email' => '[email protected]',
1208
         'author_url'   => 'http://timothylovejoy.jr',
1209
        );
1210
1211
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1212
        $request->add_header('content-type', 'application/json');
1213
        $request->set_body(wp_json_encode($params));
1214
1215
        $response = $this->server->dispatch($request);
1216
        $this->assertErrorResponse('rest_comment_content_invalid', $response, 400);
1217
1218
        $params['content'] = '';
1219
        $request->set_body(wp_json_encode($params));
1220
        $response = $this->server->dispatch($request);
1221
        $this->assertErrorResponse('rest_comment_content_invalid', $response, 400);
1222
    }
1223
1224
    public function test_create_item_invalid_date() 
1225
    {
1226
        wp_set_current_user(self::$admin_id);
1227
1228
        $params = array(
1229
         'post'         => self::$post_id,
1230
         'author_name'  => 'Reverend Lovejoy',
1231
         'author_email' => '[email protected]',
1232
         'author_url'   => 'http://timothylovejoy.jr',
1233
         'content'      => 'It\'s all over\, people! We don\'t have a prayer!',
1234
         'date'         => rand_str(),
1235
        );
1236
1237
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1238
        $request->add_header('content-type', 'application/json');
1239
        $request->set_body(wp_json_encode($params));
1240
1241
        $response = $this->server->dispatch($request);
1242
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1243
    }
1244
1245
1246
    public function test_create_item_assign_different_user() 
1247
    {
1248
        $subscriber_id = $this->factory->user->create(
1249
            array(
1250
            'role' => 'subscriber',
1251
            'user_email' => '[email protected]',
1252
            )
1253
        );
1254
1255
        wp_set_current_user(self::$admin_id);
1256
        $params = array(
1257
         'post'    => self::$post_id,
1258
         'author_name'  => 'Comic Book Guy',
1259
         'author_email' => '[email protected]',
1260
         'author_url'   => 'http://androidsdungeon.com',
1261
         'author' => $subscriber_id,
1262
         'content' => 'Worst Comment Ever!',
1263
         'date'    => '2014-11-07T10:14:25',
1264
        );
1265
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1266
        $request->add_header('content-type', 'application/json');
1267
        $request->set_body(wp_json_encode($params));
1268
        $response = $this->server->dispatch($request);
1269
        $this->assertEquals(201, $response->get_status());
1270
1271
        $data = $response->get_data();
1272
        $this->assertEquals($subscriber_id, $data['author']);
1273
        $this->assertEquals('127.0.0.1', $data['author_ip']);
1274
    }
1275
1276
    public function test_create_comment_without_type() 
1277
    {
1278
        $post_id = $this->factory->post->create();
1279
        wp_set_current_user(self::$admin_id);
1280
1281
        $params = array(
1282
         'post'    => $post_id,
1283
         'author'       => self::$admin_id,
1284
         'author_name'  => 'Comic Book Guy',
1285
         'author_email' => '[email protected]',
1286
         'author_url'   => 'http://androidsdungeon.com',
1287
         'content' => 'Worst Comment Ever!',
1288
         'date'    => '2014-11-07T10:14:25',
1289
        );
1290
1291
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1292
        $request->add_header('content-type', 'application/json');
1293
        $request->set_body(wp_json_encode($params));
1294
1295
        $response = $this->server->dispatch($request);
1296
        $this->assertEquals(201, $response->get_status());
1297
1298
        $data = $response->get_data();
1299
        $this->assertEquals('comment', $data['type']);
1300
1301
        $comment_id = $data['id'];
1302
1303
        // Make sure the new comment is present in the collection.
1304
        $collection = new WP_REST_Request('GET', '/wp/v2/comments');
1305
        $collection->set_param('post', $post_id);
1306
        $collection_response = $this->server->dispatch($collection);
1307
        $collection_data = $collection_response->get_data();
1308
        $this->assertEquals($comment_id, $collection_data[0]['id']);
1309
    }
1310
1311
    /**
1312
     * @ticket 38820
1313
     */
1314 View Code Duplication
    public function test_create_comment_with_invalid_type() 
1315
    {
1316
        $post_id = $this->factory->post->create();
1317
        wp_set_current_user(self::$admin_id);
1318
1319
        $params = array(
1320
         'post'    => $post_id,
1321
         'author'       => self::$admin_id,
1322
         'author_name'  => 'Comic Book Guy',
1323
         'author_email' => '[email protected]',
1324
         'author_url'   => 'http://androidsdungeon.com',
1325
         'content' => 'Worst Comment Ever!',
1326
         'date'    => '2014-11-07T10:14:25',
1327
         'type' => 'foo',
1328
        );
1329
1330
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1331
        $request->add_header('content-type', 'application/json');
1332
        $request->set_body(wp_json_encode($params));
1333
1334
        $response = $this->server->dispatch($request);
1335
        $this->assertErrorResponse('rest_invalid_comment_type', $response, 400);
1336
    }
1337
1338 View Code Duplication
    public function test_create_comment_invalid_email() 
1339
    {
1340
        $post_id = $this->factory->post->create();
1341
        wp_set_current_user(self::$admin_id);
1342
1343
        $params = array(
1344
         'post'    => $post_id,
1345
         'author'       => self::$admin_id,
1346
         'author_name'  => 'Comic Book Guy',
1347
         'author_email' => 'hello:)',
1348
         'author_url'   => 'http://androidsdungeon.com',
1349
         'content' => 'Worst Comment Ever!',
1350
         'date'    => '2014-11-07T10:14:25',
1351
        );
1352
1353
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1354
        $request->add_header('content-type', 'application/json');
1355
        $request->set_body(wp_json_encode($params));
1356
1357
        $response = $this->server->dispatch($request);
1358
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1359
    }
1360
1361
    public function test_create_item_current_user() 
1362
    {
1363
        $user_id = $this->factory->user->create(
1364
            array(
1365
            'role' => 'subscriber',
1366
            'user_email' => '[email protected]',
1367
            'first_name' => 'Lyle',
1368
            'last_name' => 'Lanley',
1369
            'display_name' => 'Lyle Lanley',
1370
            'user_url' => 'http://simpsons.wikia.com/wiki/Lyle_Lanley',
1371
            )
1372
        );
1373
1374
        wp_set_current_user($user_id);
1375
1376
        $params = array(
1377
         'post' => self::$post_id,
1378
         'content' => "Well sir, there's nothing on earth like a genuine, bona fide, electrified, six-car Monorail!",
1379
        );
1380
1381
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1382
        $request->add_header('content-type', 'application/json');
1383
        $request->set_body(wp_json_encode($params));
1384
        $response = $this->server->dispatch($request);
1385
1386
        $this->assertEquals(201, $response->get_status());
1387
        $data = $response->get_data();
1388
        $this->assertEquals($user_id, $data['author']);
1389
1390
        // Check author data matches
1391
        $author = get_user_by('id', $user_id);
1392
        $comment = get_comment($data['id']);
1393
        $this->assertEquals($author->display_name, $comment->comment_author);
1394
        $this->assertEquals($author->user_email, $comment->comment_author_email);
1395
        $this->assertEquals($author->user_url, $comment->comment_author_url);
1396
    }
1397
1398 View Code Duplication
    public function test_create_comment_other_user() 
1399
    {
1400
        wp_set_current_user(self::$admin_id);
1401
1402
        $params = array(
1403
         'post'    => self::$post_id,
1404
         'author_name'  => 'Homer Jay Simpson',
1405
         'author_email' => '[email protected]',
1406
         'author_url'   => 'http://compuglobalhypermeganet.com',
1407
         'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1408
         'author'    => self::$subscriber_id,
1409
        );
1410
1411
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1412
        $request->add_header('content-type', 'application/json');
1413
        $request->set_body(wp_json_encode($params));
1414
        $response = $this->server->dispatch($request);
1415
1416
        $this->assertEquals(201, $response->get_status());
1417
        $data = $response->get_data();
1418
        $this->assertEquals(self::$subscriber_id, $data['author']);
1419
        $this->assertEquals('Homer Jay Simpson', $data['author_name']);
1420
        $this->assertEquals('[email protected]', $data['author_email']);
1421
        $this->assertEquals('http://compuglobalhypermeganet.com', $data['author_url']);
1422
    }
1423
1424
    public function test_create_comment_other_user_without_permission() 
1425
    {
1426
        wp_set_current_user(self::$subscriber_id);
1427
1428
        $params = array(
1429
         'post'         => self::$post_id,
1430
         'author_name'  => 'Homer Jay Simpson',
1431
         'author_email' => '[email protected]',
1432
         'author_url'   => 'http://compuglobalhypermeganet.com',
1433
         'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1434
         'author'       => self::$admin_id,
1435
        );
1436
1437
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1438
        $request->add_header('content-type', 'application/json');
1439
        $request->set_body(wp_json_encode($params));
1440
        $response = $this->server->dispatch($request);
1441
1442
        $this->assertErrorResponse('rest_comment_invalid_author', $response, 403);
1443
    }
1444
1445 View Code Duplication
    public function test_create_comment_invalid_post() 
1446
    {
1447
        wp_set_current_user(self::$subscriber_id);
1448
1449
        $params = array(
1450
         'post'           => 'some-slug',
1451
         'author_name'  => 'Homer Jay Simpson',
1452
         'author_email' => '[email protected]',
1453
         'author_url'   => 'http://compuglobalhypermeganet.com',
1454
         'content'       => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1455
         'author'       => self::$subscriber_id,
1456
        );
1457
1458
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1459
        $request->add_header('content-type', 'application/json');
1460
        $request->set_body(wp_json_encode($params));
1461
        $response = $this->server->dispatch($request);
1462
1463
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1464
    }
1465
1466 View Code Duplication
    public function test_create_comment_status_without_permission() 
1467
    {
1468
        wp_set_current_user(self::$subscriber_id);
1469
1470
        $params = array(
1471
         'post'         => self::$post_id,
1472
         'author_name'  => 'Homer Jay Simpson',
1473
         'author_email' => '[email protected]',
1474
         'author_url'   => 'http://compuglobalhypermeganet.com',
1475
         'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1476
         'author'       => self::$subscriber_id,
1477
         'status'        => 'approved',
1478
        );
1479
1480
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1481
        $request->add_header('content-type', 'application/json');
1482
        $request->set_body(wp_json_encode($params));
1483
        $response = $this->server->dispatch($request);
1484
1485
        $this->assertErrorResponse('rest_comment_invalid_status', $response, 403);
1486
    }
1487
1488
    public function test_create_comment_with_status_IP_and_user_agent() 
0 ignored issues
show
Coding Style introduced by
The function name test_create_comment_with_status_IP_and_user_agent is in camel caps, but expected test_create_comment_with_status_i_p_and_user_agent instead as per the coding standard.
Loading history...
1489
    {
1490
        $post_id = $this->factory->post->create();
1491
        wp_set_current_user(self::$admin_id);
1492
1493
        $params = array(
1494
         'post'         => $post_id,
1495
         'author_name'  => 'Comic Book Guy',
1496
         'author_email' => '[email protected]',
1497
         'author_ip'    => '139.130.4.5',
1498
         'author_url'   => 'http://androidsdungeon.com',
1499
         'author_user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
1500
         'content'      => 'Worst Comment Ever!',
1501
         'status'       => 'approved',
1502
        );
1503
1504
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1505
        $request->add_header('content-type', 'application/json');
1506
        $request->set_body(wp_json_encode($params));
1507
1508
        $response = $this->server->dispatch($request);
1509
        $this->assertEquals(201, $response->get_status());
1510
1511
        $data = $response->get_data();
1512
        $this->assertEquals('approved', $data['status']);
1513
        $this->assertEquals('139.130.4.5', $data['author_ip']);
1514
        $this->assertEquals('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', $data['author_user_agent']);
1515
    }
1516
1517 View Code Duplication
    public function test_create_comment_user_agent_header() 
1518
    {
1519
        wp_set_current_user(self::$admin_id);
1520
1521
        $params = array(
1522
         'post'         => self::$post_id,
1523
         'author_name'  => 'Homer Jay Simpson',
1524
         'author_email' => '[email protected]',
1525
         'author_url'   => 'http://compuglobalhypermeganet.com',
1526
         'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1527
        );
1528
1529
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1530
        $request->add_header('content-type', 'application/json');
1531
        $request->add_header('user_agent', 'Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)');
1532
        $request->set_body(wp_json_encode($params));
1533
1534
        $response = $this->server->dispatch($request);
1535
        $this->assertEquals(201, $response->get_status());
1536
1537
        $data = $response->get_data();
1538
1539
        $new_comment = get_comment($data['id']);
1540
        $this->assertEquals('Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)', $new_comment->comment_agent);
1541
    }
1542
1543
    public function test_create_comment_author_ip() 
1544
    {
1545
        wp_set_current_user(self::$admin_id);
1546
1547
        $params = array(
1548
         'post'         => self::$post_id,
1549
         'author_name'  => 'Comic Book Guy',
1550
         'author_email' => '[email protected]',
1551
         'author_url'   => 'http://androidsdungeon.com',
1552
         'author_ip'    => '127.0.0.3',
1553
         'content'      => 'Worst Comment Ever!',
1554
         'status'       => 'approved',
1555
        );
1556
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1557
        $request->add_header('content-type', 'application/json');
1558
        $request->set_body(wp_json_encode($params));
1559
        $response = $this->server->dispatch($request);
1560
        $data = $response->get_data();
1561
        $new_comment = get_comment($data['id']);
1562
        $this->assertEquals('127.0.0.3', $new_comment->comment_author_IP);
1563
    }
1564
1565 View Code Duplication
    public function test_create_comment_invalid_author_IP() 
0 ignored issues
show
Coding Style introduced by
The function name test_create_comment_invalid_author_IP is in camel caps, but expected test_create_comment_invalid_author_i_p instead as per the coding standard.
Loading history...
1566
    {
1567
        wp_set_current_user(self::$admin_id);
1568
1569
        $params = array(
1570
         'post'         => self::$post_id,
1571
         'author_name'  => 'Comic Book Guy',
1572
         'author_email' => '[email protected]',
1573
         'author_url'   => 'http://androidsdungeon.com',
1574
         'author_ip'    => '867.5309',
1575
         'content'      => 'Worst Comment Ever!',
1576
         'status'       => 'approved',
1577
        );
1578
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1579
        $request->add_header('content-type', 'application/json');
1580
        $request->set_body(wp_json_encode($params));
1581
1582
        $response = $this->server->dispatch($request);
1583
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1584
    }
1585
1586 View Code Duplication
    public function test_create_comment_author_ip_no_permission() 
1587
    {
1588
        wp_set_current_user(self::$subscriber_id);
1589
        $params = array(
1590
         'author_name'  => 'Comic Book Guy',
1591
         'author_email' => '[email protected]',
1592
         'author_url'   => 'http://androidsdungeon.com',
1593
         'author_ip'    => '10.0.10.1',
1594
         'content'      => 'Worst Comment Ever!',
1595
         'status'       => 'approved',
1596
        );
1597
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1598
        $request->add_header('content-type', 'application/json');
1599
        $request->set_body(wp_json_encode($params));
1600
        $response = $this->server->dispatch($request);
1601
        $this->assertErrorResponse('rest_comment_invalid_author_ip', $response, 403);
1602
    }
1603
1604
    public function test_create_comment_author_ip_defaults_to_remote_addr() 
1605
    {
1606
        wp_set_current_user(self::$admin_id);
1607
        $_SERVER['REMOTE_ADDR'] = '127.0.0.2';
0 ignored issues
show
introduced by
Due to using Batcache, server side based client related logic will not work, use JS instead.
Loading history...
1608
        $params = array(
1609
         'post'         => self::$post_id,
1610
         'author_name'  => 'Comic Book Guy',
1611
         'author_email' => '[email protected]',
1612
         'author_url'   => 'http://androidsdungeon.com',
1613
         'content'      => 'Worst Comment Ever!',
1614
        );
1615
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1616
        $request->add_header('content-type', 'application/json');
1617
        $request->set_body(wp_json_encode($params));
1618
        $response = $this->server->dispatch($request);
1619
        $data = $response->get_data();
1620
        $new_comment = get_comment($data['id']);
1621
        $this->assertEquals('127.0.0.2', $new_comment->comment_author_IP);
1622
    }
1623
1624 View Code Duplication
    public function test_create_comment_no_post_id() 
1625
    {
1626
        wp_set_current_user(self::$admin_id);
1627
1628
        $params = array(
1629
         'author_name'  => 'Comic Book Guy',
1630
         'author_email' => '[email protected]',
1631
         'author_url'   => 'http://androidsdungeon.com',
1632
         'content'      => 'Worst Comment Ever!',
1633
         'status'       => 'approved',
1634
        );
1635
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1636
        $request->add_header('content-type', 'application/json');
1637
        $request->set_body(wp_json_encode($params));
1638
1639
        $response = $this->server->dispatch($request);
1640
1641
        $this->assertErrorResponse('rest_comment_invalid_post_id', $response, 403);
1642
    }
1643
1644
    public function test_create_comment_no_post_id_no_permission() 
1645
    {
1646
        wp_set_current_user(self::$subscriber_id);
1647
1648
        $params = array(
1649
         'author_name'  => 'Homer Jay Simpson',
1650
         'author_email' => '[email protected]',
1651
         'author_url'   => 'http://compuglobalhypermeganet.com',
1652
         'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1653
         'author'       => self::$subscriber_id,
1654
        );
1655
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1656
        $request->add_header('content-type', 'application/json');
1657
        $request->set_body(wp_json_encode($params));
1658
1659
        $response = $this->server->dispatch($request);
1660
        $this->assertErrorResponse('rest_comment_invalid_post_id', $response, 403);
1661
    }
1662
1663 View Code Duplication
    public function test_create_comment_invalid_post_id() 
1664
    {
1665
        wp_set_current_user(self::$admin_id);
1666
1667
        $params = array(
1668
         'author_name'  => 'Homer Jay Simpson',
1669
         'author_email' => '[email protected]',
1670
         'author_url'   => 'http://compuglobalhypermeganet.com',
1671
         'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
1672
         'status'       => 'approved',
1673
         'post'         => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
1674
        );
1675
1676
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1677
        $request->add_header('content-type', 'application/json');
1678
        $request->set_body(wp_json_encode($params));
1679
1680
        $response = $this->server->dispatch($request);
1681
        $this->assertErrorResponse('rest_comment_invalid_post_id', $response, 403);
1682
    }
1683
1684
    public function test_create_comment_draft_post() 
1685
    {
1686
        wp_set_current_user(self::$subscriber_id);
1687
1688
        $params = array(
1689
         'post'         => self::$draft_id,
1690
         'author_name'  => 'Ishmael',
1691
         'author_email' => '[email protected]',
1692
         'author_url'   => 'https://en.wikipedia.org/wiki/Herman_Melville',
1693
         'content'      => 'Call me Ishmael.',
1694
         'author'       => self::$subscriber_id,
1695
        );
1696
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1697
        $request->add_header('content-type', 'application/json');
1698
        $request->set_body(wp_json_encode($params));
1699
1700
        $response = $this->server->dispatch($request);
1701
1702
        $this->assertErrorResponse('rest_comment_draft_post', $response, 403);
1703
    }
1704
1705
    public function test_create_comment_trash_post() 
1706
    {
1707
        wp_set_current_user(self::$subscriber_id);
1708
1709
        $params = array(
1710
         'post'         => self::$trash_id,
1711
         'author_name'  => 'Ishmael',
1712
         'author_email' => '[email protected]',
1713
         'author_url'   => 'https://en.wikipedia.org/wiki/Herman_Melville',
1714
         'content'      => 'Call me Ishmael.',
1715
         'author'       => self::$subscriber_id,
1716
        );
1717
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1718
        $request->add_header('content-type', 'application/json');
1719
        $request->set_body(wp_json_encode($params));
1720
1721
        $response = $this->server->dispatch($request);
1722
1723
        $this->assertErrorResponse('rest_comment_trash_post', $response, 403);
1724
    }
1725
1726
    public function test_create_comment_private_post_invalid_permission() 
1727
    {
1728
        wp_set_current_user(self::$subscriber_id);
1729
1730
        $params = array(
1731
         'post'         => self::$private_id,
1732
         'author_name'  => 'Homer Jay Simpson',
1733
         'author_email' => '[email protected]',
1734
         'author_url'   => 'http://compuglobalhypermeganet.com',
1735
         'content'      => 'I\’d be a vegetarian if bacon grew on trees.',
1736
         'author'       => self::$subscriber_id,
1737
        );
1738
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1739
        $request->add_header('content-type', 'application/json');
1740
        $request->set_body(wp_json_encode($params));
1741
1742
        $response = $this->server->dispatch($request);
1743
1744
        $this->assertErrorResponse('rest_cannot_read_post', $response, 403);
1745
    }
1746
1747
    public function test_create_comment_password_post_invalid_permission() 
1748
    {
1749
        wp_set_current_user(self::$subscriber_id);
1750
1751
        $params = array(
1752
         'post'         => self::$password_id,
1753
         'author_name'  => 'Homer Jay Simpson',
1754
         'author_email' => '[email protected]',
1755
         'author_url'   => 'http://compuglobalhypermeganet.com',
1756
         'content'      => 'I\’d be a vegetarian if bacon grew on trees.',
1757
         'author'       => self::$subscriber_id,
1758
        );
1759
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1760
        $request->add_header('content-type', 'application/json');
1761
        $request->set_body(wp_json_encode($params));
1762
1763
        $response = $this->server->dispatch($request);
1764
        $this->assertErrorResponse('rest_cannot_read_post', $response, 403);
1765
    }
1766
1767
    public function test_create_item_duplicate() 
1768
    {
1769
        wp_set_current_user(self::$subscriber_id);
1770
        $this->factory->comment->create(
1771
            array(
1772
            'comment_post_ID'      => self::$post_id,
1773
            'comment_author'       => 'Guy N. Cognito',
1774
            'comment_author_email' => '[email protected]',
1775
            'comment_content'      => 'Homer? Who is Homer? My name is Guy N. Cognito.',
1776
            )
1777
        );
1778
1779
        $params = array(
1780
         'post'    => self::$post_id,
1781
         'author_name'  => 'Guy N. Cognito',
1782
         'author_email' => '[email protected]',
1783
         'content' => 'Homer? Who is Homer? My name is Guy N. Cognito.',
1784
        );
1785
1786
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1787
        $request->add_header('content-type', 'application/json');
1788
        $request->set_body(wp_json_encode($params));
1789
        $response = $this->server->dispatch($request);
1790
1791
        $this->assertEquals(409, $response->get_status());
1792
    }
1793
1794
    public function test_create_comment_closed() 
1795
    {
1796
        $post_id = $this->factory->post->create(
1797
            array(
1798
            'comment_status' => 'closed',
1799
            )
1800
        );
1801
        wp_set_current_user(self::$subscriber_id);
1802
1803
        $params = array(
1804
         'post'      => $post_id,
1805
        );
1806
1807
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1808
        $request->add_header('content-type', 'application/json');
1809
        $request->set_body(wp_json_encode($params));
1810
        $response = $this->server->dispatch($request);
1811
1812
        $this->assertEquals(403, $response->get_status());
1813
    }
1814
1815
    public function test_create_comment_require_login() 
1816
    {
1817
        wp_set_current_user(0);
1818
        update_option('comment_registration', 1);
1819
        add_filter('rest_allow_anonymous_comments', '__return_true');
1820
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1821
        $request->set_param('post', self::$post_id);
1822
        $response = $this->server->dispatch($request);
1823
        $this->assertEquals(401, $response->get_status());
1824
        $data = $response->get_data();
1825
        $this->assertEquals('rest_comment_login_required', $data['code']);
1826
    }
1827
1828
    public function test_create_item_invalid_author() 
1829
    {
1830
        wp_set_current_user(self::$admin_id);
1831
1832
        $params = array(
1833
         'post'         => self::$post_id,
1834
         'author'       => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
1835
         'content'      => 'It\'s all over\, people! We don\'t have a prayer!',
1836
        );
1837
1838
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1839
        $request->add_header('content-type', 'application/json');
1840
        $request->set_body(wp_json_encode($params));
1841
1842
        $response = $this->server->dispatch($request);
1843
        $this->assertErrorResponse('rest_comment_author_invalid', $response, 400);
1844
    }
1845
1846
    public function test_create_item_pull_author_info() 
1847
    {
1848
        wp_set_current_user(self::$admin_id);
1849
1850
        $author = new WP_User(self::$author_id);
1851
        $params = array(
1852
         'post'         => self::$post_id,
1853
         'author'       => self::$author_id,
1854
         'content'      => 'It\'s all over\, people! We don\'t have a prayer!',
1855
        );
1856
1857
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1858
        $request->add_header('content-type', 'application/json');
1859
        $request->set_body(wp_json_encode($params));
1860
1861
        $response = $this->server->dispatch($request);
1862
1863
        $result = $response->get_data();
1864
        $this->assertSame(self::$author_id, $result['author']);
1865
        $this->assertSame('Sea Captain', $result['author_name']);
1866
        $this->assertSame('[email protected]', $result['author_email']);
1867
        $this->assertSame('http://thefryingdutchman.com', $result['author_url']);
1868
    }
1869
1870
    public function test_create_comment_two_times() 
1871
    {
1872
        add_filter('rest_allow_anonymous_comments', '__return_true');
1873
1874
        $params = array(
1875
         'post'    => self::$post_id,
1876
         'author_name'  => 'Comic Book Guy',
1877
         'author_email' => '[email protected]',
1878
         'author_url'   => 'http://androidsdungeon.com',
1879
         'content' => 'Worst Comment Ever!',
1880
        );
1881
1882
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1883
        $request->add_header('content-type', 'application/json');
1884
        $request->set_body(wp_json_encode($params));
1885
1886
        $response = $this->server->dispatch($request);
1887
        $this->assertEquals(201, $response->get_status());
1888
1889
        $params = array(
1890
         'post'    => self::$post_id,
1891
         'author_name'  => 'Comic Book Guy',
1892
         'author_email' => '[email protected]',
1893
         'author_url'   => 'http://androidsdungeon.com',
1894
         'content'      => 'Shakes fist at sky',
1895
        );
1896
1897
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1898
        $request->add_header('content-type', 'application/json');
1899
        $request->set_body(wp_json_encode($params));
1900
1901
        $response = $this->server->dispatch($request);
1902
        $this->assertEquals(400, $response->get_status());
1903
    }
1904
1905
    public function anonymous_comments_callback_null() 
1906
    {
1907
        // I'm a plugin developer who forgot to include a return value for some
1908
        // code path in my 'rest_allow_anonymous_comments' filter.
1909
    }
1910
1911
    public function test_allow_anonymous_comments_null() 
1912
    {
1913
        add_filter('rest_allow_anonymous_comments', array( $this, 'anonymous_comments_callback_null' ), 10, 2);
1914
1915
        $params = array(
1916
         'post'         => self::$post_id,
1917
         'author_name'  => 'Comic Book Guy',
1918
         'author_email' => '[email protected]',
1919
         'author_url'   => 'http://androidsdungeon.com',
1920
         'content'      => 'Worst Comment Ever!',
1921
        );
1922
1923
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1924
        $request->add_header('content-type', 'application/json');
1925
        $request->set_body(wp_json_encode($params));
1926
1927
        $response = $this->server->dispatch($request);
1928
1929
        remove_filter('rest_allow_anonymous_comments', array( $this, 'anonymous_comments_callback_null' ), 10, 2);
1930
1931
        $this->assertErrorResponse('rest_comment_login_required', $response, 401);
1932
    }
1933
1934
    /**
1935
     * @ticket 38477
1936
     */
1937 View Code Duplication
    public function test_create_comment_author_name_too_long() 
1938
    {
1939
        wp_set_current_user(self::$subscriber_id);
1940
1941
        $params = array(
1942
         'post'         => self::$post_id,
1943
         'author_name'  => rand_long_str(246),
1944
         'author_email' => '[email protected]',
1945
         'author_url'   => 'http://jazz.gingivitis.com',
1946
         'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
1947
         'date'         => '1995-04-30T10:22:00',
1948
        );
1949
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1950
1951
        $request->add_header('content-type', 'application/json');
1952
        $request->set_body(wp_json_encode($params));
1953
        $response = $this->server->dispatch($request);
1954
1955
        $this->assertErrorResponse('comment_author_column_length', $response, 400);
1956
    }
1957
1958
    /**
1959
     * @ticket 38477
1960
     */
1961 View Code Duplication
    public function test_create_comment_author_email_too_long() 
1962
    {
1963
        wp_set_current_user(self::$subscriber_id);
1964
1965
        $params = array(
1966
         'post'         => self::$post_id,
1967
         'author_name'  => 'Bleeding Gums Murphy',
1968
         'author_email' => 'murphy@' . rand_long_str(190) . '.com',
1969
         'author_url'   => 'http://jazz.gingivitis.com',
1970
         'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
1971
         'date'         => '1995-04-30T10:22:00',
1972
        );
1973
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1974
1975
        $request->add_header('content-type', 'application/json');
1976
        $request->set_body(wp_json_encode($params));
1977
        $response = $this->server->dispatch($request);
1978
1979
        $this->assertErrorResponse('comment_author_email_column_length', $response, 400);
1980
    }
1981
1982
    /**
1983
     * @ticket 38477
1984
     */
1985 View Code Duplication
    public function test_create_comment_author_url_too_long() 
1986
    {
1987
        wp_set_current_user(self::$subscriber_id);
1988
1989
        $params = array(
1990
         'post'         => self::$post_id,
1991
         'author_name'  => 'Bleeding Gums Murphy',
1992
         'author_email' => '[email protected]',
1993
         'author_url'   => 'http://jazz.' . rand_long_str(185) . '.com',
1994
         'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
1995
         'date'         => '1995-04-30T10:22:00',
1996
        );
1997
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
1998
1999
        $request->add_header('content-type', 'application/json');
2000
        $request->set_body(wp_json_encode($params));
2001
        $response = $this->server->dispatch($request);
2002
2003
        $this->assertErrorResponse('comment_author_url_column_length', $response, 400);
2004
    }
2005
2006
    /**
2007
     * @ticket 38477
2008
     */
2009 View Code Duplication
    public function test_create_comment_content_too_long() 
2010
    {
2011
        wp_set_current_user(self::$subscriber_id);
2012
2013
        $params = array(
2014
         'post'         => self::$post_id,
2015
         'author_name'  => 'Bleeding Gums Murphy',
2016
         'author_email' => '[email protected]',
2017
         'author_url'   => 'http://jazz.gingivitis.com',
2018
         'content'      => rand_long_str(66525),
2019
         'date'         => '1995-04-30T10:22:00',
2020
        );
2021
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
2022
2023
        $request->add_header('content-type', 'application/json');
2024
        $request->set_body(wp_json_encode($params));
2025
        $response = $this->server->dispatch($request);
2026
2027
        $this->assertErrorResponse('comment_content_column_length', $response, 400);
2028
    }
2029
2030
    public function test_create_comment_without_password() 
2031
    {
2032
        wp_set_current_user(self::$subscriber_id);
2033
2034
        $params = array(
2035
         'post'         => self::$password_id,
2036
         'author_name'  => 'Bleeding Gums Murphy',
2037
         'author_email' => '[email protected]',
2038
         'author_url'   => 'http://jazz.gingivitis.com',
2039
         'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
2040
        );
2041
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
2042
2043
        $request->add_header('content-type', 'application/json');
2044
        $request->set_body(wp_json_encode($params));
2045
        $response = $this->server->dispatch($request);
2046
2047
        $this->assertErrorResponse('rest_cannot_read_post', $response, 403);
2048
    }
2049
2050 View Code Duplication
    public function test_create_comment_with_password() 
2051
    {
2052
        add_filter('rest_allow_anonymous_comments', '__return_true');
2053
2054
        $params = array(
2055
         'post'         => self::$password_id,
2056
         'author_name'  => 'Bleeding Gums Murphy',
2057
         'author_email' => '[email protected]',
2058
         'author_url'   => 'http://jazz.gingivitis.com',
2059
         'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
2060
         'password'     => 'toomanysecrets',
2061
        );
2062
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
2063
2064
        $request->add_header('content-type', 'application/json');
2065
        $request->set_body(wp_json_encode($params));
2066
        $response = $this->server->dispatch($request);
2067
        $this->assertEquals(201, $response->get_status());
2068
    }
2069
2070
    public function test_update_item() 
2071
    {
2072
        $post_id = $this->factory->post->create();
2073
2074
        wp_set_current_user(self::$admin_id);
2075
2076
        $params = array(
2077
         'author'       => self::$subscriber_id,
2078
         'author_name'  => 'Disco Stu',
2079
         'author_url'   => 'http://stusdisco.com',
2080
         'author_email' => '[email protected]',
2081
         'author_ip'    => '4.4.4.4',
2082
         'content'      => 'Testing.',
2083
         'date'         => '2014-11-07T10:14:25',
2084
         'post'         => $post_id,
2085
        );
2086
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2087
        $request->add_header('content-type', 'application/json');
2088
        $request->set_body(wp_json_encode($params));
2089
2090
        $response = $this->server->dispatch($request);
2091
        $this->assertEquals(200, $response->get_status());
2092
2093
        $comment = $response->get_data();
2094
        $updated = get_comment(self::$approved_id);
2095
        $this->assertEquals($params['content'], $comment['content']['raw']);
2096
        $this->assertEquals($params['author'], $comment['author']);
2097
        $this->assertEquals($params['author_name'], $comment['author_name']);
2098
        $this->assertEquals($params['author_url'], $comment['author_url']);
2099
        $this->assertEquals($params['author_email'], $comment['author_email']);
2100
        $this->assertEquals($params['author_ip'], $comment['author_ip']);
2101
        $this->assertEquals($params['post'], $comment['post']);
2102
2103
        $this->assertEquals(mysql_to_rfc3339($updated->comment_date), $comment['date']);
2104
        $this->assertEquals('2014-11-07T10:14:25', $comment['date']);
2105
    }
2106
2107
    public function test_update_item_no_content() 
2108
    {
2109
        $post_id = $this->factory->post->create();
2110
2111
        wp_set_current_user(self::$admin_id);
2112
2113
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2114
        $request->set_param('author_email', '[email protected]');
2115
2116
        // Sending a request without content is fine.
2117
        $response = $this->server->dispatch($request);
2118
        $this->assertEquals(200, $response->get_status());
2119
2120
        // Sending a request with empty comment is not fine.
2121
        $request->set_param('author_email', '[email protected]');
2122
        $request->set_param('content', '');
2123
        $response = $this->server->dispatch($request);
2124
        $this->assertErrorResponse('rest_comment_content_invalid', $response, 400);
2125
    }
2126
2127
    public function test_update_comment_status() 
2128
    {
2129
        wp_set_current_user(self::$admin_id);
2130
2131
        $comment_id = $this->factory->comment->create(
2132
            array(
2133
            'comment_approved' => 0,
2134
            'comment_post_ID'  => self::$post_id,
2135
            )
2136
        );
2137
2138
        $params = array(
2139
         'status' => 'approve',
2140
        );
2141
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', $comment_id));
2142
        $request->add_header('content-type', 'application/json');
2143
        $request->set_body(wp_json_encode($params));
2144
2145
        $response = $this->server->dispatch($request);
2146
        $this->assertEquals(200, $response->get_status());
2147
2148
        $comment = $response->get_data();
2149
        $updated = get_comment($comment_id);
2150
        $this->assertEquals('approved', $comment['status']);
2151
        $this->assertEquals(1, $updated->comment_approved);
2152
    }
2153
2154
    public function test_update_comment_field_does_not_use_default_values() 
2155
    {
2156
        wp_set_current_user(self::$admin_id);
2157
2158
        $comment_id = $this->factory->comment->create(
2159
            array(
2160
            'comment_approved' => 0,
2161
            'comment_post_ID'  => self::$post_id,
2162
            'comment_content'  => 'some content',
2163
            )
2164
        );
2165
2166
        $params = array(
2167
         'status' => 'approve',
2168
        );
2169
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', $comment_id));
2170
        $request->add_header('content-type', 'application/json');
2171
        $request->set_body(wp_json_encode($params));
2172
2173
        $response = $this->server->dispatch($request);
2174
        $this->assertEquals(200, $response->get_status());
2175
2176
        $comment = $response->get_data();
2177
        $updated = get_comment($comment_id);
2178
        $this->assertEquals('approved', $comment['status']);
2179
        $this->assertEquals(1, $updated->comment_approved);
2180
        $this->assertEquals('some content', $updated->comment_content);
2181
    }
2182
2183
    public function test_update_comment_date_gmt() 
2184
    {
2185
        wp_set_current_user(self::$admin_id);
2186
2187
        $params = array(
2188
         'date_gmt' => '2015-05-07T10:14:25',
2189
         'content'  => 'I\'ll be deep in the cold, cold ground before I recognize Missouri.',
2190
        );
2191
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2192
        $request->add_header('content-type', 'application/json');
2193
        $request->set_body(wp_json_encode($params));
2194
2195
        $response = $this->server->dispatch($request);
2196
        $this->assertEquals(200, $response->get_status());
2197
2198
        $comment = $response->get_data();
2199
        $updated = get_comment(self::$approved_id);
2200
        $this->assertEquals($params['date_gmt'], $comment['date_gmt']);
2201
        $this->assertEquals($params['date_gmt'], mysql_to_rfc3339($updated->comment_date_gmt));
2202
    }
2203
2204
    public function test_update_comment_author_email_only() 
2205
    {
2206
        wp_set_current_user(self::$editor_id);
2207
        update_option('require_name_email', 1);
2208
2209
        $params = array(
2210
         'post'         => self::$post_id,
2211
         'author_email' => '[email protected]',
2212
         'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
2213
        );
2214
2215
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2216
        $request->add_header('content-type', 'application/json');
2217
        $request->set_body(wp_json_encode($params));
2218
2219
        $response = $this->server->dispatch($request);
2220
        $this->assertEquals(200, $response->get_status());
2221
    }
2222
2223
    public function test_update_comment_empty_author_name() 
2224
    {
2225
        wp_set_current_user(self::$editor_id);
2226
        update_option('require_name_email', 1);
2227
2228
        $params = array(
2229
         'author_name'  => '',
2230
         'author_email' => '[email protected]',
2231
         'post'         => self::$post_id,
2232
         'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
2233
        );
2234
2235
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2236
        $request->add_header('content-type', 'application/json');
2237
        $request->set_body(wp_json_encode($params));
2238
2239
        $response = $this->server->dispatch($request);
2240
        $this->assertEquals(200, $response->get_status());
2241
    }
2242
2243
    public function test_update_comment_author_name_only() 
2244
    {
2245
        wp_set_current_user(self::$admin_id);
2246
        update_option('require_name_email', 1);
2247
2248
        $params = array(
2249
         'post'        => self::$post_id,
2250
         'author_name' => 'Edna Krabappel',
2251
         'content'     => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
2252
        );
2253
2254
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2255
        $request->add_header('content-type', 'application/json');
2256
        $request->set_body(wp_json_encode($params));
2257
2258
        $response = $this->server->dispatch($request);
2259
        $this->assertEquals(200, $response->get_status());
2260
    }
2261
2262
    public function test_update_comment_empty_author_email() 
2263
    {
2264
        wp_set_current_user(self::$admin_id);
2265
        update_option('require_name_email', 1);
2266
2267
        $params = array(
2268
         'post'         => self::$post_id,
2269
         'author_name'  => 'Edna Krabappel',
2270
         'author_email' => '',
2271
         'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
2272
        );
2273
2274
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2275
        $request->add_header('content-type', 'application/json');
2276
        $request->set_body(wp_json_encode($params));
2277
2278
        $response = $this->server->dispatch($request);
2279
        $this->assertEquals(200, $response->get_status());
2280
    }
2281
2282 View Code Duplication
    public function test_update_comment_author_email_too_short() 
2283
    {
2284
        wp_set_current_user(self::$admin_id);
2285
2286
        $params = array(
2287
         'post'         => self::$post_id,
2288
         'author_name'  => 'Homer J. Simpson',
2289
         'author_email' => 'a@b',
2290
         'content'      => 'in this house, we obey the laws of thermodynamics!',
2291
        );
2292
2293
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2294
        $request->add_header('content-type', 'application/json');
2295
        $request->set_body(wp_json_encode($params));
2296
        $response = $this->server->dispatch($request);
2297
2298
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
2299
        $data = $response->get_data();
2300
        $this->assertArrayHasKey('author_email', $data['data']['params']);
2301
    }
2302
2303
    public function test_update_comment_invalid_type() 
2304
    {
2305
        wp_set_current_user(self::$admin_id);
2306
2307
        $params = array(
2308
         'type' => 'trackback',
2309
        );
2310
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2311
        $request->add_header('content-type', 'application/json');
2312
        $request->set_body(wp_json_encode($params));
2313
2314
        $response = $this->server->dispatch($request);
2315
        $this->assertErrorResponse('rest_comment_invalid_type', $response, 404);
2316
    }
2317
2318
    public function test_update_comment_with_raw_property() 
2319
    {
2320
        wp_set_current_user(self::$admin_id);
2321
2322
        $params = array(
2323
         'content' => array(
2324
          'raw' => 'What the heck kind of name is Persephone?',
2325
         ),
2326
        );
2327
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2328
        $request->add_header('content-type', 'application/json');
2329
        $request->set_body(wp_json_encode($params));
2330
2331
        $response = $this->server->dispatch($request);
2332
2333
        $this->assertEquals(200, $response->get_status());
2334
2335
        $comment = $response->get_data();
2336
        $updated = get_comment(self::$approved_id);
2337
        $this->assertEquals($params['content']['raw'], $updated->comment_content);
2338
    }
2339
2340
    public function test_update_item_invalid_date() 
2341
    {
2342
        wp_set_current_user(self::$admin_id);
2343
2344
        $params = array(
2345
         'content' => rand_str(),
2346
         'date'    => rand_str(),
2347
        );
2348
2349
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2350
        $request->add_header('content-type', 'application/json');
2351
        $request->set_body(wp_json_encode($params));
2352
2353
        $response = $this->server->dispatch($request);
2354
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
2355
    }
2356
2357
    public function test_update_item_invalid_date_gmt() 
2358
    {
2359
        wp_set_current_user(self::$admin_id);
2360
2361
        $params = array(
2362
         'content'  => rand_str(),
2363
         'date_gmt' => rand_str(),
2364
        );
2365
2366
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2367
        $request->add_header('content-type', 'application/json');
2368
        $request->set_body(wp_json_encode($params));
2369
2370
        $response = $this->server->dispatch($request);
2371
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
2372
    }
2373
2374
    public function test_update_comment_invalid_id() 
2375
    {
2376
        wp_set_current_user(self::$subscriber_id);
2377
2378
        $params = array(
2379
         'content' => 'Oh, they have the internet on computers now!',
2380
        );
2381
        $request = new WP_REST_Request('PUT', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER);
2382
        $request->add_header('content-type', 'application/json');
2383
        $request->set_body(wp_json_encode($params));
2384
2385
        $response = $this->server->dispatch($request);
2386
        $this->assertErrorResponse('rest_comment_invalid_id', $response, 404);
2387
    }
2388
2389
    public function test_update_comment_invalid_permission() 
2390
    {
2391
        add_filter('rest_allow_anonymous_comments', '__return_true');
2392
2393
        $params = array(
2394
         'content' => 'Disco Stu likes disco music.',
2395
        );
2396
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$hold_id));
2397
        $request->add_header('content-type', 'application/json');
2398
        $request->set_body(wp_json_encode($params));
2399
2400
        $response = $this->server->dispatch($request);
2401
        $this->assertErrorResponse('rest_cannot_edit', $response, 401);
2402
    }
2403
2404
    public function test_update_comment_private_post_invalid_permission() 
2405
    {
2406
        $private_comment_id = $this->factory->comment->create(
2407
            array(
2408
            'comment_approved' => 1,
2409
            'comment_post_ID'  => self::$private_id,
2410
            'user_id'          => 0,
2411
            )
2412
        );
2413
2414
        wp_set_current_user(self::$subscriber_id);
2415
2416
        $params = array(
2417
         'content' => 'Disco Stu likes disco music.',
2418
        );
2419
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', $private_comment_id));
2420
        $request->add_header('content-type', 'application/json');
2421
        $request->set_body(wp_json_encode($params));
2422
2423
        $response = $this->server->dispatch($request);
2424
        $this->assertErrorResponse('rest_cannot_edit', $response, 403);
2425
    }
2426
2427
    public function test_update_comment_with_children_link() 
2428
    {
2429
        wp_set_current_user(self::$admin_id);
2430
        $comment_id_1 = $this->factory->comment->create(
2431
            array(
2432
            'comment_approved' => 1,
2433
            'comment_post_ID'  => self::$post_id,
2434
            'user_id'          => self::$subscriber_id,
2435
            ) 
2436
        );
2437
2438
        $child_comment = $this->factory->comment->create(
2439
            array(
2440
            'comment_approved' => 1,
2441
            'comment_post_ID'  => self::$post_id,
2442
            'user_id'          => self::$subscriber_id,
2443
            ) 
2444
        );
2445
2446
        // Check if comment 1 does not have the child link.
2447
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%s', $comment_id_1));
2448
        $response = $this->server->dispatch($request);
2449
        $this->assertEquals(200, $response->get_status());
2450
        $this->assertArrayNotHasKey('children', $response->get_links());
2451
2452
        // Change the comment parent.
2453
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%s', $child_comment));
2454
        $request->set_param('parent', $comment_id_1);
2455
        $request->set_param('content', rand_str());
2456
        $response = $this->server->dispatch($request);
2457
        $this->assertEquals(200, $response->get_status());
2458
2459
        // Check if comment 1 now has the child link.
2460
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%s', $comment_id_1));
2461
        $response = $this->server->dispatch($request);
2462
        $this->assertEquals(200, $response->get_status());
2463
        $this->assertArrayHasKey('children', $response->get_links());
2464
    }
2465
2466
    /**
2467
     * @ticket 38477
2468
     */
2469
    public function test_update_comment_author_name_too_long() 
2470
    {
2471
        wp_set_current_user(self::$admin_id);
2472
2473
        $params = array(
2474
         'author_name' => rand_long_str(246),
2475
         'content'     => 'This isn\'t a saxophone. It\'s an umbrella.',
2476
        );
2477
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2478
2479
        $request->add_header('content-type', 'application/json');
2480
        $request->set_body(wp_json_encode($params));
2481
        $response = $this->server->dispatch($request);
2482
2483
        $this->assertErrorResponse('comment_author_column_length', $response, 400);
2484
    }
2485
2486
    /**
2487
     * @ticket 38477
2488
     */
2489
    public function test_update_comment_author_email_too_long() 
2490
    {
2491
        wp_set_current_user(self::$admin_id);
2492
2493
        $params = array(
2494
         'author_email' => 'murphy@' . rand_long_str(190) . '.com',
2495
         'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
2496
        );
2497
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2498
2499
        $request->add_header('content-type', 'application/json');
2500
        $request->set_body(wp_json_encode($params));
2501
        $response = $this->server->dispatch($request);
2502
2503
        $this->assertErrorResponse('comment_author_email_column_length', $response, 400);
2504
    }
2505
2506
    /**
2507
     * @ticket 38477
2508
     */
2509
    public function test_update_comment_author_url_too_long() 
2510
    {
2511
        wp_set_current_user(self::$admin_id);
2512
2513
        $params = array(
2514
         'author_url' => 'http://jazz.' . rand_long_str(185) . '.com',
2515
         'content'    => 'This isn\'t a saxophone. It\'s an umbrella.',
2516
        );
2517
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2518
2519
        $request->add_header('content-type', 'application/json');
2520
        $request->set_body(wp_json_encode($params));
2521
        $response = $this->server->dispatch($request);
2522
2523
        $this->assertErrorResponse('comment_author_url_column_length', $response, 400);
2524
    }
2525
2526
    /**
2527
     * @ticket 38477
2528
     */
2529
    public function test_update_comment_content_too_long() 
2530
    {
2531
        wp_set_current_user(self::$admin_id);
2532
2533
        $params = array(
2534
         'content' => rand_long_str(66525),
2535
        );
2536
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
2537
2538
        $request->add_header('content-type', 'application/json');
2539
        $request->set_body(wp_json_encode($params));
2540
        $response = $this->server->dispatch($request);
2541
2542
        $this->assertErrorResponse('comment_content_column_length', $response, 400);
2543
    }
2544
2545
    public function verify_comment_roundtrip( $input = array(), $expected_output = array() ) 
2546
    {
2547
        // Create the comment
2548
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
2549
        $request->set_param('author_email', '[email protected]');
2550
        $request->set_param('post', self::$post_id);
2551
        foreach ( $input as $name => $value ) {
2552
            $request->set_param($name, $value);
2553
        }
2554
        $response = $this->server->dispatch($request);
2555
        $this->assertEquals(201, $response->get_status());
2556
        $actual_output = $response->get_data();
2557
2558
        // Compare expected API output to actual API output
2559
        $this->assertInternalType('array', $actual_output['content']);
2560
        $this->assertArrayHasKey('raw', $actual_output['content']);
2561
        $this->assertEquals($expected_output['content']['raw'], $actual_output['content']['raw']);
2562
        $this->assertEquals($expected_output['content']['rendered'], trim($actual_output['content']['rendered']));
2563
        $this->assertEquals($expected_output['author_name'], $actual_output['author_name']);
2564
        $this->assertEquals($expected_output['author_user_agent'], $actual_output['author_user_agent']);
2565
2566
        // Compare expected API output to WP internal values
2567
        $comment = get_comment($actual_output['id']);
2568
        $this->assertEquals($expected_output['content']['raw'], $comment->comment_content);
2569
        $this->assertEquals($expected_output['author_name'], $comment->comment_author);
2570
        $this->assertEquals($expected_output['author_user_agent'], $comment->comment_agent);
2571
2572
        // Update the comment
2573
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', $actual_output['id']));
2574
        foreach ( $input as $name => $value ) {
2575
            $request->set_param($name, $value);
2576
        }
2577
        // FIXME at least one value must change, or update fails
2578
        // See https://core.trac.wordpress.org/ticket/38700
2579
        $request->set_param('author_ip', '127.0.0.2');
2580
        $response = $this->server->dispatch($request);
2581
        $this->assertEquals(200, $response->get_status());
2582
        $actual_output = $response->get_data();
2583
2584
        // Compare expected API output to actual API output
2585
        $this->assertEquals($expected_output['content']['raw'], $actual_output['content']['raw']);
2586
        $this->assertEquals($expected_output['content']['rendered'], trim($actual_output['content']['rendered']));
2587
        $this->assertEquals($expected_output['author_name'], $actual_output['author_name']);
2588
        $this->assertEquals($expected_output['author_user_agent'], $actual_output['author_user_agent']);
2589
2590
        // Compare expected API output to WP internal values
2591
        $comment = get_comment($actual_output['id']);
2592
        $this->assertEquals($expected_output['content']['raw'], $comment->comment_content);
2593
        $this->assertEquals($expected_output['author_name'], $comment->comment_author);
2594
        $this->assertEquals($expected_output['author_user_agent'], $comment->comment_agent);
2595
    }
2596
2597 View Code Duplication
    public function test_comment_roundtrip_as_editor() 
2598
    {
2599
        wp_set_current_user(self::$editor_id);
2600
        $this->assertEquals(! is_multisite(), current_user_can('unfiltered_html'));
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
2601
        $this->verify_comment_roundtrip(
2602
            array(
2603
            'content'           => '\o/ ¯\_(ツ)_/¯',
2604
            'author_name'       => '\o/ ¯\_(ツ)_/¯',
2605
            'author_user_agent' => '\o/ ¯\_(ツ)_/¯',
2606
            ), array(
2607
            'content' => array(
2608
            'raw'      => '\o/ ¯\_(ツ)_/¯',
2609
            'rendered' => '<p>\o/ ¯\_(ツ)_/¯</p>',
2610
            ),
2611
            'author_name'       => '\o/ ¯\_(ツ)_/¯',
2612
            'author_user_agent' => '\o/ ¯\_(ツ)_/¯',
2613
            ) 
2614
        );
2615
    }
2616
2617
    public function test_comment_roundtrip_as_editor_unfiltered_html() 
2618
    {
2619
        wp_set_current_user(self::$editor_id);
2620
        if (is_multisite() ) {
2621
            $this->assertFalse(current_user_can('unfiltered_html'));
2622
            $this->verify_comment_roundtrip(
2623
                array(
2624
                'content'           => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2625
                'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2626
                'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2627
                ), array(
2628
                'content' => array(
2629
                'raw'      => 'div <strong>strong</strong> oh noes',
2630
                'rendered' => '<p>div <strong>strong</strong> oh noes</p>',
2631
                ),
2632
                'author_name'       => 'div strong',
2633
                'author_user_agent' => 'div strong',
2634
                ) 
2635
            );
2636
        } else {
2637
            $this->assertTrue(current_user_can('unfiltered_html'));
2638
            $this->verify_comment_roundtrip(
2639
                array(
2640
                'content'           => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2641
                'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2642
                'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2643
                ), array(
2644
                'content' => array(
2645
                'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2646
                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
2647
                ),
2648
                'author_name'       => 'div strong',
2649
                'author_user_agent' => 'div strong',
2650
                ) 
2651
            );
2652
        }
2653
    }
2654
2655 View Code Duplication
    public function test_comment_roundtrip_as_superadmin() 
2656
    {
2657
        wp_set_current_user(self::$superadmin_id);
2658
        $this->assertTrue(current_user_can('unfiltered_html'));
2659
        $this->verify_comment_roundtrip(
2660
            array(
2661
            'content'           => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2662
            'author_name'       => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2663
            'author_user_agent' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2664
            ), array(
2665
            'content' => array(
2666
            'raw'      => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2667
            'rendered' => '<p>\\\&#038;\\\ &amp; &invalid; < &lt; &amp;lt;' . "\n</p>",
2668
            ),
2669
            'author_name'       => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
2670
            'author_user_agent' => '\\\&\\\ &amp; &invalid; &lt; &lt; &amp;lt;',
2671
            ) 
2672
        );
2673
    }
2674
2675 View Code Duplication
    public function test_comment_roundtrip_as_superadmin_unfiltered_html() 
2676
    {
2677
        wp_set_current_user(self::$superadmin_id);
2678
        $this->assertTrue(current_user_can('unfiltered_html'));
2679
        $this->verify_comment_roundtrip(
2680
            array(
2681
            'content'           => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2682
            'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2683
            'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2684
            ), array(
2685
            'content' => array(
2686
            'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2687
            'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
2688
            ),
2689
            'author_name'       => 'div strong',
2690
            'author_user_agent' => 'div strong',
2691
            ) 
2692
        );
2693
    }
2694
2695 View Code Duplication
    public function test_delete_item() 
2696
    {
2697
        wp_set_current_user(self::$admin_id);
2698
2699
        $comment_id = $this->factory->comment->create(
2700
            array(
2701
            'comment_approved' => 1,
2702
            'comment_post_ID'  => self::$post_id,
2703
            'user_id'          => self::$subscriber_id,
2704
            )
2705
        );
2706
2707
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/comments/%d', $comment_id));
2708
        $request->set_param('force', 'false');
2709
        $response = $this->server->dispatch($request);
2710
        $this->assertEquals(200, $response->get_status());
2711
2712
        $data = $response->get_data();
2713
        $this->assertEquals('trash', $data['status']);
2714
    }
2715
2716
    public function test_delete_item_skip_trash() 
2717
    {
2718
        wp_set_current_user(self::$admin_id);
2719
2720
        $comment_id = $this->factory->comment->create(
2721
            array(
2722
            'comment_approved' => 1,
2723
            'comment_post_ID'  => self::$post_id,
2724
            'user_id'          => self::$subscriber_id,
2725
            )
2726
        );
2727
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/comments/%d', $comment_id));
2728
        $request['force'] = true;
2729
2730
        $response = $this->server->dispatch($request);
2731
        $this->assertEquals(200, $response->get_status());
2732
        $data = $response->get_data();
2733
        $this->assertTrue($data['deleted']);
2734
        $this->assertNotEmpty($data['previous']['post']);
2735
    }
2736
2737 View Code Duplication
    public function test_delete_item_already_trashed() 
2738
    {
2739
        wp_set_current_user(self::$admin_id);
2740
2741
        $comment_id = $this->factory->comment->create(
2742
            array(
2743
            'comment_approved' => 1,
2744
            'comment_post_ID'  => self::$post_id,
2745
            'user_id'          => self::$subscriber_id,
2746
            )
2747
        );
2748
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/comments/%d', $comment_id));
2749
        $response = $this->server->dispatch($request);
2750
        $this->assertEquals(200, $response->get_status());
2751
        $data = $response->get_data();
2752
        $response = $this->server->dispatch($request);
2753
        $this->assertErrorResponse('rest_already_trashed', $response, 410);
2754
    }
2755
2756 View Code Duplication
    public function test_delete_comment_invalid_id() 
2757
    {
2758
        wp_set_current_user(self::$admin_id);
2759
2760
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER));
2761
2762
        $response = $this->server->dispatch($request);
2763
        $this->assertErrorResponse('rest_comment_invalid_id', $response, 404);
2764
    }
2765
2766
    public function test_delete_comment_without_permission() 
2767
    {
2768
        wp_set_current_user(self::$subscriber_id);
2769
2770
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/comments/%d', self::$approved_id));
2771
2772
        $response = $this->server->dispatch($request);
2773
        $this->assertErrorResponse('rest_cannot_delete', $response, 403);
2774
    }
2775
2776
    public function test_delete_child_comment_link() 
2777
    {
2778
        wp_set_current_user(self::$admin_id);
2779
        $comment_id_1 = $this->factory->comment->create(
2780
            array(
2781
            'comment_approved' => 1,
2782
            'comment_post_ID'  => self::$post_id,
2783
            'user_id'          => self::$subscriber_id,
2784
            ) 
2785
        );
2786
2787
        $child_comment = $this->factory->comment->create(
2788
            array(
2789
            'comment_approved' => 1,
2790
            'comment_parent'   => $comment_id_1,
2791
            'comment_post_ID'  => self::$post_id,
2792
            'user_id'          => self::$subscriber_id,
2793
            ) 
2794
        );
2795
2796
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/comments/%s', $child_comment));
2797
        $response = $this->server->dispatch($request);
2798
        $this->assertEquals(200, $response->get_status());
2799
2800
        // Verify children link is gone.
2801
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%s', $comment_id_1));
2802
        $response = $this->server->dispatch($request);
2803
        $this->assertEquals(200, $response->get_status());
2804
        $this->assertArrayNotHasKey('children', $response->get_links());
2805
    }
2806
2807
    public function test_get_item_schema() 
2808
    {
2809
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/comments');
2810
        $response = $this->server->dispatch($request);
2811
        $data = $response->get_data();
2812
        $properties = $data['schema']['properties'];
2813
        $this->assertEquals(17, count($properties));
2814
        $this->assertArrayHasKey('id', $properties);
2815
        $this->assertArrayHasKey('author', $properties);
2816
        $this->assertArrayHasKey('author_avatar_urls', $properties);
2817
        $this->assertArrayHasKey('author_email', $properties);
2818
        $this->assertArrayHasKey('author_ip', $properties);
2819
        $this->assertArrayHasKey('author_name', $properties);
2820
        $this->assertArrayHasKey('author_url', $properties);
2821
        $this->assertArrayHasKey('author_user_agent', $properties);
2822
        $this->assertArrayHasKey('content', $properties);
2823
        $this->assertArrayHasKey('date', $properties);
2824
        $this->assertArrayHasKey('date_gmt', $properties);
2825
        $this->assertArrayHasKey('link', $properties);
2826
        $this->assertArrayHasKey('meta', $properties);
2827
        $this->assertArrayHasKey('parent', $properties);
2828
        $this->assertArrayHasKey('post', $properties);
2829
        $this->assertArrayHasKey('status', $properties);
2830
        $this->assertArrayHasKey('type', $properties);
2831
2832
        $this->assertEquals(0, $properties['parent']['default']);
2833
        $this->assertEquals(0, $properties['post']['default']);
2834
2835
        $this->assertEquals(true, $properties['link']['readonly']);
2836
        $this->assertEquals(true, $properties['type']['readonly']);
2837
    }
2838
2839 View Code Duplication
    public function test_get_item_schema_show_avatar() 
2840
    {
2841
        update_option('show_avatars', false);
2842
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/users');
2843
        $response = $this->server->dispatch($request);
2844
        $data = $response->get_data();
2845
        $properties = $data['schema']['properties'];
2846
2847
        $this->assertArrayNotHasKey('author_avatar_urls', $properties);
2848
    }
2849
2850
    public function test_get_additional_field_registration() 
2851
    {
2852
2853
        $schema = array(
2854
         'type'        => 'integer',
2855
         'description' => 'Some integer of mine',
2856
         'enum'        => array( 1, 2, 3, 4 ),
2857
         'context'     => array( 'view', 'edit' ),
2858
        );
2859
2860
        register_rest_field(
2861
            'comment', 'my_custom_int', array(
2862
            'schema'          => $schema,
2863
            'get_callback'    => array( $this, 'additional_field_get_callback' ),
2864
            'update_callback' => array( $this, 'additional_field_update_callback' ),
2865
            ) 
2866
        );
2867
2868
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/comments');
2869
2870
        $response = $this->server->dispatch($request);
2871
        $data = $response->get_data();
2872
2873
        $this->assertArrayHasKey('my_custom_int', $data['schema']['properties']);
2874
        $this->assertEquals($schema, $data['schema']['properties']['my_custom_int']);
2875
2876
        $request = new WP_REST_Request('GET', '/wp/v2/comments/' . self::$approved_id);
2877
2878
        $response = $this->server->dispatch($request);
2879
        $this->assertArrayHasKey('my_custom_int', $response->data);
2880
2881
        $request = new WP_REST_Request('POST', '/wp/v2/comments/' . self::$approved_id);
2882
        $request->set_body_params(
2883
            array(
2884
            'my_custom_int' => 123,
2885
            'content'       => 'abc',
2886
            )
2887
        );
2888
2889
        wp_set_current_user(1);
2890
        $this->server->dispatch($request);
2891
        $this->assertEquals(123, get_comment_meta(self::$approved_id, 'my_custom_int', true));
2892
2893
        $request = new WP_REST_Request('POST', '/wp/v2/comments');
2894
        $request->set_body_params(
2895
            array(
2896
            'my_custom_int' => 123,
2897
            'title'         => 'hello',
2898
            'content'       => 'goodbye',
2899
            'post'          => self::$post_id,
2900
            )
2901
        );
2902
2903
        $response = $this->server->dispatch($request);
2904
2905
        $this->assertEquals(123, $response->data['my_custom_int']);
2906
2907
        global $wp_rest_additional_fields;
2908
        $wp_rest_additional_fields = array();
2909
    }
2910
2911 View Code Duplication
    public function test_additional_field_update_errors() 
2912
    {
2913
        $schema = array(
2914
         'type'        => 'integer',
2915
         'description' => 'Some integer of mine',
2916
         'enum'        => array( 1, 2, 3, 4 ),
2917
         'context'     => array( 'view', 'edit' ),
2918
        );
2919
2920
        register_rest_field(
2921
            'comment', 'my_custom_int', array(
2922
            'schema'          => $schema,
2923
            'get_callback'    => array( $this, 'additional_field_get_callback' ),
2924
            'update_callback' => array( $this, 'additional_field_update_callback' ),
2925
            ) 
2926
        );
2927
2928
        wp_set_current_user(self::$admin_id);
2929
2930
        // Check for error on update.
2931
        $request = new WP_REST_Request('POST', sprintf('/wp/v2/comments/%d', self::$approved_id));
2932
        $request->set_body_params(
2933
            array(
2934
            'my_custom_int' => 'returnError',
2935
            'content' => 'abc',
2936
            )
2937
        );
2938
2939
        $response = $this->server->dispatch($request);
2940
2941
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
2942
2943
        global $wp_rest_additional_fields;
2944
        $wp_rest_additional_fields = array();
2945
    }
2946
2947
    public function additional_field_get_callback( $object ) 
2948
    {
2949
        return get_comment_meta($object['id'], 'my_custom_int', true);
2950
    }
2951
2952
    public function additional_field_update_callback( $value, $comment ) 
2953
    {
2954
        if ('returnError' === $value ) {
2955
            return new WP_Error('rest_invalid_param', 'Testing an error.', array( 'status' => 400 ));
2956
        }
2957
        update_comment_meta($comment->comment_ID, 'my_custom_int', $value);
2958
    }
2959
2960
    protected function check_comment_data( $data, $context, $links ) 
2961
    {
2962
        $comment = get_comment($data['id']);
2963
2964
        $this->assertEquals($comment->comment_ID, $data['id']);
2965
        $this->assertEquals($comment->comment_post_ID, $data['post']);
2966
        $this->assertEquals($comment->comment_parent, $data['parent']);
2967
        $this->assertEquals($comment->user_id, $data['author']);
2968
        $this->assertEquals($comment->comment_author, $data['author_name']);
2969
        $this->assertEquals($comment->comment_author_url, $data['author_url']);
2970
        $this->assertEquals(wpautop($comment->comment_content), $data['content']['rendered']);
2971
        $this->assertEquals(mysql_to_rfc3339($comment->comment_date), $data['date']);
2972
        $this->assertEquals(mysql_to_rfc3339($comment->comment_date_gmt), $data['date_gmt']);
2973
        $this->assertEquals(get_comment_link($comment), $data['link']);
2974
        $this->assertContains('author_avatar_urls', $data);
2975
        $this->assertEqualSets(
2976
            array(
2977
            'self',
2978
            'collection',
2979
            'up',
2980
            ), array_keys($links) 
2981
        );
2982
2983
        if ('edit' === $context ) {
2984
            $this->assertEquals($comment->comment_author_email, $data['author_email']);
2985
            $this->assertEquals($comment->comment_author_IP, $data['author_ip']);
2986
            $this->assertEquals($comment->comment_agent, $data['author_user_agent']);
2987
            $this->assertEquals($comment->comment_content, $data['content']['raw']);
2988
        }
2989
2990
        if ('edit' !== $context ) {
2991
            $this->assertArrayNotHasKey('author_email', $data);
2992
            $this->assertArrayNotHasKey('author_ip', $data);
2993
            $this->assertArrayNotHasKey('author_user_agent', $data);
2994
            $this->assertArrayNotHasKey('raw', $data['content']);
2995
        }
2996
    }
2997
}
2998