2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Test api's in message lib.
20 * @package core_message
22 * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/message/lib.php');
32 * Test api's in message lib.
34 * @package core_message
36 * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_message_messagelib_testcase extends advanced_testcase {
41 /** @var phpunit_message_sink keep track of messages. */
42 protected $messagesink = null;
47 * This is executed before running any test in this file.
49 public function setUp() {
50 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
51 $this->messagesink = $this->redirectMessages();
52 $this->resetAfterTest();
56 * Send a fake message.
58 * {@link message_send()} does not support transaction, this function will simulate a message
59 * sent from a user to another. We should stop using it once {@link message_send()} will support
60 * transactions. This is not clean at all, this is just used to add rows to the table.
62 * @param stdClass $userfrom user object of the one sending the message.
63 * @param stdClass $userto user object of the one receiving the message.
64 * @param string $message message to send.
65 * @return int the id of the message
67 protected function send_fake_message($userfrom, $userto, $message = 'Hello world!') {
70 $record = new stdClass();
71 $record->useridfrom = $userfrom->id;
72 $record->useridto = $userto->id;
73 $record->subject = 'No subject';
74 $record->fullmessage = $message;
75 $record->smallmessage = $message;
76 $record->timecreated = time();
78 return $DB->insert_record('message', $record);
82 * Test message_get_blocked_users.
84 public function test_message_get_blocked_users() {
85 // Set this user as the admin.
86 $this->setAdminUser();
88 // Create a user to add to the admin's contact list.
89 $user1 = $this->getDataGenerator()->create_user();
90 $user2 = $this->getDataGenerator()->create_user();
92 // Add users to the admin's contact list.
93 message_add_contact($user1->id);
94 message_add_contact($user2->id, 1);
96 $this->assertCount(1, message_get_blocked_users());
99 message_block_contact($user1->id);
100 $this->assertCount(2, message_get_blocked_users());
102 // Test deleting users.
104 $this->assertCount(1, message_get_blocked_users());
108 * Test message_get_contacts.
110 public function test_message_get_contacts() {
113 // Set this user as the admin.
114 $this->setAdminUser();
116 $noreplyuser = core_user::get_noreply_user();
117 $supportuser = core_user::get_support_user();
119 // Create a user to add to the admin's contact list.
120 $user1 = $this->getDataGenerator()->create_user();
121 $user2 = $this->getDataGenerator()->create_user();
122 $user3 = $this->getDataGenerator()->create_user(); // Stranger.
124 // Add users to the admin's contact list.
125 message_add_contact($user1->id);
126 message_add_contact($user2->id);
128 // Send some messages.
129 $this->send_fake_message($user1, $USER);
130 $this->send_fake_message($user2, $USER);
131 $this->send_fake_message($user3, $USER);
133 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
134 $this->assertCount(0, $onlinecontacts);
135 $this->assertCount(2, $offlinecontacts);
136 $this->assertCount(1, $strangers);
138 // Send message from noreply and support users.
139 $this->send_fake_message($noreplyuser, $USER);
140 $this->send_fake_message($supportuser, $USER);
141 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
142 $this->assertCount(0, $onlinecontacts);
143 $this->assertCount(2, $offlinecontacts);
144 $this->assertCount(3, $strangers);
147 message_block_contact($user2->id);
148 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
149 $this->assertCount(0, $onlinecontacts);
150 $this->assertCount(1, $offlinecontacts);
151 $this->assertCount(3, $strangers);
153 // Noreply user being valid user.
154 core_user::reset_internal_users();
155 $CFG->noreplyuserid = $user3->id;
156 $noreplyuser = core_user::get_noreply_user();
157 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
158 $this->assertCount(0, $onlinecontacts);
159 $this->assertCount(1, $offlinecontacts);
160 $this->assertCount(2, $strangers);
162 // Test deleting users.
166 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
167 $this->assertCount(0, $onlinecontacts);
168 $this->assertCount(0, $offlinecontacts);
169 $this->assertCount(1, $strangers);
173 * Test message_count_messages.
175 public function test_message_count_messages() {
178 // Create users to send and receive message.
179 $userfrom = $this->getDataGenerator()->create_user();
180 $userto = $this->getDataGenerator()->create_user();
182 message_post_message($userfrom, $userto, 'Message 1', FORMAT_PLAIN);
183 message_post_message($userfrom, $userto, 'Message 2', FORMAT_PLAIN);
184 message_post_message($userto, $userfrom, 'Message 3', FORMAT_PLAIN);
186 // Return 0 when no message.
188 $this->assertEquals(0, message_count_messages($messages, 'Test', 'Test'));
190 // Check number of messages from userfrom and userto.
191 $messages = $this->messagesink->get_messages();
192 $this->assertEquals(2, message_count_messages($messages, 'useridfrom', $userfrom->id));
193 $this->assertEquals(1, message_count_messages($messages, 'useridfrom', $userto->id));
197 * Test message_count_unread_messages.
199 public function test_message_count_unread_messages() {
200 // Create users to send and receive message.
201 $userfrom1 = $this->getDataGenerator()->create_user();
202 $userfrom2 = $this->getDataGenerator()->create_user();
203 $userto = $this->getDataGenerator()->create_user();
205 $this->assertEquals(0, message_count_unread_messages($userto));
207 // Send fake messages.
208 $this->send_fake_message($userfrom1, $userto);
209 $this->send_fake_message($userfrom2, $userto);
211 $this->assertEquals(2, message_count_unread_messages($userto));
212 $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
216 * Test message_count_blocked_users.
219 public function test_message_count_blocked_users() {
220 // Set this user as the admin.
221 $this->setAdminUser();
223 // Create users to add to the admin's contact list.
224 $user1 = $this->getDataGenerator()->create_user();
225 $user2 = $this->getDataGenerator()->create_user();
227 $this->assertEquals(0, message_count_blocked_users());
229 // Add 1 blocked and 1 normal contact to admin's contact list.
230 message_add_contact($user1->id);
231 message_add_contact($user2->id, 1);
233 $this->assertEquals(0, message_count_blocked_users($user2));
234 $this->assertEquals(1, message_count_blocked_users());
238 * Test message_add_contact.
240 public function test_message_add_contact() {
241 // Set this user as the admin.
242 $this->setAdminUser();
244 // Create a user to add to the admin's contact list.
245 $user1 = $this->getDataGenerator()->create_user();
246 $user2 = $this->getDataGenerator()->create_user();
247 $user3 = $this->getDataGenerator()->create_user();
249 message_add_contact($user1->id);
250 message_add_contact($user2->id, 0);
251 // Add duplicate contact and make sure only 1 record exists.
252 message_add_contact($user2->id, 1);
254 $this->assertNotEmpty(message_get_contact($user1->id));
255 $this->assertNotEmpty(message_get_contact($user2->id));
256 $this->assertEquals(false, message_get_contact($user3->id));
257 $this->assertEquals(1, message_count_blocked_users());
261 * Test message_remove_contact.
263 public function test_message_remove_contact() {
264 // Set this user as the admin.
265 $this->setAdminUser();
267 // Create a user to add to the admin's contact list.
268 $user = $this->getDataGenerator()->create_user();
270 // Add the user to the admin's contact list.
271 message_add_contact($user->id);
272 $this->assertNotEmpty(message_get_contact($user->id));
274 // Remove user from admin's contact list.
275 message_remove_contact($user->id);
276 $this->assertEquals(false, message_get_contact($user->id));
280 * Test message_block_contact.
282 public function test_message_block_contact() {
283 // Set this user as the admin.
284 $this->setAdminUser();
286 // Create a user to add to the admin's contact list.
287 $user1 = $this->getDataGenerator()->create_user();
288 $user2 = $this->getDataGenerator()->create_user();
290 // Add users to the admin's contact list.
291 message_add_contact($user1->id);
292 message_add_contact($user2->id);
294 $this->assertEquals(0, message_count_blocked_users());
297 message_block_contact($user2->id);
298 $this->assertEquals(1, message_count_blocked_users());
303 * Test message_unblock_contact.
305 public function test_message_unblock_contact() {
306 // Set this user as the admin.
307 $this->setAdminUser();
309 // Create a user to add to the admin's contact list.
310 $user1 = $this->getDataGenerator()->create_user();
311 $user2 = $this->getDataGenerator()->create_user();
313 // Add users to the admin's contact list.
314 message_add_contact($user1->id);
315 message_add_contact($user2->id, 1); // Add blocked contact.
317 $this->assertEquals(1, message_count_blocked_users());
320 message_unblock_contact($user2->id);
321 $this->assertEquals(0, message_count_blocked_users());
325 * Test message_search_users.
327 public function test_message_search_users() {
328 // Set this user as the admin.
329 $this->setAdminUser();
331 // Create a user to add to the admin's contact list.
332 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
333 $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
335 // Add users to the admin's contact list.
336 message_add_contact($user1->id);
337 message_add_contact($user2->id); // Add blocked contact.
339 $this->assertCount(1, message_search_users(0, 'Test1'));
340 $this->assertCount(2, message_search_users(0, 'Test'));
341 $this->assertCount(1, message_search_users(0, 'user1'));
342 $this->assertCount(2, message_search_users(0, 'user'));
346 * Test message_search.
348 public function test_message_search() {
351 // Set this user as the admin.
352 $this->setAdminUser();
354 // Create a user to add to the admin's contact list.
355 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
356 $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
358 // Send few messages, real (read).
359 message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
360 message_post_message($USER, $user1, 'Message 2', FORMAT_PLAIN);
361 message_post_message($USER, $user2, 'Message 3', FORMAT_PLAIN);
363 $this->assertCount(2, message_search(array('Message'), true, false));
364 $this->assertCount(3, message_search(array('Message'), true, true));
366 // Send fake message (not-read).
367 $this->send_fake_message($USER, $user1, 'Message 4');
368 $this->send_fake_message($user1, $USER, 'Message 5');
369 $this->assertCount(3, message_search(array('Message'), true, false));
370 $this->assertCount(5, message_search(array('Message'), true, true));
372 // If courseid given then should be 0.
373 $this->assertEquals(false, message_search(array('Message'), true, true, ''));
374 $this->assertEquals(false, message_search(array('Message'), true, true, 2));
375 $this->assertCount(5, message_search(array('Message'), true, true, SITEID));
379 * The data provider for message_get_recent_conversations.
381 * This provides sets of data to for testing.
384 public function message_get_recent_conversations_provider() {
387 // Test that conversations with multiple contacts is correctly ordered.
435 'expectations' => array(
437 // User1 has conversed most recently with user3. The most recent message is M5.
439 'messageposition' => 0,
443 // User1 has also conversed with user2. The most recent message is S2.
445 'messageposition' => 1,
451 // User2 has only conversed with user1. Their most recent shared message was S2.
453 'messageposition' => 0,
459 // User3 has only conversed with user1. Their most recent shared message was S5.
461 'messageposition' => 0,
469 // Test conversations with a single user, where some messages are read and some are not.
502 'expectations' => array(
503 // The most recent message between user1 and user2 was S4.
506 'messageposition' => 0,
512 // The most recent message between user1 and user2 was S4.
514 'messageposition' => 0,
522 // Test conversations with a single user, where some messages are read and some are not, and messages
524 // This can happen through a combination of factors including multi-master DB replication with messages
525 // read somehow (e.g. API).
558 'expectations' => array(
559 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
562 'messageposition' => 0,
569 'messageposition' => 0,
580 * Test message_get_recent_conversations with a mixture of messages.
582 * @dataProvider message_get_recent_conversations_provider
583 * @param array $usersdata The list of users to create for this test.
584 * @param array $messagesdata The list of messages to create.
585 * @param array $expectations The list of expected outcomes.
587 public function test_message_get_recent_conversations($usersdata, $messagesdata, $expectations) {
590 // Create all of the users.
592 foreach ($usersdata as $username) {
593 $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
596 $defaulttimecreated = time();
597 foreach ($messagesdata as $messagedata) {
598 $from = $users[$messagedata['from']];
599 $to = $users[$messagedata['to']];
600 $subject = $messagedata['subject'];
602 if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
604 $messageid = $this->send_fake_message($from, $to, $subject, FORMAT_PLAIN);
606 // If there is no state, or the state is not 'unread', assume the message is read.
607 $table = 'message_read';
608 $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN);
611 $updatemessage = new stdClass();
612 $updatemessage->id = $messageid;
613 if (isset($messagedata['timecreated'])) {
614 $updatemessage->timecreated = $messagedata['timecreated'];
615 } else if (isset($messagedata['timemodifier'])) {
616 $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier'];
618 $updatemessage->timecreated = $defaulttimecreated;
620 $DB->update_record($table, $updatemessage);
623 foreach ($expectations as $username => $data) {
624 // Get the recent conversations for the specified user.
625 $user = $users[$username];
626 $conversations = message_get_recent_conversations($user);
627 foreach ($data as $expectation) {
628 $otheruser = $users[$expectation['with']];
629 $conversation = $conversations[$expectation['messageposition']];
630 $this->assertEquals($otheruser->id, $conversation->id);
631 $this->assertEquals($expectation['subject'], $conversation->smallmessage);
637 * Test message_get_recent_notifications.
639 public function test_message_get_recent_notifications() {
642 // Set this user as the admin.
643 $this->setAdminUser();
645 // Create a user to send messages from.
646 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
648 // Add two messages - will mark them as notifications later.
649 $m1 = message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
650 $m2 = message_post_message($user1, $USER, 'Message 2', FORMAT_PLAIN);
652 // Mark the second message as a notification.
653 $updatemessage = new stdClass();
654 $updatemessage->id = $m2;
655 $updatemessage->notification = 1;
656 $DB->update_record('message_read', $updatemessage);
658 // Mark the first message as a notification and change the timecreated to 0.
659 $updatemessage->id = $m1;
660 $updatemessage->notification = 1;
661 $updatemessage->timecreated = 0;
662 $DB->update_record('message_read', $updatemessage);
664 $notifications = message_get_recent_notifications($USER);
667 $firstmessage = array_shift($notifications);
668 $secondmessage = array_shift($notifications);
670 // Confirm that we have received the notifications with the maximum timecreated, rather than the max id.
671 $this->assertEquals('Message 2', $firstmessage->smallmessage);
672 $this->assertEquals('Message 1', $secondmessage->smallmessage);
676 * Test that message_can_post_message returns false if the sender does not have the
677 * moode/site:sendmessage capability.
679 public function test_message_can_post_message_returns_false_without_capability() {
680 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
681 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
682 $context = context_system::instance();
683 $roleid = $this->getDataGenerator()->create_role();
684 $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
686 assign_capability('moodle/site:sendmessage', CAP_PROHIBIT, $roleid, $context);
688 $this->assertFalse(message_can_post_message($recipient, $sender));
692 * Test that message_can_post_message returns false if the receiver only accepts
693 * messages from contacts and the sender isn't a contact.
695 public function test_message_can_post_message_returns_false_non_contact_blocked() {
696 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
697 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
699 set_user_preference('message_blocknoncontacts', true, $recipient);
701 $this->assertFalse(message_can_post_message($recipient, $sender));
705 * Test that message_can_post_message returns false if the receiver has blocked the
706 * sender from messaging them.
708 public function test_message_can_post_message_returns_false_if_blocked() {
709 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
710 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
712 $this->setUser($recipient);
713 message_block_contact($sender->id);
715 $this->assertFalse(message_can_post_message($recipient, $sender));
719 * Test that message_can_post_message returns false if the receiver has blocked the
720 * sender from messaging them.
722 public function test_message_can_post_message_returns_true() {
723 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
724 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
726 $this->assertTrue(message_can_post_message($recipient, $sender));
730 * Test that message_is_user_non_contact_blocked returns false if the recipient allows
731 * messages from non-contacts.
733 public function test_message_is_user_non_contact_blocked_false_without_preference() {
734 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
735 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
737 set_user_preference('message_blocknoncontacts', false, $recipient);
739 $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
743 * Test that message_is_user_non_contact_blocked returns true if the recipient doesn't
744 * allow messages from non-contacts and the sender isn't a contact.
746 public function test_message_is_user_non_contact_blocked_true_with_preference() {
747 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
748 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
750 set_user_preference('message_blocknoncontacts', true, $recipient);
752 $this->assertTrue(message_is_user_non_contact_blocked($recipient, $sender));
756 * Test that message_is_user_non_contact_blocked returns false if the recipient doesn't
757 * allow messages from non-contacts but the sender is a contact.
759 public function test_message_is_user_non_contact_blocked_false_with_if_contact() {
760 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
761 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
763 $this->setUser($recipient);
764 set_user_preference('message_blocknoncontacts', true, $recipient);
765 message_add_contact($sender->id);
767 $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
771 * Test that message_is_user_blocked returns false if the sender is not a contact of
774 public function test_message_is_user_blocked_false_no_contact() {
775 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
776 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
778 $this->assertFalse(message_is_user_blocked($recipient, $sender));
782 * Test that message_is_user_blocked returns false if the sender is a contact that is
783 * blocked by the recipient but has the moodle/site:readallmessages capability.
785 public function test_message_is_user_blocked_false_if_readallmessages() {
786 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
787 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
789 $this->setUser($recipient);
790 message_block_contact($sender->id);
792 $context = context_system::instance();
793 $roleid = $this->getDataGenerator()->create_role();
794 $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
796 assign_capability('moodle/site:readallmessages', CAP_ALLOW, $roleid, $context);
798 $this->assertFalse(message_is_user_blocked($recipient, $sender));
802 * Test that message_is_user_blocked returns true if the sender is a contact that is
803 * blocked by the recipient and does not have the moodle/site:readallmessages capability.
805 public function test_message_is_user_blocked_true_if_blocked() {
806 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
807 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
809 $this->setUser($recipient);
810 message_block_contact($sender->id);
812 $context = context_system::instance();
813 $roleid = $this->getDataGenerator()->create_role();
814 $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
816 assign_capability('moodle/site:readallmessages', CAP_PROHIBIT, $roleid, $context);
818 $this->assertTrue(message_is_user_blocked($recipient, $sender));