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