7
7
8
8
namespace Activitypub \Tests \Scheduler ;
9
9
10
+ use Activitypub \Collection \Outbox ;
11
+ use Activitypub \Comment ;
12
+
10
13
/**
11
14
* Test Comment scheduler class.
12
15
*
@@ -41,15 +44,15 @@ public function test_schedule_comment_activity_on_approval() {
41
44
'comment_approved ' => 0 ,
42
45
)
43
46
);
44
- $ activitpub_id = \ Activitypub \ Comment::generate_id ( $ comment_id );
47
+ $ activitpub_id = Comment::generate_id ( $ comment_id );
45
48
46
49
wp_set_comment_status ( $ comment_id , 'approve ' );
47
50
48
51
$ post = $ this ->get_latest_outbox_item ( $ activitpub_id );
49
52
$ id = \get_post_meta ( $ post ->ID , '_activitypub_object_id ' , true );
50
53
$ this ->assertSame ( $ activitpub_id , $ id );
51
54
52
- wp_delete_comment ( $ comment_id , true );
55
+ \ wp_delete_comment ( $ comment_id , true );
53
56
}
54
57
55
58
/**
@@ -63,13 +66,13 @@ public function test_schedule_comment_activity_on_insert() {
63
66
'comment_approved ' => 1 ,
64
67
)
65
68
);
66
- $ activitpub_id = \ Activitypub \ Comment::generate_id ( $ comment_id );
69
+ $ activitpub_id = Comment::generate_id ( $ comment_id );
67
70
68
71
$ post = $ this ->get_latest_outbox_item ( $ activitpub_id );
69
72
$ id = \get_post_meta ( $ post ->ID , '_activitypub_object_id ' , true );
70
73
$ this ->assertSame ( $ activitpub_id , $ id );
71
74
72
- wp_delete_comment ( $ comment_id , true );
75
+ \ wp_delete_comment ( $ comment_id , true );
73
76
}
74
77
75
78
/**
@@ -120,10 +123,106 @@ public function test_no_activity_scheduled( $comment_data ) {
120
123
}
121
124
122
125
$ comment_id = self ::factory ()->comment ->create ( $ comment_data );
123
- $ activitpub_id = \ Activitypub \ Comment::generate_id ( $ comment_id );
126
+ $ activitpub_id = Comment::generate_id ( $ comment_id );
124
127
125
128
$ this ->assertNull ( $ this ->get_latest_outbox_item ( $ activitpub_id ) );
126
129
127
- wp_delete_comment ( $ comment_id , true );
130
+ \wp_delete_comment ( $ comment_id , true );
131
+ }
132
+
133
+ /**
134
+ * Test scheduling Delete activity when comment is permanently deleted.
135
+ *
136
+ * @covers ::schedule_comment_delete_activity
137
+ */
138
+ public function test_schedule_comment_delete_activity () {
139
+ // Create a comment that gets federated.
140
+ $ comment_id = self ::factory ()->comment ->create (
141
+ array (
142
+ 'comment_post_ID ' => self ::$ comment_post_ID ,
143
+ 'user_id ' => self ::$ user_id ,
144
+ 'comment_approved ' => 1 ,
145
+ 'comment_meta ' => array (
146
+ 'activitypub_status ' => 'federated ' ,
147
+ ),
148
+ )
149
+ );
150
+
151
+ $ activitypub_id = Comment::generate_id ( $ comment_id );
152
+
153
+ // Permanently delete the comment - this should trigger a Delete activity.
154
+ \wp_delete_comment ( $ comment_id , true );
155
+
156
+ // Check if a Delete activity was created.
157
+ $ outbox_posts = \get_posts (
158
+ array (
159
+ 'post_type ' => Outbox::POST_TYPE ,
160
+ 'post_status ' => array ( 'publish ' , 'draft ' , 'pending ' , 'private ' ),
161
+ 'numberposts ' => 1 ,
162
+ 'meta_query ' => array ( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
163
+ array (
164
+ 'key ' => '_activitypub_object_id ' ,
165
+ 'value ' => $ activitypub_id ,
166
+ ),
167
+ array (
168
+ 'key ' => '_activitypub_activity_type ' ,
169
+ 'value ' => 'Delete ' ,
170
+ ),
171
+ ),
172
+ )
173
+ );
174
+
175
+ $ this ->assertCount ( 1 , $ outbox_posts , 'Should create exactly one Delete activity for permanently deleted federated comment ' );
176
+ $ outbox_post = $ outbox_posts [0 ];
177
+
178
+ // Verify the outbox post has correct metadata.
179
+ $ this ->assertEquals ( 'Delete ' , \get_post_meta ( $ outbox_post ->ID , '_activitypub_activity_type ' , true ) );
180
+ $ this ->assertEquals ( $ activitypub_id , \get_post_meta ( $ outbox_post ->ID , '_activitypub_object_id ' , true ) );
181
+ $ this ->assertEquals ( self ::$ user_id , $ outbox_post ->post_author );
182
+ }
183
+
184
+ /**
185
+ * Test that non-federated comments don't create Delete activities.
186
+ *
187
+ * @covers ::schedule_comment_delete_activity
188
+ */
189
+ public function test_no_delete_activity_for_non_federated_comment () {
190
+ // Create a comment that was NOT federated.
191
+ $ comment_id = self ::factory ()->comment ->create (
192
+ array (
193
+ 'comment_post_ID ' => self ::$ comment_post_ID ,
194
+ 'user_id ' => self ::$ user_id ,
195
+ 'comment_approved ' => 1 ,
196
+ )
197
+ );
198
+
199
+ $ activitypub_id = Comment::generate_id ( $ comment_id );
200
+
201
+ // Ensure this comment is NOT marked as sent.
202
+ \delete_comment_meta ( $ comment_id , 'activitypub_status ' );
203
+
204
+ // Permanently delete the comment.
205
+ \wp_delete_comment ( $ comment_id , true );
206
+
207
+ // Check that no Delete activity was created for this specific comment.
208
+ $ outbox_posts = \get_posts (
209
+ array (
210
+ 'post_type ' => Outbox::POST_TYPE ,
211
+ 'post_status ' => array ( 'publish ' , 'draft ' , 'pending ' , 'private ' ),
212
+ 'numberposts ' => 1 ,
213
+ 'meta_query ' => array ( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
214
+ array (
215
+ 'key ' => '_activitypub_object_id ' ,
216
+ 'value ' => $ activitypub_id ,
217
+ ),
218
+ array (
219
+ 'key ' => '_activitypub_activity_type ' ,
220
+ 'value ' => 'Delete ' ,
221
+ ),
222
+ ),
223
+ )
224
+ );
225
+
226
+ $ this ->assertEmpty ( $ outbox_posts , 'Should not create Delete activity for non-federated comment deletion ' );
128
227
}
129
228
}
0 commit comments