on-demand release 3.6dev+
[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
32/**
33 * Test message API.
34 *
35 * @package core_message
36 * @category test
37 * @copyright 2016 Mark Nelson <markn@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class core_message_api_testcase extends core_message_messagelib_testcase {
41
1f64514d 42 public function test_mark_all_read_for_user_touser() {
79f6c36c
MN
43 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
44 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
45
7d69958e
RW
46 $this->send_fake_message($sender, $recipient, 'Notification', 1);
47 $this->send_fake_message($sender, $recipient, 'Notification', 1);
48 $this->send_fake_message($sender, $recipient, 'Notification', 1);
79f6c36c
MN
49 $this->send_fake_message($sender, $recipient);
50 $this->send_fake_message($sender, $recipient);
51 $this->send_fake_message($sender, $recipient);
52
53 \core_message\api::mark_all_read_for_user($recipient->id);
74ad60bf 54 $this->assertDebuggingCalled();
79f6c36c
MN
55 $this->assertEquals(message_count_unread_messages($recipient), 0);
56 }
57
1f64514d 58 public function test_mark_all_read_for_user_touser_with_fromuser() {
79f6c36c
MN
59 $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
60 $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
61 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
62
7d69958e
RW
63 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
64 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
65 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
79f6c36c
MN
66 $this->send_fake_message($sender1, $recipient);
67 $this->send_fake_message($sender1, $recipient);
68 $this->send_fake_message($sender1, $recipient);
7d69958e
RW
69 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
70 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
71 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
79f6c36c
MN
72 $this->send_fake_message($sender2, $recipient);
73 $this->send_fake_message($sender2, $recipient);
74 $this->send_fake_message($sender2, $recipient);
75
76 \core_message\api::mark_all_read_for_user($recipient->id, $sender1->id);
74ad60bf 77 $this->assertDebuggingCalled();
29c3b0b4 78 $this->assertEquals(message_count_unread_messages($recipient), 3);
79f6c36c
MN
79 }
80
1f64514d 81 public function test_mark_all_read_for_user_touser_with_type() {
79f6c36c
MN
82 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
83 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
84
7d69958e
RW
85 $this->send_fake_message($sender, $recipient, 'Notification', 1);
86 $this->send_fake_message($sender, $recipient, 'Notification', 1);
87 $this->send_fake_message($sender, $recipient, 'Notification', 1);
79f6c36c
MN
88 $this->send_fake_message($sender, $recipient);
89 $this->send_fake_message($sender, $recipient);
90 $this->send_fake_message($sender, $recipient);
91
92 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
74ad60bf 93 $this->assertDebuggingCalled();
79f6c36c
MN
94 $this->assertEquals(message_count_unread_messages($recipient), 3);
95
96 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
74ad60bf 97 $this->assertDebuggingCalled();
79f6c36c
MN
98 $this->assertEquals(message_count_unread_messages($recipient), 0);
99 }
100
5b0769db
MN
101 /**
102 * Test count_blocked_users.
5b0769db 103 */
1f64514d 104 public function test_count_blocked_users() {
f219eac7
MN
105 global $USER;
106
5b0769db
MN
107 // Set this user as the admin.
108 $this->setAdminUser();
109
f219eac7 110 // Create user to add to the admin's block list.
5b0769db 111 $user1 = $this->getDataGenerator()->create_user();
f7dfa9ba 112 $user2 = $this->getDataGenerator()->create_user();
5b0769db
MN
113
114 $this->assertEquals(0, \core_message\api::count_blocked_users());
115
f219eac7
MN
116 // Add 1 blocked user to admin's blocked user list.
117 \core_message\api::block_user($USER->id, $user1->id);
5b0769db 118
f219eac7 119 $this->assertEquals(0, \core_message\api::count_blocked_users($user1));
5b0769db
MN
120 $this->assertEquals(1, \core_message\api::count_blocked_users());
121 }
1f64514d
MN
122
123 /**
124 * Tests searching users in a course.
125 */
126 public function test_search_users_in_course() {
127 // Create some users.
128 $user1 = new stdClass();
129 $user1->firstname = 'User';
130 $user1->lastname = 'One';
131 $user1 = self::getDataGenerator()->create_user($user1);
132
133 // The person doing the search.
134 $this->setUser($user1);
135
136 // Second user is going to have their last access set to now, so they are online.
137 $user2 = new stdClass();
138 $user2->firstname = 'User';
139 $user2->lastname = 'Two';
140 $user2->lastaccess = time();
141 $user2 = self::getDataGenerator()->create_user($user2);
142
143 // Block the second user.
f219eac7 144 \core_message\api::block_user($user1->id, $user2->id);
1f64514d
MN
145
146 $user3 = new stdClass();
147 $user3->firstname = 'User';
148 $user3->lastname = 'Three';
149 $user3 = self::getDataGenerator()->create_user($user3);
150
151 // Create a course.
152 $course1 = new stdClass();
153 $course1->fullname = 'Course';
154 $course1->shortname = 'One';
155 $course1 = $this->getDataGenerator()->create_course($course1);
156
157 // Enrol the searcher and one user in the course.
158 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
159 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
160
161 // Perform a search.
162 $results = \core_message\api::search_users_in_course($user1->id, $course1->id, 'User');
163
164 $this->assertEquals(1, count($results));
165
166 $user = $results[0];
167 $this->assertEquals($user2->id, $user->userid);
168 $this->assertEquals(fullname($user2), $user->fullname);
169 $this->assertFalse($user->ismessaging);
170 $this->assertNull($user->lastmessage);
171 $this->assertNull($user->messageid);
cb805753 172 $this->assertNull($user->isonline);
1f64514d
MN
173 $this->assertFalse($user->isread);
174 $this->assertTrue($user->isblocked);
175 $this->assertNull($user->unreadcount);
176 }
177
178 /**
179 * Tests searching users.
180 */
181 public function test_search_users() {
09a05b79
AN
182 global $DB;
183
1f64514d
MN
184 // Create some users.
185 $user1 = new stdClass();
186 $user1->firstname = 'User';
187 $user1->lastname = 'One';
188 $user1 = self::getDataGenerator()->create_user($user1);
189
190 // Set as the user performing the search.
191 $this->setUser($user1);
192
193 $user2 = new stdClass();
194 $user2->firstname = 'User search';
195 $user2->lastname = 'Two';
196 $user2 = self::getDataGenerator()->create_user($user2);
197
198 $user3 = new stdClass();
199 $user3->firstname = 'User search';
200 $user3->lastname = 'Three';
201 $user3 = self::getDataGenerator()->create_user($user3);
202
203 $user4 = new stdClass();
204 $user4->firstname = 'User';
205 $user4->lastname = 'Four';
206 $user4 = self::getDataGenerator()->create_user($user4);
207
208 $user5 = new stdClass();
209 $user5->firstname = 'User search';
210 $user5->lastname = 'Five';
211 $user5 = self::getDataGenerator()->create_user($user5);
212
213 $user6 = new stdClass();
214 $user6->firstname = 'User';
215 $user6->lastname = 'Six';
216 $user6 = self::getDataGenerator()->create_user($user6);
217
218 // Create some courses.
219 $course1 = new stdClass();
220 $course1->fullname = 'Course search';
221 $course1->shortname = 'One';
222 $course1 = $this->getDataGenerator()->create_course($course1);
223
224 $course2 = new stdClass();
225 $course2->fullname = 'Course';
226 $course2->shortname = 'Two';
227 $course2 = $this->getDataGenerator()->create_course($course2);
228
229 $course3 = new stdClass();
230 $course3->fullname = 'Course';
231 $course3->shortname = 'Three search';
232 $course3 = $this->getDataGenerator()->create_course($course3);
233
87d4ab65
AG
234 $course4 = new stdClass();
235 $course4->fullname = 'Course Four';
236 $course4->shortname = 'CF100';
237 $course4 = $this->getDataGenerator()->create_course($course4);
238
09a05b79
AN
239 $course5 = new stdClass();
240 $course5->fullname = 'Course';
241 $course5->shortname = 'Five search';
242 $course5 = $this->getDataGenerator()->create_course($course5);
243
244 $role = $DB->get_record('role', ['shortname' => 'student']);
245 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $role->id);
246 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $role->id);
247 $this->getDataGenerator()->enrol_user($user1->id, $course3->id, $role->id);
248 $this->getDataGenerator()->enrol_user($user1->id, $course5->id, $role->id);
87d4ab65 249
1f64514d 250 // Add some users as contacts.
f219eac7
MN
251 \core_message\api::add_contact($user1->id, $user2->id);
252 \core_message\api::add_contact($user1->id, $user3->id);
253 \core_message\api::add_contact($user1->id, $user4->id);
1f64514d 254
09a05b79
AN
255 // Remove the viewparticipants capability from one of the courses.
256 $course5context = context_course::instance($course5->id);
257 assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $role->id, $course5context->id);
09a05b79 258
1f64514d
MN
259 // Perform a search.
260 list($contacts, $courses, $noncontacts) = \core_message\api::search_users($user1->id, 'search');
261
262 // Check that we retrieved the correct contacts.
263 $this->assertEquals(2, count($contacts));
264 $this->assertEquals($user3->id, $contacts[0]->userid);
265 $this->assertEquals($user2->id, $contacts[1]->userid);
266
267 // Check that we retrieved the correct courses.
268 $this->assertEquals(2, count($courses));
269 $this->assertEquals($course3->id, $courses[0]->id);
270 $this->assertEquals($course1->id, $courses[1]->id);
271
272 // Check that we retrieved the correct non-contacts.
273 $this->assertEquals(1, count($noncontacts));
274 $this->assertEquals($user5->id, $noncontacts[0]->userid);
275 }
276
277 /**
278 * Tests searching messages.
279 */
280 public function test_search_messages() {
281 // Create some users.
282 $user1 = self::getDataGenerator()->create_user();
283 $user2 = self::getDataGenerator()->create_user();
f219eac7 284 $user3 = self::getDataGenerator()->create_user();
1f64514d
MN
285
286 // The person doing the search.
287 $this->setUser($user1);
288
289 // Send some messages back and forth.
290 $time = 1;
f219eac7
MN
291 $this->send_fake_message($user3, $user1, 'Don\'t block me.', 0, $time);
292 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
293 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
294 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
295 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
296
297 // Block user 3.
298 \core_message\api::block_user($user1->id, $user3->id);
1f64514d
MN
299
300 // Perform a search.
301 $messages = \core_message\api::search_messages($user1->id, 'o');
302
303 // Confirm the data is correct.
f219eac7 304 $this->assertEquals(3, count($messages));
1f64514d
MN
305
306 $message1 = $messages[0];
307 $message2 = $messages[1];
f219eac7 308 $message3 = $messages[2];
1f64514d
MN
309
310 $this->assertEquals($user2->id, $message1->userid);
311 $this->assertEquals($user2->id, $message1->useridfrom);
312 $this->assertEquals(fullname($user2), $message1->fullname);
313 $this->assertTrue($message1->ismessaging);
314 $this->assertEquals('Word.', $message1->lastmessage);
315 $this->assertNotEmpty($message1->messageid);
cb805753 316 $this->assertNull($message1->isonline);
1f64514d
MN
317 $this->assertFalse($message1->isread);
318 $this->assertFalse($message1->isblocked);
319 $this->assertNull($message1->unreadcount);
320
321 $this->assertEquals($user2->id, $message2->userid);
322 $this->assertEquals($user1->id, $message2->useridfrom);
323 $this->assertEquals(fullname($user2), $message2->fullname);
324 $this->assertTrue($message2->ismessaging);
325 $this->assertEquals('Yo!', $message2->lastmessage);
326 $this->assertNotEmpty($message2->messageid);
cb805753 327 $this->assertNull($message2->isonline);
1f64514d
MN
328 $this->assertTrue($message2->isread);
329 $this->assertFalse($message2->isblocked);
330 $this->assertNull($message2->unreadcount);
f219eac7
MN
331
332 $this->assertEquals($user3->id, $message3->userid);
333 $this->assertEquals($user3->id, $message3->useridfrom);
334 $this->assertEquals(fullname($user3), $message3->fullname);
335 $this->assertTrue($message3->ismessaging);
336 $this->assertEquals('Don\'t block me.', $message3->lastmessage);
337 $this->assertNotEmpty($message3->messageid);
338 $this->assertNull($message3->isonline);
339 $this->assertFalse($message3->isread);
340 $this->assertTrue($message3->isblocked);
341 $this->assertNull($message3->unreadcount);
1f64514d
MN
342 }
343
344 /**
345 * Tests retrieving conversations.
346 */
347 public function test_get_conversations() {
348 // Create some users.
349 $user1 = self::getDataGenerator()->create_user();
350 $user2 = self::getDataGenerator()->create_user();
351 $user3 = self::getDataGenerator()->create_user();
352 $user4 = self::getDataGenerator()->create_user();
353
354 // The person doing the search.
355 $this->setUser($user1);
356
bb2924d0
DM
357 // No conversations yet.
358 $this->assertEquals([], \core_message\api::get_conversations($user1->id));
359
1f64514d
MN
360 // Send some messages back and forth, have some different conversations with different users.
361 $time = 1;
362 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
363 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
364 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
4d146f1a 365 $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1f64514d
MN
366
367 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
368 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
369 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
4d146f1a 370 $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
1f64514d
MN
371
372 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
373 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
4d146f1a 374 $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
1f64514d
MN
375
376 // Retrieve the conversations.
377 $conversations = \core_message\api::get_conversations($user1->id);
378
379 // Confirm the data is correct.
380 $this->assertEquals(3, count($conversations));
381
382 $message1 = array_shift($conversations);
383 $message2 = array_shift($conversations);
384 $message3 = array_shift($conversations);
385
386 $this->assertEquals($user4->id, $message1->userid);
387 $this->assertEquals($user1->id, $message1->useridfrom);
388 $this->assertTrue($message1->ismessaging);
389 $this->assertEquals('Dope.', $message1->lastmessage);
4d146f1a 390 $this->assertEquals($messageid3, $message1->messageid);
cb805753 391 $this->assertNull($message1->isonline);
4d146f1a 392 $this->assertFalse($message1->isread);
1f64514d 393 $this->assertFalse($message1->isblocked);
4d146f1a 394 $this->assertEquals(1, $message1->unreadcount);
1f64514d
MN
395
396 $this->assertEquals($user3->id, $message2->userid);
397 $this->assertEquals($user3->id, $message2->useridfrom);
398 $this->assertTrue($message2->ismessaging);
399 $this->assertEquals('Cool.', $message2->lastmessage);
4d146f1a 400 $this->assertEquals($messageid2, $message2->messageid);
cb805753 401 $this->assertNull($message2->isonline);
1f64514d
MN
402 $this->assertFalse($message2->isread);
403 $this->assertFalse($message2->isblocked);
404 $this->assertEquals(2, $message2->unreadcount);
405
406 $this->assertEquals($user2->id, $message3->userid);
407 $this->assertEquals($user2->id, $message3->useridfrom);
408 $this->assertTrue($message3->ismessaging);
409 $this->assertEquals('Word.', $message3->lastmessage);
4d146f1a 410 $this->assertEquals($messageid1, $message3->messageid);
cb805753 411 $this->assertNull($message3->isonline);
1f64514d
MN
412 $this->assertFalse($message3->isread);
413 $this->assertFalse($message3->isblocked);
414 $this->assertEquals(2, $message3->unreadcount);
415 }
416
4d146f1a
RW
417 /**
418 * Tests retrieving conversations with a limit and offset to ensure pagination works correctly.
419 */
420 public function test_get_conversations_limit_offset() {
421 // Create some users.
422 $user1 = self::getDataGenerator()->create_user();
423 $user2 = self::getDataGenerator()->create_user();
424 $user3 = self::getDataGenerator()->create_user();
425 $user4 = self::getDataGenerator()->create_user();
426
427 // The person doing the search.
428 $this->setUser($user1);
429
430 // Send some messages back and forth, have some different conversations with different users.
431 $time = 1;
432 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
433 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
434 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
435 $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
436
437 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
438 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
439 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
440 $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
441
442 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
443 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
444 $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
445
446 // Retrieve the conversations.
447 $conversations = \core_message\api::get_conversations($user1->id, 1, 1);
448
449 // We should only have one conversation because of the limit.
450 $this->assertCount(1, $conversations);
451
452 $conversation = array_shift($conversations);
453
454 $this->assertEquals($user3->id, $conversation->userid);
455 $this->assertEquals($user3->id, $conversation->useridfrom);
456 $this->assertTrue($conversation->ismessaging);
457 $this->assertEquals('Cool.', $conversation->lastmessage);
458 $this->assertEquals($messageid2, $conversation->messageid);
459 $this->assertNull($conversation->isonline);
460 $this->assertFalse($conversation->isread);
461 $this->assertFalse($conversation->isblocked);
462 $this->assertEquals(2, $conversation->unreadcount);
463
464 // Retrieve the next conversation.
465 $conversations = \core_message\api::get_conversations($user1->id, 2, 1);
466
467 // We should only have one conversation because of the limit.
468 $this->assertCount(1, $conversations);
469
470 $conversation = array_shift($conversations);
471
472 $this->assertEquals($user2->id, $conversation->userid);
473 $this->assertEquals($user2->id, $conversation->useridfrom);
474 $this->assertTrue($conversation->ismessaging);
475 $this->assertEquals('Word.', $conversation->lastmessage);
476 $this->assertEquals($messageid1, $conversation->messageid);
477 $this->assertNull($conversation->isonline);
478 $this->assertFalse($conversation->isread);
479 $this->assertFalse($conversation->isblocked);
480 $this->assertEquals(2, $conversation->unreadcount);
481
482 // Ask for an offset that doesn't exist.
483 $conversations = \core_message\api::get_conversations($user1->id, 4, 1);
484
485 // We should not get any conversations back.
486 $this->assertCount(0, $conversations);
487 }
488
f512355b
MN
489 /**
490 * Tests retrieving conversations when a conversation contains a deleted user.
491 */
492 public function test_get_conversations_with_deleted_user() {
493 // Create some users.
494 $user1 = self::getDataGenerator()->create_user();
495 $user2 = self::getDataGenerator()->create_user();
496 $user3 = self::getDataGenerator()->create_user();
497
498 // Send some messages back and forth, have some different conversations with different users.
499 $time = 1;
500 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
501 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
502 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
503 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
504
505 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
506 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
507 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
508 $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
509
510 // Delete the second user.
511 delete_user($user2);
512
513 // Retrieve the conversations.
514 $conversations = \core_message\api::get_conversations($user1->id);
515
516 // We should only have one conversation because the other user was deleted.
517 $this->assertCount(1, $conversations);
518
519 // Confirm the conversation is from the non-deleted user.
520 $conversation = reset($conversations);
521 $this->assertEquals($user3->id, $conversation->userid);
522 }
523
e12d9f49
RW
524 /**
525 * The data provider for get_conversations_mixed.
526 *
527 * This provides sets of data to for testing.
528 * @return array
529 */
530 public function get_conversations_mixed_provider() {
531 return array(
4d146f1a
RW
532 'Test that conversations with messages contacts is correctly ordered.' => array(
533 'users' => array(
534 'user1',
535 'user2',
536 'user3',
537 ),
538 'contacts' => array(
539 ),
540 'messages' => array(
541 array(
542 'from' => 'user1',
543 'to' => 'user2',
544 'state' => 'unread',
545 'subject' => 'S1',
546 ),
547 array(
548 'from' => 'user2',
549 'to' => 'user1',
550 'state' => 'unread',
551 'subject' => 'S2',
552 ),
553 array(
554 'from' => 'user1',
555 'to' => 'user2',
556 'state' => 'unread',
557 'timecreated' => 0,
558 'subject' => 'S3',
559 ),
560 array(
561 'from' => 'user1',
562 'to' => 'user3',
563 'state' => 'read',
564 'timemodifier' => 1,
565 'subject' => 'S4',
566 ),
567 array(
568 'from' => 'user3',
569 'to' => 'user1',
570 'state' => 'read',
571 'timemodifier' => 1,
572 'subject' => 'S5',
573 ),
574 array(
575 'from' => 'user1',
576 'to' => 'user3',
577 'state' => 'read',
578 'timecreated' => 0,
579 'subject' => 'S6',
580 ),
581 ),
582 'expectations' => array(
583 'user1' => array(
584 // User1 has conversed most recently with user3. The most recent message is M5.
585 array(
586 'messageposition' => 0,
587 'with' => 'user3',
588 'subject' => 'S5',
e12d9f49 589 'unreadcount' => 0,
4d146f1a
RW
590 ),
591 // User1 has also conversed with user2. The most recent message is S2.
592 array(
593 'messageposition' => 1,
594 'with' => 'user2',
595 'subject' => 'S2',
e12d9f49 596 'unreadcount' => 1,
4d146f1a
RW
597 ),
598 ),
599 'user2' => array(
600 // User2 has only conversed with user1. Their most recent shared message was S2.
601 array(
602 'messageposition' => 0,
603 'with' => 'user1',
604 'subject' => 'S2',
e12d9f49 605 'unreadcount' => 2,
4d146f1a
RW
606 ),
607 ),
608 'user3' => array(
609 // User3 has only conversed with user1. Their most recent shared message was S5.
610 array(
611 'messageposition' => 0,
612 'with' => 'user1',
613 'subject' => 'S5',
e12d9f49 614 'unreadcount' => 0,
4d146f1a
RW
615 ),
616 ),
617 ),
618 ),
619 'Test that users with contacts and messages to self work as expected' => array(
620 'users' => array(
621 'user1',
622 'user2',
623 'user3',
624 ),
625 'contacts' => array(
626 'user1' => array(
627 'user2' => 0,
628 'user3' => 0,
629 ),
630 'user2' => array(
631 'user3' => 0,
632 ),
633 ),
634 'messages' => array(
635 array(
636 'from' => 'user1',
637 'to' => 'user1',
638 'state' => 'unread',
639 'subject' => 'S1',
640 ),
641 array(
642 'from' => 'user1',
643 'to' => 'user1',
644 'state' => 'unread',
645 'subject' => 'S2',
646 ),
647 ),
648 'expectations' => array(
649 'user1' => array(
650 // User1 has conversed most recently with user1. The most recent message is S2.
651 array(
652 'messageposition' => 0,
653 'with' => 'user1',
654 'subject' => 'S2',
883ce421 655 'unreadcount' => 0, // Messages sent to and from the same user are counted as read.
4d146f1a
RW
656 ),
657 ),
658 ),
659 ),
660 'Test conversations with a single user, where some messages are read and some are not.' => array(
661 'users' => array(
662 'user1',
663 'user2',
664 ),
665 'contacts' => array(
666 ),
667 'messages' => array(
668 array(
669 'from' => 'user1',
670 'to' => 'user2',
671 'state' => 'read',
672 'subject' => 'S1',
673 ),
674 array(
675 'from' => 'user2',
676 'to' => 'user1',
677 'state' => 'read',
678 'subject' => 'S2',
679 ),
680 array(
681 'from' => 'user1',
682 'to' => 'user2',
683 'state' => 'unread',
684 'timemodifier' => 1,
685 'subject' => 'S3',
686 ),
687 array(
688 'from' => 'user1',
689 'to' => 'user2',
690 'state' => 'unread',
691 'timemodifier' => 1,
692 'subject' => 'S4',
693 ),
694 ),
695 'expectations' => array(
696 // The most recent message between user1 and user2 was S4.
697 'user1' => array(
698 array(
699 'messageposition' => 0,
700 'with' => 'user2',
701 'subject' => 'S4',
e12d9f49 702 'unreadcount' => 0,
4d146f1a
RW
703 ),
704 ),
705 'user2' => array(
706 // The most recent message between user1 and user2 was S4.
707 array(
708 'messageposition' => 0,
709 'with' => 'user1',
710 'subject' => 'S4',
e12d9f49 711 'unreadcount' => 2,
4d146f1a
RW
712 ),
713 ),
714 ),
715 ),
716 'Test conversations with a single user, where some messages are read and some are not, and messages ' .
717 'are out of order' => array(
718 // This can happen through a combination of factors including multi-master DB replication with messages
719 // read somehow (e.g. API).
720 'users' => array(
721 'user1',
722 'user2',
723 ),
724 'contacts' => array(
725 ),
726 'messages' => array(
727 array(
728 'from' => 'user1',
729 'to' => 'user2',
730 'state' => 'read',
731 'subject' => 'S1',
732 'timemodifier' => 1,
733 ),
734 array(
735 'from' => 'user2',
736 'to' => 'user1',
737 'state' => 'read',
738 'subject' => 'S2',
739 'timemodifier' => 2,
740 ),
741 array(
742 'from' => 'user1',
743 'to' => 'user2',
744 'state' => 'unread',
745 'subject' => 'S3',
746 ),
747 array(
748 'from' => 'user1',
749 'to' => 'user2',
750 'state' => 'unread',
751 'subject' => 'S4',
752 ),
753 ),
754 'expectations' => array(
755 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
756 'user1' => array(
757 array(
758 'messageposition' => 0,
759 'with' => 'user2',
760 'subject' => 'S2',
e12d9f49 761 'unreadcount' => 0,
4d146f1a
RW
762 ),
763 ),
764 'user2' => array(
765 array(
766 'messageposition' => 0,
767 'with' => 'user1',
768 'subject' => 'S2',
e12d9f49
RW
769 'unreadcount' => 2
770 ),
771 ),
772 ),
773 ),
774 'Test unread message count is correct for both users' => array(
775 'users' => array(
776 'user1',
777 'user2',
778 ),
779 'contacts' => array(
780 ),
781 'messages' => array(
782 array(
783 'from' => 'user1',
784 'to' => 'user2',
785 'state' => 'read',
786 'subject' => 'S1',
787 'timemodifier' => 1,
788 ),
789 array(
790 'from' => 'user2',
791 'to' => 'user1',
792 'state' => 'read',
793 'subject' => 'S2',
794 'timemodifier' => 2,
795 ),
796 array(
797 'from' => 'user1',
798 'to' => 'user2',
799 'state' => 'read',
800 'subject' => 'S3',
801 'timemodifier' => 3,
802 ),
803 array(
804 'from' => 'user1',
805 'to' => 'user2',
806 'state' => 'read',
807 'subject' => 'S4',
808 'timemodifier' => 4,
809 ),
810 array(
811 'from' => 'user1',
812 'to' => 'user2',
813 'state' => 'unread',
814 'subject' => 'S5',
815 'timemodifier' => 5,
816 ),
817 array(
818 'from' => 'user2',
819 'to' => 'user1',
820 'state' => 'unread',
821 'subject' => 'S6',
822 'timemodifier' => 6,
823 ),
824 array(
825 'from' => 'user1',
826 'to' => 'user2',
827 'state' => 'unread',
828 'subject' => 'S7',
829 'timemodifier' => 7,
830 ),
831 array(
832 'from' => 'user1',
833 'to' => 'user2',
834 'state' => 'unread',
835 'subject' => 'S8',
836 'timemodifier' => 8,
837 ),
838 ),
839 'expectations' => array(
840 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
841 'user1' => array(
842 array(
843 'messageposition' => 0,
844 'with' => 'user2',
845 'subject' => 'S8',
846 'unreadcount' => 1,
847 ),
848 ),
849 'user2' => array(
850 array(
851 'messageposition' => 0,
852 'with' => 'user1',
853 'subject' => 'S8',
854 'unreadcount' => 3,
4d146f1a
RW
855 ),
856 ),
857 ),
858 ),
859 );
860 }
861
862 /**
863 * Test get_conversations with a mixture of messages.
864 *
865 * @dataProvider get_conversations_mixed_provider
866 * @param array $usersdata The list of users to create for this test.
867 * @param array $messagesdata The list of messages to create.
868 * @param array $expectations The list of expected outcomes.
869 */
870 public function test_get_conversations_mixed($usersdata, $contacts, $messagesdata, $expectations) {
871 global $DB;
872
873 // Create all of the users.
874 $users = array();
875 foreach ($usersdata as $username) {
876 $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
877 }
878
879 foreach ($contacts as $username => $contact) {
880 foreach ($contact as $contactname => $blocked) {
881 $record = new stdClass();
882 $record->userid = $users[$username]->id;
883 $record->contactid = $users[$contactname]->id;
884 $record->blocked = $blocked;
885 $record->id = $DB->insert_record('message_contacts', $record);
886 }
887 }
888
889 $defaulttimecreated = time();
890 foreach ($messagesdata as $messagedata) {
891 $from = $users[$messagedata['from']];
892 $to = $users[$messagedata['to']];
893 $subject = $messagedata['subject'];
894
895 if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
4d146f1a
RW
896 $messageid = $this->send_fake_message($from, $to, $subject);
897 } else {
898 // If there is no state, or the state is not 'unread', assume the message is read.
4d146f1a
RW
899 $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN);
900 }
901
902 $updatemessage = new stdClass();
903 $updatemessage->id = $messageid;
904 if (isset($messagedata['timecreated'])) {
905 $updatemessage->timecreated = $messagedata['timecreated'];
906 } else if (isset($messagedata['timemodifier'])) {
907 $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier'];
908 } else {
909 $updatemessage->timecreated = $defaulttimecreated;
910 }
e12d9f49 911
883ce421 912 $DB->update_record('messages', $updatemessage);
4d146f1a
RW
913 }
914
915 foreach ($expectations as $username => $data) {
916 // Get the recent conversations for the specified user.
917 $user = $users[$username];
918 $conversations = array_values(\core_message\api::get_conversations($user->id));
919 foreach ($data as $expectation) {
920 $otheruser = $users[$expectation['with']];
921 $conversation = $conversations[$expectation['messageposition']];
922 $this->assertEquals($otheruser->id, $conversation->userid);
923 $this->assertEquals($expectation['subject'], $conversation->lastmessage);
e12d9f49 924 $this->assertEquals($expectation['unreadcount'], $conversation->unreadcount);
4d146f1a
RW
925 }
926 }
927 }
928
1f64514d
MN
929 /**
930 * Tests retrieving contacts.
931 */
932 public function test_get_contacts() {
933 // Create some users.
934 $user1 = self::getDataGenerator()->create_user();
935
936 // Set as the user.
937 $this->setUser($user1);
938
939 $user2 = new stdClass();
940 $user2->firstname = 'User';
941 $user2->lastname = 'A';
942 $user2 = self::getDataGenerator()->create_user($user2);
943
944 $user3 = new stdClass();
945 $user3->firstname = 'User';
946 $user3->lastname = 'B';
947 $user3 = self::getDataGenerator()->create_user($user3);
948
949 $user4 = new stdClass();
950 $user4->firstname = 'User';
951 $user4->lastname = 'C';
952 $user4 = self::getDataGenerator()->create_user($user4);
953
954 $user5 = new stdClass();
955 $user5->firstname = 'User';
956 $user5->lastname = 'D';
957 $user5 = self::getDataGenerator()->create_user($user5);
958
959 // Add some users as contacts.
f219eac7
MN
960 \core_message\api::add_contact($user1->id, $user2->id);
961 \core_message\api::add_contact($user1->id, $user3->id);
962 \core_message\api::add_contact($user1->id, $user4->id);
1f64514d
MN
963
964 // Retrieve the contacts.
965 $contacts = \core_message\api::get_contacts($user1->id);
966
967 // Confirm the data is correct.
968 $this->assertEquals(3, count($contacts));
f219eac7 969 usort($contacts, ['static', 'sort_contacts']);
1f64514d
MN
970
971 $contact1 = $contacts[0];
972 $contact2 = $contacts[1];
973 $contact3 = $contacts[2];
974
975 $this->assertEquals($user2->id, $contact1->userid);
976 $this->assertEmpty($contact1->useridfrom);
977 $this->assertFalse($contact1->ismessaging);
978 $this->assertNull($contact1->lastmessage);
979 $this->assertNull($contact1->messageid);
cb805753 980 $this->assertNull($contact1->isonline);
1f64514d
MN
981 $this->assertFalse($contact1->isread);
982 $this->assertFalse($contact1->isblocked);
983 $this->assertNull($contact1->unreadcount);
984
985 $this->assertEquals($user3->id, $contact2->userid);
986 $this->assertEmpty($contact2->useridfrom);
987 $this->assertFalse($contact2->ismessaging);
988 $this->assertNull($contact2->lastmessage);
989 $this->assertNull($contact2->messageid);
cb805753 990 $this->assertNull($contact2->isonline);
1f64514d
MN
991 $this->assertFalse($contact2->isread);
992 $this->assertFalse($contact2->isblocked);
993 $this->assertNull($contact2->unreadcount);
994
995 $this->assertEquals($user4->id, $contact3->userid);
996 $this->assertEmpty($contact3->useridfrom);
997 $this->assertFalse($contact3->ismessaging);
998 $this->assertNull($contact3->lastmessage);
999 $this->assertNull($contact3->messageid);
cb805753 1000 $this->assertNull($contact3->isonline);
1f64514d
MN
1001 $this->assertFalse($contact3->isread);
1002 $this->assertFalse($contact3->isblocked);
1003 $this->assertNull($contact3->unreadcount);
1004 }
1005
1006 /**
1007 * Tests retrieving messages.
1008 */
1009 public function test_get_messages() {
1010 // Create some users.
1011 $user1 = self::getDataGenerator()->create_user();
1012 $user2 = self::getDataGenerator()->create_user();
1013
1014 // The person doing the search.
1015 $this->setUser($user1);
1016
1017 // Send some messages back and forth.
1018 $time = 1;
1019 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1020 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1021 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1022 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1023
1024 // Retrieve the messages.
1025 $messages = \core_message\api::get_messages($user1->id, $user2->id);
1026
1027 // Confirm the message data is correct.
1028 $this->assertEquals(4, count($messages));
1029
1030 $message1 = $messages[0];
1031 $message2 = $messages[1];
1032 $message3 = $messages[2];
1033 $message4 = $messages[3];
1034
1035 $this->assertEquals($user1->id, $message1->useridfrom);
1036 $this->assertEquals($user2->id, $message1->useridto);
1037 $this->assertTrue($message1->displayblocktime);
1038 $this->assertContains('Yo!', $message1->text);
1039
1040 $this->assertEquals($user2->id, $message2->useridfrom);
1041 $this->assertEquals($user1->id, $message2->useridto);
1042 $this->assertFalse($message2->displayblocktime);
1043 $this->assertContains('Sup mang?', $message2->text);
1044
1045 $this->assertEquals($user1->id, $message3->useridfrom);
1046 $this->assertEquals($user2->id, $message3->useridto);
1047 $this->assertFalse($message3->displayblocktime);
1048 $this->assertContains('Writing PHPUnit tests!', $message3->text);
1049
1050 $this->assertEquals($user2->id, $message4->useridfrom);
1051 $this->assertEquals($user1->id, $message4->useridto);
1052 $this->assertFalse($message4->displayblocktime);
1053 $this->assertContains('Word.', $message4->text);
1054 }
1055
1056 /**
1057 * Tests retrieving most recent message.
1058 */
1059 public function test_get_most_recent_message() {
1060 // Create some users.
1061 $user1 = self::getDataGenerator()->create_user();
1062 $user2 = self::getDataGenerator()->create_user();
1063
1064 // The person doing the search.
1065 $this->setUser($user1);
1066
1067 // Send some messages back and forth.
1068 $time = 1;
1069 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1070 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1071 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1072 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1073
1074 // Retrieve the most recent messages.
1075 $message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
1076
1077 // Check the results are correct.
1078 $this->assertEquals($user2->id, $message->useridfrom);
1079 $this->assertEquals($user1->id, $message->useridto);
1080 $this->assertContains('Word.', $message->text);
1081 }
1082
1083 /**
1084 * Tests retrieving a user's profile.
1085 */
1086 public function test_get_profile() {
1087 // Create some users.
1088 $user1 = self::getDataGenerator()->create_user();
1089
1090 $user2 = new stdClass();
1091 $user2->country = 'AU';
1092 $user2->city = 'Perth';
1093 $user2 = self::getDataGenerator()->create_user($user2);
1094
1095 // The person doing the search.
1096 $this->setUser($user1);
1097
1098 // Get the profile.
1099 $profile = \core_message\api::get_profile($user1->id, $user2->id);
1100
1101 $this->assertEquals($user2->id, $profile->userid);
1102 $this->assertEmpty($profile->email);
1103 $this->assertEmpty($profile->country);
1104 $this->assertEmpty($profile->city);
1105 $this->assertEquals(fullname($user2), $profile->fullname);
cb805753 1106 $this->assertNull($profile->isonline);
1f64514d
MN
1107 $this->assertFalse($profile->isblocked);
1108 $this->assertFalse($profile->iscontact);
1109 }
1110
1111 /**
1112 * Tests retrieving a user's profile.
1113 */
1114 public function test_get_profile_as_admin() {
1115 // The person doing the search.
1116 $this->setAdminUser();
1117
1118 // Create some users.
1119 $user1 = self::getDataGenerator()->create_user();
1120
1121 $user2 = new stdClass();
1122 $user2->country = 'AU';
1123 $user2->city = 'Perth';
1124 $user2 = self::getDataGenerator()->create_user($user2);
1125
1126 // Get the profile.
1127 $profile = \core_message\api::get_profile($user1->id, $user2->id);
1128
1129 $this->assertEquals($user2->id, $profile->userid);
1130 $this->assertEquals($user2->email, $profile->email);
1131 $this->assertEquals($user2->country, $profile->country);
1132 $this->assertEquals($user2->city, $profile->city);
1133 $this->assertEquals(fullname($user2), $profile->fullname);
1134 $this->assertFalse($profile->isonline);
1135 $this->assertFalse($profile->isblocked);
1136 $this->assertFalse($profile->iscontact);
1137 }
1138
1139 /**
1140 * Tests checking if a user can delete a conversation.
1141 */
1142 public function test_can_delete_conversation() {
1143 // Set as the admin.
1144 $this->setAdminUser();
1145
1146 // Create some users.
1147 $user1 = self::getDataGenerator()->create_user();
1148 $user2 = self::getDataGenerator()->create_user();
1149
15663b0b
MN
1150 // Send some messages back and forth.
1151 $time = 1;
1152 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1153 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1154 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1155 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1156
1157 $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1158
1f64514d 1159 // The admin can do anything.
15663b0b 1160 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id, $conversationid));
1f64514d
MN
1161
1162 // Set as the user 1.
1163 $this->setUser($user1);
1164
1165 // They can delete their own messages.
15663b0b 1166 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id, $conversationid));
1f64514d
MN
1167
1168 // They can't delete someone elses.
15663b0b 1169 $this->assertFalse(\core_message\api::can_delete_conversation($user2->id, $conversationid));
1f64514d
MN
1170 }
1171
1172 /**
1173 * Tests deleting a conversation.
1174 */
1175 public function test_delete_conversation() {
1176 global $DB;
1177
1178 // Create some users.
1179 $user1 = self::getDataGenerator()->create_user();
1180 $user2 = self::getDataGenerator()->create_user();
1181
1182 // The person doing the search.
1183 $this->setUser($user1);
1184
1185 // Send some messages back and forth.
1186 $time = 1;
883ce421
MN
1187 $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1188 $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1189 $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1190 $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1f64514d
MN
1191
1192 // Delete the conversation as user 1.
1193 \core_message\api::delete_conversation($user1->id, $user2->id);
263ad984
MN
1194 $this->assertDebuggingCalled();
1195
1196 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
1197 $this->assertCount(4, $muas);
1198 // Sort by id.
1199 ksort($muas);
1200
1201 $mua1 = array_shift($muas);
1202 $mua2 = array_shift($muas);
1203 $mua3 = array_shift($muas);
1204 $mua4 = array_shift($muas);
1205
1206 $this->assertEquals($user1->id, $mua1->userid);
1207 $this->assertEquals($m1id, $mua1->messageid);
1208 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
1209
1210 $this->assertEquals($user1->id, $mua2->userid);
1211 $this->assertEquals($m2id, $mua2->messageid);
1212 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
1213
1214 $this->assertEquals($user1->id, $mua3->userid);
1215 $this->assertEquals($m3id, $mua3->messageid);
1216 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
1217
1218 $this->assertEquals($user1->id, $mua4->userid);
1219 $this->assertEquals($m4id, $mua4->messageid);
1220 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
1221 }
1222
1223 /**
1224 * Tests deleting a conversation by conversation id.
1225 */
1226 public function test_delete_conversation_by_id() {
1227 global $DB;
1228
1229 // Create some users.
1230 $user1 = self::getDataGenerator()->create_user();
1231 $user2 = self::getDataGenerator()->create_user();
1232
1233 // The person doing the search.
1234 $this->setUser($user1);
1235
1236 // Send some messages back and forth.
1237 $time = 1;
1238 $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1239 $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1240 $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1241 $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1242
1243 // Delete the conversation as user 1.
1244 $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1245 \core_message\api::delete_conversation_by_id($user1->id, $conversationid);
1f64514d 1246
883ce421
MN
1247 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
1248 $this->assertCount(4, $muas);
1249 // Sort by id.
1250 ksort($muas);
1f64514d 1251
883ce421
MN
1252 $mua1 = array_shift($muas);
1253 $mua2 = array_shift($muas);
1254 $mua3 = array_shift($muas);
1255 $mua4 = array_shift($muas);
1f64514d 1256
883ce421
MN
1257 $this->assertEquals($user1->id, $mua1->userid);
1258 $this->assertEquals($m1id, $mua1->messageid);
1259 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
1f64514d 1260
883ce421
MN
1261 $this->assertEquals($user1->id, $mua2->userid);
1262 $this->assertEquals($m2id, $mua2->messageid);
1263 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
1f64514d 1264
883ce421
MN
1265 $this->assertEquals($user1->id, $mua3->userid);
1266 $this->assertEquals($m3id, $mua3->messageid);
1267 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
1f64514d 1268
883ce421
MN
1269 $this->assertEquals($user1->id, $mua4->userid);
1270 $this->assertEquals($m4id, $mua4->messageid);
1271 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
1f64514d
MN
1272 }
1273
1274 /**
1275 * Tests counting unread conversations.
1276 */
1277 public function test_count_unread_conversations() {
1278 $this->resetAfterTest(true);
1279
1280 // Create some users.
1281 $user1 = self::getDataGenerator()->create_user();
1282 $user2 = self::getDataGenerator()->create_user();
1283 $user3 = self::getDataGenerator()->create_user();
1284 $user4 = self::getDataGenerator()->create_user();
1285
1286 // The person wanting the conversation count.
1287 $this->setUser($user1);
1288
1289 // Send some messages back and forth, have some different conversations with different users.
1290 $this->send_fake_message($user1, $user2, 'Yo!');
1291 $this->send_fake_message($user2, $user1, 'Sup mang?');
1292 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
1293 $this->send_fake_message($user2, $user1, 'Word.');
1294
1295 $this->send_fake_message($user1, $user3, 'Booyah');
1296 $this->send_fake_message($user3, $user1, 'Whaaat?');
1297 $this->send_fake_message($user1, $user3, 'Nothing.');
1298 $this->send_fake_message($user3, $user1, 'Cool.');
1299
1300 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
1301 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
1302 $this->send_fake_message($user1, $user4, 'Dope.');
1303
1304 // Check the amount for the current user.
1305 $this->assertEquals(3, core_message\api::count_unread_conversations());
1306
1307 // Check the amount for the second user.
1308 $this->assertEquals(1, core_message\api::count_unread_conversations($user2));
1309 }
1310
1311 /**
1312 * Tests deleting a conversation.
1313 */
1314 public function test_get_all_message_preferences() {
1315 $user = self::getDataGenerator()->create_user();
1316 $this->setUser($user);
1317
1318 // Set a couple of preferences to test.
1319 set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
1320 set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
1321
1322 $processors = get_message_processors();
1323 $providers = message_get_providers_for_user($user->id);
1324 $prefs = \core_message\api::get_all_message_preferences($processors, $providers, $user);
1325
1326 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedin['popup']);
1327 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']);
1328 }
1329
1330 /**
1331 * Tests the user can post a message.
1332 */
1333 public function test_can_post_message() {
1334 // Create some users.
1335 $user1 = self::getDataGenerator()->create_user();
1336 $user2 = self::getDataGenerator()->create_user();
1337
f7dfa9ba 1338 // Set as the first user.
1f64514d
MN
1339 $this->setUser($user1);
1340
f7dfa9ba
SA
1341 // With the default privacy setting, users can't message them.
1342 $this->assertFalse(\core_message\api::can_post_message($user2));
1343
1344 // Enrol users to the same course.
1345 $course = $this->getDataGenerator()->create_course();
1346 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1347 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
1348 // After enrolling users to the course, they should be able to message them with the default privacy setting.
1f64514d
MN
1349 $this->assertTrue(\core_message\api::can_post_message($user2));
1350 }
1351
1352 /**
1353 * Tests the user can't post a message without proper capability.
1354 */
1355 public function test_can_post_message_without_cap() {
1356 global $DB;
1357
1358 // Create some users.
1359 $user1 = self::getDataGenerator()->create_user();
1360 $user2 = self::getDataGenerator()->create_user();
1361
1362 // Set as the user 1.
1363 $this->setUser($user1);
1364
1365 // Remove the capability to send a message.
1366 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
1367 unassign_capability('moodle/site:sendmessage', $roleids['user'],
1368 context_system::instance());
1369
1370 // Check that we can not post a message without the capability.
1371 $this->assertFalse(\core_message\api::can_post_message($user2));
1372 }
1373
f7dfa9ba
SA
1374 /**
1375 * Tests the user can post a message when they are contact.
1376 */
1377 public function test_can_post_message_when_contact() {
1378 // Create some users.
1379 $user1 = self::getDataGenerator()->create_user();
1380 $user2 = self::getDataGenerator()->create_user();
1381
1382 // Set as the first user.
1383 $this->setUser($user1);
1384
1385 // Check that we can not send user2 a message.
1386 $this->assertFalse(\core_message\api::can_post_message($user2));
1387
1388 // Add users as contacts.
1389 \core_message\api::add_contact($user1->id, $user2->id);
1390
1391 // Check that the return result is now true.
1392 $this->assertTrue(\core_message\api::can_post_message($user2));
1393 }
1394
1f64514d
MN
1395 /**
1396 * Tests the user can't post a message if they are not a contact and the user
1397 * has requested messages only from contacts.
1398 */
1399 public function test_can_post_message_when_not_contact() {
1400 // Create some users.
1401 $user1 = self::getDataGenerator()->create_user();
1402 $user2 = self::getDataGenerator()->create_user();
1403
1404 // Set as the first user.
1405 $this->setUser($user1);
1406
1407 // Set the second user's preference to not receive messages from non-contacts.
f7dfa9ba 1408 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1f64514d
MN
1409
1410 // Check that we can not send user 2 a message.
1411 $this->assertFalse(\core_message\api::can_post_message($user2));
1412 }
1413
1414 /**
1415 * Tests the user can't post a message if they are blocked.
1416 */
1417 public function test_can_post_message_when_blocked() {
1418 // Create some users.
1419 $user1 = self::getDataGenerator()->create_user();
1420 $user2 = self::getDataGenerator()->create_user();
1421
1422 // Set the user.
1423 $this->setUser($user1);
1424
1425 // Block the second user.
f219eac7 1426 \core_message\api::block_user($user1->id, $user2->id);
1f64514d
MN
1427
1428 // Check that the second user can no longer send the first user a message.
1429 $this->assertFalse(\core_message\api::can_post_message($user1, $user2));
1430 }
1431
f7dfa9ba
SA
1432 /**
1433 * Tests the user can post a message when site-wide messaging setting is enabled,
1434 * even if they are not a contact and are not members of the same course.
1435 */
1436 public function test_can_post_message_site_messaging_setting() {
1437 // Create some users.
1438 $user1 = self::getDataGenerator()->create_user();
1439 $user2 = self::getDataGenerator()->create_user();
1440
1441 // Set as the first user.
1442 $this->setUser($user1);
1443
1444 // Set the second user's preference to receive messages from everybody. As site-wide messaging setting
1445 // is disabled by default, the value will be changed to MESSAGE_PRIVACY_COURSEMEMBER.
1446 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user2->id);
1447 $this->assertFalse(\core_message\api::can_post_message($user2));
1448
1449 // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
1450 set_config('messagingallusers', true);
1451 // Check that we can send user2 a message.
1452 $this->assertTrue(\core_message\api::can_post_message($user2));
1453 }
1454
1455 /**
1456 * Tests get_user_privacy_messaging_preference method.
1457 */
1458 public function test_get_user_privacy_messaging_preference() {
1459 // Create some users.
1460 $user1 = self::getDataGenerator()->create_user();
1461 $user2 = self::getDataGenerator()->create_user();
1462 $user3 = self::getDataGenerator()->create_user();
1463
1464 // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
1465 set_config('messagingallusers', true);
1466
1467 // Set some user preferences.
1468 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user1->id);
1469 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1470
1471 // Check the returned value for each user.
1472 $this->assertEquals(
1473 \core_message\api::MESSAGE_PRIVACY_SITE,
1474 \core_message\api::get_user_privacy_messaging_preference($user1->id)
1475 );
1476 $this->assertEquals(
1477 \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
1478 \core_message\api::get_user_privacy_messaging_preference($user2->id)
1479 );
1480 $this->assertEquals(
1481 \core_message\api::MESSAGE_PRIVACY_SITE,
1482 \core_message\api::get_user_privacy_messaging_preference($user3->id)
1483 );
1484
1485 // Disable site-wide messagging privacy setting. The user will be able to receive messages from members of their course.
1486 set_config('messagingallusers', false);
1487
1488 // Check the returned value for each user.
1489 $this->assertEquals(
1490 \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
1491 \core_message\api::get_user_privacy_messaging_preference($user1->id)
1492 );
1493 $this->assertEquals(
1494 \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
1495 \core_message\api::get_user_privacy_messaging_preference($user2->id)
1496 );
1497 $this->assertEquals(
1498 \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
1499 \core_message\api::get_user_privacy_messaging_preference($user3->id)
1500 );
1501 }
1502
1f64514d
MN
1503 /**
1504 * Tests that when blocking messages from non-contacts is enabled that
1505 * non-contacts trying to send a message return false.
1506 */
1507 public function test_is_user_non_contact_blocked() {
1508 // Create some users.
1509 $user1 = self::getDataGenerator()->create_user();
1510 $user2 = self::getDataGenerator()->create_user();
1511
1512 // Set as the first user.
1513 $this->setUser($user1);
1514
f7dfa9ba
SA
1515 // By default, user only can be messaged by contacts and members of any of his/her courses.
1516 $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
1517
1518 // Enable all users privacy messaging and check now the default user's preference has been set to allow receiving
1519 // messages from everybody.
1520 set_config('messagingallusers', true);
1521 // Check that the return result is now false because any site user can contact him/her.
1f64514d
MN
1522 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1523
1524 // Set the second user's preference to not receive messages from non-contacts.
f7dfa9ba
SA
1525 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1526 // Check that the return result is still true (because is even more restricted).
1f64514d
MN
1527 $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
1528
1529 // Add the first user as a contact for the second user.
f219eac7 1530 \core_message\api::add_contact($user2->id, $user1->id);
1f64514d
MN
1531
1532 // Check that the return result is now false.
1533 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
f219eac7 1534
f7dfa9ba
SA
1535 // Set the second user's preference to receive messages from course members.
1536 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER, $user2->id);
1537 // Check that the return result is still false (because $user1 is still his/her contact).
1538 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1f64514d
MN
1539 }
1540
1541 /**
1542 * Tests that we return true when a user is blocked, or false
1543 * if they are not blocked.
1544 */
1545 public function test_is_user_blocked() {
1546 // Create some users.
1547 $user1 = self::getDataGenerator()->create_user();
1548 $user2 = self::getDataGenerator()->create_user();
1549
1550 // Set the user.
1551 $this->setUser($user1);
1552
1553 // User shouldn't be blocked.
26dca05d 1554 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
c886e2c9 1555 $this->assertDebuggingCalled();
1f64514d
MN
1556
1557 // Block the user.
f219eac7 1558 \core_message\api::block_user($user1->id, $user2->id);
1f64514d
MN
1559
1560 // User should be blocked.
26dca05d 1561 $this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
c886e2c9 1562 $this->assertDebuggingCalled();
727145bf
AN
1563
1564 // Unblock the user.
f219eac7 1565 \core_message\api::unblock_user($user1->id, $user2->id);
727145bf 1566 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
c886e2c9 1567 $this->assertDebuggingCalled();
1f64514d
MN
1568 }
1569
1570 /**
1571 * Tests that the admin is not blocked even if someone has chosen to block them.
1572 */
1573 public function test_is_user_blocked_as_admin() {
1574 // Create a user.
1575 $user1 = self::getDataGenerator()->create_user();
1576
1577 // Set the user.
1578 $this->setUser($user1);
1579
1580 // Block the admin user.
f219eac7 1581 \core_message\api::block_user($user1->id, 2);
1f64514d
MN
1582
1583 // Now change to the admin user.
1584 $this->setAdminUser();
1585
1586 // As the admin you should still be able to send messages to the user.
26dca05d 1587 $this->assertFalse(\core_message\api::is_user_blocked($user1->id));
c886e2c9 1588 $this->assertDebuggingCalled();
1f64514d 1589 }
20ab51fd
AA
1590
1591 /*
1592 * Tes get_message_processor api.
1593 */
1594 public function test_get_message_processor() {
55b2a33c 1595 $processors = get_message_processors(true);
20ab51fd
AA
1596 if (empty($processors)) {
1597 $this->markTestSkipped("No message processors found");
1598 }
1599
33683bc8
MG
1600 $name = key($processors);
1601 $processor = current($processors);
20ab51fd
AA
1602 $testprocessor = \core_message\api::get_message_processor($name);
1603 $this->assertEquals($processor->name, $testprocessor->name);
1604 $this->assertEquals($processor->enabled, $testprocessor->enabled);
1605 $this->assertEquals($processor->available, $testprocessor->available);
1606 $this->assertEquals($processor->configured, $testprocessor->configured);
1607
1608 // Disable processor and test.
1609 \core_message\api::update_processor_status($testprocessor, 0);
1610 $testprocessor = \core_message\api::get_message_processor($name, true);
1611 $this->assertEmpty($testprocessor);
1612 $testprocessor = \core_message\api::get_message_processor($name);
1613 $this->assertEquals($processor->name, $testprocessor->name);
1614 $this->assertEquals(0, $testprocessor->enabled);
1615
1616 // Enable again and test.
1617 \core_message\api::update_processor_status($testprocessor, 1);
1618 $testprocessor = \core_message\api::get_message_processor($name, true);
1619 $this->assertEquals($processor->name, $testprocessor->name);
1620 $this->assertEquals(1, $testprocessor->enabled);
1621 $testprocessor = \core_message\api::get_message_processor($name);
1622 $this->assertEquals($processor->name, $testprocessor->name);
1623 $this->assertEquals(1, $testprocessor->enabled);
1624 }
1625
1626 /**
1627 * Test method update_processor_status.
1628 */
1629 public function test_update_processor_status() {
1630 $processors = get_message_processors();
1631 if (empty($processors)) {
1632 $this->markTestSkipped("No message processors found");
1633 }
33683bc8
MG
1634 $name = key($processors);
1635 $testprocessor = current($processors);
20ab51fd
AA
1636
1637 // Enable.
1638 \core_message\api::update_processor_status($testprocessor, 1);
1639 $testprocessor = \core_message\api::get_message_processor($name);
1640 $this->assertEquals(1, $testprocessor->enabled);
1641
1642 // Disable.
1643 \core_message\api::update_processor_status($testprocessor, 0);
1644 $testprocessor = \core_message\api::get_message_processor($name);
1645 $this->assertEquals(0, $testprocessor->enabled);
1646
1647 // Enable again.
1648 \core_message\api::update_processor_status($testprocessor, 1);
1649 $testprocessor = \core_message\api::get_message_processor($name);
1650 $this->assertEquals(1, $testprocessor->enabled);
1651 }
1652
1653 /**
1654 * Test method is_user_enabled.
1655 */
1656 public function is_user_enabled() {
1657 $processors = get_message_processors();
1658 if (empty($processors)) {
1659 $this->markTestSkipped("No message processors found");
1660 }
33683bc8
MG
1661 $name = key($processors);
1662 $testprocessor = current($processors);
20ab51fd
AA
1663
1664 // Enable.
1665 \core_message\api::update_processor_status($testprocessor, 1);
1666 $status = \core_message\api::is_processor_enabled($name);
1667 $this->assertEquals(1, $status);
1668
1669 // Disable.
1670 \core_message\api::update_processor_status($testprocessor, 0);
1671 $status = \core_message\api::is_processor_enabled($name);
1672 $this->assertEquals(0, $status);
1673
1674 // Enable again.
1675 \core_message\api::update_processor_status($testprocessor, 1);
1676 $status = \core_message\api::is_processor_enabled($name);
1677 $this->assertEquals(1, $status);
1678 }
fb1469d8
RW
1679
1680 /**
1681 * Test retrieving messages by providing a minimum timecreated value.
1682 */
ffd7798c 1683 public function test_get_messages_time_from_only() {
fb1469d8
RW
1684 // Create some users.
1685 $user1 = self::getDataGenerator()->create_user();
1686 $user2 = self::getDataGenerator()->create_user();
1687
1688 // The person doing the search.
1689 $this->setUser($user1);
1690
1691 // Send some messages back and forth.
1692 $time = 1;
1693 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1694 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1695 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1696 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1697
ffd7798c 1698 // Retrieve the messages from $time, which should be all of them.
fb1469d8
RW
1699 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
1700
1701 // Confirm the message data is correct.
1702 $this->assertEquals(4, count($messages));
1703
1704 $message1 = $messages[0];
1705 $message2 = $messages[1];
1706 $message3 = $messages[2];
1707 $message4 = $messages[3];
1708
1709 $this->assertContains('Message 1', $message1->text);
1710 $this->assertContains('Message 2', $message2->text);
1711 $this->assertContains('Message 3', $message3->text);
1712 $this->assertContains('Message 4', $message4->text);
1713
ffd7798c 1714 // Retrieve the messages from $time + 3, which should only be the 2 last messages.
fb1469d8
RW
1715 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
1716
1717 // Confirm the message data is correct.
1718 $this->assertEquals(2, count($messages));
1719
1720 $message1 = $messages[0];
1721 $message2 = $messages[1];
1722
1723 $this->assertContains('Message 3', $message1->text);
1724 $this->assertContains('Message 4', $message2->text);
1725 }
1726
1727 /**
1728 * Test retrieving messages by providing a maximum timecreated value.
1729 */
ffd7798c 1730 public function test_get_messages_time_to_only() {
fb1469d8
RW
1731 // Create some users.
1732 $user1 = self::getDataGenerator()->create_user();
1733 $user2 = self::getDataGenerator()->create_user();
1734
1735 // The person doing the search.
1736 $this->setUser($user1);
1737
1738 // Send some messages back and forth.
1739 $time = 1;
1740 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1741 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1742 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1743 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1744
ffd7798c 1745 // Retrieve the messages up until $time + 4, which should be all of them.
fb1469d8
RW
1746 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
1747
1748 // Confirm the message data is correct.
1749 $this->assertEquals(4, count($messages));
1750
1751 $message1 = $messages[0];
1752 $message2 = $messages[1];
1753 $message3 = $messages[2];
1754 $message4 = $messages[3];
1755
1756 $this->assertContains('Message 1', $message1->text);
1757 $this->assertContains('Message 2', $message2->text);
1758 $this->assertContains('Message 3', $message3->text);
1759 $this->assertContains('Message 4', $message4->text);
1760
ffd7798c 1761 // Retrieve the messages up until $time + 2, which should be the first two.
fb1469d8
RW
1762 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
1763
1764 // Confirm the message data is correct.
1765 $this->assertEquals(2, count($messages));
1766
1767 $message1 = $messages[0];
1768 $message2 = $messages[1];
1769
1770 $this->assertContains('Message 1', $message1->text);
1771 $this->assertContains('Message 2', $message2->text);
1772 }
1773
1774 /**
1775 * Test retrieving messages by providing a minimum and maximum timecreated value.
1776 */
ffd7798c 1777 public function test_get_messages_time_from_and_to() {
fb1469d8
RW
1778 // Create some users.
1779 $user1 = self::getDataGenerator()->create_user();
1780 $user2 = self::getDataGenerator()->create_user();
1781
1782 // The person doing the search.
1783 $this->setUser($user1);
1784
1785 // Send some messages back and forth.
1786 $time = 1;
1787 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1788 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1789 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1790 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1791
ffd7798c 1792 // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
fb1469d8
RW
1793 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
1794
1795 // Confirm the message data is correct.
1796 $this->assertEquals(2, count($messages));
1797
1798 $message1 = $messages[0];
1799 $message2 = $messages[1];
1800
1801 $this->assertContains('Message 2', $message1->text);
1802 $this->assertContains('Message 3', $message2->text);
1803 }
883ce421
MN
1804
1805 /**
1806 * Test returning blocked users.
1807 */
1808 public function test_get_blocked_users() {
1809 global $USER;
1810
1811 // Set this user as the admin.
1812 $this->setAdminUser();
1813
1814 // Create a user to add to the admin's contact list.
1815 $user1 = $this->getDataGenerator()->create_user();
1816 $user2 = $this->getDataGenerator()->create_user();
1817
1818 // Add users to the admin's contact list.
f219eac7 1819 \core_message\api::block_user($USER->id, $user2->id);
883ce421
MN
1820
1821 $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
1822
1823 // Block other user.
f219eac7 1824 \core_message\api::block_user($USER->id, $user1->id);
883ce421
MN
1825 $this->assertCount(2, \core_message\api::get_blocked_users($USER->id));
1826
1827 // Test deleting users.
1828 delete_user($user1);
1829 $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
1830 }
1831
1832 /**
1833 * Test returning contacts with unread message count.
1834 */
1835 public function test_get_contacts_with_unread_message_count() {
548936a6
MN
1836 global $DB;
1837
883ce421
MN
1838 $user1 = self::getDataGenerator()->create_user();
1839 $user2 = self::getDataGenerator()->create_user();
1840 $user3 = self::getDataGenerator()->create_user();
1841 $user4 = self::getDataGenerator()->create_user();
1842
1843 // Add the users to each of their contacts.
f219eac7
MN
1844 \core_message\api::add_contact($user1->id, $user2->id);
1845 \core_message\api::add_contact($user2->id, $user3->id);
883ce421
MN
1846
1847 $this->send_fake_message($user1, $user2);
1848 $this->send_fake_message($user1, $user2);
1849 $this->send_fake_message($user1, $user2);
1850 $message4id = $this->send_fake_message($user1, $user2);
1851
1852 $this->send_fake_message($user3, $user2);
1853 $message6id = $this->send_fake_message($user3, $user2);
1854 $this->send_fake_message($user3, $user2);
1855 $this->send_fake_message($user3, $user2);
1856 $this->send_fake_message($user3, $user2);
1857
1858 // Send a message that should never be included as the user is not a contact.
1859 $this->send_fake_message($user4, $user2);
1860
1861 // Get the contacts and the unread message count.
1862 $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
f7dfa9ba 1863
883ce421
MN
1864 // Confirm the size is correct.
1865 $this->assertCount(2, $messages);
1866 ksort($messages);
1867
1868 $messageinfo1 = array_shift($messages);
1869 $messageinfo2 = array_shift($messages);
f219eac7 1870
883ce421
MN
1871 $this->assertEquals($user1->id, $messageinfo1->id);
1872 $this->assertEquals(4, $messageinfo1->messagecount);
1873 $this->assertEquals($user3->id, $messageinfo2->id);
1874 $this->assertEquals(5, $messageinfo2->messagecount);
1875
1876 // Mark some of the messages as read.
548936a6
MN
1877 $m4 = $DB->get_record('messages', ['id' => $message4id]);
1878 $m6 = $DB->get_record('messages', ['id' => $message6id]);
1879 \core_message\api::mark_message_as_read($user2->id, $m4);
1880 \core_message\api::mark_message_as_read($user2->id, $m6);
883ce421
MN
1881
1882 // Get the contacts and the unread message count.
1883 $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1884
1885 // Confirm the size is correct.
1886 $this->assertCount(2, $messages);
1887 ksort($messages);
1888
1889 // Confirm read messages are not included.
1890 $messageinfo1 = array_shift($messages);
1891 $messageinfo2 = array_shift($messages);
1892 $this->assertEquals($user1->id, $messageinfo1->id);
1893 $this->assertEquals(3, $messageinfo1->messagecount);
1894 $this->assertEquals($user3->id, $messageinfo2->id);
1895 $this->assertEquals(4, $messageinfo2->messagecount);
1896
1897 // Now, let's populate the database with messages from user2 to user 1.
1898 $this->send_fake_message($user2, $user1);
1899 $this->send_fake_message($user2, $user1);
1900 $messageid = $this->send_fake_message($user2, $user1);
1901
1902 // Send a message that should never be included as the user is not a contact.
1903 $this->send_fake_message($user4, $user1);
1904
1905 // Get the contacts and the unread message count.
1906 $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
1907
1908 // Confirm the size is correct.
1909 $this->assertCount(1, $messages);
1910 $messageinfo1 = array_shift($messages);
1911 $this->assertEquals($user2->id, $messageinfo1->id);
1912 $this->assertEquals(3, $messageinfo1->messagecount);
1913
1914 // Mark the last message as read.
548936a6
MN
1915 $m = $DB->get_record('messages', ['id' => $messageid]);
1916 \core_message\api::mark_message_as_read($user1->id, $m);
883ce421
MN
1917
1918 $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
1919
1920 // Confirm the size is correct.
1921 $this->assertCount(1, $messages);
1922
1923 // Confirm read messages are not included.
1924 $messageinfo1 = array_shift($messages);
1925 $this->assertEquals($user2->id, $messageinfo1->id);
1926 $this->assertEquals(2, $messageinfo1->messagecount);
1927 }
1928
1929 /**
1930 * Test returning contacts with unread message count when there are no messages.
1931 */
1932 public function test_get_contacts_with_unread_message_count_no_messages() {
1933 $user1 = self::getDataGenerator()->create_user();
1934 $user2 = self::getDataGenerator()->create_user();
1935
1936 // Add the users to each of their contacts.
f219eac7 1937 \core_message\api::add_contact($user2->id, $user1->id);
883ce421
MN
1938
1939 // Check we get the correct message count.
1940 $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1941
1942 // Confirm the size is correct.
1943 $this->assertCount(1, $messages);
1944
1945 $messageinfo = array_shift($messages);
1946
1947 $this->assertEquals($user1->id, $messageinfo->id);
1948 $this->assertEquals(0, $messageinfo->messagecount);
1949 }
1950
1951 /**
1952 * Test returning non-contacts with unread message count.
1953 */
1954 public function test_get_non_contacts_with_unread_message_count() {
548936a6
MN
1955 global $DB;
1956
883ce421
MN
1957 $user1 = self::getDataGenerator()->create_user();
1958 $user2 = self::getDataGenerator()->create_user();
1959 $user3 = self::getDataGenerator()->create_user();
1960 $user4 = self::getDataGenerator()->create_user();
1961
1962 // Add a user to the contact list of the users we are testing this function with.
f219eac7
MN
1963 \core_message\api::add_contact($user1->id, $user4->id);
1964 \core_message\api::add_contact($user2->id, $user4->id);
883ce421
MN
1965
1966 $this->send_fake_message($user1, $user2);
1967 $this->send_fake_message($user1, $user2);
1968 $this->send_fake_message($user1, $user2);
1969 $message4id = $this->send_fake_message($user1, $user2);
1970
1971 $this->send_fake_message($user3, $user2);
1972 $message6id = $this->send_fake_message($user3, $user2);
1973 $this->send_fake_message($user3, $user2);
1974 $this->send_fake_message($user3, $user2);
1975 $this->send_fake_message($user3, $user2);
1976
1977 // Send a message that should never be included as the user is a contact.
1978 $this->send_fake_message($user4, $user2);
1979
1980 // Get the non-contacts and the unread message count.
1981 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
1982
1983 // Check we get the correct message count.
1984 ksort($messages);
1985 $this->assertCount(2, $messages);
1986 $messageinfo1 = array_shift($messages);
1987 $messageinfo2 = array_shift($messages);
1988 $this->assertEquals($user1->id, $messageinfo1->id);
1989 $this->assertEquals(4, $messageinfo1->messagecount);
1990 $this->assertEquals($user3->id, $messageinfo2->id);
1991 $this->assertEquals(5, $messageinfo2->messagecount);
1992
1993 // Mark some of the messages as read.
548936a6
MN
1994 $m4 = $DB->get_record('messages', ['id' => $message4id]);
1995 $m6 = $DB->get_record('messages', ['id' => $message6id]);
1996 \core_message\api::mark_message_as_read($user2->id, $m4);
1997 \core_message\api::mark_message_as_read($user2->id, $m6);
883ce421
MN
1998
1999 // Get the non-contacts and the unread message count.
2000 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
2001
2002 // Check the marked message is not returned in the message count.
2003 ksort($messages);
2004 $this->assertCount(2, $messages);
2005 $messageinfo1 = array_shift($messages);
2006 $messageinfo2 = array_shift($messages);
2007 $this->assertEquals($user1->id, $messageinfo1->id);
2008 $this->assertEquals(3, $messageinfo1->messagecount);
2009 $this->assertEquals($user3->id, $messageinfo2->id);
2010 $this->assertEquals(4, $messageinfo2->messagecount);
2011
2012 // Now, let's populate the database with messages from user2 to user 1.
2013 $this->send_fake_message($user2, $user1);
2014 $this->send_fake_message($user2, $user1);
2015 $messageid = $this->send_fake_message($user2, $user1);
2016
2017 // Send a message that should never be included as the user is a contact.
2018 $this->send_fake_message($user4, $user1);
2019
2020 // Get the non-contacts and the unread message count.
2021 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
2022
2023 // Confirm the size is correct.
2024 $this->assertCount(1, $messages);
2025 $messageinfo1 = array_shift($messages);
2026 $this->assertEquals($user2->id, $messageinfo1->id);
2027 $this->assertEquals(3, $messageinfo1->messagecount);
2028
2029 // Mark the last message as read.
548936a6
MN
2030 $m = $DB->get_record('messages', ['id' => $messageid]);
2031 \core_message\api::mark_message_as_read($user1->id, $m);
883ce421
MN
2032
2033 // Get the non-contacts and the unread message count.
2034 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
2035
2036 // Check the marked message is not returned in the message count.
2037 $this->assertCount(1, $messages);
2038 $messageinfo1 = array_shift($messages);
2039 $this->assertEquals($user2->id, $messageinfo1->id);
2040 $this->assertEquals(2, $messageinfo1->messagecount);
2041 }
2042
2043 /**
2044 * Test marking a message as read.
2045 */
2046 public function test_mark_message_as_read() {
2047 global $DB;
2048
2049 $user1 = self::getDataGenerator()->create_user();
2050 $user2 = self::getDataGenerator()->create_user();
2051
2052 $this->send_fake_message($user1, $user2);
2053 $m2id = $this->send_fake_message($user1, $user2);
2054 $this->send_fake_message($user2, $user1);
2055 $m4id = $this->send_fake_message($user2, $user1);
2056
548936a6
MN
2057 $m2 = $DB->get_record('messages', ['id' => $m2id]);
2058 $m4 = $DB->get_record('messages', ['id' => $m4id]);
2059 \core_message\api::mark_message_as_read($user2->id, $m2, 11);
2060 \core_message\api::mark_message_as_read($user1->id, $m4, 12);
883ce421
MN
2061
2062 // Confirm there are two user actions.
2063 $muas = $DB->get_records('message_user_actions', [], 'timecreated ASC');
2064 $this->assertEquals(2, count($muas));
2065
2066 // Confirm they are correct.
2067 $mua1 = array_shift($muas);
2068 $mua2 = array_shift($muas);
2069
2070 // Confirm first action.
2071 $this->assertEquals($user2->id, $mua1->userid);
2072 $this->assertEquals($m2id, $mua1->messageid);
2073 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua1->action);
2074 $this->assertEquals(11, $mua1->timecreated);
2075
2076 // Confirm second action.
2077 $this->assertEquals($user1->id, $mua2->userid);
2078 $this->assertEquals($m4id, $mua2->messageid);
2079 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua2->action);
2080 $this->assertEquals(12, $mua2->timecreated);
2081 }
2082
2083 /**
2084 * Test marking a notification as read.
2085 */
2086 public function test_mark_notification_as_read() {
2087 global $DB;
2088
2089 $user1 = self::getDataGenerator()->create_user();
2090 $user2 = self::getDataGenerator()->create_user();
2091
2092 $this->send_fake_message($user1, $user2, 'Notification 1', 1);
2093 $n2id = $this->send_fake_message($user1, $user2, 'Notification 2', 1);
2094 $this->send_fake_message($user2, $user1, 'Notification 3', 1);
2095 $n4id = $this->send_fake_message($user2, $user1, 'Notification 4', 1);
2096
548936a6
MN
2097 $n2 = $DB->get_record('notifications', ['id' => $n2id]);
2098 $n4 = $DB->get_record('notifications', ['id' => $n4id]);
2099
2100 \core_message\api::mark_notification_as_read($n2, 11);
2101 \core_message\api::mark_notification_as_read($n4, 12);
883ce421
MN
2102
2103 // Retrieve the notifications.
2104 $n2 = $DB->get_record('notifications', ['id' => $n2id]);
2105 $n4 = $DB->get_record('notifications', ['id' => $n4id]);
2106
2107 // Confirm they have been marked as read.
2108 $this->assertEquals(11, $n2->timeread);
2109 $this->assertEquals(12, $n4->timeread);
2110 }
2111
2112 /**
2113 * Test a conversation is not returned if there is none.
2114 */
2115 public function test_get_conversation_between_users_no_conversation() {
2116 $user1 = self::getDataGenerator()->create_user();
2117 $user2 = self::getDataGenerator()->create_user();
2118
b2cd17e6 2119 $this->assertFalse(\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
883ce421
MN
2120 }
2121
2122 /**
2123 * Test we can return a conversation that exists between users.
2124 */
2125 public function test_get_conversation_between_users_with_existing_conversation() {
2126 $user1 = self::getDataGenerator()->create_user();
2127 $user2 = self::getDataGenerator()->create_user();
2128
b2cd17e6 2129 $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
883ce421
MN
2130
2131 $this->assertEquals($conversationid,
b2cd17e6 2132 \core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
883ce421 2133 }
0b3eadcd 2134
0d203bbf
MN
2135 /**
2136 * Test can create a contact request.
2137 */
2138 public function test_can_create_contact_request() {
2139 global $CFG;
2140
2141 $user1 = self::getDataGenerator()->create_user();
2142 $user2 = self::getDataGenerator()->create_user();
2143
2144 // Disable messaging.
2145 $CFG->messaging = 0;
2146 $this->assertFalse(\core_message\api::can_create_contact($user1->id, $user2->id));
2147
2148 // Re-enable messaging.
2149 $CFG->messaging = 1;
2150
2151 // Allow users to message anyone site-wide.
2152 $CFG->messagingallusers = 1;
2153 $this->assertTrue(\core_message\api::can_create_contact($user1->id, $user2->id));
2154
2155 // Disallow users from messaging anyone site-wide.
2156 $CFG->messagingallusers = 0;
2157 $this->assertFalse(\core_message\api::can_create_contact($user1->id, $user2->id));
2158
2159 // Put the users in the same course so a contact request should be possible.
2160 $course = self::getDataGenerator()->create_course();
2161 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
2162 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
2163 $this->assertTrue(\core_message\api::can_create_contact($user1->id, $user2->id));
2164 }
2165
0b3eadcd
MN
2166 /**
2167 * Test creating a contact request.
2168 */
2169 public function test_create_contact_request() {
2170 global $DB;
2171
2172 $user1 = self::getDataGenerator()->create_user();
2173 $user2 = self::getDataGenerator()->create_user();
2174
2175 \core_message\api::create_contact_request($user1->id, $user2->id);
2176
2177 $request = $DB->get_records('message_contact_requests');
2178
2179 $this->assertCount(1, $request);
2180
2181 $request = reset($request);
2182
2183 $this->assertEquals($user1->id, $request->userid);
2184 $this->assertEquals($user2->id, $request->requesteduserid);
2185 }
2186
2187 /**
2188 * Test confirming a contact request.
2189 */
2190 public function test_confirm_contact_request() {
2191 global $DB;
2192
2193 $user1 = self::getDataGenerator()->create_user();
2194 $user2 = self::getDataGenerator()->create_user();
2195
2196 \core_message\api::create_contact_request($user1->id, $user2->id);
2197
2198 \core_message\api::confirm_contact_request($user1->id, $user2->id);
2199
2200 $this->assertEquals(0, $DB->count_records('message_contact_requests'));
2201
2202 $contact = $DB->get_records('message_contacts');
2203
2204 $this->assertCount(1, $contact);
2205
2206 $contact = reset($contact);
2207
2208 $this->assertEquals($user1->id, $contact->userid);
2209 $this->assertEquals($user2->id, $contact->contactid);
2210 }
2211
2212 /**
2213 * Test declining a contact request.
2214 */
2215 public function test_decline_contact_request() {
2216 global $DB;
2217
2218 $user1 = self::getDataGenerator()->create_user();
2219 $user2 = self::getDataGenerator()->create_user();
2220
2221 \core_message\api::create_contact_request($user1->id, $user2->id);
2222
2223 \core_message\api::decline_contact_request($user1->id, $user2->id);
2224
2225 $this->assertEquals(0, $DB->count_records('message_contact_requests'));
2226 $this->assertEquals(0, $DB->count_records('message_contacts'));
2227 }
2228
2229 /**
2230 * Test retrieving contact requests.
2231 */
2232 public function test_get_contact_requests() {
2233 $user1 = self::getDataGenerator()->create_user();
2234 $user2 = self::getDataGenerator()->create_user();
2235 $user3 = self::getDataGenerator()->create_user();
2236
2237 // Block one user, their request should not show up.
2238 \core_message\api::block_user($user1->id, $user3->id);
2239
2240 \core_message\api::create_contact_request($user2->id, $user1->id);
2241 \core_message\api::create_contact_request($user3->id, $user1->id);
2242
2243 $requests = \core_message\api::get_contact_requests($user1->id);
2244
2245 $this->assertCount(1, $requests);
2246
2247 $request = reset($requests);
2248
2249 $this->assertEquals($user2->id, $request->id);
2250 $this->assertEquals($user2->picture, $request->picture);
2251 $this->assertEquals($user2->firstname, $request->firstname);
2252 $this->assertEquals($user2->lastname, $request->lastname);
2253 $this->assertEquals($user2->firstnamephonetic, $request->firstnamephonetic);
2254 $this->assertEquals($user2->lastnamephonetic, $request->lastnamephonetic);
2255 $this->assertEquals($user2->middlename, $request->middlename);
2256 $this->assertEquals($user2->alternatename, $request->alternatename);
2257 $this->assertEquals($user2->email, $request->email);
2258 }
2259
2260 /**
2261 * Test adding contacts.
2262 */
2263 public function test_add_contact() {
2264 global $DB;
2265
2266 $user1 = self::getDataGenerator()->create_user();
2267 $user2 = self::getDataGenerator()->create_user();
2268
2269 \core_message\api::add_contact($user1->id, $user2->id);
2270
2271 $contact = $DB->get_records('message_contacts');
2272
2273 $this->assertCount(1, $contact);
2274
2275 $contact = reset($contact);
2276
2277 $this->assertEquals($user1->id, $contact->userid);
2278 $this->assertEquals($user2->id, $contact->contactid);
2279 }
2280
2281 /**
2282 * Test removing contacts.
2283 */
2284 public function test_remove_contact() {
2285 global $DB;
2286
2287 $user1 = self::getDataGenerator()->create_user();
2288 $user2 = self::getDataGenerator()->create_user();
2289
2290 \core_message\api::add_contact($user1->id, $user2->id);
2291 \core_message\api::remove_contact($user1->id, $user2->id);
2292
2293 $this->assertEquals(0, $DB->count_records('message_contacts'));
2294 }
2295
2296 /**
2297 * Test blocking users.
2298 */
2299 public function test_block_user() {
2300 global $DB;
2301
2302 $user1 = self::getDataGenerator()->create_user();
2303 $user2 = self::getDataGenerator()->create_user();
2304
2305 \core_message\api::block_user($user1->id, $user2->id);
2306
2307 $blockedusers = $DB->get_records('message_users_blocked');
2308
2309 $this->assertCount(1, $blockedusers);
2310
2311 $blockeduser = reset($blockedusers);
2312
2313 $this->assertEquals($user1->id, $blockeduser->userid);
2314 $this->assertEquals($user2->id, $blockeduser->blockeduserid);
2315 }
2316
2317 /**
2318 * Test unblocking users.
2319 */
2320 public function test_unblock_user() {
2321 global $DB;
2322
2323 $user1 = self::getDataGenerator()->create_user();
2324 $user2 = self::getDataGenerator()->create_user();
2325
2326 \core_message\api::block_user($user1->id, $user2->id);
2327 \core_message\api::unblock_user($user1->id, $user2->id);
2328
2329 $this->assertEquals(0, $DB->count_records('message_users_blocked'));
2330 }
2331
2332 /**
2333 * Test is contact check.
2334 */
2335 public function test_is_contact() {
2336 $user1 = self::getDataGenerator()->create_user();
2337 $user2 = self::getDataGenerator()->create_user();
2338 $user3 = self::getDataGenerator()->create_user();
2339
2340 \core_message\api::add_contact($user1->id, $user2->id);
2341
2342 $this->assertTrue(\core_message\api::is_contact($user1->id, $user2->id));
2343 $this->assertTrue(\core_message\api::is_contact($user2->id, $user1->id));
2344 $this->assertFalse(\core_message\api::is_contact($user2->id, $user3->id));
2345 }
2346
2347 /**
2348 * Test get contact.
2349 */
2350 public function test_get_contact() {
2351 $user1 = self::getDataGenerator()->create_user();
2352 $user2 = self::getDataGenerator()->create_user();
2353
2354 \core_message\api::add_contact($user1->id, $user2->id);
2355
2356 $contact = \core_message\api::get_contact($user1->id, $user2->id);
2357
2358 $this->assertEquals($user1->id, $contact->userid);
2359 $this->assertEquals($user2->id, $contact->contactid);
2360 }
2361
2362 /**
2363 * Test is blocked checked.
2364 */
2365 public function test_is_blocked() {
2366 $user1 = self::getDataGenerator()->create_user();
2367 $user2 = self::getDataGenerator()->create_user();
2368
2369 $this->assertFalse(\core_message\api::is_blocked($user1->id, $user2->id));
2370 $this->assertFalse(\core_message\api::is_blocked($user2->id, $user1->id));
2371
2372 \core_message\api::block_user($user1->id, $user2->id);
2373
2374 $this->assertTrue(\core_message\api::is_blocked($user1->id, $user2->id));
2375 $this->assertFalse(\core_message\api::is_blocked($user2->id, $user1->id));
2376 }
2377
2378 /**
2379 * Test the contact request exist check.
2380 */
2381 public function test_does_contact_request_exist() {
2382 $user1 = self::getDataGenerator()->create_user();
2383 $user2 = self::getDataGenerator()->create_user();
2384
2385 $this->assertFalse(\core_message\api::does_contact_request_exist($user1->id, $user2->id));
2386 $this->assertFalse(\core_message\api::does_contact_request_exist($user2->id, $user1->id));
2387
2388 \core_message\api::create_contact_request($user1->id, $user2->id);
2389
2390 $this->assertTrue(\core_message\api::does_contact_request_exist($user1->id, $user2->id));
2391 $this->assertTrue(\core_message\api::does_contact_request_exist($user2->id, $user1->id));
2392 }
f219eac7 2393
08cb8a34
MN
2394 /**
2395 * Test the user in conversation check.
2396 */
2397 public function test_is_user_in_conversation() {
2398 $user1 = self::getDataGenerator()->create_user();
2399 $user2 = self::getDataGenerator()->create_user();
2400
2401 $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
2402
2403 $this->assertTrue(\core_message\api::is_user_in_conversation($user1->id, $conversationid));
2404 }
2405
2406 /**
2407 * Test the user in conversation check when they are not.
2408 */
2409 public function test_is_user_in_conversation_when_not() {
2410 $user1 = self::getDataGenerator()->create_user();
2411 $user2 = self::getDataGenerator()->create_user();
2412 $user3 = self::getDataGenerator()->create_user();
2413
2414 $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
2415
2416 $this->assertFalse(\core_message\api::is_user_in_conversation($user3->id, $conversationid));
2417 }
2418
f219eac7
MN
2419 /**
2420 * Comparison function for sorting contacts.
2421 *
2422 * @param stdClass $a
2423 * @param stdClass $b
2424 * @return bool
2425 */
2426 protected static function sort_contacts($a, $b) {
2427 return $a->userid > $b->userid;
2428 }
5cdebf2d 2429}