@@ -2511,4 +2511,216 @@ public function test_comment_objects_should_be_fetched_from_database_when_suspen
2511
2511
$ found = wp_list_pluck ( $ q ->comments , 'comment_ID ' );
2512
2512
$ this ->assertEqualSets ( array ( $ c ), $ found );
2513
2513
}
2514
+
2515
+ public function test_comment_query_should_be_cached () {
2516
+ global $ wpdb ;
2517
+
2518
+ $ q = new WP_Comment_Query ( array (
2519
+ 'post_id ' => self ::$ post_id ,
2520
+ 'fields ' => 'ids ' ,
2521
+ ) );
2522
+
2523
+ $ c = wp_insert_comment ( array (
2524
+ 'comment_author ' => 'Foo ' ,
2525
+ 'comment_author_email ' =>
'[email protected] ' ,
2526
+ 'comment_post_ID ' => self ::$ post_id ,
2527
+ ) );
2528
+
2529
+ $ num_queries = $ wpdb ->num_queries ;
2530
+ }
2531
+
2532
+ public function test_created_comment_should_invalidate_query_cache () {
2533
+ global $ wpdb ;
2534
+
2535
+ $ c = self ::factory ()->comment ->create ( array (
2536
+ 'comment_post_ID ' => self ::$ post_id ,
2537
+ 'comment_approved ' => '1 ' ,
2538
+ ) );
2539
+
2540
+ $ q = new WP_Comment_Query ( array (
2541
+ 'post_id ' => self ::$ post_id ,
2542
+ 'fields ' => 'ids ' ,
2543
+ ) );
2544
+
2545
+ $ num_queries = $ wpdb ->num_queries ;
2546
+
2547
+ $ q = new WP_Comment_Query ( array (
2548
+ 'post_id ' => self ::$ post_id ,
2549
+ 'fields ' => 'ids ' ,
2550
+ ) );
2551
+
2552
+ $ this ->assertSame ( $ num_queries , $ wpdb ->num_queries );
2553
+ $ this ->assertEqualSets ( array ( $ c ), $ q ->comments );
2554
+ }
2555
+
2556
+ public function test_updated_comment_should_invalidate_query_cache () {
2557
+ global $ wpdb ;
2558
+
2559
+ $ c = self ::factory ()->comment ->create ( array (
2560
+ 'comment_post_ID ' => self ::$ post_id ,
2561
+ 'comment_approved ' => '1 ' ,
2562
+ ) );
2563
+
2564
+ $ q = new WP_Comment_Query ( array (
2565
+ 'post_id ' => self ::$ post_id ,
2566
+ 'fields ' => 'ids ' ,
2567
+ ) );
2568
+
2569
+ wp_update_comment ( array (
2570
+ 'comment_ID ' => $ c ,
2571
+ 'comment_author ' => 'Foo ' ,
2572
+ 'comment_author_email ' =>
'[email protected] ' ,
2573
+ 'comment_post_ID ' => self ::$ post_id ,
2574
+ ) );
2575
+
2576
+ $ num_queries = $ wpdb ->num_queries ;
2577
+
2578
+ $ q = new WP_Comment_Query ( array (
2579
+ 'post_id ' => self ::$ post_id ,
2580
+ 'fields ' => 'ids ' ,
2581
+ ) );
2582
+
2583
+ $ num_queries ++;
2584
+ $ this ->assertSame ( $ num_queries , $ wpdb ->num_queries );
2585
+ $ this ->assertEqualSets ( array ( $ c ), $ q ->comments );
2586
+ }
2587
+
2588
+ public function test_deleted_comment_should_invalidate_query_cache () {
2589
+ global $ wpdb ;
2590
+
2591
+ $ c = self ::factory ()->comment ->create ( array (
2592
+ 'comment_post_ID ' => self ::$ post_id ,
2593
+ 'comment_approved ' => '1 ' ,
2594
+ ) );
2595
+
2596
+ $ q = new WP_Comment_Query ( array (
2597
+ 'post_id ' => self ::$ post_id ,
2598
+ 'fields ' => 'ids ' ,
2599
+ ) );
2600
+
2601
+ wp_delete_comment ( $ c );
2602
+
2603
+ $ num_queries = $ wpdb ->num_queries ;
2604
+
2605
+ $ q = new WP_Comment_Query ( array (
2606
+ 'post_id ' => self ::$ post_id ,
2607
+ 'fields ' => 'ids ' ,
2608
+ ) );
2609
+
2610
+ $ num_queries ++;
2611
+ $ this ->assertSame ( $ num_queries , $ wpdb ->num_queries );
2612
+ $ this ->assertEqualSets ( array (), $ q ->comments );
2613
+ }
2614
+
2615
+ public function test_trashed_comment_should_invalidate_query_cache () {
2616
+ global $ wpdb ;
2617
+
2618
+ $ c = self ::factory ()->comment ->create ( array (
2619
+ 'comment_post_ID ' => self ::$ post_id ,
2620
+ 'comment_approved ' => '1 ' ,
2621
+ ) );
2622
+
2623
+ $ q = new WP_Comment_Query ( array (
2624
+ 'post_id ' => self ::$ post_id ,
2625
+ 'fields ' => 'ids ' ,
2626
+ ) );
2627
+
2628
+ wp_trash_comment ( $ c );
2629
+
2630
+ $ num_queries = $ wpdb ->num_queries ;
2631
+
2632
+ $ q = new WP_Comment_Query ( array (
2633
+ 'post_id ' => self ::$ post_id ,
2634
+ 'fields ' => 'ids ' ,
2635
+ ) );
2636
+
2637
+ $ num_queries ++;
2638
+ $ this ->assertSame ( $ num_queries , $ wpdb ->num_queries );
2639
+ $ this ->assertEqualSets ( array (), $ q ->comments );
2640
+ }
2641
+
2642
+ public function test_untrashed_comment_should_invalidate_query_cache () {
2643
+ global $ wpdb ;
2644
+
2645
+ $ c = self ::factory ()->comment ->create ( array (
2646
+ 'comment_post_ID ' => self ::$ post_id ,
2647
+ 'comment_approved ' => '1 ' ,
2648
+ ) );
2649
+
2650
+ wp_trash_comment ( $ c );
2651
+
2652
+ $ q = new WP_Comment_Query ( array (
2653
+ 'post_id ' => self ::$ post_id ,
2654
+ 'fields ' => 'ids ' ,
2655
+ ) );
2656
+
2657
+ wp_untrash_comment ( $ c );
2658
+
2659
+ $ num_queries = $ wpdb ->num_queries ;
2660
+
2661
+ $ q = new WP_Comment_Query ( array (
2662
+ 'post_id ' => self ::$ post_id ,
2663
+ 'fields ' => 'ids ' ,
2664
+ ) );
2665
+
2666
+ $ num_queries ++;
2667
+ $ this ->assertSame ( $ num_queries , $ wpdb ->num_queries );
2668
+ $ this ->assertEqualSets ( array ( $ c ), $ q ->comments );
2669
+ }
2670
+
2671
+ public function test_spammed_comment_should_invalidate_query_cache () {
2672
+ global $ wpdb ;
2673
+
2674
+ $ c = self ::factory ()->comment ->create ( array (
2675
+ 'comment_post_ID ' => self ::$ post_id ,
2676
+ 'comment_approved ' => '1 ' ,
2677
+ ) );
2678
+
2679
+ $ q = new WP_Comment_Query ( array (
2680
+ 'post_id ' => self ::$ post_id ,
2681
+ 'fields ' => 'ids ' ,
2682
+ ) );
2683
+
2684
+ wp_spam_comment ( $ c );
2685
+
2686
+ $ num_queries = $ wpdb ->num_queries ;
2687
+
2688
+ $ q = new WP_Comment_Query ( array (
2689
+ 'post_id ' => self ::$ post_id ,
2690
+ 'fields ' => 'ids ' ,
2691
+ ) );
2692
+
2693
+ $ num_queries ++;
2694
+ $ this ->assertSame ( $ num_queries , $ wpdb ->num_queries );
2695
+ $ this ->assertEqualSets ( array (), $ q ->comments );
2696
+ }
2697
+
2698
+ public function test_unspammed_comment_should_invalidate_query_cache () {
2699
+ global $ wpdb ;
2700
+
2701
+ $ c = self ::factory ()->comment ->create ( array (
2702
+ 'comment_post_ID ' => self ::$ post_id ,
2703
+ 'comment_approved ' => '1 ' ,
2704
+ ) );
2705
+
2706
+ wp_spam_comment ( $ c );
2707
+
2708
+ $ q = new WP_Comment_Query ( array (
2709
+ 'post_id ' => self ::$ post_id ,
2710
+ 'fields ' => 'ids ' ,
2711
+ ) );
2712
+
2713
+ wp_unspam_comment ( $ c );
2714
+
2715
+ $ num_queries = $ wpdb ->num_queries ;
2716
+
2717
+ $ q = new WP_Comment_Query ( array (
2718
+ 'post_id ' => self ::$ post_id ,
2719
+ 'fields ' => 'ids ' ,
2720
+ ) );
2721
+
2722
+ $ num_queries ++;
2723
+ $ this ->assertSame ( $ num_queries , $ wpdb ->num_queries );
2724
+ $ this ->assertEqualSets ( array ( $ c ), $ q ->comments );
2725
+ }
2514
2726
}
0 commit comments