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