Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 26... Línea 26...
26
 * @copyright 2016 Mark Nelson <markn@moodle.com>
26
 * @copyright 2016 Mark Nelson <markn@moodle.com>
27
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers \core_message\api
28
 * @covers \core_message\api
29
 */
29
 */
30
final class api_test extends \advanced_testcase {
30
final class api_test extends \advanced_testcase {
-
 
31
    /**
-
 
32
     * Test set up.
-
 
33
     *
-
 
34
     * This is executed before running any test in this file.
-
 
35
     */
-
 
36
    public function setUp(): void {
-
 
37
        parent::setUp();
-
 
38
 
-
 
39
        $this->resetAfterTest();
-
 
40
    }
-
 
41
 
31
    public function test_mark_all_read_for_user_touser(): void {
42
    public function test_mark_all_read_for_user_touser(): void {
32
        $this->resetAfterTest();
43
        $this->resetAfterTest();
Línea 33... Línea 44...
33
 
44
 
34
        $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
45
        $sender = $this->getDataGenerator()->create_user(['firstname' => 'Test1', 'lastname' => 'User1']);
Línea 35... Línea 46...
35
        $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
46
        $recipient = $this->getDataGenerator()->create_user(['firstname' => 'Test2', 'lastname' => 'User2']);
36
 
47
 
37
        testhelper::send_fake_message($sender, $recipient, 'Notification', 1);
48
        testhelper::send_fake_message($sender, $recipient, 'Notification', 1);
38
        testhelper::send_fake_message($sender, $recipient, 'Notification', 1);
49
        testhelper::send_fake_message($sender, $recipient, 'Notification', 1);
Línea 51... Línea 62...
51
    }
62
    }
Línea 52... Línea 63...
52
 
63
 
53
    public function test_mark_all_read_for_user_touser_with_fromuser(): void {
64
    public function test_mark_all_read_for_user_touser_with_fromuser(): void {
Línea 54... Línea -...
54
        $this->resetAfterTest();
-
 
55
 
-
 
56
        $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
-
 
57
        $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
65
        $this->resetAfterTest();
58
        $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
66
 
59
        $sender1 = $this->getDataGenerator()->create_user(['firstname' => 'Test1', 'lastname' => 'User1']);
67
        $sender1 = $this->getDataGenerator()->create_user(['firstname' => 'Test1', 'lastname' => 'User1']);
Línea 60... Línea 68...
60
        $sender2 = $this->getDataGenerator()->create_user(['firstname' => 'Test3', 'lastname' => 'User3']);
68
        $sender2 = $this->getDataGenerator()->create_user(['firstname' => 'Test3', 'lastname' => 'User3']);
Línea 88... Línea 96...
88
     */
96
     */
89
    public function test_count_blocked_users(): void {
97
    public function test_count_blocked_users(): void {
90
        global $USER;
98
        global $USER;
91
        $this->resetAfterTest();
99
        $this->resetAfterTest();
Línea 92... Línea -...
92
 
-
 
93
 
100
 
94
        // Set this user as the admin.
101
        // Set this user as the admin.
Línea 95... Línea 102...
95
        $this->setAdminUser();
102
        $this->setAdminUser();
96
 
103
 
Línea 142... Línea 149...
142
        api::add_contact($users[3]->id, $users[1]->id);
149
        api::add_contact($users[3]->id, $users[1]->id);
143
        api::add_contact($users[1]->id, $users[4]->id);
150
        api::add_contact($users[1]->id, $users[4]->id);
Línea 144... Línea 151...
144
 
151
 
145
        // Enrol a user as a teacher in the course, and make the teacher role a course contact role.
152
        // Enrol a user as a teacher in the course, and make the teacher role a course contact role.
146
        $this->getDataGenerator()->enrol_user($users[8]->id, $course2->id, 'editingteacher');
153
        $this->getDataGenerator()->enrol_user($users[8]->id, $course2->id, 'editingteacher');
147
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
154
        $teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
Línea 148... Línea 155...
148
        set_config('coursecontact', $teacherrole->id);
155
        set_config('coursecontact', $teacherrole->id);
-
 
156
 
149
 
157
        // Create individual conversations between some users, one contact and one non-contact.
150
        // Create individual conversations between some users, one contact and one non-contact.
158
        $ic1 = api::create_conversation(
-
 
159
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
160
            [$users[1]->id, $users[2]->id]
151
        $ic1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
161
        );
152
            [$users[1]->id, $users[2]->id]);
162
        $ic2 = api::create_conversation(
-
 
163
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
Línea 153... Línea 164...
153
        $ic2 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
164
            [$users[6]->id, $users[1]->id]
-
 
165
        );
154
            [$users[6]->id, $users[1]->id]);
166
 
155
 
167
        // Create a group conversation between 4 users, including a contact and a non-contact.
-
 
168
        $gc1 = api::create_conversation(
-
 
169
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
Línea 156... Línea 170...
156
        // Create a group conversation between 4 users, including a contact and a non-contact.
170
            [$users[1]->id, $users[2]->id, $users[4]->id, $users[7]->id],
157
        $gc1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
171
            'Project chat'
Línea 158... Línea 172...
158
            [$users[1]->id, $users[2]->id, $users[4]->id, $users[7]->id], 'Project chat');
172
        );
Línea 191... Línea 205...
191
        $this->assertEquals($users[6]->id, $noncontacts[1]->id);
205
        $this->assertEquals($users[6]->id, $noncontacts[1]->id);
192
        $this->assertEquals($users[7]->id, $noncontacts[2]->id);
206
        $this->assertEquals($users[7]->id, $noncontacts[2]->id);
Línea 193... Línea 207...
193
 
207
 
194
        // Verify the correct conversations were returned for the non-contacts.
208
        // Verify the correct conversations were returned for the non-contacts.
-
 
209
        $this->assertCount(1, $noncontacts[1]->conversations);
195
        $this->assertCount(1, $noncontacts[1]->conversations);
210
        $this->assertEquals(
196
        $this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
211
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
212
            $noncontacts[1]->conversations[$ic2->id]->type
Línea 197... Línea 213...
197
            $noncontacts[1]->conversations[$ic2->id]->type);
213
        );
198
 
214
 
199
        $this->assertCount(1, $noncontacts[2]->conversations);
215
        $this->assertCount(1, $noncontacts[2]->conversations);
Línea 234... Línea 250...
234
        api::add_contact($users[3]->id, $users[1]->id);
250
        api::add_contact($users[3]->id, $users[1]->id);
235
        api::add_contact($users[1]->id, $users[4]->id);
251
        api::add_contact($users[1]->id, $users[4]->id);
Línea 236... Línea 252...
236
 
252
 
237
        // Enrol a user as a teacher in the course, and make the teacher role a course contact role.
253
        // Enrol a user as a teacher in the course, and make the teacher role a course contact role.
238
        $this->getDataGenerator()->enrol_user($users[9]->id, $course1->id, 'editingteacher');
254
        $this->getDataGenerator()->enrol_user($users[9]->id, $course1->id, 'editingteacher');
239
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
255
        $teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
Línea 240... Línea 256...
240
        set_config('coursecontact', $teacherrole->id);
256
        set_config('coursecontact', $teacherrole->id);
241
 
257
 
Línea 242... Línea 258...
242
        // Get self-conversation.
258
        // Get self-conversation.
-
 
259
        $selfconversation = api::get_self_conversation($users[1]->id);
243
        $selfconversation = api::get_self_conversation($users[1]->id);
260
 
244
 
261
        // Create individual conversations between some users, one contact and one non-contact.
-
 
262
        $ic1 = api::create_conversation(
-
 
263
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
245
        // Create individual conversations between some users, one contact and one non-contact.
264
            [$users[1]->id, $users[2]->id]
246
        $ic1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
265
        );
-
 
266
        $ic2 = api::create_conversation(
Línea 247... Línea 267...
247
            [$users[1]->id, $users[2]->id]);
267
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
268
            [$users[6]->id, $users[1]->id]
248
        $ic2 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
269
        );
249
            [$users[6]->id, $users[1]->id]);
270
 
-
 
271
        // Create a group conversation between 5 users, including a contact and a non-contact, and a user NOT in a shared course.
-
 
272
        $gc1 = api::create_conversation(
Línea 250... Línea 273...
250
 
273
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
251
        // Create a group conversation between 5 users, including a contact and a non-contact, and a user NOT in a shared course.
274
            [$users[1]->id, $users[2]->id, $users[4]->id, $users[7]->id, $users[8]->id],
Línea 252... Línea 275...
252
        $gc1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
275
            'Project chat'
Línea 290... Línea 313...
290
        $this->assertCount(1, $noncontacts[1]->conversations);
313
        $this->assertCount(1, $noncontacts[1]->conversations);
291
        $this->assertCount(1, $noncontacts[2]->conversations);
314
        $this->assertCount(1, $noncontacts[2]->conversations);
292
        $this->assertCount(0, $noncontacts[3]->conversations);
315
        $this->assertCount(0, $noncontacts[3]->conversations);
Línea 293... Línea 316...
293
 
316
 
-
 
317
        // Verify the correct conversations were returned for the non-contacts.
294
        // Verify the correct conversations were returned for the non-contacts.
318
        $this->assertEquals(
295
        $this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_SELF,
319
            api::MESSAGE_CONVERSATION_TYPE_SELF,
-
 
320
            $noncontacts[0]->conversations[$selfconversation->id]->type
Línea 296... Línea 321...
296
            $noncontacts[0]->conversations[$selfconversation->id]->type);
321
        );
-
 
322
 
297
 
323
        $this->assertCount(1, $noncontacts[1]->conversations);
298
        $this->assertCount(1, $noncontacts[1]->conversations);
324
        $this->assertEquals(
-
 
325
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
Línea 299... Línea 326...
299
        $this->assertEquals(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
326
            $noncontacts[1]->conversations[$ic2->id]->type
300
            $noncontacts[1]->conversations[$ic2->id]->type);
327
        );
Línea 301... Línea 328...
301
 
328
 
Línea 548... Línea 575...
548
        // Add some users as contacts.
575
        // Add some users as contacts.
549
        api::add_contact($user1->id, $user2->id);
576
        api::add_contact($user1->id, $user2->id);
550
        api::add_contact($user6->id, $user1->id);
577
        api::add_contact($user6->id, $user1->id);
Línea 551... Línea 578...
551
 
578
 
-
 
579
        // Create private conversations with some users.
552
        // Create private conversations with some users.
580
        api::create_conversation(
553
        api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
581
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
582
            [$user1->id, $user2->id]
-
 
583
        );
554
            array($user1->id, $user2->id));
584
        api::create_conversation(
555
        api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
585
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
586
            [$user3->id, $user1->id]
Línea 556... Línea 587...
556
            array($user3->id, $user1->id));
587
        );
-
 
588
 
557
 
589
        // Create a group conversation with users.
558
        // Create a group conversation with users.
590
        api::create_conversation(
559
        api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
591
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
-
 
592
            [$user1->id, $user2->id, $user3->id, $user4->id],
Línea 560... Línea 593...
560
            array($user1->id, $user2->id, $user3->id, $user4->id),
593
            'Project chat'
561
            'Project chat');
594
        );
562
 
595
 
563
        // Check that we retrieved the correct conversations.
596
        // Check that we retrieved the correct conversations.
Línea 598... Línea 631...
598
        // Add some users as contacts.
631
        // Add some users as contacts.
599
        api::add_contact($user1->id, $user2->id);
632
        api::add_contact($user1->id, $user2->id);
600
        api::add_contact($user3->id, $user1->id);
633
        api::add_contact($user3->id, $user1->id);
Línea 601... Línea 634...
601
 
634
 
-
 
635
        // Create private conversations with some users.
602
        // Create private conversations with some users.
636
        api::create_conversation(
603
        api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
637
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
638
            [$user1->id, $user2->id]
-
 
639
        );
604
            array($user1->id, $user2->id));
640
        api::create_conversation(
605
        api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
641
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
642
            [$user3->id, $user1->id]
Línea 606... Línea 643...
606
            array($user3->id, $user1->id));
643
        );
-
 
644
 
607
 
645
        // Create a group conversation with users.
608
        // Create a group conversation with users.
646
        $gc = api::create_conversation(
609
        $gc = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
647
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
-
 
648
            [$user1->id, $user2->id, $user3->id],
Línea 610... Línea 649...
610
            array($user1->id, $user2->id, $user3->id),
649
            'Project chat'
611
            'Project chat');
650
        );
612
 
651
 
613
        // Get self-conversations.
652
        // Get self-conversations.
Línea 1095... Línea 1134...
1095
        // 3) At least one of each type (group, individual) of which user1 IS NOT a member.
1134
        // 3) At least one of each type (group, individual) of which user1 IS NOT a member.
1096
        // 4) At least two group conversation having 0 messages, of which user1 IS a member (To confirm conversationid ordering).
1135
        // 4) At least two group conversation having 0 messages, of which user1 IS a member (To confirm conversationid ordering).
1097
        // 5) At least one group conversation having 0 messages, of which user1 IS NOT a member.
1136
        // 5) At least one group conversation having 0 messages, of which user1 IS NOT a member.
Línea 1098... Línea 1137...
1098
 
1137
 
-
 
1138
        // Individual conversation, user1 is a member, last message from other user.
1099
        // Individual conversation, user1 is a member, last message from other user.
1139
        $ic1 = api::create_conversation(
1100
        $ic1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1140
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
1141
            [$user1->id, $user2->id]
1101
            [$user1->id, $user2->id]);
1142
        );
1102
        testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 1', $time);
1143
        testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 1', $time);
Línea 1103... Línea 1144...
1103
        testhelper::send_fake_message_to_conversation($user2, $ic1->id, 'Message 2', $time + 1);
1144
        testhelper::send_fake_message_to_conversation($user2, $ic1->id, 'Message 2', $time + 1);
-
 
1145
 
1104
 
1146
        // Individual conversation, user1 is a member, last message from user1.
1105
        // Individual conversation, user1 is a member, last message from user1.
1147
        $ic2 = api::create_conversation(
-
 
1148
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1106
        $ic2 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1149
            [$user1->id, $user3->id]
1107
            [$user1->id, $user3->id]);
1150
        );
Línea 1108... Línea 1151...
1108
        testhelper::send_fake_message_to_conversation($user3, $ic2->id, 'Message 3', $time + 2);
1151
        testhelper::send_fake_message_to_conversation($user3, $ic2->id, 'Message 3', $time + 2);
-
 
1152
        testhelper::send_fake_message_to_conversation($user1, $ic2->id, 'Message 4', $time + 3);
1109
        testhelper::send_fake_message_to_conversation($user1, $ic2->id, 'Message 4', $time + 3);
1153
 
1110
 
1154
        // Individual conversation, user1 is not a member.
-
 
1155
        $ic3 = api::create_conversation(
1111
        // Individual conversation, user1 is not a member.
1156
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1112
        $ic3 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1157
            [$user2->id, $user3->id]
Línea 1113... Línea 1158...
1113
            [$user2->id, $user3->id]);
1158
        );
-
 
1159
        testhelper::send_fake_message_to_conversation($user2, $ic3->id, 'Message 5', $time + 4);
1114
        testhelper::send_fake_message_to_conversation($user2, $ic3->id, 'Message 5', $time + 4);
1160
        testhelper::send_fake_message_to_conversation($user3, $ic3->id, 'Message 6', $time + 5);
1115
        testhelper::send_fake_message_to_conversation($user3, $ic3->id, 'Message 6', $time + 5);
1161
 
-
 
1162
        // Group conversation, user1 is not a member.
-
 
1163
        $gc1 = api::create_conversation(
1116
 
1164
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
1117
        // Group conversation, user1 is not a member.
1165
            [$user2->id, $user3->id, $user4->id],
Línea 1118... Línea 1166...
1118
        $gc1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
1166
            'Project discussions'
-
 
1167
        );
1119
            [$user2->id, $user3->id, $user4->id], 'Project discussions');
1168
        testhelper::send_fake_message_to_conversation($user2, $gc1->id, 'Message 7', $time + 6);
1120
        testhelper::send_fake_message_to_conversation($user2, $gc1->id, 'Message 7', $time + 6);
1169
        testhelper::send_fake_message_to_conversation($user4, $gc1->id, 'Message 8', $time + 7);
-
 
1170
 
-
 
1171
        // Group conversation, user1 is a member, last message from another user.
1121
        testhelper::send_fake_message_to_conversation($user4, $gc1->id, 'Message 8', $time + 7);
1172
        $gc2 = api::create_conversation(
1122
 
1173
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
1123
        // Group conversation, user1 is a member, last message from another user.
1174
            [$user1->id, $user3->id, $user4->id],
Línea 1124... Línea 1175...
1124
        $gc2 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
1175
            'Group chat'
-
 
1176
        );
1125
            [$user1->id, $user3->id, $user4->id], 'Group chat');
1177
        testhelper::send_fake_message_to_conversation($user1, $gc2->id, 'Message 9', $time + 8);
1126
        testhelper::send_fake_message_to_conversation($user1, $gc2->id, 'Message 9', $time + 8);
1178
        testhelper::send_fake_message_to_conversation($user3, $gc2->id, 'Message 10', $time + 9);
-
 
1179
        testhelper::send_fake_message_to_conversation($user4, $gc2->id, 'Message 11', $time + 10);
-
 
1180
 
1127
        testhelper::send_fake_message_to_conversation($user3, $gc2->id, 'Message 10', $time + 9);
1181
        // Group conversation, user1 is a member, last message from user1.
1128
        testhelper::send_fake_message_to_conversation($user4, $gc2->id, 'Message 11', $time + 10);
1182
        $gc3 = api::create_conversation(
1129
 
1183
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
Línea 1130... Línea 1184...
1130
        // Group conversation, user1 is a member, last message from user1.
1184
            [$user1->id, $user2->id, $user3->id, $user4->id],
-
 
1185
            'Group chat again!'
1131
        $gc3 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
1186
        );
1132
            [$user1->id, $user2->id, $user3->id, $user4->id], 'Group chat again!');
1187
        testhelper::send_fake_message_to_conversation($user4, $gc3->id, 'Message 12', $time + 11);
-
 
1188
        testhelper::send_fake_message_to_conversation($user3, $gc3->id, 'Message 13', $time + 12);
-
 
1189
        testhelper::send_fake_message_to_conversation($user1, $gc3->id, 'Message 14', $time + 13);
-
 
1190
 
1133
        testhelper::send_fake_message_to_conversation($user4, $gc3->id, 'Message 12', $time + 11);
1191
        // Empty group conversations (x2), user1 is a member.
1134
        testhelper::send_fake_message_to_conversation($user3, $gc3->id, 'Message 13', $time + 12);
1192
        $gc4 = api::create_conversation(
-
 
1193
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
-
 
1194
            [$user1->id, $user2->id, $user3->id],
Línea 1135... Línea 1195...
1135
        testhelper::send_fake_message_to_conversation($user1, $gc3->id, 'Message 14', $time + 13);
1195
            'Empty group'
-
 
1196
        );
1136
 
1197
        $gc5 = api::create_conversation(
1137
        // Empty group conversations (x2), user1 is a member.
1198
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
-
 
1199
            [$user1->id, $user2->id, $user4->id],
-
 
1200
            'Another empty group'
Línea 1138... Línea 1201...
1138
        $gc4 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
1201
        );
1139
            [$user1->id, $user2->id, $user3->id], 'Empty group');
1202
 
Línea 1140... Línea 1203...
1140
        $gc5 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
1203
        // Empty group conversation, user1 is NOT a member.
Línea 1158... Línea 1221...
1158
        $user1 = self::getDataGenerator()->create_user();
1221
        $user1 = self::getDataGenerator()->create_user();
1159
        // Self-conversation should exists.
1222
        // Self-conversation should exists.
1160
        $this->assertCount(1, api::get_conversations($user1->id));
1223
        $this->assertCount(1, api::get_conversations($user1->id));
Línea 1161... Línea 1224...
1161
 
1224
 
1162
        // Get a bunch of conversations, some group, some individual and in different states.
1225
        // Get a bunch of conversations, some group, some individual and in different states.
1163
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1226
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 1164... Línea 1227...
1164
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1227
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
1165
 
1228
 
Línea 1166... Línea 1229...
1166
        // Get all conversations for user1.
1229
        // Get all conversations for user1.
Línea 1312... Línea 1375...
1312
        // Create some users.
1375
        // Create some users.
1313
        $user1 = self::getDataGenerator()->create_user();
1376
        $user1 = self::getDataGenerator()->create_user();
1314
        $user2 = self::getDataGenerator()->create_user();
1377
        $user2 = self::getDataGenerator()->create_user();
1315
        $user3 = self::getDataGenerator()->create_user();
1378
        $user3 = self::getDataGenerator()->create_user();
Línea -... Línea 1379...
-
 
1379
 
1316
 
1380
        $conversation1 = api::create_conversation(
1317
        $conversation1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1381
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
1382
            [$user1->id, $user2->id]
1318
            [$user1->id, $user2->id]);
1383
        );
1319
        testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1');
1384
        testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1');
1320
        testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 2');
1385
        testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 2');
Línea -... Línea 1386...
-
 
1386
        api::mute_conversation($user1->id, $conversation1->id);
1321
        api::mute_conversation($user1->id, $conversation1->id);
1387
 
1322
 
1388
        $conversation2 = api::create_conversation(
-
 
1389
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1323
        $conversation2 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1390
            [$user1->id, $user3->id]
1324
            [$user1->id, $user3->id]);
1391
        );
Línea 1325... Línea 1392...
1325
        testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 1');
1392
        testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 1');
-
 
1393
        testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2');
1326
        testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2');
1394
 
-
 
1395
        $conversation3 = api::create_conversation(
1327
 
1396
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
Línea 1328... Línea 1397...
1328
        $conversation3 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
1397
            [$user1->id, $user2->id]
-
 
1398
        );
1329
            [$user1->id, $user2->id]);
1399
        api::mute_conversation($user1->id, $conversation3->id);
-
 
1400
 
Línea 1330... Línea 1401...
1330
        api::mute_conversation($user1->id, $conversation3->id);
1401
        $conversation4 = api::create_conversation(
Línea 1331... Línea 1402...
1331
 
1402
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
1332
        $conversation4 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP,
1403
            [$user1->id, $user3->id]
1333
            [$user1->id, $user3->id]);
1404
        );
Línea 1334... Línea 1405...
1334
 
1405
 
1335
        $conversations = api::get_conversations($user1->id);
1406
        $conversations = api::get_conversations($user1->id);
Línea 1356... Línea 1427...
1356
     */
1427
     */
1357
    public function test_get_conversations_limit_offset(): void {
1428
    public function test_get_conversations_limit_offset(): void {
1358
        $this->resetAfterTest();
1429
        $this->resetAfterTest();
Línea 1359... Línea 1430...
1359
 
1430
 
1360
        // Get a bunch of conversations, some group, some individual and in different states.
1431
        // Get a bunch of conversations, some group, some individual and in different states.
1361
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1432
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 1362... Línea 1433...
1362
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1433
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
1363
 
1434
 
Línea 1364... Línea 1435...
1364
        // Get all conversations for user1, limited to 1 result.
1435
        // Get all conversations for user1, limited to 1 result.
Línea 1397... Línea 1468...
1397
     */
1468
     */
1398
    public function test_get_conversations_type_filter(): void {
1469
    public function test_get_conversations_type_filter(): void {
1399
        $this->resetAfterTest();
1470
        $this->resetAfterTest();
Línea 1400... Línea 1471...
1400
 
1471
 
1401
        // Get a bunch of conversations, some group, some individual and in different states.
1472
        // Get a bunch of conversations, some group, some individual and in different states.
1402
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1473
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 1403... Línea 1474...
1403
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1474
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
1404
 
1475
 
-
 
1476
        // Verify we can ask for only individual conversations.
-
 
1477
        $conversations = api::get_conversations(
-
 
1478
            $user1->id,
1405
        // Verify we can ask for only individual conversations.
1479
            0,
-
 
1480
            20,
1406
        $conversations = api::get_conversations($user1->id, 0, 20,
1481
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
Línea 1407... Línea 1482...
1407
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL);
1482
        );
1408
        $this->assertCount(2, $conversations);
1483
        $this->assertCount(2, $conversations);
-
 
1484
 
-
 
1485
        // Verify we can ask for only group conversations.
-
 
1486
        $conversations = api::get_conversations(
1409
 
1487
            $user1->id,
-
 
1488
            0,
1410
        // Verify we can ask for only group conversations.
1489
            20,
Línea 1411... Línea 1490...
1411
        $conversations = api::get_conversations($user1->id, 0, 20,
1490
            api::MESSAGE_CONVERSATION_TYPE_GROUP
1412
            api::MESSAGE_CONVERSATION_TYPE_GROUP);
1491
        );
1413
        $this->assertCount(4, $conversations);
1492
        $this->assertCount(4, $conversations);
Línea 1422... Línea 1501...
1422
     */
1501
     */
1423
    public function test_get_conversations_self_conversations(): void {
1502
    public function test_get_conversations_self_conversations(): void {
1424
        global $DB;
1503
        global $DB;
1425
        $this->resetAfterTest();
1504
        $this->resetAfterTest();
Línea 1426... Línea -...
1426
 
-
 
1427
 
1505
 
1428
        // Create a conversation between one user and themself.
1506
        // Create a conversation between one user and themself.
1429
        $user1 = self::getDataGenerator()->create_user();
1507
        $user1 = self::getDataGenerator()->create_user();
1430
        $user2 = self::getDataGenerator()->create_user();
1508
        $user2 = self::getDataGenerator()->create_user();
1431
        $user3 = self::getDataGenerator()->create_user();
1509
        $user3 = self::getDataGenerator()->create_user();
Línea 1432... Línea 1510...
1432
        $user4 = self::getDataGenerator()->create_user();
1510
        $user4 = self::getDataGenerator()->create_user();
-
 
1511
 
1433
 
1512
        // Create some individual conversations.
1434
        // Create some individual conversations.
1513
        $ic1 = api::create_conversation(
-
 
1514
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
1515
            [$user1->id, $user2->id]
1435
        $ic1 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1516
        );
1436
            [$user1->id, $user2->id]);
1517
        $ic2 = api::create_conversation(
-
 
1518
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1437
        $ic2 = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1519
            [$user1->id, $user3->id]
Línea 1438... Línea 1520...
1438
            [$user1->id, $user3->id]);
1520
        );
1439
        testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message from user1 to user2');
1521
        testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message from user1 to user2');
1440
 
1522
 
Línea 1458... Línea 1540...
1458
        $conversations = api::get_conversations($user4->id);
1540
        $conversations = api::get_conversations($user4->id);
1459
        // The self-conversation.
1541
        // The self-conversation.
1460
        $this->assertCount(1, $conversations);
1542
        $this->assertCount(1, $conversations);
Línea 1461... Línea 1543...
1461
 
1543
 
1462
        // Get only private conversations for user1 (empty conversations, like $ic2, are not returned).
1544
        // Get only private conversations for user1 (empty conversations, like $ic2, are not returned).
-
 
1545
        $conversations = api::get_conversations(
-
 
1546
            $user1->id,
-
 
1547
            0,
1463
        $conversations = api::get_conversations($user1->id, 0, 20,
1548
            20,
-
 
1549
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
1464
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL);
1550
        );
Línea 1465... Línea 1551...
1465
        $this->assertCount(1, $conversations);
1551
        $this->assertCount(1, $conversations);
1466
 
1552
 
-
 
1553
        // Merge self with private conversations for user1.
-
 
1554
        $conversations = api::get_conversations(
-
 
1555
            $user1->id,
1467
        // Merge self with private conversations for user1.
1556
            0,
-
 
1557
            20,
-
 
1558
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
1559
            null,
1468
        $conversations = api::get_conversations($user1->id, 0, 20,
1560
            true
Línea 1469... Línea 1561...
1469
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, null, true);
1561
        );
1470
        $this->assertCount(2, $conversations);
1562
        $this->assertCount(2, $conversations);
-
 
1563
 
-
 
1564
        // Get only private conversations for user2.
-
 
1565
        $conversations = api::get_conversations(
1471
 
1566
            $user2->id,
-
 
1567
            0,
1472
        // Get only private conversations for user2.
1568
            20,
Línea 1473... Línea 1569...
1473
        $conversations = api::get_conversations($user2->id, 0, 20,
1569
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
1474
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL);
1570
        );
-
 
1571
        $this->assertCount(1, $conversations);
-
 
1572
 
-
 
1573
        // Merge self with private conversations for user2.
1475
        $this->assertCount(1, $conversations);
1574
        $conversations = api::get_conversations(
-
 
1575
            $user2->id,
-
 
1576
            0,
-
 
1577
            20,
1476
 
1578
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1477
        // Merge self with private conversations for user2.
1579
            null,
Línea 1478... Línea 1580...
1478
        $conversations = api::get_conversations($user2->id, 0, 20,
1580
            true
1479
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, null, true);
1581
        );
1480
        $this->assertCount(2, $conversations);
1582
        $this->assertCount(2, $conversations);
1481
    }
1583
    }
1482
 
1584
 
Línea 1483... Línea 1585...
1483
    /**
1585
    /**
1484
     * Tests retrieving conversations when a conversation contains a deleted user.
1586
     * Tests retrieving conversations when a conversation contains a deleted user.
1485
     */
1587
     */
Línea 1486... Línea 1588...
1486
    public function test_get_conversations_with_deleted_user(): void {
1588
    public function test_get_conversations_with_deleted_user(): void {
1487
        $this->resetAfterTest();
1589
        $this->resetAfterTest();
1488
 
1590
 
1489
        // Get a bunch of conversations, some group, some individual and in different states.
1591
        // Get a bunch of conversations, some group, some individual and in different states.
Línea 1544... Línea 1646...
1544
     */
1646
     */
1545
    public function test_get_conversations_deleted_messages(): void {
1647
    public function test_get_conversations_deleted_messages(): void {
1546
        $this->resetAfterTest();
1648
        $this->resetAfterTest();
Línea 1547... Línea 1649...
1547
 
1649
 
1548
        // Get a bunch of conversations, some group, some individual and in different states.
1650
        // Get a bunch of conversations, some group, some individual and in different states.
1549
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1651
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 1550... Línea 1652...
1550
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1652
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
1551
 
1653
 
1552
        $conversations = api::get_conversations($user1->id);
1654
        $conversations = api::get_conversations($user1->id);
Línea 1584... Línea 1686...
1584
     */
1686
     */
1585
    public function test_get_conversations_favourite_conversations_single(): void {
1687
    public function test_get_conversations_favourite_conversations_single(): void {
1586
        $this->resetAfterTest();
1688
        $this->resetAfterTest();
Línea 1587... Línea 1689...
1587
 
1689
 
1588
        // Get a bunch of conversations, some group, some individual and in different states.
1690
        // Get a bunch of conversations, some group, some individual and in different states.
1589
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1691
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 1590... Línea 1692...
1590
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1692
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
1591
 
1693
 
Línea 1592... Línea 1694...
1592
        // Mark a single conversation as favourites.
1694
        // Mark a single conversation as favourites.
Línea 1615... Línea 1717...
1615
                $this->assertEquals($ic2->id, $conv->id);
1717
                $this->assertEquals($ic2->id, $conv->id);
1616
            }
1718
            }
1617
        }
1719
        }
Línea 1618... Línea 1720...
1618
 
1720
 
1619
        // Now, try ONLY favourites of type 'group'.
1721
        // Now, try ONLY favourites of type 'group'.
-
 
1722
        $conversations = api::get_conversations(
-
 
1723
            $user1->id,
-
 
1724
            0,
1620
        $conversations = api::get_conversations($user1->id, 0, 20,
1725
            20,
-
 
1726
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
-
 
1727
            true
1621
            api::MESSAGE_CONVERSATION_TYPE_GROUP, true);
1728
        );
Línea 1622... Línea 1729...
1622
        $this->assertEmpty($conversations);
1729
        $this->assertEmpty($conversations);
1623
 
1730
 
1624
        // And NO favourite conversations.
1731
        // And NO favourite conversations.
Línea 1635... Línea 1742...
1635
     */
1742
     */
1636
    public function test_get_conversations_favourite_conversations(): void {
1743
    public function test_get_conversations_favourite_conversations(): void {
1637
        $this->resetAfterTest();
1744
        $this->resetAfterTest();
Línea 1638... Línea 1745...
1638
 
1745
 
1639
        // Get a bunch of conversations, some group, some individual and in different states.
1746
        // Get a bunch of conversations, some group, some individual and in different states.
1640
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1747
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 1641... Línea 1748...
1641
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1748
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
1642
 
1749
 
Línea 1643... Línea 1750...
1643
        // Try to get ONLY favourite conversations, when only self-conversation exist.
1750
        // Try to get ONLY favourite conversations, when only self-conversation exist.
Línea 1677... Línea 1784...
1677
            $this->assertTrue($conv->isfavourite);
1784
            $this->assertTrue($conv->isfavourite);
1678
            $this->assertNotFalse(array_search($conv->id, $favouriteids));
1785
            $this->assertNotFalse(array_search($conv->id, $favouriteids));
1679
        }
1786
        }
Línea 1680... Línea 1787...
1680
 
1787
 
1681
        // Now, try ONLY favourites of type 'group'.
1788
        // Now, try ONLY favourites of type 'group'.
-
 
1789
        $conversations = api::get_conversations(
-
 
1790
            $user1->id,
-
 
1791
            0,
1682
        $conversations = api::get_conversations($user1->id, 0, 20,
1792
            20,
-
 
1793
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
-
 
1794
            true
1683
            api::MESSAGE_CONVERSATION_TYPE_GROUP, true);
1795
        );
1684
        $this->assertCount(2, $conversations);
1796
        $this->assertCount(2, $conversations);
1685
        foreach ($conversations as $conv) {
1797
        foreach ($conversations as $conv) {
1686
            $this->assertTrue($conv->isfavourite);
1798
            $this->assertTrue($conv->isfavourite);
1687
            $this->assertNotFalse(array_search($conv->id, [$gc2->id, $gc5->id]));
1799
            $this->assertNotFalse(array_search($conv->id, [$gc2->id, $gc5->id]));
Línea 1712... Línea 1824...
1712
 
1824
 
1713
        $conversation = api::create_conversation(
1825
        $conversation = api::create_conversation(
1714
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1826
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1715
            [
1827
            [
1716
                $user1->id,
1828
                $user1->id,
1717
                $user2->id
1829
                $user2->id,
1718
            ],
1830
            ],
1719
            'Individual conversation'
1831
            'Individual conversation'
Línea 1720... Línea 1832...
1720
        );
1832
        );
Línea 1762... Línea 1874...
1762
     */
1874
     */
1763
    public function test_get_conversations_group_linked(): void {
1875
    public function test_get_conversations_group_linked(): void {
1764
        global $CFG, $DB;
1876
        global $CFG, $DB;
1765
        $this->resetAfterTest();
1877
        $this->resetAfterTest();
Línea 1766... Línea -...
1766
 
-
 
1767
 
1878
 
1768
        // Create some users.
1879
        // Create some users.
1769
        $user1 = self::getDataGenerator()->create_user();
1880
        $user1 = self::getDataGenerator()->create_user();
1770
        $user2 = self::getDataGenerator()->create_user();
1881
        $user2 = self::getDataGenerator()->create_user();
Línea 1778... Línea 1889...
1778
        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1889
        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1779
        $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1890
        $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1780
        $group1 = $this->getDataGenerator()->create_group([
1891
        $group1 = $this->getDataGenerator()->create_group([
1781
            'courseid' => $course1->id,
1892
            'courseid' => $course1->id,
1782
            'enablemessaging' => 1,
1893
            'enablemessaging' => 1,
1783
            'picturepath' => $CFG->dirroot . '/lib/tests/fixtures/gd-logo.png'
1894
            'picturepath' => $CFG->dirroot . '/lib/tests/fixtures/gd-logo.png',
1784
        ]);
1895
        ]);
Línea 1785... Línea 1896...
1785
 
1896
 
1786
        // Add users to group1.
1897
        // Add users to group1.
1787
        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
1898
        $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user1->id]);
Línea 1788... Línea 1899...
1788
        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
1899
        $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user2->id]);
1789
 
1900
 
1790
        // Verify the group with the image works as expected.
1901
        // Verify the group with the image works as expected.
1791
        $conversations = api::get_conversations($user1->id);
1902
        $conversations = api::get_conversations($user1->id);
Línea 1799... Línea 1910...
1799
            'courseid' => $course1->id,
1910
            'courseid' => $course1->id,
1800
            'enablemessaging' => 1,
1911
            'enablemessaging' => 1,
1801
        ]);
1912
        ]);
Línea 1802... Línea 1913...
1802
 
1913
 
1803
        // Add users to group2.
1914
        // Add users to group2.
1804
        $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
1915
        $this->getDataGenerator()->create_group_member(['groupid' => $group2->id, 'userid' => $user2->id]);
Línea 1805... Línea 1916...
1805
        $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user3->id));
1916
        $this->getDataGenerator()->create_group_member(['groupid' => $group2->id, 'userid' => $user3->id]);
1806
 
1917
 
1807
        // Verify the group without any image works as expected too.
1918
        // Verify the group without any image works as expected too.
1808
        $conversations = api::get_conversations($user3->id);
1919
        $conversations = api::get_conversations($user3->id);
Línea 1819... Línea 1930...
1819
 
1930
 
1820
   /**
1931
   /**
1821
    * The data provider for get_conversations_mixed.
1932
    * The data provider for get_conversations_mixed.
1822
    *
1933
    *
-
 
1934
    * This provides sets of data to for testing.
1823
    * This provides sets of data to for testing.
1935
    *
1824
    * @return array
1936
    * @return array
1825
    */
1937
    */
1826
   public function get_conversations_mixed_provider() {
1938
    public static function get_conversations_mixed_provider(): array {
1827
       return array(
1939
        return [
1828
            'Test that conversations with messages contacts is correctly ordered.' => array(
1940
            'Test that conversations with messages contacts is correctly ordered.' => [
1829
                'users' => array(
1941
                'usersdata' => [
1830
                    'user1',
1942
                    'user1',
1831
                    'user2',
1943
                    'user2',
1832
                    'user3',
1944
                    'user3',
1833
                ),
1945
                ],
1834
                'contacts' => array(
1946
                'contacts' => [
1835
                ),
1947
                ],
1836
                'messages' => array(
1948
                'messagesdata' => [
1837
                    array(
1949
                    [
1838
                        'from'          => 'user1',
1950
                        'from'          => 'user1',
1839
                        'to'            => 'user2',
1951
                        'to'            => 'user2',
1840
                        'state'         => 'unread',
1952
                        'state'         => 'unread',
1841
                        'subject'       => 'S1',
1953
                        'subject'       => 'S1',
1842
                    ),
1954
                    ],
1843
                    array(
1955
                    [
1844
                        'from'          => 'user2',
1956
                        'from'          => 'user2',
1845
                        'to'            => 'user1',
1957
                        'to'            => 'user1',
1846
                        'state'         => 'unread',
1958
                        'state'         => 'unread',
1847
                        'subject'       => 'S2',
1959
                        'subject'       => 'S2',
1848
                    ),
1960
                    ],
1849
                    array(
1961
                    [
1850
                        'from'          => 'user1',
1962
                        'from'          => 'user1',
1851
                        'to'            => 'user2',
1963
                        'to'            => 'user2',
1852
                        'state'         => 'unread',
1964
                        'state'         => 'unread',
1853
                        'timecreated'   => 0,
1965
                        'timecreated'   => 0,
1854
                        'subject'       => 'S3',
1966
                        'subject'       => 'S3',
1855
                    ),
1967
                    ],
1856
                    array(
1968
                    [
1857
                        'from'          => 'user1',
1969
                        'from'          => 'user1',
1858
                        'to'            => 'user3',
1970
                        'to'            => 'user3',
1859
                        'state'         => 'read',
1971
                        'state'         => 'read',
1860
                        'timemodifier'  => 1,
1972
                        'timemodifier'  => 1,
1861
                        'subject'       => 'S4',
1973
                        'subject'       => 'S4',
1862
                    ),
1974
                    ],
1863
                    array(
1975
                    [
1864
                        'from'          => 'user3',
1976
                        'from'          => 'user3',
1865
                        'to'            => 'user1',
1977
                        'to'            => 'user1',
1866
                        'state'         => 'read',
1978
                        'state'         => 'read',
1867
                        'timemodifier'  => 1,
1979
                        'timemodifier'  => 1,
1868
                        'subject'       => 'S5',
1980
                        'subject'       => 'S5',
1869
                    ),
1981
                    ],
1870
                    array(
1982
                    [
1871
                        'from'          => 'user1',
1983
                        'from'          => 'user1',
1872
                        'to'            => 'user3',
1984
                        'to'            => 'user3',
1873
                        'state'         => 'read',
1985
                        'state'         => 'read',
1874
                        'timecreated'   => 0,
1986
                        'timecreated'   => 0,
1875
                        'subject'       => 'S6',
1987
                        'subject'       => 'S6',
1876
                    ),
1988
                    ],
1877
                ),
1989
                ],
1878
                'expectations' => array(
1990
                'expectations' => [
1879
                    'user1' => array(
1991
                    'user1' => [
1880
                        // User1 has conversed most recently with user3. The most recent message is M5.
1992
                        // User1 has conversed most recently with user3. The most recent message is M5.
1881
                        array(
1993
                        [
1882
                            'messageposition'   => 0,
1994
                            'messageposition'   => 0,
1883
                            'with'              => 'user3',
1995
                            'with'              => 'user3',
1884
                            'subject'           => '<p>S5</p>',
1996
                            'subject'           => '<p>S5</p>',
1885
                            'unreadcount'       => 0,
1997
                            'unreadcount'       => 0,
1886
                        ),
1998
                        ],
1887
                        // User1 has also conversed with user2. The most recent message is S2.
1999
                        // User1 has also conversed with user2. The most recent message is S2.
1888
                        array(
2000
                        [
1889
                            'messageposition'   => 1,
2001
                            'messageposition'   => 1,
1890
                            'with'              => 'user2',
2002
                            'with'              => 'user2',
1891
                            'subject'           => '<p>S2</p>',
2003
                            'subject'           => '<p>S2</p>',
1892
                            'unreadcount'       => 1,
2004
                            'unreadcount'       => 1,
1893
                        ),
2005
                        ],
1894
                    ),
2006
                    ],
1895
                    'user2' => array(
2007
                    'user2' => [
1896
                        // User2 has only conversed with user1. Their most recent shared message was S2.
2008
                        // User2 has only conversed with user1. Their most recent shared message was S2.
1897
                        array(
2009
                        [
1898
                            'messageposition'   => 0,
2010
                            'messageposition'   => 0,
1899
                            'with'              => 'user1',
2011
                            'with'              => 'user1',
1900
                            'subject'           => '<p>S2</p>',
2012
                            'subject'           => '<p>S2</p>',
1901
                            'unreadcount'       => 2,
2013
                            'unreadcount'       => 2,
1902
                        ),
2014
                        ],
1903
                    ),
2015
                    ],
1904
                    'user3' => array(
2016
                    'user3' => [
1905
                        // User3 has only conversed with user1. Their most recent shared message was S5.
2017
                        // User3 has only conversed with user1. Their most recent shared message was S5.
1906
                        array(
2018
                        [
1907
                            'messageposition'   => 0,
2019
                            'messageposition'   => 0,
1908
                            'with'              => 'user1',
2020
                            'with'              => 'user1',
1909
                            'subject'           => '<p>S5</p>',
2021
                            'subject'           => '<p>S5</p>',
1910
                            'unreadcount'       => 0,
2022
                            'unreadcount'       => 0,
1911
                        ),
2023
                        ],
1912
                    ),
2024
                    ],
1913
                ),
2025
                ],
1914
            ),
2026
            ],
1915
            'Test conversations with a single user, where some messages are read and some are not.' => array(
2027
            'Test conversations with a single user, where some messages are read and some are not.' => [
1916
                'users' => array(
2028
                'usersdata' => [
1917
                    'user1',
2029
                    'user1',
1918
                    'user2',
2030
                    'user2',
1919
                ),
2031
                ],
1920
                'contacts' => array(
2032
                'contacts' => [
1921
                ),
2033
                ],
1922
                'messages' => array(
2034
                'messagesdata' => [
1923
                    array(
2035
                    [
1924
                        'from'          => 'user1',
2036
                        'from'          => 'user1',
1925
                        'to'            => 'user2',
2037
                        'to'            => 'user2',
1926
                        'state'         => 'read',
2038
                        'state'         => 'read',
1927
                        'subject'       => 'S1',
2039
                        'subject'       => 'S1',
1928
                    ),
2040
                    ],
1929
                    array(
2041
                    [
1930
                        'from'          => 'user2',
2042
                        'from'          => 'user2',
1931
                        'to'            => 'user1',
2043
                        'to'            => 'user1',
1932
                        'state'         => 'read',
2044
                        'state'         => 'read',
1933
                        'subject'       => 'S2',
2045
                        'subject'       => 'S2',
1934
                    ),
2046
                    ],
1935
                    array(
2047
                    [
1936
                        'from'          => 'user1',
2048
                        'from'          => 'user1',
1937
                        'to'            => 'user2',
2049
                        'to'            => 'user2',
1938
                        'state'         => 'unread',
2050
                        'state'         => 'unread',
1939
                        'timemodifier'  => 1,
2051
                        'timemodifier'  => 1,
1940
                        'subject'       => 'S3',
2052
                        'subject'       => 'S3',
1941
                    ),
2053
                    ],
1942
                    array(
2054
                    [
1943
                        'from'          => 'user1',
2055
                        'from'          => 'user1',
1944
                        'to'            => 'user2',
2056
                        'to'            => 'user2',
1945
                        'state'         => 'unread',
2057
                        'state'         => 'unread',
1946
                        'timemodifier'  => 1,
2058
                        'timemodifier'  => 1,
1947
                        'subject'       => 'S4',
2059
                        'subject'       => 'S4',
1948
                    ),
2060
                    ],
1949
                ),
2061
                ],
1950
                'expectations' => array(
2062
                'expectations' => [
1951
                    // The most recent message between user1 and user2 was S4.
2063
                    // The most recent message between user1 and user2 was S4.
1952
                    'user1' => array(
2064
                    'user1' => [
1953
                        array(
2065
                        [
1954
                            'messageposition'   => 0,
2066
                            'messageposition'   => 0,
1955
                            'with'              => 'user2',
2067
                            'with'              => 'user2',
1956
                            'subject'           => '<p>S4</p>',
2068
                            'subject'           => '<p>S4</p>',
1957
                            'unreadcount'       => 0,
2069
                            'unreadcount'       => 0,
1958
                        ),
2070
                        ],
1959
                    ),
2071
                    ],
1960
                    'user2' => array(
2072
                    'user2' => [
1961
                        // The most recent message between user1 and user2 was S4.
2073
                        // The most recent message between user1 and user2 was S4.
1962
                        array(
2074
                        [
1963
                            'messageposition'   => 0,
2075
                            'messageposition'   => 0,
1964
                            'with'              => 'user1',
2076
                            'with'              => 'user1',
1965
                            'subject'           => '<p>S4</p>',
2077
                            'subject'           => '<p>S4</p>',
1966
                            'unreadcount'       => 2,
2078
                            'unreadcount'       => 2,
1967
                        ),
2079
                        ],
1968
                    ),
2080
                    ],
1969
                ),
2081
                ],
1970
            ),
2082
            ],
1971
            'Test conversations with a single user, where some messages are read and some are not, and messages ' .
2083
            'Test conversations with a single user, where some messages are read and some are not, and messages ' .
1972
            'are out of order' => array(
2084
            'are out of order' => [
1973
            // This can happen through a combination of factors including multi-master DB replication with messages
2085
            // This can happen through a combination of factors including multi-master DB replication with messages
1974
            // read somehow (e.g. API).
2086
            // read somehow (e.g. API).
1975
                'users' => array(
2087
                'usersdata' => [
1976
                    'user1',
2088
                    'user1',
1977
                    'user2',
2089
                    'user2',
1978
                ),
2090
                ],
1979
                'contacts' => array(
2091
                'contacts' => [
1980
                ),
2092
                ],
1981
                'messages' => array(
2093
                'messagesdata' => [
1982
                    array(
2094
                    [
1983
                        'from'          => 'user1',
2095
                        'from'          => 'user1',
1984
                        'to'            => 'user2',
2096
                        'to'            => 'user2',
1985
                        'state'         => 'read',
2097
                        'state'         => 'read',
1986
                        'subject'       => 'S1',
2098
                        'subject'       => 'S1',
1987
                        'timemodifier'  => 1,
2099
                        'timemodifier'  => 1,
1988
                    ),
2100
                    ],
1989
                    array(
2101
                    [
1990
                        'from'          => 'user2',
2102
                        'from'          => 'user2',
1991
                        'to'            => 'user1',
2103
                        'to'            => 'user1',
1992
                        'state'         => 'read',
2104
                        'state'         => 'read',
1993
                        'subject'       => 'S2',
2105
                        'subject'       => 'S2',
1994
                        'timemodifier'  => 2,
2106
                        'timemodifier'  => 2,
1995
                    ),
2107
                    ],
1996
                    array(
2108
                    [
1997
                        'from'          => 'user1',
2109
                        'from'          => 'user1',
1998
                        'to'            => 'user2',
2110
                        'to'            => 'user2',
1999
                        'state'         => 'unread',
2111
                        'state'         => 'unread',
2000
                        'subject'       => 'S3',
2112
                        'subject'       => 'S3',
2001
                    ),
2113
                    ],
2002
                    array(
2114
                    [
2003
                        'from'          => 'user1',
2115
                        'from'          => 'user1',
2004
                        'to'            => 'user2',
2116
                        'to'            => 'user2',
2005
                        'state'         => 'unread',
2117
                        'state'         => 'unread',
2006
                        'subject'       => 'S4',
2118
                        'subject'       => 'S4',
2007
                    ),
2119
                    ],
2008
                ),
2120
                ],
2009
                'expectations' => array(
2121
                'expectations' => [
2010
                    // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
2122
                    // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
2011
                    'user1' => array(
2123
                    'user1' => [
2012
                        array(
2124
                        [
2013
                            'messageposition'   => 0,
2125
                            'messageposition'   => 0,
2014
                            'with'              => 'user2',
2126
                            'with'              => 'user2',
2015
                            'subject'           => '<p>S2</p>',
2127
                            'subject'           => '<p>S2</p>',
2016
                            'unreadcount'       => 0,
2128
                            'unreadcount'       => 0,
2017
                        ),
2129
                        ],
2018
                    ),
2130
                    ],
2019
                    'user2' => array(
2131
                    'user2' => [
2020
                        array(
2132
                        [
2021
                            'messageposition'   => 0,
2133
                            'messageposition'   => 0,
2022
                            'with'              => 'user1',
2134
                            'with'              => 'user1',
2023
                            'subject'           => '<p>S2</p>',
2135
                            'subject'           => '<p>S2</p>',
2024
                            'unreadcount'       => 2
2136
                            'unreadcount'       => 2,
2025
                        ),
2137
                        ],
2026
                    ),
2138
                    ],
2027
                ),
2139
                ],
2028
            ),
2140
            ],
2029
            'Test unread message count is correct for both users' => array(
2141
            'Test unread message count is correct for both users' => [
2030
                'users' => array(
2142
                'usersdata' => [
2031
                    'user1',
2143
                    'user1',
2032
                    'user2',
2144
                    'user2',
2033
                ),
2145
                ],
2034
                'contacts' => array(
2146
                'contacts' => [
2035
                ),
2147
                ],
2036
                'messages' => array(
2148
                'messagesdata' => [
2037
                    array(
2149
                    [
2038
                        'from'          => 'user1',
2150
                        'from'          => 'user1',
2039
                        'to'            => 'user2',
2151
                        'to'            => 'user2',
2040
                        'state'         => 'read',
2152
                        'state'         => 'read',
2041
                        'subject'       => 'S1',
2153
                        'subject'       => 'S1',
2042
                        'timemodifier'  => 1,
2154
                        'timemodifier'  => 1,
2043
                    ),
2155
                    ],
2044
                    array(
2156
                    [
2045
                        'from'          => 'user2',
2157
                        'from'          => 'user2',
2046
                        'to'            => 'user1',
2158
                        'to'            => 'user1',
2047
                        'state'         => 'read',
2159
                        'state'         => 'read',
2048
                        'subject'       => 'S2',
2160
                        'subject'       => 'S2',
2049
                        'timemodifier'  => 2,
2161
                        'timemodifier'  => 2,
2050
                    ),
2162
                    ],
2051
                    array(
2163
                    [
2052
                        'from'          => 'user1',
2164
                        'from'          => 'user1',
2053
                        'to'            => 'user2',
2165
                        'to'            => 'user2',
2054
                        'state'         => 'read',
2166
                        'state'         => 'read',
2055
                        'subject'       => 'S3',
2167
                        'subject'       => 'S3',
2056
                        'timemodifier'  => 3,
2168
                        'timemodifier'  => 3,
2057
                    ),
2169
                    ],
2058
                    array(
2170
                    [
2059
                        'from'          => 'user1',
2171
                        'from'          => 'user1',
2060
                        'to'            => 'user2',
2172
                        'to'            => 'user2',
2061
                        'state'         => 'read',
2173
                        'state'         => 'read',
2062
                        'subject'       => 'S4',
2174
                        'subject'       => 'S4',
2063
                        'timemodifier'  => 4,
2175
                        'timemodifier'  => 4,
2064
                    ),
2176
                    ],
2065
                    array(
2177
                    [
2066
                        'from'          => 'user1',
2178
                        'from'          => 'user1',
2067
                        'to'            => 'user2',
2179
                        'to'            => 'user2',
2068
                        'state'         => 'unread',
2180
                        'state'         => 'unread',
2069
                        'subject'       => 'S5',
2181
                        'subject'       => 'S5',
2070
                        'timemodifier'  => 5,
2182
                        'timemodifier'  => 5,
2071
                    ),
2183
                    ],
2072
                    array(
2184
                    [
2073
                        'from'          => 'user2',
2185
                        'from'          => 'user2',
2074
                        'to'            => 'user1',
2186
                        'to'            => 'user1',
2075
                        'state'         => 'unread',
2187
                        'state'         => 'unread',
2076
                        'subject'       => 'S6',
2188
                        'subject'       => 'S6',
2077
                        'timemodifier'  => 6,
2189
                        'timemodifier'  => 6,
2078
                    ),
2190
                    ],
2079
                    array(
2191
                    [
2080
                        'from'          => 'user1',
2192
                        'from'          => 'user1',
2081
                        'to'            => 'user2',
2193
                        'to'            => 'user2',
2082
                        'state'         => 'unread',
2194
                        'state'         => 'unread',
2083
                        'subject'       => 'S7',
2195
                        'subject'       => 'S7',
2084
                        'timemodifier'  => 7,
2196
                        'timemodifier'  => 7,
2085
                    ),
2197
                    ],
2086
                    array(
2198
                    [
2087
                        'from'          => 'user1',
2199
                        'from'          => 'user1',
2088
                        'to'            => 'user2',
2200
                        'to'            => 'user2',
2089
                        'state'         => 'unread',
2201
                        'state'         => 'unread',
2090
                        'subject'       => 'S8',
2202
                        'subject'       => 'S8',
2091
                        'timemodifier'  => 8,
2203
                        'timemodifier'  => 8,
2092
                    ),
2204
                    ],
2093
                ),
2205
                ],
2094
                'expectations' => array(
2206
                'expectations' => [
2095
                    // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
2207
                    // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
2096
                    'user1' => array(
2208
                    'user1' => [
2097
                        array(
2209
                        [
2098
                            'messageposition'   => 0,
2210
                            'messageposition'   => 0,
2099
                            'with'              => 'user2',
2211
                            'with'              => 'user2',
2100
                            'subject'           => '<p>S8</p>',
2212
                            'subject'           => '<p>S8</p>',
2101
                            'unreadcount'       => 1,
2213
                            'unreadcount'       => 1,
2102
                        ),
2214
                        ],
2103
                    ),
2215
                    ],
2104
                    'user2' => array(
2216
                    'user2' => [
2105
                        array(
2217
                        [
2106
                            'messageposition'   => 0,
2218
                            'messageposition'   => 0,
2107
                            'with'              => 'user1',
2219
                            'with'              => 'user1',
2108
                            'subject'           => '<p>S8</p>',
2220
                            'subject'           => '<p>S8</p>',
2109
                            'unreadcount'       => 3,
2221
                            'unreadcount'       => 3,
2110
                        ),
2222
                        ],
2111
                    ),
2223
                    ],
2112
                ),
2224
                ],
2113
            ),
2225
            ],
2114
        );
2226
        ];
Línea 2115... Línea 2227...
2115
    }
2227
    }
2116
 
2228
 
2117
    /**
2229
    /**
Línea 2142... Línea 2254...
2142
     */
2254
     */
2143
    public function test_get_conversations_mixed($usersdata, $contacts, $messagesdata, $expectations): void {
2255
    public function test_get_conversations_mixed($usersdata, $contacts, $messagesdata, $expectations): void {
2144
        global $DB;
2256
        global $DB;
2145
        $this->resetAfterTest();
2257
        $this->resetAfterTest();
Línea 2146... Línea -...
2146
 
-
 
2147
 
2258
 
Línea 2148... Línea 2259...
2148
        $this->redirectMessages();
2259
        $this->redirectMessages();
2149
 
2260
 
2150
        // Create all of the users.
2261
        // Create all of the users.
2151
        $users = array();
2262
        $users = [];
2152
        foreach ($usersdata as $username) {
2263
        foreach ($usersdata as $username) {
Línea 2153... Línea 2264...
2153
            $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
2264
            $users[$username] = $this->getDataGenerator()->create_user(['username' => $username]);
2154
        }
2265
        }
2155
 
2266
 
Línea 2507... Línea 2618...
2507
        // Confirm the members data is correct.
2618
        // Confirm the members data is correct.
2508
        $members = $convmessages['members'];
2619
        $members = $convmessages['members'];
2509
        $this->assertEquals(3, count($members));
2620
        $this->assertEquals(3, count($members));
Línea 2510... Línea 2621...
2510
 
2621
 
2511
        // Retrieve the messages from $time + 3, which should only be the 2 last messages.
2622
        // Retrieve the messages from $time + 3, which should only be the 2 last messages.
-
 
2623
        $convmessages = api::get_conversation_messages(
-
 
2624
            $user1->id,
-
 
2625
            $conversation->id,
-
 
2626
            0,
2512
        $convmessages = api::get_conversation_messages($user1->id, $conversation->id, 0, 0,
2627
            0,
-
 
2628
            'timecreated ASC',
-
 
2629
            $time + 3
Línea 2513... Línea 2630...
2513
            'timecreated ASC', $time + 3);
2630
        );
2514
 
2631
 
Línea 2515... Línea 2632...
2515
        // Confirm the conversation id is correct.
2632
        // Confirm the conversation id is correct.
Línea 2557... Línea 2674...
2557
        testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2674
        testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2558
        testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2675
        testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2559
        testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2676
        testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
Línea 2560... Línea 2677...
2560
 
2677
 
2561
        // Retrieve the messages up until $time + 4, which should be all of them.
2678
        // Retrieve the messages up until $time + 4, which should be all of them.
-
 
2679
        $convmessages = api::get_conversation_messages(
-
 
2680
            $user1->id,
-
 
2681
            $conversation->id,
-
 
2682
            0,
-
 
2683
            0,
-
 
2684
            'timecreated ASC',
2562
        $convmessages = api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC',
2685
            0,
-
 
2686
            $time + 4
Línea 2563... Línea 2687...
2563
            0, $time + 4);
2687
        );
2564
 
2688
 
Línea 2565... Línea 2689...
2565
        // Confirm the conversation id is correct.
2689
        // Confirm the conversation id is correct.
Línea 2582... Línea 2706...
2582
        // Confirm the members data is correct.
2706
        // Confirm the members data is correct.
2583
        $members = $convmessages['members'];
2707
        $members = $convmessages['members'];
2584
        $this->assertEquals(3, count($members));
2708
        $this->assertEquals(3, count($members));
Línea 2585... Línea 2709...
2585
 
2709
 
2586
        // Retrieve the messages up until $time + 2, which should be the first two.
2710
        // Retrieve the messages up until $time + 2, which should be the first two.
-
 
2711
        $convmessages = api::get_conversation_messages(
-
 
2712
            $user1->id,
-
 
2713
            $conversation->id,
-
 
2714
            0,
-
 
2715
            0,
-
 
2716
            'timecreated ASC',
2587
        $convmessages = api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC',
2717
            0,
-
 
2718
            $time + 2
Línea 2588... Línea 2719...
2588
            0, $time + 2);
2719
        );
2589
 
2720
 
Línea 2590... Línea 2721...
2590
        // Confirm the conversation id is correct.
2721
        // Confirm the conversation id is correct.
Línea 2632... Línea 2763...
2632
        testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2763
        testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2633
        testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2764
        testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2634
        testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2765
        testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
Línea 2635... Línea 2766...
2635
 
2766
 
2636
        // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
2767
        // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
-
 
2768
        $convmessages = api::get_conversation_messages(
-
 
2769
            $user1->id,
-
 
2770
            $conversation->id,
-
 
2771
            0,
2637
        $convmessages = api::get_conversation_messages($user1->id, $conversation->id, 0, 0,
2772
            0,
-
 
2773
            'timecreated ASC',
-
 
2774
            $time + 2,
-
 
2775
            $time + 3
Línea 2638... Línea 2776...
2638
            'timecreated ASC', $time + 2, $time + 3);
2776
        );
2639
 
2777
 
Línea 2640... Línea 2778...
2640
        // Confirm the conversation id is correct.
2778
        // Confirm the conversation id is correct.
Línea 2653... Línea 2791...
2653
        // Confirm the members data is correct.
2791
        // Confirm the members data is correct.
2654
        $members = $convmessages['members'];
2792
        $members = $convmessages['members'];
2655
        $this->assertEquals(2, count($members));
2793
        $this->assertEquals(2, count($members));
2656
    }
2794
    }
Línea 2657... Línea -...
2657
 
-
 
2658
 
2795
 
2659
    /**
2796
    /**
2660
     * Test retrieving conversation messages by providing a limitfrom value.
2797
     * Test retrieving conversation messages by providing a limitfrom value.
2661
     */
2798
     */
2662
    public function test_get_conversation_messages_limitfrom_only(): void {
2799
    public function test_get_conversation_messages_limitfrom_only(): void {
Línea 2867... Línea 3004...
2867
     */
3004
     */
2868
    public function test_delete_conversation_by_id(): void {
3005
    public function test_delete_conversation_by_id(): void {
2869
        global $DB;
3006
        global $DB;
2870
        $this->resetAfterTest();
3007
        $this->resetAfterTest();
Línea 2871... Línea -...
2871
 
-
 
2872
 
3008
 
2873
        // Create some users.
3009
        // Create some users.
2874
        $user1 = self::getDataGenerator()->create_user();
3010
        $user1 = self::getDataGenerator()->create_user();
Línea 2875... Línea 3011...
2875
        $user2 = self::getDataGenerator()->create_user();
3011
        $user2 = self::getDataGenerator()->create_user();
Línea 2893... Línea 3029...
2893
        $conversationid = api::get_conversation_between_users([$user1->id, $user2->id]);
3029
        $conversationid = api::get_conversation_between_users([$user1->id, $user2->id]);
Línea 2894... Línea 3030...
2894
 
3030
 
2895
        // Delete the individual conversation between user1 and user2 (only for user1).
3031
        // Delete the individual conversation between user1 and user2 (only for user1).
Línea 2896... Línea 3032...
2896
        api::delete_conversation_by_id($user1->id, $conversationid);
3032
        api::delete_conversation_by_id($user1->id, $conversationid);
2897
 
3033
 
2898
        $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
3034
        $muas = $DB->get_records('message_user_actions', [], 'timecreated ASC');
2899
        $this->assertCount(4, $muas);
3035
        $this->assertCount(4, $muas);
Línea 2900... Línea 3036...
2900
        // Sort by id.
3036
        // Sort by id.
Línea 2922... Línea 3058...
2922
        $this->assertEquals(api::MESSAGE_ACTION_DELETED, $mua4->action);
3058
        $this->assertEquals(api::MESSAGE_ACTION_DELETED, $mua4->action);
Línea 2923... Línea 3059...
2923
 
3059
 
2924
        // Delete the self-conversation as user 1.
3060
        // Delete the self-conversation as user 1.
Línea 2925... Línea 3061...
2925
        api::delete_conversation_by_id($user1->id, $sc1->id);
3061
        api::delete_conversation_by_id($user1->id, $sc1->id);
2926
 
3062
 
Línea 2927... Línea 3063...
2927
        $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
3063
        $muas = $DB->get_records('message_user_actions', [], 'timecreated ASC');
2928
        $this->assertCount(5, $muas);
3064
        $this->assertCount(5, $muas);
Línea 3067... Línea 3203...
3067
     */
3203
     */
3068
    public function test_can_send_message_without_sendmessage_cap(): void {
3204
    public function test_can_send_message_without_sendmessage_cap(): void {
3069
        global $DB;
3205
        global $DB;
3070
        $this->resetAfterTest();
3206
        $this->resetAfterTest();
Línea 3071... Línea -...
3071
 
-
 
3072
 
3207
 
3073
        // Create some users.
3208
        // Create some users.
3074
        $user1 = self::getDataGenerator()->create_user();
3209
        $user1 = self::getDataGenerator()->create_user();
Línea 3075... Línea 3210...
3075
        $user2 = self::getDataGenerator()->create_user();
3210
        $user2 = self::getDataGenerator()->create_user();
3076
 
3211
 
Línea 3077... Línea 3212...
3077
        // Set as the user 1.
3212
        // Set as the user 1.
3078
        $this->setUser($user1);
3213
        $this->setUser($user1);
-
 
3214
 
3079
 
3215
        // Remove the capability to send a message.
-
 
3216
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
3080
        // Remove the capability to send a message.
3217
        unassign_capability(
-
 
3218
            'moodle/site:sendmessage',
Línea 3081... Línea 3219...
3081
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
3219
            $roleids['user'],
3082
        unassign_capability('moodle/site:sendmessage', $roleids['user'],
3220
            \context_system::instance()
3083
            \context_system::instance());
3221
        );
Línea 3206... Línea 3344...
3206
     */
3344
     */
3207
    public function test_can_send_message_with_messageanyuser_cap(): void {
3345
    public function test_can_send_message_with_messageanyuser_cap(): void {
3208
        global $DB;
3346
        global $DB;
3209
        $this->resetAfterTest();
3347
        $this->resetAfterTest();
Línea 3210... Línea -...
3210
 
-
 
3211
 
3348
 
3212
        // Create some users.
3349
        // Create some users.
3213
        $teacher1 = self::getDataGenerator()->create_user();
3350
        $teacher1 = self::getDataGenerator()->create_user();
3214
        $student1 = self::getDataGenerator()->create_user();
3351
        $student1 = self::getDataGenerator()->create_user();
Línea 3269... Línea 3406...
3269
        $this->resetAfterTest();
3406
        $this->resetAfterTest();
Línea 3270... Línea 3407...
3270
 
3407
 
3271
        $user1 = self::getDataGenerator()->create_user();
3408
        $user1 = self::getDataGenerator()->create_user();
Línea 3272... Línea 3409...
3272
        $user2 = self::getDataGenerator()->create_user();
3409
        $user2 = self::getDataGenerator()->create_user();
3273
 
3410
 
Línea 3274... Línea 3411...
3274
        $authenticateduserrole = $DB->get_record('role', array('shortname' => 'user'));
3411
        $authenticateduserrole = $DB->get_record('role', ['shortname' => 'user']);
3275
        assign_capability('moodle/site:messageanyuser', CAP_ALLOW, $authenticateduserrole->id, \context_system::instance(), true);
3412
        assign_capability('moodle/site:messageanyuser', CAP_ALLOW, $authenticateduserrole->id, \context_system::instance(), true);
Línea 3287... Línea 3424...
3287
        $this->resetAfterTest();
3424
        $this->resetAfterTest();
Línea 3288... Línea 3425...
3288
 
3425
 
3289
        $user1 = self::getDataGenerator()->create_user();
3426
        $user1 = self::getDataGenerator()->create_user();
Línea 3290... Línea 3427...
3290
        $user2 = self::getDataGenerator()->create_user();
3427
        $user2 = self::getDataGenerator()->create_user();
3291
 
3428
 
Línea 3292... Línea 3429...
3292
        $authenticateduserrole = $DB->get_record('role', array('shortname' => 'user'));
3429
        $authenticateduserrole = $DB->get_record('role', ['shortname' => 'user']);
3293
        assign_capability('moodle/site:readallmessages', CAP_ALLOW, $authenticateduserrole->id, \context_system::instance(), true);
3430
        assign_capability('moodle/site:readallmessages', CAP_ALLOW, $authenticateduserrole->id, \context_system::instance(), true);
Línea 3320... Línea 3457...
3320
    public function test_can_send_message_even_if_blocked_shared_course_with_message_any_user_cap(): void {
3457
    public function test_can_send_message_even_if_blocked_shared_course_with_message_any_user_cap(): void {
3321
        global $DB;
3458
        global $DB;
Línea 3322... Línea 3459...
3322
 
3459
 
Línea 3323... Línea 3460...
3323
        $this->resetAfterTest();
3460
        $this->resetAfterTest();
Línea 3324... Línea 3461...
3324
 
3461
 
3325
        $editingteacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
3462
        $editingteacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
Línea 3326... Línea 3463...
3326
 
3463
 
Línea 3327... Línea 3464...
3327
        $teacher = self::getDataGenerator()->create_user();
3464
        $teacher = self::getDataGenerator()->create_user();
3328
        $student = self::getDataGenerator()->create_user();
3465
        $student = self::getDataGenerator()->create_user();
Línea -... Línea 3466...
-
 
3466
 
3329
 
3467
        $course = self::getDataGenerator()->create_course();
-
 
3468
 
-
 
3469
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $editingteacherrole->id);
3330
        $course = self::getDataGenerator()->create_course();
3470
        $this->getDataGenerator()->enrol_user($student->id, $course->id);
-
 
3471
 
-
 
3472
        assign_capability(
Línea 3331... Línea 3473...
3331
 
3473
            'moodle/site:messageanyuser',
3332
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $editingteacherrole->id);
3474
            CAP_ALLOW,
3333
        $this->getDataGenerator()->enrol_user($student->id, $course->id);
3475
            $editingteacherrole->id,
Línea 3353... Línea 3495...
3353
        // Create an individual conversation between user1 and user2.
3495
        // Create an individual conversation between user1 and user2.
3354
        $ic1 = api::create_conversation(
3496
        $ic1 = api::create_conversation(
3355
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3497
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3356
            [
3498
            [
3357
                $user1->id,
3499
                $user1->id,
3358
                $user2->id
3500
                $user2->id,
3359
            ]
3501
            ]
3360
        );
3502
        );
Línea 3361... Línea 3503...
3361
 
3503
 
3362
        // Create a group conversation between and users 1, 2 and 3.
3504
        // Create a group conversation between and users 1, 2 and 3.
3363
        $gc1 = api::create_conversation(
3505
        $gc1 = api::create_conversation(
3364
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3506
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3365
            [
3507
            [
3366
                $user1->id,
3508
                $user1->id,
3367
                $user2->id,
3509
                $user2->id,
3368
                $user3->id
3510
                $user3->id,
3369
            ]
3511
            ]
Línea 3370... Línea 3512...
3370
        );
3512
        );
3371
 
3513
 
Línea 3396... Línea 3538...
3396
     */
3538
     */
3397
    public function test_can_send_message_to_conversation_sendmessage_cap(): void {
3539
    public function test_can_send_message_to_conversation_sendmessage_cap(): void {
3398
        global $DB;
3540
        global $DB;
3399
        $this->resetAfterTest();
3541
        $this->resetAfterTest();
Línea 3400... Línea -...
3400
 
-
 
3401
 
3542
 
3402
        $user1 = self::getDataGenerator()->create_user();
3543
        $user1 = self::getDataGenerator()->create_user();
3403
        $user2 = self::getDataGenerator()->create_user();
3544
        $user2 = self::getDataGenerator()->create_user();
Línea 3404... Línea 3545...
3404
        $user3 = self::getDataGenerator()->create_user();
3545
        $user3 = self::getDataGenerator()->create_user();
Línea 3412... Línea 3553...
3412
        // Create an individual conversation between user1 and user2.
3553
        // Create an individual conversation between user1 and user2.
3413
        $ic1 = api::create_conversation(
3554
        $ic1 = api::create_conversation(
3414
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3555
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3415
            [
3556
            [
3416
                $user1->id,
3557
                $user1->id,
3417
                $user2->id
3558
                $user2->id,
3418
            ]
3559
            ]
3419
        );
3560
        );
Línea 3420... Línea 3561...
3420
 
3561
 
3421
        // Group conversation between and users 1, 2 and 3.
3562
        // Group conversation between and users 1, 2 and 3.
3422
        $gc1 = api::create_conversation(
3563
        $gc1 = api::create_conversation(
3423
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3564
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3424
            [
3565
            [
3425
                $user1->id,
3566
                $user1->id,
3426
                $user2->id,
3567
                $user2->id,
3427
                $user3->id
3568
                $user3->id,
3428
            ]
3569
            ]
Línea 3429... Línea 3570...
3429
        );
3570
        );
3430
 
3571
 
Línea 3446... Línea 3587...
3446
     */
3587
     */
3447
    public function test_can_send_message_to_conversation_messageanyuser_cap(): void {
3588
    public function test_can_send_message_to_conversation_messageanyuser_cap(): void {
3448
        global $DB;
3589
        global $DB;
3449
        $this->resetAfterTest();
3590
        $this->resetAfterTest();
Línea 3450... Línea -...
3450
 
-
 
3451
 
3591
 
3452
        $user1 = self::getDataGenerator()->create_user();
3592
        $user1 = self::getDataGenerator()->create_user();
3453
        $user2 = self::getDataGenerator()->create_user();
3593
        $user2 = self::getDataGenerator()->create_user();
Línea 3454... Línea 3594...
3454
        $user3 = self::getDataGenerator()->create_user();
3594
        $user3 = self::getDataGenerator()->create_user();
Línea 3462... Línea 3602...
3462
        // Create an individual conversation between user1 and user2.
3602
        // Create an individual conversation between user1 and user2.
3463
        $ic1 = api::create_conversation(
3603
        $ic1 = api::create_conversation(
3464
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3604
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3465
            [
3605
            [
3466
                $user1->id,
3606
                $user1->id,
3467
                $user2->id
3607
                $user2->id,
3468
            ]
3608
            ]
3469
        );
3609
        );
Línea 3470... Línea 3610...
3470
 
3610
 
3471
        // Group conversation between and users 1, 2 and 3.
3611
        // Group conversation between and users 1, 2 and 3.
3472
        $gc1 = api::create_conversation(
3612
        $gc1 = api::create_conversation(
3473
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3613
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3474
            [
3614
            [
3475
                $user1->id,
3615
                $user1->id,
3476
                $user2->id,
3616
                $user2->id,
3477
                $user3->id
3617
                $user3->id,
3478
            ]
3618
            ]
Línea 3479... Línea 3619...
3479
        );
3619
        );
3480
 
3620
 
Línea 3517... Línea 3657...
3517
        // Create an individual conversation between user1 and user2.
3657
        // Create an individual conversation between user1 and user2.
3518
        $ic1 = api::create_conversation(
3658
        $ic1 = api::create_conversation(
3519
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3659
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3520
            [
3660
            [
3521
                $user1->id,
3661
                $user1->id,
3522
                $user2->id
3662
                $user2->id,
3523
            ]
3663
            ]
3524
        );
3664
        );
Línea 3525... Línea 3665...
3525
 
3665
 
3526
        // Create a group conversation between and users 1, 2 and 3.
3666
        // Create a group conversation between and users 1, 2 and 3.
3527
        $gc1 = api::create_conversation(
3667
        $gc1 = api::create_conversation(
3528
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3668
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3529
            [
3669
            [
3530
                $user1->id,
3670
                $user1->id,
3531
                $user2->id,
3671
                $user2->id,
3532
                $user3->id
3672
                $user3->id,
3533
            ]
3673
            ]
Línea 3534... Línea 3674...
3534
        );
3674
        );
3535
 
3675
 
Línea 3556... Línea 3696...
3556
        // Create an individual conversation between user1 and user2.
3696
        // Create an individual conversation between user1 and user2.
3557
        $ic1 = api::create_conversation(
3697
        $ic1 = api::create_conversation(
3558
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3698
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3559
            [
3699
            [
3560
                $user1->id,
3700
                $user1->id,
3561
                $user2->id
3701
                $user2->id,
3562
            ]
3702
            ]
3563
        );
3703
        );
Línea 3564... Línea 3704...
3564
 
3704
 
3565
        // Create a group conversation between and users 1, 2 and 3.
3705
        // Create a group conversation between and users 1, 2 and 3.
3566
        $gc1 = api::create_conversation(
3706
        $gc1 = api::create_conversation(
3567
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3707
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3568
            [
3708
            [
3569
                $user1->id,
3709
                $user1->id,
3570
                $user2->id,
3710
                $user2->id,
3571
                $user3->id
3711
                $user3->id,
3572
            ]
3712
            ]
Línea 3573... Línea 3713...
3573
        );
3713
        );
3574
 
3714
 
Línea 3604... Línea 3744...
3604
        // Create an individual conversation between user1 and user2.
3744
        // Create an individual conversation between user1 and user2.
3605
        $ic1 = api::create_conversation(
3745
        $ic1 = api::create_conversation(
3606
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3746
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3607
            [
3747
            [
3608
                $user1->id,
3748
                $user1->id,
3609
                $user2->id
3749
                $user2->id,
3610
            ]
3750
            ]
3611
        );
3751
        );
Línea 3612... Línea 3752...
3612
 
3752
 
3613
        // Create a group conversation between and users 1, 2 and 3.
3753
        // Create a group conversation between and users 1, 2 and 3.
3614
        $gc1 = api::create_conversation(
3754
        $gc1 = api::create_conversation(
3615
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3755
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3616
            [
3756
            [
3617
                $user1->id,
3757
                $user1->id,
3618
                $user2->id,
3758
                $user2->id,
3619
                $user3->id
3759
                $user3->id,
3620
            ]
3760
            ]
Línea 3621... Línea 3761...
3621
        );
3761
        );
3622
 
3762
 
Línea 3651... Línea 3791...
3651
        // Create an individual conversation between user1 and user2.
3791
        // Create an individual conversation between user1 and user2.
3652
        $ic1 = api::create_conversation(
3792
        $ic1 = api::create_conversation(
3653
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3793
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3654
            [
3794
            [
3655
                $user1->id,
3795
                $user1->id,
3656
                $user2->id
3796
                $user2->id,
3657
            ]
3797
            ]
3658
        );
3798
        );
Línea 3659... Línea 3799...
3659
 
3799
 
3660
        // Create a group conversation between and users 1, 2 and 3.
3800
        // Create a group conversation between and users 1, 2 and 3.
3661
        $gc1 = api::create_conversation(
3801
        $gc1 = api::create_conversation(
3662
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3802
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3663
            [
3803
            [
3664
                $user1->id,
3804
                $user1->id,
3665
                $user2->id,
3805
                $user2->id,
3666
                $user3->id
3806
                $user3->id,
3667
            ]
3807
            ]
Línea 3668... Línea 3808...
3668
        );
3808
        );
3669
 
3809
 
Línea 3706... Línea 3846...
3706
        // Create an individual conversation between user1 and user2.
3846
        // Create an individual conversation between user1 and user2.
3707
        $ic1 = api::create_conversation(
3847
        $ic1 = api::create_conversation(
3708
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3848
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3709
            [
3849
            [
3710
                $user1->id,
3850
                $user1->id,
3711
                $user2->id
3851
                $user2->id,
3712
            ]
3852
            ]
3713
        );
3853
        );
Línea 3714... Línea 3854...
3714
 
3854
 
3715
        // Create a group conversation between and users 1, 2 and 3.
3855
        // Create a group conversation between and users 1, 2 and 3.
3716
        $gc1 = api::create_conversation(
3856
        $gc1 = api::create_conversation(
3717
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3857
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
3718
            [
3858
            [
3719
                $user1->id,
3859
                $user1->id,
3720
                $user2->id,
3860
                $user2->id,
3721
                $user3->id
3861
                $user3->id,
3722
            ]
3862
            ]
Línea 3723... Línea 3863...
3723
        );
3863
        );
3724
 
3864
 
Línea 3887... Línea 4027...
3887
     */
4027
     */
3888
    public function test_get_blocked_users(): void {
4028
    public function test_get_blocked_users(): void {
3889
        global $USER;
4029
        global $USER;
3890
        $this->resetAfterTest();
4030
        $this->resetAfterTest();
Línea 3891... Línea -...
3891
 
-
 
3892
 
4031
 
3893
        // Set this user as the admin.
4032
        // Set this user as the admin.
Línea 3894... Línea 4033...
3894
        $this->setAdminUser();
4033
        $this->setAdminUser();
3895
 
4034
 
Línea 4017... Línea 4156...
4017
 
4156
 
4018
        $conversation = api::create_conversation(
4157
        $conversation = api::create_conversation(
4019
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4158
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4020
            [
4159
            [
4021
                $user1->id,
4160
                $user1->id,
4022
                $user2->id
4161
                $user2->id,
4023
            ]
4162
            ]
4024
        );
4163
        );
Línea 4025... Línea 4164...
4025
        $conversationid = $conversation->id;
4164
        $conversationid = $conversation->id;
-
 
4165
 
4026
 
4166
        $this->assertEquals(
-
 
4167
            2,
4027
        $this->assertEquals(2,
4168
            api::count_conversation_members($conversationid)
Línea 4028... Línea 4169...
4028
            api::count_conversation_members($conversationid));
4169
        );
4029
    }
4170
    }
4030
 
4171
 
Línea 4040... Línea 4181...
4040
 
4181
 
4041
        $conversation = api::create_conversation(
4182
        $conversation = api::create_conversation(
4042
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4183
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4043
            [
4184
            [
4044
                $user1->id,
4185
                $user1->id,
4045
                $user2->id
4186
                $user2->id,
4046
            ]
4187
            ]
4047
        );
4188
        );
Línea 4048... Línea 4189...
4048
        $conversationid = $conversation->id;
4189
        $conversationid = $conversation->id;
Línea 4063... Línea 4204...
4063
 
4204
 
4064
        $conversation = api::create_conversation(
4205
        $conversation = api::create_conversation(
4065
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4206
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4066
            [
4207
            [
4067
                $user1->id,
4208
                $user1->id,
4068
                $user2->id
4209
                $user2->id,
4069
            ]
4210
            ]
4070
        );
4211
        );
Línea 4071... Línea 4212...
4071
        $conversationid = $conversation->id;
4212
        $conversationid = $conversation->id;
4072
 
4213
 
-
 
4214
        $this->assertNull(api::add_members_to_conversation([$user3->id], $conversationid));
4073
        $this->assertNull(api::add_members_to_conversation([$user3->id], $conversationid));
4215
        $this->assertEquals(
-
 
4216
            3,
4074
        $this->assertEquals(3,
4217
            api::count_conversation_members($conversationid)
Línea 4075... Línea 4218...
4075
            api::count_conversation_members($conversationid));
4218
        );
4076
    }
4219
    }
4077
 
4220
 
Línea 4099... Línea 4242...
4099
 
4242
 
4100
        $conversation = api::create_conversation(
4243
        $conversation = api::create_conversation(
4101
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4244
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4102
            [
4245
            [
4103
                $user1->id,
4246
                $user1->id,
4104
                $user2->id
4247
                $user2->id,
4105
            ]
4248
            ]
4106
        );
4249
        );
Línea 4107... Línea 4250...
4107
        $conversationid = $conversation->id;
4250
        $conversationid = $conversation->id;
4108
 
4251
 
4109
        // Don't throw an error for non existing user, but don't add it as a member.
4252
        // Don't throw an error for non existing user, but don't add it as a member.
-
 
4253
        $this->assertNull(api::add_members_to_conversation([0], $conversationid));
4110
        $this->assertNull(api::add_members_to_conversation([0], $conversationid));
4254
        $this->assertEquals(
-
 
4255
            2,
4111
        $this->assertEquals(2,
4256
            api::count_conversation_members($conversationid)
Línea 4112... Línea 4257...
4112
            api::count_conversation_members($conversationid));
4257
        );
4113
    }
4258
    }
4114
 
4259
 
Línea 4123... Línea 4268...
4123
 
4268
 
4124
        $conversation = api::create_conversation(
4269
        $conversation = api::create_conversation(
4125
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4270
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4126
            [
4271
            [
4127
                $user1->id,
4272
                $user1->id,
4128
                $user2->id
4273
                $user2->id,
4129
            ]
4274
            ]
4130
        );
4275
        );
Línea 4131... Línea 4276...
4131
        $conversationid = $conversation->id;
4276
        $conversationid = $conversation->id;
4132
 
4277
 
4133
        // Don't add as a member a user that is already conversation member.
4278
        // Don't add as a member a user that is already conversation member.
-
 
4279
        $this->assertNull(api::add_members_to_conversation([$user1->id], $conversationid));
4134
        $this->assertNull(api::add_members_to_conversation([$user1->id], $conversationid));
4280
        $this->assertEquals(
-
 
4281
            2,
4135
        $this->assertEquals(2,
4282
            api::count_conversation_members($conversationid)
Línea 4136... Línea 4283...
4136
            api::count_conversation_members($conversationid));
4283
        );
4137
    }
4284
    }
4138
 
4285
 
Línea 4149... Línea 4296...
4149
 
4296
 
4150
        $conversation = api::create_conversation(
4297
        $conversation = api::create_conversation(
4151
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4298
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4152
            [
4299
            [
4153
                $user1->id,
4300
                $user1->id,
4154
                $user2->id
4301
                $user2->id,
4155
            ]
4302
            ]
4156
        );
4303
        );
Línea 4157... Línea 4304...
4157
        $conversationid = $conversation->id;
4304
        $conversationid = $conversation->id;
4158
 
4305
 
-
 
4306
        $this->assertNull(api::add_members_to_conversation([$user3->id, $user4->id], $conversationid));
4159
        $this->assertNull(api::add_members_to_conversation([$user3->id, $user4->id], $conversationid));
4307
        $this->assertEquals(
-
 
4308
            4,
4160
        $this->assertEquals(4,
4309
            api::count_conversation_members($conversationid)
Línea 4161... Línea 4310...
4161
            api::count_conversation_members($conversationid));
4310
        );
4162
    }
4311
    }
4163
 
4312
 
Línea 4173... Línea 4322...
4173
 
4322
 
4174
        $conversation = api::create_conversation(
4323
        $conversation = api::create_conversation(
4175
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4324
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4176
            [
4325
            [
4177
                $user1->id,
4326
                $user1->id,
4178
                $user2->id
4327
                $user2->id,
4179
            ]
4328
            ]
4180
        );
4329
        );
Línea 4181... Línea 4330...
4181
        $conversationid = $conversation->id;
4330
        $conversationid = $conversation->id;
4182
 
4331
 
4183
        // Don't throw errors, but don't add as members users don't exist or are already conversation members.
4332
        // Don't throw errors, but don't add as members users don't exist or are already conversation members.
-
 
4333
        $this->assertNull(api::add_members_to_conversation([$user3->id, $user1->id, 0], $conversationid));
4184
        $this->assertNull(api::add_members_to_conversation([$user3->id, $user1->id, 0], $conversationid));
4334
        $this->assertEquals(
-
 
4335
            3,
4185
        $this->assertEquals(3,
4336
            api::count_conversation_members($conversationid)
Línea 4186... Línea 4337...
4186
            api::count_conversation_members($conversationid));
4337
        );
4187
    }
4338
    }
4188
 
4339
 
Línea 4197... Línea 4348...
4197
 
4348
 
4198
        $conversation = api::create_conversation(
4349
        $conversation = api::create_conversation(
4199
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4350
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4200
            [
4351
            [
4201
                $user1->id,
4352
                $user1->id,
4202
                $user2->id
4353
                $user2->id,
4203
            ]
4354
            ]
4204
        );
4355
        );
Línea 4205... Línea 4356...
4205
        $conversationid = $conversation->id;
4356
        $conversationid = $conversation->id;
Línea 4219... Línea 4370...
4219
 
4370
 
4220
        $conversation = api::create_conversation(
4371
        $conversation = api::create_conversation(
4221
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4372
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4222
            [
4373
            [
4223
                $user1->id,
4374
                $user1->id,
4224
                $user2->id
4375
                $user2->id,
4225
            ]
4376
            ]
4226
        );
4377
        );
Línea 4227... Línea 4378...
4227
        $conversationid = $conversation->id;
4378
        $conversationid = $conversation->id;
4228
 
4379
 
-
 
4380
        $this->assertNull(api::remove_members_from_conversation([$user1->id], $conversationid));
4229
        $this->assertNull(api::remove_members_from_conversation([$user1->id], $conversationid));
4381
        $this->assertEquals(
-
 
4382
            1,
4230
        $this->assertEquals(1,
4383
            api::count_conversation_members($conversationid)
Línea 4231... Línea 4384...
4231
            api::count_conversation_members($conversationid));
4384
        );
4232
    }
4385
    }
4233
 
4386
 
Línea 4255... Línea 4408...
4255
 
4408
 
4256
        $conversation = api::create_conversation(
4409
        $conversation = api::create_conversation(
4257
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4410
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4258
            [
4411
            [
4259
                $user1->id,
4412
                $user1->id,
4260
                $user2->id
4413
                $user2->id,
4261
            ]
4414
            ]
4262
        );
4415
        );
Línea 4263... Línea 4416...
4263
        $conversationid = $conversation->id;
4416
        $conversationid = $conversation->id;
4264
 
4417
 
-
 
4418
        $this->assertNull(api::remove_members_from_conversation([0], $conversationid));
4265
        $this->assertNull(api::remove_members_from_conversation([0], $conversationid));
4419
        $this->assertEquals(
-
 
4420
            2,
4266
        $this->assertEquals(2,
4421
            api::count_conversation_members($conversationid)
Línea 4267... Línea 4422...
4267
            api::count_conversation_members($conversationid));
4422
        );
4268
    }
4423
    }
4269
 
4424
 
Línea 4280... Línea 4435...
4280
 
4435
 
4281
        $conversation = api::create_conversation(
4436
        $conversation = api::create_conversation(
4282
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4437
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4283
            [
4438
            [
4284
                $user1->id,
4439
                $user1->id,
4285
                $user2->id
4440
                $user2->id,
4286
            ]
4441
            ]
4287
        );
4442
        );
Línea 4288... Línea 4443...
4288
        $conversationid = $conversation->id;
4443
        $conversationid = $conversation->id;
4289
 
4444
 
4290
        $this->assertNull(api::add_members_to_conversation([$user3->id, $user4->id], $conversationid));
4445
        $this->assertNull(api::add_members_to_conversation([$user3->id, $user4->id], $conversationid));
-
 
4446
        $this->assertNull(api::remove_members_from_conversation([$user3->id, $user4->id], $conversationid));
4291
        $this->assertNull(api::remove_members_from_conversation([$user3->id, $user4->id], $conversationid));
4447
        $this->assertEquals(
-
 
4448
            2,
4292
        $this->assertEquals(2,
4449
            api::count_conversation_members($conversationid)
Línea 4293... Línea 4450...
4293
            api::count_conversation_members($conversationid));
4450
        );
4294
    }
4451
    }
4295
 
4452
 
Línea 4306... Línea 4463...
4306
 
4463
 
4307
        $conversation = api::create_conversation(
4464
        $conversation = api::create_conversation(
4308
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4465
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4309
            [
4466
            [
4310
                $user1->id,
4467
                $user1->id,
4311
                $user2->id
4468
                $user2->id,
4312
            ]
4469
            ]
4313
        );
4470
        );
Línea 4314... Línea 4471...
4314
        $conversationid = $conversation->id;
4471
        $conversationid = $conversation->id;
4315
 
4472
 
4316
        $this->assertNull(api::add_members_to_conversation([$user3->id], $conversationid));
4473
        $this->assertNull(api::add_members_to_conversation([$user3->id], $conversationid));
4317
        $this->assertNull(
4474
        $this->assertNull(
4318
            api::remove_members_from_conversation([$user2->id, $user3->id, $user4->id, 0], $conversationid)
4475
            api::remove_members_from_conversation([$user2->id, $user3->id, $user4->id, 0], $conversationid)
-
 
4476
        );
4319
        );
4477
        $this->assertEquals(
-
 
4478
            1,
4320
        $this->assertEquals(1,
4479
            api::count_conversation_members($conversationid)
Línea 4321... Línea 4480...
4321
            api::count_conversation_members($conversationid));
4480
        );
4322
    }
4481
    }
4323
 
4482
 
Línea 4332... Línea 4491...
4332
 
4491
 
4333
        $conversation = api::create_conversation(
4492
        $conversation = api::create_conversation(
4334
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4493
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4335
            [
4494
            [
4336
                $user1->id,
4495
                $user1->id,
4337
                $user2->id
4496
                $user2->id,
4338
            ]
4497
            ]
4339
        );
4498
        );
Línea 4340... Línea 4499...
4340
        $conversationid = $conversation->id;
4499
        $conversationid = $conversation->id;
Línea 4341... Línea 4500...
4341
 
4500
 
-
 
4501
        $this->assertNull(api::remove_members_from_conversation([$user1->id, $user2->id], $conversationid));
4342
        $this->assertNull(api::remove_members_from_conversation([$user1->id, $user2->id], $conversationid));
4502
 
-
 
4503
        $this->assertEquals(
4343
 
4504
            0,
Línea 4344... Línea 4505...
4344
        $this->assertEquals(0,
4505
            api::count_conversation_members($conversationid)
4345
            api::count_conversation_members($conversationid));
4506
        );
4346
    }
4507
    }
Línea 4618... Línea 4779...
4618
 
4779
 
4619
        $conversation = api::create_conversation(
4780
        $conversation = api::create_conversation(
4620
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4781
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4621
            [
4782
            [
4622
                $user1->id,
4783
                $user1->id,
4623
                $user2->id
4784
                $user2->id,
4624
            ]
4785
            ]
4625
        );
4786
        );
Línea 4626... Línea 4787...
4626
        $conversationid = $conversation->id;
4787
        $conversationid = $conversation->id;
Línea 4650... Línea 4811...
4650
 
4811
 
4651
        $conversation = api::create_conversation(
4812
        $conversation = api::create_conversation(
4652
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4813
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4653
            [
4814
            [
4654
                $user1->id,
4815
                $user1->id,
4655
                $user2->id
4816
                $user2->id,
4656
            ]
4817
            ]
4657
        );
4818
        );
Línea 4658... Línea 4819...
4658
        $conversationid = $conversation->id;
4819
        $conversationid = $conversation->id;
Línea 4674... Línea 4835...
4674
 
4835
 
4675
        $conversation = api::create_conversation(
4836
        $conversation = api::create_conversation(
4676
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4837
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4677
            [
4838
            [
4678
                $user1->id,
4839
                $user1->id,
4679
                $user2->id
4840
                $user2->id,
4680
            ]
4841
            ]
4681
        );
4842
        );
Línea 4682... Línea 4843...
4682
        $conversationid = $conversation->id;
4843
        $conversationid = $conversation->id;
Línea 4864... Línea 5025...
4864
 
5025
 
4865
        $conversation = api::create_conversation(
5026
        $conversation = api::create_conversation(
4866
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
5027
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4867
            [
5028
            [
4868
                $user1->id,
5029
                $user1->id,
4869
                $user2->id
5030
                $user2->id,
4870
            ]
5031
            ]
4871
        );
5032
        );
Línea 4872... Línea 5033...
4872
        $conversationid = $conversation->id;
5033
        $conversationid = $conversation->id;
Línea 4886... Línea 5047...
4886
 
5047
 
4887
        $conversation = api::create_conversation(
5048
        $conversation = api::create_conversation(
4888
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
5049
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4889
            [
5050
            [
4890
                $user1->id,
5051
                $user1->id,
4891
                $user2->id
5052
                $user2->id,
4892
            ]
5053
            ]
4893
        );
5054
        );
Línea 4894... Línea 5055...
4894
        $conversationid = $conversation->id;
5055
        $conversationid = $conversation->id;
Línea 4937... Línea 5098...
4937
 
5098
 
4938
        $conversation = api::create_conversation(
5099
        $conversation = api::create_conversation(
4939
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
5100
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
4940
            [
5101
            [
4941
                $user1->id,
5102
                $user1->id,
4942
                $user2->id
5103
                $user2->id,
4943
            ],
5104
            ],
4944
            'A conversation name'
5105
            'A conversation name'
Línea 4945... Línea 5106...
4945
        );
5106
        );
Línea 4973... Línea 5134...
4973
        $conversation = api::create_conversation(
5134
        $conversation = api::create_conversation(
4974
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
5135
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
4975
            [
5136
            [
4976
                $user1->id,
5137
                $user1->id,
4977
                $user2->id,
5138
                $user2->id,
4978
                $user3->id
5139
                $user3->id,
4979
            ],
5140
            ],
4980
            'A conversation name'
5141
            'A conversation name'
4981
        );
5142
        );
Línea 4982... Línea 5143...
4982
 
5143
 
Línea 5099... Línea 5260...
5099
     */
5260
     */
5100
    public function test_disable_conversation(): void {
5261
    public function test_disable_conversation(): void {
5101
        global $DB;
5262
        global $DB;
5102
        $this->resetAfterTest();
5263
        $this->resetAfterTest();
Línea 5103... Línea -...
5103
 
-
 
5104
 
5264
 
Línea 5105... Línea 5265...
5105
        $name = 'Name of conversation';
5265
        $name = 'Name of conversation';
5106
 
5266
 
5107
        $conversation = api::create_conversation(
5267
        $conversation = api::create_conversation(
Línea 5128... Línea 5288...
5128
 
5288
 
5129
        $newname = 'New name of conversation';
5289
        $newname = 'New name of conversation';
Línea 5130... Línea 5290...
5130
        api::update_conversation_name($conversation->id, $newname);
5290
        api::update_conversation_name($conversation->id, $newname);
5131
 
5291
 
5132
        $this->assertEquals(
5292
        $this->assertEquals(
5133
                $newname,
5293
            $newname,
5134
                $DB->get_field('message_conversations', 'name', ['id' => $conversation->id])
5294
            $DB->get_field('message_conversations', 'name', ['id' => $conversation->id])
Línea 5135... Línea 5295...
5135
        );
5295
        );
5136
    }
5296
    }
Línea 5164... Línea 5324...
5164
        $conversation = api::create_conversation(
5324
        $conversation = api::create_conversation(
5165
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
5325
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
5166
            [
5326
            [
5167
                $user1->id,
5327
                $user1->id,
5168
                $user2->id,
5328
                $user2->id,
5169
                $user3->id
5329
                $user3->id,
5170
            ]
5330
            ]
5171
        );
5331
        );
5172
        $conversationid = $conversation->id;
5332
        $conversationid = $conversation->id;
5173
        $members = api::get_conversation_members($user1->id, $conversationid, false);
5333
        $members = api::get_conversation_members($user1->id, $conversationid, false);
Línea 5237... Línea 5397...
5237
        $conversation = api::create_conversation(
5397
        $conversation = api::create_conversation(
5238
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
5398
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
5239
            [
5399
            [
5240
                $user1->id,
5400
                $user1->id,
5241
                $user2->id,
5401
                $user2->id,
5242
                $user3->id
5402
                $user3->id,
5243
            ]
5403
            ]
5244
        );
5404
        );
5245
        $conversationid = $conversation->id;
5405
        $conversationid = $conversation->id;
Línea 5246... Línea 5406...
5246
 
5406
 
Línea 5335... Línea 5495...
5335
     */
5495
     */
5336
    public function test_send_message_to_conversation_individual_conversation(): void {
5496
    public function test_send_message_to_conversation_individual_conversation(): void {
5337
        $this->resetAfterTest();
5497
        $this->resetAfterTest();
Línea 5338... Línea 5498...
5338
 
5498
 
5339
        // Get a bunch of conversations, some group, some individual and in different states.
5499
        // Get a bunch of conversations, some group, some individual and in different states.
5340
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5500
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 5341... Línea 5501...
5341
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5501
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
5342
 
5502
 
5343
        // Enrol the users into the same course so the privacy checks will pass using default (contact+course members) setting.
5503
        // Enrol the users into the same course so the privacy checks will pass using default (contact+course members) setting.
5344
        $course = $this->getDataGenerator()->create_course();
5504
        $course = $this->getDataGenerator()->create_course();
Línea 5386... Línea 5546...
5386
     * Test verifying that messages can be sent to existing group conversations.
5546
     * Test verifying that messages can be sent to existing group conversations.
5387
     */
5547
     */
5388
    public function test_send_message_to_conversation_group_conversation(): void {
5548
    public function test_send_message_to_conversation_group_conversation(): void {
5389
        $this->resetAfterTest();
5549
        $this->resetAfterTest();
5390
        // Get a bunch of conversations, some group, some individual and in different states.
5550
        // Get a bunch of conversations, some group, some individual and in different states.
5391
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5551
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5392
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5552
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
Línea 5393... Línea 5553...
5393
 
5553
 
5394
        // Enrol the users into the same course so the privacy checks will pass using default (contact+course members) setting.
5554
        // Enrol the users into the same course so the privacy checks will pass using default (contact+course members) setting.
5395
        $course = $this->getDataGenerator()->create_course();
5555
        $course = $this->getDataGenerator()->create_course();
5396
        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
5556
        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
Línea 5453... Línea 5613...
5453
        $this->getDataGenerator()->enrol_user($user2->id, $course->id);
5613
        $this->getDataGenerator()->enrol_user($user2->id, $course->id);
5454
        $this->getDataGenerator()->enrol_user($user3->id, $course->id);
5614
        $this->getDataGenerator()->enrol_user($user3->id, $course->id);
5455
        $group = $this->getDataGenerator()->create_group([
5615
        $group = $this->getDataGenerator()->create_group([
5456
            'courseid' => $course->id,
5616
            'courseid' => $course->id,
5457
            'enablemessaging' => 1,
5617
            'enablemessaging' => 1,
5458
            'picturepath' => $CFG->dirroot . '/lib/tests/fixtures/gd-logo.png'
5618
            'picturepath' => $CFG->dirroot . '/lib/tests/fixtures/gd-logo.png',
5459
        ]);
5619
        ]);
Línea 5460... Línea 5620...
5460
 
5620
 
5461
        // Add users to group.
5621
        // Add users to group.
5462
        $this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user1->id));
5622
        $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user1->id]);
Línea 5463... Línea 5623...
5463
        $this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user2->id));
5623
        $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user2->id]);
5464
 
5624
 
5465
        // Verify the group with the image works as expected.
5625
        // Verify the group with the image works as expected.
5466
        $conversations = api::get_conversations($user1->id);
5626
        $conversations = api::get_conversations($user1->id);
Línea 5475... Línea 5635...
5475
        $this->preventResetByRollback();
5635
        $this->preventResetByRollback();
5476
        $sink = $this->redirectMessages();
5636
        $sink = $this->redirectMessages();
Línea 5477... Línea 5637...
5477
 
5637
 
5478
        // Send a message to a group conversation.
5638
        // Send a message to a group conversation.
5479
        $messagessink = $this->redirectMessages();
5639
        $messagessink = $this->redirectMessages();
-
 
5640
        $message1 = api::send_message_to_conversation(
-
 
5641
            $user1->id,
5480
        $message1 = api::send_message_to_conversation($user1->id, $conversations[0]->id,
5642
            $conversations[0]->id,
-
 
5643
            'message to the group',
-
 
5644
            FORMAT_MOODLE
5481
            'message to the group', FORMAT_MOODLE);
5645
        );
5482
        $messages = $messagessink->get_messages();
5646
        $messages = $messagessink->get_messages();
5483
        // Verify the message returned.
5647
        // Verify the message returned.
5484
        $this->assertInstanceOf(\stdClass::class, $message1);
5648
        $this->assertInstanceOf(\stdClass::class, $message1);
5485
        $this->assertObjectHasProperty('id', $message1);
5649
        $this->assertObjectHasProperty('id', $message1);
Línea 5502... Línea 5666...
5502
     */
5666
     */
5503
    public function test_send_message_to_conversation_non_existent_conversation(): void {
5667
    public function test_send_message_to_conversation_non_existent_conversation(): void {
5504
        $this->resetAfterTest();
5668
        $this->resetAfterTest();
Línea 5505... Línea 5669...
5505
 
5669
 
5506
        // Get a bunch of conversations, some group, some individual and in different states.
5670
        // Get a bunch of conversations, some group, some individual and in different states.
5507
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5671
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 5508... Línea 5672...
5508
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5672
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
5509
 
5673
 
5510
        $this->expectException(\moodle_exception::class);
5674
        $this->expectException(\moodle_exception::class);
Línea 5516... Línea 5680...
5516
     */
5680
     */
5517
    public function test_send_message_to_conversation_non_member(): void {
5681
    public function test_send_message_to_conversation_non_member(): void {
5518
        $this->resetAfterTest();
5682
        $this->resetAfterTest();
Línea 5519... Línea 5683...
5519
 
5683
 
5520
        // Get a bunch of conversations, some group, some individual and in different states.
5684
        // Get a bunch of conversations, some group, some individual and in different states.
5521
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5685
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 5522... Línea 5686...
5522
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5686
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
5523
 
5687
 
5524
        // Enrol the users into the same course so the privacy checks will pass using default (contact+course members) setting.
5688
        // Enrol the users into the same course so the privacy checks will pass using default (contact+course members) setting.
5525
        $course = $this->getDataGenerator()->create_course();
5689
        $course = $this->getDataGenerator()->create_course();
Línea 5537... Línea 5701...
5537
     */
5701
     */
5538
    public function test_send_message_to_conversation_blocked_user(): void {
5702
    public function test_send_message_to_conversation_blocked_user(): void {
5539
        $this->resetAfterTest();
5703
        $this->resetAfterTest();
Línea 5540... Línea 5704...
5540
 
5704
 
5541
        // Get a bunch of conversations, some group, some individual and in different states.
5705
        // Get a bunch of conversations, some group, some individual and in different states.
5542
        list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5706
        [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
Línea 5543... Línea 5707...
5543
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5707
            $gc1, $gc2, $gc3, $gc4, $gc5, $gc6] = $this->create_conversation_test_data();
5544
 
5708
 
5545
        // Enrol the users into the same course so the privacy checks will pass using default (contact+course members) setting.
5709
        // Enrol the users into the same course so the privacy checks will pass using default (contact+course members) setting.
5546
        $course = $this->getDataGenerator()->create_course();
5710
        $course = $this->getDataGenerator()->create_course();
Línea 5569... Línea 5733...
5569
        $user1 = self::getDataGenerator()->create_user();
5733
        $user1 = self::getDataGenerator()->create_user();
5570
        $user2 = self::getDataGenerator()->create_user();
5734
        $user2 = self::getDataGenerator()->create_user();
Línea 5571... Línea 5735...
5571
 
5735
 
Línea -... Línea 5736...
-
 
5736
        $this->setUser($user1);
5572
        $this->setUser($user1);
5737
 
5573
 
5738
        $conversation = api::create_conversation(
-
 
5739
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
Línea 5574... Línea 5740...
5574
        $conversation = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
5740
            [$user1->id, $user2->id]
Línea 5575... Línea 5741...
5575
            [$user1->id, $user2->id]);
5741
        );
Línea 5587... Línea 5753...
5587
    }
5753
    }
Línea 5588... Línea 5754...
5588
 
5754
 
5589
    /**
5755
    /**
5590
     * Data provider for test_get_conversation_counts().
5756
     * Data provider for test_get_conversation_counts().
5591
     */
5757
     */
5592
    public function get_conversation_counts_test_cases() {
5758
    public static function get_conversation_counts_test_cases(): array {
5593
        $typeindividual = api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL;
5759
        $typeindividual = api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL;
5594
        $typegroup = api::MESSAGE_CONVERSATION_TYPE_GROUP;
5760
        $typegroup = api::MESSAGE_CONVERSATION_TYPE_GROUP;
5595
        $typeself = api::MESSAGE_CONVERSATION_TYPE_SELF;
5761
        $typeself = api::MESSAGE_CONVERSATION_TYPE_SELF;
5596
        list($user1, $user2, $user3, $user4, $user5, $user6, $user7, $user8) = [0, 1, 2, 3, 4, 5, 6, 7];
5762
        [$user1, $user2, $user3, $user4, $user5, $user6, $user7, $user8] = [0, 1, 2, 3, 4, 5, 6, 7];
5597
        $conversations = [
5763
        $conversations = [
5598
            [
5764
            [
5599
                'type' => $typeindividual,
5765
                'type' => $typeindividual,
5600
                'users' => [$user1, $user2],
5766
                'users' => [$user1, $user2],
5601
                'messages' => [$user1, $user2, $user2],
5767
                'messages' => [$user1, $user2, $user2],
5602
                'favourites' => [$user1],
5768
                'favourites' => [$user1],
5603
                'enabled' => null // Individual conversations cannot be disabled.
5769
                'enabled' => null, // Individual conversations cannot be disabled.
5604
            ],
5770
            ],
5605
            [
5771
            [
5606
                'type' => $typeindividual,
5772
                'type' => $typeindividual,
5607
                'users' => [$user1, $user3],
5773
                'users' => [$user1, $user3],
5608
                'messages' => [$user1, $user3, $user1],
5774
                'messages' => [$user1, $user3, $user1],
5609
                'favourites' => [],
5775
                'favourites' => [],
5610
                'enabled' => null // Individual conversations cannot be disabled.
5776
                'enabled' => null, // Individual conversations cannot be disabled.
5611
            ],
5777
            ],
5612
            [
5778
            [
5613
                'type' => $typegroup,
5779
                'type' => $typegroup,
5614
                'users' => [$user1, $user2, $user3, $user4],
5780
                'users' => [$user1, $user2, $user3, $user4],
5615
                'messages' => [$user1, $user2, $user3, $user4],
5781
                'messages' => [$user1, $user2, $user3, $user4],
5616
                'favourites' => [],
5782
                'favourites' => [],
5617
                'enabled' => true
5783
                'enabled' => true,
5618
            ],
5784
            ],
5619
            [
5785
            [
5620
                'type' => $typegroup,
5786
                'type' => $typegroup,
5621
                'users' => [$user2, $user3, $user4],
5787
                'users' => [$user2, $user3, $user4],
5622
                'messages' => [$user2, $user3, $user4],
5788
                'messages' => [$user2, $user3, $user4],
5623
                'favourites' => [],
5789
                'favourites' => [],
5624
                'enabled' => true
5790
                'enabled' => true,
5625
            ],
5791
            ],
5626
            [
5792
            [
5627
                'type' => $typegroup,
5793
                'type' => $typegroup,
5628
                'users' => [$user6, $user7],
5794
                'users' => [$user6, $user7],
5629
                'messages' => [$user6, $user7, $user7],
5795
                'messages' => [$user6, $user7, $user7],
5630
                'favourites' => [$user6],
5796
                'favourites' => [$user6],
5631
                'enabled' => false
5797
                'enabled' => false,
5632
            ],
5798
            ],
5633
            [
5799
            [
5634
                'type' => $typeself,
5800
                'type' => $typeself,
5635
                'users' => [$user8],
5801
                'users' => [$user8],
5636
                'messages' => [$user8],
5802
                'messages' => [$user8],
5637
                'favourites' => [],
5803
                'favourites' => [],
5638
                'enabled' => null // Self-conversations cannot be disabled.
5804
                'enabled' => null, // Self-conversations cannot be disabled.
5639
            ],
5805
            ],
Línea 5640... Línea 5806...
5640
        ];
5806
        ];
5641
 
5807
 
5642
        return [
5808
        return [
5643
            'No conversations' => [
5809
            'No conversations' => [
5644
                'conversationConfigs' => $conversations,
5810
                'conversationconfigs' => $conversations,
5645
                'deletemessagesuser' => null,
5811
                'deletemessagesuser' => null,
5646
                'deletemessages' => [],
5812
                'deletemessages' => [],
-
 
5813
                'arguments' => [$user5],
-
 
5814
                'expectedcounts' => [
5647
                'arguments' => [$user5],
5815
                    'favourites' => 1,
5648
                'expectedcounts' => ['favourites' => 1, 'types' => [
5816
                    'types' => [
5649
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
5817
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
5818
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5650
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5819
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5651
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5820
                    ],
-
 
5821
                ],
-
 
5822
                'expectedunreadcounts' => [
5652
                ]],
5823
                    'favourites' => 0,
5653
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
5824
                    'types' => [
5654
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
5825
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
5826
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5655
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5827
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5656
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5828
                    ],
5657
                ]],
5829
                ],
5658
                'deletedusers' => []
5830
                'deletedusers' => [],
5659
            ],
5831
            ],
5660
            'No individual conversations, 2 group conversations' => [
5832
            'No individual conversations, 2 group conversations' => [
5661
                'conversationConfigs' => $conversations,
5833
                'conversationconfigs' => $conversations,
5662
                'deletemessagesuser' => null,
5834
                'deletemessagesuser' => null,
5663
                'deletemessages' => [],
5835
                'deletemessages' => [],
-
 
5836
                'arguments' => [$user4],
-
 
5837
                'expectedcounts' => [
5664
                'arguments' => [$user4],
5838
                    'favourites' => 1,
5665
                'expectedcounts' => ['favourites' => 1, 'types' => [
5839
                    'types' => [
5666
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
5840
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
5841
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5667
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5842
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5668
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5843
                    ],
-
 
5844
                ],
-
 
5845
                'expectedunreadcounts' => [
5669
                ]],
5846
                    'favourites' => 0,
5670
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
5847
                    'types' => [
5671
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
5848
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
5849
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5672
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5850
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5673
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5851
                    ],
5674
                ]],
5852
                ],
5675
                'deletedusers' => []
5853
                'deletedusers' => [],
5676
            ],
5854
            ],
5677
            '2 individual conversations (one favourited), 1 group conversation' => [
5855
            '2 individual conversations (one favourited), 1 group conversation' => [
5678
                'conversationConfigs' => $conversations,
5856
                'conversationconfigs' => $conversations,
5679
                'deletemessagesuser' => null,
5857
                'deletemessagesuser' => null,
5680
                'deletemessages' => [],
5858
                'deletemessages' => [],
-
 
5859
                'arguments' => [$user1],
-
 
5860
                'expectedcounts' => [
5681
                'arguments' => [$user1],
5861
                    'favourites' => 2,
5682
                'expectedcounts' => ['favourites' => 2, 'types' => [
5862
                    'types' => [
5683
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5863
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5864
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5684
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5865
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5685
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5866
                    ],
-
 
5867
                ],
-
 
5868
                'expectedunreadcounts' => [
5686
                ]],
5869
                    'favourites' => 1,
5687
                'expectedunreadcounts' => ['favourites' => 1, 'types' => [
5870
                    'types' => [
5688
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5871
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5872
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5689
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5873
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5690
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5874
                    ],
5691
                ]],
5875
                ],
5692
                'deletedusers' => []
5876
                'deletedusers' => [],
5693
            ],
5877
            ],
5694
            '1 individual conversation, 2 group conversations' => [
5878
            '1 individual conversation, 2 group conversations' => [
5695
                'conversationConfigs' => $conversations,
5879
                'conversationconfigs' => $conversations,
5696
                'deletemessagesuser' => null,
5880
                'deletemessagesuser' => null,
5697
                'deletemessages' => [],
5881
                'deletemessages' => [],
-
 
5882
                'arguments' => [$user2],
-
 
5883
                'expectedcounts' => [
5698
                'arguments' => [$user2],
5884
                    'favourites' => 1,
5699
                'expectedcounts' => ['favourites' => 1, 'types' => [
5885
                    'types' => [
5700
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5886
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5887
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5701
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5888
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5702
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5889
                    ],
-
 
5890
                ],
-
 
5891
                'expectedunreadcounts' => [
5703
                ]],
5892
                    'favourites' => 0,
5704
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
5893
                    'types' => [
5705
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5894
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5895
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5706
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5896
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5707
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5897
                    ],
5708
                ]],
5898
                ],
5709
                'deletedusers' => []
5899
                'deletedusers' => [],
5710
            ],
5900
            ],
5711
            '2 group conversations only' => [
5901
            '2 group conversations only' => [
5712
                'conversationConfigs' => $conversations,
5902
                'conversationconfigs' => $conversations,
5713
                'deletemessagesuser' => null,
5903
                'deletemessagesuser' => null,
5714
                'deletemessages' => [],
5904
                'deletemessages' => [],
-
 
5905
                'arguments' => [$user4],
-
 
5906
                'expectedcounts' => [
5715
                'arguments' => [$user4],
5907
                    'favourites' => 1,
5716
                'expectedcounts' => ['favourites' => 1, 'types' => [
5908
                    'types' => [
5717
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
5909
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
5910
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5718
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5911
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5719
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5912
                    ],
-
 
5913
                ],
-
 
5914
                'expectedunreadcounts' => [
5720
                ]],
5915
                    'favourites' => 0,
5721
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
5916
                    'types' => [
5722
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
5917
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
5918
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5723
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5919
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5724
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5920
                    ],
5725
                ]],
5921
                ],
5726
                'deletedusers' => []
5922
                'deletedusers' => [],
5727
            ],
5923
            ],
5728
            'All conversation types, delete a message from individual favourited, messages remaining' => [
5924
            'All conversation types, delete a message from individual favourited, messages remaining' => [
5729
                'conversationConfigs' => $conversations,
5925
                'conversationconfigs' => $conversations,
5730
                'deletemessagesuser' => $user1,
5926
                'deletemessagesuser' => $user1,
5731
                'deletemessages' => [0],
5927
                'deletemessages' => [0],
-
 
5928
                'arguments' => [$user1],
-
 
5929
                'expectedcounts' => [
5732
                'arguments' => [$user1],
5930
                    'favourites' => 2,
5733
                'expectedcounts' => ['favourites' => 2, 'types' => [
5931
                    'types' => [
5734
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5932
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5933
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5735
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5934
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5736
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5935
                    ],
-
 
5936
                ],
-
 
5937
                'expectedunreadcounts' => [
5737
                ]],
5938
                    'favourites' => 1,
5738
                'expectedunreadcounts' => ['favourites' => 1, 'types' => [
5939
                    'types' => [
5739
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5940
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5941
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5740
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5942
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5741
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5943
                    ],
5742
                ]],
5944
                ],
5743
                'deletedusers' => []
5945
                'deletedusers' => [],
5744
            ],
5946
            ],
5745
            'All conversation types, delete a message from individual non-favourited, messages remaining' => [
5947
            'All conversation types, delete a message from individual non-favourited, messages remaining' => [
5746
                'conversationConfigs' => $conversations,
5948
                'conversationconfigs' => $conversations,
5747
                'deletemessagesuser' => $user1,
5949
                'deletemessagesuser' => $user1,
5748
                'deletemessages' => [3],
5950
                'deletemessages' => [3],
-
 
5951
                'arguments' => [$user1],
-
 
5952
                'expectedcounts' => [
5749
                'arguments' => [$user1],
5953
                    'favourites' => 2,
5750
                'expectedcounts' => ['favourites' => 2, 'types' => [
5954
                    'types' => [
5751
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5955
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5956
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5752
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5957
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5753
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5958
                    ],
-
 
5959
                ],
-
 
5960
                'expectedunreadcounts' => [
5754
                ]],
5961
                    'favourites' => 1,
5755
                'expectedunreadcounts' => ['favourites' => 1, 'types' => [
5962
                    'types' => [
5756
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5963
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5964
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5757
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5965
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5758
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5966
                    ],
5759
                ]],
5967
                ],
5760
                'deletedusers' => []
5968
                'deletedusers' => [],
5761
            ],
5969
            ],
5762
            'All conversation types, delete all messages from individual favourited, no messages remaining' => [
5970
            'All conversation types, delete all messages from individual favourited, no messages remaining' => [
5763
                'conversationConfigs' => $conversations,
5971
                'conversationconfigs' => $conversations,
5764
                'deletemessagesuser' => $user1,
5972
                'deletemessagesuser' => $user1,
5765
                'deletemessages' => [0, 1, 2],
5973
                'deletemessages' => [0, 1, 2],
-
 
5974
                'arguments' => [$user1],
-
 
5975
                'expectedcounts' => [
5766
                'arguments' => [$user1],
5976
                    'favourites' => 1,
5767
                'expectedcounts' => ['favourites' => 1, 'types' => [
5977
                    'types' => [
5768
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5978
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5979
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5769
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5980
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5770
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5981
                    ],
-
 
5982
                ],
-
 
5983
                'expectedunreadcounts' => [
5771
                ]],
5984
                    'favourites' => 0,
5772
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
5985
                    'types' => [
5773
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
5986
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
5987
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5774
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5988
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5775
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
5989
                    ],
5776
                ]],
5990
                ],
5777
                'deletedusers' => []
5991
                'deletedusers' => [],
5778
            ],
5992
            ],
5779
            'All conversation types, delete all messages from individual non-favourited, no messages remaining' => [
5993
            'All conversation types, delete all messages from individual non-favourited, no messages remaining' => [
5780
                'conversationConfigs' => $conversations,
5994
                'conversationconfigs' => $conversations,
5781
                'deletemessagesuser' => $user1,
5995
                'deletemessagesuser' => $user1,
5782
                'deletemessages' => [3, 4, 5],
5996
                'deletemessages' => [3, 4, 5],
-
 
5997
                'arguments' => [$user1],
-
 
5998
                'expectedcounts' => [
5783
                'arguments' => [$user1],
5999
                    'favourites' => 2,
5784
                'expectedcounts' => ['favourites' => 2, 'types' => [
6000
                    'types' => [
5785
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
6001
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
6002
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5786
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
6003
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5787
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6004
                    ],
-
 
6005
                ],
-
 
6006
                'expectedunreadcounts' => [
5788
                ]],
6007
                    'favourites' => 1,
5789
                'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6008
                    'types' => [
5790
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
6009
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
6010
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5791
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
6011
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5792
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6012
                    ],
5793
                ]],
6013
                ],
5794
                'deletedusers' => []
6014
                'deletedusers' => [],
5795
            ],
6015
            ],
5796
            'All conversation types, delete all messages from individual favourited, no messages remaining, different user' => [
6016
            'All conversation types, delete all messages from individual favourited, no messages remaining, different user' => [
5797
                'conversationConfigs' => $conversations,
6017
                'conversationconfigs' => $conversations,
5798
                'deletemessagesuser' => $user1,
6018
                'deletemessagesuser' => $user1,
5799
                'deletemessages' => [0, 1, 2],
6019
                'deletemessages' => [0, 1, 2],
-
 
6020
                'arguments' => [$user2],
-
 
6021
                'expectedcounts' => [
5800
                'arguments' => [$user2],
6022
                    'favourites' => 1,
5801
                'expectedcounts' => ['favourites' => 1, 'types' => [
6023
                    'types' => [
5802
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6024
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6025
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5803
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
6026
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5804
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6027
                    ],
-
 
6028
                ],
-
 
6029
                'expectedunreadcounts' => [
5805
                ]],
6030
                    'favourites' => 0,
5806
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6031
                    'types' => [
5807
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6032
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6033
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5808
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
6034
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5809
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6035
                    ],
5810
                ]],
6036
                ],
5811
                'deletedusers' => []
6037
                'deletedusers' => [],
5812
            ],
6038
            ],
5813
            'All conversation types, delete all messages from individual non-favourited, no messages remaining, different user' => [
6039
            'All conversation types, delete all messages from individual non-favourited, no messages remaining, different user' => [
5814
                'conversationConfigs' => $conversations,
6040
                'conversationconfigs' => $conversations,
5815
                'deletemessagesuser' => $user1,
6041
                'deletemessagesuser' => $user1,
5816
                'deletemessages' => [3, 4, 5],
6042
                'deletemessages' => [3, 4, 5],
-
 
6043
                'arguments' => [$user3],
-
 
6044
                'expectedcounts' => [
5817
                'arguments' => [$user3],
6045
                    'favourites' => 1,
5818
                'expectedcounts' => ['favourites' => 1, 'types' => [
6046
                    'types' => [
5819
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6047
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6048
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5820
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
6049
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5821
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6050
                    ],
-
 
6051
                ],
-
 
6052
                'expectedunreadcounts' => [
5822
                ]],
6053
                    'favourites' => 0,
5823
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6054
                    'types' => [
5824
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6055
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6056
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
5825
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 2,
6057
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5826
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6058
                    ],
5827
                ]],
6059
                ],
5828
                'deletedusers' => []
6060
                'deletedusers' => [],
5829
            ],
6061
            ],
5830
            'All conversation types, delete some messages from group non-favourited, messages remaining,' => [
6062
            'All conversation types, delete some messages from group non-favourited, messages remaining,' => [
5831
                'conversationConfigs' => $conversations,
6063
                'conversationconfigs' => $conversations,
5832
                'deletemessagesuser' => $user1,
6064
                'deletemessagesuser' => $user1,
5833
                'deletemessages' => [6, 7],
6065
                'deletemessages' => [6, 7],
-
 
6066
                'arguments' => [$user1],
-
 
6067
                'expectedcounts' => [
5834
                'arguments' => [$user1],
6068
                    'favourites' => 2,
5835
                'expectedcounts' => ['favourites' => 2, 'types' => [
6069
                    'types' => [
5836
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6070
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6071
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5837
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
6072
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5838
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6073
                    ],
-
 
6074
                ],
-
 
6075
                'expectedunreadcounts' => [
5839
                ]],
6076
                    'favourites' => 1,
5840
                'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6077
                    'types' => [
5841
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6078
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6079
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5842
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
6080
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5843
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6081
                    ],
5844
                ]],
6082
                ],
5845
                'deletedusers' => []
6083
                'deletedusers' => [],
5846
            ],
6084
            ],
5847
            'All conversation types, delete all messages from group non-favourited, no messages remaining,' => [
6085
            'All conversation types, delete all messages from group non-favourited, no messages remaining,' => [
5848
                'conversationConfigs' => $conversations,
6086
                'conversationconfigs' => $conversations,
5849
                'deletemessagesuser' => $user1,
6087
                'deletemessagesuser' => $user1,
5850
                'deletemessages' => [6, 7, 8, 9],
6088
                'deletemessages' => [6, 7, 8, 9],
-
 
6089
                'arguments' => [$user1],
-
 
6090
                'expectedcounts' => [
5851
                'arguments' => [$user1],
6091
                    'favourites' => 2,
5852
                'expectedcounts' => ['favourites' => 2, 'types' => [
6092
                    'types' => [
5853
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6093
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6094
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5854
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
6095
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5855
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6096
                    ],
-
 
6097
                ],
-
 
6098
                'expectedunreadcounts' => [
5856
                ]],
6099
                    'favourites' => 1,
5857
                'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6100
                    'types' => [
5858
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6101
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6102
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5859
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
6103
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5860
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6104
                    ],
5861
                ]],
6105
                ],
5862
                'deletedusers' => []
6106
                'deletedusers' => [],
5863
            ],
6107
            ],
5864
            'All conversation types, another user soft deleted' => [
6108
            'All conversation types, another user soft deleted' => [
5865
                'conversationConfigs' => $conversations,
6109
                'conversationconfigs' => $conversations,
5866
                'deletemessagesuser' => null,
6110
                'deletemessagesuser' => null,
5867
                'deletemessages' => [],
6111
                'deletemessages' => [],
-
 
6112
                'arguments' => [$user1],
-
 
6113
                'expectedcounts' => [
5868
                'arguments' => [$user1],
6114
                    'favourites' => 2,
5869
                'expectedcounts' => ['favourites' => 2, 'types' => [
6115
                    'types' => [
5870
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6116
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6117
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5871
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
6118
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5872
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6119
                    ],
-
 
6120
                ],
-
 
6121
                'expectedunreadcounts' => [
5873
                ]],
6122
                    'favourites' => 1,
5874
                'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6123
                    'types' => [
5875
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6124
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6125
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5876
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
6126
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5877
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6127
                    ],
5878
                ]],
6128
                ],
5879
                'deletedusers' => [$user2]
6129
                'deletedusers' => [$user2],
5880
            ],
6130
            ],
5881
            'All conversation types, all group users soft deleted' => [
6131
            'All conversation types, all group users soft deleted' => [
5882
                'conversationConfigs' => $conversations,
6132
                'conversationconfigs' => $conversations,
5883
                'deletemessagesuser' => null,
6133
                'deletemessagesuser' => null,
5884
                'deletemessages' => [],
6134
                'deletemessages' => [],
-
 
6135
                'arguments' => [$user1],
-
 
6136
                'expectedcounts' => [
5885
                'arguments' => [$user1],
6137
                    'favourites' => 2,
5886
                'expectedcounts' => ['favourites' => 2, 'types' => [
6138
                    'types' => [
5887
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6139
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6140
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5888
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
6141
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5889
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6142
                    ],
-
 
6143
                ],
-
 
6144
                'expectedunreadcounts' => [
5890
                ]],
6145
                    'favourites' => 1,
5891
                'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6146
                    'types' => [
5892
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
6147
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 1,
-
 
6148
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
5893
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 1,
6149
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5894
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6150
                    ],
5895
                ]],
6151
                ],
5896
                'deletedusers' => [$user2, $user3, $user4]
6152
                'deletedusers' => [$user2, $user3, $user4],
5897
            ],
6153
            ],
5898
            'Group conversation which is disabled, favourited' => [
6154
            'Group conversation which is disabled, favourited' => [
5899
                'conversationConfigs' => $conversations,
6155
                'conversationconfigs' => $conversations,
5900
                'deletemessagesuser' => null,
6156
                'deletemessagesuser' => null,
5901
                'deletemessages' => [],
6157
                'deletemessages' => [],
-
 
6158
                'arguments' => [$user6],
-
 
6159
                'expectedcounts' => [
5902
                'arguments' => [$user6],
6160
                    'favourites' => 1,
5903
                'expectedcounts' => ['favourites' => 1, 'types' => [
6161
                    'types' => [
5904
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
6162
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
6163
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5905
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
6164
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5906
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6165
                    ],
-
 
6166
                ],
-
 
6167
                'expectedunreadcounts' => [
5907
                ]],
6168
                    'favourites' => 0,
5908
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6169
                    'types' => [
5909
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
6170
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
6171
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5910
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
6172
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5911
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6173
                    ],
5912
                ]],
6174
                ],
5913
                'deletedusers' => []
6175
                'deletedusers' => [],
5914
            ],
6176
            ],
5915
            'Group conversation which is disabled, non-favourited' => [
6177
            'Group conversation which is disabled, non-favourited' => [
5916
                'conversationConfigs' => $conversations,
6178
                'conversationconfigs' => $conversations,
5917
                'deletemessagesuser' => null,
6179
                'deletemessagesuser' => null,
5918
                'deletemessages' => [],
6180
                'deletemessages' => [],
-
 
6181
                'arguments' => [$user7],
-
 
6182
                'expectedcounts' => [
5919
                'arguments' => [$user7],
6183
                    'favourites' => 1,
5920
                'expectedcounts' => ['favourites' => 1, 'types' => [
6184
                    'types' => [
5921
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
6185
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
6186
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5922
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
6187
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5923
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6188
                    ],
-
 
6189
                ],
-
 
6190
                'expectedunreadcounts' => [
5924
                ]],
6191
                    'favourites' => 0,
5925
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6192
                    'types' => [
5926
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
6193
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
6194
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5927
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
6195
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5928
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6196
                    ],
5929
                ]],
6197
                ],
5930
                'deletedusers' => []
6198
                'deletedusers' => [],
5931
            ],
6199
            ],
5932
            'Conversation with self' => [
6200
            'Conversation with self' => [
5933
                'conversationConfigs' => $conversations,
6201
                'conversationconfigs' => $conversations,
5934
                'deletemessagesuser' => null,
6202
                'deletemessagesuser' => null,
5935
                'deletemessages' => [],
6203
                'deletemessages' => [],
-
 
6204
                'arguments' => [$user8],
-
 
6205
                'expectedcounts' => [
5936
                'arguments' => [$user8],
6206
                    'favourites' => 0,
5937
                'expectedcounts' => ['favourites' => 0, 'types' => [
6207
                    'types' => [
5938
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
6208
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
6209
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5939
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
6210
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 1,
5940
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 1
6211
                    ],
-
 
6212
                ],
-
 
6213
                'expectedunreadcounts' => [
5941
                ]],
6214
                    'favourites' => 0,
5942
                'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6215
                    'types' => [
5943
                    api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
6216
                        api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL => 0,
-
 
6217
                        api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
5944
                    api::MESSAGE_CONVERSATION_TYPE_GROUP => 0,
6218
                        api::MESSAGE_CONVERSATION_TYPE_SELF => 0,
5945
                    api::MESSAGE_CONVERSATION_TYPE_SELF => 0
6219
                    ],
5946
                ]],
6220
                ],
5947
                'deletedusers' => []
6221
                'deletedusers' => [],
5948
            ],
6222
            ],
Línea 5949... Línea 6223...
5949
        ];
6223
        ];
Línea 5979... Línea 6253...
5979
            $generator->create_user(),
6253
            $generator->create_user(),
5980
            $generator->create_user(),
6254
            $generator->create_user(),
5981
            $generator->create_user(),
6255
            $generator->create_user(),
5982
            $generator->create_user(),
6256
            $generator->create_user(),
5983
            $generator->create_user(),
6257
            $generator->create_user(),
5984
            $generator->create_user()
6258
            $generator->create_user(),
5985
        ];
6259
        ];
Línea 5986... Línea 6260...
5986
 
6260
 
5987
        $deleteuser = !is_null($deletemessagesuser) ? $users[$deletemessagesuser] : null;
6261
        $deleteuser = !is_null($deletemessagesuser) ? $users[$deletemessagesuser] : null;
5988
        $arguments[0] = $users[$arguments[0]]->id;
6262
        $arguments[0] = $users[$arguments[0]]->id;
Línea 5991... Línea 6265...
5991
        $messageids = [];
6265
        $messageids = [];
Línea 5992... Línea 6266...
5992
 
6266
 
5993
        foreach ($conversationconfigs as $config) {
6267
        foreach ($conversationconfigs as $config) {
5994
            $conversation = api::create_conversation(
6268
            $conversation = api::create_conversation(
5995
                $config['type'],
6269
                $config['type'],
5996
                array_map(function($userindex) use ($users) {
6270
                array_map(function ($userindex) use ($users) {
5997
                    return $users[$userindex]->id;
6271
                    return $users[$userindex]->id;
5998
                }, $config['users']),
6272
                }, $config['users']),
5999
                null,
6273
                null,
6000
                ($config['enabled'] ?? true)
6274
                ($config['enabled'] ?? true)
Línea 6005... Línea 6279...
6005
                $messageids[] = testhelper::send_fake_message_to_conversation($userfrom, $conversation->id);
6279
                $messageids[] = testhelper::send_fake_message_to_conversation($userfrom, $conversation->id);
6006
            }
6280
            }
Línea 6007... Línea 6281...
6007
 
6281
 
6008
            // Remove the self conversations created by the generator,
6282
            // Remove the self conversations created by the generator,
6009
            // so we can choose to set that ourself and honour the original intention of the test.
6283
            // so we can choose to set that ourself and honour the original intention of the test.
6010
            $userids = array_map(function($userindex) use ($users) {
6284
            $userids = array_map(function ($userindex) use ($users) {
6011
                return $users[$userindex]->id;
6285
                return $users[$userindex]->id;
6012
            }, $config['users']);
6286
            }, $config['users']);
6013
            foreach ($userids as $userid) {
6287
            foreach ($userids as $userid) {
6014
                if ($conversation->type == api::MESSAGE_CONVERSATION_TYPE_SELF) {
6288
                if ($conversation->type == api::MESSAGE_CONVERSATION_TYPE_SELF) {
Línea 6035... Línea 6309...
6035
        }
6309
        }
Línea 6036... Línea 6310...
6036
 
6310
 
Línea 6037... Línea 6311...
6037
        $counts = api::get_conversation_counts(...$arguments);
6311
        $counts = api::get_conversation_counts(...$arguments);
-
 
6312
 
6038
 
6313
        $this->assertEquals($expectedcounts['favourites'], $counts['favourites']);
6039
        $this->assertEquals($expectedcounts['favourites'], $counts['favourites']);
6314
        $this->assertEquals(
-
 
6315
            $expectedcounts['types'][api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL],
-
 
6316
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL]
6040
        $this->assertEquals($expectedcounts['types'][api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL],
6317
        );
6041
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL]);
6318
        $this->assertEquals(
-
 
6319
            $expectedcounts['types'][api::MESSAGE_CONVERSATION_TYPE_GROUP],
-
 
6320
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_GROUP]
6042
        $this->assertEquals($expectedcounts['types'][api::MESSAGE_CONVERSATION_TYPE_GROUP],
6321
        );
6043
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_GROUP]);
6322
        $this->assertEquals(
-
 
6323
            $expectedcounts['types'][api::MESSAGE_CONVERSATION_TYPE_SELF],
6044
        $this->assertEquals($expectedcounts['types'][api::MESSAGE_CONVERSATION_TYPE_SELF],
6324
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_SELF]
Línea 6045... Línea 6325...
6045
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_SELF]);
6325
        );
6046
    }
6326
    }
6047
 
6327
 
Línea 6105... Línea 6385...
6105
            $generator->create_user(),
6385
            $generator->create_user(),
6106
            $generator->create_user(),
6386
            $generator->create_user(),
6107
            $generator->create_user(),
6387
            $generator->create_user(),
6108
            $generator->create_user(),
6388
            $generator->create_user(),
6109
            $generator->create_user(),
6389
            $generator->create_user(),
6110
            $generator->create_user()
6390
            $generator->create_user(),
6111
        ];
6391
        ];
Línea 6112... Línea 6392...
6112
 
6392
 
6113
        $deleteuser = !is_null($deletemessagesuser) ? $users[$deletemessagesuser] : null;
6393
        $deleteuser = !is_null($deletemessagesuser) ? $users[$deletemessagesuser] : null;
6114
        $this->setUser($users[$arguments[0]]);
6394
        $this->setUser($users[$arguments[0]]);
Línea 6118... Línea 6398...
6118
        $messageids = [];
6398
        $messageids = [];
Línea 6119... Línea 6399...
6119
 
6399
 
6120
        foreach ($conversationconfigs as $config) {
6400
        foreach ($conversationconfigs as $config) {
6121
            $conversation = api::create_conversation(
6401
            $conversation = api::create_conversation(
6122
                $config['type'],
6402
                $config['type'],
6123
                array_map(function($userindex) use ($users) {
6403
                array_map(function ($userindex) use ($users) {
6124
                    return $users[$userindex]->id;
6404
                    return $users[$userindex]->id;
6125
                }, $config['users']),
6405
                }, $config['users']),
6126
                null,
6406
                null,
6127
                ($config['enabled'] ?? true)
6407
                ($config['enabled'] ?? true)
Línea 6151... Línea 6431...
6151
        }
6431
        }
Línea 6152... Línea 6432...
6152
 
6432
 
Línea 6153... Línea 6433...
6153
        $counts = api::get_unread_conversation_counts(...$arguments);
6433
        $counts = api::get_unread_conversation_counts(...$arguments);
-
 
6434
 
6154
 
6435
        $this->assertEquals($expectedunreadcounts['favourites'], $counts['favourites']);
6155
        $this->assertEquals($expectedunreadcounts['favourites'], $counts['favourites']);
6436
        $this->assertEquals(
-
 
6437
            $expectedunreadcounts['types'][api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL],
-
 
6438
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL]
6156
        $this->assertEquals($expectedunreadcounts['types'][api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL],
6439
        );
6157
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL]);
6440
        $this->assertEquals(
-
 
6441
            $expectedunreadcounts['types'][api::MESSAGE_CONVERSATION_TYPE_GROUP],
-
 
6442
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_GROUP]
6158
        $this->assertEquals($expectedunreadcounts['types'][api::MESSAGE_CONVERSATION_TYPE_GROUP],
6443
        );
6159
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_GROUP]);
6444
        $this->assertEquals(
-
 
6445
            $expectedunreadcounts['types'][api::MESSAGE_CONVERSATION_TYPE_SELF],
6160
        $this->assertEquals($expectedunreadcounts['types'][api::MESSAGE_CONVERSATION_TYPE_SELF],
6446
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_SELF]
Línea 6161... Línea 6447...
6161
            $counts['types'][api::MESSAGE_CONVERSATION_TYPE_SELF]);
6447
        );
6162
    }
6448
    }
Línea 6175... Línea 6461...
6175
        $user2 = $this->getDataGenerator()->create_user();
6461
        $user2 = $this->getDataGenerator()->create_user();
Línea 6176... Línea 6462...
6176
 
6462
 
6177
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
6463
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
Línea 6178... Línea 6464...
6178
        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
6464
        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
6179
 
6465
 
Línea 6180... Línea 6466...
6180
        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
6466
        $group1 = $this->getDataGenerator()->create_group(['courseid' => $course1->id, 'enablemessaging' => 1]);
6181
        $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
6467
        $group2 = $this->getDataGenerator()->create_group(['courseid' => $course1->id, 'enablemessaging' => 1]);
6182
 
6468
 
Línea 6183... Línea 6469...
6183
        // Add users to both groups.
6469
        // Add users to both groups.
6184
        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
6470
        $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user1->id]);
Línea 6185... Línea 6471...
6185
        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
6471
        $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user2->id]);
6186
 
6472
 
6187
        $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
6473
        $this->getDataGenerator()->create_group_member(['groupid' => $group2->id, 'userid' => $user1->id]);
6188
        $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
6474
        $this->getDataGenerator()->create_group_member(['groupid' => $group2->id, 'userid' => $user2->id]);
Línea 6261... Línea 6547...
6261
        $this->assertEquals(1, $DB->count_records('messages', ['id' => $g2m3]));
6547
        $this->assertEquals(1, $DB->count_records('messages', ['id' => $g2m3]));
6262
        $this->assertEquals(1, $DB->count_records('messages', ['id' => $g2m4]));
6548
        $this->assertEquals(1, $DB->count_records('messages', ['id' => $g2m4]));
Línea 6263... Línea 6549...
6263
 
6549
 
6264
        // Confirm favourites were deleted for both users.
6550
        // Confirm favourites were deleted for both users.
6265
        $user1service = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($user1->id));
6551
        $user1service = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($user1->id));
-
 
6552
        $this->assertFalse($user1service->favourite_exists(
-
 
6553
            'core_message',
-
 
6554
            'message_conversations',
6266
        $this->assertFalse($user1service->favourite_exists('core_message', 'message_conversations', $groupconversation1->id,
6555
            $groupconversation1->id,
-
 
6556
            $coursecontext1
6267
            $coursecontext1));
6557
        ));
6268
        $user2service = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($user1->id));
6558
        $user2service = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($user1->id));
-
 
6559
        $this->assertFalse($user2service->favourite_exists(
-
 
6560
            'core_message',
-
 
6561
            'message_conversations',
6269
        $this->assertFalse($user2service->favourite_exists('core_message', 'message_conversations', $groupconversation1->id,
6562
            $groupconversation1->id,
-
 
6563
            $coursecontext1
6270
            $coursecontext1));
6564
        ));
Línea 6271... Línea 6565...
6271
    }
6565
    }
6272
 
6566
 
6273
    /**
6567
    /**
6274
     * Tests the user can delete message for all users as a teacher.
6568
     * Tests the user can delete message for all users as a teacher.
6275
     */
6569
     */
6276
    public function test_can_delete_message_for_all_users_teacher(): void {
6570
    public function test_can_delete_message_for_all_users_teacher(): void {
Línea 6277... Línea 6571...
6277
        global $DB;
6571
        global $DB;
6278
        $this->resetAfterTest(true);
6572
        $this->resetAfterTest(true);
Línea 6279... Línea 6573...
6279
 
6573
 
6280
        // Create fake data to test it.
6574
        // Create fake data to test it.
6281
        list($teacher, $student1, $student2, $convgroup, $convindividual) = $this->create_delete_message_test_data();
6575
        [$teacher, $student1, $student2, $convgroup, $convindividual] = $this->create_delete_message_test_data();
Línea 6303... Línea 6597...
6303
     */
6597
     */
6304
    public function test_can_delete_message_for_all_users_student(): void {
6598
    public function test_can_delete_message_for_all_users_student(): void {
6305
        $this->resetAfterTest(true);
6599
        $this->resetAfterTest(true);
Línea 6306... Línea 6600...
6306
 
6600
 
6307
        // Create fake data to test it.
6601
        // Create fake data to test it.
Línea 6308... Línea 6602...
6308
        list($teacher, $student1, $student2, $convgroup, $convindividual) = $this->create_delete_message_test_data();
6602
        [$teacher, $student1, $student2, $convgroup, $convindividual] = $this->create_delete_message_test_data();
6309
 
6603
 
Línea 6310... Línea 6604...
6310
        // Set as the first user.
6604
        // Set as the first user.
Línea 6327... Línea 6621...
6327
    public function test_delete_message_for_all_users_group_conversation(): void {
6621
    public function test_delete_message_for_all_users_group_conversation(): void {
6328
        global $DB;
6622
        global $DB;
6329
        $this->resetAfterTest(true);
6623
        $this->resetAfterTest(true);
Línea 6330... Línea 6624...
6330
 
6624
 
6331
        // Create fake data to test it.
6625
        // Create fake data to test it.
Línea 6332... Línea 6626...
6332
        list($teacher, $student1, $student2, $convgroup, $convindividual) = $this->create_delete_message_test_data();
6626
        [$teacher, $student1, $student2, $convgroup, $convindividual] = $this->create_delete_message_test_data();
6333
 
6627
 
6334
        // Send 3 messages to a group conversation.
6628
        // Send 3 messages to a group conversation.
6335
        $mgid1 = testhelper::send_fake_message_to_conversation($teacher, $convgroup->id);
6629
        $mgid1 = testhelper::send_fake_message_to_conversation($teacher, $convgroup->id);
Línea 6373... Línea 6667...
6373
    public function test_delete_message_for_all_users_individual_conversation(): void {
6667
    public function test_delete_message_for_all_users_individual_conversation(): void {
6374
        global $DB;
6668
        global $DB;
6375
        $this->resetAfterTest(true);
6669
        $this->resetAfterTest(true);
Línea 6376... Línea 6670...
6376
 
6670
 
6377
        // Create fake data to test it.
6671
        // Create fake data to test it.
Línea 6378... Línea 6672...
6378
        list($teacher, $student1, $student2, $convgroup, $convindividual) = $this->create_delete_message_test_data();
6672
        [$teacher, $student1, $student2, $convgroup, $convindividual] = $this->create_delete_message_test_data();
6379
 
6673
 
6380
        // Send 2 messages in a individual conversation.
6674
        // Send 2 messages in a individual conversation.
Línea 6457... Línea 6751...
6457
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');
6751
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');
6458
        $this->getDataGenerator()->enrol_user($student1->id, $course->id, 'student');
6752
        $this->getDataGenerator()->enrol_user($student1->id, $course->id, 'student');
6459
        $this->getDataGenerator()->enrol_user($student2->id, $course->id, 'student');
6753
        $this->getDataGenerator()->enrol_user($student2->id, $course->id, 'student');
Línea 6460... Línea 6754...
6460
 
6754
 
6461
        // Create a group and added the users into.
6755
        // Create a group and added the users into.
6462
        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
6756
        $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
6463
        groups_add_member($group1->id, $teacher->id);
6757
        groups_add_member($group1->id, $teacher->id);
6464
        groups_add_member($group1->id, $student1->id);
6758
        groups_add_member($group1->id, $student1->id);
Línea 6465... Línea 6759...
6465
        groups_add_member($group1->id, $student2->id);
6759
        groups_add_member($group1->id, $student2->id);
6466
 
6760
 
6467
        // Create a group conversation linked with the course.
6761
        // Create a group conversation linked with the course.
6468
        $convgroup = api::create_conversation(
6762
        $convgroup = api::create_conversation(
-
 
6763
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
6469
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
6764
            [$teacher->id, $student1->id, $student2->id],
6470
            [$teacher->id, $student1->id, $student2->id],
6765
            'Group test delete for everyone',
6471
            'Group test delete for everyone', api::MESSAGE_CONVERSATION_ENABLED,
6766
            api::MESSAGE_CONVERSATION_ENABLED,
6472
            'core_group',
6767
            'core_group',
6473
            'groups',
6768
            'groups',
6474
            $group1->id,
6769
            $group1->id,