24
24
namespace OCA \Spreed \Tests \php \Chat ;
25
25
26
26
use OCA \Spreed \Chat \Notifier ;
27
+ use OCA \Spreed \Exceptions \ParticipantNotFoundException ;
28
+ use OCA \Spreed \Manager ;
29
+ use OCA \Spreed \Room ;
27
30
use OCP \Comments \IComment ;
28
31
use OCP \Notification \IManager as INotificationManager ;
29
32
use OCP \Notification \INotification ;
@@ -37,6 +40,9 @@ class NotifierTest extends \Test\TestCase {
37
40
/** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */
38
41
protected $ userManager ;
39
42
43
+ /** @var \OCA\Spreed\Manager|\PHPUnit_Framework_MockObject_MockObject */
44
+ protected $ manager ;
45
+
40
46
/** @var \OCA\Spreed\Chat\Notifier */
41
47
protected $ notifier ;
42
48
@@ -56,8 +62,11 @@ public function setUp() {
56
62
return true ;
57
63
}));
58
64
65
+ $ this ->manager = $ this ->createMock (Manager::class);
66
+
59
67
$ this ->notifier = new Notifier ($ this ->notificationManager ,
60
- $ this ->userManager );
68
+ $ this ->userManager ,
69
+ $ this ->manager );
61
70
}
62
71
63
72
private function newComment ($ id , $ actorType , $ actorId , $ creationDateTime , $ message ) {
@@ -73,6 +82,7 @@ private function newComment($id, $actorType, $actorId, $creationDateTime, $messa
73
82
$ comment = $ this ->createMock (IComment::class);
74
83
75
84
$ comment ->method ('getId ' )->willReturn ($ id );
85
+ $ comment ->method ('getObjectId ' )->willReturn ('roomId ' );
76
86
$ comment ->method ('getActorType ' )->willReturn ($ actorType );
77
87
$ comment ->method ('getActorId ' )->willReturn ($ actorId );
78
88
$ comment ->method ('getCreationDateTime ' )->willReturn ($ creationDateTime );
@@ -127,6 +137,21 @@ public function testNotifyMentionedUsers() {
127
137
->with ($ comment ->getMessage ())
128
138
->willReturnSelf ();
129
139
140
+ $ room = $ this ->createMock (Room::class);
141
+ $ this ->manager ->expects ($ this ->once ())
142
+ ->method ('getRoomById ' )
143
+ ->with ('roomId ' )
144
+ ->willReturn ($ room );
145
+
146
+ $ room ->expects ($ this ->once ())
147
+ ->method ('getType ' )
148
+ ->willReturn (Room::ONE_TO_ONE_CALL );
149
+
150
+ $ room ->expects ($ this ->once ())
151
+ ->method ('getParticipant ' )
152
+ ->with ('anotherUser ' )
153
+ ->willReturn (true );
154
+
130
155
$ this ->notificationManager ->expects ($ this ->once ())
131
156
->method ('notify ' )
132
157
->with ($ notification );
@@ -153,6 +178,20 @@ public function testNotifyMentionedUsersByGuest() {
153
178
->with ($ comment ->getMessage ())
154
179
->willReturnSelf ();
155
180
181
+ $ room = $ this ->createMock (Room::class);
182
+ $ this ->manager ->expects ($ this ->once ())
183
+ ->method ('getRoomById ' )
184
+ ->with ('roomId ' )
185
+ ->willReturn ($ room );
186
+
187
+ $ room ->expects ($ this ->once ())
188
+ ->method ('getType ' )
189
+ ->willReturn (Room::PUBLIC_CALL );
190
+
191
+ $ room ->expects ($ this ->once ())
192
+ ->method ('hasPassword ' )
193
+ ->willReturn (false );
194
+
156
195
$ this ->notificationManager ->expects ($ this ->once ())
157
196
->method ('notify ' )
158
197
->with ($ notification );
@@ -180,6 +219,21 @@ public function testNotifyMentionedUsersWithLongMessageStartMention() {
180
219
->with ('123456789 @anotherUserWithOddLengthName 123456789-123456789-1234 ' , ['ellipsisEnd ' ])
181
220
->willReturnSelf ();
182
221
222
+ $ room = $ this ->createMock (Room::class);
223
+ $ this ->manager ->expects ($ this ->once ())
224
+ ->method ('getRoomById ' )
225
+ ->with ('roomId ' )
226
+ ->willReturn ($ room );
227
+
228
+ $ room ->expects ($ this ->once ())
229
+ ->method ('getType ' )
230
+ ->willReturn (Room::GROUP_CALL );
231
+
232
+ $ room ->expects ($ this ->once ())
233
+ ->method ('getParticipant ' )
234
+ ->with ('anotherUserWithOddLengthName ' )
235
+ ->willReturn (true );
236
+
183
237
$ this ->notificationManager ->expects ($ this ->once ())
184
238
->method ('notify ' )
185
239
->with ($ notification );
@@ -207,6 +261,21 @@ public function testNotifyMentionedUsersWithLongMessageMiddleMention() {
207
261
->with ('89-123456789-1234 @anotherUserWithOddLengthName 6789-123456789-1 ' , ['ellipsisStart ' , 'ellipsisEnd ' ])
208
262
->willReturnSelf ();
209
263
264
+ $ room = $ this ->createMock (Room::class);
265
+ $ this ->manager ->expects ($ this ->once ())
266
+ ->method ('getRoomById ' )
267
+ ->with ('roomId ' )
268
+ ->willReturn ($ room );
269
+
270
+ $ room ->expects ($ this ->once ())
271
+ ->method ('getType ' )
272
+ ->willReturn (Room::GROUP_CALL );
273
+
274
+ $ room ->expects ($ this ->once ())
275
+ ->method ('getParticipant ' )
276
+ ->with ('anotherUserWithOddLengthName ' )
277
+ ->willReturn (true );
278
+
210
279
$ this ->notificationManager ->expects ($ this ->once ())
211
280
->method ('notify ' )
212
281
->with ($ notification );
@@ -234,6 +303,21 @@ public function testNotifyMentionedUsersWithLongMessageEndMention() {
234
303
->with ('6789-123456789-123456789 @anotherUserWithOddLengthName 123456789 ' , ['ellipsisStart ' ])
235
304
->willReturnSelf ();
236
305
306
+ $ room = $ this ->createMock (Room::class);
307
+ $ this ->manager ->expects ($ this ->once ())
308
+ ->method ('getRoomById ' )
309
+ ->with ('roomId ' )
310
+ ->willReturn ($ room );
311
+
312
+ $ room ->expects ($ this ->once ())
313
+ ->method ('getType ' )
314
+ ->willReturn (Room::GROUP_CALL );
315
+
316
+ $ room ->expects ($ this ->once ())
317
+ ->method ('getParticipant ' )
318
+ ->with ('anotherUserWithOddLengthName ' )
319
+ ->willReturn (true );
320
+
237
321
$ this ->notificationManager ->expects ($ this ->once ())
238
322
->method ('notify ' )
239
323
->with ($ notification );
@@ -265,6 +349,112 @@ public function testNotifyMentionedUsersToUnknownUser() {
265
349
$ this ->notifier ->notifyMentionedUsers ($ comment );
266
350
}
267
351
352
+ public function testNotifyMentionedUsersToUserNotInvitedToPrivateChat () {
353
+ $ comment = $ this ->newComment (108 , 'users ' , 'testUser ' , new \DateTime ('@ ' . 1000000016 ), 'Mention @userNotInOneToOneChat ' );
354
+
355
+ $ this ->notificationManager ->expects ($ this ->never ())
356
+ ->method ('createNotification ' );
357
+
358
+ $ room = $ this ->createMock (Room::class);
359
+ $ this ->manager ->expects ($ this ->once ())
360
+ ->method ('getRoomById ' )
361
+ ->with ('roomId ' )
362
+ ->willReturn ($ room );
363
+
364
+ $ room ->expects ($ this ->once ())
365
+ ->method ('getType ' )
366
+ ->willReturn (Room::ONE_TO_ONE_CALL );
367
+
368
+ $ room ->expects ($ this ->once ())
369
+ ->method ('getParticipant ' )
370
+ ->with ('userNotInOneToOneChat ' )
371
+ ->will ($ this ->throwException (new ParticipantNotFoundException ()));
372
+
373
+ $ this ->notificationManager ->expects ($ this ->never ())
374
+ ->method ('createNotification ' );
375
+
376
+ $ this ->notificationManager ->expects ($ this ->never ())
377
+ ->method ('notify ' );
378
+
379
+ $ this ->notifier ->notifyMentionedUsers ($ comment );
380
+ }
381
+
382
+ public function testNotifyMentionedUsersToUserNotInvitedToPasswordProtectedPublicChat () {
383
+ $ comment = $ this ->newComment (108 , 'users ' , 'testUser ' , new \DateTime ('@ ' . 1000000016 ), 'Mention @userNotInvitedToPasswordProtectedPublicChat ' );
384
+
385
+ $ this ->notificationManager ->expects ($ this ->never ())
386
+ ->method ('createNotification ' );
387
+
388
+ $ room = $ this ->createMock (Room::class);
389
+ $ this ->manager ->expects ($ this ->once ())
390
+ ->method ('getRoomById ' )
391
+ ->with ('roomId ' )
392
+ ->willReturn ($ room );
393
+
394
+ $ room ->expects ($ this ->once ())
395
+ ->method ('getType ' )
396
+ ->willReturn (Room::PUBLIC_CALL );
397
+
398
+ $ room ->expects ($ this ->once ())
399
+ ->method ('hasPassword ' )
400
+ ->willReturn (true );
401
+
402
+ $ room ->expects ($ this ->once ())
403
+ ->method ('getParticipant ' )
404
+ ->with ('userNotInvitedToPasswordProtectedPublicChat ' )
405
+ ->will ($ this ->throwException (new ParticipantNotFoundException ()));
406
+
407
+ $ this ->notificationManager ->expects ($ this ->never ())
408
+ ->method ('notify ' );
409
+
410
+ $ this ->notifier ->notifyMentionedUsers ($ comment );
411
+ }
412
+
413
+ public function testNotifyMentionedUsersToUserInvitedToPasswordProtectedPublicChat () {
414
+ $ comment = $ this ->newComment (108 , 'users ' , 'testUser ' , new \DateTime ('@ ' . 1000000016 ), 'Mention @userInvitedToPasswordProtectedPublicChat ' );
415
+
416
+ $ notification = $ this ->newNotification ($ comment );
417
+
418
+ $ this ->notificationManager ->expects ($ this ->once ())
419
+ ->method ('createNotification ' )
420
+ ->willReturn ($ notification );
421
+
422
+ $ notification ->expects ($ this ->once ())
423
+ ->method ('setUser ' )
424
+ ->with ('userInvitedToPasswordProtectedPublicChat ' )
425
+ ->willReturnSelf ();
426
+
427
+ $ notification ->expects ($ this ->once ())
428
+ ->method ('setMessage ' )
429
+ ->with ($ comment ->getMessage ())
430
+ ->willReturnSelf ();
431
+
432
+ $ room = $ this ->createMock (Room::class);
433
+ $ this ->manager ->expects ($ this ->once ())
434
+ ->method ('getRoomById ' )
435
+ ->with ('roomId ' )
436
+ ->willReturn ($ room );
437
+
438
+ $ room ->expects ($ this ->once ())
439
+ ->method ('getType ' )
440
+ ->willReturn (Room::PUBLIC_CALL );
441
+
442
+ $ room ->expects ($ this ->once ())
443
+ ->method ('hasPassword ' )
444
+ ->willReturn (true );
445
+
446
+ $ room ->expects ($ this ->once ())
447
+ ->method ('getParticipant ' )
448
+ ->with ('userInvitedToPasswordProtectedPublicChat ' )
449
+ ->willReturn (true );
450
+
451
+ $ this ->notificationManager ->expects ($ this ->once ())
452
+ ->method ('notify ' )
453
+ ->with ($ notification );
454
+
455
+ $ this ->notifier ->notifyMentionedUsers ($ comment );
456
+ }
457
+
268
458
public function testNotifyMentionedUsersNoMentions () {
269
459
$ comment = $ this ->newComment (108 , 'users ' , 'testUser ' , new \DateTime ('@ ' . 1000000016 ), 'No mentions ' );
270
460
@@ -310,6 +500,20 @@ public function testNotifyMentionedUsersSeveralMentions() {
310
500
->with ('notherUser, and @unknownUser, and @testUser, and @userAbleToJoin ' )
311
501
->willReturnSelf ();
312
502
503
+ $ room = $ this ->createMock (Room::class);
504
+ $ this ->manager ->expects ($ this ->exactly (2 ))
505
+ ->method ('getRoomById ' )
506
+ ->with ('roomId ' )
507
+ ->willReturn ($ room );
508
+
509
+ $ room ->expects ($ this ->exactly (2 ))
510
+ ->method ('getType ' )
511
+ ->willReturn (Room::PUBLIC_CALL );
512
+
513
+ $ room ->expects ($ this ->exactly (2 ))
514
+ ->method ('hasPassword ' )
515
+ ->willReturn (false );
516
+
313
517
$ this ->notificationManager ->expects ($ this ->exactly (2 ))
314
518
->method ('notify ' )
315
519
->withConsecutive (
0 commit comments