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