forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment.php
More file actions
1244 lines (1029 loc) · 35.6 KB
/
comment.php
File metadata and controls
1244 lines (1029 loc) · 35.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @group comment
*/
class Tests_Comment extends WP_UnitTestCase {
protected static $user_id;
protected static $post_id;
public function setUp() {
parent::setUp();
reset_phpmailer_instance();
}
public static function wpSetUpBeforeClass( $factory ) {
self::$user_id = $factory->user->create(
array(
'role' => 'author',
'user_login' => 'test_wp_user_get',
'user_pass' => 'password',
'user_email' => 'test@test.com',
)
);
self::$post_id = $factory->post->create(
array(
'post_author' => self::$user_id,
)
);
}
function test_wp_update_comment() {
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'some-post',
'post_type' => 'post',
)
);
$post2 = self::factory()->post->create_and_get(
array(
'post_title' => 'some-post-2',
'post_type' => 'post',
)
);
$comments = self::factory()->comment->create_post_comments( $post->ID, 5 );
$result = wp_update_comment(
array(
'comment_ID' => $comments[0],
'comment_parent' => $comments[1],
)
);
$this->assertEquals( 1, $result );
$comment = get_comment( $comments[0] );
$this->assertEquals( $comments[1], $comment->comment_parent );
$result = wp_update_comment(
array(
'comment_ID' => $comments[0],
'comment_parent' => $comments[1],
)
);
$this->assertEquals( 0, $result );
$result = wp_update_comment(
array(
'comment_ID' => $comments[0],
'comment_post_ID' => $post2->ID,
)
);
$comment = get_comment( $comments[0] );
$this->assertEquals( $post2->ID, $comment->comment_post_ID );
}
/**
* @ticket 30627
*/
function test_wp_update_comment_updates_comment_type() {
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_type' => 'pingback',
)
);
$comment = get_comment( $comment_id );
$this->assertEquals( 'pingback', $comment->comment_type );
}
/**
* @ticket 36784
*/
function test_wp_update_comment_updates_comment_meta() {
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_meta' => array(
'food' => 'taco',
'sauce' => 'fire',
),
)
);
$this->assertEquals( 'fire', get_comment_meta( $comment_id, 'sauce', true ) );
}
/**
* @ticket 30307
*/
function test_wp_update_comment_updates_user_id() {
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
wp_update_comment(
array(
'comment_ID' => $comment_id,
'user_id' => 1,
)
);
$comment = get_comment( $comment_id );
$this->assertEquals( 1, $comment->user_id );
}
/**
* @ticket 34954
*/
function test_wp_update_comment_with_no_post_id() {
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => 0 ) );
$updated_comment_text = 'I should be able to update a comment with a Post ID of zero';
$update = wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_content' => $updated_comment_text,
'comment_post_ID' => 0,
)
);
$this->assertSame( 1, $update );
$comment = get_comment( $comment_id );
$this->assertEquals( $updated_comment_text, $comment->comment_content );
}
public function test_get_approved_comments() {
$ca1 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$ca2 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$ca3 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '0',
)
);
$c2 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_type' => 'pingback',
)
);
$c3 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_type' => 'trackback',
)
);
$c4 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_type' => 'mario',
)
);
$c5 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_type' => 'luigi',
)
);
$found = get_approved_comments( self::$post_id );
// all comments types will be returned
$this->assertEquals( array( $ca1, $ca2, $c2, $c3, $c4, $c5 ), wp_list_pluck( $found, 'comment_ID' ) );
}
/**
* @ticket 30412
*/
public function test_get_approved_comments_with_post_id_0_should_return_empty_array() {
$ca1 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$found = get_approved_comments( 0 );
$this->assertSame( array(), $found );
}
/**
* @ticket 14279
*/
public function test_wp_new_comment_respects_dates() {
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => 'Comment',
'comment_date' => '2011-01-01 10:00:00',
'comment_date_gmt' => '2011-01-01 10:00:00',
);
$id = wp_new_comment( $data );
$comment = get_comment( $id );
$this->assertEquals( $data['comment_date'], $comment->comment_date );
$this->assertEquals( $data['comment_date_gmt'], $comment->comment_date_gmt );
}
/**
* @ticket 14601
*/
public function test_wp_new_comment_respects_author_ip() {
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_IP' => '192.168.1.1',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => 'Comment',
);
$id = wp_new_comment( $data );
$comment = get_comment( $id );
$this->assertEquals( $data['comment_author_IP'], $comment->comment_author_IP );
}
/**
* @ticket 14601
*/
public function test_wp_new_comment_respects_author_ip_empty_string() {
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_IP' => '',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => 'Comment',
);
$id = wp_new_comment( $data );
$comment = get_comment( $id );
$this->assertEquals( $data['comment_author_IP'], $comment->comment_author_IP );
}
/**
* @ticket 14601
*/
public function test_wp_new_comment_respects_comment_agent() {
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_IP' => '',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53',
'comment_type' => '',
'comment_content' => 'Comment',
);
$id = wp_new_comment( $data );
$comment = get_comment( $id );
$this->assertEquals( $data['comment_agent'], $comment->comment_agent );
}
/**
* @ticket 14601
*/
public function test_wp_new_comment_should_trim_provided_comment_agent_to_254_chars() {
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_IP' => '',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en; rv:1.8.1.4pre) Gecko/20070511 Camino/1.6pre',
'comment_type' => '',
'comment_content' => 'Comment',
);
$id = wp_new_comment( $data );
$comment = get_comment( $id );
$this->assertEquals( 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS ', $comment->comment_agent );
}
/**
* @ticket 14601
*/
public function test_wp_new_comment_respects_comment_agent_empty_string() {
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_IP' => '',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_agent' => '',
'comment_type' => '',
'comment_content' => 'Comment',
);
$id = wp_new_comment( $data );
$comment = get_comment( $id );
$this->assertEquals( $data['comment_agent'], $comment->comment_agent );
}
public function test_comment_field_lengths() {
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => str_repeat( 'A', 65536 ),
'comment_date' => '2011-01-01 10:00:00',
'comment_date_gmt' => '2011-01-01 10:00:00',
);
$id = wp_new_comment( $data );
$comment = get_comment( $id );
$this->assertEquals( strlen( $comment->comment_content ), 65535 );
}
/**
* @ticket 32566
*/
public function test_wp_notify_moderator_should_not_throw_notice_when_post_author_is_0() {
$p = self::factory()->post->create(
array(
'post_author' => 0,
)
);
$c = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
)
);
$this->assertTrue( wp_notify_moderator( $c ) );
}
public function test_wp_new_comment_notify_postauthor_should_send_email_when_comment_is_approved() {
$c = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
)
);
$sent = wp_new_comment_notify_postauthor( $c );
$this->assertTrue( $sent );
}
public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_is_unapproved() {
$c = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '0',
)
);
$sent = wp_new_comment_notify_postauthor( $c );
$this->assertFalse( $sent );
}
/**
* @ticket 33587
*/
public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_marked_as_spam() {
$c = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => 'spam',
)
);
$sent = wp_new_comment_notify_postauthor( $c );
$this->assertFalse( $sent );
}
/**
* @ticket 35006
*/
public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_trashed() {
$c = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => 'trash',
)
);
$sent = wp_new_comment_notify_postauthor( $c );
$this->assertFalse( $sent );
}
/**
* @ticket 12431
*/
public function test_wp_new_comment_with_meta() {
$c = self::factory()->comment->create(
array(
'comment_approved' => '1',
'comment_meta' => array(
'food' => 'taco',
'sauce' => 'fire',
),
)
);
$this->assertEquals( 'fire', get_comment_meta( $c, 'sauce', true ) );
}
/**
* @ticket 8071
*/
public function test_wp_comment_get_children_should_fill_children() {
$c1 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$c2 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c1,
)
);
$c3 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c2,
)
);
$c4 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c1,
)
);
$c5 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$c6 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c5,
)
);
$comment = get_comment( $c1 );
$children = $comment->get_children();
// Direct descendants of $c1.
$this->assertEqualSets( array( $c2, $c4 ), array_values( wp_list_pluck( $children, 'comment_ID' ) ) );
// Direct descendants of $c2.
$this->assertEqualSets( array( $c3 ), array_values( wp_list_pluck( $children[ $c2 ]->get_children(), 'comment_ID' ) ) );
}
/**
* @ticket 27571
*/
public function test_post_properties_should_be_lazyloaded() {
$c = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
$post = get_post( self::$post_id );
$comment = get_comment( $c );
$post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' );
foreach ( $post_fields as $pf ) {
$this->assertTrue( isset( $comment->$pf ), $pf );
$this->assertSame( $post->$pf, $comment->$pf, $pf );
}
}
/**
* Helper function to set up comment for 761 tests.
*
* @since 4.4.0
* @access public
*/
public function setup_notify_comment() {
/**
* Prevent flood alert from firing.
*/
add_filter( 'comment_flood_filter', '__return_false' );
/**
* Set up a comment for testing.
*/
$post = $this->factory->post->create(
array(
'post_author' => self::$user_id,
)
);
$comment = $this->factory->comment->create(
array(
'comment_post_ID' => $post,
)
);
return array(
'post' => $post,
'comment' => $comment,
);
}
/**
* @ticket 761
*/
public function test_wp_notify_moderator_filter_moderation_notify_option_true_filter_false() {
$comment_data = $this->setup_notify_comment();
/**
* Test with moderator notification setting on, filter set to off.
* Should not send a notification.
*/
update_option( 'moderation_notify', 1 );
add_filter( 'notify_moderator', '__return_false' );
$notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] );
$this->assertFalse( $notification_sent, 'Moderator notification setting on, filter set to off' );
remove_filter( 'notify_moderator', '__return_false' );
remove_filter( 'comment_flood_filter', '__return_false' );
}
/**
* @ticket 761
*/
public function test_wp_notify_moderator_filter_moderation_notify_option_false_filter_true() {
$comment_data = $this->setup_notify_comment();
/**
* Test with moderator notification setting off, filter set to on.
* Should send a notification.
*/
update_option( 'moderation_notify', 0 );
add_filter( 'notify_moderator', '__return_true' );
$notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] );
$this->assertTrue( $notification_sent, 'Moderator notification setting off, filter set to on' );
remove_filter( 'notify_moderator', '__return_true' );
remove_filter( 'comment_flood_filter', '__return_false' );
}
/**
* @ticket 761
*/
public function test_wp_notify_post_author_filter_comments_notify_option_true_filter_false() {
$comment_data = $this->setup_notify_comment();
/**
* Test with author notification setting on, filter set to off.
* Should not send a notification.
*/
update_option( 'comments_notify', 1 );
add_filter( 'notify_post_author', '__return_false' );
$notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] );
$this->assertFalse( $notification_sent, 'Test with author notification setting on, filter set to off' );
remove_filter( 'notify_post_author', '__return_false' );
remove_filter( 'comment_flood_filter', '__return_false' );
}
/**
* @ticket 761
*/
public function test_wp_notify_post_author_filter_comments_notify_option_false_filter_true() {
$comment_data = $this->setup_notify_comment();
/**
* Test with author notification setting off, filter set to on.
* Should send a notification.
*/
update_option( 'comments_notify', 0 );
add_filter( 'notify_post_author', '__return_true' );
$notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] );
$this->assertTrue( $notification_sent, 'Test with author notification setting off, filter set to on' );
remove_filter( 'notify_post_author', '__return_true' );
remove_filter( 'comment_flood_filter', '__return_false' );
}
/**
* Helper function to test moderator notifications.
*
* @since 4.4.0
* @access public
*/
public function try_sending_moderator_notification( $comment, $post ) {
// Don't approve comments, triggering notifications.
add_filter( 'pre_comment_approved', '__return_false' );
// Moderators are notified when a new comment is added.
$data = array(
'comment_post_ID' => $post,
'comment_author' => 'Comment Author',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => 'Comment',
);
wp_new_comment( $data );
// Check to see if a notification email was sent to the moderator `admin@example.org`.
if ( isset( $GLOBALS['phpmailer']->mock_sent )
&& ! empty( $GLOBALS['phpmailer']->mock_sent )
&& WP_TESTS_EMAIL == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
) {
$email_sent_when_comment_added = true;
reset_phpmailer_instance();
} else {
$email_sent_when_comment_added = false;
}
return $email_sent_when_comment_added;
}
/**
* Helper function to test sending author notifications.
*
* @since 4.4.0
* @access public
*/
public function try_sending_author_notification( $comment, $post ) {
// Approve comments, triggering notifications.
add_filter( 'pre_comment_approved', '__return_true' );
// Post authors possibly notified when a comment is approved on their post.
wp_set_comment_status( $comment, 'approve' );
// Check to see if a notification email was sent to the post author `test@test.com`.
if ( isset( $GLOBALS['phpmailer']->mock_sent )
&& ! empty( $GLOBALS['phpmailer']->mock_sent )
&& 'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
) {
$email_sent_when_comment_approved = true;
} else {
$email_sent_when_comment_approved = false;
}
reset_phpmailer_instance();
// Post authors are notified when a new comment is added to their post.
$data = array(
'comment_post_ID' => $post,
'comment_author' => 'Comment Author',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => 'Comment',
);
wp_new_comment( $data );
// Check to see if a notification email was sent to the post author `test@test.com`.
if ( isset( $GLOBALS['phpmailer']->mock_sent ) &&
! empty( $GLOBALS['phpmailer']->mock_sent ) &&
'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) {
$email_sent_when_comment_added = true;
reset_phpmailer_instance();
} else {
$email_sent_when_comment_added = false;
}
return $email_sent_when_comment_approved || $email_sent_when_comment_added;
}
public function test_close_comments_for_old_post() {
update_option( 'close_comments_for_old_posts', true );
// Close comments more than one day old.
update_option( 'close_comments_days_old', 1 );
$old_date = strtotime( '-25 hours' );
$old_post_id = self::factory()->post->create( array( 'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $old_date ) ) );
$old_post_comment_status = _close_comments_for_old_post( true, $old_post_id );
$this->assertFalse( $old_post_comment_status );
$new_post_comment_status = _close_comments_for_old_post( true, self::$post_id );
$this->assertTrue( $new_post_comment_status );
}
public function test_close_comments_for_old_post_undated_draft() {
$draft_id = self::factory()->post->create(
array(
'post_status' => 'draft',
'post_type' => 'post',
)
);
$draft_comment_status = _close_comments_for_old_post( true, $draft_id );
$this->assertTrue( $draft_comment_status );
}
/**
* @ticket 35276
*/
public function test_wp_update_comment_author_id_and_agent() {
$default_data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_IP' => '192.168.0.1',
'comment_agent' => 'WRONG_AGENT',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => 'Comment',
);
$comment_id = wp_new_comment( $default_data );
// Confirm that the IP and Agent are correct on initial save.
$save = get_comment( $comment_id );
$this->assertSame( $default_data['comment_author_IP'], $save->comment_author_IP );
$this->assertSame( $default_data['comment_agent'], $save->comment_agent );
// Update the comment.
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_author_IP' => '111.111.1.1',
'comment_agent' => 'SHIELD_AGENT',
)
);
// Retrieve and check the new values.
$updated = get_comment( $comment_id );
$this->assertSame( '111.111.1.1', $updated->comment_author_IP );
$this->assertSame( 'SHIELD_AGENT', $updated->comment_agent );
}
public function test_wp_get_comment_fields_max_lengths() {
$expected = array(
'comment_author' => 245,
'comment_author_email' => 100,
'comment_author_url' => 200,
'comment_content' => 65525,
);
$lengths = wp_get_comment_fields_max_lengths();
foreach ( $lengths as $field => $length ) {
$this->assertSame( $expected[ $field ], $length );
}
}
/**
* The `wp_comments_personal_data_eraser()` function should erase user's comments.
*
* @group privacy
* @ticket 43442
*/
public function test_wp_comments_personal_data_eraser() {
$post_id = self::factory()->post->create();
$user_id = self::factory()->user->create();
$args = array(
'user_id' => $user_id,
'comment_post_ID' => $post_id,
'comment_author' => 'Comment Author',
'comment_author_email' => 'personal@local.host',
'comment_author_url' => 'https://local.host/',
'comment_author_IP' => '192.168.0.1',
'comment_date' => '2018-04-14 17:20:00',
'comment_agent' => 'COMMENT_AGENT',
'comment_content' => 'Comment Content',
);
$comment_id = self::factory()->comment->create( $args );
wp_comments_personal_data_eraser( $args['comment_author_email'] );
$comment = get_comment( $comment_id );
$actual = array(
'comment_ID' => $comment->comment_ID,
'user_id' => $comment->user_id,
'comment_author' => $comment->comment_author,
'comment_author_email' => $comment->comment_author_email,
'comment_author_url' => $comment->comment_author_url,
'comment_author_IP' => $comment->comment_author_IP,
'comment_date' => $comment->comment_date,
'comment_date_gmt' => $comment->comment_date_gmt,
'comment_agent' => $comment->comment_agent,
'comment_content' => $comment->comment_content,
);
$expected = array(
'comment_ID' => (string) $comment_id,
'user_id' => '0', // Anonymized.
'comment_author' => 'Anonymous', // Anonymized.
'comment_author_email' => '', // Anonymized.
'comment_author_url' => '', // Anonymized.
'comment_author_IP' => '192.168.0.0', // Anonymized.
'comment_date' => '2018-04-14 17:20:00',
'comment_date_gmt' => '2018-04-14 17:20:00',
'comment_agent' => '', // Anonymized.
'comment_content' => 'Comment Content',
);
$this->assertSame( $expected, $actual );
}
/**
* Testing the `wp_comments_personal_data_eraser()` function's output on an empty first page.
*
* @group privacy
* @ticket 43442
*/
public function test_wp_comments_personal_data_eraser_empty_first_page_output() {
$actual = wp_comments_personal_data_eraser( 'nocommentsfound@local.host' );
$expected = array(
'items_removed' => false,
'items_retained' => false,
'messages' => array(),
'done' => true,
);
$this->assertSame( $expected, $actual );
}
/**
* Testing the `wp_comments_personal_data_eraser()` function's output, for the non-empty first page.
*
* @group privacy
* @ticket 43442
*/
public function test_wp_comments_personal_data_eraser_non_empty_first_page_output() {
$post_id = self::factory()->post->create();
$args = array(
'comment_post_ID' => $post_id,
'comment_author' => 'Comment Author',
'comment_author_email' => 'personal@local.host',
'comment_author_url' => 'https://local.host/',
'comment_author_IP' => '192.168.0.1',
'comment_date' => '2018-04-14 17:20:00',
'comment_agent' => 'COMMENT_AGENT',
'comment_content' => 'Comment Content',
);
self::factory()->comment->create( $args );
$actual = wp_comments_personal_data_eraser( $args['comment_author_email'] );
$expected = array(
'items_removed' => true,
'items_retained' => false,
'messages' => array(),
'done' => true,
);
$this->assertSame( $expected, $actual );
}
/**
* Testing the `wp_comments_personal_data_eraser()` function's output, for an empty second page.
*
* @group privacy
* @ticket 43442
*/
public function test_wp_comments_personal_data_eraser_empty_second_page_output() {
$post_id = self::factory()->post->create();
$args = array(
'comment_post_ID' => $post_id,
'comment_author' => 'Comment Author',
'comment_author_email' => 'personal@local.host',
'comment_author_url' => 'https://local.host/',
'comment_author_IP' => '192.168.0.1',
'comment_date' => '2018-04-14 17:20:00',
'comment_agent' => 'COMMENT_AGENT',
'comment_content' => 'Comment Content',
);
self::factory()->comment->create( $args );
$actual = wp_comments_personal_data_eraser( $args['comment_author_email'], 2 );
$expected = array(
'items_removed' => false,
'items_retained' => false,
'messages' => array(),
'done' => true,
);
$this->assertSame( $expected, $actual );
}
/**
* Testing the `wp_anonymize_comment` filter, to prevent comment anonymization.
*
* @group privacy
* @ticket 43442
*/
public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization() {
$post_id = self::factory()->post->create();
$args = array(
'comment_post_ID' => $post_id,
'comment_author' => 'Comment Author',
'comment_author_email' => 'personal@local.host',
'comment_author_url' => 'https://local.host/',
'comment_author_IP' => '192.168.0.1',
'comment_date' => '2018-04-14 17:20:00',
'comment_agent' => 'COMMENT_AGENT',
'comment_content' => 'Comment Content',
);
$comment_id = self::factory()->comment->create( $args );
add_filter( 'wp_anonymize_comment', '__return_false' );
$actual = wp_comments_personal_data_eraser( $args['comment_author_email'] );
remove_filter( 'wp_anonymize_comment', '__return_false' );
$message = sprintf( 'Comment %d contains personal data but could not be anonymized.', $comment_id );
$expected = array(
'items_removed' => false,
'items_retained' => true,
'messages' => array( $message ),
'done' => true,
);
$this->assertSame( $expected, $actual );
}
/**
* Testing the `wp_anonymize_comment` filter, to prevent comment anonymization, with a custom message.
*
* @group privacy
* @ticket 43442
*/
public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization_with_custom_message() {
$post_id = self::factory()->post->create();