MDL-65566 core_message: fix bug allowing duplicate unique conversations
[moodle.git] / message / tests / api_test.php
CommitLineData
79f6c36c
MN
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Test message API.
19 *
20 * @package core_message
21 * @category test
22 * @copyright 2016 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
31
fb04293b
SA
32use \core_message\tests\helper as testhelper;
33
79f6c36c
MN
34/**
35 * Test message API.
36 *
37 * @package core_message
38 * @category test
39 * @copyright 2016 Mark Nelson <markn@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class core_message_api_testcase extends core_message_messagelib_testcase {
43
1f64514d 44 public function test_mark_all_read_for_user_touser() {
79f6c36c
MN
45 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
46 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
47
7d69958e
RW
48 $this->send_fake_message($sender, $recipient, 'Notification', 1);
49 $this->send_fake_message($sender, $recipient, 'Notification', 1);
50 $this->send_fake_message($sender, $recipient, 'Notification', 1);
79f6c36c
MN
51 $this->send_fake_message($sender, $recipient);
52 $this->send_fake_message($sender, $recipient);
53 $this->send_fake_message($sender, $recipient);
54
55 \core_message\api::mark_all_read_for_user($recipient->id);
74ad60bf 56 $this->assertDebuggingCalled();
79f6c36c
MN
57 $this->assertEquals(message_count_unread_messages($recipient), 0);
58 }
59
1f64514d 60 public function test_mark_all_read_for_user_touser_with_fromuser() {
79f6c36c
MN
61 $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
62 $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
63 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
64
7d69958e
RW
65 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
66 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
67 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
79f6c36c
MN
68 $this->send_fake_message($sender1, $recipient);
69 $this->send_fake_message($sender1, $recipient);
70 $this->send_fake_message($sender1, $recipient);
7d69958e
RW
71 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
72 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
73 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
79f6c36c
MN
74 $this->send_fake_message($sender2, $recipient);
75 $this->send_fake_message($sender2, $recipient);
76 $this->send_fake_message($sender2, $recipient);
77
78 \core_message\api::mark_all_read_for_user($recipient->id, $sender1->id);
74ad60bf 79 $this->assertDebuggingCalled();
29c3b0b4 80 $this->assertEquals(message_count_unread_messages($recipient), 3);
79f6c36c
MN
81 }
82
1f64514d 83 public function test_mark_all_read_for_user_touser_with_type() {
79f6c36c
MN
84 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
85 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
86
7d69958e
RW
87 $this->send_fake_message($sender, $recipient, 'Notification', 1);
88 $this->send_fake_message($sender, $recipient, 'Notification', 1);
89 $this->send_fake_message($sender, $recipient, 'Notification', 1);
79f6c36c
MN
90 $this->send_fake_message($sender, $recipient);
91 $this->send_fake_message($sender, $recipient);
92 $this->send_fake_message($sender, $recipient);
93
94 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
74ad60bf 95 $this->assertDebuggingCalled();
79f6c36c
MN
96 $this->assertEquals(message_count_unread_messages($recipient), 3);
97
98 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
74ad60bf 99 $this->assertDebuggingCalled();
79f6c36c
MN
100 $this->assertEquals(message_count_unread_messages($recipient), 0);
101 }
102
5b0769db
MN
103 /**
104 * Test count_blocked_users.
5b0769db 105 */
1f64514d 106 public function test_count_blocked_users() {
f219eac7
MN
107 global $USER;
108
5b0769db
MN
109 // Set this user as the admin.
110 $this->setAdminUser();
111
f219eac7 112 // Create user to add to the admin's block list.
5b0769db 113 $user1 = $this->getDataGenerator()->create_user();
f7dfa9ba 114 $user2 = $this->getDataGenerator()->create_user();
5b0769db
MN
115
116 $this->assertEquals(0, \core_message\api::count_blocked_users());
117
f219eac7
MN
118 // Add 1 blocked user to admin's blocked user list.
119 \core_message\api::block_user($USER->id, $user1->id);
5b0769db 120
f219eac7 121 $this->assertEquals(0, \core_message\api::count_blocked_users($user1));
5b0769db
MN
122 $this->assertEquals(1, \core_message\api::count_blocked_users());
123 }
1f64514d
MN
124
125 /**
126 * Tests searching users in a course.
127 */
128 public function test_search_users_in_course() {
129 // Create some users.
130 $user1 = new stdClass();
131 $user1->firstname = 'User';
132 $user1->lastname = 'One';
133 $user1 = self::getDataGenerator()->create_user($user1);
134
135 // The person doing the search.
136 $this->setUser($user1);
137
138 // Second user is going to have their last access set to now, so they are online.
139 $user2 = new stdClass();
140 $user2->firstname = 'User';
141 $user2->lastname = 'Two';
142 $user2->lastaccess = time();
143 $user2 = self::getDataGenerator()->create_user($user2);
144
145 // Block the second user.
f219eac7 146 \core_message\api::block_user($user1->id, $user2->id);
1f64514d
MN
147
148 $user3 = new stdClass();
149 $user3->firstname = 'User';
150 $user3->lastname = 'Three';
151 $user3 = self::getDataGenerator()->create_user($user3);
152
153 // Create a course.
154 $course1 = new stdClass();
155 $course1->fullname = 'Course';
156 $course1->shortname = 'One';
157 $course1 = $this->getDataGenerator()->create_course($course1);
158
159 // Enrol the searcher and one user in the course.
160 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
161 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
162
163 // Perform a search.
164 $results = \core_message\api::search_users_in_course($user1->id, $course1->id, 'User');
165
166 $this->assertEquals(1, count($results));
167
168 $user = $results[0];
169 $this->assertEquals($user2->id, $user->userid);
170 $this->assertEquals(fullname($user2), $user->fullname);
171 $this->assertFalse($user->ismessaging);
172 $this->assertNull($user->lastmessage);
173 $this->assertNull($user->messageid);
cb805753 174 $this->assertNull($user->isonline);
1f64514d
MN
175 $this->assertFalse($user->isread);
176 $this->assertTrue($user->isblocked);
177 $this->assertNull($user->unreadcount);
178 }
179
180 /**
181 * Tests searching users.
182 */
183 public function test_search_users() {
09a05b79
AN
184 global $DB;
185
1f64514d
MN
186 // Create some users.
187 $user1 = new stdClass();
188 $user1->firstname = 'User';
189 $user1->lastname = 'One';
190 $user1 = self::getDataGenerator()->create_user($user1);
191
192 // Set as the user performing the search.
193 $this->setUser($user1);
194
195 $user2 = new stdClass();
196 $user2->firstname = 'User search';
197 $user2->lastname = 'Two';
198 $user2 = self::getDataGenerator()->create_user($user2);
199
200 $user3 = new stdClass();
201 $user3->firstname = 'User search';
202 $user3->lastname = 'Three';
203 $user3 = self::getDataGenerator()->create_user($user3);
204
205 $user4 = new stdClass();
206 $user4->firstname = 'User';
207 $user4->lastname = 'Four';
208 $user4 = self::getDataGenerator()->create_user($user4);
209
210 $user5 = new stdClass();
211 $user5->firstname = 'User search';
212 $user5->lastname = 'Five';
213 $user5 = self::getDataGenerator()->create_user($user5);
214
215 $user6 = new stdClass();
216 $user6->firstname = 'User';
217 $user6->lastname = 'Six';
218 $user6 = self::getDataGenerator()->create_user($user6);
219
220 // Create some courses.
221 $course1 = new stdClass();
222 $course1->fullname = 'Course search';
223 $course1->shortname = 'One';
224 $course1 = $this->getDataGenerator()->create_course($course1);
225
226 $course2 = new stdClass();
227 $course2->fullname = 'Course';
228 $course2->shortname = 'Two';
229 $course2 = $this->getDataGenerator()->create_course($course2);
230
231 $course3 = new stdClass();
232 $course3->fullname = 'Course';
233 $course3->shortname = 'Three search';
234 $course3 = $this->getDataGenerator()->create_course($course3);
235
87d4ab65
AG
236 $course4 = new stdClass();
237 $course4->fullname = 'Course Four';
238 $course4->shortname = 'CF100';
239 $course4 = $this->getDataGenerator()->create_course($course4);
240
09a05b79
AN
241 $course5 = new stdClass();
242 $course5->fullname = 'Course';
243 $course5->shortname = 'Five search';
244 $course5 = $this->getDataGenerator()->create_course($course5);
245
246 $role = $DB->get_record('role', ['shortname' => 'student']);
247 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $role->id);
248 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $role->id);
249 $this->getDataGenerator()->enrol_user($user1->id, $course3->id, $role->id);
250 $this->getDataGenerator()->enrol_user($user1->id, $course5->id, $role->id);
87d4ab65 251
1f64514d 252 // Add some users as contacts.
f219eac7
MN
253 \core_message\api::add_contact($user1->id, $user2->id);
254 \core_message\api::add_contact($user1->id, $user3->id);
255 \core_message\api::add_contact($user1->id, $user4->id);
1f64514d 256
09a05b79
AN
257 // Remove the viewparticipants capability from one of the courses.
258 $course5context = context_course::instance($course5->id);
259 assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $role->id, $course5context->id);
09a05b79 260
548cac7d
AA
261 // Perform a search $CFG->messagingallusers setting enabled.
262 set_config('messagingallusers', 1);
1f64514d
MN
263 list($contacts, $courses, $noncontacts) = \core_message\api::search_users($user1->id, 'search');
264
265 // Check that we retrieved the correct contacts.
266 $this->assertEquals(2, count($contacts));
267 $this->assertEquals($user3->id, $contacts[0]->userid);
268 $this->assertEquals($user2->id, $contacts[1]->userid);
269
270 // Check that we retrieved the correct courses.
271 $this->assertEquals(2, count($courses));
272 $this->assertEquals($course3->id, $courses[0]->id);
273 $this->assertEquals($course1->id, $courses[1]->id);
274
275 // Check that we retrieved the correct non-contacts.
276 $this->assertEquals(1, count($noncontacts));
277 $this->assertEquals($user5->id, $noncontacts[0]->userid);
278 }
279
548cac7d
AA
280 /**
281 * Tests searching users with empty result.
282 */
283 public function test_search_users_with_empty_result() {
284
285 // Create some users.
286 $user1 = new stdClass();
287 $user1->firstname = 'User';
288 $user1->lastname = 'One';
289 $user1 = self::getDataGenerator()->create_user($user1);
290
291 // Set as the user performing the search.
292 $this->setUser($user1);
293
294 $user2 = new stdClass();
295 $user2->firstname = 'User';
296 $user2->lastname = 'Two';
297 $user2 = self::getDataGenerator()->create_user($user2);
298
299 // Perform a search $CFG->messagingallusers setting enabled.
300 set_config('messagingallusers', 1);
301 list($contacts, $courses, $noncontacts) = \core_message\api::search_users($user1->id, 'search');
302
303 // Check results are empty.
304 $this->assertEquals(0, count($contacts));
305 $this->assertEquals(0, count($courses));
306 $this->assertEquals(0, count($noncontacts));
307 }
308
309 /**
41485be2
JD
310 * Tests searching for users when site-wide messaging is disabled.
311 *
312 * This test verifies that any contacts are returned, as well as any non-contacts whose profile we can view.
313 * If checks this by placing some users in the same course, where default caps would permit a user to view another user's
314 * profile.
548cac7d 315 */
41485be2 316 public function test_message_search_users_messagingallusers_disabled() {
3edac090 317 global $DB;
41485be2
JD
318 $this->resetAfterTest();
319
548cac7d 320 // Create some users.
41485be2 321 $users = [];
3edac090 322 foreach (range(1, 8) as $i) {
41485be2
JD
323 $user = new stdClass();
324 $user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term.
325 $user->lastname = $i;
326 $user = $this->getDataGenerator()->create_user($user);
327 $users[$i] = $user;
328 }
329
330 // Enrol a few users in the same course, but leave them as non-contacts.
331 $course1 = $this->getDataGenerator()->create_course();
3edac090
JD
332 $course2 = $this->getDataGenerator()->create_course();
333
41485be2
JD
334 $this->setAdminUser();
335 $this->getDataGenerator()->enrol_user($users[1]->id, $course1->id);
336 $this->getDataGenerator()->enrol_user($users[6]->id, $course1->id);
337 $this->getDataGenerator()->enrol_user($users[7]->id, $course1->id);
338
339 // Add some other users as contacts.
340 \core_message\api::add_contact($users[1]->id, $users[2]->id);
341 \core_message\api::add_contact($users[3]->id, $users[1]->id);
342 \core_message\api::add_contact($users[1]->id, $users[4]->id);
343
3edac090
JD
344 // Enrol a user as a teacher in the course, and make the teacher role a course contact role.
345 $this->getDataGenerator()->enrol_user($users[8]->id, $course2->id, 'editingteacher');
346 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
347 set_config('coursecontact', $teacherrole->id);
348
41485be2
JD
349 // Create individual conversations between some users, one contact and one non-contact.
350 $ic1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
351 [$users[1]->id, $users[2]->id]);
352 $ic2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
353 [$users[6]->id, $users[1]->id]);
354
355 // Create a group conversation between 4 users, including a contact and a non-contact.
356 $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
357 [$users[1]->id, $users[2]->id, $users[4]->id, $users[7]->id], 'Project chat');
548cac7d
AA
358
359 // Set as the user performing the search.
41485be2 360 $this->setUser($users[1]);
548cac7d 361
41485be2
JD
362 // Perform a search with $CFG->messagingallusers disabled.
363 set_config('messagingallusers', 0);
364 $result = \core_message\api::message_search_users($users[1]->id, 'search');
548cac7d 365
41485be2
JD
366 // Confirm that we returns contacts and non-contacts.
367 $this->assertArrayHasKey(0, $result);
368 $this->assertArrayHasKey(1, $result);
369 $contacts = $result[0];
370 $noncontacts = $result[1];
548cac7d 371
41485be2
JD
372 // Check that we retrieved the correct contacts.
373 $this->assertCount(2, $contacts);
374 $this->assertEquals($users[2]->id, $contacts[0]->id);
375 $this->assertEquals($users[3]->id, $contacts[1]->id);
548cac7d 376
41485be2
JD
377 // Verify the correct conversations were returned for the contacts.
378 $this->assertCount(2, $contacts[0]->conversations);
379 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $contacts[0]->conversations[$gc1->id]->type);
380 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $contacts[0]->conversations[$ic1->id]->type);
548cac7d 381
41485be2 382 $this->assertCount(0, $contacts[1]->conversations);
548cac7d 383
41485be2 384 // Check that we retrieved the correct non-contacts.
3edac090
JD
385 // When site wide messaging is disabled, we expect to see only those users who we share a course with and whose profiles
386 // are visible in that course. This excludes users like course contacts.
888a467a
AA
387 $this->assertCount(3, $noncontacts);
388 // Self-conversation first.
389 $this->assertEquals($users[1]->id, $noncontacts[0]->id);
390 $this->assertEquals($users[6]->id, $noncontacts[1]->id);
391 $this->assertEquals($users[7]->id, $noncontacts[2]->id);
548cac7d 392
41485be2 393 // Verify the correct conversations were returned for the non-contacts.
888a467a 394 $this->assertCount(1, $noncontacts[1]->conversations);
41485be2 395 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
888a467a 396 $noncontacts[1]->conversations[$ic2->id]->type);
548cac7d 397
888a467a
AA
398 $this->assertCount(1, $noncontacts[2]->conversations);
399 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $noncontacts[2]->conversations[$gc1->id]->type);
41485be2 400 }
548cac7d 401
41485be2
JD
402 /**
403 * Tests searching for users when site-wide messaging is enabled.
404 *
3edac090
JD
405 * This test verifies that any contacts are returned, as well as any non-contacts,
406 * provided the searching user can view their profile.
41485be2
JD
407 */
408 public function test_message_search_users_messagingallusers_enabled() {
3edac090 409 global $DB;
41485be2
JD
410 $this->resetAfterTest();
411
412 // Create some users.
413 $users = [];
3edac090 414 foreach (range(1, 9) as $i) {
41485be2
JD
415 $user = new stdClass();
416 $user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term.
417 $user->lastname = $i;
418 $user = $this->getDataGenerator()->create_user($user);
419 $users[$i] = $user;
420 }
421
41485be2 422 $course1 = $this->getDataGenerator()->create_course();
3edac090
JD
423 $coursecontext = \context_course::instance($course1->id);
424
425 // Enrol a few users in the same course, but leave them as non-contacts.
41485be2 426 $this->setAdminUser();
3edac090
JD
427 $this->getDataGenerator()->enrol_user($users[1]->id, $course1->id, 'student');
428 $this->getDataGenerator()->enrol_user($users[6]->id, $course1->id, 'student');
429 $this->getDataGenerator()->enrol_user($users[7]->id, $course1->id, 'student');
41485be2
JD
430
431 // Add some other users as contacts.
432 \core_message\api::add_contact($users[1]->id, $users[2]->id);
433 \core_message\api::add_contact($users[3]->id, $users[1]->id);
434 \core_message\api::add_contact($users[1]->id, $users[4]->id);
435
3edac090
JD
436 // Enrol a user as a teacher in the course, and make the teacher role a course contact role.
437 $this->getDataGenerator()->enrol_user($users[9]->id, $course1->id, 'editingteacher');
438 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
439 set_config('coursecontact', $teacherrole->id);
440
888a467a
AA
441 // Get self-conversation.
442 $selfconversation = \core_message\api::get_self_conversation($users[1]->id);
443
41485be2
JD
444 // Create individual conversations between some users, one contact and one non-contact.
445 $ic1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
446 [$users[1]->id, $users[2]->id]);
447 $ic2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
448 [$users[6]->id, $users[1]->id]);
449
450 // Create a group conversation between 5 users, including a contact and a non-contact, and a user NOT in a shared course.
451 $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
452 [$users[1]->id, $users[2]->id, $users[4]->id, $users[7]->id, $users[8]->id], 'Project chat');
453
454 // Set as the user performing the search.
455 $this->setUser($users[1]);
456
457 // Perform a search with $CFG->messagingallusers enabled.
548cac7d 458 set_config('messagingallusers', 1);
41485be2
JD
459 $result = \core_message\api::message_search_users($users[1]->id, 'search');
460
461 // Confirm that we returns contacts and non-contacts.
462 $this->assertArrayHasKey(0, $result);
463 $this->assertArrayHasKey(1, $result);
464 $contacts = $result[0];
465 $noncontacts = $result[1];
548cac7d
AA
466
467 // Check that we retrieved the correct contacts.
468 $this->assertCount(2, $contacts);
41485be2
JD
469 $this->assertEquals($users[2]->id, $contacts[0]->id);
470 $this->assertEquals($users[3]->id, $contacts[1]->id);
471
472 // Verify the correct conversations were returned for the contacts.
473 $this->assertCount(2, $contacts[0]->conversations);
474 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $contacts[0]->conversations[$gc1->id]->type);
475 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $contacts[0]->conversations[$ic1->id]->type);
476
477 $this->assertCount(0, $contacts[1]->conversations);
548cac7d
AA
478
479 // Check that we retrieved the correct non-contacts.
3edac090
JD
480 // If site wide messaging is enabled, we expect to only be able to search for users whose profiles we can view.
481 // In this case, as a student, that's the course contact for course2 and those noncontacts sharing a course with user1.
888a467a
AA
482 // Consider first conversations is self-conversation.
483 $this->assertCount(4, $noncontacts);
484 $this->assertEquals($users[1]->id, $noncontacts[0]->id);
485 $this->assertEquals($users[6]->id, $noncontacts[1]->id);
486 $this->assertEquals($users[7]->id, $noncontacts[2]->id);
487 $this->assertEquals($users[9]->id, $noncontacts[3]->id);
488
41485be2 489 $this->assertCount(1, $noncontacts[1]->conversations);
888a467a
AA
490 $this->assertCount(1, $noncontacts[2]->conversations);
491 $this->assertCount(0, $noncontacts[3]->conversations);
492
493 // Verify the correct conversations were returned for the non-contacts.
494 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
495 $noncontacts[0]->conversations[$selfconversation->id]->type);
496
497 $this->assertCount(1, $noncontacts[1]->conversations);
498 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
499 $noncontacts[1]->conversations[$ic2->id]->type);
500
501 $this->assertCount(1, $noncontacts[2]->conversations);
502 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $noncontacts[2]->conversations[$gc1->id]->type);
503
504 $this->assertCount(0, $noncontacts[3]->conversations);
41485be2
JD
505 }
506
734b198f
SA
507 /**
508 * Verify searching for users find themselves when they have self-conversations.
509 */
510 public function test_message_search_users_self_conversations() {
511 $this->resetAfterTest();
512
513 // Create some users.
514 $user1 = new stdClass();
515 $user1->firstname = 'User';
516 $user1->lastname = 'One';
517 $user1 = $this->getDataGenerator()->create_user($user1);
518 $user2 = new stdClass();
519 $user2->firstname = 'User';
520 $user2->lastname = 'Two';
521 $user2 = $this->getDataGenerator()->create_user($user2);
522
888a467a
AA
523 // Get self-conversation for user1.
524 $sc1 = \core_message\api::get_self_conversation($user1->id);
734b198f
SA
525 testhelper::send_fake_message_to_conversation($user1, $sc1->id, 'Hi myself!');
526
527 // Perform a search as user1.
528 $this->setUser($user1);
529 $result = \core_message\api::message_search_users($user1->id, 'One');
530
531 // Check user1 is found as non-contacts.
532 $this->assertCount(0, $result[0]);
533 $this->assertCount(1, $result[1]);
534 }
535
41485be2
JD
536 /**
537 * Verify searching for users works even if no matching users from either contacts, or non-contacts can be found.
538 */
539 public function test_message_search_users_with_empty_result() {
540 $this->resetAfterTest();
541
542 // Create some users, but make sure neither will match the search term.
543 $user1 = new stdClass();
544 $user1->firstname = 'User';
545 $user1->lastname = 'One';
546 $user1 = $this->getDataGenerator()->create_user($user1);
547 $user2 = new stdClass();
548 $user2->firstname = 'User';
549 $user2->lastname = 'Two';
550 $user2 = $this->getDataGenerator()->create_user($user2);
551
552 // Perform a search as user1.
553 $this->setUser($user1);
554 $result = \core_message\api::message_search_users($user1->id, 'search');
555
556 // Check results are empty.
557 $this->assertCount(0, $result[0]);
558 $this->assertCount(0, $result[1]);
559 }
560
561 /**
562 * Test verifying that limits and offsets work for both the contacts and non-contacts return data.
563 */
564 public function test_message_search_users_limit_offset() {
565 $this->resetAfterTest();
566
567 // Create 20 users.
568 $users = [];
569 foreach (range(1, 20) as $i) {
570 $user = new stdClass();
571 $user->firstname = "User search";
572 $user->lastname = $i;
573 $user = $this->getDataGenerator()->create_user($user);
574 $users[$i] = $user;
575 }
576
577 // Enrol the first 9 users in the same course, but leave them as non-contacts.
578 $this->setAdminUser();
579 $course1 = $this->getDataGenerator()->create_course();
888a467a 580 foreach (range(1, 8) as $i) {
41485be2
JD
581 $this->getDataGenerator()->enrol_user($users[$i]->id, $course1->id);
582 }
583
584 // Add 5 users, starting at the 11th user, as contacts for user1.
585 foreach (range(11, 15) as $i) {
586 \core_message\api::add_contact($users[1]->id, $users[$i]->id);
587 }
588
589 // Set as the user performing the search.
590 $this->setUser($users[1]);
591
592 // Search using a limit of 3.
593 // This tests the case where we have more results than the limit for both contacts and non-contacts.
594 $result = \core_message\api::message_search_users($users[1]->id, 'search', 0, 3);
595 $contacts = $result[0];
596 $noncontacts = $result[1];
597
598 // Check that we retrieved the correct contacts.
599 $this->assertCount(3, $contacts);
600 $this->assertEquals($users[11]->id, $contacts[0]->id);
601 $this->assertEquals($users[12]->id, $contacts[1]->id);
602 $this->assertEquals($users[13]->id, $contacts[2]->id);
603
604 // Check that we retrieved the correct non-contacts.
888a467a 605 // Consider first conversations is self-conversation.
41485be2 606 $this->assertCount(3, $noncontacts);
888a467a
AA
607 $this->assertEquals($users[1]->id, $noncontacts[0]->id);
608 $this->assertEquals($users[2]->id, $noncontacts[1]->id);
609 $this->assertEquals($users[3]->id, $noncontacts[2]->id);
41485be2
JD
610
611 // Now, offset to get the next batch of results.
612 // We expect to see 2 contacts, and 3 non-contacts.
613 $result = \core_message\api::message_search_users($users[1]->id, 'search', 3, 3);
614 $contacts = $result[0];
615 $noncontacts = $result[1];
616 $this->assertCount(2, $contacts);
617 $this->assertEquals($users[14]->id, $contacts[0]->id);
618 $this->assertEquals($users[15]->id, $contacts[1]->id);
619
548cac7d 620 $this->assertCount(3, $noncontacts);
888a467a
AA
621 $this->assertEquals($users[4]->id, $noncontacts[0]->id);
622 $this->assertEquals($users[5]->id, $noncontacts[1]->id);
623 $this->assertEquals($users[6]->id, $noncontacts[2]->id);
41485be2
JD
624
625 // Now, offset to get the next batch of results.
626 // We expect to see 0 contacts, and 2 non-contacts.
627 $result = \core_message\api::message_search_users($users[1]->id, 'search', 6, 3);
628 $contacts = $result[0];
629 $noncontacts = $result[1];
630 $this->assertCount(0, $contacts);
631
632 $this->assertCount(2, $noncontacts);
888a467a
AA
633 $this->assertEquals($users[7]->id, $noncontacts[0]->id);
634 $this->assertEquals($users[8]->id, $noncontacts[1]->id);
41485be2 635 }
548cac7d 636
41485be2
JD
637 /**
638 * Tests searching users as a user having the 'moodle/user:viewdetails' capability.
639 */
640 public function test_message_search_users_with_cap() {
641 $this->resetAfterTest();
642 global $DB;
643
644 // Create some users.
645 $users = [];
646 foreach (range(1, 8) as $i) {
647 $user = new stdClass();
648 $user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term.
649 $user->lastname = $i;
650 $user = $this->getDataGenerator()->create_user($user);
651 $users[$i] = $user;
652 }
653
654 // Enrol a few users in the same course, but leave them as non-contacts.
655 $course1 = $this->getDataGenerator()->create_course();
656 $this->setAdminUser();
657 $this->getDataGenerator()->enrol_user($users[1]->id, $course1->id);
658 $this->getDataGenerator()->enrol_user($users[6]->id, $course1->id);
659 $this->getDataGenerator()->enrol_user($users[7]->id, $course1->id);
660
661 // Add some other users as contacts.
662 \core_message\api::add_contact($users[1]->id, $users[2]->id);
663 \core_message\api::add_contact($users[3]->id, $users[1]->id);
664 \core_message\api::add_contact($users[1]->id, $users[4]->id);
665
666 // Set as the user performing the search.
667 $this->setUser($users[1]);
668
669 // Grant the authenticated user role the capability 'user:viewdetails' at site context.
670 $authenticatedrole = $DB->get_record('role', ['shortname' => 'user'], '*', MUST_EXIST);
671 assign_capability('moodle/user:viewdetails', CAP_ALLOW, $authenticatedrole->id, context_system::instance());
672
673 // Perform a search with $CFG->messagingallusers disabled.
548cac7d 674 set_config('messagingallusers', 0);
41485be2
JD
675 $result = \core_message\api::message_search_users($users[1]->id, 'search');
676 $contacts = $result[0];
677 $noncontacts = $result[1];
548cac7d
AA
678
679 // Check that we retrieved the correct contacts.
680 $this->assertCount(2, $contacts);
41485be2
JD
681 $this->assertEquals($users[2]->id, $contacts[0]->id);
682 $this->assertEquals($users[3]->id, $contacts[1]->id);
548cac7d
AA
683
684 // Check that we retrieved the correct non-contacts.
3edac090 685 // Site-wide messaging is disabled, so we expect to be able to search for any users whose profiles we can view.
888a467a
AA
686 // Consider first conversations is self-conversation.
687 $this->assertCount(3, $noncontacts);
688 $this->assertEquals($users[1]->id, $noncontacts[0]->id);
689 $this->assertEquals($users[6]->id, $noncontacts[1]->id);
690 $this->assertEquals($users[7]->id, $noncontacts[2]->id);
41485be2
JD
691 }
692
693 /**
694 * Tests searching users with messaging disabled.
695 */
696 public function test_message_search_users_messaging_disabled() {
697 $this->resetAfterTest();
698
699 // Create a user.
700 $user = $this->getDataGenerator()->create_user();
701
702 // Disable messaging.
703 set_config('messaging', 0);
704
705 // Ensure an exception is thrown.
706 $this->expectException('moodle_exception');
707 \core_message\api::message_search_users($user->id, 'User');
548cac7d
AA
708 }
709
710 /**
711 * Tests getting conversations between 2 users.
712 */
713 public function test_get_conversations_between_users() {
714 // Create some users.
715 $user1 = new stdClass();
716 $user1->firstname = 'User';
717 $user1->lastname = 'One';
718 $user1 = self::getDataGenerator()->create_user($user1);
719
720 $user2 = new stdClass();
721 $user2->firstname = 'User';
722 $user2->lastname = 'Two';
723 $user2 = self::getDataGenerator()->create_user($user2);
724
725 $user3 = new stdClass();
726 $user3->firstname = 'User search';
727 $user3->lastname = 'Three';
728 $user3 = self::getDataGenerator()->create_user($user3);
729
730 $user4 = new stdClass();
731 $user4->firstname = 'User';
732 $user4->lastname = 'Four';
733 $user4 = self::getDataGenerator()->create_user($user4);
734
735 $user5 = new stdClass();
736 $user5->firstname = 'User';
737 $user5->lastname = 'Five';
738 $user5 = self::getDataGenerator()->create_user($user5);
739
740 $user6 = new stdClass();
741 $user6->firstname = 'User search';
742 $user6->lastname = 'Six';
743 $user6 = self::getDataGenerator()->create_user($user6);
744
745 // Add some users as contacts.
746 \core_message\api::add_contact($user1->id, $user2->id);
747 \core_message\api::add_contact($user6->id, $user1->id);
748
749 // Create private conversations with some users.
750 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
751 array($user1->id, $user2->id));
752 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
753 array($user3->id, $user1->id));
754
755 // Create a group conversation with users.
756 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
757 array($user1->id, $user2->id, $user3->id, $user4->id),
758 'Project chat');
759
760 // Check that we retrieved the correct conversations.
761 $this->assertCount(2, \core_message\api::get_conversations_between_users($user1->id, $user2->id));
762 $this->assertCount(2, \core_message\api::get_conversations_between_users($user2->id, $user1->id));
763 $this->assertCount(2, \core_message\api::get_conversations_between_users($user1->id, $user3->id));
764 $this->assertCount(2, \core_message\api::get_conversations_between_users($user3->id, $user1->id));
765 $this->assertCount(1, \core_message\api::get_conversations_between_users($user1->id, $user4->id));
766 $this->assertCount(1, \core_message\api::get_conversations_between_users($user4->id, $user1->id));
767 $this->assertCount(0, \core_message\api::get_conversations_between_users($user1->id, $user5->id));
768 $this->assertCount(0, \core_message\api::get_conversations_between_users($user5->id, $user1->id));
769 $this->assertCount(0, \core_message\api::get_conversations_between_users($user1->id, $user6->id));
770 $this->assertCount(0, \core_message\api::get_conversations_between_users($user6->id, $user1->id));
771 }
772
734b198f
SA
773 /**
774 * Tests getting self-conversations.
775 */
776 public function test_get_self_conversation() {
777 // Create some users.
778 $user1 = new stdClass();
779 $user1->firstname = 'User';
780 $user1->lastname = 'One';
781 $user1 = self::getDataGenerator()->create_user($user1);
782
783 $user2 = new stdClass();
784 $user2->firstname = 'User';
785 $user2->lastname = 'Two';
786 $user2 = self::getDataGenerator()->create_user($user2);
787
788 $user3 = new stdClass();
789 $user3->firstname = 'User search';
790 $user3->lastname = 'Three';
791 $user3 = self::getDataGenerator()->create_user($user3);
792
793 // Add some users as contacts.
794 \core_message\api::add_contact($user1->id, $user2->id);
795 \core_message\api::add_contact($user3->id, $user1->id);
796
797 // Create private conversations with some users.
798 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
799 array($user1->id, $user2->id));
800 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
801 array($user3->id, $user1->id));
802
803 // Create a group conversation with users.
804 $gc = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
805 array($user1->id, $user2->id, $user3->id),
806 'Project chat');
807
888a467a 808 // Get self-conversations.
734b198f
SA
809 $rsc1 = \core_message\api::get_self_conversation($user1->id);
810 $rsc2 = \core_message\api::get_self_conversation($user2->id);
811 $rsc3 = \core_message\api::get_self_conversation($user3->id);
812
888a467a
AA
813 // Send message to self-conversation.
814 testhelper::send_fake_message_to_conversation($user1, $rsc1->id, 'Message to myself!');
815
734b198f 816 // Check that we retrieved the correct conversations.
888a467a
AA
817 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF, $rsc1->type);
818 $members = \core_message\api::get_conversation_members($user1->id, $rsc1->id);
819 $this->assertCount(1, $members);
820 $member = reset($members);
821 $this->assertEquals($user1->id, $member->id);
822
823 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF, $rsc2->type);
824 $members = \core_message\api::get_conversation_members($user2->id, $rsc2->id);
825 $this->assertCount(1, $members);
826 $member = reset($members);
827 $this->assertEquals($user2->id, $member->id);
828
829 \core_message\api::delete_all_conversation_data($rsc3->id);
830 $rsc3 = \core_message\api::get_self_conversation($user3->id);
734b198f
SA
831 $this->assertFalse($rsc3);
832 }
833
1f64514d
MN
834 /**
835 * Tests searching messages.
836 */
837 public function test_search_messages() {
27674261
SA
838 $this->resetAfterTest();
839
1f64514d
MN
840 // Create some users.
841 $user1 = self::getDataGenerator()->create_user();
842 $user2 = self::getDataGenerator()->create_user();
f219eac7 843 $user3 = self::getDataGenerator()->create_user();
1f64514d
MN
844
845 // The person doing the search.
846 $this->setUser($user1);
847
888a467a
AA
848 // Get self-conversation.
849 $sc = \core_message\api::get_self_conversation($user1->id);
734b198f 850
27674261
SA
851 // Create group conversation.
852 $gc = \core_message\api::create_conversation(
853 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
854 [$user1->id, $user2->id, $user3->id]
855 );
856
1f64514d
MN
857 // Send some messages back and forth.
858 $time = 1;
734b198f
SA
859 testhelper::send_fake_message_to_conversation($user1, $sc->id, 'Test message to self!', $time);
860 testhelper::send_fake_message_to_conversation($user1, $gc->id, 'My hero!', $time + 1);
861 $this->send_fake_message($user3, $user1, 'Don\'t block me.', 0, $time + 2);
862 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 3);
863 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 4);
864 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 5);
865 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 6);
f219eac7 866
d2708759
JD
867 $convid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
868 $conv2id = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
869
f219eac7
MN
870 // Block user 3.
871 \core_message\api::block_user($user1->id, $user3->id);
1f64514d
MN
872
873 // Perform a search.
874 $messages = \core_message\api::search_messages($user1->id, 'o');
875
876 // Confirm the data is correct.
734b198f 877 $this->assertEquals(5, count($messages));
1f64514d
MN
878 $message1 = $messages[0];
879 $message2 = $messages[1];
f219eac7 880 $message3 = $messages[2];
27674261 881 $message4 = $messages[3];
734b198f 882 $message5 = $messages[4];
1f64514d
MN
883
884 $this->assertEquals($user2->id, $message1->userid);
885 $this->assertEquals($user2->id, $message1->useridfrom);
886 $this->assertEquals(fullname($user2), $message1->fullname);
887 $this->assertTrue($message1->ismessaging);
888 $this->assertEquals('Word.', $message1->lastmessage);
889 $this->assertNotEmpty($message1->messageid);
cb805753 890 $this->assertNull($message1->isonline);
1f64514d
MN
891 $this->assertFalse($message1->isread);
892 $this->assertFalse($message1->isblocked);
893 $this->assertNull($message1->unreadcount);
d2708759 894 $this->assertEquals($convid, $message1->conversationid);
1f64514d
MN
895
896 $this->assertEquals($user2->id, $message2->userid);
897 $this->assertEquals($user1->id, $message2->useridfrom);
898 $this->assertEquals(fullname($user2), $message2->fullname);
899 $this->assertTrue($message2->ismessaging);
900 $this->assertEquals('Yo!', $message2->lastmessage);
901 $this->assertNotEmpty($message2->messageid);
cb805753 902 $this->assertNull($message2->isonline);
1f64514d
MN
903 $this->assertTrue($message2->isread);
904 $this->assertFalse($message2->isblocked);
905 $this->assertNull($message2->unreadcount);
d2708759 906 $this->assertEquals($convid, $message2->conversationid);
f219eac7
MN
907
908 $this->assertEquals($user3->id, $message3->userid);
909 $this->assertEquals($user3->id, $message3->useridfrom);
910 $this->assertEquals(fullname($user3), $message3->fullname);
911 $this->assertTrue($message3->ismessaging);
912 $this->assertEquals('Don\'t block me.', $message3->lastmessage);
913 $this->assertNotEmpty($message3->messageid);
914 $this->assertNull($message3->isonline);
915 $this->assertFalse($message3->isread);
916 $this->assertTrue($message3->isblocked);
917 $this->assertNull($message3->unreadcount);
d2708759 918 $this->assertEquals($conv2id, $message3->conversationid);
27674261
SA
919
920 // This is a group conversation. For now, search_messages returns only one of the other users on the conversation. It can't
921 // be guaranteed who will be returned in the first place, so we need to use the in_array to check all the possibilities.
922 $this->assertTrue(in_array($message4->userid, [$user2->id, $user3->id]));
923 $this->assertEquals($user1->id, $message4->useridfrom);
924 $this->assertTrue($message4->ismessaging);
925 $this->assertEquals('My hero!', $message4->lastmessage);
926 $this->assertNotEmpty($message4->messageid);
927 $this->assertNull($message4->isonline);
928 $this->assertTrue($message4->isread);
929 $this->assertNull($message4->unreadcount);
930 $this->assertEquals($gc->id, $message4->conversationid);
734b198f
SA
931
932 $this->assertEquals($user1->id, $message5->userid);
933 $this->assertEquals($user1->id, $message5->useridfrom);
934 $this->assertEquals(fullname($user1), $message5->fullname);
935 $this->assertTrue($message5->ismessaging);
936 $this->assertEquals('Test message to self!', $message5->lastmessage);
937 $this->assertNotEmpty($message5->messageid);
938 $this->assertFalse($message5->isonline);
939 $this->assertTrue($message5->isread);
940 $this->assertFalse($message5->isblocked);
941 $this->assertNull($message5->unreadcount);
942 $this->assertEquals($sc->id, $message5->conversationid);
1f64514d
MN
943 }
944
945 /**
b8ff2c44
JD
946 * Test verifying that favourited conversations can be retrieved.
947 */
948 public function test_get_favourite_conversations() {
949 // Create some users.
950 $user1 = self::getDataGenerator()->create_user();
951 $user2 = self::getDataGenerator()->create_user();
952 $user3 = self::getDataGenerator()->create_user();
953 $user4 = self::getDataGenerator()->create_user();
954
955 // The person doing the search.
956 $this->setUser($user1);
957
888a467a
AA
958 // Only self-conversation created.
959 $this->assertCount(1, \core_message\api::get_conversations($user1->id));
b8ff2c44
JD
960
961 // Create some conversations for user1.
962 $time = 1;
963 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
964 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
965 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
966 $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
967
968 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
969 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
970 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
971 $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
972
973 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
974 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
975 $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
976
977 // Favourite the first 2 conversations for user1.
978 $convoids = [];
979 $convoids[] = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
980 $convoids[] = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
981 $user1context = context_user::instance($user1->id);
982 $service = \core_favourites\service_factory::get_service_for_user_context($user1context);
983 foreach ($convoids as $convoid) {
984 $service->create_favourite('core_message', 'message_conversations', $convoid, $user1context);
985 }
986
888a467a
AA
987 // We should have 4 conversations.
988 // Consider first conversations is self-conversation.
989 $this->assertCount(4, \core_message\api::get_conversations($user1->id));
b8ff2c44 990
888a467a 991 // And 3 favourited conversations (self-conversation included).
b8ff2c44 992 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, null, true);
888a467a
AA
993 $this->assertCount(3, $conversations);
994 $conversations = \core_message\api::get_conversations(
995 $user1->id,
996 0,
997 20,
998 \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
999 true
1000 );
1001 $this->assertCount(1, $conversations);
b8ff2c44
JD
1002 }
1003
1004 /**
1005 * Tests retrieving favourite conversations with a limit and offset to ensure pagination works correctly.
1006 */
1007 public function test_get_favourite_conversations_limit_offset() {
1008 // Create some users.
1009 $user1 = self::getDataGenerator()->create_user();
1010 $user2 = self::getDataGenerator()->create_user();
1011 $user3 = self::getDataGenerator()->create_user();
1012 $user4 = self::getDataGenerator()->create_user();
1013
1014 // The person doing the search.
1015 $this->setUser($user1);
1016
888a467a
AA
1017 // Only self-conversation created.
1018 $this->assertCount(1, \core_message\api::get_conversations($user1->id));
b8ff2c44
JD
1019
1020 // Create some conversations for user1.
1021 $time = 1;
1022 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1023 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1024 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1025 $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1026
1027 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
1028 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
1029 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
1030 $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
1031
1032 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
1033 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
1034 $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
1035
1036 // Favourite the all conversations for user1.
1037 $convoids = [];
1038 $convoids[] = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1039 $convoids[] = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
1040 $convoids[] = \core_message\api::get_conversation_between_users([$user1->id, $user4->id]);
1041 $user1context = context_user::instance($user1->id);
1042 $service = \core_favourites\service_factory::get_service_for_user_context($user1context);
1043 foreach ($convoids as $convoid) {
1044 $service->create_favourite('core_message', 'message_conversations', $convoid, $user1context);
1045 }
1046
888a467a 1047 // Consider first conversations is self-conversation.
b8ff2c44 1048 // Get all records, using offset 0 and large limit.
888a467a 1049 $this->assertCount(4, \core_message\api::get_conversations($user1->id, 0, 20, null, true));
b8ff2c44
JD
1050
1051 // Now, get 10 conversations starting at the second record. We should see 2 conversations.
888a467a 1052 $this->assertCount(3, \core_message\api::get_conversations($user1->id, 1, 10, null, true));
b8ff2c44
JD
1053
1054 // Now, try to get favourited conversations using an invalid offset.
888a467a 1055 $this->assertCount(0, \core_message\api::get_conversations($user1->id, 5, 10, null, true));
b8ff2c44
JD
1056 }
1057
1058 /**
1059 * Tests retrieving favourite conversations when a conversation contains a deleted user.
1060 */
1061 public function test_get_favourite_conversations_with_deleted_user() {
1062 // Create some users.
1063 $user1 = self::getDataGenerator()->create_user();
1064 $user2 = self::getDataGenerator()->create_user();
1065 $user3 = self::getDataGenerator()->create_user();
1066
1067 // Send some messages back and forth, have some different conversations with different users.
1068 $time = 1;
1069 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1070 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1071 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1072 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1073
1074 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
1075 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
1076 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
1077 $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
1078
1079 // Favourite the all conversations for user1.
1080 $convoids = [];
1081 $convoids[] = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1082 $convoids[] = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
1083 $user1context = context_user::instance($user1->id);
1084 $service = \core_favourites\service_factory::get_service_for_user_context($user1context);
1085 foreach ($convoids as $convoid) {
1086 $service->create_favourite('core_message', 'message_conversations', $convoid, $user1context);
1087 }
1088
1089 // Delete the second user.
1090 delete_user($user2);
1091
1092 // Retrieve the conversations.
1093 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, null, true);
1094
0847aa12 1095 // We should have both conversations, despite the other user being soft-deleted.
888a467a
AA
1096 // Consider first conversations is self-conversation.
1097 $this->assertCount(3, $conversations);
b8ff2c44
JD
1098
1099 // Confirm the conversation is from the non-deleted user.
1100 $conversation = reset($conversations);
eb5865da 1101 $this->assertEquals($convoids[1], $conversation->id);
b8ff2c44
JD
1102 }
1103
1104 /**
1105 * Test confirming that conversations can be marked as favourites.
1106 */
1107 public function test_set_favourite_conversation() {
1108 // Create some users.
1109 $user1 = self::getDataGenerator()->create_user();
1110 $user2 = self::getDataGenerator()->create_user();
1111 $user3 = self::getDataGenerator()->create_user();
1112
1113 // Send some messages back and forth, have some different conversations with different users.
1114 $time = 1;
1115 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1116 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1117 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1118 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1119
1120 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
1121 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
1122 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
1123 $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
1124
1125 // Favourite the first conversation as user 1.
1126 $conversationid1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
9e189a91 1127 $favourite = \core_message\api::set_favourite_conversation($conversationid1, $user1->id);
b8ff2c44 1128
888a467a
AA
1129 // Verify we have two favourite conversations a user 1.
1130 // Consider first conversations is self-conversation.
1131 $this->assertCount(2, \core_message\api::get_conversations($user1->id, 0, 20, null, true));
b8ff2c44 1132
888a467a
AA
1133 // Verify we have only one favourite as user2, despite being a member in that conversation.
1134 // Consider first conversations is self-conversation.
1135 $this->assertCount(1, \core_message\api::get_conversations($user2->id, 0, 20, null, true));
b8ff2c44 1136
9e189a91
RW
1137 // Try to favourite the same conversation again should just return the existing favourite.
1138 $repeatresult = \core_message\api::set_favourite_conversation($conversationid1, $user1->id);
1139 $this->assertEquals($favourite->id, $repeatresult->id);
b8ff2c44
JD
1140 }
1141
1142 /**
1143 * Test verifying that trying to mark a non-existent conversation as a favourite, results in an exception.
1144 */
1145 public function test_set_favourite_conversation_nonexistent_conversation() {
1146 // Create some users.
1147 $user1 = self::getDataGenerator()->create_user();
1148 // Try to favourite a non-existent conversation.
1149 $this->expectException(\moodle_exception::class);
1150 \core_message\api::set_favourite_conversation(0, $user1->id);
1151 }
1152
1153 /**
1154 * Test verifying that a conversation cannot be marked as favourite unless the user is a member of that conversation.
1155 */
1156 public function test_set_favourite_conversation_non_member() {
1157 // Create some users.
1158 $user1 = self::getDataGenerator()->create_user();
1159 $user2 = self::getDataGenerator()->create_user();
1160 $user3 = self::getDataGenerator()->create_user();
1161
1162 // Send some messages back and forth, have some different conversations with different users.
1163 $time = 1;
1164 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1165 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1166 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1167 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1168
1169 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
1170 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
1171 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
1172 $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
1173
1174 // Try to favourite the first conversation as user 3, who is not a member.
1175 $conversationid1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1176 $this->expectException(\moodle_exception::class);
1177 \core_message\api::set_favourite_conversation($conversationid1, $user3->id);
1178 }
1179
1180 /**
1181 * Test confirming that those conversations marked as favourites can be unfavourited.
1182 */
1183 public function test_unset_favourite_conversation() {
1184 // Create some users.
1185 $user1 = self::getDataGenerator()->create_user();
1186 $user2 = self::getDataGenerator()->create_user();
1187 $user3 = self::getDataGenerator()->create_user();
1188
1189 // Send some messages back and forth, have some different conversations with different users.
1190 $time = 1;
1191 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1192 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1193 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1194 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1195
1196 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
1197 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
1198 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
1199 $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
1200
1201 // Favourite the first conversation as user 1 and the second as user 3.
1202 $conversationid1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1203 $conversationid2 = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
1204 \core_message\api::set_favourite_conversation($conversationid1, $user1->id);
1205 \core_message\api::set_favourite_conversation($conversationid2, $user3->id);
1206
888a467a
AA
1207 // Verify we have two favourite conversations for both user 1 and user 3, counting self conversations.
1208 $this->assertCount(2, \core_message\api::get_conversations($user1->id, 0, 20, null, true));
1209 $this->assertCount(2, \core_message\api::get_conversations($user3->id, 0, 20, null, true));
b8ff2c44
JD
1210
1211 // Now unfavourite the conversation as user 1.
1212 \core_message\api::unset_favourite_conversation($conversationid1, $user1->id);
1213
888a467a
AA
1214 // Verify we have two favourite conversations user 3 only, and one for user1, counting self conversations.
1215 $this->assertCount(2, \core_message\api::get_conversations($user3->id, 0, 20, null, true));
1216 $this->assertCount(1, \core_message\api::get_conversations($user1->id, 0, 20, null, true));
b8ff2c44
JD
1217
1218 // Try to favourite the same conversation again as user 1.
1219 $this->expectException(\moodle_exception::class);
1220 \core_message\api::unset_favourite_conversation($conversationid1, $user1->id);
1221 }
1222
1223 /**
1224 * Test verifying that a valid conversation cannot be unset as a favourite if it's not marked as a favourite.
1225 */
1226 public function test_unset_favourite_conversation_not_favourite() {
1227 // Create some users.
1228 $user1 = self::getDataGenerator()->create_user();
1229 $user2 = self::getDataGenerator()->create_user();
1230
1231 // Send some messages back and forth, have some different conversations with different users.
1232 $time = 1;
1233 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1234 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1235 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1236 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1237
1238 // Now try to unfavourite the conversation as user 1.
1239 $conversationid1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1240 $this->expectException(\moodle_exception::class);
1241 \core_message\api::unset_favourite_conversation($conversationid1, $user1->id);
1242 }
1243
1244 /**
1245 * Test verifying that a non-existent conversation cannot be unset as a favourite.
1246 */
1247 public function test_unset_favourite_conversation_non_existent_conversation() {
1248 // Create some users.
1249 $user1 = self::getDataGenerator()->create_user();
1250
1251 // Now try to unfavourite the conversation as user 1.
1252 $this->expectException(\moodle_exception::class);
1253 \core_message\api::unset_favourite_conversation(0, $user1->id);
1254 }
1255
1f64514d 1256 /**
eb5865da 1257 * Helper to seed the database with initial state.
1f64514d 1258 */
eb5865da 1259 protected function create_conversation_test_data() {
1f64514d
MN
1260 // Create some users.
1261 $user1 = self::getDataGenerator()->create_user();
1262 $user2 = self::getDataGenerator()->create_user();
1263 $user3 = self::getDataGenerator()->create_user();
1264 $user4 = self::getDataGenerator()->create_user();
1265
eb5865da 1266 $time = 1;
1f64514d 1267
eb5865da
JD
1268 // Create some conversations. We want:
1269 // 1) At least one of each type (group, individual) of which user1 IS a member and DID send the most recent message.
1270 // 2) At least one of each type (group, individual) of which user1 IS a member and DID NOT send the most recent message.
1271 // 3) At least one of each type (group, individual) of which user1 IS NOT a member.
1272 // 4) At least two group conversation having 0 messages, of which user1 IS a member (To confirm conversationid ordering).
1273 // 5) At least one group conversation having 0 messages, of which user1 IS NOT a member.
1274
1275 // Individual conversation, user1 is a member, last message from other user.
1276 $ic1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1277 [$user1->id, $user2->id]);
1278 testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 1', $time);
1279 testhelper::send_fake_message_to_conversation($user2, $ic1->id, 'Message 2', $time + 1);
1280
1281 // Individual conversation, user1 is a member, last message from user1.
1282 $ic2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1283 [$user1->id, $user3->id]);
1284 testhelper::send_fake_message_to_conversation($user3, $ic2->id, 'Message 3', $time + 2);
1285 testhelper::send_fake_message_to_conversation($user1, $ic2->id, 'Message 4', $time + 3);
1286
1287 // Individual conversation, user1 is not a member.
1288 $ic3 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1289 [$user2->id, $user3->id]);
1290 testhelper::send_fake_message_to_conversation($user2, $ic3->id, 'Message 5', $time + 4);
1291 testhelper::send_fake_message_to_conversation($user3, $ic3->id, 'Message 6', $time + 5);
1292
1293 // Group conversation, user1 is not a member.
1294 $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1295 [$user2->id, $user3->id, $user4->id], 'Project discussions');
1296 testhelper::send_fake_message_to_conversation($user2, $gc1->id, 'Message 7', $time + 6);
1297 testhelper::send_fake_message_to_conversation($user4, $gc1->id, 'Message 8', $time + 7);
1298
1299 // Group conversation, user1 is a member, last message from another user.
1300 $gc2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1301 [$user1->id, $user3->id, $user4->id], 'Group chat');
1302 testhelper::send_fake_message_to_conversation($user1, $gc2->id, 'Message 9', $time + 8);
1303 testhelper::send_fake_message_to_conversation($user3, $gc2->id, 'Message 10', $time + 9);
1304 testhelper::send_fake_message_to_conversation($user4, $gc2->id, 'Message 11', $time + 10);
1305
1306 // Group conversation, user1 is a member, last message from user1.
1307 $gc3 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1308 [$user1->id, $user2->id, $user3->id, $user4->id], 'Group chat again!');
1309 testhelper::send_fake_message_to_conversation($user4, $gc3->id, 'Message 12', $time + 11);
1310 testhelper::send_fake_message_to_conversation($user3, $gc3->id, 'Message 13', $time + 12);
1311 testhelper::send_fake_message_to_conversation($user1, $gc3->id, 'Message 14', $time + 13);
1312
1313 // Empty group conversations (x2), user1 is a member.
1314 $gc4 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1315 [$user1->id, $user2->id, $user3->id], 'Empty group');
1316 $gc5 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1317 [$user1->id, $user2->id, $user4->id], 'Another empty group');
1318
1319 // Empty group conversation, user1 is NOT a member.
1320 $gc6 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1321 [$user2->id, $user3->id, $user4->id], 'Empty group 3');
1322
1323 return [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6];
1324 }
bb2924d0 1325
eb5865da
JD
1326 /**
1327 * Test verifying get_conversations when no limits, offsets, type filters or favourite restrictions are used.
1328 */
1329 public function test_get_conversations_no_restrictions() {
74fc14e5 1330 global $DB;
888a467a 1331
eb5865da 1332 $user1 = self::getDataGenerator()->create_user();
888a467a
AA
1333 // Self-conversation should exists.
1334 $this->assertCount(1, \core_message\api::get_conversations($user1->id));
1f64514d 1335
eb5865da
JD
1336 // Get a bunch of conversations, some group, some individual and in different states.
1337 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1338 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1339
1340 // Get all conversations for user1.
1341 $conversations = core_message\api::get_conversations($user1->id);
1342
888a467a
AA
1343 // Verify there are 2 individual conversation, 2 group conversations, 2 empty group conversations,
1344 // and a self-conversation.
eb5865da
JD
1345 // The conversations with the most recent messages should be listed first, followed by the empty
1346 // conversations, with the most recently created first.
888a467a 1347 $this->assertCount(7, $conversations);
eb5865da
JD
1348 $typecounts = array_count_values(array_column($conversations, 'type'));
1349 $this->assertEquals(2, $typecounts[1]);
1350 $this->assertEquals(4, $typecounts[2]);
888a467a 1351 $this->assertEquals(1, $typecounts[3]);
eb5865da 1352
888a467a 1353 // Those conversations having messages should be listed after self-conversation, ordered by most recent message time.
eb5865da
JD
1354 $this->assertEquals($gc3->id, $conversations[0]->id);
1355 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $conversations[0]->type);
888a467a 1356 $this->assertFalse($conversations[1]->isfavourite);
eb5865da
JD
1357 $this->assertCount(1, $conversations[0]->members);
1358 $this->assertEquals(4, $conversations[0]->membercount);
1359 $this->assertCount(1, $conversations[0]->messages);
74fc14e5
JD
1360 $message = $DB->get_record('messages', ['id' => $conversations[0]->messages[0]->id]);
1361 $expectedmessagetext = message_format_message_text($message);
1362 $this->assertEquals($expectedmessagetext, $conversations[0]->messages[0]->text);
eb5865da
JD
1363 $this->assertEquals($user1->id, $conversations[0]->messages[0]->useridfrom);
1364
1365 $this->assertEquals($gc2->id, $conversations[1]->id);
1366 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $conversations[1]->type);
1367 $this->assertFalse($conversations[1]->isfavourite);
1368 $this->assertCount(1, $conversations[1]->members);
1369 $this->assertEquals(3, $conversations[1]->membercount);
1370 $this->assertCount(1, $conversations[1]->messages);
74fc14e5
JD
1371 $message = $DB->get_record('messages', ['id' => $conversations[1]->messages[0]->id]);
1372 $expectedmessagetext = message_format_message_text($message);
1373 $this->assertEquals($expectedmessagetext, $conversations[1]->messages[0]->text);
eb5865da
JD
1374 $this->assertEquals($user4->id, $conversations[1]->messages[0]->useridfrom);
1375
1376 $this->assertEquals($ic2->id, $conversations[2]->id);
1377 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $conversations[2]->type);
1378 $this->assertFalse($conversations[2]->isfavourite);
1379 $this->assertCount(1, $conversations[2]->members);
1380 $this->assertEquals($user3->id, $conversations[2]->members[$user3->id]->id);
1381 $this->assertEquals(2, $conversations[2]->membercount);
1382 $this->assertCount(1, $conversations[2]->messages);
74fc14e5
JD
1383 $message = $DB->get_record('messages', ['id' => $conversations[2]->messages[0]->id]);
1384 $expectedmessagetext = message_format_message_text($message);
1385 $this->assertEquals($expectedmessagetext, $conversations[2]->messages[0]->text);
eb5865da
JD
1386 $this->assertEquals($user1->id, $conversations[2]->messages[0]->useridfrom);
1387
1388 $this->assertEquals($ic1->id, $conversations[3]->id);
1389 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $conversations[3]->type);
1390 $this->assertFalse($conversations[3]->isfavourite);
1391 $this->assertCount(1, $conversations[3]->members);
1392 $this->assertEquals(2, $conversations[3]->membercount);
1393 $this->assertCount(1, $conversations[3]->messages);
74fc14e5
JD
1394 $message = $DB->get_record('messages', ['id' => $conversations[3]->messages[0]->id]);
1395 $expectedmessagetext = message_format_message_text($message);
1396 $this->assertEquals($expectedmessagetext, $conversations[3]->messages[0]->text);
eb5865da
JD
1397 $this->assertEquals($user2->id, $conversations[3]->messages[0]->useridfrom);
1398
1399 // Of the groups without messages, we expect to see the most recently created first.
1400 $this->assertEquals($gc5->id, $conversations[4]->id);
1401 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $conversations[4]->type);
1402 $this->assertFalse($conversations[4]->isfavourite);
1403 $this->assertCount(0, $conversations[4]->members); // No members returned, because no recent messages exist.
1404 $this->assertEquals(3, $conversations[4]->membercount);
1405 $this->assertEmpty($conversations[4]->messages);
1406
1407 $this->assertEquals($gc4->id, $conversations[5]->id);
1408 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $conversations[5]->type);
1409 $this->assertFalse($conversations[5]->isfavourite);
1410 $this->assertCount(0, $conversations[5]->members);
1411 $this->assertEquals(3, $conversations[5]->membercount);
1412 $this->assertEmpty($conversations[5]->messages);
1413
1414 // Verify format of the return structure.
1415 foreach ($conversations as $conv) {
1416 $this->assertObjectHasAttribute('id', $conv);
1417 $this->assertObjectHasAttribute('name', $conv);
1418 $this->assertObjectHasAttribute('subname', $conv);
003cdcce 1419 $this->assertObjectHasAttribute('imageurl', $conv);
eb5865da
JD
1420 $this->assertObjectHasAttribute('type', $conv);
1421 $this->assertObjectHasAttribute('isfavourite', $conv);
1422 $this->assertObjectHasAttribute('membercount', $conv);
1423 $this->assertObjectHasAttribute('isread', $conv);
1424 $this->assertObjectHasAttribute('unreadcount', $conv);
1425 $this->assertObjectHasAttribute('members', $conv);
1426 foreach ($conv->members as $member) {
1427 $this->assertObjectHasAttribute('id', $member);
1428 $this->assertObjectHasAttribute('fullname', $member);
1429 $this->assertObjectHasAttribute('profileimageurl', $member);
1430 $this->assertObjectHasAttribute('profileimageurlsmall', $member);
1431 $this->assertObjectHasAttribute('isonline', $member);
1432 $this->assertObjectHasAttribute('showonlinestatus', $member);
1433 $this->assertObjectHasAttribute('isblocked', $member);
1434 $this->assertObjectHasAttribute('iscontact', $member);
d15c1e77 1435 $this->assertObjectHasAttribute('isdeleted', $member);
cef1d977
MN
1436 $this->assertObjectHasAttribute('canmessage', $member);
1437 $this->assertObjectHasAttribute('requirescontact', $member);
1438 $this->assertObjectHasAttribute('contactrequests', $member);
eb5865da
JD
1439 }
1440 $this->assertObjectHasAttribute('messages', $conv);
1441 foreach ($conv->messages as $message) {
1442 $this->assertObjectHasAttribute('id', $message);
1443 $this->assertObjectHasAttribute('useridfrom', $message);
1444 $this->assertObjectHasAttribute('text', $message);
1445 $this->assertObjectHasAttribute('timecreated', $message);
1446 }
1447 }
1448 }
1f64514d 1449
74fc14e5
JD
1450 /**
1451 * Test verifying that html format messages are supported, and that message_format_message_text() is being called appropriately.
1452 */
1453 public function test_get_conversations_message_format() {
1454 global $DB;
1455 // Create some users.
1456 $user1 = self::getDataGenerator()->create_user();
1457 $user2 = self::getDataGenerator()->create_user();
1458
1459 // Create conversation.
1460 $conversation = \core_message\api::create_conversation(
1461 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1462 [$user1->id, $user2->id]
1463 );
1464
1465 // Send some messages back and forth.
1466 $time = 1;
1467 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 1);
1468 $mid = testhelper::send_fake_message_to_conversation($user1, $conversation->id, '<a href="#">A link</a>', $time + 2);
1469
1470 // Verify the format of the html message.
1471 $message = $DB->get_record('messages', ['id' => $mid]);
1472 $expectedmessagetext = message_format_message_text($message);
1473 $conversations = \core_message\api::get_conversations($user1->id);
1474 $messages = $conversations[0]->messages;
1475 $this->assertEquals($expectedmessagetext, $messages[0]->text);
1476 }
1477
00c59245
MN
1478 /**
1479 * Test verifying get_conversations identifies if a conversation is muted or not.
1480 */
1481 public function test_get_conversations_some_muted() {
1482 // Create some users.
1483 $user1 = self::getDataGenerator()->create_user();
1484 $user2 = self::getDataGenerator()->create_user();
1485 $user3 = self::getDataGenerator()->create_user();
1486
1487 $conversation1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1488 [$user1->id, $user2->id]);
1489 testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1');
1490 testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 2');
1491 \core_message\api::mute_conversation($user1->id, $conversation1->id);
1492
1493 $conversation2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1494 [$user1->id, $user3->id]);
1495 testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 1');
1496 testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2');
1497
1498 $conversation3 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1499 [$user1->id, $user2->id]);
1500 \core_message\api::mute_conversation($user1->id, $conversation3->id);
1501
1502 $conversation4 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1503 [$user1->id, $user3->id]);
1504
1505 $conversations = \core_message\api::get_conversations($user1->id);
1506
1507 usort($conversations, function($first, $second){
1508 return $first->id > $second->id;
1509 });
1510
888a467a
AA
1511 // Consider first conversations is self-conversation.
1512 $selfconversation = array_shift($conversations);
00c59245
MN
1513 $conv1 = array_shift($conversations);
1514 $conv2 = array_shift($conversations);
1515 $conv3 = array_shift($conversations);
1516 $conv4 = array_shift($conversations);
1517
1518 $this->assertTrue($conv1->ismuted);
1519 $this->assertFalse($conv2->ismuted);
1520 $this->assertTrue($conv3->ismuted);
1521 $this->assertFalse($conv4->ismuted);
1522 }
1523
eb5865da
JD
1524 /**
1525 * Tests retrieving conversations with a limit and offset to ensure pagination works correctly.
1526 */
1527 public function test_get_conversations_limit_offset() {
1528 // Get a bunch of conversations, some group, some individual and in different states.
1529 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1530 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1f64514d 1531
eb5865da
JD
1532 // Get all conversations for user1, limited to 1 result.
1533 $conversations = core_message\api::get_conversations($user1->id, 0, 1);
1f64514d 1534
eb5865da
JD
1535 // Verify the first conversation.
1536 $this->assertCount(1, $conversations);
1537 $conversation = array_shift($conversations);
1538 $this->assertEquals($conversation->id, $gc3->id);
1f64514d 1539
eb5865da
JD
1540 // Verify the next conversation.
1541 $conversations = \core_message\api::get_conversations($user1->id, 1, 1);
1542 $this->assertCount(1, $conversations);
1543 $this->assertEquals($gc2->id, $conversations[0]->id);
1f64514d 1544
eb5865da
JD
1545 // Verify the next conversation.
1546 $conversations = \core_message\api::get_conversations($user1->id, 2, 1);
1547 $this->assertCount(1, $conversations);
1548 $this->assertEquals($ic2->id, $conversations[0]->id);
1f64514d 1549
eb5865da
JD
1550 // Skip one and get both empty conversations.
1551 $conversations = \core_message\api::get_conversations($user1->id, 4, 2);
1552 $this->assertCount(2, $conversations);
1553 $this->assertEquals($gc5->id, $conversations[0]->id);
1554 $this->assertEmpty($conversations[0]->messages);
1555 $this->assertEquals($gc4->id, $conversations[1]->id);
1556 $this->assertEmpty($conversations[1]->messages);
1f64514d 1557
eb5865da
JD
1558 // Ask for an offset that doesn't exist and verify no conversations are returned.
1559 $conversations = \core_message\api::get_conversations($user1->id, 10, 1);
1560 $this->assertCount(0, $conversations);
1f64514d
MN
1561 }
1562
4d146f1a 1563 /**
eb5865da 1564 * Test verifying the type filtering behaviour of the
4d146f1a 1565 */
eb5865da
JD
1566 public function test_get_conversations_type_filter() {
1567 // Get a bunch of conversations, some group, some individual and in different states.
1568 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1569 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1570
1571 // Verify we can ask for only individual conversations.
1572 $conversations = \core_message\api::get_conversations($user1->id, 0, 20,
1573 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL);
1574 $this->assertCount(2, $conversations);
4d146f1a 1575
eb5865da
JD
1576 // Verify we can ask for only group conversations.
1577 $conversations = \core_message\api::get_conversations($user1->id, 0, 20,
1578 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP);
1579 $this->assertCount(4, $conversations);
4d146f1a 1580
eb5865da
JD
1581 // Verify an exception is thrown if an unrecognized type is specified.
1582 $this->expectException(\moodle_exception::class);
1583 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, 0);
1584 }
4d146f1a 1585
425f5adc 1586 /**
734b198f 1587 * Tests retrieving conversations when a 'self' conversation exists.
425f5adc 1588 */
734b198f 1589 public function test_get_conversations_self_conversations() {
425f5adc
JD
1590 global $DB;
1591
734b198f 1592 // Create a conversation between one user and themself.
425f5adc 1593 $user1 = self::getDataGenerator()->create_user();
734b198f
SA
1594 $user2 = self::getDataGenerator()->create_user();
1595 $user3 = self::getDataGenerator()->create_user();
1596 $user4 = self::getDataGenerator()->create_user();
1597
1598 // Create some individual conversations.
1599 $ic1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1600 [$user1->id, $user2->id]);
1601 $ic2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1602 [$user1->id, $user3->id]);
1603 testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message from user1 to user2');
1604
888a467a
AA
1605 // Get some self-conversations.
1606 $sc1 = \core_message\api::get_self_conversation($user1->id);
1607 $sc4 = \core_message\api::get_self_conversation($user4->id);
734b198f 1608 testhelper::send_fake_message_to_conversation($user1, $sc1->id, 'Test message to self 1!');
425f5adc
JD
1609
1610 // Verify we are in a 'self' conversation state.
734b198f
SA
1611 $members = $DB->get_records('message_conversation_members', ['conversationid' => $sc1->id]);
1612 $this->assertCount(1, $members);
425f5adc
JD
1613 $member = array_pop($members);
1614 $this->assertEquals($user1->id, $member->userid);
1615
734b198f
SA
1616 // Verify the self-conversations are returned by the method.
1617 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF);
1618 $this->assertCount(1, $conversations);
1619 $conversation = array_pop($conversations);
1620 $this->assertEquals($conversation->id, $sc1->id);
1621
1622 $conversations = \core_message\api::get_conversations($user4->id);
1623 // The self-conversation.
1624 $this->assertCount(1, $conversations);
1625
1626 // Get only private conversations for user1 (empty conversations, like $ic2, are not returned).
1627 $conversations = \core_message\api::get_conversations($user1->id, 0, 20,
1628 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL);
1629 $this->assertCount(1, $conversations);
1630
1631 // Merge self with private conversations for user1.
1632 $conversations = \core_message\api::get_conversations($user1->id, 0, 20,
1633 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, null, true);
1634 $this->assertCount(2, $conversations);
1635
1636 // Get only private conversations for user2.
1637 $conversations = \core_message\api::get_conversations($user2->id, 0, 20,
1638 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL);
1639 $this->assertCount(1, $conversations);
1640
888a467a 1641 // Merge self with private conversations for user2.
734b198f
SA
1642 $conversations = \core_message\api::get_conversations($user2->id, 0, 20,
1643 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, null, true);
888a467a 1644 $this->assertCount(2, $conversations);
425f5adc
JD
1645 }
1646
eb5865da
JD
1647 /**
1648 * Tests retrieving conversations when a conversation contains a deleted user.
1649 */
1650 public function test_get_conversations_with_deleted_user() {
1651 // Get a bunch of conversations, some group, some individual and in different states.
1652 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1653 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
4d146f1a 1654
eb5865da 1655 // Delete the second user and retrieve the conversations.
0847aa12
JD
1656 // We should have 6 still, as conversations with soft-deleted users are still returned.
1657 // Group conversations are also present, albeit with less members.
eb5865da 1658 delete_user($user2);
52beee65
MN
1659 // This is to confirm an exception is not thrown when a user AND the user context is deleted.
1660 // We no longer delete the user context, but historically we did.
1661 context_helper::delete_instance(CONTEXT_USER, $user2->id);
eb5865da 1662 $conversations = \core_message\api::get_conversations($user1->id);
888a467a
AA
1663 // Consider there's a self-conversation (the last one).
1664 $this->assertCount(7, $conversations);
eb5865da
JD
1665 $this->assertEquals($gc3->id, $conversations[0]->id);
1666 $this->assertcount(1, $conversations[0]->members);
1667 $this->assertEquals($gc2->id, $conversations[1]->id);
1668 $this->assertcount(1, $conversations[1]->members);
1669 $this->assertEquals($ic2->id, $conversations[2]->id);
0847aa12
JD
1670 $this->assertEquals($ic1->id, $conversations[3]->id);
1671 $this->assertEquals($gc5->id, $conversations[4]->id);
1672 $this->assertEquals($gc4->id, $conversations[5]->id);
eb5865da
JD
1673
1674 // Delete a user from a group conversation where that user had sent the most recent message.
1675 // This user will still be present in the members array, as will the message in the messages array.
1676 delete_user($user4);
1677 $conversations = \core_message\api::get_conversations($user1->id);
888a467a
AA
1678
1679 // Consider there's a self-conversation (the last one).
1680 $this->assertCount(7, $conversations);
eb5865da
JD
1681 $this->assertEquals($gc2->id, $conversations[1]->id);
1682 $this->assertcount(1, $conversations[1]->members);
1683 $this->assertEquals($user4->id, $conversations[1]->members[$user4->id]->id);
1684 $this->assertcount(1, $conversations[1]->messages);
1685 $this->assertEquals($user4->id, $conversations[1]->messages[0]->useridfrom);
1686
1687 // Delete the third user and retrieve the conversations.
0847aa12
JD
1688 // We should have 6 still, as conversations with soft-deleted users are still returned.
1689 // Group conversations are also present, albeit with less members.
eb5865da
JD
1690 delete_user($user3);
1691 $conversations = \core_message\api::get_conversations($user1->id);
888a467a
AA
1692 // Consider there's a self-conversation (the last one).
1693 $this->assertCount(7, $conversations);
eb5865da
JD
1694 $this->assertEquals($gc3->id, $conversations[0]->id);
1695 $this->assertcount(1, $conversations[0]->members);
1696 $this->assertEquals($gc2->id, $conversations[1]->id);
1697 $this->assertcount(1, $conversations[1]->members);
0847aa12
JD
1698 $this->assertEquals($ic2->id, $conversations[2]->id);
1699 $this->assertEquals($ic1->id, $conversations[3]->id);
1700 $this->assertEquals($gc5->id, $conversations[4]->id);
1701 $this->assertEquals($gc4->id, $conversations[5]->id);
eb5865da 1702 }
4d146f1a 1703
aa748351
JD
1704 /**
1705 * Test confirming the behaviour of get_conversations() when users delete all messages.
1706 */
1707 public function test_get_conversations_deleted_messages() {
1708 // Get a bunch of conversations, some group, some individual and in different states.
1709 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1710 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1711
1712 $conversations = \core_message\api::get_conversations($user1->id);
888a467a
AA
1713 // Consider first conversations is self-conversation.
1714 $this->assertCount(7, $conversations);
aa748351
JD
1715
1716 // Delete all messages from a group conversation the user is in - it should be returned.
1717 $this->assertTrue(\core_message\api::is_user_in_conversation($user1->id, $gc2->id));
1718 $convmessages = \core_message\api::get_conversation_messages($user1->id, $gc2->id);
1719 $messages = $convmessages['messages'];
1720 foreach ($messages as $message) {
1721 \core_message\api::delete_message($user1->id, $message->id);
1722 }
1723 $conversations = \core_message\api::get_conversations($user1->id);
888a467a
AA
1724 // Consider first conversations is self-conversation.
1725 $this->assertCount(7, $conversations);
aa748351
JD
1726 $this->assertContains($gc2->id, array_column($conversations, 'id'));
1727
1728 // Delete all messages from an individual conversation the user is in - it should not be returned.
1729 $this->assertTrue(\core_message\api::is_user_in_conversation($user1->id, $ic1->id));
1730 $convmessages = \core_message\api::get_conversation_messages($user1->id, $ic1->id);
1731 $messages = $convmessages['messages'];
1732 foreach ($messages as $message) {
1733 \core_message\api::delete_message($user1->id, $message->id);
1734 }
1735 $conversations = \core_message\api::get_conversations($user1->id);
888a467a
AA
1736 // Consider first conversations is self-conversation.
1737 $this->assertCount(6, $conversations);
aa748351
JD
1738 $this->assertNotContains($ic1->id, array_column($conversations, 'id'));
1739 }
1740
e926b4d0
AN
1741 /**
1742 * Test verifying the behaviour of get_conversations() when fetching favourite conversations with only a single
1743 * favourite.
1744 */
1745 public function test_get_conversations_favourite_conversations_single() {
1746 // Get a bunch of conversations, some group, some individual and in different states.
1747 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1748 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1749
1750 // Mark a single conversation as favourites.
1751 \core_message\api::set_favourite_conversation($ic2->id, $user1->id);
1752
1753 // Get the conversation, first with no restrictions, confirming the favourite status of the conversations.
1754 $conversations = \core_message\api::get_conversations($user1->id);
888a467a
AA
1755 // Consider there is a self-conversation.
1756 $selfconversation = \core_message\api::get_self_conversation($user1->id);
1757 $this->assertCount(7, $conversations);
e926b4d0 1758 foreach ($conversations as $conv) {
888a467a 1759 if (in_array($conv->id, [$ic2->id, $selfconversation->id])) {
e926b4d0
AN
1760 $this->assertTrue($conv->isfavourite);
1761 } else {
1762 $this->assertFalse($conv->isfavourite);
1763 }
1764 }
1765
888a467a 1766 // Now, get ONLY favourite conversations (including self-conversation).
e926b4d0 1767 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, null, true);
888a467a 1768 $this->assertCount(2, $conversations);
e926b4d0 1769 foreach ($conversations as $conv) {
e4987f0f
AA
1770 if ($conv->type != \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF) {
1771 $this->assertTrue($conv->isfavourite);
1772 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $conv->type);
1773 $this->assertEquals($ic2->id, $conv->id);
1774 }
e926b4d0
AN
1775 }
1776
1777 // Now, try ONLY favourites of type 'group'.
1778 $conversations = \core_message\api::get_conversations($user1->id, 0, 20,
1779 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, true);
1780 $this->assertEmpty($conversations);
1781
1782 // And NO favourite conversations.
1783 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, null, false);
1784 $this->assertCount(5, $conversations);
1785 foreach ($conversations as $conv) {
1786 $this->assertFalse($conv->isfavourite);
1787 $this->assertNotEquals($ic2, $conv->id);
1788 }
1789 }
1790
eb5865da
JD
1791 /**
1792 * Test verifying the behaviour of get_conversations() when fetching favourite conversations.
1793 */
1794 public function test_get_conversations_favourite_conversations() {
1795 // Get a bunch of conversations, some group, some individual and in different states.
1796 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1797 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
4d146f1a 1798
888a467a
AA
1799 // Try to get ONLY favourite conversations, when only self-conversation exist.
1800 $this->assertCount(1, \core_message\api::get_conversations($user1->id, 0, 20, null, true));
1801
1802 // Unstar self-conversation.
1803 $selfconversation = \core_message\api::get_self_conversation($user1->id);
1804 \core_message\api::unset_favourite_conversation($selfconversation->id, $user1->id);
1805
eb5865da
JD
1806 // Try to get ONLY favourite conversations, when no favourites exist.
1807 $this->assertEquals([], \core_message\api::get_conversations($user1->id, 0, 20, null, true));
4d146f1a 1808
eb5865da 1809 // Try to get NO favourite conversations, when no favourites exist.
888a467a 1810 $this->assertCount(7, \core_message\api::get_conversations($user1->id, 0, 20, null, false));
4d146f1a 1811
eb5865da
JD
1812 // Mark a few conversations as favourites.
1813 \core_message\api::set_favourite_conversation($ic1->id, $user1->id);
1814 \core_message\api::set_favourite_conversation($gc2->id, $user1->id);
1815 \core_message\api::set_favourite_conversation($gc5->id, $user1->id);
e926b4d0 1816 $favouriteids = [$ic1->id, $gc2->id, $gc5->id];
4d146f1a 1817
eb5865da
JD
1818 // Get the conversations, first with no restrictions, confirming the favourite status of the conversations.
1819 $conversations = \core_message\api::get_conversations($user1->id);
888a467a 1820 $this->assertCount(7, $conversations);
eb5865da 1821 foreach ($conversations as $conv) {
e926b4d0 1822 if (in_array($conv->id, $favouriteids)) {
eb5865da 1823 $this->assertTrue($conv->isfavourite);
e926b4d0
AN
1824 } else {
1825 $this->assertFalse($conv->isfavourite);
eb5865da
JD
1826 }
1827 }
4d146f1a 1828
eb5865da
JD
1829 // Now, get ONLY favourite conversations.
1830 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, null, true);
1831 $this->assertCount(3, $conversations);
1832 foreach ($conversations as $conv) {
1833 $this->assertTrue($conv->isfavourite);
e926b4d0 1834 $this->assertNotFalse(array_search($conv->id, $favouriteids));
eb5865da 1835 }
4d146f1a 1836
eb5865da
JD
1837 // Now, try ONLY favourites of type 'group'.
1838 $conversations = \core_message\api::get_conversations($user1->id, 0, 20,
1839 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, true);
1840 $this->assertCount(2, $conversations);
1841 foreach ($conversations as $conv) {
1842 $this->assertTrue($conv->isfavourite);
e926b4d0 1843 $this->assertNotFalse(array_search($conv->id, [$gc2->id, $gc5->id]));
eb5865da 1844 }
4d146f1a 1845
eb5865da
JD
1846 // And NO favourite conversations.
1847 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, null, false);
888a467a 1848 $this->assertCount(4, $conversations);
eb5865da
JD
1849 foreach ($conversations as $conv) {
1850 $this->assertFalse($conv->isfavourite);
e926b4d0 1851 $this->assertFalse(array_search($conv->id, $favouriteids));
eb5865da 1852 }
4d146f1a
RW
1853 }
1854
cef1d977
MN
1855 /**
1856 * Test verifying get_conversations when there are users in a group and/or individual conversation. The reason this
1857 * test is performed is because we do not need as much data for group conversations (saving DB calls), so we want
1858 * to confirm this happens.
1859 */
1860 public function test_get_conversations_user_in_group_and_individual_chat() {
1861 $this->resetAfterTest();
1862
1863 $user1 = self::getDataGenerator()->create_user();
1864 $user2 = self::getDataGenerator()->create_user();
1865 $user3 = self::getDataGenerator()->create_user();
1866
1867 $conversation = \core_message\api::create_conversation(
1868 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1869 [
1870 $user1->id,
1871 $user2->id
1872 ],
1873 'Individual conversation'
1874 );
1875
1876 testhelper::send_fake_message_to_conversation($user1, $conversation->id);
1877
1878 $conversation = \core_message\api::create_conversation(
1879 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1880 [
1881 $user1->id,
1882 $user2->id,
1883 ],
1884 'Group conversation'
1885 );
1886
1887 testhelper::send_fake_message_to_conversation($user1, $conversation->id);
1888
1889 \core_message\api::create_contact_request($user1->id, $user2->id);
1890 \core_message\api::create_contact_request($user1->id, $user3->id);
1891
1892 $conversations = \core_message\api::get_conversations($user2->id);
1893
1894 $groupconversation = array_shift($conversations);
1895 $individualconversation = array_shift($conversations);
1896
1897 $this->assertEquals('Group conversation', $groupconversation->name);
1898 $this->assertEquals('Individual conversation', $individualconversation->name);
1899
1900 $this->assertCount(1, $groupconversation->members);
1901 $this->assertCount(1, $individualconversation->members);
1902
1903 $groupmember = reset($groupconversation->members);
1904 $this->assertNull($groupmember->requirescontact);
1905 $this->assertNull($groupmember->canmessage);
1906 $this->assertEmpty($groupmember->contactrequests);
1907
1908 $individualmember = reset($individualconversation->members);
1909 $this->assertNotNull($individualmember->requirescontact);
1910 $this->assertNotNull($individualmember->canmessage);
1911 $this->assertNotEmpty($individualmember->contactrequests);
1912 }
1913
f512355b 1914 /**
eb5865da 1915 * Test verifying that group linked conversations are returned and contain a subname matching the course name.
f512355b 1916 */
eb5865da 1917 public function test_get_conversations_group_linked() {
18550c0a 1918 global $CFG, $DB;
003cdcce 1919
f512355b
MN
1920 // Create some users.
1921 $user1 = self::getDataGenerator()->create_user();
1922 $user2 = self::getDataGenerator()->create_user();
1923 $user3 = self::getDataGenerator()->create_user();
1924
eb5865da 1925 $course1 = $this->getDataGenerator()->create_course();
f512355b 1926
9cef5491 1927 // Create a group with a linked conversation and a valid image.
eb5865da
JD
1928 $this->setAdminUser();
1929 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1930 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1931 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
003cdcce
JD
1932 $group1 = $this->getDataGenerator()->create_group([
1933 'courseid' => $course1->id,
1934 'enablemessaging' => 1,
1935 'picturepath' => $CFG->dirroot . '/lib/tests/fixtures/gd-logo.png'
1936 ]);
f512355b 1937
eb5865da
JD
1938 // Add users to group1.
1939 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
1940 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
f512355b 1941
9cef5491 1942 // Verify the group with the image works as expected.
f512355b 1943 $conversations = \core_message\api::get_conversations($user1->id);
003cdcce 1944 $this->assertEquals(2, $conversations[0]->membercount);
eb5865da 1945 $this->assertEquals($course1->shortname, $conversations[0]->subname);
003cdcce
JD
1946 $groupimageurl = get_group_picture_url($group1, $group1->courseid, true);
1947 $this->assertEquals($groupimageurl, $conversations[0]->imageurl);
9cef5491
JD
1948
1949 // Create a group with a linked conversation and without any image.
1950 $group2 = $this->getDataGenerator()->create_group([
1951 'courseid' => $course1->id,
1952 'enablemessaging' => 1,
1953 ]);
1954
1955 // Add users to group2.
1956 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
1957 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user3->id));
1958
1959 // Verify the group without any image works as expected too.
1960 $conversations = \core_message\api::get_conversations($user3->id);
888a467a 1961 // Consider first conversations is self-conversation.
9cef5491
JD
1962 $this->assertEquals(2, $conversations[0]->membercount);
1963 $this->assertEquals($course1->shortname, $conversations[0]->subname);
442f1425 1964 $this->assertEquals('https://www.example.com/moodle/theme/image.php/_s/boost/core/1/g/g1', $conversations[0]->imageurl);
18550c0a
JD
1965
1966 // Now, disable the conversation linked to the group and verify it's no longer returned.
1967 $DB->set_field('message_conversations', 'enabled', 0, ['id' => $conversations[0]->id]);
1968 $conversations = \core_message\api::get_conversations($user3->id);
888a467a 1969 $this->assertCount(1, $conversations);
f512355b
MN
1970 }
1971
e12d9f49
RW
1972 /**
1973 * The data provider for get_conversations_mixed.
1974 *
1975 * This provides sets of data to for testing.
1976 * @return array
1977 */
1978 public function get_conversations_mixed_provider() {
1979 return array(
4d146f1a
RW
1980 'Test that conversations with messages contacts is correctly ordered.' => array(
1981 'users' => array(
1982 'user1',
1983 'user2',
1984 'user3',
1985 ),
1986 'contacts' => array(
1987 ),
1988 'messages' => array(
1989 array(
1990 'from' => 'user1',
1991 'to' => 'user2',
1992 'state' => 'unread',
1993 'subject' => 'S1',
1994 ),
1995 array(
1996 'from' => 'user2',
1997 'to' => 'user1',
1998 'state' => 'unread',
1999 'subject' => 'S2',
2000 ),
2001 array(
2002 'from' => 'user1',
2003 'to' => 'user2',
2004 'state' => 'unread',
2005 'timecreated' => 0,
2006 'subject' => 'S3',
2007 ),
2008 array(
2009 'from' => 'user1',
2010 'to' => 'user3',
2011 'state' => 'read',
2012 'timemodifier' => 1,
2013 'subject' => 'S4',
2014 ),
2015 array(
2016 'from' => 'user3',
2017 'to' => 'user1',
2018 'state' => 'read',
2019 'timemodifier' => 1,
2020 'subject' => 'S5',
2021 ),
2022 array(
2023 'from' => 'user1',
2024 'to' => 'user3',
2025 'state' => 'read',
2026 'timecreated' => 0,
2027 'subject' => 'S6',
2028 ),
2029 ),
2030 'expectations' => array(
2031 'user1' => array(
2032 // User1 has conversed most recently with user3. The most recent message is M5.
2033 array(
2034 'messageposition' => 0,
2035 'with' => 'user3',
74fc14e5 2036 'subject' => '<p>S5</p>',
e12d9f49 2037 'unreadcount' => 0,
4d146f1a
RW
2038 ),
2039 // User1 has also conversed with user2. The most recent message is S2.
2040 array(
2041 'messageposition' => 1,
2042 'with' => 'user2',
74fc14e5 2043 'subject' => '<p>S2</p>',
e12d9f49 2044 'unreadcount' => 1,
4d146f1a
RW
2045 ),
2046 ),
2047 'user2' => array(
2048 // User2 has only conversed with user1. Their most recent shared message was S2.
2049 array(
2050 'messageposition' => 0,
2051 'with' => 'user1',
74fc14e5 2052 'subject' => '<p>S2</p>',
e12d9f49 2053 'unreadcount' => 2,
4d146f1a
RW
2054 ),
2055 ),
2056 'user3' => array(
2057 // User3 has only conversed with user1. Their most recent shared message was S5.
2058 array(
2059 'messageposition' => 0,
2060 'with' => 'user1',
74fc14e5 2061 'subject' => '<p>S5</p>',
e12d9f49 2062 'unreadcount' => 0,
4d146f1a
RW
2063 ),
2064 ),
2065 ),
2066 ),
4d146f1a
RW
2067 'Test conversations with a single user, where some messages are read and some are not.' => array(
2068 'users' => array(
2069 'user1',
2070 'user2',
2071 ),
2072 'contacts' => array(
2073 ),
2074 'messages' => array(
2075 array(
2076 'from' => 'user1',
2077 'to' => 'user2',
2078 'state' => 'read',
2079 'subject' => 'S1',
2080 ),
2081 array(
2082 'from' => 'user2',
2083 'to' => 'user1',
2084 'state' => 'read',
2085 'subject' => 'S2',
2086 ),
2087 array(
2088 'from' => 'user1',
2089 'to' => 'user2',
2090 'state' => 'unread',
2091 'timemodifier' => 1,
2092 'subject' => 'S3',
2093 ),
2094 array(
2095 'from' => 'user1',
2096 'to' => 'user2',
2097 'state' => 'unread',
2098 'timemodifier' => 1,
2099 'subject' => 'S4',
2100 ),
2101 ),
2102 'expectations' => array(
2103 // The most recent message between user1 and user2 was S4.
2104 'user1' => array(
2105 array(
2106 'messageposition' => 0,
2107 'with' => 'user2',
74fc14e5 2108 'subject' => '<p>S4</p>',
e12d9f49 2109 'unreadcount' => 0,
4d146f1a
RW
2110 ),
2111 ),
2112 'user2' => array(
2113 // The most recent message between user1 and user2 was S4.
2114 array(
2115 'messageposition' => 0,
2116 'with' => 'user1',
74fc14e5 2117 'subject' => '<p>S4</p>',
e12d9f49 2118 'unreadcount' => 2,
4d146f1a
RW
2119 ),
2120 ),
2121 ),
2122 ),
2123 'Test conversations with a single user, where some messages are read and some are not, and messages ' .
2124 'are out of order' => array(
2125 // This can happen through a combination of factors including multi-master DB replication with messages
2126 // read somehow (e.g. API).
2127 'users' => array(
2128 'user1',
2129 'user2',
2130 ),
2131 'contacts' => array(
2132 ),
2133 'messages' => array(
2134 array(
2135 'from' => 'user1',
2136 'to' => 'user2',
2137 'state' => 'read',
2138 'subject' => 'S1',
2139 'timemodifier' => 1,
2140 ),
2141 array(
2142 'from' => 'user2',
2143 'to' => 'user1',
2144 'state' => 'read',
2145 'subject' => 'S2',
2146 'timemodifier' => 2,
2147 ),
2148 array(
2149 'from' => 'user1',
2150 'to' => 'user2',
2151 'state' => 'unread',
2152 'subject' => 'S3',
2153 ),
2154 array(
2155 'from' => 'user1',
2156 'to' => 'user2',
2157 'state' => 'unread',
2158 'subject' => 'S4',
2159 ),
2160 ),
2161 'expectations' => array(
2162 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
2163 'user1' => array(
2164 array(
2165 'messageposition' => 0,
2166 'with' => 'user2',
74fc14e5 2167 'subject' => '<p>S2</p>',
e12d9f49 2168 'unreadcount' => 0,
4d146f1a
RW
2169 ),
2170 ),
2171 'user2' => array(
2172 array(
2173 'messageposition' => 0,
2174 'with' => 'user1',
74fc14e5 2175 'subject' => '<p>S2</p>',
e12d9f49
RW
2176 'unreadcount' => 2
2177 ),
2178 ),
2179 ),
2180 ),
2181 'Test unread message count is correct for both users' => array(
2182 'users' => array(
2183 'user1',
2184 'user2',
2185 ),
2186 'contacts' => array(
2187 ),
2188 'messages' => array(
2189 array(
2190 'from' => 'user1',
2191 'to' => 'user2',
2192 'state' => 'read',
2193 'subject' => 'S1',
2194 'timemodifier' => 1,
2195 ),
2196 array(
2197 'from' => 'user2',
2198 'to' => 'user1',
2199 'state' => 'read',
2200 'subject' => 'S2',
2201 'timemodifier' => 2,
2202 ),
2203 array(
2204 'from' => 'user1',
2205 'to' => 'user2',
2206 'state' => 'read',
2207 'subject' => 'S3',
2208 'timemodifier' => 3,
2209 ),
2210 array(
2211 'from' => 'user1',
2212 'to' => 'user2',
2213 'state' => 'read',
2214 'subject' => 'S4',
2215 'timemodifier' => 4,
2216 ),
2217 array(
2218 'from' => 'user1',
2219 'to' => 'user2',
2220 'state' => 'unread',
2221 'subject' => 'S5',
2222 'timemodifier' => 5,
2223 ),
2224 array(
2225 'from' => 'user2',
2226 'to' => 'user1',
2227 'state' => 'unread',
2228 'subject' => 'S6',
2229 'timemodifier' => 6,
2230 ),
2231 array(
2232 'from' => 'user1',
2233 'to' => 'user2',
2234 'state' => 'unread',
2235 'subject' => 'S7',
2236 'timemodifier' => 7,
2237 ),
2238 array(
2239 'from' => 'user1',
2240 'to' => 'user2',
2241 'state' => 'unread',
2242 'subject' => 'S8',
2243 'timemodifier' => 8,
2244 ),
2245 ),
2246 'expectations' => array(
2247 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
2248 'user1' => array(
2249 array(
2250 'messageposition' => 0,
2251 'with' => 'user2',
74fc14e5 2252 'subject' => '<p>S8</p>',
e12d9f49
RW
2253 'unreadcount' => 1,
2254 ),
2255 ),
2256 'user2' => array(
2257 array(
2258 'messageposition' => 0,
2259 'with' => 'user1',
74fc14e5 2260 'subject' => '<p>S8</p>',
e12d9f49 2261 'unreadcount' => 3,
4d146f1a
RW
2262 ),
2263 ),
2264 ),
2265 ),
2266 );
2267 }
2268
510ede56
JD
2269 /**
2270 * Test that creation can't create the same conversation twice for 1:1 conversations.
2271 */
2272 public function test_create_conversation_duplicate_conversations() {
2273 global $DB;
2274 $user1 = $this::getDataGenerator()->create_user();
2275
2276 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF, [$user1->id]);
2277 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF, [$user1->id]);
2278
2279 $convhash = \core_message\helper::get_conversation_hash([$user1->id]);
2280 $countconversations = $DB->count_records('message_conversations', ['convhash' => $convhash]);
2281 $this->assertEquals(1, $countconversations);
2282 $this->assertNotEmpty($conversation = \core_message\api::get_self_conversation($user1->id));
2283 }
2284
4d146f1a
RW
2285 /**
2286 * Test get_conversations with a mixture of messages.
2287 *
2288 * @dataProvider get_conversations_mixed_provider
2289 * @param array $usersdata The list of users to create for this test.
2290 * @param array $messagesdata The list of messages to create.
2291 * @param array $expectations The list of expected outcomes.
2292 */
2293 public function test_get_conversations_mixed($usersdata, $contacts, $messagesdata, $expectations) {
2294 global $DB;
2295
2296 // Create all of the users.
2297 $users = array();
2298 foreach ($usersdata as $username) {
2299 $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
2300 }
2301
2302 foreach ($contacts as $username => $contact) {
2303 foreach ($contact as $contactname => $blocked) {
2304 $record = new stdClass();
2305 $record->userid = $users[$username]->id;
2306 $record->contactid = $users[$contactname]->id;
2307 $record->blocked = $blocked;
2308 $record->id = $DB->insert_record('message_contacts', $record);
2309 }
2310 }
2311
2312 $defaulttimecreated = time();
2313 foreach ($messagesdata as $messagedata) {
2314 $from = $users[$messagedata['from']];
2315 $to = $users[$messagedata['to']];
2316 $subject = $messagedata['subject'];
2317
2318 if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
4d146f1a
RW
2319 $messageid = $this->send_fake_message($from, $to, $subject);
2320 } else {
2321 // If there is no state, or the state is not 'unread', assume the message is read.
4d146f1a
RW
2322 $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN);
2323 }
2324
2325 $updatemessage = new stdClass();
2326 $updatemessage->id = $messageid;
2327 if (isset($messagedata['timecreated'])) {
2328 $updatemessage->timecreated = $messagedata['timecreated'];
2329 } else if (isset($messagedata['timemodifier'])) {
2330 $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier'];
2331 } else {
2332 $updatemessage->timecreated = $defaulttimecreated;
2333 }
e12d9f49 2334
883ce421 2335 $DB->update_record('messages', $updatemessage);
4d146f1a
RW
2336 }
2337
2338 foreach ($expectations as $username => $data) {
2339 // Get the recent conversations for the specified user.
2340 $user = $users[$username];
2341 $conversations = array_values(\core_message\api::get_conversations($user->id));
2342 foreach ($data as $expectation) {
2343 $otheruser = $users[$expectation['with']];
2344 $conversation = $conversations[$expectation['messageposition']];
eb5865da
JD
2345 $this->assertEquals($otheruser->id, $conversation->members[$otheruser->id]->id);
2346 $this->assertEquals($expectation['subject'], $conversation->messages[0]->text);
e12d9f49 2347 $this->assertEquals($expectation['unreadcount'], $conversation->unreadcount);
4d146f1a
RW
2348 }
2349 }
2350 }
2351
1f64514d
MN
2352 /**
2353 * Tests retrieving contacts.
2354 */
2355 public function test_get_contacts() {
2356 // Create some users.
2357 $user1 = self::getDataGenerator()->create_user();
2358
2359 // Set as the user.
2360 $this->setUser($user1);
2361
2362 $user2 = new stdClass();
2363 $user2->firstname = 'User';
2364 $user2->lastname = 'A';
2365 $user2 = self::getDataGenerator()->create_user($user2);
2366
2367 $user3 = new stdClass();
2368 $user3->firstname = 'User';
2369 $user3->lastname = 'B';
2370 $user3 = self::getDataGenerator()->create_user($user3);
2371
2372 $user4 = new stdClass();
2373 $user4->firstname = 'User';
2374 $user4->lastname = 'C';
2375 $user4 = self::getDataGenerator()->create_user($user4);
2376
2377 $user5 = new stdClass();
2378 $user5->firstname = 'User';
2379 $user5->lastname = 'D';
2380 $user5 = self::getDataGenerator()->create_user($user5);
2381
2382 // Add some users as contacts.
f219eac7
MN
2383 \core_message\api::add_contact($user1->id, $user2->id);
2384 \core_message\api::add_contact($user1->id, $user3->id);
2385 \core_message\api::add_contact($user1->id, $user4->id);
1f64514d
MN
2386
2387 // Retrieve the contacts.
2388 $contacts = \core_message\api::get_contacts($user1->id);
2389
2390 // Confirm the data is correct.
2391 $this->assertEquals(3, count($contacts));
f219eac7 2392 usort($contacts, ['static', 'sort_contacts']);
1f64514d
MN
2393
2394 $contact1 = $contacts[0];
2395 $contact2 = $contacts[1];
2396 $contact3 = $contacts[2];
2397
2398 $this->assertEquals($user2->id, $contact1->userid);
2399 $this->assertEmpty($contact1->useridfrom);
2400 $this->assertFalse($contact1->ismessaging);
2401 $this->assertNull($contact1->lastmessage);
2402 $this->assertNull($contact1->messageid);
cb805753 2403 $this->assertNull($contact1->isonline);
1f64514d
MN
2404 $this->assertFalse($contact1->isread);
2405 $this->assertFalse($contact1->isblocked);
2406 $this->assertNull($contact1->unreadcount);
2407
2408 $this->assertEquals($user3->id, $contact2->userid);
2409 $this->assertEmpty($contact2->useridfrom);
2410 $this->assertFalse($contact2->ismessaging);
2411 $this->assertNull($contact2->lastmessage);
2412 $this->assertNull($contact2->messageid);
cb805753 2413 $this->assertNull($contact2->isonline);
1f64514d
MN
2414 $this->assertFalse($contact2->isread);
2415 $this->assertFalse($contact2->isblocked);
2416 $this->assertNull($contact2->unreadcount);
2417
2418 $this->assertEquals($user4->id, $contact3->userid);
2419 $this->assertEmpty($contact3->useridfrom);
2420 $this->assertFalse($contact3->ismessaging);
2421 $this->assertNull($contact3->lastmessage);
2422 $this->assertNull($contact3->messageid);
cb805753 2423 $this->assertNull($contact3->isonline);
1f64514d
MN
2424 $this->assertFalse($contact3->isread);
2425 $this->assertFalse($contact3->isblocked);
2426 $this->assertNull($contact3->unreadcount);
2427 }
2428
715c45aa
MN
2429 /**
2430 * Tests retrieving user contacts.
2431 */
2432 public function test_get_user_contacts() {
2433 // Create some users.
2434 $user1 = self::getDataGenerator()->create_user();
2435
2436 // Set as the user.
2437 $this->setUser($user1);
2438
2439 $user2 = new stdClass();
2440 $user2->firstname = 'User';
2441 $user2->lastname = 'A';
2442 $user2 = self::getDataGenerator()->create_user($user2);
2443
2444 $user3 = new stdClass();
2445 $user3->firstname = 'User';
2446 $user3->lastname = 'B';
2447 $user3 = self::getDataGenerator()->create_user($user3);
2448
2449 $user4 = new stdClass();
2450 $user4->firstname = 'User';
2451 $user4->lastname = 'C';
2452 $user4 = self::getDataGenerator()->create_user($user4);
2453
2454 $user5 = new stdClass();
2455 $user5->firstname = 'User';
2456 $user5->lastname = 'D';
2457 $user5 = self::getDataGenerator()->create_user($user5);
2458
2459 // Add some users as contacts.
2460 \core_message\api::add_contact($user1->id, $user2->id);
2461 \core_message\api::add_contact($user1->id, $user3->id);
2462 \core_message\api::add_contact($user1->id, $user4->id);
2463
2464 // Retrieve the contacts.
2465 $contacts = \core_message\api::get_user_contacts($user1->id);
2466
2467 // Confirm the data is correct.
2468 $this->assertEquals(3, count($contacts));
2469
2470 ksort($contacts);
2471
2472 $contact1 = array_shift($contacts);
2473 $contact2 = array_shift($contacts);
2474 $contact3 = array_shift($contacts);
2475
2476 $this->assertEquals($user2->id, $contact1->id);
2477 $this->assertEquals(fullname($user2), $contact1->fullname);
2478 $this->assertTrue($contact1->iscontact);
2479
2480 $this->assertEquals($user3->id, $contact2->id);
2481 $this->assertEquals(fullname($user3), $contact2->fullname);
2482 $this->assertTrue($contact2->iscontact);
2483
2484 $this->assertEquals($user4->id, $contact3->id);
2485 $this->assertEquals(fullname($user4), $contact3->fullname);
2486 $this->assertTrue($contact3->iscontact);
2487 }
2488
1f64514d
MN
2489 /**
2490 * Tests retrieving messages.
2491 */
2492 public function test_get_messages() {
2493 // Create some users.
2494 $user1 = self::getDataGenerator()->create_user();
2495 $user2 = self::getDataGenerator()->create_user();
2496
2497 // The person doing the search.
2498 $this->setUser($user1);
2499
2500 // Send some messages back and forth.
2501 $time = 1;
2502 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
2503 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
2504 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
2505 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
2506
2507 // Retrieve the messages.
2508 $messages = \core_message\api::get_messages($user1->id, $user2->id);
2509
2510 // Confirm the message data is correct.
2511 $this->assertEquals(4, count($messages));
2512
2513 $message1 = $messages[0];
2514 $message2 = $messages[1];
2515 $message3 = $messages[2];
2516 $message4 = $messages[3];
2517
2518 $this->assertEquals($user1->id, $message1->useridfrom);
2519 $this->assertEquals($user2->id, $message1->useridto);
2520 $this->assertTrue($message1->displayblocktime);
2521 $this->assertContains('Yo!', $message1->text);
2522
2523 $this->assertEquals($user2->id, $message2->useridfrom);
2524 $this->assertEquals($user1->id, $message2->useridto);
2525 $this->assertFalse($message2->displayblocktime);
2526 $this->assertContains('Sup mang?', $message2->text);
2527
2528 $this->assertEquals($user1->id, $message3->useridfrom);
2529 $this->assertEquals($user2->id, $message3->useridto);
2530 $this->assertFalse($message3->displayblocktime);
2531 $this->assertContains('Writing PHPUnit tests!', $message3->text);
2532
2533 $this->assertEquals($user2->id, $message4->useridfrom);
2534 $this->assertEquals($user1->id, $message4->useridto);
2535 $this->assertFalse($message4->displayblocktime);
2536 $this->assertContains('Word.', $message4->text);
2537 }
2538
fb04293b
SA
2539 /**
2540 * Tests retrieving conversation messages.
2541 */
2542 public function test_get_conversation_messages() {
2543 // Create some users.
2544 $user1 = self::getDataGenerator()->create_user();
2545 $user2 = self::getDataGenerator()->create_user();
2546
2547 // Create conversation.
2548 $conversation = \core_message\api::create_conversation(
2549 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
2550 [$user1->id, $user2->id]
2551 );
2552
2553 // The person doing the search.
2554 $this->setUser($user1);
2555
2556 // Send some messages back and forth.
2557 $time = 1;
2558 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
2559 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
2560 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
2561 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 4);
2562
2563 // Retrieve the messages.
2564 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id);
2565
2566 // Confirm the conversation id is correct.
2567 $this->assertEquals($conversation->id, $convmessages['id']);
2568
2569 // Confirm the message data is correct.
2570 $messages = $convmessages['messages'];
2571 $this->assertEquals(4, count($messages));
2572 $message1 = $messages[0];
2573 $message2 = $messages[1];
2574 $message3 = $messages[2];
2575 $message4 = $messages[3];
2576
2577 $this->assertEquals($user1->id, $message1->useridfrom);
2578 $this->assertContains('Yo!', $message1->text);
2579
2580 $this->assertEquals($user2->id, $message2->useridfrom);
2581 $this->assertContains('Sup mang?', $message2->text);
2582
2583 $this->assertEquals($user1->id, $message3->useridfrom);
2584 $this->assertContains('Writing PHPUnit tests!', $message3->text);
2585
2586 $this->assertEquals($user1->id, $message4->useridfrom);
2587 $this->assertContains('Word.', $message4->text);
2588
2589 // Confirm the members data is correct.
2590 $members = $convmessages['members'];
2591 $this->assertEquals(2, count($members));
2592 }
2593
2594 /**
2595 * Tests retrieving group conversation messages.
2596 */
2597 public function test_get_group_conversation_messages() {
2598 // Create some users.
2599 $user1 = self::getDataGenerator()->create_user();
2600 $user2 = self::getDataGenerator()->create_user();
2601 $user3 = self::getDataGenerator()->create_user();
2602 $user4 = self::getDataGenerator()->create_user();
2603
2604 // Create group conversation.
2605 $conversation = \core_message\api::create_conversation(
2606 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2607 [$user1->id, $user2->id, $user3->id, $user4->id]
2608 );
2609
2610 // The person doing the search.
2611 $this->setUser($user1);
2612
2613 // Send some messages back and forth.
2614 $time = 1;
2615 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
2616 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
2617 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
2618 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 4);
2619 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Yeah!', $time + 5);
2620
2621 // Retrieve the messages.
2622 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id);
2623
2624 // Confirm the conversation id is correct.
2625 $this->assertEquals($conversation->id, $convmessages['id']);
2626
2627 // Confirm the message data is correct.
2628 $messages = $convmessages['messages'];
2629 $this->assertEquals(5, count($messages));
2630
2631 $message1 = $messages[0];
2632 $message2 = $messages[1];
2633 $message3 = $messages[2];
2634 $message4 = $messages[3];
2635 $message5 = $messages[4];
2636
2637 $this->assertEquals($user1->id, $message1->useridfrom);
2638 $this->assertContains('Yo!', $message1->text);
2639
2640 $this->assertEquals($user2->id, $message2->useridfrom);
2641 $this->assertContains('Sup mang?', $message2->text);
2642
2643 $this->assertEquals($user3->id, $message3->useridfrom);
2644 $this->assertContains('Writing PHPUnit tests!', $message3->text);
2645
2646 $this->assertEquals($user1->id, $message4->useridfrom);
2647 $this->assertContains('Word.', $message4->text);
2648
2649 $this->assertEquals($user2->id, $message5->useridfrom);
2650 $this->assertContains('Yeah!', $message5->text);
2651
2652 // Confirm the members data is correct.
2653 $members = $convmessages['members'];
2654 $this->assertEquals(3, count($members));
2655 }
2656
b95e4c78
JD
2657 /**
2658 * Test verifying the sorting param for get_conversation_messages is respected().
2659 */
2660 public function test_get_conversation_messages_sorting() {
2661 // Create some users.
2662 $user1 = self::getDataGenerator()->create_user();
2663 $user2 = self::getDataGenerator()->create_user();
2664 $user3 = self::getDataGenerator()->create_user();
2665
2666 // Create conversations - 1 group and 1 individual.
2667 $conversation = \core_message\api::create_conversation(
2668 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
2669 [$user1->id, $user2->id]
2670 );
2671 $conversation2 = \core_message\api::create_conversation(
2672 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2673 [$user1->id, $user2->id, $user3->id]
2674 );
2675
2676 // Send some messages back and forth.
2677 $time = 1;
2678 $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
2679 $m2id = testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
2680 $m3id = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
2681 $m4id = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 4);
2682
2683 $gm1id = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Yo!', $time + 1);
2684 $gm2id = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Sup mang?', $time + 2);
2685 $gm3id = testhelper::send_fake_message_to_conversation($user3, $conversation2->id, 'Writing PHPUnit tests!', $time + 3);
2686 $gm4id = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Word.', $time + 4);
2687
2688 // The person doing the search.
2689 $this->setUser($user1);
2690
2691 // Retrieve the messages using default sort ('timecreated ASC') and verify ordering.
2692 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id);
2693 $messages = $convmessages['messages'];
2694 $this->assertEquals($m1id, $messages[0]->id);
2695 $this->assertEquals($m2id, $messages[1]->id);
2696 $this->assertEquals($m3id, $messages[2]->id);
2697 $this->assertEquals($m4id, $messages[3]->id);
2698
2699 // Retrieve the messages without specifying DESC sort ordering, and verify ordering.
2700 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated DESC');
2701 $messages = $convmessages['messages'];
2702 $this->assertEquals($m1id, $messages[3]->id);
2703 $this->assertEquals($m2id, $messages[2]->id);
2704 $this->assertEquals($m3id, $messages[1]->id);
2705 $this->assertEquals($m4id, $messages[0]->id);
2706
2707 // Retrieve the messages using default sort ('timecreated ASC') and verify ordering.
2708 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation2->id);
2709 $messages = $convmessages['messages'];
2710 $this->assertEquals($gm1id, $messages[0]->id);
2711 $this->assertEquals($gm2id, $messages[1]->id);
2712 $this->assertEquals($gm3id, $messages[2]->id);
2713 $this->assertEquals($gm4id, $messages[3]->id);
2714
2715 // Retrieve the messages without specifying DESC sort ordering, and verify ordering.
2716 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation2->id, 0, 0, 'timecreated DESC');
2717 $messages = $convmessages['messages'];
2718 $this->assertEquals($gm1id, $messages[3]->id);
2719 $this->assertEquals($gm2id, $messages[2]->id);
2720 $this->assertEquals($gm3id, $messages[1]->id);
2721 $this->assertEquals($gm4id, $messages[0]->id);
2722 }
2723
fb04293b
SA
2724 /**
2725 * Test retrieving conversation messages by providing a minimum timecreated value.
2726 */
2727 public function test_get_conversation_messages_time_from_only() {
2728 // Create some users.
2729 $user1 = self::getDataGenerator()->create_user();
2730 $user2 = self::getDataGenerator()->create_user();
2731 $user3 = self::getDataGenerator()->create_user();
2732 $user4 = self::getDataGenerator()->create_user();
2733
2734 // Create group conversation.
2735 $conversation = \core_message\api::create_conversation(
2736 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2737 [$user1->id, $user2->id, $user3->id, $user4->id]
2738 );
2739
2740 // The person doing the search.
2741 $this->setUser($user1);
2742
2743 // Send some messages back and forth.
2744 $time = 1;
2745 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2746 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2747 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2748 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2749
2750 // Retrieve the messages from $time, which should be all of them.
2751 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC', $time);
2752
2753 // Confirm the conversation id is correct.
2754 $this->assertEquals($conversation->id, $convmessages['id']);
2755
2756 // Confirm the message data is correct.
2757 $messages = $convmessages['messages'];
2758 $this->assertEquals(4, count($messages));
2759
2760 $message1 = $messages[0];
2761 $message2 = $messages[1];
2762 $message3 = $messages[2];
2763 $message4 = $messages[3];
2764
2765 $this->assertContains('Message 1', $message1->text);
2766 $this->assertContains('Message 2', $message2->text);
2767 $this->assertContains('Message 3', $message3->text);
2768 $this->assertContains('Message 4', $message4->text);
2769
2770 // Confirm the members data is correct.
2771 $members = $convmessages['members'];
2772 $this->assertEquals(3, count($members));
2773
2774 // Retrieve the messages from $time + 3, which should only be the 2 last messages.
2775 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0,
2776 'timecreated ASC', $time + 3);
2777
2778 // Confirm the conversation id is correct.
2779 $this->assertEquals($conversation->id, $convmessages['id']);
2780
2781 // Confirm the message data is correct.
2782 $messages = $convmessages['messages'];
2783 $this->assertEquals(2, count($messages));
2784
2785 $message1 = $messages[0];
2786 $message2 = $messages[1];
2787
2788 $this->assertContains('Message 3', $message1->text);
2789 $this->assertContains('Message 4', $message2->text);
2790
2791 // Confirm the members data is correct.
2792 $members = $convmessages['members'];
2793 $this->assertEquals(2, count($members));
2794 }
2795
2796 /**
2797 * Test retrieving conversation messages by providing a maximum timecreated value.
2798 */
2799 public function test_get_conversation_messages_time_to_only() {
2800 // Create some users.
2801 $user1 = self::getDataGenerator()->create_user();
2802 $user2 = self::getDataGenerator()->create_user();
2803 $user3 = self::getDataGenerator()->create_user();
2804 $user4 = self::getDataGenerator()->create_user();
2805
2806 // Create group conversation.
2807 $conversation = \core_message\api::create_conversation(
2808 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2809 [$user1->id, $user2->id, $user3->id, $user4->id]
2810 );
2811
2812 // The person doing the search.
2813 $this->setUser($user1);
2814
2815 // Send some messages back and forth.
2816 $time = 1;
2817 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2818 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2819 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2820 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2821
2822 // Retrieve the messages up until $time + 4, which should be all of them.
2823 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC',
2824 0, $time + 4);
2825
2826 // Confirm the conversation id is correct.
2827 $this->assertEquals($conversation->id, $convmessages['id']);
2828
2829 // Confirm the message data is correct.
2830 $messages = $convmessages['messages'];
2831 $this->assertEquals(4, count($messages));
2832
2833 $message1 = $messages[0];
2834 $message2 = $messages[1];
2835 $message3 = $messages[2];
2836 $message4 = $messages[3];
2837
2838 $this->assertContains('Message 1', $message1->text);
2839 $this->assertContains('Message 2', $message2->text);
2840 $this->assertContains('Message 3', $message3->text);
2841 $this->assertContains('Message 4', $message4->text);
2842
2843 // Confirm the members data is correct.
2844 $members = $convmessages['members'];
2845 $this->assertEquals(3, count($members));
2846
2847 // Retrieve the messages up until $time + 2, which should be the first two.
2848 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC',
2849 0, $time + 2);
2850
2851 // Confirm the conversation id is correct.
2852 $this->assertEquals($conversation->id, $convmessages['id']);
2853
2854 // Confirm the message data is correct.
2855 $messages = $convmessages['messages'];
2856 $this->assertEquals(2, count($messages));
2857
2858 $message1 = $messages[0];
2859 $message2 = $messages[1];
2860
2861 $this->assertContains('Message 1', $message1->text);
2862 $this->assertContains('Message 2', $message2->text);
2863
2864 // Confirm the members data is correct.
2865 $members = $convmessages['members'];
2866 $this->assertEquals(2, count($members));
2867 }
2868
2869 /**
2870 * Test retrieving conversation messages by providing a minimum and maximum timecreated value.
2871 */
2872 public function test_get_conversation_messages_time_from_and_to() {
2873 // Create some users.
2874 $user1 = self::getDataGenerator()->create_user();
2875 $user2 = self::getDataGenerator()->create_user();
2876 $user3 = self::getDataGenerator()->create_user();
2877 $user4 = self::getDataGenerator()->create_user();
2878
2879 // Create group conversation.
2880 $conversation = \core_message\api::create_conversation(
2881 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2882 [$user1->id, $user2->id, $user3->id, $user4->id]
2883 );
2884
2885 // The person doing the search.
2886 $this->setUser($user1);
2887
2888 // Send some messages back and forth.
2889 $time = 1;
2890 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2891 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2892 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2893 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2894
2895 // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
2896 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0,
2897 'timecreated ASC', $time + 2, $time + 3);
2898
2899 // Confirm the conversation id is correct.
2900 $this->assertEquals($conversation->id, $convmessages['id']);
2901
2902 // Confirm the message data is correct.
2903 $messages = $convmessages['messages'];
2904 $this->assertEquals(2, count($messages));
2905
2906 $message1 = $messages[0];
2907 $message2 = $messages[1];
2908
2909 $this->assertContains('Message 2', $message1->text);
2910 $this->assertContains('Message 3', $message2->text);
2911
2912 // Confirm the members data is correct.
2913 $members = $convmessages['members'];
2914 $this->assertEquals(2, count($members));
2915 }
2916
2917
2918 /**
2919 * Test retrieving conversation messages by providing a limitfrom value.
2920 */
2921 public function test_get_conversation_messages_limitfrom_only() {
2922 // Create some users.
2923 $user1 = self::getDataGenerator()->create_user();
2924 $user2 = self::getDataGenerator()->create_user();
2925 $user3 = self::getDataGenerator()->create_user();
2926 $user4 = self::getDataGenerator()->create_user();
2927
2928 // Create group conversation.
2929 $conversation = \core_message\api::create_conversation(
2930 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2931 [$user1->id, $user2->id, $user3->id, $user4->id]
2932 );
2933
2934 // The person doing the search.
2935 $this->setUser($user1);
2936
2937 // Send some messages back and forth.
2938 $time = 1;
2939 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2940 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2941 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2942 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2943
2944 // Retrieve the messages from $time, which should be all of them.
2945 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 2);
2946
2947 // Confirm the conversation id is correct.
2948 $messages = $convmessages['messages'];
2949 $this->assertEquals($conversation->id, $convmessages['id']);
2950
2951 // Confirm the message data is correct.
2952 $this->assertEquals(2, count($messages));
2953
2954 $message1 = $messages[0];
2955 $message2 = $messages[1];
2956
2957 $this->assertContains('Message 3', $message1->text);
2958 $this->assertContains('Message 4', $message2->text);
2959
2960 // Confirm the members data is correct.
2961 $members = $convmessages['members'];
2962 $this->assertEquals(2, count($members));
2963 }
2964
2965 /**
2966 * Test retrieving conversation messages by providing a limitnum value.
2967 */
2968 public function test_get_conversation_messages_limitnum() {
2969 // Create some users.
2970 $user1 = self::getDataGenerator()->create_user();
2971 $user2 = self::getDataGenerator()->create_user();
2972 $user3 = self::getDataGenerator()->create_user();
2973 $user4 = self::getDataGenerator()->create_user();
2974
2975 // Create group conversation.
2976 $conversation = \core_message\api::create_conversation(
2977 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2978 [$user1->id, $user2->id, $user3->id, $user4->id]
2979 );
2980
2981 // The person doing the search.
2982 $this->setUser($user1);
2983
2984 // Send some messages back and forth.
2985 $time = 1;
2986 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2987 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2988 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2989 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2990
2991 // Retrieve the messages from $time, which should be all of them.
2992 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 2, 1);
2993
2994 // Confirm the conversation id is correct.
2995 $messages = $convmessages['messages'];
2996 $this->assertEquals($conversation->id, $convmessages['id']);
2997
2998 // Confirm the message data is correct.
2999 $messages = $convmessages['messages'];
3000 $this->assertEquals(1, count($messages));
3001
3002 $message1 = $messages[0];
3003
3004 $this->assertContains('Message 3', $message1->text);
3005
3006 // Confirm the members data is correct.
3007 $members = $convmessages['members'];
3008 $this->assertEquals(1, count($members));
3009 }
3010
1f64514d
MN
3011 /**
3012 * Tests retrieving most recent message.
3013 */
3014 public function test_get_most_recent_message() {
3015 // Create some users.
3016 $user1 = self::getDataGenerator()->create_user();
3017 $user2 = self::getDataGenerator()->create_user();
3018
3019 // The person doing the search.
3020 $this->setUser($user1);
3021
3022 // Send some messages back and forth.
3023 $time = 1;
3024 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
3025 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
3026 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
3027 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
3028
3029 // Retrieve the most recent messages.
3030 $message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
3031
3032 // Check the results are correct.
3033 $this->assertEquals($user2->id, $message->useridfrom);
3034 $this->assertEquals($user1->id, $message->useridto);
3035 $this->assertContains('Word.', $message->text);
3036 }
3037
fb04293b
SA
3038 /**
3039 * Tests retrieving most recent conversation message.
3040 */
3041 public function test_get_most_recent_conversation_message() {
3042 // Create some users.
3043 $user1 = self::getDataGenerator()->create_user();
3044 $user2 = self::getDataGenerator()->create_user();
3045 $user3 = self::getDataGenerator()->create_user();
3046
3047 // Create group conversation.
3048 $conversation = \core_message\api::create_conversation(
3049 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
3050 [$user1->id, $user2->id, $user3->id]
3051 );
3052
3053 // The person getting the most recent conversation message.
3054 $this->setUser($user1);
3055
3056 // Send some messages back and forth.
3057 $time = 1;
3058 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
3059 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
3060 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
3061 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Word.', $time + 4);
3062
3063 // Retrieve the most recent messages.
3064 $message = \core_message\api::get_most_recent_conversation_message($conversation->id, $user1->id);
3065
3066 // Check the results are correct.
3067 $this->assertEquals($user2->id, $message->useridfrom);
3068 $this->assertContains('Word.', $message->text);
3069 }
3070
1f64514d
MN
3071 /**
3072 * Tests retrieving a user's profile.
3073 */
3074 public function test_get_profile() {
3075 // Create some users.
3076 $user1 = self::getDataGenerator()->create_user();
3077
3078 $user2 = new stdClass();
3079 $user2->country = 'AU';
3080 $user2->city = 'Perth';
3081 $user2 = self::getDataGenerator()->create_user($user2);
3082
3083 // The person doing the search.
3084 $this->setUser($user1);
3085
3086 // Get the profile.
3087 $profile = \core_message\api::get_profile($user1->id, $user2->id);
3088
3089 $this->assertEquals($user2->id, $profile->userid);
3090 $this->assertEmpty($profile->email);
3091 $this->assertEmpty($profile->country);
3092 $this->assertEmpty($profile->city);
3093 $this->assertEquals(fullname($user2), $profile->fullname);
cb805753 3094 $this->assertNull($profile->isonline);
1f64514d
MN
3095 $this->assertFalse($profile->isblocked);
3096 $this->assertFalse($profile->iscontact);
3097 }
3098
3099 /**
3100 * Tests retrieving a user's profile.
3101 */
3102 public function test_get_profile_as_admin() {
3103 // The person doing the search.
3104 $this->setAdminUser();
3105
3106 // Create some users.
3107 $user1 = self::getDataGenerator()->create_user();
3108
3109 $user2 = new stdClass();
3110 $user2->country = 'AU';
3111 $user2->city = 'Perth';
3112 $user2 = self::getDataGenerator()->create_user($user2);
3113
3114 // Get the profile.
3115 $profile = \core_message\api::get_profile($user1->id, $user2->id);
3116
3117 $this->assertEquals($user2->id, $profile->userid);
3118 $this->assertEquals($user2->email, $profile->email);
3119 $this->assertEquals($user2->country, $profile->country);
3120 $this->assertEquals($user2->city, $profile->city);
3121 $this->assertEquals(fullname($user2), $profile->fullname);
3122 $this->assertFalse($profile->isonline);
3123 $this->assertFalse($profile->isblocked);
3124 $this->assertFalse($profile->iscontact);
3125 }
3126
abf7a261
MN
3127 /**
3128 * Tests checking if a user can mark all messages as read.
3129 */
3130 public function test_can_mark_all_messages_as_read() {
3131 // Set as the admin.
3132 $this->setAdminUser();
3133
3134 // Create some users.
3135 $user1 = self::getDataGenerator()->create_user();
3136 $user2 = self::getDataGenerator()->create_user();
3137 $user3 = self::getDataGenerator()->create_user();
3138
3139 // Send some messages back and forth.
3140 $time = 1;
3141 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
3142 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
3143 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
3144 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
3145
3146 $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
3147
3148 // The admin can do anything.
3149 $this->assertTrue(\core_message\api::can_mark_all_messages_as_read($user1->id, $conversationid));
3150
3151 // Set as the user 1.
3152 $this->setUser($user1);
3153
3154 // The user can mark the messages as he is in the conversation.
3155 $this->assertTrue(\core_message\api::can_mark_all_messages_as_read($user1->id, $conversationid));
3156
3157 // User 1 can not mark the messages read for user 2.
3158 $this->assertFalse(\core_message\api::can_mark_all_messages_as_read($user2->id, $conversationid));
3159
3160 // This user is not a part of the conversation.
3161 $this->assertFalse(\core_message\api::can_mark_all_messages_as_read($user3->id, $conversationid));
3162 }
3163
1f64514d
MN
3164 /**
3165 * Tests checking if a user can delete a conversation.
3166 */
3167 public function test_can_delete_conversation() {
3168 // Set as the admin.
3169 $this->setAdminUser();
3170
3171 // Create some users.
3172 $user1 = self::getDataGenerator()->create_user();
3173 $user2 = self::getDataGenerator()->create_user();
3174
15663b0b
MN
3175 // Send some messages back and forth.
3176 $time = 1;
3177 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
3178 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
3179 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
3180 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
3181
3182 $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
3183
1f64514d 3184 // The admin can do anything.
15663b0b 3185 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id, $conversationid));
1f64514d
MN
3186
3187 // Set as the user 1.
3188 $this->setUser($user1);
3189
3190 // They can delete their own messages.
15663b0b 3191 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id, $conversationid));
1f64514d
MN
3192
3193 // They can't delete someone elses.
15663b0b 3194 $this->assertFalse(\core_message\api::can_delete_conversation($user2->id, $conversationid));
1f64514d
MN
3195 }
3196
3197 /**
3198 * Tests deleting a conversation.
3199 */
3200 public function test_delete_conversation() {
3201 global $DB;
3202
3203 // Create some users.
3204 $user1 = self::getDataGenerator()->create_user();
3205 $user2 = self::getDataGenerator()->create_user();
3206
3207 // The person doing the search.
3208 $this->setUser($user1);
3209
3210 // Send some messages back and forth.
3211 $time = 1;
883ce421
MN
3212 $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
3213 $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
3214 $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
3215 $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1f64514d
MN
3216
3217 // Delete the conversation as user 1.
3218 \core_message\api::delete_conversation($user1->id, $user2->id);
263ad984
MN
3219 $this->assertDebuggingCalled();
3220
3221 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
3222 $this->assertCount(4, $muas);
3223 // Sort by id.
3224 ksort($muas);
3225
3226 $mua1 = array_shift($muas);
3227 $mua2 = array_shift($muas);
3228 $mua3 = array_shift($muas);
3229 $mua4 = array_shift($muas);
3230
3231 $this->assertEquals($user1->id, $mua1->userid);
3232 $this->assertEquals($m1id, $mua1->messageid);
3233 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
3234
3235 $this->assertEquals($user1->id, $mua2->userid);
3236 $this->assertEquals($m2id, $mua2->messageid);
3237 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
3238
3239 $this->assertEquals($user1->id, $mua3->userid);
3240 $this->assertEquals($m3id, $mua3->messageid);
3241 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
3242
3243 $this->assertEquals($user1->id, $mua4->userid);
3244 $this->assertEquals($m4id, $mua4->messageid);
3245 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
3246 }
3247
3248 /**
3249 * Tests deleting a conversation by conversation id.
3250 */
3251 public function test_delete_conversation_by_id() {
3252 global $DB;
3253
3254 // Create some users.
3255 $user1 = self::getDataGenerator()->create_user();
3256 $user2 = self::getDataGenerator()->create_user();
3257
3258 // The person doing the search.
3259 $this->setUser($user1);
3260
888a467a
AA
3261 // Get self-conversation.
3262 $sc1 = \core_message\api::get_self_conversation($user1->id);
3263 $sc2 = \core_message\api::get_self_conversation($user2->id);
734b198f 3264
263ad984
MN
3265 // Send some messages back and forth.
3266 $time = 1;
3267 $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
3268 $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
3269 $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
3270 $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
734b198f
SA
3271 $m5id = testhelper::send_fake_message_to_conversation($user1, $sc1->id, 'Hi to myself!', $time + 5);
3272 $m6id = testhelper::send_fake_message_to_conversation($user2, $sc2->id, 'I am talking with myself', $time + 6);
263ad984 3273
263ad984 3274 $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
734b198f
SA
3275
3276 // Delete the individual conversation between user1 and user2 (only for user1).
263ad984 3277 \core_message\api::delete_conversation_by_id($user1->id, $conversationid);
1f64514d 3278
883ce421
MN
3279 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
3280 $this->assertCount(4, $muas);
3281 // Sort by id.
3282 ksort($muas);
1f64514d 3283
883ce421
MN
3284 $mua1 = array_shift($muas);
3285 $mua2 = array_shift($muas);
3286 $mua3 = array_shift($muas);
3287 $mua4 = array_shift($muas);
1f64514d 3288
883ce421
MN
3289 $this->assertEquals($user1->id, $mua1->userid);
3290 $this->assertEquals($m1id, $mua1->messageid);
3291 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
1f64514d 3292
883ce421
MN
3293 $this->assertEquals($user1->id, $mua2->userid);
3294 $this->assertEquals($m2id, $mua2->messageid);
3295 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
1f64514d 3296
883ce421
MN
3297 $this->assertEquals($user1->id, $mua3->userid);
3298 $this->assertEquals($m3id, $mua3->messageid);
3299 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
1f64514d 3300
883ce421
MN
3301 $this->assertEquals($user1->id, $mua4->userid);
3302 $this->assertEquals($m4id, $mua4->messageid);
3303 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
734b198f
SA
3304
3305 // Delete the self-conversation as user 1.
3306 \core_message\api::delete_conversation_by_id($user1->id, $sc1->id);
3307
3308 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
3309 $this->assertCount(5, $muas);
3310
3311 // Sort by id.
3312 ksort($muas);
3313
3314 $mua1 = array_shift($muas);
3315 $mua2 = array_shift($muas);
3316 $mua3 = array_shift($muas);
3317 $mua4 = array_shift($muas);
3318 $mua5 = array_shift($muas);
3319
3320 // Check only messages in self-conversion for user1 are deleted (self-conversation for user2 shouldn't be removed).
3321 $this->assertEquals($user1->id, $mua5->userid);
3322 $this->assertEquals($m5id, $mua5->messageid);
3323 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua5->action);
1f64514d
MN
3324 }
3325
3326 /**
3327 * Tests counting unread conversations.
3328 */
3329 public function test_count_unread_conversations() {
3330 $this->resetAfterTest(true);
3331
3332 // Create some users.
3333 $user1 = self::getDataGenerator()->create_user();
3334 $user2 = self::getDataGenerator()->create_user();
3335 $user3 = self::getDataGenerator()->create_user();
3336 $user4 = self::getDataGenerator()->create_user();
3337
3338 // The person wanting the conversation count.
3339 $this->setUser($user1);
3340
3341 // Send some messages back and forth, have some different conversations with different users.
3342 $this->send_fake_message($user1, $user2, 'Yo!');
3343 $this->send_fake_message($user2, $user1, 'Sup mang?');
3344 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
3345 $this->send_fake_message($user2, $user1, 'Word.');
3346
3347 $this->send_fake_message($user1, $user3, 'Booyah');
3348 $this->send_fake_message($user3, $user1, 'Whaaat?');
3349 $this->send_fake_message($user1, $user3, 'Nothing.');
3350 $this->send_fake_message($user3, $user1, 'Cool.');
3351
3352 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
3353 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
3354 $this->send_fake_message($user1, $user4, 'Dope.');
3355
3356 // Check the amount for the current user.
3357 $this->assertEquals(3, core_message\api::count_unread_conversations());
3358
3359 // Check the amount for the second user.
3360 $this->assertEquals(1, core_message\api::count_unread_conversations($user2));
3361 }
3362
7c8ba89d
MN
3363 /**
3364 * Tests counting unread conversations where one conversation is disabled.
3365 */
3366 public function test_count_unread_conversations_disabled() {
3367 $this->resetAfterTest(true);
3368
3369 // Create some users.
3370 $user1 = self::getDataGenerator()->create_user();
3371 $user2 = self::getDataGenerator()->create_user();
3372 $user3 = self::getDataGenerator()->create_user();
3373 $user4 = self::getDataGenerator()->create_user();
3374
3375 // The person wanting the conversation count.
3376 $this->setUser($user1);
3377
3378 // Send some messages back and forth, have some different conversations with different users.
3379 $this->send_fake_message($user1, $user2, 'Yo!');
3380 $this->send_fake_message($user2, $user1, 'Sup mang?');
3381 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
3382 $this->send_fake_message($user2, $user1, 'Word.');
3383
3384 $this->send_fake_message($user1, $user3, 'Booyah');
3385 $this->send_fake_message($user3, $user1, 'Whaaat?');
3386 $this->send_fake_message($user1, $user3, 'Nothing.');
3387 $this->send_fake_message($user3, $user1, 'Cool.');
3388
3389 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
3390 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
3391 $this->send_fake_message($user1, $user4, 'Dope.');
3392
3393 // Let's disable the last conversation.
3394 $conversationid = core_message\api::get_conversation_between_users([$user1->id, $user4->id]);
3395 core_message\api::disable_conversation($conversationid);
3396
3397 // Check that the disabled conversation was not included.
3398 $this->assertEquals(2, core_message\api::count_unread_conversations());
3399 }
3400
1f64514d
MN
3401 /**
3402 * Tests deleting a conversation.
3403 */
3404 public function test_get_all_message_preferences() {
3405 $user = self::getDataGenerator()->create_user();
3406 $this->setUser($user);
3407
3408 // Set a couple of preferences to test.
3409 set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
3410 set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
3411
3412 $processors = get_message_processors();
3413 $providers = message_get_providers_for_user($user->id);
3414 $prefs = \core_message\api::get_all_message_preferences($processors, $providers, $user);
3415
3416 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedin['popup']);
3417 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']);
3418 }
3419
3420 /**
3421 * Tests the user can post a message.
3422 */
3423 public function test_can_post_message() {
3424 // Create some users.
3425 $user1 = self::getDataGenerator()->create_user();
3426 $user2 = self::getDataGenerator()->create_user();
3427
f7dfa9ba 3428 // Set as the first user.
1f64514d
MN
3429 $this->setUser($user1);
3430
f7dfa9ba
SA
3431 // With the default privacy setting, users can't message them.
3432 $this->assertFalse(\core_message\api::can_post_message($user2));
3433
3434 // Enrol users to the same course.
3435 $course = $this->getDataGenerator()->create_course();
3436 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
3437 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
3438 // After enrolling users to the course, they should be able to message them with the default privacy setting.
1f64514d
MN
3439 $this->assertTrue(\core_message\api::can_post_message($user2));
3440 }
3441
3442 /**
3443 * Tests the user can't post a message without proper capability.
3444 */
7983fb83 3445 public function test_can_post_message_without_sendmessage_cap() {
1f64514d
MN
3446 global $DB;
3447
3448 // Create some users.
3449 $user1 = self::getDataGenerator()->create_user();
3450 $user2 = self::getDataGenerator()->create_user();
3451
3452 // Set as the user 1.
3453 $this->setUser($user1);
3454
3455 // Remove the capability to send a message.
3456 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
3457 unassign_capability('moodle/site:sendmessage', $roleids['user'],
3458 context_system::instance());
3459
3460 // Check that we can not post a message without the capability.
3461 $this->assertFalse(\core_message\api::can_post_message($user2));
3462 }
3463
f7dfa9ba
SA
3464 /**
3465 * Tests the user can post a message when they are contact.
3466 */
3467 public function test_can_post_message_when_contact() {
3468 // Create some users.
3469 $user1 = self::getDataGenerator()->create_user();
3470 $user2 = self::getDataGenerator()->create_user();
3471
3472 // Set as the first user.
3473 $this->setUser($user1);
3474
3475 // Check that we can not send user2 a message.
3476 $this->assertFalse(\core_message\api::can_post_message($user2));
3477
3478 // Add users as contacts.
3479 \core_message\api::add_contact($user1->id, $user2->id);
3480
3481 // Check that the return result is now true.
3482 $this->assertTrue(\core_message\api::can_post_message($user2));
3483 }
3484
1f64514d
MN
3485 /**
3486 * Tests the user can't post a message if they are not a contact and the user
3487 * has requested messages only from contacts.
3488 */
3489 public function test_can_post_message_when_not_contact() {
3490 // Create some users.
3491 $user1 = self::getDataGenerator()->create_user();
3492 $user2 = self::getDataGenerator()->create_user();
3493
3494 // Set as the first user.
3495 $this->setUser($user1);
3496
3497 // Set the second user's preference to not receive messages from non-contacts.
f7dfa9ba 3498 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1f64514d
MN
3499
3500 // Check that we can not send user 2 a message.
3501 $this->assertFalse(\core_message\api::can_post_message($user2));
3502 }
3503
3504 /**
3505 * Tests the user can't post a message if they are blocked.
3506 */
3507 public function test_can_post_message_when_blocked() {
3508 // Create some users.
3509 $user1 = self::getDataGenerator()->create_user();
3510 $user2 = self::getDataGenerator()->create_user();
3511
3512 // Set the user.
3513 $this->setUser($user1);
3514
3515 // Block the second user.