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
@@ -222,21 +223,121 @@ public function test_init() {
222
223
delete_option ( 'activitypub_mailer_new_dm ' );
223
224
}
224
225
226
+ /**
227
+ * Data provider for direct message notification.
228
+ *
229
+ * @return array
230
+ */
231
+ public function direct_message_provider () {
232
+ return array (
233
+ 'to ' => array (
234
+ true ,
235
+ array (
236
+ 'actor ' => 'https://example.com/author ' ,
237
+ 'object ' => array (
238
+ 'content ' => 'Test direct message ' ,
239
+ ),
240
+ 'to ' => array ( 'user_url ' ),
241
+ ),
242
+ ),
243
+ 'none ' => array (
244
+ false ,
245
+ array (
246
+ 'actor ' => 'https://example.com/author ' ,
247
+ 'object ' => array (
248
+ 'content ' => 'Test direct message ' ,
249
+ ),
250
+ ),
251
+ ),
252
+ 'public+reply ' => array (
253
+ false ,
254
+ array (
255
+ 'actor ' => 'https://example.com/author ' ,
256
+ 'object ' => array (
257
+ 'content ' => 'Test public reply ' ,
258
+ 'inReplyTo ' => 'https://example.com/post/1 ' ,
259
+ ),
260
+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
261
+ ),
262
+ ),
263
+ 'public+reply+cc ' => array (
264
+ false ,
265
+ array (
266
+ 'actor ' => 'https://example.com/author ' ,
267
+ 'object ' => array (
268
+ 'content ' => 'Test public reply ' ,
269
+ 'inReplyTo ' => 'https://example.com/post/1 ' ,
270
+ ),
271
+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
272
+ 'cc ' => array ( 'user_url ' ),
273
+ ),
274
+ ),
275
+ 'public+followers ' => array (
276
+ false ,
277
+ array (
278
+ 'actor ' => 'https://example.com/author ' ,
279
+ 'object ' => array (
280
+ 'content ' => 'Test public activity ' ,
281
+ 'inReplyTo ' => null ,
282
+ ),
283
+ 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
284
+ 'cc ' => array ( 'https://example.com/followers ' ),
285
+ ),
286
+ ),
287
+ 'followers ' => array (
288
+ false ,
289
+ array (
290
+ 'actor ' => 'https://example.com/author ' ,
291
+ 'object ' => array (
292
+ 'content ' => 'Test activity just to followers ' ,
293
+ 'inReplyTo ' => null ,
294
+ ),
295
+ 'to ' => array ( 'https://example.com/followers ' ),
296
+ ),
297
+ ),
298
+ 'reply+cc ' => array (
299
+ false ,
300
+ array (
301
+ 'actor ' => 'https://example.com/author ' ,
302
+ 'object ' => array (
303
+ 'content ' => 'Reply activity to me and to followers ' ,
304
+ 'inReplyTo ' => 'https://example.com/post/1 ' ,
305
+ ),
306
+ 'to ' => array ( 'https://example.com/followers ' ),
307
+ 'cc ' => array ( 'user_url ' ),
308
+ ),
309
+ ),
310
+ );
311
+ }
312
+
225
313
/**
226
314
* Test direct message notification.
227
315
*
316
+ * @param bool $send_email Whether email should be sent.
317
+ * @param array $activity Activity object.
318
+ * @dataProvider direct_message_provider
228
319
* @covers ::direct_message
229
320
*/
230
- public function test_direct_message () {
321
+ public function test_direct_message ( $ send_email , $ activity ) {
231
322
$ user_id = self ::$ user_id ;
232
323
$ mock = new \MockAction ();
233
324
234
- $ activity = array (
235
- 'actor ' => 'https://example.com/author ' ,
236
- 'object ' => array (
237
- 'content ' => 'Test direct message ' ,
238
- ),
239
- );
325
+ // We need to replace back in the user URL because the user_id is not available in the data provider.
326
+ $ replace = function ( $ url ) use ( $ user_id ) {
327
+ if ( 'user_url ' === $ url ) {
328
+ return Actors::get_by_id ( $ user_id )->get_id ();
329
+
330
+ }
331
+ return $ url ;
332
+ };
333
+
334
+ foreach ( $ activity as $ key => $ value ) {
335
+ if ( is_array ( $ value ) ) {
336
+ $ activity [ $ key ] = array_map ( $ replace , $ value );
337
+ } else {
338
+ $ activity [ $ key ] = $ replace ( $ value );
339
+ }
340
+ }
240
341
241
342
// Mock remote metadata.
242
343
add_filter (
@@ -250,69 +351,33 @@ function () {
250
351
);
251
352
add_filter ( 'wp_mail ' , array ( $ mock , 'filter ' ), 1 );
252
353
253
- // Capture email.
254
- add_filter (
255
- 'wp_mail ' ,
256
- function ( $ args ) use ( $ user_id ) {
257
- $ this ->assertStringContainsString ( 'Direct Message ' , $ args ['subject ' ] );
258
- $ this ->assertStringContainsString ( 'Test Sender ' , $ args ['subject ' ] );
259
- $ this ->assertStringContainsString ( 'Test direct message ' , $ args ['message ' ] );
260
- $ this ->assertStringContainsString ( 'https://example.com/author ' , $ args ['message ' ] );
261
- $ this ->assertEquals ( get_user_by ( 'id ' , $ user_id )->user_email , $ args ['to ' ] );
262
- return $ args ;
263
- }
264
- );
354
+ if ( $ send_email ) {
355
+ // Capture email.
356
+ add_filter (
357
+ 'wp_mail ' ,
358
+ function ( $ args ) use ( $ user_id , $ activity ) {
359
+ $ this ->assertStringContainsString ( 'Direct Message ' , $ args ['subject ' ] );
360
+ $ this ->assertStringContainsString ( 'Test Sender ' , $ args ['subject ' ] );
361
+ $ this ->assertStringContainsString ( $ activity ['object ' ]['content ' ], $ args ['message ' ] );
362
+ $ this ->assertStringContainsString ( 'https://example.com/author ' , $ args ['message ' ] );
363
+ $ this ->assertEquals ( get_user_by ( 'id ' , $ user_id )->user_email , $ args ['to ' ] );
364
+ return $ args ;
365
+ }
366
+ );
367
+ } else {
368
+ add_filter (
369
+ 'wp_mail ' ,
370
+ function ( $ args ) {
371
+ $ this ->fail ( 'Email should not be sent for public activity ' );
372
+ return $ args ;
373
+ }
374
+ );
375
+
376
+ }
265
377
266
378
Mailer::direct_message ( $ activity , $ user_id );
267
379
268
- // Test public activity (should not send email).
269
- $ public_activity = array (
270
- 'actor ' => 'https://example.com/author ' ,
271
- 'object ' => array (
272
- 'content ' => 'Test public reply ' ,
273
- 'inReplyTo ' => 'https://example.com/post/1 ' ,
274
- ),
275
- 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
276
- );
277
-
278
- // Reset email capture.
279
- remove_all_filters ( 'wp_mail ' );
280
- add_filter ( 'wp_mail ' , array ( $ mock , 'filter ' ), 1 );
281
- add_filter (
282
- 'wp_mail ' ,
283
- function ( $ args ) {
284
- $ this ->fail ( 'Email should not be sent for public activity ' );
285
- return $ args ;
286
- }
287
- );
288
-
289
- Mailer::direct_message ( $ public_activity , $ user_id );
290
-
291
- // Test public activity (should not send email).
292
- $ public_activity = array (
293
- 'actor ' => 'https://example.com/author ' ,
294
- 'object ' => array (
295
- 'content ' => 'Test public activity ' ,
296
- 'inReplyTo ' => null ,
297
- ),
298
- 'to ' => array ( 'https://www.w3.org/ns/activitystreams#Public ' ),
299
- 'cc ' => array ( 'https://example.com/followers ' ),
300
- );
301
-
302
- // Reset email capture.
303
- remove_all_filters ( 'wp_mail ' );
304
- add_filter ( 'wp_mail ' , array ( $ mock , 'filter ' ), 1 );
305
- add_filter (
306
- 'wp_mail ' ,
307
- function ( $ args ) {
308
- $ this ->fail ( 'Email should not be sent for public activity ' );
309
- return $ args ;
310
- }
311
- );
312
-
313
- Mailer::direct_message ( $ public_activity , $ user_id );
314
-
315
- $ this ->assertEquals ( 1 , $ mock ->get_call_count () );
380
+ $ this ->assertEquals ( $ send_email ? 1 : 0 , $ mock ->get_call_count () );
316
381
317
382
// Clean up.
318
383
remove_all_filters ( 'pre_get_remote_metadata_by_actor ' );
@@ -355,6 +420,7 @@ public function test_direct_message_text( $text, $expected ) {
355
420
'object ' => array (
356
421
'content ' => $ text ,
357
422
),
423
+ 'to ' => array ( Actors::get_by_id ( $ user_id )->get_id () ),
358
424
);
359
425
360
426
// Mock remote metadata.
0 commit comments