forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_test.php
6271 lines (5235 loc) · 269 KB
/
api_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_message;
use core_message\tests\helper as testhelper;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
/**
* Test message API.
*
* @package core_message
* @category test
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api_test extends messagelib_test {
public function test_mark_all_read_for_user_touser() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
$this->assertEquals(1, api::count_unread_conversations($recipient));
api::mark_all_notifications_as_read($recipient->id);
api::mark_all_messages_as_read($recipient->id);
// We've marked all of recipients conversation messages as read.
$this->assertEquals(0, api::count_unread_conversations($recipient));
}
public function test_mark_all_read_for_user_touser_with_fromuser() {
$sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_message($sender1, $recipient, 'Notification', 1);
$this->send_fake_message($sender1, $recipient, 'Notification', 1);
$this->send_fake_message($sender1, $recipient, 'Notification', 1);
$this->send_fake_message($sender1, $recipient);
$this->send_fake_message($sender1, $recipient);
$this->send_fake_message($sender1, $recipient);
$this->send_fake_message($sender2, $recipient, 'Notification', 1);
$this->send_fake_message($sender2, $recipient, 'Notification', 1);
$this->send_fake_message($sender2, $recipient, 'Notification', 1);
$this->send_fake_message($sender2, $recipient);
$this->send_fake_message($sender2, $recipient);
$this->send_fake_message($sender2, $recipient);
$this->assertEquals(2, api::count_unread_conversations($recipient));
api::mark_all_notifications_as_read($recipient->id, $sender1->id);
$conversationid = api::get_conversation_between_users([$recipient->id, $sender1->id]);
api::mark_all_messages_as_read($recipient->id, $conversationid);
// We've marked only the conversation with sender1 messages as read.
$this->assertEquals(1, api::count_unread_conversations($recipient));
}
/**
* Test count_blocked_users.
*/
public function test_count_blocked_users() {
global $USER;
// Set this user as the admin.
$this->setAdminUser();
// Create user to add to the admin's block list.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$this->assertEquals(0, api::count_blocked_users());
// Add 1 blocked user to admin's blocked user list.
api::block_user($USER->id, $user1->id);
$this->assertEquals(0, api::count_blocked_users($user1));
$this->assertEquals(1, api::count_blocked_users());
}
/**
* Tests searching for users when site-wide messaging is disabled.
*
* This test verifies that any contacts are returned, as well as any non-contacts whose profile we can view.
* If checks this by placing some users in the same course, where default caps would permit a user to view another user's
* profile.
*/
public function test_message_search_users_messagingallusers_disabled() {
global $DB;
$this->resetAfterTest();
// Create some users.
$users = [];
foreach (range(1, 8) as $i) {
$user = new \stdClass();
$user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term.
$user->lastname = $i;
$user = $this->getDataGenerator()->create_user($user);
$users[$i] = $user;
}
// Enrol a few users in the same course, but leave them as non-contacts.
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$this->setAdminUser();
$this->getDataGenerator()->enrol_user($users[1]->id, $course1->id);
$this->getDataGenerator()->enrol_user($users[6]->id, $course1->id);
$this->getDataGenerator()->enrol_user($users[7]->id, $course1->id);
// Add some other users as contacts.
api::add_contact($users[1]->id, $users[2]->id);
api::add_contact($users[3]->id, $users[1]->id);
api::add_contact($users[1]->id, $users[4]->id);
// Enrol a user as a teacher in the course, and make the teacher role a course contact role.
$this->getDataGenerator()->enrol_user($users[8]->id, $course2->id, 'editingteacher');
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
set_config('coursecontact', $teacherrole->id);
// Create individual conversations between some users, one contact and one non-contact.
$ic1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
[$users[1]->id, $users[2]->id]);
$ic2 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
[$users[6]->id, $users[1]->id]);
// Create a group conversation between 4 users, including a contact and a non-contact.
$gc1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$users[1]->id, $users[2]->id, $users[4]->id, $users[7]->id], 'Project chat');
// Set as the user performing the search.
$this->setUser($users[1]);
// Perform a search with $CFG->messagingallusers disabled.
set_config('messagingallusers', 0);
$result = api::message_search_users($users[1]->id, 'search');
// Confirm that we returns contacts and non-contacts.
$this->assertArrayHasKey(0, $result);
$this->assertArrayHasKey(1, $result);
$contacts = $result[0];
$noncontacts = $result[1];
// Check that we retrieved the correct contacts.
$this->assertCount(2, $contacts);
$this->assertEquals($users[2]->id, $contacts[0]->id);
$this->assertEquals($users[3]->id, $contacts[1]->id);
// Verify the correct conversations were returned for the contacts.
$this->assertCount(2, $contacts[0]->conversations);
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_GROUP, $contacts[0]->conversations[$gc1->id]->type);
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $contacts[0]->conversations[$ic1->id]->type);
$this->assertCount(0, $contacts[1]->conversations);
// Check that we retrieved the correct non-contacts.
// When site wide messaging is disabled, we expect to see only those users who we share a course with and whose profiles
// are visible in that course. This excludes users like course contacts.
$this->assertCount(3, $noncontacts);
// Self-conversation first.
$this->assertEquals($users[1]->id, $noncontacts[0]->id);
$this->assertEquals($users[6]->id, $noncontacts[1]->id);
$this->assertEquals($users[7]->id, $noncontacts[2]->id);
// Verify the correct conversations were returned for the non-contacts.
$this->assertCount(1, $noncontacts[1]->conversations);
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
$noncontacts[1]->conversations[$ic2->id]->type);
$this->assertCount(1, $noncontacts[2]->conversations);
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_GROUP, $noncontacts[2]->conversations[$gc1->id]->type);
}
/**
* Tests searching for users when site-wide messaging is enabled.
*
* This test verifies that any contacts are returned, as well as any non-contacts,
* provided the searching user can view their profile.
*/
public function test_message_search_users_messagingallusers_enabled() {
global $DB;
$this->resetAfterTest();
// Create some users.
$users = [];
foreach (range(1, 9) as $i) {
$user = new \stdClass();
$user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term.
$user->lastname = $i;
$user = $this->getDataGenerator()->create_user($user);
$users[$i] = $user;
}
$course1 = $this->getDataGenerator()->create_course();
$coursecontext = \context_course::instance($course1->id);
// Enrol a few users in the same course, but leave them as non-contacts.
$this->setAdminUser();
$this->getDataGenerator()->enrol_user($users[1]->id, $course1->id, 'student');
$this->getDataGenerator()->enrol_user($users[6]->id, $course1->id, 'student');
$this->getDataGenerator()->enrol_user($users[7]->id, $course1->id, 'student');
// Add some other users as contacts.
api::add_contact($users[1]->id, $users[2]->id);
api::add_contact($users[3]->id, $users[1]->id);
api::add_contact($users[1]->id, $users[4]->id);
// Enrol a user as a teacher in the course, and make the teacher role a course contact role.
$this->getDataGenerator()->enrol_user($users[9]->id, $course1->id, 'editingteacher');
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
set_config('coursecontact', $teacherrole->id);
// Get self-conversation.
$selfconversation = api::get_self_conversation($users[1]->id);
// Create individual conversations between some users, one contact and one non-contact.
$ic1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
[$users[1]->id, $users[2]->id]);
$ic2 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
[$users[6]->id, $users[1]->id]);
// Create a group conversation between 5 users, including a contact and a non-contact, and a user NOT in a shared course.
$gc1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$users[1]->id, $users[2]->id, $users[4]->id, $users[7]->id, $users[8]->id], 'Project chat');
// Set as the user performing the search.
$this->setUser($users[1]);
// Perform a search with $CFG->messagingallusers enabled.
set_config('messagingallusers', 1);
$result = api::message_search_users($users[1]->id, 'search');
// Confirm that we returns contacts and non-contacts.
$this->assertArrayHasKey(0, $result);
$this->assertArrayHasKey(1, $result);
$contacts = $result[0];
$noncontacts = $result[1];
// Check that we retrieved the correct contacts.
$this->assertCount(2, $contacts);
$this->assertEquals($users[2]->id, $contacts[0]->id);
$this->assertEquals($users[3]->id, $contacts[1]->id);
// Verify the correct conversations were returned for the contacts.
$this->assertCount(2, $contacts[0]->conversations);
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_GROUP, $contacts[0]->conversations[$gc1->id]->type);
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $contacts[0]->conversations[$ic1->id]->type);
$this->assertCount(0, $contacts[1]->conversations);
// Check that we retrieved the correct non-contacts.
// If site wide messaging is enabled, we expect to only be able to search for users whose profiles we can view.
// In this case, as a student, that's the course contact for course2 and those noncontacts sharing a course with user1.
// Consider first conversations is self-conversation.
$this->assertCount(4, $noncontacts);
$this->assertEquals($users[1]->id, $noncontacts[0]->id);
$this->assertEquals($users[6]->id, $noncontacts[1]->id);
$this->assertEquals($users[7]->id, $noncontacts[2]->id);
$this->assertEquals($users[9]->id, $noncontacts[3]->id);
$this->assertCount(1, $noncontacts[1]->conversations);
$this->assertCount(1, $noncontacts[2]->conversations);
$this->assertCount(0, $noncontacts[3]->conversations);
// Verify the correct conversations were returned for the non-contacts.
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_SELF,
$noncontacts[0]->conversations[$selfconversation->id]->type);
$this->assertCount(1, $noncontacts[1]->conversations);
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
$noncontacts[1]->conversations[$ic2->id]->type);
$this->assertCount(1, $noncontacts[2]->conversations);
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_GROUP, $noncontacts[2]->conversations[$gc1->id]->type);
$this->assertCount(0, $noncontacts[3]->conversations);
}
/**
* Verify searching for users find themselves when they have self-conversations.
*/
public function test_message_search_users_self_conversations() {
$this->resetAfterTest();
// Create some users.
$user1 = new \stdClass();
$user1->firstname = 'User';
$user1->lastname = 'One';
$user1 = $this->getDataGenerator()->create_user($user1);
$user2 = new \stdClass();
$user2->firstname = 'User';
$user2->lastname = 'Two';
$user2 = $this->getDataGenerator()->create_user($user2);
// Get self-conversation for user1.
$sc1 = api::get_self_conversation($user1->id);
testhelper::send_fake_message_to_conversation($user1, $sc1->id, 'Hi myself!');
// Perform a search as user1.
$this->setUser($user1);
$result = api::message_search_users($user1->id, 'One');
// Check user1 is found as non-contacts.
$this->assertCount(0, $result[0]);
$this->assertCount(1, $result[1]);
}
/**
* Verify searching for users works even if no matching users from either contacts, or non-contacts can be found.
*/
public function test_message_search_users_with_empty_result() {
$this->resetAfterTest();
// Create some users, but make sure neither will match the search term.
$user1 = new \stdClass();
$user1->firstname = 'User';
$user1->lastname = 'One';
$user1 = $this->getDataGenerator()->create_user($user1);
$user2 = new \stdClass();
$user2->firstname = 'User';
$user2->lastname = 'Two';
$user2 = $this->getDataGenerator()->create_user($user2);
// Perform a search as user1.
$this->setUser($user1);
$result = api::message_search_users($user1->id, 'search');
// Check results are empty.
$this->assertCount(0, $result[0]);
$this->assertCount(0, $result[1]);
}
/**
* Test verifying that limits and offsets work for both the contacts and non-contacts return data.
*/
public function test_message_search_users_limit_offset() {
$this->resetAfterTest();
// Create 20 users.
$users = [];
foreach (range(1, 20) as $i) {
$user = new \stdClass();
$user->firstname = "User search";
$user->lastname = $i;
$user = $this->getDataGenerator()->create_user($user);
$users[$i] = $user;
}
// Enrol the first 9 users in the same course, but leave them as non-contacts.
$this->setAdminUser();
$course1 = $this->getDataGenerator()->create_course();
foreach (range(1, 8) as $i) {
$this->getDataGenerator()->enrol_user($users[$i]->id, $course1->id);
}
// Add 5 users, starting at the 11th user, as contacts for user1.
foreach (range(11, 15) as $i) {
api::add_contact($users[1]->id, $users[$i]->id);
}
// Set as the user performing the search.
$this->setUser($users[1]);
// Search using a limit of 3.
// This tests the case where we have more results than the limit for both contacts and non-contacts.
$result = api::message_search_users($users[1]->id, 'search', 0, 3);
$contacts = $result[0];
$noncontacts = $result[1];
// Check that we retrieved the correct contacts.
$this->assertCount(3, $contacts);
$this->assertEquals($users[11]->id, $contacts[0]->id);
$this->assertEquals($users[12]->id, $contacts[1]->id);
$this->assertEquals($users[13]->id, $contacts[2]->id);
// Check that we retrieved the correct non-contacts.
// Consider first conversations is self-conversation.
$this->assertCount(3, $noncontacts);
$this->assertEquals($users[1]->id, $noncontacts[0]->id);
$this->assertEquals($users[2]->id, $noncontacts[1]->id);
$this->assertEquals($users[3]->id, $noncontacts[2]->id);
// Now, offset to get the next batch of results.
// We expect to see 2 contacts, and 3 non-contacts.
$result = api::message_search_users($users[1]->id, 'search', 3, 3);
$contacts = $result[0];
$noncontacts = $result[1];
$this->assertCount(2, $contacts);
$this->assertEquals($users[14]->id, $contacts[0]->id);
$this->assertEquals($users[15]->id, $contacts[1]->id);
$this->assertCount(3, $noncontacts);
$this->assertEquals($users[4]->id, $noncontacts[0]->id);
$this->assertEquals($users[5]->id, $noncontacts[1]->id);
$this->assertEquals($users[6]->id, $noncontacts[2]->id);
// Now, offset to get the next batch of results.
// We expect to see 0 contacts, and 2 non-contacts.
$result = api::message_search_users($users[1]->id, 'search', 6, 3);
$contacts = $result[0];
$noncontacts = $result[1];
$this->assertCount(0, $contacts);
$this->assertCount(2, $noncontacts);
$this->assertEquals($users[7]->id, $noncontacts[0]->id);
$this->assertEquals($users[8]->id, $noncontacts[1]->id);
}
/**
* Tests searching users as a user having the 'moodle/user:viewdetails' capability.
*/
public function test_message_search_users_with_cap() {
$this->resetAfterTest();
global $DB;
// Create some users.
$users = [];
foreach (range(1, 8) as $i) {
$user = new \stdClass();
$user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term.
$user->lastname = $i;
$user = $this->getDataGenerator()->create_user($user);
$users[$i] = $user;
}
// Enrol a few users in the same course, but leave them as non-contacts.
$course1 = $this->getDataGenerator()->create_course();
$this->setAdminUser();
$this->getDataGenerator()->enrol_user($users[1]->id, $course1->id);
$this->getDataGenerator()->enrol_user($users[6]->id, $course1->id);
$this->getDataGenerator()->enrol_user($users[7]->id, $course1->id);
// Add some other users as contacts.
api::add_contact($users[1]->id, $users[2]->id);
api::add_contact($users[3]->id, $users[1]->id);
api::add_contact($users[1]->id, $users[4]->id);
// Set as the user performing the search.
$this->setUser($users[1]);
// Grant the authenticated user role the capability 'user:viewdetails' at site context.
$authenticatedrole = $DB->get_record('role', ['shortname' => 'user'], '*', MUST_EXIST);
assign_capability('moodle/user:viewdetails', CAP_ALLOW, $authenticatedrole->id, \context_system::instance());
// Perform a search with $CFG->messagingallusers disabled.
set_config('messagingallusers', 0);
$result = api::message_search_users($users[1]->id, 'search');
$contacts = $result[0];
$noncontacts = $result[1];
// Check that we retrieved the correct contacts.
$this->assertCount(2, $contacts);
$this->assertEquals($users[2]->id, $contacts[0]->id);
$this->assertEquals($users[3]->id, $contacts[1]->id);
// Check that we retrieved the correct non-contacts.
// Site-wide messaging is disabled, so we expect to be able to search for any users whose profiles we can view.
// Consider first conversations is self-conversation.
$this->assertCount(3, $noncontacts);
$this->assertEquals($users[1]->id, $noncontacts[0]->id);
$this->assertEquals($users[6]->id, $noncontacts[1]->id);
$this->assertEquals($users[7]->id, $noncontacts[2]->id);
}
/**
* Tests searching users with messaging disabled.
*/
public function test_message_search_users_messaging_disabled() {
$this->resetAfterTest();
// Create a user.
$user = $this->getDataGenerator()->create_user();
// Disable messaging.
set_config('messaging', 0);
// Ensure an exception is thrown.
$this->expectException('moodle_exception');
api::message_search_users($user->id, 'User');
}
/**
* Tests getting conversations between 2 users.
*/
public function test_get_conversations_between_users() {
// Create some users.
$user1 = new \stdClass();
$user1->firstname = 'User';
$user1->lastname = 'One';
$user1 = self::getDataGenerator()->create_user($user1);
$user2 = new \stdClass();
$user2->firstname = 'User';
$user2->lastname = 'Two';
$user2 = self::getDataGenerator()->create_user($user2);
$user3 = new \stdClass();
$user3->firstname = 'User search';
$user3->lastname = 'Three';
$user3 = self::getDataGenerator()->create_user($user3);
$user4 = new \stdClass();
$user4->firstname = 'User';
$user4->lastname = 'Four';
$user4 = self::getDataGenerator()->create_user($user4);
$user5 = new \stdClass();
$user5->firstname = 'User';
$user5->lastname = 'Five';
$user5 = self::getDataGenerator()->create_user($user5);
$user6 = new \stdClass();
$user6->firstname = 'User search';
$user6->lastname = 'Six';
$user6 = self::getDataGenerator()->create_user($user6);
// Add some users as contacts.
api::add_contact($user1->id, $user2->id);
api::add_contact($user6->id, $user1->id);
// Create private conversations with some users.
api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
array($user1->id, $user2->id));
api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
array($user3->id, $user1->id));
// Create a group conversation with users.
api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
array($user1->id, $user2->id, $user3->id, $user4->id),
'Project chat');
// Check that we retrieved the correct conversations.
$this->assertCount(2, api::get_conversations_between_users($user1->id, $user2->id));
$this->assertCount(2, api::get_conversations_between_users($user2->id, $user1->id));
$this->assertCount(2, api::get_conversations_between_users($user1->id, $user3->id));
$this->assertCount(2, api::get_conversations_between_users($user3->id, $user1->id));
$this->assertCount(1, api::get_conversations_between_users($user1->id, $user4->id));
$this->assertCount(1, api::get_conversations_between_users($user4->id, $user1->id));
$this->assertCount(0, api::get_conversations_between_users($user1->id, $user5->id));
$this->assertCount(0, api::get_conversations_between_users($user5->id, $user1->id));
$this->assertCount(0, api::get_conversations_between_users($user1->id, $user6->id));
$this->assertCount(0, api::get_conversations_between_users($user6->id, $user1->id));
}
/**
* Tests getting self-conversations.
*/
public function test_get_self_conversation() {
// Create some users.
$user1 = new \stdClass();
$user1->firstname = 'User';
$user1->lastname = 'One';
$user1 = self::getDataGenerator()->create_user($user1);
$user2 = new \stdClass();
$user2->firstname = 'User';
$user2->lastname = 'Two';
$user2 = self::getDataGenerator()->create_user($user2);
$user3 = new \stdClass();
$user3->firstname = 'User search';
$user3->lastname = 'Three';
$user3 = self::getDataGenerator()->create_user($user3);
// Add some users as contacts.
api::add_contact($user1->id, $user2->id);
api::add_contact($user3->id, $user1->id);
// Create private conversations with some users.
api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
array($user1->id, $user2->id));
api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
array($user3->id, $user1->id));
// Create a group conversation with users.
$gc = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
array($user1->id, $user2->id, $user3->id),
'Project chat');
// Get self-conversations.
$rsc1 = api::get_self_conversation($user1->id);
$rsc2 = api::get_self_conversation($user2->id);
$rsc3 = api::get_self_conversation($user3->id);
// Send message to self-conversation.
testhelper::send_fake_message_to_conversation($user1, $rsc1->id, 'Message to myself!');
// Check that we retrieved the correct conversations.
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_SELF, $rsc1->type);
$members = api::get_conversation_members($user1->id, $rsc1->id);
$this->assertCount(1, $members);
$member = reset($members);
$this->assertEquals($user1->id, $member->id);
$this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_SELF, $rsc2->type);
$members = api::get_conversation_members($user2->id, $rsc2->id);
$this->assertCount(1, $members);
$member = reset($members);
$this->assertEquals($user2->id, $member->id);
api::delete_all_conversation_data($rsc3->id);
$selfconversation = api::get_self_conversation($user3->id);
$members = api::get_conversation_members($user1->id, $selfconversation->id);
$this->assertCount(1, $members);
}
/**
* Tests searching messages.
*/
public function test_search_messages() {
$this->resetAfterTest();
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
// The person doing the search.
$this->setUser($user1);
// Get self-conversation.
$sc = api::get_self_conversation($user1->id);
// Create group conversation.
$gc = api::create_conversation(
api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id, $user3->id]
);
// Send some messages back and forth.
$time = 1;
testhelper::send_fake_message_to_conversation($user1, $sc->id, 'Test message to self!', $time);
testhelper::send_fake_message_to_conversation($user1, $gc->id, 'My hero!', $time + 1);
$this->send_fake_message($user3, $user1, 'Don\'t block me.', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 4);
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 5);
$this->send_fake_message($user2, $user1, 'Word.', 0, $time + 6);
$convid = api::get_conversation_between_users([$user1->id, $user2->id]);
$conv2id = api::get_conversation_between_users([$user1->id, $user3->id]);
// Block user 3.
api::block_user($user1->id, $user3->id);
// Perform a search.
$messages = api::search_messages($user1->id, 'o');
// Confirm the data is correct.
$this->assertEquals(5, count($messages));
$message1 = $messages[0];
$message2 = $messages[1];
$message3 = $messages[2];
$message4 = $messages[3];
$message5 = $messages[4];
$this->assertEquals($user2->id, $message1->userid);
$this->assertEquals($user2->id, $message1->useridfrom);
$this->assertEquals(fullname($user2), $message1->fullname);
$this->assertTrue($message1->ismessaging);
$this->assertEquals('Word.', $message1->lastmessage);
$this->assertNotEmpty($message1->messageid);
$this->assertNull($message1->isonline);
$this->assertFalse($message1->isread);
$this->assertFalse($message1->isblocked);
$this->assertNull($message1->unreadcount);
$this->assertEquals($convid, $message1->conversationid);
$this->assertEquals($user2->id, $message2->userid);
$this->assertEquals($user1->id, $message2->useridfrom);
$this->assertEquals(fullname($user2), $message2->fullname);
$this->assertTrue($message2->ismessaging);
$this->assertEquals('Yo!', $message2->lastmessage);
$this->assertNotEmpty($message2->messageid);
$this->assertNull($message2->isonline);
$this->assertTrue($message2->isread);
$this->assertFalse($message2->isblocked);
$this->assertNull($message2->unreadcount);
$this->assertEquals($convid, $message2->conversationid);
$this->assertEquals($user3->id, $message3->userid);
$this->assertEquals($user3->id, $message3->useridfrom);
$this->assertEquals(fullname($user3), $message3->fullname);
$this->assertTrue($message3->ismessaging);
$this->assertEquals('Don\'t block me.', $message3->lastmessage);
$this->assertNotEmpty($message3->messageid);
$this->assertNull($message3->isonline);
$this->assertFalse($message3->isread);
$this->assertTrue($message3->isblocked);
$this->assertNull($message3->unreadcount);
$this->assertEquals($conv2id, $message3->conversationid);
// This is a group conversation. For now, search_messages returns only one of the other users on the conversation. It can't
// be guaranteed who will be returned in the first place, so we need to use the in_array to check all the possibilities.
$this->assertTrue(in_array($message4->userid, [$user2->id, $user3->id]));
$this->assertEquals($user1->id, $message4->useridfrom);
$this->assertTrue($message4->ismessaging);
$this->assertEquals('My hero!', $message4->lastmessage);
$this->assertNotEmpty($message4->messageid);
$this->assertNull($message4->isonline);
$this->assertTrue($message4->isread);
$this->assertNull($message4->unreadcount);
$this->assertEquals($gc->id, $message4->conversationid);
$this->assertEquals($user1->id, $message5->userid);
$this->assertEquals($user1->id, $message5->useridfrom);
$this->assertEquals(fullname($user1), $message5->fullname);
$this->assertTrue($message5->ismessaging);
$this->assertEquals('Test message to self!', $message5->lastmessage);
$this->assertNotEmpty($message5->messageid);
$this->assertFalse($message5->isonline);
$this->assertTrue($message5->isread);
$this->assertFalse($message5->isblocked);
$this->assertNull($message5->unreadcount);
$this->assertEquals($sc->id, $message5->conversationid);
}
/**
* Test verifying that favourited conversations can be retrieved.
*/
public function test_get_favourite_conversations() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
// The person doing the search.
$this->setUser($user1);
// Only self-conversation created.
$this->assertCount(1, api::get_conversations($user1->id));
// Create some conversations for user1.
$time = 1;
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
$messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
$this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
$this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
$this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
$messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
$this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
$this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
$messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
// Favourite the first 2 conversations for user1.
$convoids = [];
$convoids[] = api::get_conversation_between_users([$user1->id, $user2->id]);
$convoids[] = api::get_conversation_between_users([$user1->id, $user3->id]);
$user1context = \context_user::instance($user1->id);
$service = \core_favourites\service_factory::get_service_for_user_context($user1context);
foreach ($convoids as $convoid) {
$service->create_favourite('core_message', 'message_conversations', $convoid, $user1context);
}
// We should have 4 conversations.
// Consider first conversations is self-conversation.
$this->assertCount(4, api::get_conversations($user1->id));
// And 3 favourited conversations (self-conversation included).
$conversations = api::get_conversations($user1->id, 0, 20, null, true);
$this->assertCount(3, $conversations);
$conversations = api::get_conversations(
$user1->id,
0,
20,
api::MESSAGE_CONVERSATION_TYPE_SELF,
true
);
$this->assertCount(1, $conversations);
}
/**
* Tests retrieving favourite conversations with a limit and offset to ensure pagination works correctly.
*/
public function test_get_favourite_conversations_limit_offset() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
// The person doing the search.
$this->setUser($user1);
// Only self-conversation created.
$this->assertCount(1, api::get_conversations($user1->id));
// Create some conversations for user1.
$time = 1;
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
$messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
$this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
$this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
$this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
$messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
$this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
$this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
$messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
// Favourite the all conversations for user1.
$convoids = [];
$convoids[] = api::get_conversation_between_users([$user1->id, $user2->id]);
$convoids[] = api::get_conversation_between_users([$user1->id, $user3->id]);
$convoids[] = api::get_conversation_between_users([$user1->id, $user4->id]);
$user1context = \context_user::instance($user1->id);
$service = \core_favourites\service_factory::get_service_for_user_context($user1context);
foreach ($convoids as $convoid) {
$service->create_favourite('core_message', 'message_conversations', $convoid, $user1context);
}
// Consider first conversations is self-conversation.
// Get all records, using offset 0 and large limit.
$this->assertCount(4, api::get_conversations($user1->id, 0, 20, null, true));
// Now, get 10 conversations starting at the second record. We should see 2 conversations.
$this->assertCount(3, api::get_conversations($user1->id, 1, 10, null, true));
// Now, try to get favourited conversations using an invalid offset.
$this->assertCount(0, api::get_conversations($user1->id, 5, 10, null, true));
}
/**
* Tests retrieving favourite conversations when a conversation contains a deleted user.
*/
public function test_get_favourite_conversations_with_deleted_user() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
// Send some messages back and forth, have some different conversations with different users.
$time = 1;
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
$this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
$this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
$this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
$this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
// Favourite the all conversations for user1.
$convoids = [];
$convoids[] = api::get_conversation_between_users([$user1->id, $user2->id]);
$convoids[] = api::get_conversation_between_users([$user1->id, $user3->id]);
$user1context = \context_user::instance($user1->id);
$service = \core_favourites\service_factory::get_service_for_user_context($user1context);
foreach ($convoids as $convoid) {
$service->create_favourite('core_message', 'message_conversations', $convoid, $user1context);
}
// Delete the second user.
delete_user($user2);
// Retrieve the conversations.
$conversations = api::get_conversations($user1->id, 0, 20, null, true);
// We should have both conversations, despite the other user being soft-deleted.
// Consider first conversations is self-conversation.
$this->assertCount(3, $conversations);
// Confirm the conversation is from the non-deleted user.
$conversation = reset($conversations);
$this->assertEquals($convoids[1], $conversation->id);
}
/**
* Test confirming that conversations can be marked as favourites.
*/
public function test_set_favourite_conversation() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
// Send some messages back and forth, have some different conversations with different users.
$time = 1;
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
$this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
$this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
$this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
$this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
// Favourite the first conversation as user 1.
$conversationid1 = api::get_conversation_between_users([$user1->id, $user2->id]);
$favourite = api::set_favourite_conversation($conversationid1, $user1->id);
// Verify we have two favourite conversations a user 1.
// Consider first conversations is self-conversation.
$this->assertCount(2, api::get_conversations($user1->id, 0, 20, null, true));
// Verify we have only one favourite as user2, despite being a member in that conversation.
// Consider first conversations is self-conversation.
$this->assertCount(1, api::get_conversations($user2->id, 0, 20, null, true));
// Try to favourite the same conversation again should just return the existing favourite.
$repeatresult = api::set_favourite_conversation($conversationid1, $user1->id);
$this->assertEquals($favourite->id, $repeatresult->id);
}
/**
* Test verifying that trying to mark a non-existent conversation as a favourite, results in an exception.
*/
public function test_set_favourite_conversation_nonexistent_conversation() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
// Try to favourite a non-existent conversation.
$this->expectException(\moodle_exception::class);
api::set_favourite_conversation(0, $user1->id);
}
/**
* Test verifying that a conversation cannot be marked as favourite unless the user is a member of that conversation.
*/
public function test_set_favourite_conversation_non_member() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
// Send some messages back and forth, have some different conversations with different users.
$time = 1;
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
$this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
$this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
$this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
$this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
// Try to favourite the first conversation as user 3, who is not a member.
$conversationid1 = api::get_conversation_between_users([$user1->id, $user2->id]);
$this->expectException(\moodle_exception::class);
api::set_favourite_conversation($conversationid1, $user3->id);
}
/**
* Test confirming that those conversations marked as favourites can be unfavourited.
*/
public function test_unset_favourite_conversation() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
// Send some messages back and forth, have some different conversations with different users.
$time = 1;
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
$this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
$this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
$this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
$this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
// Favourite the first conversation as user 1 and the second as user 3.