Merge branch 'MDL-63625-master' of git://github.com/marinaglancy/moodle
[moodle.git] / message / tests / messagelib_test.php
blobdbc7a0befbd2024999a2c39aa1885df71214f097
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 api's in message lib.
20 * @package core_message
21 * @category test
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();
28 global $CFG;
29 require_once($CFG->dirroot . '/message/lib.php');
31 /**
32 * Test api's in message lib.
34 * @package core_message
35 * @category test
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;
44 /**
45 * Test set up.
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();
55 /**
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 * @param int $notification if the message is a notification.
66 * @param int $time the time the message was sent
67 * @return int the id of the message
69 protected function send_fake_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) {
70 global $DB;
72 if (empty($time)) {
73 $time = time();
76 if ($notification) {
77 $record = new stdClass();
78 $record->useridfrom = $userfrom->id;
79 $record->useridto = $userto->id;
80 $record->subject = 'No subject';
81 $record->fullmessage = $message;
82 $record->smallmessage = $message;
83 $record->timecreated = $time;
85 return $DB->insert_record('notifications', $record);
88 if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
89 $conversation = \core_message\api::create_conversation(
90 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
92 $userfrom->id,
93 $userto->id
96 $conversationid = $conversation->id;
99 // Ok, send the message.
100 $record = new stdClass();
101 $record->useridfrom = $userfrom->id;
102 $record->conversationid = $conversationid;
103 $record->subject = 'No subject';
104 $record->fullmessage = $message;
105 $record->smallmessage = $message;
106 $record->timecreated = $time;
108 return $DB->insert_record('messages', $record);
112 * Test message_get_blocked_users.
114 public function test_message_get_blocked_users() {
115 global $USER;
117 // Set this user as the admin.
118 $this->setAdminUser();
120 // Create a user to add to the admin's contact list.
121 $user1 = $this->getDataGenerator()->create_user();
122 $user2 = $this->getDataGenerator()->create_user();
124 \core_message\api::block_user($USER->id, $user2->id);
126 $this->assertCount(1, message_get_blocked_users());
127 $this->assertDebuggingCalled();
129 // Block other user.
130 \core_message\api::block_user($USER->id, $user1->id);
131 $this->assertCount(2, message_get_blocked_users());
132 $this->assertDebuggingCalled();
134 // Test deleting users.
135 delete_user($user1);
136 $this->assertCount(1, message_get_blocked_users());
137 $this->assertDebuggingCalled();
141 * Test message_get_contacts.
143 public function test_message_get_contacts() {
144 global $USER, $CFG;
146 // Set this user as the admin.
147 $this->setAdminUser();
149 $noreplyuser = core_user::get_noreply_user();
150 $supportuser = core_user::get_support_user();
152 // Create a user to add to the admin's contact list.
153 $user1 = $this->getDataGenerator()->create_user();
154 $user2 = $this->getDataGenerator()->create_user();
155 $user3 = $this->getDataGenerator()->create_user(); // Stranger.
157 // Add users to the admin's contact list.
158 \core_message\api::add_contact($USER->id, $user1->id);
159 \core_message\api::add_contact($USER->id, $user2->id);
161 // Send some messages.
162 $this->send_fake_message($user1, $USER);
163 $this->send_fake_message($user2, $USER);
164 $this->send_fake_message($user3, $USER);
166 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
167 $this->assertDebuggingCalled();
168 $this->assertCount(0, $onlinecontacts);
169 $this->assertCount(2, $offlinecontacts);
170 $this->assertCount(1, $strangers);
172 // Send message from noreply and support users.
173 $this->send_fake_message($noreplyuser, $USER);
174 $this->send_fake_message($supportuser, $USER);
175 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
176 $this->assertDebuggingCalled();
177 $this->assertCount(0, $onlinecontacts);
178 $this->assertCount(2, $offlinecontacts);
179 $this->assertCount(3, $strangers);
181 // Block 1 user.
182 \core_message\api::block_user($USER->id, $user2->id);
183 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
184 $this->assertDebuggingCalled();
185 $this->assertCount(0, $onlinecontacts);
186 $this->assertCount(1, $offlinecontacts);
187 $this->assertCount(3, $strangers);
189 // Noreply user being valid user.
190 core_user::reset_internal_users();
191 $CFG->noreplyuserid = $user3->id;
192 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
193 $this->assertDebuggingCalled();
194 $this->assertCount(0, $onlinecontacts);
195 $this->assertCount(1, $offlinecontacts);
196 $this->assertCount(2, $strangers);
198 // Test deleting users.
199 delete_user($user1);
200 delete_user($user3);
201 core_user::reset_internal_users();
202 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
203 $this->assertDebuggingCalled();
204 $this->assertCount(0, $onlinecontacts);
205 $this->assertCount(0, $offlinecontacts);
206 $this->assertCount(1, $strangers);
210 * Test message_count_unread_messages.
212 public function test_message_count_unread_messages() {
213 // Create users to send and receive message.
214 $userfrom1 = $this->getDataGenerator()->create_user();
215 $userfrom2 = $this->getDataGenerator()->create_user();
216 $userto = $this->getDataGenerator()->create_user();
218 $this->assertEquals(0, message_count_unread_messages($userto));
220 // Send fake messages.
221 $this->send_fake_message($userfrom1, $userto);
222 $this->send_fake_message($userfrom2, $userto);
224 $this->assertEquals(2, message_count_unread_messages($userto));
225 $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
229 * Test message_count_unread_messages with read messages.
231 public function test_message_count_unread_messages_with_read_messages() {
232 global $DB;
234 // Create users to send and receive messages.
235 $userfrom1 = $this->getDataGenerator()->create_user();
236 $userfrom2 = $this->getDataGenerator()->create_user();
237 $userto = $this->getDataGenerator()->create_user();
239 $this->assertEquals(0, message_count_unread_messages($userto));
241 // Send fake messages.
242 $messageid = $this->send_fake_message($userfrom1, $userto);
243 $this->send_fake_message($userfrom2, $userto);
245 // Mark message as read.
246 $message = $DB->get_record('messages', ['id' => $messageid]);
247 \core_message\api::mark_message_as_read($userto->id, $message);
249 // Should only count the messages that weren't read by the current user.
250 $this->assertEquals(1, message_count_unread_messages($userto));
251 $this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
255 * Test message_count_unread_messages with deleted messages.
257 public function test_message_count_unread_messages_with_deleted_messages() {
258 global $DB;
260 // Create users to send and receive messages.
261 $userfrom1 = $this->getDataGenerator()->create_user();
262 $userfrom2 = $this->getDataGenerator()->create_user();
263 $userto = $this->getDataGenerator()->create_user();
265 $this->assertEquals(0, message_count_unread_messages($userto));
267 // Send fake messages.
268 $messageid = $this->send_fake_message($userfrom1, $userto);
269 $this->send_fake_message($userfrom2, $userto);
271 // Delete a message.
272 \core_message\api::delete_message($userto->id, $messageid);
274 // Should only count the messages that weren't deleted by the current user.
275 $this->assertEquals(1, message_count_unread_messages($userto));
276 $this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
280 * Test message_count_unread_messages with sent messages.
282 public function test_message_count_unread_messages_with_sent_messages() {
283 $userfrom = $this->getDataGenerator()->create_user();
284 $userto = $this->getDataGenerator()->create_user();
286 $this->send_fake_message($userfrom, $userto);
288 $this->assertEquals(0, message_count_unread_messages($userfrom));
292 * Test message_add_contact.
294 public function test_message_add_contact() {
295 global $DB, $USER;
297 // Set this user as the admin.
298 $this->setAdminUser();
300 // Create a user to add to the admin's contact list.
301 $user1 = $this->getDataGenerator()->create_user();
302 $user2 = $this->getDataGenerator()->create_user();
304 message_add_contact($user1->id);
305 $this->assertDebuggingCalled();
306 $this->assertEquals(1, $DB->count_records('message_contact_requests'));
308 message_add_contact($user2->id, 1);
309 $this->assertDebuggingCalled();
310 $this->assertEquals(1, $DB->count_records('message_users_blocked'));
312 message_add_contact($user2->id, 0);
313 $this->assertDebuggingCalled();
314 $this->assertEquals(0, $DB->count_records('message_users_blocked'));
318 * Test message_remove_contact.
320 public function test_message_remove_contact() {
321 global $USER;
323 // Set this user as the admin.
324 $this->setAdminUser();
326 // Create a user to add to the admin's contact list.
327 $user = $this->getDataGenerator()->create_user();
329 // Add the user to the admin's contact list.
330 \core_message\api::add_contact($USER->id, $user->id);
332 // Remove user from admin's contact list.
333 message_remove_contact($user->id);
334 $this->assertDebuggingCalled();
335 $this->assertEquals(false, message_get_contact($user->id));
336 $this->assertDebuggingCalled();
340 * Test message_block_contact.
342 public function test_message_block_contact() {
343 global $USER;
345 // Set this user as the admin.
346 $this->setAdminUser();
348 // Create a user to add to the admin's contact list.
349 $user1 = $this->getDataGenerator()->create_user();
350 $user2 = $this->getDataGenerator()->create_user();
352 // Add users to the admin's contact list.
353 \core_message\api::add_contact($USER->id, $user1->id);
354 \core_message\api::add_contact($USER->id, $user2->id);
356 $this->assertEquals(0, \core_message\api::count_blocked_users());
358 // Block 1 user.
359 message_block_contact($user2->id);
360 $this->assertDebuggingCalled();
361 $this->assertEquals(1, \core_message\api::count_blocked_users());
366 * Test message_unblock_contact.
368 public function test_message_unblock_contact() {
369 global $USER;
371 // Set this user as the admin.
372 $this->setAdminUser();
374 // Create a user to add to the admin's contact list.
375 $user1 = $this->getDataGenerator()->create_user();
377 // Add users to the admin's blocked list.
378 \core_message\api::block_user($USER->id, $user1->id);
379 $this->assertEquals(1, \core_message\api::count_blocked_users());
381 // Unblock user.
382 message_unblock_contact($user1->id);
383 $this->assertDebuggingCalled();
384 $this->assertEquals(0, \core_message\api::count_blocked_users());
388 * Test message_search_users.
390 public function test_message_search_users() {
391 global $USER;
393 // Set this user as the admin.
394 $this->setAdminUser();
396 // Create a user to add to the admin's contact list.
397 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
398 $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
400 // Add users to the admin's contact list.
401 \core_message\api::add_contact($USER->id, $user1->id);
402 \core_message\api::add_contact($USER->id, $user2->id);
404 $this->assertCount(1, message_search_users(0, 'Test1'));
405 $this->assertCount(2, message_search_users(0, 'Test'));
406 $this->assertCount(1, message_search_users(0, 'user1'));
407 $this->assertCount(2, message_search_users(0, 'user'));