MDL-62796 tool_policy: show popup for users who are not logged in
[moodle.git] / message / tests / api_test.php
blob60a5bb7eecf193566330842acfb00a234983ebb7
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.
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/>.
17 /**
18 * Test message API.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
32 /**
33 * Test message API.
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
40 class core_message_api_testcase extends core_message_messagelib_testcase {
42 public function test_mark_all_read_for_user_touser() {
43 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
44 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
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);
49 $this->send_fake_message($sender, $recipient);
50 $this->send_fake_message($sender, $recipient);
51 $this->send_fake_message($sender, $recipient);
53 \core_message\api::mark_all_read_for_user($recipient->id);
54 $this->assertDebuggingCalled();
55 $this->assertEquals(message_count_unread_messages($recipient), 0);
58 public function test_mark_all_read_for_user_touser_with_fromuser() {
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'));
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);
66 $this->send_fake_message($sender1, $recipient);
67 $this->send_fake_message($sender1, $recipient);
68 $this->send_fake_message($sender1, $recipient);
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);
72 $this->send_fake_message($sender2, $recipient);
73 $this->send_fake_message($sender2, $recipient);
74 $this->send_fake_message($sender2, $recipient);
76 \core_message\api::mark_all_read_for_user($recipient->id, $sender1->id);
77 $this->assertDebuggingCalled();
78 $this->assertEquals(message_count_unread_messages($recipient), 3);
81 public function test_mark_all_read_for_user_touser_with_type() {
82 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
83 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
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);
88 $this->send_fake_message($sender, $recipient);
89 $this->send_fake_message($sender, $recipient);
90 $this->send_fake_message($sender, $recipient);
92 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
93 $this->assertDebuggingCalled();
94 $this->assertEquals(message_count_unread_messages($recipient), 3);
96 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
97 $this->assertDebuggingCalled();
98 $this->assertEquals(message_count_unread_messages($recipient), 0);
102 * Test count_blocked_users.
104 public function test_count_blocked_users() {
105 // Set this user as the admin.
106 $this->setAdminUser();
108 // Create users to add to the admin's contact list.
109 $user1 = $this->getDataGenerator()->create_user();
110 $user2 = $this->getDataGenerator()->create_user();
112 $this->assertEquals(0, \core_message\api::count_blocked_users());
114 // Add 1 blocked and 1 normal contact to admin's contact list.
115 message_add_contact($user1->id);
116 message_add_contact($user2->id, 1);
118 $this->assertEquals(0, \core_message\api::count_blocked_users($user2));
119 $this->assertEquals(1, \core_message\api::count_blocked_users());
123 * Tests searching users in a course.
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);
132 // The person doing the search.
133 $this->setUser($user1);
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);
142 // Block the second user.
143 message_block_contact($user2->id, $user1->id);
145 $user3 = new stdClass();
146 $user3->firstname = 'User';
147 $user3->lastname = 'Three';
148 $user3 = self::getDataGenerator()->create_user($user3);
150 // Create a course.
151 $course1 = new stdClass();
152 $course1->fullname = 'Course';
153 $course1->shortname = 'One';
154 $course1 = $this->getDataGenerator()->create_course($course1);
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);
160 // Perform a search.
161 $results = \core_message\api::search_users_in_course($user1->id, $course1->id, 'User');
163 $this->assertEquals(1, count($results));
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);
171 $this->assertNull($user->isonline);
172 $this->assertFalse($user->isread);
173 $this->assertTrue($user->isblocked);
174 $this->assertNull($user->unreadcount);
178 * Tests searching users.
180 public function test_search_users() {
181 global $DB;
183 // Create some users.
184 $user1 = new stdClass();
185 $user1->firstname = 'User';
186 $user1->lastname = 'One';
187 $user1 = self::getDataGenerator()->create_user($user1);
189 // Set as the user performing the search.
190 $this->setUser($user1);
192 $user2 = new stdClass();
193 $user2->firstname = 'User search';
194 $user2->lastname = 'Two';
195 $user2 = self::getDataGenerator()->create_user($user2);
197 $user3 = new stdClass();
198 $user3->firstname = 'User search';
199 $user3->lastname = 'Three';
200 $user3 = self::getDataGenerator()->create_user($user3);
202 $user4 = new stdClass();
203 $user4->firstname = 'User';
204 $user4->lastname = 'Four';
205 $user4 = self::getDataGenerator()->create_user($user4);
207 $user5 = new stdClass();
208 $user5->firstname = 'User search';
209 $user5->lastname = 'Five';
210 $user5 = self::getDataGenerator()->create_user($user5);
212 $user6 = new stdClass();
213 $user6->firstname = 'User';
214 $user6->lastname = 'Six';
215 $user6 = self::getDataGenerator()->create_user($user6);
217 // Create some courses.
218 $course1 = new stdClass();
219 $course1->fullname = 'Course search';
220 $course1->shortname = 'One';
221 $course1 = $this->getDataGenerator()->create_course($course1);
223 $course2 = new stdClass();
224 $course2->fullname = 'Course';
225 $course2->shortname = 'Two';
226 $course2 = $this->getDataGenerator()->create_course($course2);
228 $course3 = new stdClass();
229 $course3->fullname = 'Course';
230 $course3->shortname = 'Three search';
231 $course3 = $this->getDataGenerator()->create_course($course3);
233 $course4 = new stdClass();
234 $course4->fullname = 'Course Four';
235 $course4->shortname = 'CF100';
236 $course4 = $this->getDataGenerator()->create_course($course4);
238 $course5 = new stdClass();
239 $course5->fullname = 'Course';
240 $course5->shortname = 'Five search';
241 $course5 = $this->getDataGenerator()->create_course($course5);
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);
249 // Add some users as contacts.
250 message_add_contact($user2->id, 0, $user1->id);
251 message_add_contact($user3->id, 0, $user1->id);
252 message_add_contact($user4->id, 0, $user1->id);
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);
257 $course5context->mark_dirty();
259 // Perform a search.
260 list($contacts, $courses, $noncontacts) = \core_message\api::search_users($user1->id, 'search');
262 // Check that we retrieved the correct contacts.
263 $this->assertEquals(2, count($contacts));
264 $this->assertEquals($user3->id, $contacts[0]->userid);
265 $this->assertEquals($user2->id, $contacts[1]->userid);
267 // Check that we retrieved the correct courses.
268 $this->assertEquals(2, count($courses));
269 $this->assertEquals($course3->id, $courses[0]->id);
270 $this->assertEquals($course1->id, $courses[1]->id);
272 // Check that we retrieved the correct non-contacts.
273 $this->assertEquals(1, count($noncontacts));
274 $this->assertEquals($user5->id, $noncontacts[0]->userid);
278 * Tests searching messages.
280 public function test_search_messages() {
281 // Create some users.
282 $user1 = self::getDataGenerator()->create_user();
283 $user2 = self::getDataGenerator()->create_user();
285 // The person doing the search.
286 $this->setUser($user1);
288 // Send some messages back and forth.
289 $time = 1;
290 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time);
291 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 1);
292 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
293 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 3);
295 // Perform a search.
296 $messages = \core_message\api::search_messages($user1->id, 'o');
298 // Confirm the data is correct.
299 $this->assertEquals(2, count($messages));
301 $message1 = $messages[0];
302 $message2 = $messages[1];
304 $this->assertEquals($user2->id, $message1->userid);
305 $this->assertEquals($user2->id, $message1->useridfrom);
306 $this->assertEquals(fullname($user2), $message1->fullname);
307 $this->assertTrue($message1->ismessaging);
308 $this->assertEquals('Word.', $message1->lastmessage);
309 $this->assertNotEmpty($message1->messageid);
310 $this->assertNull($message1->isonline);
311 $this->assertFalse($message1->isread);
312 $this->assertFalse($message1->isblocked);
313 $this->assertNull($message1->unreadcount);
315 $this->assertEquals($user2->id, $message2->userid);
316 $this->assertEquals($user1->id, $message2->useridfrom);
317 $this->assertEquals(fullname($user2), $message2->fullname);
318 $this->assertTrue($message2->ismessaging);
319 $this->assertEquals('Yo!', $message2->lastmessage);
320 $this->assertNotEmpty($message2->messageid);
321 $this->assertNull($message2->isonline);
322 $this->assertTrue($message2->isread);
323 $this->assertFalse($message2->isblocked);
324 $this->assertNull($message2->unreadcount);
328 * Tests retrieving conversations.
330 public function test_get_conversations() {
331 // Create some users.
332 $user1 = self::getDataGenerator()->create_user();
333 $user2 = self::getDataGenerator()->create_user();
334 $user3 = self::getDataGenerator()->create_user();
335 $user4 = self::getDataGenerator()->create_user();
337 // The person doing the search.
338 $this->setUser($user1);
340 // No conversations yet.
341 $this->assertEquals([], \core_message\api::get_conversations($user1->id));
343 // Send some messages back and forth, have some different conversations with different users.
344 $time = 1;
345 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
346 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
347 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
348 $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
350 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
351 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
352 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
353 $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
355 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
356 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
357 $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
359 // Retrieve the conversations.
360 $conversations = \core_message\api::get_conversations($user1->id);
362 // Confirm the data is correct.
363 $this->assertEquals(3, count($conversations));
365 $message1 = array_shift($conversations);
366 $message2 = array_shift($conversations);
367 $message3 = array_shift($conversations);
369 $this->assertEquals($user4->id, $message1->userid);
370 $this->assertEquals($user1->id, $message1->useridfrom);
371 $this->assertTrue($message1->ismessaging);
372 $this->assertEquals('Dope.', $message1->lastmessage);
373 $this->assertEquals($messageid3, $message1->messageid);
374 $this->assertNull($message1->isonline);
375 $this->assertFalse($message1->isread);
376 $this->assertFalse($message1->isblocked);
377 $this->assertEquals(1, $message1->unreadcount);
379 $this->assertEquals($user3->id, $message2->userid);
380 $this->assertEquals($user3->id, $message2->useridfrom);
381 $this->assertTrue($message2->ismessaging);
382 $this->assertEquals('Cool.', $message2->lastmessage);
383 $this->assertEquals($messageid2, $message2->messageid);
384 $this->assertNull($message2->isonline);
385 $this->assertFalse($message2->isread);
386 $this->assertFalse($message2->isblocked);
387 $this->assertEquals(2, $message2->unreadcount);
389 $this->assertEquals($user2->id, $message3->userid);
390 $this->assertEquals($user2->id, $message3->useridfrom);
391 $this->assertTrue($message3->ismessaging);
392 $this->assertEquals('Word.', $message3->lastmessage);
393 $this->assertEquals($messageid1, $message3->messageid);
394 $this->assertNull($message3->isonline);
395 $this->assertFalse($message3->isread);
396 $this->assertFalse($message3->isblocked);
397 $this->assertEquals(2, $message3->unreadcount);
401 * Tests retrieving conversations with a limit and offset to ensure pagination works correctly.
403 public function test_get_conversations_limit_offset() {
404 // Create some users.
405 $user1 = self::getDataGenerator()->create_user();
406 $user2 = self::getDataGenerator()->create_user();
407 $user3 = self::getDataGenerator()->create_user();
408 $user4 = self::getDataGenerator()->create_user();
410 // The person doing the search.
411 $this->setUser($user1);
413 // Send some messages back and forth, have some different conversations with different users.
414 $time = 1;
415 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
416 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
417 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
418 $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
420 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
421 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
422 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
423 $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
425 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
426 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
427 $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
429 // Retrieve the conversations.
430 $conversations = \core_message\api::get_conversations($user1->id, 1, 1);
432 // We should only have one conversation because of the limit.
433 $this->assertCount(1, $conversations);
435 $conversation = array_shift($conversations);
437 $this->assertEquals($user3->id, $conversation->userid);
438 $this->assertEquals($user3->id, $conversation->useridfrom);
439 $this->assertTrue($conversation->ismessaging);
440 $this->assertEquals('Cool.', $conversation->lastmessage);
441 $this->assertEquals($messageid2, $conversation->messageid);
442 $this->assertNull($conversation->isonline);
443 $this->assertFalse($conversation->isread);
444 $this->assertFalse($conversation->isblocked);
445 $this->assertEquals(2, $conversation->unreadcount);
447 // Retrieve the next conversation.
448 $conversations = \core_message\api::get_conversations($user1->id, 2, 1);
450 // We should only have one conversation because of the limit.
451 $this->assertCount(1, $conversations);
453 $conversation = array_shift($conversations);
455 $this->assertEquals($user2->id, $conversation->userid);
456 $this->assertEquals($user2->id, $conversation->useridfrom);
457 $this->assertTrue($conversation->ismessaging);
458 $this->assertEquals('Word.', $conversation->lastmessage);
459 $this->assertEquals($messageid1, $conversation->messageid);
460 $this->assertNull($conversation->isonline);
461 $this->assertFalse($conversation->isread);
462 $this->assertFalse($conversation->isblocked);
463 $this->assertEquals(2, $conversation->unreadcount);
465 // Ask for an offset that doesn't exist.
466 $conversations = \core_message\api::get_conversations($user1->id, 4, 1);
468 // We should not get any conversations back.
469 $this->assertCount(0, $conversations);
473 * Tests retrieving conversations when a conversation contains a deleted user.
475 public function test_get_conversations_with_deleted_user() {
476 // Create some users.
477 $user1 = self::getDataGenerator()->create_user();
478 $user2 = self::getDataGenerator()->create_user();
479 $user3 = self::getDataGenerator()->create_user();
481 // Send some messages back and forth, have some different conversations with different users.
482 $time = 1;
483 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
484 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
485 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
486 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
488 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
489 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
490 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
491 $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
493 // Delete the second user.
494 delete_user($user2);
496 // Retrieve the conversations.
497 $conversations = \core_message\api::get_conversations($user1->id);
499 // We should only have one conversation because the other user was deleted.
500 $this->assertCount(1, $conversations);
502 // Confirm the conversation is from the non-deleted user.
503 $conversation = reset($conversations);
504 $this->assertEquals($user3->id, $conversation->userid);
508 * The data provider for get_conversations_mixed.
510 * This provides sets of data to for testing.
511 * @return array
513 public function get_conversations_mixed_provider() {
514 return array(
515 'Test that conversations with messages contacts is correctly ordered.' => array(
516 'users' => array(
517 'user1',
518 'user2',
519 'user3',
521 'contacts' => array(
523 'messages' => array(
524 array(
525 'from' => 'user1',
526 'to' => 'user2',
527 'state' => 'unread',
528 'subject' => 'S1',
530 array(
531 'from' => 'user2',
532 'to' => 'user1',
533 'state' => 'unread',
534 'subject' => 'S2',
536 array(
537 'from' => 'user1',
538 'to' => 'user2',
539 'state' => 'unread',
540 'timecreated' => 0,
541 'subject' => 'S3',
543 array(
544 'from' => 'user1',
545 'to' => 'user3',
546 'state' => 'read',
547 'timemodifier' => 1,
548 'subject' => 'S4',
550 array(
551 'from' => 'user3',
552 'to' => 'user1',
553 'state' => 'read',
554 'timemodifier' => 1,
555 'subject' => 'S5',
557 array(
558 'from' => 'user1',
559 'to' => 'user3',
560 'state' => 'read',
561 'timecreated' => 0,
562 'subject' => 'S6',
565 'expectations' => array(
566 'user1' => array(
567 // User1 has conversed most recently with user3. The most recent message is M5.
568 array(
569 'messageposition' => 0,
570 'with' => 'user3',
571 'subject' => 'S5',
572 'unreadcount' => 0,
574 // User1 has also conversed with user2. The most recent message is S2.
575 array(
576 'messageposition' => 1,
577 'with' => 'user2',
578 'subject' => 'S2',
579 'unreadcount' => 1,
582 'user2' => array(
583 // User2 has only conversed with user1. Their most recent shared message was S2.
584 array(
585 'messageposition' => 0,
586 'with' => 'user1',
587 'subject' => 'S2',
588 'unreadcount' => 2,
591 'user3' => array(
592 // User3 has only conversed with user1. Their most recent shared message was S5.
593 array(
594 'messageposition' => 0,
595 'with' => 'user1',
596 'subject' => 'S5',
597 'unreadcount' => 0,
602 'Test that users with contacts and messages to self work as expected' => array(
603 'users' => array(
604 'user1',
605 'user2',
606 'user3',
608 'contacts' => array(
609 'user1' => array(
610 'user2' => 0,
611 'user3' => 0,
613 'user2' => array(
614 'user3' => 0,
617 'messages' => array(
618 array(
619 'from' => 'user1',
620 'to' => 'user1',
621 'state' => 'unread',
622 'subject' => 'S1',
624 array(
625 'from' => 'user1',
626 'to' => 'user1',
627 'state' => 'unread',
628 'subject' => 'S2',
631 'expectations' => array(
632 'user1' => array(
633 // User1 has conversed most recently with user1. The most recent message is S2.
634 array(
635 'messageposition' => 0,
636 'with' => 'user1',
637 'subject' => 'S2',
638 'unreadcount' => 0, // Messages sent to and from the same user are counted as read.
643 'Test conversations with a single user, where some messages are read and some are not.' => array(
644 'users' => array(
645 'user1',
646 'user2',
648 'contacts' => array(
650 'messages' => array(
651 array(
652 'from' => 'user1',
653 'to' => 'user2',
654 'state' => 'read',
655 'subject' => 'S1',
657 array(
658 'from' => 'user2',
659 'to' => 'user1',
660 'state' => 'read',
661 'subject' => 'S2',
663 array(
664 'from' => 'user1',
665 'to' => 'user2',
666 'state' => 'unread',
667 'timemodifier' => 1,
668 'subject' => 'S3',
670 array(
671 'from' => 'user1',
672 'to' => 'user2',
673 'state' => 'unread',
674 'timemodifier' => 1,
675 'subject' => 'S4',
678 'expectations' => array(
679 // The most recent message between user1 and user2 was S4.
680 'user1' => array(
681 array(
682 'messageposition' => 0,
683 'with' => 'user2',
684 'subject' => 'S4',
685 'unreadcount' => 0,
688 'user2' => array(
689 // The most recent message between user1 and user2 was S4.
690 array(
691 'messageposition' => 0,
692 'with' => 'user1',
693 'subject' => 'S4',
694 'unreadcount' => 2,
699 'Test conversations with a single user, where some messages are read and some are not, and messages ' .
700 'are out of order' => array(
701 // This can happen through a combination of factors including multi-master DB replication with messages
702 // read somehow (e.g. API).
703 'users' => array(
704 'user1',
705 'user2',
707 'contacts' => array(
709 'messages' => array(
710 array(
711 'from' => 'user1',
712 'to' => 'user2',
713 'state' => 'read',
714 'subject' => 'S1',
715 'timemodifier' => 1,
717 array(
718 'from' => 'user2',
719 'to' => 'user1',
720 'state' => 'read',
721 'subject' => 'S2',
722 'timemodifier' => 2,
724 array(
725 'from' => 'user1',
726 'to' => 'user2',
727 'state' => 'unread',
728 'subject' => 'S3',
730 array(
731 'from' => 'user1',
732 'to' => 'user2',
733 'state' => 'unread',
734 'subject' => 'S4',
737 'expectations' => array(
738 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
739 'user1' => array(
740 array(
741 'messageposition' => 0,
742 'with' => 'user2',
743 'subject' => 'S2',
744 'unreadcount' => 0,
747 'user2' => array(
748 array(
749 'messageposition' => 0,
750 'with' => 'user1',
751 'subject' => 'S2',
752 'unreadcount' => 2
757 'Test unread message count is correct for both users' => array(
758 'users' => array(
759 'user1',
760 'user2',
762 'contacts' => array(
764 'messages' => array(
765 array(
766 'from' => 'user1',
767 'to' => 'user2',
768 'state' => 'read',
769 'subject' => 'S1',
770 'timemodifier' => 1,
772 array(
773 'from' => 'user2',
774 'to' => 'user1',
775 'state' => 'read',
776 'subject' => 'S2',
777 'timemodifier' => 2,
779 array(
780 'from' => 'user1',
781 'to' => 'user2',
782 'state' => 'read',
783 'subject' => 'S3',
784 'timemodifier' => 3,
786 array(
787 'from' => 'user1',
788 'to' => 'user2',
789 'state' => 'read',
790 'subject' => 'S4',
791 'timemodifier' => 4,
793 array(
794 'from' => 'user1',
795 'to' => 'user2',
796 'state' => 'unread',
797 'subject' => 'S5',
798 'timemodifier' => 5,
800 array(
801 'from' => 'user2',
802 'to' => 'user1',
803 'state' => 'unread',
804 'subject' => 'S6',
805 'timemodifier' => 6,
807 array(
808 'from' => 'user1',
809 'to' => 'user2',
810 'state' => 'unread',
811 'subject' => 'S7',
812 'timemodifier' => 7,
814 array(
815 'from' => 'user1',
816 'to' => 'user2',
817 'state' => 'unread',
818 'subject' => 'S8',
819 'timemodifier' => 8,
822 'expectations' => array(
823 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
824 'user1' => array(
825 array(
826 'messageposition' => 0,
827 'with' => 'user2',
828 'subject' => 'S8',
829 'unreadcount' => 1,
832 'user2' => array(
833 array(
834 'messageposition' => 0,
835 'with' => 'user1',
836 'subject' => 'S8',
837 'unreadcount' => 3,
846 * Test get_conversations with a mixture of messages.
848 * @dataProvider get_conversations_mixed_provider
849 * @param array $usersdata The list of users to create for this test.
850 * @param array $messagesdata The list of messages to create.
851 * @param array $expectations The list of expected outcomes.
853 public function test_get_conversations_mixed($usersdata, $contacts, $messagesdata, $expectations) {
854 global $DB;
856 // Create all of the users.
857 $users = array();
858 foreach ($usersdata as $username) {
859 $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
862 foreach ($contacts as $username => $contact) {
863 foreach ($contact as $contactname => $blocked) {
864 $record = new stdClass();
865 $record->userid = $users[$username]->id;
866 $record->contactid = $users[$contactname]->id;
867 $record->blocked = $blocked;
868 $record->id = $DB->insert_record('message_contacts', $record);
872 $defaulttimecreated = time();
873 foreach ($messagesdata as $messagedata) {
874 $from = $users[$messagedata['from']];
875 $to = $users[$messagedata['to']];
876 $subject = $messagedata['subject'];
878 if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
879 $messageid = $this->send_fake_message($from, $to, $subject);
880 } else {
881 // If there is no state, or the state is not 'unread', assume the message is read.
882 $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN);
885 $updatemessage = new stdClass();
886 $updatemessage->id = $messageid;
887 if (isset($messagedata['timecreated'])) {
888 $updatemessage->timecreated = $messagedata['timecreated'];
889 } else if (isset($messagedata['timemodifier'])) {
890 $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier'];
891 } else {
892 $updatemessage->timecreated = $defaulttimecreated;
895 $DB->update_record('messages', $updatemessage);
898 foreach ($expectations as $username => $data) {
899 // Get the recent conversations for the specified user.
900 $user = $users[$username];
901 $conversations = array_values(\core_message\api::get_conversations($user->id));
902 foreach ($data as $expectation) {
903 $otheruser = $users[$expectation['with']];
904 $conversation = $conversations[$expectation['messageposition']];
905 $this->assertEquals($otheruser->id, $conversation->userid);
906 $this->assertEquals($expectation['subject'], $conversation->lastmessage);
907 $this->assertEquals($expectation['unreadcount'], $conversation->unreadcount);
913 * Tests retrieving contacts.
915 public function test_get_contacts() {
916 // Create some users.
917 $user1 = self::getDataGenerator()->create_user();
919 // Set as the user.
920 $this->setUser($user1);
922 $user2 = new stdClass();
923 $user2->firstname = 'User';
924 $user2->lastname = 'A';
925 $user2 = self::getDataGenerator()->create_user($user2);
927 $user3 = new stdClass();
928 $user3->firstname = 'User';
929 $user3->lastname = 'B';
930 $user3 = self::getDataGenerator()->create_user($user3);
932 $user4 = new stdClass();
933 $user4->firstname = 'User';
934 $user4->lastname = 'C';
935 $user4 = self::getDataGenerator()->create_user($user4);
937 $user5 = new stdClass();
938 $user5->firstname = 'User';
939 $user5->lastname = 'D';
940 $user5 = self::getDataGenerator()->create_user($user5);
942 // Add some users as contacts.
943 message_add_contact($user2->id, 0, $user1->id);
944 message_add_contact($user3->id, 0, $user1->id);
945 message_add_contact($user4->id, 0, $user1->id);
947 // Retrieve the contacts.
948 $contacts = \core_message\api::get_contacts($user1->id);
950 // Confirm the data is correct.
951 $this->assertEquals(3, count($contacts));
953 $contact1 = $contacts[0];
954 $contact2 = $contacts[1];
955 $contact3 = $contacts[2];
957 $this->assertEquals($user2->id, $contact1->userid);
958 $this->assertEmpty($contact1->useridfrom);
959 $this->assertFalse($contact1->ismessaging);
960 $this->assertNull($contact1->lastmessage);
961 $this->assertNull($contact1->messageid);
962 $this->assertNull($contact1->isonline);
963 $this->assertFalse($contact1->isread);
964 $this->assertFalse($contact1->isblocked);
965 $this->assertNull($contact1->unreadcount);
967 $this->assertEquals($user3->id, $contact2->userid);
968 $this->assertEmpty($contact2->useridfrom);
969 $this->assertFalse($contact2->ismessaging);
970 $this->assertNull($contact2->lastmessage);
971 $this->assertNull($contact2->messageid);
972 $this->assertNull($contact2->isonline);
973 $this->assertFalse($contact2->isread);
974 $this->assertFalse($contact2->isblocked);
975 $this->assertNull($contact2->unreadcount);
977 $this->assertEquals($user4->id, $contact3->userid);
978 $this->assertEmpty($contact3->useridfrom);
979 $this->assertFalse($contact3->ismessaging);
980 $this->assertNull($contact3->lastmessage);
981 $this->assertNull($contact3->messageid);
982 $this->assertNull($contact3->isonline);
983 $this->assertFalse($contact3->isread);
984 $this->assertFalse($contact3->isblocked);
985 $this->assertNull($contact3->unreadcount);
989 * Tests retrieving messages.
991 public function test_get_messages() {
992 // Create some users.
993 $user1 = self::getDataGenerator()->create_user();
994 $user2 = self::getDataGenerator()->create_user();
996 // The person doing the search.
997 $this->setUser($user1);
999 // Send some messages back and forth.
1000 $time = 1;
1001 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1002 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1003 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1004 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1006 // Retrieve the messages.
1007 $messages = \core_message\api::get_messages($user1->id, $user2->id);
1009 // Confirm the message data is correct.
1010 $this->assertEquals(4, count($messages));
1012 $message1 = $messages[0];
1013 $message2 = $messages[1];
1014 $message3 = $messages[2];
1015 $message4 = $messages[3];
1017 $this->assertEquals($user1->id, $message1->useridfrom);
1018 $this->assertEquals($user2->id, $message1->useridto);
1019 $this->assertTrue($message1->displayblocktime);
1020 $this->assertContains('Yo!', $message1->text);
1022 $this->assertEquals($user2->id, $message2->useridfrom);
1023 $this->assertEquals($user1->id, $message2->useridto);
1024 $this->assertFalse($message2->displayblocktime);
1025 $this->assertContains('Sup mang?', $message2->text);
1027 $this->assertEquals($user1->id, $message3->useridfrom);
1028 $this->assertEquals($user2->id, $message3->useridto);
1029 $this->assertFalse($message3->displayblocktime);
1030 $this->assertContains('Writing PHPUnit tests!', $message3->text);
1032 $this->assertEquals($user2->id, $message4->useridfrom);
1033 $this->assertEquals($user1->id, $message4->useridto);
1034 $this->assertFalse($message4->displayblocktime);
1035 $this->assertContains('Word.', $message4->text);
1039 * Tests retrieving most recent message.
1041 public function test_get_most_recent_message() {
1042 // Create some users.
1043 $user1 = self::getDataGenerator()->create_user();
1044 $user2 = self::getDataGenerator()->create_user();
1046 // The person doing the search.
1047 $this->setUser($user1);
1049 // Send some messages back and forth.
1050 $time = 1;
1051 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1052 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1053 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1054 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1056 // Retrieve the most recent messages.
1057 $message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
1059 // Check the results are correct.
1060 $this->assertEquals($user2->id, $message->useridfrom);
1061 $this->assertEquals($user1->id, $message->useridto);
1062 $this->assertContains('Word.', $message->text);
1066 * Tests retrieving a user's profile.
1068 public function test_get_profile() {
1069 // Create some users.
1070 $user1 = self::getDataGenerator()->create_user();
1072 $user2 = new stdClass();
1073 $user2->country = 'AU';
1074 $user2->city = 'Perth';
1075 $user2 = self::getDataGenerator()->create_user($user2);
1077 // The person doing the search.
1078 $this->setUser($user1);
1080 // Get the profile.
1081 $profile = \core_message\api::get_profile($user1->id, $user2->id);
1083 $this->assertEquals($user2->id, $profile->userid);
1084 $this->assertEmpty($profile->email);
1085 $this->assertEmpty($profile->country);
1086 $this->assertEmpty($profile->city);
1087 $this->assertEquals(fullname($user2), $profile->fullname);
1088 $this->assertNull($profile->isonline);
1089 $this->assertFalse($profile->isblocked);
1090 $this->assertFalse($profile->iscontact);
1094 * Tests retrieving a user's profile.
1096 public function test_get_profile_as_admin() {
1097 // The person doing the search.
1098 $this->setAdminUser();
1100 // Create some users.
1101 $user1 = self::getDataGenerator()->create_user();
1103 $user2 = new stdClass();
1104 $user2->country = 'AU';
1105 $user2->city = 'Perth';
1106 $user2 = self::getDataGenerator()->create_user($user2);
1108 // Get the profile.
1109 $profile = \core_message\api::get_profile($user1->id, $user2->id);
1111 $this->assertEquals($user2->id, $profile->userid);
1112 $this->assertEquals($user2->email, $profile->email);
1113 $this->assertEquals($user2->country, $profile->country);
1114 $this->assertEquals($user2->city, $profile->city);
1115 $this->assertEquals(fullname($user2), $profile->fullname);
1116 $this->assertFalse($profile->isonline);
1117 $this->assertFalse($profile->isblocked);
1118 $this->assertFalse($profile->iscontact);
1122 * Tests checking if a user can delete a conversation.
1124 public function test_can_delete_conversation() {
1125 // Set as the admin.
1126 $this->setAdminUser();
1128 // Create some users.
1129 $user1 = self::getDataGenerator()->create_user();
1130 $user2 = self::getDataGenerator()->create_user();
1132 // The admin can do anything.
1133 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id));
1135 // Set as the user 1.
1136 $this->setUser($user1);
1138 // They can delete their own messages.
1139 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id));
1141 // They can't delete someone elses.
1142 $this->assertFalse(\core_message\api::can_delete_conversation($user2->id));
1146 * Tests deleting a conversation.
1148 public function test_delete_conversation() {
1149 global $DB;
1151 // Create some users.
1152 $user1 = self::getDataGenerator()->create_user();
1153 $user2 = self::getDataGenerator()->create_user();
1155 // The person doing the search.
1156 $this->setUser($user1);
1158 // Send some messages back and forth.
1159 $time = 1;
1160 $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1161 $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1162 $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1163 $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1165 // Delete the conversation as user 1.
1166 \core_message\api::delete_conversation($user1->id, $user2->id);
1168 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
1169 $this->assertCount(4, $muas);
1170 // Sort by id.
1171 ksort($muas);
1173 $mua1 = array_shift($muas);
1174 $mua2 = array_shift($muas);
1175 $mua3 = array_shift($muas);
1176 $mua4 = array_shift($muas);
1178 $this->assertEquals($user1->id, $mua1->userid);
1179 $this->assertEquals($m1id, $mua1->messageid);
1180 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
1182 $this->assertEquals($user1->id, $mua2->userid);
1183 $this->assertEquals($m2id, $mua2->messageid);
1184 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
1186 $this->assertEquals($user1->id, $mua3->userid);
1187 $this->assertEquals($m3id, $mua3->messageid);
1188 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
1190 $this->assertEquals($user1->id, $mua4->userid);
1191 $this->assertEquals($m4id, $mua4->messageid);
1192 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
1196 * Tests counting unread conversations.
1198 public function test_count_unread_conversations() {
1199 $this->resetAfterTest(true);
1201 // Create some users.
1202 $user1 = self::getDataGenerator()->create_user();
1203 $user2 = self::getDataGenerator()->create_user();
1204 $user3 = self::getDataGenerator()->create_user();
1205 $user4 = self::getDataGenerator()->create_user();
1207 // The person wanting the conversation count.
1208 $this->setUser($user1);
1210 // Send some messages back and forth, have some different conversations with different users.
1211 $this->send_fake_message($user1, $user2, 'Yo!');
1212 $this->send_fake_message($user2, $user1, 'Sup mang?');
1213 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
1214 $this->send_fake_message($user2, $user1, 'Word.');
1216 $this->send_fake_message($user1, $user3, 'Booyah');
1217 $this->send_fake_message($user3, $user1, 'Whaaat?');
1218 $this->send_fake_message($user1, $user3, 'Nothing.');
1219 $this->send_fake_message($user3, $user1, 'Cool.');
1221 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
1222 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
1223 $this->send_fake_message($user1, $user4, 'Dope.');
1225 // Check the amount for the current user.
1226 $this->assertEquals(3, core_message\api::count_unread_conversations());
1228 // Check the amount for the second user.
1229 $this->assertEquals(1, core_message\api::count_unread_conversations($user2));
1233 * Tests deleting a conversation.
1235 public function test_get_all_message_preferences() {
1236 $user = self::getDataGenerator()->create_user();
1237 $this->setUser($user);
1239 // Set a couple of preferences to test.
1240 set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
1241 set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
1243 $processors = get_message_processors();
1244 $providers = message_get_providers_for_user($user->id);
1245 $prefs = \core_message\api::get_all_message_preferences($processors, $providers, $user);
1247 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedin['popup']);
1248 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']);
1252 * Tests the user can post a message.
1254 public function test_can_post_message() {
1255 // Create some users.
1256 $user1 = self::getDataGenerator()->create_user();
1257 $user2 = self::getDataGenerator()->create_user();
1259 // Set as the user 1.
1260 $this->setUser($user1);
1262 // They can post to someone else.
1263 $this->assertTrue(\core_message\api::can_post_message($user2));
1267 * Tests the user can't post a message without proper capability.
1269 public function test_can_post_message_without_cap() {
1270 global $DB;
1272 // Create some users.
1273 $user1 = self::getDataGenerator()->create_user();
1274 $user2 = self::getDataGenerator()->create_user();
1276 // Set as the user 1.
1277 $this->setUser($user1);
1279 // Remove the capability to send a message.
1280 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
1281 unassign_capability('moodle/site:sendmessage', $roleids['user'],
1282 context_system::instance());
1284 // Check that we can not post a message without the capability.
1285 $this->assertFalse(\core_message\api::can_post_message($user2));
1289 * Tests the user can't post a message if they are not a contact and the user
1290 * has requested messages only from contacts.
1292 public function test_can_post_message_when_not_contact() {
1293 // Create some users.
1294 $user1 = self::getDataGenerator()->create_user();
1295 $user2 = self::getDataGenerator()->create_user();
1297 // Set as the first user.
1298 $this->setUser($user1);
1300 // Set the second user's preference to not receive messages from non-contacts.
1301 set_user_preference('message_blocknoncontacts', 1, $user2->id);
1303 // Check that we can not send user 2 a message.
1304 $this->assertFalse(\core_message\api::can_post_message($user2));
1308 * Tests the user can't post a message if they are blocked.
1310 public function test_can_post_message_when_blocked() {
1311 // Create some users.
1312 $user1 = self::getDataGenerator()->create_user();
1313 $user2 = self::getDataGenerator()->create_user();
1315 // Set the user.
1316 $this->setUser($user1);
1318 // Block the second user.
1319 message_block_contact($user2->id);
1321 // Check that the second user can no longer send the first user a message.
1322 $this->assertFalse(\core_message\api::can_post_message($user1, $user2));
1326 * Tests that when blocking messages from non-contacts is enabled that
1327 * non-contacts trying to send a message return false.
1329 public function test_is_user_non_contact_blocked() {
1330 // Create some users.
1331 $user1 = self::getDataGenerator()->create_user();
1332 $user2 = self::getDataGenerator()->create_user();
1334 // Set as the first user.
1335 $this->setUser($user1);
1337 // User hasn't sent their preference to block non-contacts, so should return false.
1338 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1340 // Set the second user's preference to not receive messages from non-contacts.
1341 set_user_preference('message_blocknoncontacts', 1, $user2->id);
1343 // Check that the return result is now true.
1344 $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
1346 // Add the first user as a contact for the second user.
1347 message_add_contact($user1->id, 0, $user2->id);
1349 // Check that the return result is now false.
1350 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1354 * Tests that we return true when a user is blocked, or false
1355 * if they are not blocked.
1357 public function test_is_user_blocked() {
1358 // Create some users.
1359 $user1 = self::getDataGenerator()->create_user();
1360 $user2 = self::getDataGenerator()->create_user();
1362 // Set the user.
1363 $this->setUser($user1);
1365 // User shouldn't be blocked.
1366 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1368 // Block the user.
1369 message_block_contact($user2->id);
1371 // User should be blocked.
1372 $this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
1374 // Unblock the user.
1375 message_unblock_contact($user2->id);
1376 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1380 * Tests that the admin is not blocked even if someone has chosen to block them.
1382 public function test_is_user_blocked_as_admin() {
1383 // Create a user.
1384 $user1 = self::getDataGenerator()->create_user();
1386 // Set the user.
1387 $this->setUser($user1);
1389 // Block the admin user.
1390 message_block_contact(2);
1392 // Now change to the admin user.
1393 $this->setAdminUser();
1395 // As the admin you should still be able to send messages to the user.
1396 $this->assertFalse(\core_message\api::is_user_blocked($user1->id));
1400 * Tes get_message_processor api.
1402 public function test_get_message_processor() {
1403 $processors = get_message_processors(true);
1404 if (empty($processors)) {
1405 $this->markTestSkipped("No message processors found");
1408 $name = key($processors);
1409 $processor = current($processors);
1410 $testprocessor = \core_message\api::get_message_processor($name);
1411 $this->assertEquals($processor->name, $testprocessor->name);
1412 $this->assertEquals($processor->enabled, $testprocessor->enabled);
1413 $this->assertEquals($processor->available, $testprocessor->available);
1414 $this->assertEquals($processor->configured, $testprocessor->configured);
1416 // Disable processor and test.
1417 \core_message\api::update_processor_status($testprocessor, 0);
1418 $testprocessor = \core_message\api::get_message_processor($name, true);
1419 $this->assertEmpty($testprocessor);
1420 $testprocessor = \core_message\api::get_message_processor($name);
1421 $this->assertEquals($processor->name, $testprocessor->name);
1422 $this->assertEquals(0, $testprocessor->enabled);
1424 // Enable again and test.
1425 \core_message\api::update_processor_status($testprocessor, 1);
1426 $testprocessor = \core_message\api::get_message_processor($name, true);
1427 $this->assertEquals($processor->name, $testprocessor->name);
1428 $this->assertEquals(1, $testprocessor->enabled);
1429 $testprocessor = \core_message\api::get_message_processor($name);
1430 $this->assertEquals($processor->name, $testprocessor->name);
1431 $this->assertEquals(1, $testprocessor->enabled);
1435 * Test method update_processor_status.
1437 public function test_update_processor_status() {
1438 $processors = get_message_processors();
1439 if (empty($processors)) {
1440 $this->markTestSkipped("No message processors found");
1442 $name = key($processors);
1443 $testprocessor = current($processors);
1445 // Enable.
1446 \core_message\api::update_processor_status($testprocessor, 1);
1447 $testprocessor = \core_message\api::get_message_processor($name);
1448 $this->assertEquals(1, $testprocessor->enabled);
1450 // Disable.
1451 \core_message\api::update_processor_status($testprocessor, 0);
1452 $testprocessor = \core_message\api::get_message_processor($name);
1453 $this->assertEquals(0, $testprocessor->enabled);
1455 // Enable again.
1456 \core_message\api::update_processor_status($testprocessor, 1);
1457 $testprocessor = \core_message\api::get_message_processor($name);
1458 $this->assertEquals(1, $testprocessor->enabled);
1462 * Test method is_user_enabled.
1464 public function is_user_enabled() {
1465 $processors = get_message_processors();
1466 if (empty($processors)) {
1467 $this->markTestSkipped("No message processors found");
1469 $name = key($processors);
1470 $testprocessor = current($processors);
1472 // Enable.
1473 \core_message\api::update_processor_status($testprocessor, 1);
1474 $status = \core_message\api::is_processor_enabled($name);
1475 $this->assertEquals(1, $status);
1477 // Disable.
1478 \core_message\api::update_processor_status($testprocessor, 0);
1479 $status = \core_message\api::is_processor_enabled($name);
1480 $this->assertEquals(0, $status);
1482 // Enable again.
1483 \core_message\api::update_processor_status($testprocessor, 1);
1484 $status = \core_message\api::is_processor_enabled($name);
1485 $this->assertEquals(1, $status);
1489 * Test retrieving messages by providing a minimum timecreated value.
1491 public function test_get_messages_time_from_only() {
1492 // Create some users.
1493 $user1 = self::getDataGenerator()->create_user();
1494 $user2 = self::getDataGenerator()->create_user();
1496 // The person doing the search.
1497 $this->setUser($user1);
1499 // Send some messages back and forth.
1500 $time = 1;
1501 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1502 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1503 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1504 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1506 // Retrieve the messages from $time, which should be all of them.
1507 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
1509 // Confirm the message data is correct.
1510 $this->assertEquals(4, count($messages));
1512 $message1 = $messages[0];
1513 $message2 = $messages[1];
1514 $message3 = $messages[2];
1515 $message4 = $messages[3];
1517 $this->assertContains('Message 1', $message1->text);
1518 $this->assertContains('Message 2', $message2->text);
1519 $this->assertContains('Message 3', $message3->text);
1520 $this->assertContains('Message 4', $message4->text);
1522 // Retrieve the messages from $time + 3, which should only be the 2 last messages.
1523 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
1525 // Confirm the message data is correct.
1526 $this->assertEquals(2, count($messages));
1528 $message1 = $messages[0];
1529 $message2 = $messages[1];
1531 $this->assertContains('Message 3', $message1->text);
1532 $this->assertContains('Message 4', $message2->text);
1536 * Test retrieving messages by providing a maximum timecreated value.
1538 public function test_get_messages_time_to_only() {
1539 // Create some users.
1540 $user1 = self::getDataGenerator()->create_user();
1541 $user2 = self::getDataGenerator()->create_user();
1543 // The person doing the search.
1544 $this->setUser($user1);
1546 // Send some messages back and forth.
1547 $time = 1;
1548 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1549 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1550 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1551 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1553 // Retrieve the messages up until $time + 4, which should be all of them.
1554 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
1556 // Confirm the message data is correct.
1557 $this->assertEquals(4, count($messages));
1559 $message1 = $messages[0];
1560 $message2 = $messages[1];
1561 $message3 = $messages[2];
1562 $message4 = $messages[3];
1564 $this->assertContains('Message 1', $message1->text);
1565 $this->assertContains('Message 2', $message2->text);
1566 $this->assertContains('Message 3', $message3->text);
1567 $this->assertContains('Message 4', $message4->text);
1569 // Retrieve the messages up until $time + 2, which should be the first two.
1570 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
1572 // Confirm the message data is correct.
1573 $this->assertEquals(2, count($messages));
1575 $message1 = $messages[0];
1576 $message2 = $messages[1];
1578 $this->assertContains('Message 1', $message1->text);
1579 $this->assertContains('Message 2', $message2->text);
1583 * Test retrieving messages by providing a minimum and maximum timecreated value.
1585 public function test_get_messages_time_from_and_to() {
1586 // Create some users.
1587 $user1 = self::getDataGenerator()->create_user();
1588 $user2 = self::getDataGenerator()->create_user();
1590 // The person doing the search.
1591 $this->setUser($user1);
1593 // Send some messages back and forth.
1594 $time = 1;
1595 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1596 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1597 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1598 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1600 // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
1601 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
1603 // Confirm the message data is correct.
1604 $this->assertEquals(2, count($messages));
1606 $message1 = $messages[0];
1607 $message2 = $messages[1];
1609 $this->assertContains('Message 2', $message1->text);
1610 $this->assertContains('Message 3', $message2->text);
1614 * Test returning blocked users.
1616 public function test_get_blocked_users() {
1617 global $USER;
1619 // Set this user as the admin.
1620 $this->setAdminUser();
1622 // Create a user to add to the admin's contact list.
1623 $user1 = $this->getDataGenerator()->create_user();
1624 $user2 = $this->getDataGenerator()->create_user();
1626 // Add users to the admin's contact list.
1627 message_add_contact($user1->id);
1628 message_add_contact($user2->id, 1);
1630 $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
1632 // Block other user.
1633 message_block_contact($user1->id);
1634 $this->assertCount(2, \core_message\api::get_blocked_users($USER->id));
1636 // Test deleting users.
1637 delete_user($user1);
1638 $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
1642 * Test returning contacts with unread message count.
1644 public function test_get_contacts_with_unread_message_count() {
1645 global $DB;
1647 $user1 = self::getDataGenerator()->create_user();
1648 $user2 = self::getDataGenerator()->create_user();
1649 $user3 = self::getDataGenerator()->create_user();
1650 $user4 = self::getDataGenerator()->create_user();
1652 // Add the users to each of their contacts.
1653 message_add_contact($user1->id, 0, $user2->id);
1654 message_add_contact($user2->id, 0, $user1->id);
1655 message_add_contact($user3->id, 0, $user2->id);
1657 $this->send_fake_message($user1, $user2);
1658 $this->send_fake_message($user1, $user2);
1659 $this->send_fake_message($user1, $user2);
1660 $message4id = $this->send_fake_message($user1, $user2);
1662 $this->send_fake_message($user3, $user2);
1663 $message6id = $this->send_fake_message($user3, $user2);
1664 $this->send_fake_message($user3, $user2);
1665 $this->send_fake_message($user3, $user2);
1666 $this->send_fake_message($user3, $user2);
1668 // Send a message that should never be included as the user is not a contact.
1669 $this->send_fake_message($user4, $user2);
1671 // Get the contacts and the unread message count.
1672 $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1674 // Confirm the size is correct.
1675 $this->assertCount(2, $messages);
1676 ksort($messages);
1678 $messageinfo1 = array_shift($messages);
1679 $messageinfo2 = array_shift($messages);
1680 $this->assertEquals($user1->id, $messageinfo1->id);
1681 $this->assertEquals(4, $messageinfo1->messagecount);
1682 $this->assertEquals($user3->id, $messageinfo2->id);
1683 $this->assertEquals(5, $messageinfo2->messagecount);
1685 // Mark some of the messages as read.
1686 $m4 = $DB->get_record('messages', ['id' => $message4id]);
1687 $m6 = $DB->get_record('messages', ['id' => $message6id]);
1688 \core_message\api::mark_message_as_read($user2->id, $m4);
1689 \core_message\api::mark_message_as_read($user2->id, $m6);
1691 // Get the contacts and the unread message count.
1692 $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1694 // Confirm the size is correct.
1695 $this->assertCount(2, $messages);
1696 ksort($messages);
1698 // Confirm read messages are not included.
1699 $messageinfo1 = array_shift($messages);
1700 $messageinfo2 = array_shift($messages);
1701 $this->assertEquals($user1->id, $messageinfo1->id);
1702 $this->assertEquals(3, $messageinfo1->messagecount);
1703 $this->assertEquals($user3->id, $messageinfo2->id);
1704 $this->assertEquals(4, $messageinfo2->messagecount);
1706 // Now, let's populate the database with messages from user2 to user 1.
1707 $this->send_fake_message($user2, $user1);
1708 $this->send_fake_message($user2, $user1);
1709 $messageid = $this->send_fake_message($user2, $user1);
1711 // Send a message that should never be included as the user is not a contact.
1712 $this->send_fake_message($user4, $user1);
1714 // Get the contacts and the unread message count.
1715 $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
1717 // Confirm the size is correct.
1718 $this->assertCount(1, $messages);
1719 $messageinfo1 = array_shift($messages);
1720 $this->assertEquals($user2->id, $messageinfo1->id);
1721 $this->assertEquals(3, $messageinfo1->messagecount);
1723 // Mark the last message as read.
1724 $m = $DB->get_record('messages', ['id' => $messageid]);
1725 \core_message\api::mark_message_as_read($user1->id, $m);
1727 $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
1729 // Confirm the size is correct.
1730 $this->assertCount(1, $messages);
1732 // Confirm read messages are not included.
1733 $messageinfo1 = array_shift($messages);
1734 $this->assertEquals($user2->id, $messageinfo1->id);
1735 $this->assertEquals(2, $messageinfo1->messagecount);
1739 * Test returning contacts with unread message count when there are no messages.
1741 public function test_get_contacts_with_unread_message_count_no_messages() {
1742 $user1 = self::getDataGenerator()->create_user();
1743 $user2 = self::getDataGenerator()->create_user();
1745 // Add the users to each of their contacts.
1746 message_add_contact($user1->id, 0, $user2->id);
1748 // Check we get the correct message count.
1749 $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1751 // Confirm the size is correct.
1752 $this->assertCount(1, $messages);
1754 $messageinfo = array_shift($messages);
1756 $this->assertEquals($user1->id, $messageinfo->id);
1757 $this->assertEquals(0, $messageinfo->messagecount);
1761 * Test returning non-contacts with unread message count.
1763 public function test_get_non_contacts_with_unread_message_count() {
1764 global $DB;
1766 $user1 = self::getDataGenerator()->create_user();
1767 $user2 = self::getDataGenerator()->create_user();
1768 $user3 = self::getDataGenerator()->create_user();
1769 $user4 = self::getDataGenerator()->create_user();
1771 // Add a user to the contact list of the users we are testing this function with.
1772 message_add_contact($user4->id, 0, $user1->id);
1773 message_add_contact($user4->id, 0, $user2->id);
1775 $this->send_fake_message($user1, $user2);
1776 $this->send_fake_message($user1, $user2);
1777 $this->send_fake_message($user1, $user2);
1778 $message4id = $this->send_fake_message($user1, $user2);
1780 $this->send_fake_message($user3, $user2);
1781 $message6id = $this->send_fake_message($user3, $user2);
1782 $this->send_fake_message($user3, $user2);
1783 $this->send_fake_message($user3, $user2);
1784 $this->send_fake_message($user3, $user2);
1786 // Send a message that should never be included as the user is a contact.
1787 $this->send_fake_message($user4, $user2);
1789 // Get the non-contacts and the unread message count.
1790 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
1792 // Check we get the correct message count.
1793 ksort($messages);
1794 $this->assertCount(2, $messages);
1795 $messageinfo1 = array_shift($messages);
1796 $messageinfo2 = array_shift($messages);
1797 $this->assertEquals($user1->id, $messageinfo1->id);
1798 $this->assertEquals(4, $messageinfo1->messagecount);
1799 $this->assertEquals($user3->id, $messageinfo2->id);
1800 $this->assertEquals(5, $messageinfo2->messagecount);
1802 // Mark some of the messages as read.
1803 $m4 = $DB->get_record('messages', ['id' => $message4id]);
1804 $m6 = $DB->get_record('messages', ['id' => $message6id]);
1805 \core_message\api::mark_message_as_read($user2->id, $m4);
1806 \core_message\api::mark_message_as_read($user2->id, $m6);
1808 // Get the non-contacts and the unread message count.
1809 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
1811 // Check the marked message is not returned in the message count.
1812 ksort($messages);
1813 $this->assertCount(2, $messages);
1814 $messageinfo1 = array_shift($messages);
1815 $messageinfo2 = array_shift($messages);
1816 $this->assertEquals($user1->id, $messageinfo1->id);
1817 $this->assertEquals(3, $messageinfo1->messagecount);
1818 $this->assertEquals($user3->id, $messageinfo2->id);
1819 $this->assertEquals(4, $messageinfo2->messagecount);
1821 // Now, let's populate the database with messages from user2 to user 1.
1822 $this->send_fake_message($user2, $user1);
1823 $this->send_fake_message($user2, $user1);
1824 $messageid = $this->send_fake_message($user2, $user1);
1826 // Send a message that should never be included as the user is a contact.
1827 $this->send_fake_message($user4, $user1);
1829 // Get the non-contacts and the unread message count.
1830 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
1832 // Confirm the size is correct.
1833 $this->assertCount(1, $messages);
1834 $messageinfo1 = array_shift($messages);
1835 $this->assertEquals($user2->id, $messageinfo1->id);
1836 $this->assertEquals(3, $messageinfo1->messagecount);
1838 // Mark the last message as read.
1839 $m = $DB->get_record('messages', ['id' => $messageid]);
1840 \core_message\api::mark_message_as_read($user1->id, $m);
1842 // Get the non-contacts and the unread message count.
1843 $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
1845 // Check the marked message is not returned in the message count.
1846 $this->assertCount(1, $messages);
1847 $messageinfo1 = array_shift($messages);
1848 $this->assertEquals($user2->id, $messageinfo1->id);
1849 $this->assertEquals(2, $messageinfo1->messagecount);
1853 * Test marking a message as read.
1855 public function test_mark_message_as_read() {
1856 global $DB;
1858 $user1 = self::getDataGenerator()->create_user();
1859 $user2 = self::getDataGenerator()->create_user();
1861 $this->send_fake_message($user1, $user2);
1862 $m2id = $this->send_fake_message($user1, $user2);
1863 $this->send_fake_message($user2, $user1);
1864 $m4id = $this->send_fake_message($user2, $user1);
1866 $m2 = $DB->get_record('messages', ['id' => $m2id]);
1867 $m4 = $DB->get_record('messages', ['id' => $m4id]);
1868 \core_message\api::mark_message_as_read($user2->id, $m2, 11);
1869 \core_message\api::mark_message_as_read($user1->id, $m4, 12);
1871 // Confirm there are two user actions.
1872 $muas = $DB->get_records('message_user_actions', [], 'timecreated ASC');
1873 $this->assertEquals(2, count($muas));
1875 // Confirm they are correct.
1876 $mua1 = array_shift($muas);
1877 $mua2 = array_shift($muas);
1879 // Confirm first action.
1880 $this->assertEquals($user2->id, $mua1->userid);
1881 $this->assertEquals($m2id, $mua1->messageid);
1882 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua1->action);
1883 $this->assertEquals(11, $mua1->timecreated);
1885 // Confirm second action.
1886 $this->assertEquals($user1->id, $mua2->userid);
1887 $this->assertEquals($m4id, $mua2->messageid);
1888 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua2->action);
1889 $this->assertEquals(12, $mua2->timecreated);
1893 * Test marking a notification as read.
1895 public function test_mark_notification_as_read() {
1896 global $DB;
1898 $user1 = self::getDataGenerator()->create_user();
1899 $user2 = self::getDataGenerator()->create_user();
1901 $this->send_fake_message($user1, $user2, 'Notification 1', 1);
1902 $n2id = $this->send_fake_message($user1, $user2, 'Notification 2', 1);
1903 $this->send_fake_message($user2, $user1, 'Notification 3', 1);
1904 $n4id = $this->send_fake_message($user2, $user1, 'Notification 4', 1);
1906 $n2 = $DB->get_record('notifications', ['id' => $n2id]);
1907 $n4 = $DB->get_record('notifications', ['id' => $n4id]);
1909 \core_message\api::mark_notification_as_read($n2, 11);
1910 \core_message\api::mark_notification_as_read($n4, 12);
1912 // Retrieve the notifications.
1913 $n2 = $DB->get_record('notifications', ['id' => $n2id]);
1914 $n4 = $DB->get_record('notifications', ['id' => $n4id]);
1916 // Confirm they have been marked as read.
1917 $this->assertEquals(11, $n2->timeread);
1918 $this->assertEquals(12, $n4->timeread);
1922 * Test a conversation is not returned if there is none.
1924 public function test_get_conversation_between_users_no_conversation() {
1925 $user1 = self::getDataGenerator()->create_user();
1926 $user2 = self::getDataGenerator()->create_user();
1928 $this->assertFalse(\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
1932 * Test we can return a conversation that exists between users.
1934 public function test_get_conversation_between_users_with_existing_conversation() {
1935 $user1 = self::getDataGenerator()->create_user();
1936 $user2 = self::getDataGenerator()->create_user();
1938 $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
1940 $this->assertEquals($conversationid,
1941 \core_message\api::get_conversation_between_users([$user1->id, $user2->id]));