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