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/>.
17 namespace core_message
;
19 use core_message\tests\helper
as testhelper
;
21 defined('MOODLE_INTERNAL') ||
die();
24 require_once($CFG->dirroot
. '/message/lib.php');
27 * Test api's in message lib.
29 * @package core_message
31 * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class messagelib_test
extends \advanced_testcase
{
36 /** @var phpunit_message_sink keep track of messages. */
37 protected $messagesink = null;
42 * This is executed before running any test in this file.
44 public function setUp(): void
{
45 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
46 $this->messagesink
= $this->redirectMessages();
47 $this->resetAfterTest();
51 * Send a fake message.
53 * {@link message_send()} does not support transaction, this function will simulate a message
54 * sent from a user to another. We should stop using it once {@link message_send()} will support
55 * transactions. This is not clean at all, this is just used to add rows to the table.
57 * @param \stdClass $userfrom user object of the one sending the message.
58 * @param \stdClass $userto user object of the one receiving the message.
59 * @param string $message message to send.
60 * @param int $notification if the message is a notification.
61 * @param int $time the time the message was sent
62 * @return int the id of the message
64 protected function send_fake_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) {
72 $record = new \
stdClass();
73 $record->useridfrom
= $userfrom->id
;
74 $record->useridto
= $userto->id
;
75 $record->subject
= 'No subject';
76 $record->fullmessage
= $message;
77 $record->smallmessage
= $message;
78 $record->timecreated
= $time;
80 return $DB->insert_record('notifications', $record);
83 if ($userfrom->id
== $userto->id
) {
84 // It's a self conversation.
85 $conversation = \core_message\api
::get_self_conversation($userfrom->id
);
86 if (empty($conversation)) {
87 $conversation = \core_message\api
::create_conversation(
88 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
,
92 $conversationid = $conversation->id
;
93 } else if (!$conversationid = \core_message\api
::get_conversation_between_users([$userfrom->id
, $userto->id
])) {
94 // It's an individual conversation between two different users.
95 $conversation = \core_message\api
::create_conversation(
96 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
102 $conversationid = $conversation->id
;
105 // Ok, send the message.
106 $record = new \
stdClass();
107 $record->useridfrom
= $userfrom->id
;
108 $record->conversationid
= $conversationid;
109 $record->subject
= 'No subject';
110 $record->fullmessage
= $message;
111 $record->smallmessage
= $message;
112 $record->timecreated
= $time;
114 return $DB->insert_record('messages', $record);
118 * Test message_get_blocked_users throws an exception because has been removed.
120 public function test_message_get_blocked_users() {
121 $this->expectException('coding_exception');
122 $this->expectExceptionMessage(
123 'message_get_blocked_users() has been removed, please use \core_message\api::get_blocked_users() instead.'
125 message_get_blocked_users();
129 * Test message_get_contacts throws an exception because has been removed.
131 public function test_message_get_contacts() {
132 $this->expectException('coding_exception');
133 $this->expectExceptionMessage('message_get_contacts() has been removed.');
134 message_get_contacts();
138 * Test message_search_users.
140 public function test_message_search_users() {
143 // Set this user as the admin.
144 $this->setAdminUser();
146 // Create a user to add to the admin's contact list.
147 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
148 $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
150 // Add users to the admin's contact list.
151 \core_message\api
::add_contact($USER->id
, $user1->id
);
152 \core_message\api
::add_contact($USER->id
, $user2->id
);
154 $this->assertCount(1, message_search_users(0, 'Test1'));
155 $this->assertCount(2, message_search_users(0, 'Test'));
156 $this->assertCount(1, message_search_users(0, 'user1'));
157 $this->assertCount(2, message_search_users(0, 'user'));
161 * Test message_get_messages.
163 public function test_message_get_messages() {
166 $this->resetAfterTest(true);
168 // Set this user as the admin.
169 $this->setAdminUser();
171 $user1 = self
::getDataGenerator()->create_user();
172 $user2 = self
::getDataGenerator()->create_user();
173 $user3 = self
::getDataGenerator()->create_user();
175 \core_message\api
::add_contact($user1->id
, $user2->id
);
176 \core_message\api
::add_contact($user1->id
, $user3->id
);
178 // Create some individual conversations.
179 $ic1 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
180 [$user1->id
, $user2->id
]);
181 $ic2 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
182 [$user1->id
, $user3->id
]);
184 // Send some messages to individual conversations.
185 $im1 = testhelper
::send_fake_message_to_conversation($user1, $ic1->id
, 'Message 1');
186 $im2 = testhelper
::send_fake_message_to_conversation($user2, $ic1->id
, 'Message 2');
187 $im3 = testhelper
::send_fake_message_to_conversation($user1, $ic1->id
, 'Message 3');
188 $im4 = testhelper
::send_fake_message_to_conversation($user1, $ic2->id
, 'Message 4');
190 // Mark a message as read by user2.
191 $message = $DB->get_record('messages', ['id' => $im1]);
192 \core_message\api
::mark_message_as_read($user2->id
, $message);
194 // Retrieve unread messages sent from user1 to user2.
195 $lastmessages = message_get_messages($user2->id
, $user1->id
, 0, MESSAGE_GET_UNREAD
);
196 $this->assertCount(1, $lastmessages);
197 $this->assertArrayHasKey($im3, $lastmessages);
199 // Get only read messages.
200 $lastmessages = message_get_messages($user2->id
, $user1->id
, 0, MESSAGE_GET_READ
);
201 $this->assertCount(1, $lastmessages);
202 $this->assertArrayHasKey($im1, $lastmessages);
204 // Get both read and unread.
205 $lastmessages = message_get_messages($user2->id
, $user1->id
, 0, MESSAGE_GET_READ_AND_UNREAD
);
206 $this->assertCount(2, $lastmessages);
207 $this->assertArrayHasKey($im1, $lastmessages);
208 $this->assertArrayHasKey($im3, $lastmessages);
210 // Repeat retrieve read/unread messages but using a bool to test backwards compatibility.
211 $lastmessages = message_get_messages($user2->id
, $user1->id
, 0, false);
212 $this->assertCount(1, $lastmessages);
213 $this->assertArrayHasKey($im3, $lastmessages);
215 $lastmessages = message_get_messages($user2->id
, $user1->id
, 0, true);
216 $this->assertCount(1, $lastmessages);
217 $this->assertArrayHasKey($im1, $lastmessages);
219 // Create some group conversations.
220 $gc1 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
221 [$user1->id
, $user2->id
, $user3->id
], 'Group chat');
223 // Send some messages to group conversations.
224 $gm1 = testhelper
::send_fake_message_to_conversation($user1, $gc1->id
, 'Group message 1');
226 // Retrieve all messages sent from user1 to user2 (the result should be the same as before, because only individual
227 // conversations should be considered by the message_get_messages function).
228 $lastmessages = message_get_messages($user2->id
, $user1->id
, 0, MESSAGE_GET_READ_AND_UNREAD
);
229 $this->assertCount(2, $lastmessages);
230 $this->assertArrayHasKey($im1, $lastmessages);
231 $this->assertArrayHasKey($im3, $lastmessages);
235 * Test message_get_messages with only group conversations between users.
237 public function test_message_get_messages_only_group_conversations() {
238 $this->resetAfterTest(true);
240 // Set this user as the admin.
241 $this->setAdminUser();
243 $user1 = self
::getDataGenerator()->create_user();
244 $user2 = self
::getDataGenerator()->create_user();
245 $user3 = self
::getDataGenerator()->create_user();
247 // Create some group conversations.
248 $gc1 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
249 [$user1->id
, $user2->id
, $user3->id
], 'Group chat');
251 // Send some messages to group conversations.
252 $gm1 = testhelper
::send_fake_message_to_conversation($user1, $gc1->id
, 'Group message 1');
253 $gm2 = testhelper
::send_fake_message_to_conversation($user2, $gc1->id
, 'Group message 2');
255 // Retrieve all messages sent from user1 to user2. There shouldn't be messages, because only individual
256 // conversations should be considered by the message_get_messages function.
257 $lastmessages = message_get_messages($user2->id
, $user1->id
, 0, MESSAGE_GET_READ_AND_UNREAD
);
258 $this->assertCount(0, $lastmessages);