8
8
namespace Activitypub \Tests ;
9
9
10
10
use Activitypub \Mailer ;
11
+ use Activitypub \Collection \Actors ;
11
12
use Activitypub \Notification ;
12
13
use WP_UnitTestCase ;
13
14
@@ -210,21 +211,121 @@ public function test_init() {
210
211
$ this ->assertEquals ( 10 , has_action ( 'activitypub_notification_follow ' , array ( Mailer::class, 'new_follower ' ) ) );
211
212
}
212
213
214
+ /**
215
+ * Data provider for direct message notification.
216
+ *
217
+ * @return array
218
+ */
219
+ public function direct_message_provider () {
220
+ return array (
221
+ 'to ' => array (
222
+ true ,
223
+ array (
224
+ 'actor ' => 'https://example.com/author ' ,
225
+ 'object ' => array (
226
+ 'content ' => 'Test direct message ' ,
227
+ ),
228
+ 'to ' => array ( 'user_url ' ),
229
+ ),
230
+ ),
231
+ 'none ' => array (
232
+ false ,
233
+ array (
234
+ 'actor ' => 'https://example.com/author ' ,
235
+ 'object ' => array (
236
+ 'content ' => 'Test direct message ' ,
237
+ ),
238
+ ),
239
+ ),
240
+ 'public+reply ' => array (
241
+ false ,
242
+ array (
243
+ 'actor ' => 'https://example.com/author ' ,
244
+ 'object ' => array (
245
+ 'content ' => 'Test public reply ' ,
246
+ 'inReplyTo ' => 'https://example.com/post/1 ' ,
247
+ ),
248
+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
249
+ ),
250
+ ),
251
+ 'public+reply+cc ' => array (
252
+ false ,
253
+ array (
254
+ 'actor ' => 'https://example.com/author ' ,
255
+ 'object ' => array (
256
+ 'content ' => 'Test public reply ' ,
257
+ 'inReplyTo ' => 'https://example.com/post/1 ' ,
258
+ ),
259
+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
260
+ 'cc ' => array ( 'user_url ' ),
261
+ ),
262
+ ),
263
+ 'public+followers ' => array (
264
+ false ,
265
+ array (
266
+ 'actor ' => 'https://example.com/author ' ,
267
+ 'object ' => array (
268
+ 'content ' => 'Test public activity ' ,
269
+ 'inReplyTo ' => null ,
270
+ ),
271
+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
272
+ 'cc ' => array ( 'https://example.com/followers ' ),
273
+ ),
274
+ ),
275
+ 'followers ' => array (
276
+ false ,
277
+ array (
278
+ 'actor ' => 'https://example.com/author ' ,
279
+ 'object ' => array (
280
+ 'content ' => 'Test activity just to followers ' ,
281
+ 'inReplyTo ' => null ,
282
+ ),
283
+ 'to ' => array ( 'https://example.com/followers ' ),
284
+ ),
285
+ ),
286
+ 'reply+cc ' => array (
287
+ false ,
288
+ array (
289
+ 'actor ' => 'https://example.com/author ' ,
290
+ 'object ' => array (
291
+ 'content ' => 'Reply activity to me and to followers ' ,
292
+ 'inReplyTo ' => 'https://example.com/post/1 ' ,
293
+ ),
294
+ 'to ' => array ( 'https://example.com/followers ' ),
295
+ 'cc ' => array ( 'user_url ' ),
296
+ ),
297
+ ),
298
+ );
299
+ }
300
+
213
301
/**
214
302
* Test direct message notification.
215
303
*
304
+ * @param bool $send_email Whether email should be sent.
305
+ * @param array $activity Activity object.
306
+ * @dataProvider direct_message_provider
216
307
* @covers ::direct_message
217
308
*/
218
- public function test_direct_message () {
309
+ public function test_direct_message ( $ send_email , $ activity ) {
219
310
$ user_id = self ::$ user_id ;
220
311
$ mock = new \MockAction ();
221
312
222
- $ activity = array (
223
- 'actor ' => 'https://example.com/author ' ,
224
- 'object ' => array (
225
- 'content ' => 'Test direct message ' ,
226
- ),
227
- );
313
+ // We need to replace back in the user URL because the user_id is not available in the data provider.
314
+ $ replace = function ( $ url ) use ( $ user_id ) {
315
+ if ( 'user_url ' === $ url ) {
316
+ return Actors::get_by_id ( $ user_id )->get_id ();
317
+
318
+ }
319
+ return $ url ;
320
+ };
321
+
322
+ foreach ( $ activity as $ key => $ value ) {
323
+ if ( is_array ( $ value ) ) {
324
+ $ activity [ $ key ] = array_map ( $ replace , $ value );
325
+ } else {
326
+ $ activity [ $ key ] = $ replace ( $ value );
327
+ }
328
+ }
228
329
229
330
// Mock remote metadata.
230
331
add_filter (
@@ -238,69 +339,33 @@ function () {
238
339
);
239
340
add_filter ( 'wp_mail ' , array ( $ mock , 'filter ' ), 1 );
240
341
241
- // Capture email.
242
- add_filter (
243
- 'wp_mail ' ,
244
- function ( $ args ) use ( $ user_id ) {
245
- $ this ->assertStringContainsString ( 'Direct Message ' , $ args ['subject ' ] );
246
- $ this ->assertStringContainsString ( 'Test Sender ' , $ args ['subject ' ] );
247
- $ this ->assertStringContainsString ( 'Test direct message ' , $ args ['message ' ] );
248
- $ this ->assertStringContainsString ( 'https://example.com/author ' , $ args ['message ' ] );
249
- $ this ->assertEquals ( get_user_by ( 'id ' , $ user_id )->user_email , $ args ['to ' ] );
250
- return $ args ;
251
- }
252
- );
342
+ if ( $ send_email ) {
343
+ // Capture email.
344
+ add_filter (
345
+ 'wp_mail ' ,
346
+ function ( $ args ) use ( $ user_id , $ activity ) {
347
+ $ this ->assertStringContainsString ( 'Direct Message ' , $ args ['subject ' ] );
348
+ $ this ->assertStringContainsString ( 'Test Sender ' , $ args ['subject ' ] );
349
+ $ this ->assertStringContainsString ( $ activity ['object ' ]['content ' ], $ args ['message ' ] );
350
+ $ this ->assertStringContainsString ( 'https://example.com/author ' , $ args ['message ' ] );
351
+ $ this ->assertEquals ( get_user_by ( 'id ' , $ user_id )->user_email , $ args ['to ' ] );
352
+ return $ args ;
353
+ }
354
+ );
355
+ } else {
356
+ add_filter (
357
+ 'wp_mail ' ,
358
+ function ( $ args ) {
359
+ $ this ->fail ( 'Email should not be sent for public activity ' );
360
+ return $ args ;
361
+ }
362
+ );
363
+
364
+ }
253
365
254
366
Mailer::direct_message ( $ activity , $ user_id );
255
367
256
- // Test public activity (should not send email).
257
- $ public_activity = array (
258
- 'actor ' => 'https://example.com/author ' ,
259
- 'object ' => array (
260
- 'content ' => 'Test public reply ' ,
261
- 'inReplyTo ' => 'https://example.com/post/1 ' ,
262
- ),
263
- 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
264
- );
265
-
266
- // Reset email capture.
267
- remove_all_filters ( 'wp_mail ' );
268
- add_filter ( 'wp_mail ' , array ( $ mock , 'filter ' ), 1 );
269
- add_filter (
270
- 'wp_mail ' ,
271
- function ( $ args ) {
272
- $ this ->fail ( 'Email should not be sent for public activity ' );
273
- return $ args ;
274
- }
275
- );
276
-
277
- Mailer::direct_message ( $ public_activity , $ user_id );
278
-
279
- // Test public activity (should not send email).
280
- $ public_activity = array (
281
- 'actor ' => 'https://example.com/author ' ,
282
- 'object ' => array (
283
- 'content ' => 'Test public activity ' ,
284
- 'inReplyTo ' => null ,
285
- ),
286
- 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
287
- 'cc ' => array ( 'https://example.com/followers ' ),
288
- );
289
-
290
- // Reset email capture.
291
- remove_all_filters ( 'wp_mail ' );
292
- add_filter ( 'wp_mail ' , array ( $ mock , 'filter ' ), 1 );
293
- add_filter (
294
- 'wp_mail ' ,
295
- function ( $ args ) {
296
- $ this ->fail ( 'Email should not be sent for public activity ' );
297
- return $ args ;
298
- }
299
- );
300
-
301
- Mailer::direct_message ( $ public_activity , $ user_id );
302
-
303
- $ this ->assertEquals ( 1 , $ mock ->get_call_count () );
368
+ $ this ->assertEquals ( $ send_email ? 1 : 0 , $ mock ->get_call_count () );
304
369
305
370
// Clean up.
306
371
remove_all_filters ( 'pre_get_remote_metadata_by_actor ' );
@@ -343,6 +408,7 @@ public function test_direct_message_text( $text, $expected ) {
343
408
'object ' => array (
344
409
'content ' => $ text ,
345
410
),
411
+ 'to ' => array ( Actors::get_by_id ( $ user_id )->get_id () ),
346
412
);
347
413
348
414
// Mock remote metadata.
0 commit comments