77
88namespace Activitypub \Tests ;
99
10+ use Activitypub \Collection \Posts ;
1011use Activitypub \Comment ;
1112
1213/**
@@ -772,11 +773,11 @@ public function test_ap_post_comments_shown_on_frontend() {
772773 }
773774
774775 /**
775- * Test that ap_post comments are shown when querying for specific post.
776+ * Test that ap_post comments are hidden even when querying for specific post.
776777 *
777778 * @covers ::comment_query
778779 */
779- public function test_ap_post_comments_shown_when_querying_specific_post () {
780+ public function test_ap_post_comments_hidden_when_querying_specific_post () {
780781 // Create an ap_post.
781782 $ ap_post_id = wp_insert_post (
782783 array (
@@ -799,7 +800,7 @@ public function test_ap_post_comments_shown_when_querying_specific_post() {
799800 // Simulate admin context.
800801 \set_current_screen ( 'edit-comments ' );
801802
802- // Query comments for specific post - should include ap_post comments.
803+ // Query comments for specific post - should NOT include ap_post comments.
803804 $ query = new \WP_Comment_Query ();
804805 $ comments = $ query ->query (
805806 array (
@@ -808,7 +809,7 @@ public function test_ap_post_comments_shown_when_querying_specific_post() {
808809 );
809810
810811 $ comment_ids = wp_list_pluck ( $ comments , 'comment_ID ' );
811- $ this ->assertContains ( (string ) $ ap_comment_id , $ comment_ids , 'AP post comment should be shown when querying specific post ' );
812+ $ this ->assertNotContains ( (string ) $ ap_comment_id , $ comment_ids , 'AP post comment should be hidden even when querying specific post ' );
812813
813814 // Clean up.
814815 \set_current_screen ( 'front ' );
@@ -859,11 +860,11 @@ public function test_auto_approve_comments_on_ap_post_when_enabled() {
859860 }
860861
861862 /**
862- * Test not auto-approving comments on ap_post when option is disabled .
863+ * Test auto-approving comments on ap_post regardless of option .
863864 *
864865 * @covers ::pre_comment_approved
865866 */
866- public function test_no_auto_approve_comments_on_ap_post_when_disabled () {
867+ public function test_auto_approve_comments_on_ap_post_always () {
867868 // Disable flood control.
868869 \remove_action ( 'check_comment_flood ' , 'check_comment_flood_db ' , 10 );
869870
@@ -893,9 +894,9 @@ public function test_no_auto_approve_comments_on_ap_post_when_disabled() {
893894 )
894895 );
895896
896- // The comment should NOT be auto-approved.
897+ // The comment should be auto-approved on ap_post regardless of option .
897898 $ comment = \get_comment ( $ comment_id );
898- $ this ->assertEquals ( '0 ' , $ comment ->comment_approved , 'Comment on ap_post should not be auto-approved when option is disabled ' );
899+ $ this ->assertEquals ( '1 ' , $ comment ->comment_approved , 'Comment on ap_post should be auto-approved regardless of option ' );
899900
900901 // Clean up.
901902 \add_action ( 'check_comment_flood ' , 'check_comment_flood_db ' , 10 , 4 );
@@ -992,6 +993,195 @@ public function test_auto_approve_different_comment_types_on_ap_post() {
992993 \add_action ( 'check_comment_flood ' , 'check_comment_flood_db ' , 10 , 4 );
993994 }
994995
996+ /**
997+ * Test hide_for returns ap_post by default.
998+ *
999+ * @covers ::hide_for
1000+ */
1001+ public function test_hide_for () {
1002+ $ post_types = Comment::hide_for ();
1003+
1004+ $ this ->assertIsArray ( $ post_types );
1005+ $ this ->assertContains ( Posts::POST_TYPE , $ post_types , 'ap_post should be in the list of post types to hide comments for ' );
1006+ $ this ->assertCount ( 1 , $ post_types , 'Only ap_post should be in the default list ' );
1007+ }
1008+
1009+ /**
1010+ * Test hide_for filter can add post types.
1011+ *
1012+ * @covers ::hide_for
1013+ */
1014+ public function test_hide_for_filter_can_add_post_types () {
1015+ $ filter = function ( $ post_types ) {
1016+ $ post_types [] = 'custom_post_type ' ;
1017+ return $ post_types ;
1018+ };
1019+
1020+ \add_filter ( 'activitypub_hide_comments_for ' , $ filter );
1021+
1022+ $ post_types = Comment::hide_for ();
1023+
1024+ $ this ->assertContains ( 'custom_post_type ' , $ post_types , 'Filter should be able to add custom post types ' );
1025+ $ this ->assertContains ( Posts::POST_TYPE , $ post_types , 'ap_post should still be in the list ' );
1026+
1027+ \remove_filter ( 'activitypub_hide_comments_for ' , $ filter );
1028+ }
1029+
1030+ /**
1031+ * Test hide_for filter can remove post types.
1032+ *
1033+ * @covers ::hide_for
1034+ */
1035+ public function test_hide_for_filter_can_remove_post_types () {
1036+ $ filter = function ( $ post_types ) {
1037+ return array_diff ( $ post_types , array ( Posts::POST_TYPE ) );
1038+ };
1039+
1040+ \add_filter ( 'activitypub_hide_comments_for ' , $ filter );
1041+
1042+ $ post_types = Comment::hide_for ();
1043+
1044+ $ this ->assertNotContains ( Posts::POST_TYPE , $ post_types , 'Filter should be able to remove ap_post from the list ' );
1045+
1046+ \remove_filter ( 'activitypub_hide_comments_for ' , $ filter );
1047+ }
1048+
1049+ /**
1050+ * Test hide_for filter affects comment_query behavior.
1051+ *
1052+ * @covers ::hide_for
1053+ * @covers ::comment_query
1054+ */
1055+ public function test_hide_for_filter_affects_comment_query () {
1056+ // Register a custom post type for testing.
1057+ \register_post_type (
1058+ 'custom_hidden ' ,
1059+ array (
1060+ 'public ' => true ,
1061+ 'supports ' => array ( 'comments ' ),
1062+ )
1063+ );
1064+
1065+ // Create a custom post.
1066+ $ custom_post_id = wp_insert_post (
1067+ array (
1068+ 'post_type ' => 'custom_hidden ' ,
1069+ 'post_title ' => 'Custom Post ' ,
1070+ 'post_status ' => 'publish ' ,
1071+ )
1072+ );
1073+
1074+ // Create a comment on the custom post.
1075+ $ custom_comment_id = wp_insert_comment (
1076+ array (
1077+ 'comment_post_ID ' => $ custom_post_id ,
1078+ 'comment_content ' => 'Comment on custom post ' ,
1079+ 'comment_author ' => 'Test User ' ,
1080+ )
1081+ );
1082+
1083+ // Simulate admin context.
1084+ \set_current_screen ( 'edit-comments ' );
1085+
1086+ // Without filter, comment should be visible.
1087+ $ query = new \WP_Comment_Query ();
1088+ $ comments = $ query ->query ( array () );
1089+ $ comment_ids = wp_list_pluck ( $ comments , 'comment_ID ' );
1090+ $ this ->assertContains ( (string ) $ custom_comment_id , $ comment_ids , 'Custom post comment should be visible without filter ' );
1091+
1092+ // Add filter to hide custom_hidden post type.
1093+ $ filter = function ( $ post_types ) {
1094+ $ post_types [] = 'custom_hidden ' ;
1095+ return $ post_types ;
1096+ };
1097+ \add_filter ( 'activitypub_hide_comments_for ' , $ filter );
1098+
1099+ // With filter, comment should be hidden.
1100+ $ query = new \WP_Comment_Query ();
1101+ $ comments = $ query ->query ( array () );
1102+ $ comment_ids = wp_list_pluck ( $ comments , 'comment_ID ' );
1103+ $ this ->assertNotContains ( (string ) $ custom_comment_id , $ comment_ids , 'Custom post comment should be hidden with filter ' );
1104+
1105+ // Clean up.
1106+ \remove_filter ( 'activitypub_hide_comments_for ' , $ filter );
1107+ \set_current_screen ( 'front ' );
1108+ \unregister_post_type ( 'custom_hidden ' );
1109+ }
1110+
1111+ /**
1112+ * Test hide_for filter affects pre_comment_approved behavior.
1113+ *
1114+ * @covers ::hide_for
1115+ * @covers ::pre_comment_approved
1116+ */
1117+ public function test_hide_for_filter_affects_auto_approval () {
1118+ // Disable flood control.
1119+ \remove_action ( 'check_comment_flood ' , 'check_comment_flood_db ' , 10 );
1120+
1121+ // Register a custom post type for testing.
1122+ \register_post_type (
1123+ 'custom_hidden ' ,
1124+ array (
1125+ 'public ' => true ,
1126+ 'supports ' => array ( 'comments ' ),
1127+ )
1128+ );
1129+
1130+ // Create a custom post.
1131+ $ custom_post_id = self ::factory ()->post ->create (
1132+ array (
1133+ 'post_type ' => 'custom_hidden ' ,
1134+ 'post_status ' => 'publish ' ,
1135+ )
1136+ );
1137+
1138+ // Without filter, comment should NOT be auto-approved.
1139+ $ comment_id = \wp_new_comment (
1140+ array (
1141+ 'comment_type ' => 'comment ' ,
1142+ 'comment_content ' => 'Comment without filter. ' ,
1143+ 'comment_author ' => 'Test User ' ,
1144+ 'comment_author_url ' => 'https://example.com/@testuser ' ,
1145+ 'comment_post_ID ' => $ custom_post_id ,
1146+ 'comment_author_email ' => '' ,
1147+ 'comment_meta ' => array (
1148+ 'protocol ' => 'activitypub ' ,
1149+ ),
1150+ )
1151+ );
1152+ $ comment = \get_comment ( $ comment_id );
1153+ $ this ->assertEquals ( '0 ' , $ comment ->comment_approved , 'Comment should not be auto-approved without filter ' );
1154+
1155+ // Add filter to include custom_hidden in hide_for list.
1156+ $ filter = function ( $ post_types ) {
1157+ $ post_types [] = 'custom_hidden ' ;
1158+ return $ post_types ;
1159+ };
1160+ \add_filter ( 'activitypub_hide_comments_for ' , $ filter );
1161+
1162+ // With filter, comment should be auto-approved.
1163+ $ comment_id_2 = \wp_new_comment (
1164+ array (
1165+ 'comment_type ' => 'comment ' ,
1166+ 'comment_content ' => 'Comment with filter. ' ,
1167+ 'comment_author ' => 'Test User 2 ' ,
1168+ 'comment_author_url ' => 'https://example.com/@testuser2 ' ,
1169+ 'comment_post_ID ' => $ custom_post_id ,
1170+ 'comment_author_email ' => '' ,
1171+ 'comment_meta ' => array (
1172+ 'protocol ' => 'activitypub ' ,
1173+ ),
1174+ )
1175+ );
1176+ $ comment_2 = \get_comment ( $ comment_id_2 );
1177+ $ this ->assertEquals ( '1 ' , $ comment_2 ->comment_approved , 'Comment should be auto-approved with filter ' );
1178+
1179+ // Clean up.
1180+ \remove_filter ( 'activitypub_hide_comments_for ' , $ filter );
1181+ \unregister_post_type ( 'custom_hidden ' );
1182+ \add_action ( 'check_comment_flood ' , 'check_comment_flood_db ' , 10 , 4 );
1183+ }
1184+
9951185 /**
9961186 * Test that multiple ap_post comments are excluded while regular comments remain.
9971187 *
0 commit comments