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