Tests_Comment::try_sending_author_notification()   C
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 44
Code Lines 26

Duplication

Lines 16
Ratio 36.36 %

Importance

Changes 0
Metric Value
cc 8
eloc 26
nc 8
nop 2
dl 16
loc 44
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @group comment
5
 */
6
class Tests_Comment extends WP_UnitTestCase
7
{
8
    protected static $user_id;
9
    protected static $post_id;
10
11
    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...
12
    {
13
        parent::setUp();
14
        reset_phpmailer_instance();
15
    }
16
17
    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...
18
    {
19
        self::$user_id = $factory->user->create(
20
            array(
21
            'role'       => 'author',
22
            'user_login' => 'test_wp_user_get',
23
            'user_pass'  => 'password',
24
            'user_email' => '[email protected]',
25
            ) 
26
        );
27
28
        self::$post_id = $factory->post->create(
29
            array(
30
            'post_author' => self::$user_id
31
            ) 
32
        );
33
    }
34
35
    function test_wp_update_comment() 
36
    {
37
        $post = self::factory()->post->create_and_get(array( 'post_title' => 'some-post', 'post_type' => 'post' ));
38
        $post2 = self::factory()->post->create_and_get(array( 'post_title' => 'some-post-2', 'post_type' => 'post' ));
39
        $comments = self::factory()->comment->create_post_comments($post->ID, 5);
40
        $result = wp_update_comment(array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ));
41
        $this->assertEquals(1, $result);
42
        $comment = get_comment($comments[0]);
43
        $this->assertEquals($comments[1], $comment->comment_parent);
44
        $result = wp_update_comment(array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ));
45
        $this->assertEquals(0, $result);
46
        $result = wp_update_comment(array( 'comment_ID' => $comments[0], 'comment_post_ID' => $post2->ID ));
47
        $comment = get_comment($comments[0]);
48
        $this->assertEquals($post2->ID, $comment->comment_post_ID);
49
    }
50
51
    /**
52
     * @ticket 30627
53
     */
54 View Code Duplication
    function test_wp_update_comment_updates_comment_type() 
55
    {
56
        $comment_id = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id ));
57
58
        wp_update_comment(array( 'comment_ID' => $comment_id, 'comment_type' => 'pingback' ));
59
60
        $comment = get_comment($comment_id);
61
        $this->assertEquals('pingback', $comment->comment_type);
62
    }
63
64
    /**
65
     * @ticket 30307
66
     */
67 View Code Duplication
    function test_wp_update_comment_updates_user_id() 
68
    {
69
        $comment_id = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id ));
70
71
        wp_update_comment(array( 'comment_ID' => $comment_id, 'user_id' => 1 ));
72
73
        $comment = get_comment($comment_id);
74
        $this->assertEquals(1, $comment->user_id);
75
    }
76
77
    /**
78
     * @ticket 34954
79
     */
80 View Code Duplication
    function test_wp_update_comment_with_no_post_id() 
81
    {
82
        $comment_id = self::factory()->comment->create(array( 'comment_post_ID' => 0 ));
83
84
        $updated_comment_text = 'I should be able to update a comment with a Post ID of zero';
85
86
        $update = wp_update_comment(array( 'comment_ID' => $comment_id, 'comment_content' => $updated_comment_text, 'comment_post_ID' => 0 ));
87
        $this->assertSame(1, $update);
88
89
        $comment = get_comment($comment_id);
90
        $this->assertEquals($updated_comment_text, $comment->comment_content);
91
    }
92
93
    public function test_get_approved_comments() 
94
    {
95
        $ca1 = self::factory()->comment->create(
96
            array(
97
            'comment_post_ID' => self::$post_id, 'comment_approved' => '1'
98
            ) 
99
        );
100
        $ca2 = self::factory()->comment->create(
101
            array(
102
            'comment_post_ID' => self::$post_id, 'comment_approved' => '1'
103
            ) 
104
        );
105
        $ca3 = self::factory()->comment->create(
106
            array(
107
            'comment_post_ID' => self::$post_id, 'comment_approved' => '0'
108
            ) 
109
        );
110
        $c2 = self::factory()->comment->create(
111
            array(
112
            'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback'
113
            ) 
114
        );
115
        $c3 = self::factory()->comment->create(
116
            array(
117
            'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback'
118
            ) 
119
        );
120
        $c4 = self::factory()->comment->create(
121
            array(
122
            'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario'
123
            ) 
124
        );
125
        $c5 = self::factory()->comment->create(
126
            array(
127
            'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi'
128
            ) 
129
        );
130
131
        $found = get_approved_comments(self::$post_id);
132
133
        // all comments types will be returned
134
        $this->assertEquals(array( $ca1, $ca2, $c2, $c3, $c4, $c5 ), wp_list_pluck($found, 'comment_ID'));
135
    }
136
137
    /**
138
     * @ticket 30412
139
     */
140
    public function test_get_approved_comments_with_post_id_0_should_return_empty_array() 
141
    {
142
        $ca1 = self::factory()->comment->create(
143
            array(
144
            'comment_post_ID' => self::$post_id, 'comment_approved' => '1'
145
            ) 
146
        );
147
148
        $found = get_approved_comments(0);
149
150
        $this->assertSame(array(), $found);
151
    }
152
153
    /**
154
     * @ticket 14279
155
     */
156
    public function test_wp_new_comment_respects_dates() 
157
    {
158
        $data = array(
159
         'comment_post_ID' => self::$post_id,
160
         'comment_author' => 'Comment Author',
161
         'comment_author_url' => '',
162
         'comment_author_email' => '',
163
         'comment_type' => '',
164
         'comment_content' => 'Comment',
165
         'comment_date' => '2011-01-01 10:00:00',
166
         'comment_date_gmt' => '2011-01-01 10:00:00',
167
        );
168
169
        $id = wp_new_comment($data);
170
171
        $comment = get_comment($id);
172
173
        $this->assertEquals($data['comment_date'], $comment->comment_date);
174
        $this->assertEquals($data['comment_date_gmt'], $comment->comment_date_gmt);
175
    }
176
177
    /**
178
     * @ticket 14601
179
     */
180
    public function test_wp_new_comment_respects_author_ip() 
181
    {
182
        $data = array(
183
         'comment_post_ID'      => self::$post_id,
184
         'comment_author'       => 'Comment Author',
185
         'comment_author_IP'    => '192.168.1.1',
186
         'comment_author_url'   => '',
187
         'comment_author_email' => '',
188
         'comment_type'         => '',
189
         'comment_content'      => 'Comment',
190
        );
191
192
        $id = wp_new_comment($data);
193
194
        $comment = get_comment($id);
195
196
        $this->assertEquals($data['comment_author_IP'], $comment->comment_author_IP);
197
    }
198
199
    /**
200
     * @ticket 14601
201
     */
202
    public function test_wp_new_comment_respects_author_ip_empty_string() 
203
    {
204
        $data = array(
205
         'comment_post_ID'      => self::$post_id,
206
         'comment_author'       => 'Comment Author',
207
         'comment_author_IP'    => '',
208
         'comment_author_url'   => '',
209
         'comment_author_email' => '',
210
         'comment_type'         => '',
211
         'comment_content'      => 'Comment',
212
        );
213
214
        $id = wp_new_comment($data);
215
216
        $comment = get_comment($id);
217
218
        $this->assertEquals($data['comment_author_IP'], $comment->comment_author_IP);
219
    }
220
221
    /**
222
     * @ticket 14601
223
     */
224
    public function test_wp_new_comment_respects_comment_agent() 
225
    {
226
        $data = array(
227
         'comment_post_ID'      => self::$post_id,
228
         'comment_author'       => 'Comment Author',
229
         'comment_author_IP'    => '',
230
         'comment_author_url'   => '',
231
         'comment_author_email' => '',
232
         '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',
233
         'comment_type'         => '',
234
         'comment_content'      => 'Comment',
235
        );
236
237
        $id = wp_new_comment($data);
238
239
        $comment = get_comment($id);
240
241
        $this->assertEquals($data['comment_agent'], $comment->comment_agent);
242
    }
243
244
    /**
245
     * @ticket 14601
246
     */
247 View Code Duplication
    public function test_wp_new_comment_should_trim_provided_comment_agent_to_254_chars() 
248
    {
249
        $data = array(
250
         'comment_post_ID'      => self::$post_id,
251
         'comment_author'       => 'Comment Author',
252
         'comment_author_IP'    => '',
253
         'comment_author_url'   => '',
254
         'comment_author_email' => '',
255
         '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',
256
         'comment_type'         => '',
257
         'comment_content'      => 'Comment',
258
        );
259
260
        $id = wp_new_comment($data);
261
262
        $comment = get_comment($id);
263
264
        $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);
265
    }
266
267
    /**
268
     * @ticket 14601
269
     */
270
    public function test_wp_new_comment_respects_comment_agent_empty_string() 
271
    {
272
        $data = array(
273
         'comment_post_ID'      => self::$post_id,
274
         'comment_author'       => 'Comment Author',
275
         'comment_author_IP'    => '',
276
         'comment_author_url'   => '',
277
         'comment_author_email' => '',
278
         'comment_agent'        => '',
279
         'comment_type'         => '',
280
         'comment_content'      => 'Comment',
281
        );
282
283
        $id = wp_new_comment($data);
284
285
        $comment = get_comment($id);
286
287
        $this->assertEquals($data['comment_agent'], $comment->comment_agent);
288
    }
289
290
291 View Code Duplication
    public function test_comment_field_lengths() 
292
    {
293
        $data = array(
294
         'comment_post_ID' => self::$post_id,
295
         'comment_author' => 'Comment Author',
296
         'comment_author_url' => '',
297
         'comment_author_email' => '',
298
         'comment_type' => '',
299
         'comment_content' => str_repeat('A', 65536),
300
         'comment_date' => '2011-01-01 10:00:00',
301
         'comment_date_gmt' => '2011-01-01 10:00:00',
302
        );
303
304
        $id = wp_new_comment($data);
305
306
        $comment = get_comment($id);
307
308
        $this->assertEquals(strlen($comment->comment_content), 65535);
309
    }
310
311
    /**
312
     * @ticket 32566
313
     */
314
    public function test_wp_notify_moderator_should_not_throw_notice_when_post_author_is_0() 
315
    {
316
        $p = self::factory()->post->create(
317
            array(
318
            'post_author' => 0,
319
            ) 
320
        );
321
322
        $c = self::factory()->comment->create(
323
            array(
324
            'comment_post_ID' => $p,
325
            ) 
326
        );
327
328
        $this->assertTrue(wp_notify_moderator($c));
329
    }
330
331 View Code Duplication
    public function test_wp_new_comment_notify_postauthor_should_send_email_when_comment_is_approved() 
332
    {
333
        $c = self::factory()->comment->create(
334
            array(
335
            'comment_post_ID' => self::$post_id,
336
            ) 
337
        );
338
339
        $sent = wp_new_comment_notify_postauthor($c);
340
        $this->assertTrue($sent);
341
    }
342
343 View Code Duplication
    public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_is_unapproved() 
344
    {
345
        $c = self::factory()->comment->create(
346
            array(
347
            'comment_post_ID' => self::$post_id,
348
            'comment_approved' => '0',
349
            ) 
350
        );
351
352
        $sent = wp_new_comment_notify_postauthor($c);
353
        $this->assertFalse($sent);
354
    }
355
356
    /**
357
     * @ticket 33587
358
     */
359 View Code Duplication
    public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_marked_as_spam() 
360
    {
361
        $c = self::factory()->comment->create(
362
            array(
363
            'comment_post_ID' => self::$post_id,
364
            'comment_approved' => 'spam',
365
            ) 
366
        );
367
368
        $sent = wp_new_comment_notify_postauthor($c);
369
        $this->assertFalse($sent);
370
    }
371
372
    /**
373
     * @ticket 35006
374
     */
375 View Code Duplication
    public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_trashed() 
376
    {
377
        $c = self::factory()->comment->create(
378
            array(
379
            'comment_post_ID' => self::$post_id,
380
            'comment_approved' => 'trash',
381
            ) 
382
        );
383
384
        $sent = wp_new_comment_notify_postauthor($c);
385
        $this->assertFalse($sent);
386
    }
387
388
    /**
389
     * @ticket 12431
390
     */
391
    public function test_wp_new_comment_with_meta() 
392
    {
393
        $c = self::factory()->comment->create(
394
            array(
395
            'comment_approved' => '1',
396
            'comment_meta' => array(
397
            'food' => 'taco',
398
            'sauce' => 'fire'
399
            )
400
            ) 
401
        );
402
403
        $this->assertEquals('fire', get_comment_meta($c, 'sauce', true));
404
    }
405
406
    /**
407
     * @ticket 8071
408
     */
409
    public function test_wp_comment_get_children_should_fill_children() 
410
    {
411
        $c1 = self::factory()->comment->create(
412
            array(
413
            'comment_post_ID' => self::$post_id,
414
            'comment_approved' => '1',
415
            ) 
416
        );
417
418
        $c2 = self::factory()->comment->create(
419
            array(
420
            'comment_post_ID' => self::$post_id,
421
            'comment_approved' => '1',
422
            'comment_parent' => $c1,
423
            ) 
424
        );
425
426
        $c3 = self::factory()->comment->create(
427
            array(
428
            'comment_post_ID' => self::$post_id,
429
            'comment_approved' => '1',
430
            'comment_parent' => $c2,
431
            ) 
432
        );
433
434
        $c4 = self::factory()->comment->create(
435
            array(
436
            'comment_post_ID' => self::$post_id,
437
            'comment_approved' => '1',
438
            'comment_parent' => $c1,
439
            ) 
440
        );
441
442
        $c5 = self::factory()->comment->create(
443
            array(
444
            'comment_post_ID' => self::$post_id,
445
            'comment_approved' => '1',
446
            ) 
447
        );
448
449
        $c6 = self::factory()->comment->create(
450
            array(
451
            'comment_post_ID' => self::$post_id,
452
            'comment_approved' => '1',
453
            'comment_parent' => $c5,
454
            ) 
455
        );
456
457
        $comment = get_comment($c1);
458
        $children = $comment->get_children();
459
460
        // Direct descendants of $c1.
461
        $this->assertEqualSets(array( $c2, $c4 ), array_values(wp_list_pluck($children, 'comment_ID')));
462
463
        // Direct descendants of $c2.
464
        $this->assertEqualSets(array( $c3 ), array_values(wp_list_pluck($children[ $c2 ]->get_children(), 'comment_ID')));
465
    }
466
467
    /**
468
     * @ticket 27571
469
     */
470
    public function test_post_properties_should_be_lazyloaded() 
471
    {
472
        $c = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id ));
473
474
        $post = get_post(self::$post_id);
475
        $comment = get_comment($c);
476
477
        $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' );
478
479
        foreach ( $post_fields as $pf ) {
480
            $this->assertTrue(isset($comment->$pf), $pf);
481
            $this->assertSame($post->$pf, $comment->$pf, $pf);
482
        }
483
    }
484
485
486
    /**
487
     * Helper function to set up comment for 761 tests.
488
     *
489
     * @since  4.4.0
490
     * @access public
491
     */
492
    public function setup_notify_comment()
493
    {
494
        /**
495
         * Prevent flood alert from firing.
496
         */
497
        add_filter('comment_flood_filter', '__return_false');
498
499
        /**
500
         * Set up a comment for testing.
501
         */
502
        $post = $this->factory->post->create(
503
            array(
504
            'post_author' => self::$user_id,
505
            ) 
506
        );
507
508
        $comment = $this->factory->comment->create(
509
            array(
510
            'comment_post_ID' => $post,
511
            ) 
512
        );
513
514
        return array(
515
         'post'    => $post,
516
         'comment' => $comment,
517
        );
518
    }
519
520
    /**
521
     * @ticket 761
522
     */
523 View Code Duplication
    public function test_wp_notify_moderator_filter_moderation_notify_option_true_filter_false() 
524
    {
525
        $comment_data = $this->setup_notify_comment();
526
527
        /**
528
         * Test with moderator notification setting on, filter set to off.
529
         * Should not send a notification.
530
         */
531
        update_option('moderation_notify', 1);
532
        add_filter('notify_moderator', '__return_false');
533
534
        $notification_sent = $this->try_sending_moderator_notification($comment_data['comment'], $comment_data['post']);
535
536
        $this->assertFalse($notification_sent, 'Moderator notification setting on, filter set to off');
537
538
        remove_filter('notify_moderator', '__return_false');
539
        remove_filter('comment_flood_filter', '__return_false');
540
    }
541
542
    /**
543
     * @ticket 761
544
     */
545 View Code Duplication
    public function test_wp_notify_moderator_filter_moderation_notify_option_false_filter_true() 
546
    {
547
        $comment_data = $this->setup_notify_comment();
548
549
        /**
550
         * Test with moderator notification setting off, filter set to on.
551
         * Should send a notification.
552
         */
553
        update_option('moderation_notify', 0);
554
        add_filter('notify_moderator', '__return_true');
555
556
        $notification_sent = $this->try_sending_moderator_notification($comment_data['comment'], $comment_data['post']);
557
558
        $this->assertTrue($notification_sent, 'Moderator notification setting off, filter set to on');
559
560
        remove_filter('notify_moderator', '__return_true');
561
        remove_filter('comment_flood_filter', '__return_false');
562
    }
563
564
    /**
565
     * @ticket 761
566
     */
567 View Code Duplication
    public function test_wp_notify_post_author_filter_comments_notify_option_true_filter_false() 
568
    {
569
570
        $comment_data = $this->setup_notify_comment();
571
572
        /**
573
         * Test with author notification setting on, filter set to off.
574
         * Should not send a notification.
575
         */
576
        update_option('comments_notify', 1);
577
        add_filter('notify_post_author', '__return_false');
578
579
        $notification_sent = $this->try_sending_author_notification($comment_data['comment'], $comment_data['post']);
580
581
        $this->assertFalse($notification_sent, 'Test with author notification setting on, filter set to off');
582
583
        remove_filter('notify_post_author', '__return_false');
584
        remove_filter('comment_flood_filter', '__return_false');
585
    }
586
587
    /**
588
     * @ticket 761
589
     */
590 View Code Duplication
    public function test_wp_notify_post_author_filter_comments_notify_option_false_filter_true() 
591
    {
592
        $comment_data = $this->setup_notify_comment();
593
594
        /**
595
         * Test with author notification setting off, filter set to on.
596
         * Should send a notification.
597
         */
598
        update_option('comments_notify', 0);
599
        add_filter('notify_post_author', '__return_true');
600
601
        $notification_sent = $this->try_sending_author_notification($comment_data['comment'], $comment_data['post']);
602
603
        $this->assertTrue($notification_sent, 'Test with author notification setting off, filter set to on');
604
605
        remove_filter('notify_post_author', '__return_true');
606
        remove_filter('comment_flood_filter', '__return_false');
607
    }
608
609
    /**
610
     * Helper function to test moderator notifications.
611
     *
612
     * @since  4.4.0
613
     * @access public
614
     */
615
    public function try_sending_moderator_notification( $comment, $post ) 
616
    {
617
618
        // Don't approve comments, triggering notifications.
619
        add_filter('pre_comment_approved', '__return_false');
620
621
        // Moderators are notified when a new comment is added.
622
        $data = array(
623
         'comment_post_ID'      => $post,
624
         'comment_author'       => 'Comment Author',
625
         'comment_author_url'   => '',
626
         'comment_author_email' => '',
627
         'comment_type'         => '',
628
         'comment_content'      => 'Comment',
629
        );
630
        wp_new_comment($data);
631
632
        // Check to see if a notification email was sent to the moderator `[email protected]`.
633 View Code Duplication
        if (isset($GLOBALS['phpmailer']->mock_sent)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
634
            && ! empty($GLOBALS['phpmailer']->mock_sent)
635
            && WP_TESTS_EMAIL == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
636
        ) {
637
            $email_sent_when_comment_added = true;
638
            reset_phpmailer_instance();
639
        } else {
640
            $email_sent_when_comment_added = false;
641
        }
642
643
        return $email_sent_when_comment_added;
644
    }
645
646
    /**
647
     * Helper function to test sending author notifications.
648
     *
649
     * @since  4.4.0
650
     * @access public
651
     */
652
    public function try_sending_author_notification( $comment, $post ) 
653
    {
654
655
        // Approve comments, triggering notifications.
656
        add_filter('pre_comment_approved', '__return_true');
657
658
        // Post authors possibly notified when a comment is approved on their post.
659
        wp_set_comment_status($comment, 'approve');
660
661
        // Check to see if a notification email was sent to the post author `[email protected]`.
662 View Code Duplication
        if (isset($GLOBALS['phpmailer']->mock_sent)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
663
            && ! empty($GLOBALS['phpmailer']->mock_sent)
664
            && '[email protected]' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
665
        ) {
666
            $email_sent_when_comment_approved = true;
667
        } else {
668
            $email_sent_when_comment_approved = false;
669
        }
670
        reset_phpmailer_instance();
671
672
        // Post authors are notified when a new comment is added to their post.
673
        $data = array(
674
         'comment_post_ID'      => $post,
675
         'comment_author'       => 'Comment Author',
676
         'comment_author_url'   => '',
677
         'comment_author_email' => '',
678
         'comment_type'         => '',
679
         'comment_content'      => 'Comment',
680
        );
681
        wp_new_comment($data);
682
683
        // Check to see if a notification email was sent to the post author `[email protected]`.
684 View Code Duplication
        if (isset($GLOBALS['phpmailer']->mock_sent) 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
685
            && ! empty($GLOBALS['phpmailer']->mock_sent) 
686
            && '[email protected]' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] 
687
        ) {
688
            $email_sent_when_comment_added = true;
689
            reset_phpmailer_instance();
690
        } else {
691
            $email_sent_when_comment_added = false;
692
        }
693
694
        return $email_sent_when_comment_approved || $email_sent_when_comment_added;
695
    }
696
697
    public function test_close_comments_for_old_post() 
698
    {
699
        update_option('close_comments_for_old_posts', true);
700
        // Close comments more than one day old.
701
        update_option('close_comments_days_old', 1);
702
703
        $old_date = strtotime('-25 hours');
704
        $old_post_id = self::factory()->post->create(array( 'post_date' => strftime('%Y-%m-%d %H:%M:%S', $old_date) ));
705
706
        $old_post_comment_status = _close_comments_for_old_post(true, $old_post_id);
707
        $this->assertFalse($old_post_comment_status);
708
709
        $new_post_comment_status = _close_comments_for_old_post(true, self::$post_id);
710
        $this->assertTrue($new_post_comment_status);
711
    }
712
713
    public function test_close_comments_for_old_post_undated_draft() 
714
    {
715
        $draft_id = self::factory()->post->create(array( 'post_status' => 'draft', 'post_type' => 'post' ));
716
        $draft_comment_status = _close_comments_for_old_post(true, $draft_id);
717
718
        $this->assertTrue($draft_comment_status);
719
    }
720
721
    /**
722
     * @ticket 35276
723
     */
724
    public function test_wp_update_comment_author_id_and_agent() 
725
    {
726
727
        $default_data = array(
728
         'comment_post_ID'      => self::$post_id,
729
         'comment_author'       => 'Comment Author',
730
         'comment_author_IP'    => '192.168.0.1',
731
         'comment_agent'        => 'WRONG_AGENT',
732
         'comment_author_url'   => '',
733
         'comment_author_email' => '',
734
         'comment_type'         => '',
735
         'comment_content'      => 'Comment',
736
        );
737
738
        $comment_id = wp_new_comment($default_data);
739
740
        // Confirm that the IP and Agent are correct on initial save.
741
        $save = get_comment($comment_id);
742
        $this->assertSame($default_data['comment_author_IP'], $save->comment_author_IP);
743
        $this->assertSame($default_data['comment_agent'], $save->comment_agent);
744
745
        // Update the comment.
746
        wp_update_comment(
747
            array(
748
            'comment_ID'        => $comment_id,
749
            'comment_author_IP' => '111.111.1.1',
750
            'comment_agent'     => 'SHIELD_AGENT',
751
            ) 
752
        );
753
754
        // Retrieve and check the new values.
755
        $updated = get_comment($comment_id);
756
        $this->assertSame('111.111.1.1', $updated->comment_author_IP);
757
        $this->assertSame('SHIELD_AGENT', $updated->comment_agent);
758
    }
759
760
    public function test_wp_get_comment_fields_max_lengths() 
761
    {
762
        $expected = array(
763
         'comment_author'       => 245,
764
         'comment_author_email' => 100,
765
         'comment_author_url'   => 200,
766
         'comment_content'      => 65525,
767
        );
768
769
        $lengths = wp_get_comment_fields_max_lengths();
770
771
        foreach ( $lengths as $field => $length ) {
772
            $this->assertSame($expected[ $field ], $length);
773
        }
774
    }
775
776 View Code Duplication
    public function test_update_should_invalidate_comment_cache() 
777
    {
778
        global $wpdb;
779
780
        $c = self::factory()->comment->create(array( 'comment_author' => 'Foo' ));
781
782
        $comment = get_comment($c);
783
        $this->assertSame('Foo', $comment->comment_author);
784
785
        wp_update_comment(
786
            array(
787
            'comment_ID' => $c,
788
            'comment_author' => 'Bar',
789
            ) 
790
        );
791
792
        $comment = get_comment($c);
793
794
        $this->assertSame('Bar', $comment->comment_author);
795
    }
796
797 View Code Duplication
    public function test_trash_should_invalidate_comment_cache() 
798
    {
799
        global $wpdb;
800
801
        $c = self::factory()->comment->create();
802
803
        $comment = get_comment($c);
804
805
        wp_trash_comment($c);
806
807
        $comment = get_comment($c);
808
809
        $this->assertSame('trash', $comment->comment_approved);
810
    }
811
812 View Code Duplication
    public function test_untrash_should_invalidate_comment_cache() 
813
    {
814
        global $wpdb;
815
816
        $c = self::factory()->comment->create();
817
        wp_trash_comment($c);
818
819
        $comment = get_comment($c);
820
        $this->assertSame('trash', $comment->comment_approved);
821
822
        wp_untrash_comment($c);
823
824
        $comment = get_comment($c);
825
826
        $this->assertSame('1', $comment->comment_approved);
827
    }
828
829 View Code Duplication
    public function test_spam_should_invalidate_comment_cache() 
830
    {
831
        global $wpdb;
832
833
        $c = self::factory()->comment->create();
834
835
        $comment = get_comment($c);
836
837
        wp_spam_comment($c);
838
839
        $comment = get_comment($c);
840
841
        $this->assertSame('spam', $comment->comment_approved);
842
    }
843
844 View Code Duplication
    public function test_unspam_should_invalidate_comment_cache() 
845
    {
846
        global $wpdb;
847
848
        $c = self::factory()->comment->create();
849
        wp_spam_comment($c);
850
851
        $comment = get_comment($c);
852
        $this->assertSame('spam', $comment->comment_approved);
853
854
        wp_unspam_comment($c);
855
856
        $comment = get_comment($c);
857
858
        $this->assertSame('1', $comment->comment_approved);
859
    }
860
}
861