Merge branch 'MDL-63012-master' of https://github.com/jobyh/moodle
[moodle.git] / message / tests / messagelib_test.php
blob231149aec8ce78dc34cd1e1ff63e4f6f94b7e7f9
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 $conversationid = \core_message\api::create_conversation_between_users([$userfrom->id,
90 $userto->id]);
93 // Ok, send the message.
94 $record = new stdClass();
95 $record->useridfrom = $userfrom->id;
96 $record->conversationid = $conversationid;
97 $record->subject = 'No subject';
98 $record->fullmessage = $message;
99 $record->smallmessage = $message;
100 $record->timecreated = $time;
102 return $DB->insert_record('messages', $record);
106 * Test message_get_blocked_users.
108 public function test_message_get_blocked_users() {
109 // Set this user as the admin.
110 $this->setAdminUser();
112 // Create a user to add to the admin's contact list.
113 $user1 = $this->getDataGenerator()->create_user();
114 $user2 = $this->getDataGenerator()->create_user();
116 // Add users to the admin's contact list.
117 message_add_contact($user1->id);
118 message_add_contact($user2->id, 1);
120 $this->assertCount(1, message_get_blocked_users());
121 $this->assertDebuggingCalled();
123 // Block other user.
124 message_block_contact($user1->id);
125 $this->assertCount(2, message_get_blocked_users());
126 $this->assertDebuggingCalled();
128 // Test deleting users.
129 delete_user($user1);
130 $this->assertCount(1, message_get_blocked_users());
131 $this->assertDebuggingCalled();
135 * Test message_get_contacts.
137 public function test_message_get_contacts() {
138 global $USER, $CFG;
140 // Set this user as the admin.
141 $this->setAdminUser();
143 $noreplyuser = core_user::get_noreply_user();
144 $supportuser = core_user::get_support_user();
146 // Create a user to add to the admin's contact list.
147 $user1 = $this->getDataGenerator()->create_user();
148 $user2 = $this->getDataGenerator()->create_user();
149 $user3 = $this->getDataGenerator()->create_user(); // Stranger.
151 // Add users to the admin's contact list.
152 message_add_contact($user1->id);
153 message_add_contact($user2->id);
155 // Send some messages.
156 $this->send_fake_message($user1, $USER);
157 $this->send_fake_message($user2, $USER);
158 $this->send_fake_message($user3, $USER);
160 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
161 $this->assertDebuggingCalled();
162 $this->assertCount(0, $onlinecontacts);
163 $this->assertCount(2, $offlinecontacts);
164 $this->assertCount(1, $strangers);
166 // Send message from noreply and support users.
167 $this->send_fake_message($noreplyuser, $USER);
168 $this->send_fake_message($supportuser, $USER);
169 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
170 $this->assertDebuggingCalled();
171 $this->assertCount(0, $onlinecontacts);
172 $this->assertCount(2, $offlinecontacts);
173 $this->assertCount(3, $strangers);
175 // Block 1 user.
176 message_block_contact($user2->id);
177 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
178 $this->assertDebuggingCalled();
179 $this->assertCount(0, $onlinecontacts);
180 $this->assertCount(1, $offlinecontacts);
181 $this->assertCount(3, $strangers);
183 // Noreply user being valid user.
184 core_user::reset_internal_users();
185 $CFG->noreplyuserid = $user3->id;
186 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
187 $this->assertDebuggingCalled();
188 $this->assertCount(0, $onlinecontacts);
189 $this->assertCount(1, $offlinecontacts);
190 $this->assertCount(2, $strangers);
192 // Test deleting users.
193 delete_user($user1);
194 delete_user($user3);
195 core_user::reset_internal_users();
196 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
197 $this->assertDebuggingCalled();
198 $this->assertCount(0, $onlinecontacts);
199 $this->assertCount(0, $offlinecontacts);
200 $this->assertCount(1, $strangers);
204 * Test message_count_unread_messages.
206 public function test_message_count_unread_messages() {
207 // Create users to send and receive message.
208 $userfrom1 = $this->getDataGenerator()->create_user();
209 $userfrom2 = $this->getDataGenerator()->create_user();
210 $userto = $this->getDataGenerator()->create_user();
212 $this->assertEquals(0, message_count_unread_messages($userto));
214 // Send fake messages.
215 $this->send_fake_message($userfrom1, $userto);
216 $this->send_fake_message($userfrom2, $userto);
218 $this->assertEquals(2, message_count_unread_messages($userto));
219 $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
223 * Test message_count_unread_messages with read messages.
225 public function test_message_count_unread_messages_with_read_messages() {
226 global $DB;
228 // Create users to send and receive messages.
229 $userfrom1 = $this->getDataGenerator()->create_user();
230 $userfrom2 = $this->getDataGenerator()->create_user();
231 $userto = $this->getDataGenerator()->create_user();
233 $this->assertEquals(0, message_count_unread_messages($userto));
235 // Send fake messages.
236 $messageid = $this->send_fake_message($userfrom1, $userto);
237 $this->send_fake_message($userfrom2, $userto);
239 // Mark message as read.
240 $message = $DB->get_record('messages', ['id' => $messageid]);
241 \core_message\api::mark_message_as_read($userto->id, $message);
243 // Should only count the messages that weren't read by the current user.
244 $this->assertEquals(1, message_count_unread_messages($userto));
245 $this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
249 * Test message_count_unread_messages with deleted messages.
251 public function test_message_count_unread_messages_with_deleted_messages() {
252 global $DB;
254 // Create users to send and receive messages.
255 $userfrom1 = $this->getDataGenerator()->create_user();
256 $userfrom2 = $this->getDataGenerator()->create_user();
257 $userto = $this->getDataGenerator()->create_user();
259 $this->assertEquals(0, message_count_unread_messages($userto));
261 // Send fake messages.
262 $messageid = $this->send_fake_message($userfrom1, $userto);
263 $this->send_fake_message($userfrom2, $userto);
265 // Delete a message.
266 \core_message\api::delete_message($userto->id, $messageid);
268 // Should only count the messages that weren't deleted by the current user.
269 $this->assertEquals(1, message_count_unread_messages($userto));
270 $this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
274 * Test message_add_contact.
276 public function test_message_add_contact() {
277 // Set this user as the admin.
278 $this->setAdminUser();
280 // Create a user to add to the admin's contact list.
281 $user1 = $this->getDataGenerator()->create_user();
282 $user2 = $this->getDataGenerator()->create_user();
283 $user3 = $this->getDataGenerator()->create_user();
285 message_add_contact($user1->id);
286 message_add_contact($user2->id, 0);
287 // Add duplicate contact and make sure only 1 record exists.
288 message_add_contact($user2->id, 1);
290 $this->assertNotEmpty(message_get_contact($user1->id));
291 $this->assertNotEmpty(message_get_contact($user2->id));
292 $this->assertEquals(false, message_get_contact($user3->id));
293 $this->assertEquals(1, \core_message\api::count_blocked_users());
297 * Test message_remove_contact.
299 public function test_message_remove_contact() {
300 // Set this user as the admin.
301 $this->setAdminUser();
303 // Create a user to add to the admin's contact list.
304 $user = $this->getDataGenerator()->create_user();
306 // Add the user to the admin's contact list.
307 message_add_contact($user->id);
308 $this->assertNotEmpty(message_get_contact($user->id));
310 // Remove user from admin's contact list.
311 message_remove_contact($user->id);
312 $this->assertEquals(false, message_get_contact($user->id));
316 * Test message_block_contact.
318 public function test_message_block_contact() {
319 // Set this user as the admin.
320 $this->setAdminUser();
322 // Create a user to add to the admin's contact list.
323 $user1 = $this->getDataGenerator()->create_user();
324 $user2 = $this->getDataGenerator()->create_user();
326 // Add users to the admin's contact list.
327 message_add_contact($user1->id);
328 message_add_contact($user2->id);
330 $this->assertEquals(0, \core_message\api::count_blocked_users());
332 // Block 1 user.
333 message_block_contact($user2->id);
334 $this->assertEquals(1, \core_message\api::count_blocked_users());
339 * Test message_unblock_contact.
341 public function test_message_unblock_contact() {
342 // Set this user as the admin.
343 $this->setAdminUser();
345 // Create a user to add to the admin's contact list.
346 $user1 = $this->getDataGenerator()->create_user();
347 $user2 = $this->getDataGenerator()->create_user();
349 // Add users to the admin's contact list.
350 message_add_contact($user1->id);
351 message_add_contact($user2->id, 1); // Add blocked contact.
353 $this->assertEquals(1, \core_message\api::count_blocked_users());
355 // Unblock user.
356 message_unblock_contact($user2->id);
357 $this->assertEquals(0, \core_message\api::count_blocked_users());
361 * Test message_search_users.
363 public function test_message_search_users() {
364 // Set this user as the admin.
365 $this->setAdminUser();
367 // Create a user to add to the admin's contact list.
368 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
369 $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
371 // Add users to the admin's contact list.
372 message_add_contact($user1->id);
373 message_add_contact($user2->id); // Add blocked contact.
375 $this->assertCount(1, message_search_users(0, 'Test1'));
376 $this->assertCount(2, message_search_users(0, 'Test'));
377 $this->assertCount(1, message_search_users(0, 'user1'));
378 $this->assertCount(2, message_search_users(0, 'user'));