MDL-63850 core_message: helper now returns privacy information
[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);
1092 }
1093 $this->assertObjectHasAttribute('messages', $conv);
1094 foreach ($conv->messages as $message) {
1095 $this->assertObjectHasAttribute('id', $message);
1096 $this->assertObjectHasAttribute('useridfrom', $message);
1097 $this->assertObjectHasAttribute('text', $message);
1098 $this->assertObjectHasAttribute('timecreated', $message);
1099 }
1100 }
1101 }
1f64514d 1102
74fc14e5
JD
1103 /**
1104 * Test verifying that html format messages are supported, and that message_format_message_text() is being called appropriately.
1105 */
1106 public function test_get_conversations_message_format() {
1107 global $DB;
1108 // Create some users.
1109 $user1 = self::getDataGenerator()->create_user();
1110 $user2 = self::getDataGenerator()->create_user();
1111
1112 // Create conversation.
1113 $conversation = \core_message\api::create_conversation(
1114 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1115 [$user1->id, $user2->id]
1116 );
1117
1118 // Send some messages back and forth.
1119 $time = 1;
1120 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 1);
1121 $mid = testhelper::send_fake_message_to_conversation($user1, $conversation->id, '<a href="#">A link</a>', $time + 2);
1122
1123 // Verify the format of the html message.
1124 $message = $DB->get_record('messages', ['id' => $mid]);
1125 $expectedmessagetext = message_format_message_text($message);
1126 $conversations = \core_message\api::get_conversations($user1->id);
1127 $messages = $conversations[0]->messages;
1128 $this->assertEquals($expectedmessagetext, $messages[0]->text);
1129 }
1130
eb5865da
JD
1131 /**
1132 * Tests retrieving conversations with a limit and offset to ensure pagination works correctly.
1133 */
1134 public function test_get_conversations_limit_offset() {
1135 // Get a bunch of conversations, some group, some individual and in different states.
1136 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1137 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1f64514d 1138
eb5865da
JD
1139 // Get all conversations for user1, limited to 1 result.
1140 $conversations = core_message\api::get_conversations($user1->id, 0, 1);
1f64514d 1141
eb5865da
JD
1142 // Verify the first conversation.
1143 $this->assertCount(1, $conversations);
1144 $conversation = array_shift($conversations);
1145 $this->assertEquals($conversation->id, $gc3->id);
1f64514d 1146
eb5865da
JD
1147 // Verify the next conversation.
1148 $conversations = \core_message\api::get_conversations($user1->id, 1, 1);
1149 $this->assertCount(1, $conversations);
1150 $this->assertEquals($gc2->id, $conversations[0]->id);
1f64514d 1151
eb5865da
JD
1152 // Verify the next conversation.
1153 $conversations = \core_message\api::get_conversations($user1->id, 2, 1);
1154 $this->assertCount(1, $conversations);
1155 $this->assertEquals($ic2->id, $conversations[0]->id);
1f64514d 1156
eb5865da
JD
1157 // Skip one and get both empty conversations.
1158 $conversations = \core_message\api::get_conversations($user1->id, 4, 2);
1159 $this->assertCount(2, $conversations);
1160 $this->assertEquals($gc5->id, $conversations[0]->id);
1161 $this->assertEmpty($conversations[0]->messages);
1162 $this->assertEquals($gc4->id, $conversations[1]->id);
1163 $this->assertEmpty($conversations[1]->messages);
1f64514d 1164
eb5865da
JD
1165 // Ask for an offset that doesn't exist and verify no conversations are returned.
1166 $conversations = \core_message\api::get_conversations($user1->id, 10, 1);
1167 $this->assertCount(0, $conversations);
1f64514d
MN
1168 }
1169
4d146f1a 1170 /**
eb5865da 1171 * Test verifying the type filtering behaviour of the
4d146f1a 1172 */
eb5865da
JD
1173 public function test_get_conversations_type_filter() {
1174 // Get a bunch of conversations, some group, some individual and in different states.
1175 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1176 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1177
1178 // Verify we can ask for only individual conversations.
1179 $conversations = \core_message\api::get_conversations($user1->id, 0, 20,
1180 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL);
1181 $this->assertCount(2, $conversations);
4d146f1a 1182
eb5865da
JD
1183 // Verify we can ask for only group conversations.
1184 $conversations = \core_message\api::get_conversations($user1->id, 0, 20,
1185 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP);
1186 $this->assertCount(4, $conversations);
4d146f1a 1187
eb5865da
JD
1188 // Verify an exception is thrown if an unrecognized type is specified.
1189 $this->expectException(\moodle_exception::class);
1190 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, 0);
1191 }
4d146f1a 1192
eb5865da
JD
1193 /**
1194 * Tests retrieving conversations when a conversation contains a deleted user.
1195 */
1196 public function test_get_conversations_with_deleted_user() {
1197 // Get a bunch of conversations, some group, some individual and in different states.
1198 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1199 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
4d146f1a 1200
eb5865da
JD
1201 // Delete the second user and retrieve the conversations.
1202 // We should have 5, as $ic1 drops off the list.
1203 // Group conversations remain albeit with less members.
1204 delete_user($user2);
1205 $conversations = \core_message\api::get_conversations($user1->id);
1206 $this->assertCount(5, $conversations);
1207 $this->assertEquals($gc3->id, $conversations[0]->id);
1208 $this->assertcount(1, $conversations[0]->members);
1209 $this->assertEquals($gc2->id, $conversations[1]->id);
1210 $this->assertcount(1, $conversations[1]->members);
1211 $this->assertEquals($ic2->id, $conversations[2]->id);
1212 $this->assertEquals($gc5->id, $conversations[3]->id);
1213 $this->assertEquals($gc4->id, $conversations[4]->id);
1214
1215 // Delete a user from a group conversation where that user had sent the most recent message.
1216 // This user will still be present in the members array, as will the message in the messages array.
1217 delete_user($user4);
1218 $conversations = \core_message\api::get_conversations($user1->id);
1219 $this->assertCount(5, $conversations);
1220 $this->assertEquals($gc2->id, $conversations[1]->id);
1221 $this->assertcount(1, $conversations[1]->members);
1222 $this->assertEquals($user4->id, $conversations[1]->members[$user4->id]->id);
1223 $this->assertcount(1, $conversations[1]->messages);
1224 $this->assertEquals($user4->id, $conversations[1]->messages[0]->useridfrom);
1225
1226 // Delete the third user and retrieve the conversations.
1227 // We should have 4, as $ic1, $ic2 drop off the list.
1228 // Group conversations remain albeit with less members.
1229 delete_user($user3);
1230 $conversations = \core_message\api::get_conversations($user1->id);
1231 $this->assertCount(4, $conversations);
1232 $this->assertEquals($gc3->id, $conversations[0]->id);
1233 $this->assertcount(1, $conversations[0]->members);
1234 $this->assertEquals($gc2->id, $conversations[1]->id);
1235 $this->assertcount(1, $conversations[1]->members);
1236 $this->assertEquals($gc5->id, $conversations[2]->id);
1237 $this->assertEquals($gc4->id, $conversations[3]->id);
1238 }
4d146f1a 1239
aa748351
JD
1240 /**
1241 * Test confirming the behaviour of get_conversations() when users delete all messages.
1242 */
1243 public function test_get_conversations_deleted_messages() {
1244 // Get a bunch of conversations, some group, some individual and in different states.
1245 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1246 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
1247
1248 $conversations = \core_message\api::get_conversations($user1->id);
1249 $this->assertCount(6, $conversations);
1250
1251 // Delete all messages from a group conversation the user is in - it should be returned.
1252 $this->assertTrue(\core_message\api::is_user_in_conversation($user1->id, $gc2->id));
1253 $convmessages = \core_message\api::get_conversation_messages($user1->id, $gc2->id);
1254 $messages = $convmessages['messages'];
1255 foreach ($messages as $message) {
1256 \core_message\api::delete_message($user1->id, $message->id);
1257 }
1258 $conversations = \core_message\api::get_conversations($user1->id);
1259 $this->assertCount(6, $conversations);
1260 $this->assertContains($gc2->id, array_column($conversations, 'id'));
1261
1262 // Delete all messages from an individual conversation the user is in - it should not be returned.
1263 $this->assertTrue(\core_message\api::is_user_in_conversation($user1->id, $ic1->id));
1264 $convmessages = \core_message\api::get_conversation_messages($user1->id, $ic1->id);
1265 $messages = $convmessages['messages'];
1266 foreach ($messages as $message) {
1267 \core_message\api::delete_message($user1->id, $message->id);
1268 }
1269 $conversations = \core_message\api::get_conversations($user1->id);
1270 $this->assertCount(5, $conversations);
1271 $this->assertNotContains($ic1->id, array_column($conversations, 'id'));
1272 }
1273
eb5865da
JD
1274 /**
1275 * Test verifying the behaviour of get_conversations() when fetching favourite conversations.
1276 */
1277 public function test_get_conversations_favourite_conversations() {
1278 // Get a bunch of conversations, some group, some individual and in different states.
1279 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
1280 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
4d146f1a 1281
eb5865da
JD
1282 // Try to get ONLY favourite conversations, when no favourites exist.
1283 $this->assertEquals([], \core_message\api::get_conversations($user1->id, 0, 20, null, true));
4d146f1a 1284
eb5865da
JD
1285 // Try to get NO favourite conversations, when no favourites exist.
1286 $this->assertCount(6, \core_message\api::get_conversations($user1->id, 0, 20, null, false));
4d146f1a 1287
eb5865da
JD
1288 // Mark a few conversations as favourites.
1289 \core_message\api::set_favourite_conversation($ic1->id, $user1->id);
1290 \core_message\api::set_favourite_conversation($gc2->id, $user1->id);
1291 \core_message\api::set_favourite_conversation($gc5->id, $user1->id);
4d146f1a 1292
eb5865da
JD
1293 // Get the conversations, first with no restrictions, confirming the favourite status of the conversations.
1294 $conversations = \core_message\api::get_conversations($user1->id);
1295 $this->assertCount(6, $conversations);
1296 foreach ($conversations as $conv) {
1297 if (in_array($conv->id, [$ic1->id, $gc2->id, $gc5->id])) {
1298 $this->assertTrue($conv->isfavourite);
1299 }
1300 }
4d146f1a 1301
eb5865da
JD
1302 // Now, get ONLY favourite conversations.
1303 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, null, true);
1304 $this->assertCount(3, $conversations);
1305 foreach ($conversations as $conv) {
1306 $this->assertTrue($conv->isfavourite);
1307 }
4d146f1a 1308
eb5865da
JD
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->assertCount(2, $conversations);
1313 foreach ($conversations as $conv) {
1314 $this->assertTrue($conv->isfavourite);
1315 }
4d146f1a 1316
eb5865da
JD
1317 // And NO favourite conversations.
1318 $conversations = \core_message\api::get_conversations($user1->id, 0, 20, null, false);
1319 $this->assertCount(3, $conversations);
1320 foreach ($conversations as $conv) {
1321 $this->assertFalse($conv->isfavourite);
1322 }
4d146f1a
RW
1323 }
1324
f512355b 1325 /**
eb5865da 1326 * Test verifying that group linked conversations are returned and contain a subname matching the course name.
f512355b 1327 */
eb5865da 1328 public function test_get_conversations_group_linked() {
003cdcce
JD
1329 global $CFG;
1330
f512355b
MN
1331 // Create some users.
1332 $user1 = self::getDataGenerator()->create_user();
1333 $user2 = self::getDataGenerator()->create_user();
1334 $user3 = self::getDataGenerator()->create_user();
1335
eb5865da 1336 $course1 = $this->getDataGenerator()->create_course();
f512355b 1337
eb5865da
JD
1338 // Create a group with a linked conversation.
1339 $this->setAdminUser();
1340 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1341 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1342 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
003cdcce
JD
1343 $group1 = $this->getDataGenerator()->create_group([
1344 'courseid' => $course1->id,
1345 'enablemessaging' => 1,
1346 'picturepath' => $CFG->dirroot . '/lib/tests/fixtures/gd-logo.png'
1347 ]);
f512355b 1348
eb5865da
JD
1349 // Add users to group1.
1350 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
1351 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
f512355b 1352
f512355b 1353 $conversations = \core_message\api::get_conversations($user1->id);
003cdcce 1354 $this->assertEquals(2, $conversations[0]->membercount);
eb5865da 1355 $this->assertEquals($course1->shortname, $conversations[0]->subname);
003cdcce
JD
1356 $groupimageurl = get_group_picture_url($group1, $group1->courseid, true);
1357 $this->assertEquals($groupimageurl, $conversations[0]->imageurl);
f512355b
MN
1358 }
1359
e12d9f49
RW
1360 /**
1361 * The data provider for get_conversations_mixed.
1362 *
1363 * This provides sets of data to for testing.
1364 * @return array
1365 */
1366 public function get_conversations_mixed_provider() {
1367 return array(
4d146f1a
RW
1368 'Test that conversations with messages contacts is correctly ordered.' => array(
1369 'users' => array(
1370 'user1',
1371 'user2',
1372 'user3',
1373 ),
1374 'contacts' => array(
1375 ),
1376 'messages' => array(
1377 array(
1378 'from' => 'user1',
1379 'to' => 'user2',
1380 'state' => 'unread',
1381 'subject' => 'S1',
1382 ),
1383 array(
1384 'from' => 'user2',
1385 'to' => 'user1',
1386 'state' => 'unread',
1387 'subject' => 'S2',
1388 ),
1389 array(
1390 'from' => 'user1',
1391 'to' => 'user2',
1392 'state' => 'unread',
1393 'timecreated' => 0,
1394 'subject' => 'S3',
1395 ),
1396 array(
1397 'from' => 'user1',
1398 'to' => 'user3',
1399 'state' => 'read',
1400 'timemodifier' => 1,
1401 'subject' => 'S4',
1402 ),
1403 array(
1404 'from' => 'user3',
1405 'to' => 'user1',
1406 'state' => 'read',
1407 'timemodifier' => 1,
1408 'subject' => 'S5',
1409 ),
1410 array(
1411 'from' => 'user1',
1412 'to' => 'user3',
1413 'state' => 'read',
1414 'timecreated' => 0,
1415 'subject' => 'S6',
1416 ),
1417 ),
1418 'expectations' => array(
1419 'user1' => array(
1420 // User1 has conversed most recently with user3. The most recent message is M5.
1421 array(
1422 'messageposition' => 0,
1423 'with' => 'user3',
74fc14e5 1424 'subject' => '<p>S5</p>',
e12d9f49 1425 'unreadcount' => 0,
4d146f1a
RW
1426 ),
1427 // User1 has also conversed with user2. The most recent message is S2.
1428 array(
1429 'messageposition' => 1,
1430 'with' => 'user2',
74fc14e5 1431 'subject' => '<p>S2</p>',
e12d9f49 1432 'unreadcount' => 1,
4d146f1a
RW
1433 ),
1434 ),
1435 'user2' => array(
1436 // User2 has only conversed with user1. Their most recent shared message was S2.
1437 array(
1438 'messageposition' => 0,
1439 'with' => 'user1',
74fc14e5 1440 'subject' => '<p>S2</p>',
e12d9f49 1441 'unreadcount' => 2,
4d146f1a
RW
1442 ),
1443 ),
1444 'user3' => array(
1445 // User3 has only conversed with user1. Their most recent shared message was S5.
1446 array(
1447 'messageposition' => 0,
1448 'with' => 'user1',
74fc14e5 1449 'subject' => '<p>S5</p>',
e12d9f49 1450 'unreadcount' => 0,
4d146f1a
RW
1451 ),
1452 ),
1453 ),
1454 ),
4d146f1a
RW
1455 'Test conversations with a single user, where some messages are read and some are not.' => array(
1456 'users' => array(
1457 'user1',
1458 'user2',
1459 ),
1460 'contacts' => array(
1461 ),
1462 'messages' => array(
1463 array(
1464 'from' => 'user1',
1465 'to' => 'user2',
1466 'state' => 'read',
1467 'subject' => 'S1',
1468 ),
1469 array(
1470 'from' => 'user2',
1471 'to' => 'user1',
1472 'state' => 'read',
1473 'subject' => 'S2',
1474 ),
1475 array(
1476 'from' => 'user1',
1477 'to' => 'user2',
1478 'state' => 'unread',
1479 'timemodifier' => 1,
1480 'subject' => 'S3',
1481 ),
1482 array(
1483 'from' => 'user1',
1484 'to' => 'user2',
1485 'state' => 'unread',
1486 'timemodifier' => 1,
1487 'subject' => 'S4',
1488 ),
1489 ),
1490 'expectations' => array(
1491 // The most recent message between user1 and user2 was S4.
1492 'user1' => array(
1493 array(
1494 'messageposition' => 0,
1495 'with' => 'user2',
74fc14e5 1496 'subject' => '<p>S4</p>',
e12d9f49 1497 'unreadcount' => 0,
4d146f1a
RW
1498 ),
1499 ),
1500 'user2' => array(
1501 // The most recent message between user1 and user2 was S4.
1502 array(
1503 'messageposition' => 0,
1504 'with' => 'user1',
74fc14e5 1505 'subject' => '<p>S4</p>',
e12d9f49 1506 'unreadcount' => 2,
4d146f1a
RW
1507 ),
1508 ),
1509 ),
1510 ),
1511 'Test conversations with a single user, where some messages are read and some are not, and messages ' .
1512 'are out of order' => array(
1513 // This can happen through a combination of factors including multi-master DB replication with messages
1514 // read somehow (e.g. API).
1515 'users' => array(
1516 'user1',
1517 'user2',
1518 ),
1519 'contacts' => array(
1520 ),
1521 'messages' => array(
1522 array(
1523 'from' => 'user1',
1524 'to' => 'user2',
1525 'state' => 'read',
1526 'subject' => 'S1',
1527 'timemodifier' => 1,
1528 ),
1529 array(
1530 'from' => 'user2',
1531 'to' => 'user1',
1532 'state' => 'read',
1533 'subject' => 'S2',
1534 'timemodifier' => 2,
1535 ),
1536 array(
1537 'from' => 'user1',
1538 'to' => 'user2',
1539 'state' => 'unread',
1540 'subject' => 'S3',
1541 ),
1542 array(
1543 'from' => 'user1',
1544 'to' => 'user2',
1545 'state' => 'unread',
1546 'subject' => 'S4',
1547 ),
1548 ),
1549 'expectations' => array(
1550 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
1551 'user1' => array(
1552 array(
1553 'messageposition' => 0,
1554 'with' => 'user2',
74fc14e5 1555 'subject' => '<p>S2</p>',
e12d9f49 1556 'unreadcount' => 0,
4d146f1a
RW
1557 ),
1558 ),
1559 'user2' => array(
1560 array(
1561 'messageposition' => 0,
1562 'with' => 'user1',
74fc14e5 1563 'subject' => '<p>S2</p>',
e12d9f49
RW
1564 'unreadcount' => 2
1565 ),
1566 ),
1567 ),
1568 ),
1569 'Test unread message count is correct for both users' => array(
1570 'users' => array(
1571 'user1',
1572 'user2',
1573 ),
1574 'contacts' => array(
1575 ),
1576 'messages' => array(
1577 array(
1578 'from' => 'user1',
1579 'to' => 'user2',
1580 'state' => 'read',
1581 'subject' => 'S1',
1582 'timemodifier' => 1,
1583 ),
1584 array(
1585 'from' => 'user2',
1586 'to' => 'user1',
1587 'state' => 'read',
1588 'subject' => 'S2',
1589 'timemodifier' => 2,
1590 ),
1591 array(
1592 'from' => 'user1',
1593 'to' => 'user2',
1594 'state' => 'read',
1595 'subject' => 'S3',
1596 'timemodifier' => 3,
1597 ),
1598 array(
1599 'from' => 'user1',
1600 'to' => 'user2',
1601 'state' => 'read',
1602 'subject' => 'S4',
1603 'timemodifier' => 4,
1604 ),
1605 array(
1606 'from' => 'user1',
1607 'to' => 'user2',
1608 'state' => 'unread',
1609 'subject' => 'S5',
1610 'timemodifier' => 5,
1611 ),
1612 array(
1613 'from' => 'user2',
1614 'to' => 'user1',
1615 'state' => 'unread',
1616 'subject' => 'S6',
1617 'timemodifier' => 6,
1618 ),
1619 array(
1620 'from' => 'user1',
1621 'to' => 'user2',
1622 'state' => 'unread',
1623 'subject' => 'S7',
1624 'timemodifier' => 7,
1625 ),
1626 array(
1627 'from' => 'user1',
1628 'to' => 'user2',
1629 'state' => 'unread',
1630 'subject' => 'S8',
1631 'timemodifier' => 8,
1632 ),
1633 ),
1634 'expectations' => array(
1635 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
1636 'user1' => array(
1637 array(
1638 'messageposition' => 0,
1639 'with' => 'user2',
74fc14e5 1640 'subject' => '<p>S8</p>',
e12d9f49
RW
1641 'unreadcount' => 1,
1642 ),
1643 ),
1644 'user2' => array(
1645 array(
1646 'messageposition' => 0,
1647 'with' => 'user1',
74fc14e5 1648 'subject' => '<p>S8</p>',
e12d9f49 1649 'unreadcount' => 3,
4d146f1a
RW
1650 ),
1651 ),
1652 ),
1653 ),
1654 );
1655 }
1656
1657 /**
1658 * Test get_conversations with a mixture of messages.
1659 *
1660 * @dataProvider get_conversations_mixed_provider
1661 * @param array $usersdata The list of users to create for this test.
1662 * @param array $messagesdata The list of messages to create.
1663 * @param array $expectations The list of expected outcomes.
1664 */
1665 public function test_get_conversations_mixed($usersdata, $contacts, $messagesdata, $expectations) {
1666 global $DB;
1667
1668 // Create all of the users.
1669 $users = array();
1670 foreach ($usersdata as $username) {
1671 $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
1672 }
1673
1674 foreach ($contacts as $username => $contact) {
1675 foreach ($contact as $contactname => $blocked) {
1676 $record = new stdClass();
1677 $record->userid = $users[$username]->id;
1678 $record->contactid = $users[$contactname]->id;
1679 $record->blocked = $blocked;
1680 $record->id = $DB->insert_record('message_contacts', $record);
1681 }
1682 }
1683
1684 $defaulttimecreated = time();
1685 foreach ($messagesdata as $messagedata) {
1686 $from = $users[$messagedata['from']];
1687 $to = $users[$messagedata['to']];
1688 $subject = $messagedata['subject'];
1689
1690 if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
4d146f1a
RW
1691 $messageid = $this->send_fake_message($from, $to, $subject);
1692 } else {
1693 // If there is no state, or the state is not 'unread', assume the message is read.
4d146f1a
RW
1694 $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN);
1695 }
1696
1697 $updatemessage = new stdClass();
1698 $updatemessage->id = $messageid;
1699 if (isset($messagedata['timecreated'])) {
1700 $updatemessage->timecreated = $messagedata['timecreated'];
1701 } else if (isset($messagedata['timemodifier'])) {
1702 $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier'];
1703 } else {
1704 $updatemessage->timecreated = $defaulttimecreated;
1705 }
e12d9f49 1706
883ce421 1707 $DB->update_record('messages', $updatemessage);
4d146f1a
RW
1708 }
1709
1710 foreach ($expectations as $username => $data) {
1711 // Get the recent conversations for the specified user.
1712 $user = $users[$username];
1713 $conversations = array_values(\core_message\api::get_conversations($user->id));
1714 foreach ($data as $expectation) {
1715 $otheruser = $users[$expectation['with']];
1716 $conversation = $conversations[$expectation['messageposition']];
eb5865da
JD
1717 $this->assertEquals($otheruser->id, $conversation->members[$otheruser->id]->id);
1718 $this->assertEquals($expectation['subject'], $conversation->messages[0]->text);
e12d9f49 1719 $this->assertEquals($expectation['unreadcount'], $conversation->unreadcount);
4d146f1a
RW
1720 }
1721 }
1722 }
1723
1f64514d
MN
1724 /**
1725 * Tests retrieving contacts.
1726 */
1727 public function test_get_contacts() {
1728 // Create some users.
1729 $user1 = self::getDataGenerator()->create_user();
1730
1731 // Set as the user.
1732 $this->setUser($user1);
1733
1734 $user2 = new stdClass();
1735 $user2->firstname = 'User';
1736 $user2->lastname = 'A';
1737 $user2 = self::getDataGenerator()->create_user($user2);
1738
1739 $user3 = new stdClass();
1740 $user3->firstname = 'User';
1741 $user3->lastname = 'B';
1742 $user3 = self::getDataGenerator()->create_user($user3);
1743
1744 $user4 = new stdClass();
1745 $user4->firstname = 'User';
1746 $user4->lastname = 'C';
1747 $user4 = self::getDataGenerator()->create_user($user4);
1748
1749 $user5 = new stdClass();
1750 $user5->firstname = 'User';
1751 $user5->lastname = 'D';
1752 $user5 = self::getDataGenerator()->create_user($user5);
1753
1754 // Add some users as contacts.
f219eac7
MN
1755 \core_message\api::add_contact($user1->id, $user2->id);
1756 \core_message\api::add_contact($user1->id, $user3->id);
1757 \core_message\api::add_contact($user1->id, $user4->id);
1f64514d
MN
1758
1759 // Retrieve the contacts.
1760 $contacts = \core_message\api::get_contacts($user1->id);
1761
1762 // Confirm the data is correct.
1763 $this->assertEquals(3, count($contacts));
f219eac7 1764 usort($contacts, ['static', 'sort_contacts']);
1f64514d
MN
1765
1766 $contact1 = $contacts[0];
1767 $contact2 = $contacts[1];
1768 $contact3 = $contacts[2];
1769
1770 $this->assertEquals($user2->id, $contact1->userid);
1771 $this->assertEmpty($contact1->useridfrom);
1772 $this->assertFalse($contact1->ismessaging);
1773 $this->assertNull($contact1->lastmessage);
1774 $this->assertNull($contact1->messageid);
cb805753 1775 $this->assertNull($contact1->isonline);
1f64514d
MN
1776 $this->assertFalse($contact1->isread);
1777 $this->assertFalse($contact1->isblocked);
1778 $this->assertNull($contact1->unreadcount);
1779
1780 $this->assertEquals($user3->id, $contact2->userid);
1781 $this->assertEmpty($contact2->useridfrom);
1782 $this->assertFalse($contact2->ismessaging);
1783 $this->assertNull($contact2->lastmessage);
1784 $this->assertNull($contact2->messageid);
cb805753 1785 $this->assertNull($contact2->isonline);
1f64514d
MN
1786 $this->assertFalse($contact2->isread);
1787 $this->assertFalse($contact2->isblocked);
1788 $this->assertNull($contact2->unreadcount);
1789
1790 $this->assertEquals($user4->id, $contact3->userid);
1791 $this->assertEmpty($contact3->useridfrom);
1792 $this->assertFalse($contact3->ismessaging);
1793 $this->assertNull($contact3->lastmessage);
1794 $this->assertNull($contact3->messageid);
cb805753 1795 $this->assertNull($contact3->isonline);
1f64514d
MN
1796 $this->assertFalse($contact3->isread);
1797 $this->assertFalse($contact3->isblocked);
1798 $this->assertNull($contact3->unreadcount);
1799 }
1800
1801 /**
1802 * Tests retrieving messages.
1803 */
1804 public function test_get_messages() {
1805 // Create some users.
1806 $user1 = self::getDataGenerator()->create_user();
1807 $user2 = self::getDataGenerator()->create_user();
1808
1809 // The person doing the search.
1810 $this->setUser($user1);
1811
1812 // Send some messages back and forth.
1813 $time = 1;
1814 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1815 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1816 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1817 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1818
1819 // Retrieve the messages.
1820 $messages = \core_message\api::get_messages($user1->id, $user2->id);
d1e8e69d 1821 $this->assertDebuggingCalledCount(3);
1f64514d
MN
1822
1823 // Confirm the message data is correct.
1824 $this->assertEquals(4, count($messages));
1825
1826 $message1 = $messages[0];
1827 $message2 = $messages[1];
1828 $message3 = $messages[2];
1829 $message4 = $messages[3];
1830
1831 $this->assertEquals($user1->id, $message1->useridfrom);
1832 $this->assertEquals($user2->id, $message1->useridto);
1833 $this->assertTrue($message1->displayblocktime);
1834 $this->assertContains('Yo!', $message1->text);
1835
1836 $this->assertEquals($user2->id, $message2->useridfrom);
1837 $this->assertEquals($user1->id, $message2->useridto);
1838 $this->assertFalse($message2->displayblocktime);
1839 $this->assertContains('Sup mang?', $message2->text);
1840
1841 $this->assertEquals($user1->id, $message3->useridfrom);
1842 $this->assertEquals($user2->id, $message3->useridto);
1843 $this->assertFalse($message3->displayblocktime);
1844 $this->assertContains('Writing PHPUnit tests!', $message3->text);
1845
1846 $this->assertEquals($user2->id, $message4->useridfrom);
1847 $this->assertEquals($user1->id, $message4->useridto);
1848 $this->assertFalse($message4->displayblocktime);
1849 $this->assertContains('Word.', $message4->text);
1850 }
1851
fb04293b
SA
1852 /**
1853 * Tests retrieving conversation messages.
1854 */
1855 public function test_get_conversation_messages() {
1856 // Create some users.
1857 $user1 = self::getDataGenerator()->create_user();
1858 $user2 = self::getDataGenerator()->create_user();
1859
1860 // Create conversation.
1861 $conversation = \core_message\api::create_conversation(
1862 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1863 [$user1->id, $user2->id]
1864 );
1865
1866 // The person doing the search.
1867 $this->setUser($user1);
1868
1869 // Send some messages back and forth.
1870 $time = 1;
1871 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
1872 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
1873 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
1874 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 4);
1875
1876 // Retrieve the messages.
1877 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id);
1878
1879 // Confirm the conversation id is correct.
1880 $this->assertEquals($conversation->id, $convmessages['id']);
1881
1882 // Confirm the message data is correct.
1883 $messages = $convmessages['messages'];
1884 $this->assertEquals(4, count($messages));
1885 $message1 = $messages[0];
1886 $message2 = $messages[1];
1887 $message3 = $messages[2];
1888 $message4 = $messages[3];
1889
1890 $this->assertEquals($user1->id, $message1->useridfrom);
1891 $this->assertContains('Yo!', $message1->text);
1892
1893 $this->assertEquals($user2->id, $message2->useridfrom);
1894 $this->assertContains('Sup mang?', $message2->text);
1895
1896 $this->assertEquals($user1->id, $message3->useridfrom);
1897 $this->assertContains('Writing PHPUnit tests!', $message3->text);
1898
1899 $this->assertEquals($user1->id, $message4->useridfrom);
1900 $this->assertContains('Word.', $message4->text);
1901
1902 // Confirm the members data is correct.
1903 $members = $convmessages['members'];
1904 $this->assertEquals(2, count($members));
1905 }
1906
1907 /**
1908 * Tests retrieving group conversation messages.
1909 */
1910 public function test_get_group_conversation_messages() {
1911 // Create some users.
1912 $user1 = self::getDataGenerator()->create_user();
1913 $user2 = self::getDataGenerator()->create_user();
1914 $user3 = self::getDataGenerator()->create_user();
1915 $user4 = self::getDataGenerator()->create_user();
1916
1917 // Create group conversation.
1918 $conversation = \core_message\api::create_conversation(
1919 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1920 [$user1->id, $user2->id, $user3->id, $user4->id]
1921 );
1922
1923 // The person doing the search.
1924 $this->setUser($user1);
1925
1926 // Send some messages back and forth.
1927 $time = 1;
1928 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
1929 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
1930 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
1931 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 4);
1932 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Yeah!', $time + 5);
1933
1934 // Retrieve the messages.
1935 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id);
1936
1937 // Confirm the conversation id is correct.
1938 $this->assertEquals($conversation->id, $convmessages['id']);
1939
1940 // Confirm the message data is correct.
1941 $messages = $convmessages['messages'];
1942 $this->assertEquals(5, count($messages));
1943
1944 $message1 = $messages[0];
1945 $message2 = $messages[1];
1946 $message3 = $messages[2];
1947 $message4 = $messages[3];
1948 $message5 = $messages[4];
1949
1950 $this->assertEquals($user1->id, $message1->useridfrom);
1951 $this->assertContains('Yo!', $message1->text);
1952
1953 $this->assertEquals($user2->id, $message2->useridfrom);
1954 $this->assertContains('Sup mang?', $message2->text);
1955
1956 $this->assertEquals($user3->id, $message3->useridfrom);
1957 $this->assertContains('Writing PHPUnit tests!', $message3->text);
1958
1959 $this->assertEquals($user1->id, $message4->useridfrom);
1960 $this->assertContains('Word.', $message4->text);
1961
1962 $this->assertEquals($user2->id, $message5->useridfrom);
1963 $this->assertContains('Yeah!', $message5->text);
1964
1965 // Confirm the members data is correct.
1966 $members = $convmessages['members'];
1967 $this->assertEquals(3, count($members));
1968 }
1969
b95e4c78
JD
1970 /**
1971 * Test verifying the sorting param for get_conversation_messages is respected().
1972 */
1973 public function test_get_conversation_messages_sorting() {
1974 // Create some users.
1975 $user1 = self::getDataGenerator()->create_user();
1976 $user2 = self::getDataGenerator()->create_user();
1977 $user3 = self::getDataGenerator()->create_user();
1978
1979 // Create conversations - 1 group and 1 individual.
1980 $conversation = \core_message\api::create_conversation(
1981 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1982 [$user1->id, $user2->id]
1983 );
1984 $conversation2 = \core_message\api::create_conversation(
1985 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
1986 [$user1->id, $user2->id, $user3->id]
1987 );
1988
1989 // Send some messages back and forth.
1990 $time = 1;
1991 $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
1992 $m2id = testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
1993 $m3id = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
1994 $m4id = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 4);
1995
1996 $gm1id = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Yo!', $time + 1);
1997 $gm2id = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Sup mang?', $time + 2);
1998 $gm3id = testhelper::send_fake_message_to_conversation($user3, $conversation2->id, 'Writing PHPUnit tests!', $time + 3);
1999 $gm4id = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Word.', $time + 4);
2000
2001 // The person doing the search.
2002 $this->setUser($user1);
2003
2004 // Retrieve the messages using default sort ('timecreated ASC') and verify ordering.
2005 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id);
2006 $messages = $convmessages['messages'];
2007 $this->assertEquals($m1id, $messages[0]->id);
2008 $this->assertEquals($m2id, $messages[1]->id);
2009 $this->assertEquals($m3id, $messages[2]->id);
2010 $this->assertEquals($m4id, $messages[3]->id);
2011
2012 // Retrieve the messages without specifying DESC sort ordering, and verify ordering.
2013 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated DESC');
2014 $messages = $convmessages['messages'];
2015 $this->assertEquals($m1id, $messages[3]->id);
2016 $this->assertEquals($m2id, $messages[2]->id);
2017 $this->assertEquals($m3id, $messages[1]->id);
2018 $this->assertEquals($m4id, $messages[0]->id);
2019
2020 // Retrieve the messages using default sort ('timecreated ASC') and verify ordering.
2021 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation2->id);
2022 $messages = $convmessages['messages'];
2023 $this->assertEquals($gm1id, $messages[0]->id);
2024 $this->assertEquals($gm2id, $messages[1]->id);
2025 $this->assertEquals($gm3id, $messages[2]->id);
2026 $this->assertEquals($gm4id, $messages[3]->id);
2027
2028 // Retrieve the messages without specifying DESC sort ordering, and verify ordering.
2029 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation2->id, 0, 0, 'timecreated DESC');
2030 $messages = $convmessages['messages'];
2031 $this->assertEquals($gm1id, $messages[3]->id);
2032 $this->assertEquals($gm2id, $messages[2]->id);
2033 $this->assertEquals($gm3id, $messages[1]->id);
2034 $this->assertEquals($gm4id, $messages[0]->id);
2035 }
2036
fb04293b
SA
2037 /**
2038 * Test retrieving conversation messages by providing a minimum timecreated value.
2039 */
2040 public function test_get_conversation_messages_time_from_only() {
2041 // Create some users.
2042 $user1 = self::getDataGenerator()->create_user();
2043 $user2 = self::getDataGenerator()->create_user();
2044 $user3 = self::getDataGenerator()->create_user();
2045 $user4 = self::getDataGenerator()->create_user();
2046
2047 // Create group conversation.
2048 $conversation = \core_message\api::create_conversation(
2049 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2050 [$user1->id, $user2->id, $user3->id, $user4->id]
2051 );
2052
2053 // The person doing the search.
2054 $this->setUser($user1);
2055
2056 // Send some messages back and forth.
2057 $time = 1;
2058 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2059 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2060 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2061 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2062
2063 // Retrieve the messages from $time, which should be all of them.
2064 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC', $time);
2065
2066 // Confirm the conversation id is correct.
2067 $this->assertEquals($conversation->id, $convmessages['id']);
2068
2069 // Confirm the message data is correct.
2070 $messages = $convmessages['messages'];
2071 $this->assertEquals(4, count($messages));
2072
2073 $message1 = $messages[0];
2074 $message2 = $messages[1];
2075 $message3 = $messages[2];
2076 $message4 = $messages[3];
2077
2078 $this->assertContains('Message 1', $message1->text);
2079 $this->assertContains('Message 2', $message2->text);
2080 $this->assertContains('Message 3', $message3->text);
2081 $this->assertContains('Message 4', $message4->text);
2082
2083 // Confirm the members data is correct.
2084 $members = $convmessages['members'];
2085 $this->assertEquals(3, count($members));
2086
2087 // Retrieve the messages from $time + 3, which should only be the 2 last messages.
2088 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0,
2089 'timecreated ASC', $time + 3);
2090
2091 // Confirm the conversation id is correct.
2092 $this->assertEquals($conversation->id, $convmessages['id']);
2093
2094 // Confirm the message data is correct.
2095 $messages = $convmessages['messages'];
2096 $this->assertEquals(2, count($messages));
2097
2098 $message1 = $messages[0];
2099 $message2 = $messages[1];
2100
2101 $this->assertContains('Message 3', $message1->text);
2102 $this->assertContains('Message 4', $message2->text);
2103
2104 // Confirm the members data is correct.
2105 $members = $convmessages['members'];
2106 $this->assertEquals(2, count($members));
2107 }
2108
2109 /**
2110 * Test retrieving conversation messages by providing a maximum timecreated value.
2111 */
2112 public function test_get_conversation_messages_time_to_only() {
2113 // Create some users.
2114 $user1 = self::getDataGenerator()->create_user();
2115 $user2 = self::getDataGenerator()->create_user();
2116 $user3 = self::getDataGenerator()->create_user();
2117 $user4 = self::getDataGenerator()->create_user();
2118
2119 // Create group conversation.
2120 $conversation = \core_message\api::create_conversation(
2121 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2122 [$user1->id, $user2->id, $user3->id, $user4->id]
2123 );
2124
2125 // The person doing the search.
2126 $this->setUser($user1);
2127
2128 // Send some messages back and forth.
2129 $time = 1;
2130 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2131 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2132 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2133 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2134
2135 // Retrieve the messages up until $time + 4, which should be all of them.
2136 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC',
2137 0, $time + 4);
2138
2139 // Confirm the conversation id is correct.
2140 $this->assertEquals($conversation->id, $convmessages['id']);
2141
2142 // Confirm the message data is correct.
2143 $messages = $convmessages['messages'];
2144 $this->assertEquals(4, count($messages));
2145
2146 $message1 = $messages[0];
2147 $message2 = $messages[1];
2148 $message3 = $messages[2];
2149 $message4 = $messages[3];
2150
2151 $this->assertContains('Message 1', $message1->text);
2152 $this->assertContains('Message 2', $message2->text);
2153 $this->assertContains('Message 3', $message3->text);
2154 $this->assertContains('Message 4', $message4->text);
2155
2156 // Confirm the members data is correct.
2157 $members = $convmessages['members'];
2158 $this->assertEquals(3, count($members));
2159
2160 // Retrieve the messages up until $time + 2, which should be the first two.
2161 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC',
2162 0, $time + 2);
2163
2164 // Confirm the conversation id is correct.
2165 $this->assertEquals($conversation->id, $convmessages['id']);
2166
2167 // Confirm the message data is correct.
2168 $messages = $convmessages['messages'];
2169 $this->assertEquals(2, count($messages));
2170
2171 $message1 = $messages[0];
2172 $message2 = $messages[1];
2173
2174 $this->assertContains('Message 1', $message1->text);
2175 $this->assertContains('Message 2', $message2->text);
2176
2177 // Confirm the members data is correct.
2178 $members = $convmessages['members'];
2179 $this->assertEquals(2, count($members));
2180 }
2181
2182 /**
2183 * Test retrieving conversation messages by providing a minimum and maximum timecreated value.
2184 */
2185 public function test_get_conversation_messages_time_from_and_to() {
2186 // Create some users.
2187 $user1 = self::getDataGenerator()->create_user();
2188 $user2 = self::getDataGenerator()->create_user();
2189 $user3 = self::getDataGenerator()->create_user();
2190 $user4 = self::getDataGenerator()->create_user();
2191
2192 // Create group conversation.
2193 $conversation = \core_message\api::create_conversation(
2194 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2195 [$user1->id, $user2->id, $user3->id, $user4->id]
2196 );
2197
2198 // The person doing the search.
2199 $this->setUser($user1);
2200
2201 // Send some messages back and forth.
2202 $time = 1;
2203 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2204 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2205 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2206 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2207
2208 // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
2209 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0,
2210 'timecreated ASC', $time + 2, $time + 3);
2211
2212 // Confirm the conversation id is correct.
2213 $this->assertEquals($conversation->id, $convmessages['id']);
2214
2215 // Confirm the message data is correct.
2216 $messages = $convmessages['messages'];
2217 $this->assertEquals(2, count($messages));
2218
2219 $message1 = $messages[0];
2220 $message2 = $messages[1];
2221
2222 $this->assertContains('Message 2', $message1->text);
2223 $this->assertContains('Message 3', $message2->text);
2224
2225 // Confirm the members data is correct.
2226 $members = $convmessages['members'];
2227 $this->assertEquals(2, count($members));
2228 }
2229
2230
2231 /**
2232 * Test retrieving conversation messages by providing a limitfrom value.
2233 */
2234 public function test_get_conversation_messages_limitfrom_only() {
2235 // Create some users.
2236 $user1 = self::getDataGenerator()->create_user();
2237 $user2 = self::getDataGenerator()->create_user();
2238 $user3 = self::getDataGenerator()->create_user();
2239 $user4 = self::getDataGenerator()->create_user();
2240
2241 // Create group conversation.
2242 $conversation = \core_message\api::create_conversation(
2243 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2244 [$user1->id, $user2->id, $user3->id, $user4->id]
2245 );
2246
2247 // The person doing the search.
2248 $this->setUser($user1);
2249
2250 // Send some messages back and forth.
2251 $time = 1;
2252 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2253 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2254 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2255 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2256
2257 // Retrieve the messages from $time, which should be all of them.
2258 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 2);
2259
2260 // Confirm the conversation id is correct.
2261 $messages = $convmessages['messages'];
2262 $this->assertEquals($conversation->id, $convmessages['id']);
2263
2264 // Confirm the message data is correct.
2265 $this->assertEquals(2, count($messages));
2266
2267 $message1 = $messages[0];
2268 $message2 = $messages[1];
2269
2270 $this->assertContains('Message 3', $message1->text);
2271 $this->assertContains('Message 4', $message2->text);
2272
2273 // Confirm the members data is correct.
2274 $members = $convmessages['members'];
2275 $this->assertEquals(2, count($members));
2276 }
2277
2278 /**
2279 * Test retrieving conversation messages by providing a limitnum value.
2280 */
2281 public function test_get_conversation_messages_limitnum() {
2282 // Create some users.
2283 $user1 = self::getDataGenerator()->create_user();
2284 $user2 = self::getDataGenerator()->create_user();
2285 $user3 = self::getDataGenerator()->create_user();
2286 $user4 = self::getDataGenerator()->create_user();
2287
2288 // Create group conversation.
2289 $conversation = \core_message\api::create_conversation(
2290 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2291 [$user1->id, $user2->id, $user3->id, $user4->id]
2292 );
2293
2294 // The person doing the search.
2295 $this->setUser($user1);
2296
2297 // Send some messages back and forth.
2298 $time = 1;
2299 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
2300 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
2301 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
2302 testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
2303
2304 // Retrieve the messages from $time, which should be all of them.
2305 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 2, 1);
2306
2307 // Confirm the conversation id is correct.
2308 $messages = $convmessages['messages'];
2309 $this->assertEquals($conversation->id, $convmessages['id']);
2310
2311 // Confirm the message data is correct.
2312 $messages = $convmessages['messages'];
2313 $this->assertEquals(1, count($messages));
2314
2315 $message1 = $messages[0];
2316
2317 $this->assertContains('Message 3', $message1->text);
2318
2319 // Confirm the members data is correct.
2320 $members = $convmessages['members'];
2321 $this->assertEquals(1, count($members));
2322 }
2323
1f64514d
MN
2324 /**
2325 * Tests retrieving most recent message.
2326 */
2327 public function test_get_most_recent_message() {
2328 // Create some users.
2329 $user1 = self::getDataGenerator()->create_user();
2330 $user2 = self::getDataGenerator()->create_user();
2331
2332 // The person doing the search.
2333 $this->setUser($user1);
2334
2335 // Send some messages back and forth.
2336 $time = 1;
2337 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
2338 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
2339 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
2340 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
2341
2342 // Retrieve the most recent messages.
2343 $message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
d1e8e69d 2344 $this->assertDebuggingCalledCount(3);
1f64514d
MN
2345
2346 // Check the results are correct.
2347 $this->assertEquals($user2->id, $message->useridfrom);
2348 $this->assertEquals($user1->id, $message->useridto);
2349 $this->assertContains('Word.', $message->text);
2350 }
2351
fb04293b
SA
2352 /**
2353 * Tests retrieving most recent conversation message.
2354 */
2355 public function test_get_most_recent_conversation_message() {
2356 // Create some users.
2357 $user1 = self::getDataGenerator()->create_user();
2358 $user2 = self::getDataGenerator()->create_user();
2359 $user3 = self::getDataGenerator()->create_user();
2360
2361 // Create group conversation.
2362 $conversation = \core_message\api::create_conversation(
2363 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2364 [$user1->id, $user2->id, $user3->id]
2365 );
2366
2367 // The person getting the most recent conversation message.
2368 $this->setUser($user1);
2369
2370 // Send some messages back and forth.
2371 $time = 1;
2372 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
2373 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
2374 testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
2375 testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Word.', $time + 4);
2376
2377 // Retrieve the most recent messages.
2378 $message = \core_message\api::get_most_recent_conversation_message($conversation->id, $user1->id);
2379
2380 // Check the results are correct.
2381 $this->assertEquals($user2->id, $message->useridfrom);
2382 $this->assertContains('Word.', $message->text);
2383 }
2384
1f64514d
MN
2385 /**
2386 * Tests retrieving a user's profile.
2387 */
2388 public function test_get_profile() {
2389 // Create some users.
2390 $user1 = self::getDataGenerator()->create_user();
2391
2392 $user2 = new stdClass();
2393 $user2->country = 'AU';
2394 $user2->city = 'Perth';
2395 $user2 = self::getDataGenerator()->create_user($user2);
2396
2397 // The person doing the search.
2398 $this->setUser($user1);
2399
2400 // Get the profile.
2401 $profile = \core_message\api::get_profile($user1->id, $user2->id);
2402
2403 $this->assertEquals($user2->id, $profile->userid);
2404 $this->assertEmpty($profile->email);
2405 $this->assertEmpty($profile->country);
2406 $this->assertEmpty($profile->city);
2407 $this->assertEquals(fullname($user2), $profile->fullname);
cb805753 2408 $this->assertNull($profile->isonline);
1f64514d
MN
2409 $this->assertFalse($profile->isblocked);
2410 $this->assertFalse($profile->iscontact);
2411 }
2412
2413 /**
2414 * Tests retrieving a user's profile.
2415 */
2416 public function test_get_profile_as_admin() {
2417 // The person doing the search.
2418 $this->setAdminUser();
2419
2420 // Create some users.
2421 $user1 = self::getDataGenerator()->create_user();
2422
2423 $user2 = new stdClass();
2424 $user2->country = 'AU';
2425 $user2->city = 'Perth';
2426 $user2 = self::getDataGenerator()->create_user($user2);
2427
2428 // Get the profile.
2429 $profile = \core_message\api::get_profile($user1->id, $user2->id);
2430
2431 $this->assertEquals($user2->id, $profile->userid);
2432 $this->assertEquals($user2->email, $profile->email);
2433 $this->assertEquals($user2->country, $profile->country);
2434 $this->assertEquals($user2->city, $profile->city);
2435 $this->assertEquals(fullname($user2), $profile->fullname);
2436 $this->assertFalse($profile->isonline);
2437 $this->assertFalse($profile->isblocked);
2438 $this->assertFalse($profile->iscontact);
2439 }
2440
abf7a261
MN
2441 /**
2442 * Tests checking if a user can mark all messages as read.
2443 */
2444 public function test_can_mark_all_messages_as_read() {
2445 // Set as the admin.
2446 $this->setAdminUser();
2447
2448 // Create some users.
2449 $user1 = self::getDataGenerator()->create_user();
2450 $user2 = self::getDataGenerator()->create_user();
2451 $user3 = self::getDataGenerator()->create_user();
2452
2453 // Send some messages back and forth.
2454 $time = 1;
2455 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
2456 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
2457 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
2458 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
2459
2460 $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2461
2462 // The admin can do anything.
2463 $this->assertTrue(\core_message\api::can_mark_all_messages_as_read($user1->id, $conversationid));
2464
2465 // Set as the user 1.
2466 $this->setUser($user1);
2467
2468 // The user can mark the messages as he is in the conversation.
2469 $this->assertTrue(\core_message\api::can_mark_all_messages_as_read($user1->id, $conversationid));
2470
2471 // User 1 can not mark the messages read for user 2.
2472 $this->assertFalse(\core_message\api::can_mark_all_messages_as_read($user2->id, $conversationid));
2473
2474 // This user is not a part of the conversation.
2475 $this->assertFalse(\core_message\api::can_mark_all_messages_as_read($user3->id, $conversationid));
2476 }
2477
1f64514d
MN
2478 /**
2479 * Tests checking if a user can delete a conversation.
2480 */
2481 public function test_can_delete_conversation() {
2482 // Set as the admin.
2483 $this->setAdminUser();
2484
2485 // Create some users.
2486 $user1 = self::getDataGenerator()->create_user();
2487 $user2 = self::getDataGenerator()->create_user();
2488
15663b0b
MN
2489 // Send some messages back and forth.
2490 $time = 1;
2491 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
2492 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
2493 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
2494 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
2495
2496 $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2497
1f64514d 2498 // The admin can do anything.
15663b0b 2499 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id, $conversationid));
1f64514d
MN
2500
2501 // Set as the user 1.
2502 $this->setUser($user1);
2503
2504 // They can delete their own messages.
15663b0b 2505 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id, $conversationid));
1f64514d
MN
2506
2507 // They can't delete someone elses.
15663b0b 2508 $this->assertFalse(\core_message\api::can_delete_conversation($user2->id, $conversationid));
1f64514d
MN
2509 }
2510
2511 /**
2512 * Tests deleting a conversation.
2513 */
2514 public function test_delete_conversation() {
2515 global $DB;
2516
2517 // Create some users.
2518 $user1 = self::getDataGenerator()->create_user();
2519 $user2 = self::getDataGenerator()->create_user();
2520
2521 // The person doing the search.
2522 $this->setUser($user1);
2523
2524 // Send some messages back and forth.
2525 $time = 1;
883ce421
MN
2526 $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
2527 $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
2528 $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
2529 $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1f64514d
MN
2530
2531 // Delete the conversation as user 1.
2532 \core_message\api::delete_conversation($user1->id, $user2->id);
263ad984
MN
2533 $this->assertDebuggingCalled();
2534
2535 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
2536 $this->assertCount(4, $muas);
2537 // Sort by id.
2538 ksort($muas);
2539
2540 $mua1 = array_shift($muas);
2541 $mua2 = array_shift($muas);
2542 $mua3 = array_shift($muas);
2543 $mua4 = array_shift($muas);
2544
2545 $this->assertEquals($user1->id, $mua1->userid);
2546 $this->assertEquals($m1id, $mua1->messageid);
2547 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
2548
2549 $this->assertEquals($user1->id, $mua2->userid);
2550 $this->assertEquals($m2id, $mua2->messageid);
2551 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
2552
2553 $this->assertEquals($user1->id, $mua3->userid);
2554 $this->assertEquals($m3id, $mua3->messageid);
2555 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
2556
2557 $this->assertEquals($user1->id, $mua4->userid);
2558 $this->assertEquals($m4id, $mua4->messageid);
2559 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
2560 }
2561
2562 /**
2563 * Tests deleting a conversation by conversation id.
2564 */
2565 public function test_delete_conversation_by_id() {
2566 global $DB;
2567
2568 // Create some users.
2569 $user1 = self::getDataGenerator()->create_user();
2570 $user2 = self::getDataGenerator()->create_user();
2571
2572 // The person doing the search.
2573 $this->setUser($user1);
2574
2575 // Send some messages back and forth.
2576 $time = 1;
2577 $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
2578 $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
2579 $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
2580 $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
2581
2582 // Delete the conversation as user 1.
2583 $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2584 \core_message\api::delete_conversation_by_id($user1->id, $conversationid);
1f64514d 2585
883ce421
MN
2586 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
2587 $this->assertCount(4, $muas);
2588 // Sort by id.
2589 ksort($muas);
1f64514d 2590
883ce421
MN
2591 $mua1 = array_shift($muas);
2592 $mua2 = array_shift($muas);
2593 $mua3 = array_shift($muas);
2594 $mua4 = array_shift($muas);
1f64514d 2595
883ce421
MN
2596 $this->assertEquals($user1->id, $mua1->userid);
2597 $this->assertEquals($m1id, $mua1->messageid);
2598 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
1f64514d 2599
883ce421
MN
2600 $this->assertEquals($user1->id, $mua2->userid);
2601 $this->assertEquals($m2id, $mua2->messageid);
2602 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
1f64514d 2603
883ce421
MN
2604 $this->assertEquals($user1->id, $mua3->userid);
2605 $this->assertEquals($m3id, $mua3->messageid);
2606 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
1f64514d 2607
883ce421
MN
2608 $this->assertEquals($user1->id, $mua4->userid);
2609 $this->assertEquals($m4id, $mua4->messageid);
2610 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
1f64514d
MN
2611 }
2612
2613 /**
2614 * Tests counting unread conversations.
2615 */
2616 public function test_count_unread_conversations() {
2617 $this->resetAfterTest(true);
2618
2619 // Create some users.
2620 $user1 = self::getDataGenerator()->create_user();
2621 $user2 = self::getDataGenerator()->create_user();
2622 $user3 = self::getDataGenerator()->create_user();
2623 $user4 = self::getDataGenerator()->create_user();
2624
2625 // The person wanting the conversation count.
2626 $this->setUser($user1);
2627
2628 // Send some messages back and forth, have some different conversations with different users.
2629 $this->send_fake_message($user1, $user2, 'Yo!');
2630 $this->send_fake_message($user2, $user1, 'Sup mang?');
2631 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
2632 $this->send_fake_message($user2, $user1, 'Word.');
2633
2634 $this->send_fake_message($user1, $user3, 'Booyah');
2635 $this->send_fake_message($user3, $user1, 'Whaaat?');
2636 $this->send_fake_message($user1, $user3, 'Nothing.');
2637 $this->send_fake_message($user3, $user1, 'Cool.');
2638
2639 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
2640 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
2641 $this->send_fake_message($user1, $user4, 'Dope.');
2642
2643 // Check the amount for the current user.
2644 $this->assertEquals(3, core_message\api::count_unread_conversations());
2645
2646 // Check the amount for the second user.
2647 $this->assertEquals(1, core_message\api::count_unread_conversations($user2));
2648 }
2649
2650 /**
2651 * Tests deleting a conversation.
2652 */
2653 public function test_get_all_message_preferences() {
2654 $user = self::getDataGenerator()->create_user();
2655 $this->setUser($user);
2656
2657 // Set a couple of preferences to test.
2658 set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
2659 set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
2660
2661 $processors = get_message_processors();
2662 $providers = message_get_providers_for_user($user->id);
2663 $prefs = \core_message\api::get_all_message_preferences($processors, $providers, $user);
2664
2665 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedin['popup']);
2666 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']);
2667 }
2668
2669 /**
2670 * Tests the user can post a message.
2671 */
2672 public function test_can_post_message() {
2673 // Create some users.
2674 $user1 = self::getDataGenerator()->create_user();
2675 $user2 = self::getDataGenerator()->create_user();
2676
f7dfa9ba 2677 // Set as the first user.
1f64514d
MN
2678 $this->setUser($user1);
2679
f7dfa9ba
SA
2680 // With the default privacy setting, users can't message them.
2681 $this->assertFalse(\core_message\api::can_post_message($user2));
2682
2683 // Enrol users to the same course.
2684 $course = $this->getDataGenerator()->create_course();
2685 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
2686 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
2687 // After enrolling users to the course, they should be able to message them with the default privacy setting.
1f64514d
MN
2688 $this->assertTrue(\core_message\api::can_post_message($user2));
2689 }
2690
2691 /**
2692 * Tests the user can't post a message without proper capability.
2693 */
7983fb83 2694 public function test_can_post_message_without_sendmessage_cap() {
1f64514d
MN
2695 global $DB;
2696
2697 // Create some users.
2698 $user1 = self::getDataGenerator()->create_user();
2699 $user2 = self::getDataGenerator()->create_user();
2700
2701 // Set as the user 1.
2702 $this->setUser($user1);
2703
2704 // Remove the capability to send a message.
2705 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
2706 unassign_capability('moodle/site:sendmessage', $roleids['user'],
2707 context_system::instance());
2708
2709 // Check that we can not post a message without the capability.
2710 $this->assertFalse(\core_message\api::can_post_message($user2));
2711 }
2712
f7dfa9ba
SA
2713 /**
2714 * Tests the user can post a message when they are contact.
2715 */
2716 public function test_can_post_message_when_contact() {
2717 // Create some users.
2718 $user1 = self::getDataGenerator()->create_user();
2719 $user2 = self::getDataGenerator()->create_user();
2720
2721 // Set as the first user.
2722 $this->setUser($user1);
2723
2724 // Check that we can not send user2 a message.
2725 $this->assertFalse(\core_message\api::can_post_message($user2));
2726
2727 // Add users as contacts.
2728 \core_message\api::add_contact($user1->id, $user2->id);
2729
2730 // Check that the return result is now true.
2731 $this->assertTrue(\core_message\api::can_post_message($user2));
2732 }
2733
1f64514d
MN
2734 /**
2735 * Tests the user can't post a message if they are not a contact and the user
2736 * has requested messages only from contacts.
2737 */
2738 public function test_can_post_message_when_not_contact() {
2739 // Create some users.
2740 $user1 = self::getDataGenerator()->create_user();
2741 $user2 = self::getDataGenerator()->create_user();
2742
2743 // Set as the first user.
2744 $this->setUser($user1);
2745
2746 // Set the second user's preference to not receive messages from non-contacts.
f7dfa9ba 2747 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1f64514d
MN
2748
2749 // Check that we can not send user 2 a message.
2750 $this->assertFalse(\core_message\api::can_post_message($user2));
2751 }
2752
2753 /**
2754 * Tests the user can't post a message if they are blocked.
2755 */
2756 public function test_can_post_message_when_blocked() {
2757 // Create some users.
2758 $user1 = self::getDataGenerator()->create_user();
2759 $user2 = self::getDataGenerator()->create_user();
2760
2761 // Set the user.
2762 $this->setUser($user1);
2763
2764 // Block the second user.
f219eac7 2765 \core_message\api::block_user($user1->id, $user2->id);
1f64514d
MN
2766
2767 // Check that the second user can no longer send the first user a message.
2768 $this->assertFalse(\core_message\api::can_post_message($user1, $user2));
2769 }
2770
f7dfa9ba
SA
2771 /**
2772 * Tests the user can post a message when site-wide messaging setting is enabled,
2773 * even if they are not a contact and are not members of the same course.
2774 */
2775 public function test_can_post_message_site_messaging_setting() {
2776 // Create some users.
2777 $user1 = self::getDataGenerator()->create_user();
2778 $user2 = self::getDataGenerator()->create_user();
2779
2780 // Set as the first user.
2781 $this->setUser($user1);
2782
7983fb83 2783 // By default, user only can be messaged by contacts and members of any of his/her courses.
f7dfa9ba
SA
2784 $this->assertFalse(\core_message\api::can_post_message($user2));
2785
2786 // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
2787 set_config('messagingallusers', true);
7983fb83
SA
2788
2789 // Set the second user's preference to receive messages from everybody.
2790 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user2->id);
2791
f7dfa9ba
SA
2792 // Check that we can send user2 a message.
2793 $this->assertTrue(\core_message\api::can_post_message($user2));
7983fb83
SA
2794
2795 // Disable site-wide messagging privacy setting. The user will be able to receive messages from contacts
2796 // and members sharing a course with her.
2797 set_config('messagingallusers', false);
2798
2799 // As site-wide messaging setting is disabled, the value for user2 will be changed to MESSAGE_PRIVACY_COURSEMEMBER.
2800 $this->assertFalse(\core_message\api::can_post_message($user2));
2801
2802 // Enrol users to the same course.
2803 $course = $this->getDataGenerator()->create_course();
2804 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
2805 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
2806 // Check that we can send user2 a message because they are sharing a course.
2807 $this->assertTrue(\core_message\api::can_post_message($user2));
2808
2809 // Set the second user's preference to receive messages only from contacts.
2810 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
2811 // Check that now the user2 can't be contacted because user1 is not their contact.
2812 $this->assertFalse(\core_message\api::can_post_message($user2));
2813
2814 // Make contacts user1 and user2.
2815 \core_message\api::add_contact($user2->id, $user1->id);
2816 // Check that we can send user2 a message because they are contacts.
2817 $this->assertTrue(\core_message\api::can_post_message($user2));
2818 }
2819
2820 /**
2821 * Tests the user with the messageanyuser capability can post a message.
2822 */
2823 public function test_can_post_message_with_messageanyuser_cap() {
2824 global $DB;
2825
2826 // Create some users.
2827 $teacher1 = self::getDataGenerator()->create_user();
2828 $student1 = self::getDataGenerator()->create_user();
2829 $student2 = self::getDataGenerator()->create_user();
2830
2831 // Create users not enrolled in any course.
2832 $user1 = self::getDataGenerator()->create_user();
2833
2834 // Create a course.
2835 $course1 = $this->getDataGenerator()->create_course();
2836
2837 // Enrol the users in the course.
2838 $this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, 'editingteacher');
2839 $this->getDataGenerator()->enrol_user($student1->id, $course1->id, 'student');
2840 $this->getDataGenerator()->enrol_user($student2->id, $course1->id, 'student');
2841
2842 // Set some student preferences to not receive messages from non-contacts.
2843 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $student1->id);
2844
2845 // Check that we can send student1 a message because teacher has the messageanyuser cap by default.
2846 $this->assertTrue(\core_message\api::can_post_message($student1, $teacher1));
2847 // Check that the teacher can't contact user1 because it's not his teacher.
2848 $this->assertFalse(\core_message\api::can_post_message($user1, $teacher1));
2849
2850 // Remove the messageanyuser capability from the course1 for teachers.
2851 $coursecontext = context_course::instance($course1->id);
2852 $teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
2853 assign_capability('moodle/site:messageanyuser', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id);
2854 $coursecontext->mark_dirty();
2855
2856 // Check that we can't send user1 a message because they are not contacts.
2857 $this->assertFalse(\core_message\api::can_post_message($student1, $teacher1));
2858 // However, teacher can message student2 because they are sharing a course.
2859 $this->assertTrue(\core_message\api::can_post_message($student2, $teacher1));
f7dfa9ba
SA
2860 }
2861
2862 /**
2863 * Tests get_user_privacy_messaging_preference method.
2864 */
2865 public function test_get_user_privacy_messaging_preference() {
2866 // Create some users.
2867 $user1 = self::getDataGenerator()->create_user();
2868 $user2 = self::getDataGenerator()->create_user();
2869 $user3 = self::getDataGenerator()->create_user();
2870
2871 // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
2872 set_config('messagingallusers', true);
2873
2874 // Set some user preferences.
2875 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user1->id);
2876 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
2877
2878 // Check the returned value for each user.
2879 $this->assertEquals(
2880 \core_message\api::MESSAGE_PRIVACY_SITE,
2881 \core_message\api::get_user_privacy_messaging_preference($user1->id)
2882 );
2883 $this->assertEquals(
2884 \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
2885 \core_message\api::get_user_privacy_messaging_preference($user2->id)
2886 );
2887 $this->assertEquals(
2888 \core_message\api::MESSAGE_PRIVACY_SITE,
2889 \core_message\api::get_user_privacy_messaging_preference($user3->id)
2890 );
2891
2892 // Disable site-wide messagging privacy setting. The user will be able to receive messages from members of their course.
2893 set_config('messagingallusers', false);
2894
2895 // Check the returned value for each user.
2896 $this->assertEquals(
2897 \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
2898 \core_message\api::get_user_privacy_messaging_preference($user1->id)
2899 );
2900 $this->assertEquals(
2901 \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
2902 \core_message\api::get_user_privacy_messaging_preference($user2->id)
2903 );
2904 $this->assertEquals(
2905 \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
2906 \core_message\api::get_user_privacy_messaging_preference($user3->id)
2907 );
2908 }
2909
1f64514d
MN
2910 /**
2911 * Tests that when blocking messages from non-contacts is enabled that
2912 * non-contacts trying to send a message return false.
2913 */
2914 public function test_is_user_non_contact_blocked() {
2915 // Create some users.
2916 $user1 = self::getDataGenerator()->create_user();
2917 $user2 = self::getDataGenerator()->create_user();
2918
2919 // Set as the first user.
2920 $this->setUser($user1);
2921
f7dfa9ba
SA
2922 // By default, user only can be messaged by contacts and members of any of his/her courses.
2923 $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
e5eba802 2924 $this->assertDebuggingCalled();
f7dfa9ba
SA
2925
2926 // Enable all users privacy messaging and check now the default user's preference has been set to allow receiving
2927 // messages from everybody.
2928 set_config('messagingallusers', true);
2929 // Check that the return result is now false because any site user can contact him/her.
1f64514d 2930 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
e5eba802 2931 $this->assertDebuggingCalled();
1f64514d
MN
2932
2933 // Set the second user's preference to not receive messages from non-contacts.
f7dfa9ba
SA
2934 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
2935 // Check that the return result is still true (because is even more restricted).
1f64514d 2936 $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
e5eba802 2937 $this->assertDebuggingCalled();
1f64514d
MN
2938
2939 // Add the first user as a contact for the second user.
f219eac7 2940 \core_message\api::add_contact($user2->id, $user1->id);
1f64514d
MN
2941
2942 // Check that the return result is now false.
2943 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
e5eba802 2944 $this->assertDebuggingCalled();
f219eac7 2945
f7dfa9ba
SA
2946 // Set the second user's preference to receive messages from course members.
2947 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER, $user2->id);
2948 // Check that the return result is still false (because $user1 is still his/her contact).
2949 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
e5eba802 2950 $this->assertDebuggingCalled();
1f64514d
MN
2951 }
2952
2953 /**
2954 * Tests that we return true when a user is blocked, or false
2955 * if they are not blocked.
2956 */
2957 public function test_is_user_blocked() {
2958 // Create some users.
2959 $user1 = self::getDataGenerator()->create_user();
2960 $user2 = self::getDataGenerator()->create_user();
2961
2962 // Set the user.
2963 $this->setUser($user1);
2964
2965 // User shouldn't be blocked.
26dca05d 2966 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
c886e2c9 2967 $this->assertDebuggingCalled();
1f64514d
MN
2968
2969 // Block the user.
f219eac7 2970 \core_message\api::block_user($user1->id, $user2->id);
1f64514d
MN
2971
2972 // User should be blocked.
26dca05d 2973 $this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
c886e2c9 2974 $this->assertDebuggingCalled();
727145bf
AN
2975
2976 // Unblock the user.
f219eac7 2977 \core_message\api::unblock_user($user1->id, $user2->id);
727145bf 2978 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
c886e2c9 2979 $this->assertDebuggingCalled();
1f64514d
MN
2980 }
2981
2982 /**
2983 * Tests that the admin is not blocked even if someone has chosen to block them.
2984 */
2985 public function test_is_user_blocked_as_admin() {
2986 // Create a user.
2987 $user1 = self::getDataGenerator()->create_user();
2988
2989 // Set the user.
2990 $this->setUser($user1);
2991
2992 // Block the admin user.
f219eac7 2993 \core_message\api::block_user($user1->id, 2);
1f64514d
MN
2994
2995 // Now change to the admin user.
2996 $this->setAdminUser();
2997
2998 // As the admin you should still be able to send messages to the user.
26dca05d 2999 $this->assertFalse(\core_message\api::is_user_blocked($user1->id));
c886e2c9 3000 $this->assertDebuggingCalled();
1f64514d 3001 }
20ab51fd
AA
3002
3003 /*
3004 * Tes get_message_processor api.
3005 */
3006 public function test_get_message_processor() {
55b2a33c 3007 $processors = get_message_processors(true);
20ab51fd
AA
3008 if (empty($processors)) {
3009 $this->markTestSkipped("No message processors found");
3010 }
3011
33683bc8
MG
3012 $name = key($processors);
3013 $processor = current($processors);
20ab51fd
AA
3014 $testprocessor = \core_message\api::get_message_processor($name);
3015 $this->assertEquals($processor->name, $testprocessor->name);
3016 $this->assertEquals($processor->enabled, $testprocessor->enabled);
3017 $this->assertEquals($processor->available, $testprocessor->available);
3018 $this->assertEquals($processor->configured, $testprocessor->configured);
3019
3020 // Disable processor and test.
3021 \core_message\api::update_processor_status($testprocessor, 0);
3022 $testprocessor = \core_message\api::get_message_processor($name, true);
3023 $this->assertEmpty($testprocessor);
3024 $testprocessor = \core_message\api::get_message_processor($name);
3025 $this->assertEquals($processor->name, $testprocessor->name);
3026 $this->assertEquals(0, $testprocessor->enabled);
3027
3028 // Enable again and test.
3029 \core_message\api::update_processor_status($testprocessor, 1);
3030 $testprocessor = \core_message\api::get_message_processor($name, true);
3031 $this->assertEquals($processor->name, $testprocessor->name);
3032 $this->assertEquals(1, $testprocessor->enabled);
3033 $testprocessor = \core_message\api::get_message_processor($name);
3034 $this->assertEquals($processor->name, $testprocessor->name);
3035 $this->assertEquals(1, $testprocessor->enabled);
3036 }
3037
3038 /**
3039 * Test method update_processor_status.
3040 */
3041 public function test_update_processor_status() {
3042 $processors = get_message_processors();
3043 if (empty($processors)) {
3044 $this->markTestSkipped("No message processors found");
3045 }
33683bc8
MG
3046 $name = key($processors);
3047 $testprocessor = current($processors);
20ab51fd
AA
3048
3049 // Enable.
3050 \core_message\api::update_processor_status($testprocessor, 1);
3051 $testprocessor = \core_message\api::get_message_processor($name);
3052 $this->assertEquals(1, $testprocessor->enabled);
3053
3054 // Disable.
3055 \core_message\api::update_processor_status($testprocessor, 0);
3056 $testprocessor = \core_message\api::get_message_processor($name);
3057 $this->assertEquals(0, $testprocessor->enabled);
3058
3059 // Enable again.
3060 \core_message\api::update_processor_status($testprocessor, 1);
3061 $testprocessor = \core_message\api::get_message_processor($name);
3062 $this->assertEquals(1, $testprocessor->enabled);
3063 }
3064
3065 /**
3066 * Test method is_user_enabled.
3067 */
3068 public function is_user_enabled() {
3069 $processors = get_message_processors();
3070 if (empty($processors)) {
3071 $this->markTestSkipped("No message processors found");
3072 }
33683bc8
MG
3073 $name = key($processors);
3074 $testprocessor = current($processors);
20ab51fd
AA
3075
3076 // Enable.
3077 \core_message\api::update_processor_status($testprocessor, 1);
3078 $status = \core_message\api::is_processor_enabled($name);
3079 $this->assertEquals(1, $status);
3080
3081 // Disable.
3082 \core_message\api::update_processor_status($testprocessor, 0);
3083 $status = \core_message\api::is_processor_enabled($name);
3084 $this->assertEquals(0, $status);
3085
3086 // Enable again.
3087 \core_message\api::update_processor_status($testprocessor, 1);
3088 $status = \core_message\api::is_processor_enabled($name);
3089 $this->assertEquals(1, $status);
3090 }
fb1469d8
RW
3091
3092 /**
3093 * Test retrieving messages by providing a minimum timecreated value.
3094 */
ffd7798c 3095 public function test_get_messages_time_from_only() {
fb1469d8
RW
3096 // Create some users.
3097 $user1 = self::getDataGenerator()->create_user();
3098 $user2 = self::getDataGenerator()->create_user();
3099
3100 // The person doing the search.
3101 $this->setUser($user1);
3102
3103 // Send some messages back and forth.
3104 $time = 1;
3105 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
3106 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
3107 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
3108 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
3109
ffd7798c 3110 // Retrieve the messages from $time, which should be all of them.
fb1469d8 3111 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
d1e8e69d 3112 $this->assertDebuggingCalledCount(3);
fb1469d8
RW
3113
3114 // Confirm the message data is correct.
3115 $this->assertEquals(4, count($messages));
3116
3117 $message1 = $messages[0];
3118 $message2 = $messages[1];
3119 $message3 = $messages[2];
3120 $message4 = $messages[3];
3121
3122 $this->assertContains('Message 1', $message1->text);
3123 $this->assertContains('Message 2', $message2->text);
3124 $this->assertContains('Message 3', $message3->text);
3125 $this->assertContains('Message 4', $message4->text);
3126
ffd7798c 3127 // Retrieve the messages from $time + 3, which should only be the 2 last messages.
fb1469d8 3128 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
d1e8e69d 3129 $this->assertDebuggingCalledCount(3);
fb1469d8
RW
3130
3131 // Confirm the message data is correct.
3132 $this->assertEquals(2, count($messages));
3133
3134 $message1 = $messages[0];
3135 $message2 = $messages[1];
3136
3137 $this->assertContains('Message 3', $message1->text);
3138 $this->assertContains('Message 4', $message2->text);
3139 }
3140
3141 /**
3142 * Test retrieving messages by providing a maximum timecreated value.
3143 */
ffd7798c 3144 public function test_get_messages_time_to_only() {
fb1469d8
RW
3145 // Create some users.
3146 $user1 = self::getDataGenerator()->create_user();
3147 $user2 = self::getDataGenerator()->create_user();
3148
3149 // The person doing the search.
3150 $this->setUser($user1);
3151
3152 // Send some messages back and forth.
3153 $time = 1;
3154 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
3155 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
3156 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
3157 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
3158
ffd7798c 3159 // Retrieve the messages up until $time + 4, which should be all of them.
fb1469d8 3160 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
d1e8e69d 3161 $this->assertDebuggingCalledCount(3);
fb1469d8
RW
3162
3163 // Confirm the message data is correct.
3164 $this->assertEquals(4, count($messages));
3165
3166 $message1 = $messages[0];
3167 $message2 = $messages[1];
3168 $message3 = $messages[2];
3169 $message4 = $messages[3];
3170
3171 $this->assertContains('Message 1', $message1->text);
3172 $this->assertContains('Message 2', $message2->text);
3173 $this->assertContains('Message 3', $message3->text);
3174 $this->assertContains('Message 4', $message4->text);
3175
ffd7798c 3176 // Retrieve the messages up until $time + 2, which should be the first two.
fb1469d8 3177 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
d1e8e69d 3178 $this->assertDebuggingCalledCount(3);
fb1469d8
RW
3179
3180 // Confirm the message data is correct.
3181 $this->assertEquals(2, count($messages));
3182
3183 $message1 = $messages[0];
3184 $message2 = $messages[1];
3185
3186 $this->assertContains('Message 1', $message1->text);
3187 $this->assertContains('Message 2', $message2->text);
3188 }
3189
3190 /**
3191 * Test retrieving messages by providing a minimum and maximum timecreated value.
3192 */
ffd7798c 3193 public function test_get_messages_time_from_and_to() {
fb1469d8
RW
3194 // Create some users.
3195 $user1 = self::getDataGenerator()->create_user();
3196 $user2 = self::getDataGenerator()->create_user();
3197
3198 // The person doing the search.
3199 $this->setUser($user1);
3200
3201 // Send some messages back and forth.
3202 $time = 1;
3203 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
3204 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
3205 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
3206 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
3207
ffd7798c 3208 // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
fb1469d8 3209 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
d1e8e69d 3210 $this->assertDebuggingCalledCount(3);
fb1469d8
RW
3211
3212 // Confirm the message data is correct.
3213 $this->assertEquals(2, count($messages));
3214
3215 $message1 = $messages[0];
3216 $message2 = $messages[1];
3217
3218 $this->assertContains('Message 2', $message1->text);
3219 $this->assertContains('Message 3', $message2->text);
3220 }
883ce421
MN
3221
3222 /**
3223 * Test returning blocked users.
3224 */
3225 public function test_get_blocked_users() {
3226 global $USER;
3227
3228 // Set this user as the admin.
3229 $this->setAdminUser();
3230
3231 // Create a user to add to the admin's contact list.
3232 $user1 = $this->getDataGenerator()->create_user();
3233 $user2 = $this->getDataGenerator()->create_user();
3234
3235 // Add users to the admin's contact list.
f219eac7 3236 \core_message\api::block_user($USER->id, $user2->id);
883ce421
MN
3237
3238 $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
3239
3240 // Block other user.
f219eac7 3241 \core_message\api::block_user($USER->id, $user1->id);
883ce421
MN
3242 $this->assertCount(2, \core_message\api::get_blocked_users($USER->id));
3243
3244 // Test deleting users.
3245 delete_user($user1);
3246 $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
3247 }
3248
3249 /**
3250 * Test returning contacts with unread message count.
3251 */
3252 public function test_get_contacts_with_unread_message_count() {
548936a6
MN
3253 global $DB;
3254
883ce421
MN
3255 $user1 = self::getDataGenerator()->create_user();
3256 $user2 = self::getDataGenerator()->create_user();
3257 $user3 = self::getDataGenerator()->create_user();
3258 $user4 = self::getDataGenerator()->create_user();
3259
3260 // Add the users to each of their contacts.
f219eac7
MN
3261 \core_message\api::add_contact($user1->id, $user2->id);
3262 \core_message\api::add_contact($user2->id, $user3->id);
883ce421
MN
3263
3264 $this->send_fake_message($user1, $user2);
3265 $this->send_fake_message($user1, $user2);
3266 $this->send_fake_message($user1, $user2);
3267 $message4id = $this->send_fake_message($user1, $user2);
3268
3269 $this->send_fake_message($user3, $user2);
3270 $message6id = $this->send_fake_message($user3, $user2);
3271 $this->send_fake_message($user3, $user2);
3272 $this->send_fake_message($user3, $user2);
3273 $this->send_fake_message($user3, $user2);
3274
3275 // Send a message that should never be included as the user is not a contact.
3276 $this->send_fake_message($user4, $user2);
3277
3278 // Get the contacts and the unread message count.
3279 $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
f7dfa9ba 3280
883ce421
MN
3281 // Confirm the size is correct.
3282 $this->assertCount(2, $messages);
3283 ksort($messages);
3284
3285 $messageinfo1 = array_shift($messages);
3286 $messageinfo2 = array_shift($messages);
f219eac7 3287
883ce421
MN
3288 $this->assertEquals($user1->id, $messageinfo1->id);
3289 $this->assertEquals(4, $messageinfo1->messagecount);
3290 $this->assertEquals($user3->id, $messageinfo2->id);
3291 $this->assertEquals(5, $messageinfo2->messagecount);
3292
3293 // Mark some of the messages as read.
548936a6
MN
3294 $m4 = $DB->get_record('messages', ['id' => $message4id]);
3295 $m6 = $DB->get_record('messages', ['id' => $message6id]);
3296 \core_message\api::mark_message_as_read($user2->id, $m4);
3297 \core_message\api::mark_message_as_read($user2->id, $m6);
883ce421
MN
3298
3299 // Get the contacts and the unread message count.
3300 $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
3301
3302 // Confirm the size is correct.
3303 $this->assertCount(2, $messages);
3304 ksort($messages);
3305
3306 // Confirm read messages are not included.
3307 $messageinfo1 = array_shift($messages);
3308 $messageinfo2 = array_shift($messages);
3309 $this->assertEquals($user1->id, $messageinfo1->id);
3310 $this->assertEquals(3, $messageinfo1->messagecount);
3311 $this->assertEquals($user3->id, $messageinfo2->id);
3312 $this->assertEquals(4, $messageinfo2->messagecount);
3313
3314 // Now, let's populate the database with messages from user2 to user 1.
3315 $this->send_fake_message($user2, $user1);
3316 $this->send_fake_message($user2, $user1);
3317 $messageid = $this->send_fake_message($user2, $user1);
3318
3319 // Send a message that should never be included as the user is not a contact.
3320 $this->send_fake_message($user4, $user1);
3321
3322 // Get the contacts and the unread message count.
3323 $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
3324
3325 // Confirm the size is correct.
3326 $this->assertCount(1, $messages);
3327 $messageinfo1 = array_shift($messages);
3328 $this->assertEquals($user2->id, $messageinfo1->id);
3329 $this->assertEquals(3, $messageinfo1->messagecount);
3330
3331 // Mark the last message as read.
548936a6
MN
3332 $m = $DB->get_record('messages', ['id' => $messageid]);
3333 \core_message\api::mark_message_as_read($user1->id, $m);
883ce421
MN
3334
3335 $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
3336
3337 // Confirm the size is correct.
3338 $this->assertCount(1, $messages);
3339
3340 // Confirm read messages are not included.
3341 $messageinfo1 = array_shift($messages);
3342 $this->assertEquals($user2->id, $messageinfo1->id);
3343 $this->assertEquals(2, $messageinfo1->messagecount);
3344 }
3345
3346 /**
3347 * Test returning contacts with unread message count when there are no messages.
3348 */
3349 public function test_get_contacts_with_unread_message_count_no_messages() {
3350 $user1 = self::getDataGenerator()->create_user();
3351 $user2 = self::getDataGenerator()->create_user();
3352
3353 // Add the users to each of their contacts.
f219eac7 3354 \core_message\api::add_contact($user2->id, $user1->id);
883ce421
MN
3355
3356 // Check we get the correct message count.
3357 $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
3358
3359 // Confirm the size is correct.
3360 $this->assertCount(1, $messages);
3361
3362 $messageinfo = array_shift($messages);
3363
3364 $this->assertEquals($user1->id, $messageinfo->id);
3365 $this->assertEquals(0, $messageinfo->messagecount);
3366 }
3367
3368 /**
3369 * Test returning non-contacts with unread message count.
3370 */
3371 public function test_get_non_contacts_with_unread_message_count() {
548936a6
MN
3372 global $DB;
3373
883ce421
MN
3374 $user1 = self::getDataGenerator()->create_user();
3375 $user2 = self::getDataGenerator()->create_user();
3376 $user3 = self::getDataGenerator()->create_user();
3377 $user4 = self::getDataGenerator()->create_user();
3378
3379 // Add a user to the contact list of the users we are testing this function with.
f219eac7
MN
3380 \core_message\api::add_contact($user1->id, $user4->id);
3381 \core_message\api::add_contact($user2->id, $user4->id);
883ce421
MN
3382
3383 $this->send_fake_message($user1, $user2);
3384 $this->send_fake_message($user1, $user2);
3385 $this->send_fake_message($user1, $user2);
3386 $message4id = $this->send_fake_message($user1, $user2);
3387
3388 $this->send_fake_message($user3, $user2);
3389 $message6id = $this->send_fake_message($user3, $user2);
3390 $this->send_fake_message($user3, $user2);
3391 $this->send_fake_message($user3, $user2);
3392 $this->send_fake_message($user3, $user2);
3393
3394 // Send a message that should never be included as the user is a contact.
3395 $this->send_fake_message($user4, $user2);
3396
3397 // Get the non-contacts and the unread message count.
3398 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
3399
3400 // Check we get the correct message count.
3401 ksort($messages);
3402 $this->assertCount(2, $messages);
3403 $messageinfo1 = array_shift($messages);
3404 $messageinfo2 = array_shift($messages);
3405 $this->assertEquals($user1->id, $messageinfo1->id);
3406 $this->assertEquals(4, $messageinfo1->messagecount);
3407 $this->assertEquals($user3->id, $messageinfo2->id);
3408 $this->assertEquals(5, $messageinfo2->messagecount);
3409
3410 // Mark some of the messages as read.
548936a6
MN
3411 $m4 = $DB->get_record('messages', ['id' => $message4id]);
3412 $m6 = $DB->get_record('messages', ['id' => $message6id]);
3413 \core_message\api::mark_message_as_read($user2->id, $m4);
3414 \core_message\api::mark_message_as_read($user2->id, $m6);
883ce421
MN
3415
3416 // Get the non-contacts and the unread message count.
3417 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
3418
3419 // Check the marked message is not returned in the message count.
3420 ksort($messages);
3421 $this->assertCount(2, $messages);
3422 $messageinfo1 = array_shift($messages);
3423 $messageinfo2 = array_shift($messages);
3424 $this->assertEquals($user1->id, $messageinfo1->id);
3425 $this->assertEquals(3, $messageinfo1->messagecount);
3426 $this->assertEquals($user3->id, $messageinfo2->id);
3427 $this->assertEquals(4, $messageinfo2->messagecount);
3428
3429 // Now, let's populate the database with messages from user2 to user 1.
3430 $this->send_fake_message($user2, $user1);
3431 $this->send_fake_message($user2, $user1);
3432 $messageid = $this->send_fake_message($user2, $user1);
3433
3434 // Send a message that should never be included as the user is a contact.
3435 $this->send_fake_message($user4, $user1);
3436
3437 // Get the non-contacts and the unread message count.
3438 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
3439
3440 // Confirm the size is correct.
3441 $this->assertCount(1, $messages);
3442 $messageinfo1 = array_shift($messages);
3443 $this->assertEquals($user2->id, $messageinfo1->id);
3444 $this->assertEquals(3, $messageinfo1->messagecount);
3445
3446 // Mark the last message as read.
548936a6
MN
3447 $m = $DB->get_record('messages', ['id' => $messageid]);
3448 \core_message\api::mark_message_as_read($user1->id, $m);
883ce421
MN
3449
3450 // Get the non-contacts and the unread message count.
3451 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
3452
3453 // Check the marked message is not returned in the message count.
3454 $this->assertCount(1, $messages);
3455 $messageinfo1 = array_shift($messages);
3456 $this->assertEquals($user2->id, $messageinfo1->id);
3457 $this->assertEquals(2, $messageinfo1->messagecount);
3458 }
3459
3460 /**
3461 * Test marking a message as read.
3462 */
3463 public function test_mark_message_as_read() {
3464 global $DB;
3465
3466 $user1 = self::getDataGenerator()->create_user();
3467 $user2 = self::getDataGenerator()->create_user();
3468
3469 $this->send_fake_message($user1, $user2);
3470 $m2id = $this->send_fake_message($user1, $user2);
3471 $this->send_fake_message($user2, $user1);
3472 $m4id = $this->send_fake_message($user2, $user1);
3473
548936a6
MN
3474 $m2 = $DB->get_record('messages', ['id' => $m2id]);
3475 $m4 = $DB->get_record('messages', ['id' => $m4id]);
3476 \core_message\api::mark_message_as_read($user2->id, $m2, 11);
3477 \core_message\api::mark_message_as_read($user1->id, $m4, 12);
883ce421
MN
3478
3479 // Confirm there are two user actions.
3480 $muas = $DB->get_records('message_user_actions', [], 'timecreated ASC');
3481 $this->assertEquals(2, count($muas));
3482
3483 // Confirm they are correct.
3484 $mua1 = array_shift($muas);
3485 $mua2 = array_shift($muas);
3486
3487 // Confirm first action.
3488 $this->assertEquals($user2->id, $mua1->userid);
3489 $this->assertEquals($m2id, $mua1->messageid);
3490 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua1->action);
3491 $this->assertEquals(11, $mua1->timecreated);
3492
3493 // Confirm second action.
3494 $this->assertEquals($user1->id, $mua2->userid);
3495 $this->assertEquals($m4id, $mua2->messageid);
3496 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua2->action);
3497 $this->assertEquals(12, $mua2->timecreated);
3498 }
3499
3500 /**
3501 * Test marking a notification as read.
3502 */
3503 public function test_mark_notification_as_read() {
3504 global $DB;
3505
3506 $user1 = self::getDataGenerator()->create_user();
3507 $user2 = self::getDataGenerator()->create_user();
3508
3509 $this->send_fake_message($user1, $user2, 'Notification 1', 1);
3510 $n2id = $this->send_fake_message($user1, $user2, 'Notification 2', 1);
3511 $this->send_fake_message($user2, $user1, 'Notification 3', 1);
3512 $n4id = $this->send_fake_message($user2, $user1, 'Notification 4', 1);
3513
548936a6
MN
3514 $n2 = $DB->get_record('notifications', ['id' => $n2id]);
3515 $n4 = $DB->get_record('notifications', ['id' => $n4id]);
3516
3517 \core_message\api::mark_notification_as_read($n2, 11);
3518 \core_message\api::mark_notification_as_read($n4, 12);
883ce421
MN
3519
3520 // Retrieve the notifications.
3521 $n2 = $DB->get_record('notifications', ['id' => $n2id]);
3522 $n4 = $DB->get_record('notifications', ['id' => $n4id]);
3523
3524 // Confirm they have been marked as read.
3525 $this->assertEquals(11, $n2->timeread);
3526 $this->assertEquals(12, $n4->timeread);
3527 }
3528
3529 /**
3530 * Test a conversation is not returned if there is none.
3531 */
3532 public function test_get_conversation_between_users_no_conversation() {
3533 $user1 = self::getDataGenerator()->create_user();
3534 $user2 = self::getDataGenerator()->create_user();
3535
b2cd17e6 3536 $this->assertFalse(\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
883ce421
MN
3537 }
3538
3539 /**
3540 * Test we can return a conversation that exists between users.
3541 */
3542 public function test_get_conversation_between_users_with_existing_conversation() {
3543 $user1 = self::getDataGenerator()->create_user();
3544 $user2 = self::getDataGenerator()->create_user();
3545
b2cd17e6 3546 $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
f2ac0a3e 3547 $this->assertDebuggingCalled();
883ce421
MN
3548
3549 $this->assertEquals($conversationid,
b2cd17e6 3550 \core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
883ce421 3551 }
0b3eadcd 3552
623048af
AA
3553 /**
3554 * Test count_conversation_members for non existing conversation.
3555 */
3556 public function test_count_conversation_members_no_existing_conversation() {
3557 $this->assertEquals(0,
3558 \core_message\api::count_conversation_members(0));
3559 }
3560
3561 /**
3562 * Test count_conversation_members for existing conversation.
3563 */
3564 public function test_count_conversation_members_existing_conversation() {
3565 $user1 = self::getDataGenerator()->create_user();
3566 $user2 = self::getDataGenerator()->create_user();
3567
d9a25f39
MN
3568 $conversation = \core_message\api::create_conversation(
3569 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3570 [
3571 $user1->id,
3572 $user2->id
3573 ]
3574 );
3575 $conversationid = $conversation->id;
623048af
AA
3576
3577 $this->assertEquals(2,
3578 \core_message\api::count_conversation_members($conversationid));
3579 }
3580
d9a25f39
MN
3581 /**
3582 * Test add_members_to_conversation for an individual conversation.
3583 */
3584 public function test_add_members_to_individual_conversation() {
3585 $user1 = self::getDataGenerator()->create_user();
3586 $user2 = self::getDataGenerator()->create_user();
3587 $user3 = self::getDataGenerator()->create_user();
3588
3589 $conversation = \core_message\api::create_conversation(
3590 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
3591 [
3592 $user1->id,
3593 $user2->id
3594 ]
3595 );
3596 $conversationid = $conversation->id;
3597
3598 $this->expectException('moodle_exception');
3599 \core_message\api::add_members_to_conversation([$user3->id], $conversationid);
3600 }
3601
623048af
AA
3602 /**
3603 * Test add_members_to_conversation for existing conversation.
3604 */
3605 public function test_add_members_to_existing_conversation() {
3606 $user1 = self::getDataGenerator()->create_user();
3607 $user2 = self::getDataGenerator()->create_user();
3608 $user3 = self::getDataGenerator()->create_user();
3609
d9a25f39
MN
3610 $conversation = \core_message\api::create_conversation(
3611 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
3612 [
3613 $user1->id,
3614 $user2->id
3615 ]
3616 );
3617 $conversationid = $conversation->id;
623048af
AA
3618
3619 $this->assertNull(\core_message\api::add_members_to_conversation([$user3->id], $conversationid));
3620 $this->assertEquals(3,
3621 \core_message\api::count_conversation_members($conversationid));
3622 }
3623
3624 /**
3625 * Test add_members_to_conversation for non existing conversation.
3626 */
3627 public function test_add_members_to_no_existing_conversation() {
3628 $user1 = self::getDataGenerator()->create_user();
3629
3630 // Throw dml_missing_record_exception for non existing conversation.
3631 $this->expectException('dml_missing_record_exception');
3632 \core_message\api::add_members_to_conversation([$user1->id], 0);
3633 }
3634
3635 /**
3636 * Test add_member_to_conversation for non existing user.
3637 */
3638 public function test_add_members_to_no_existing_user() {
3639