MDL-48374 behat: Check flag before searching for span on page
[moodle.git] / message / tests / messagelib_test.php
blob57006d0613250d354460017389b2bb5d4d3139ae
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 * @return int the id of the message
67 protected function send_fake_message($userfrom, $userto, $message = 'Hello world!') {
68 global $DB;
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->timecreated = time();
77 return $DB->insert_record('message', $record);
80 /**
81 * Test message_get_blocked_users.
83 public function test_message_get_blocked_users() {
84 // Set this user as the admin.
85 $this->setAdminUser();
87 // Create a user to add to the admin's contact list.
88 $user1 = $this->getDataGenerator()->create_user();
89 $user2 = $this->getDataGenerator()->create_user();
91 // Add users to the admin's contact list.
92 message_add_contact($user1->id);
93 message_add_contact($user2->id, 1);
95 $this->assertCount(1, message_get_blocked_users());
97 // Block other user.
98 message_block_contact($user1->id);
99 $this->assertCount(2, message_get_blocked_users());
103 * Test message_get_contacts.
105 public function test_message_get_contacts() {
106 global $USER, $CFG;
108 // Set this user as the admin.
109 $this->setAdminUser();
111 $noreplyuser = core_user::get_noreply_user();
112 $supportuser = core_user::get_support_user();
114 // Create a user to add to the admin's contact list.
115 $user1 = $this->getDataGenerator()->create_user();
116 $user2 = $this->getDataGenerator()->create_user();
117 $user3 = $this->getDataGenerator()->create_user(); // Stranger.
119 // Add users to the admin's contact list.
120 message_add_contact($user1->id);
121 message_add_contact($user2->id);
123 // Send some messages.
124 $this->send_fake_message($user1, $USER);
125 $this->send_fake_message($user2, $USER);
126 $this->send_fake_message($user3, $USER);
128 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
129 $this->assertCount(0, $onlinecontacts);
130 $this->assertCount(2, $offlinecontacts);
131 $this->assertCount(1, $strangers);
133 // Send message from noreply and support users.
134 $this->send_fake_message($noreplyuser, $USER);
135 $this->send_fake_message($supportuser, $USER);
136 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
137 $this->assertCount(0, $onlinecontacts);
138 $this->assertCount(2, $offlinecontacts);
139 $this->assertCount(3, $strangers);
141 // Block 1 user.
142 message_block_contact($user2->id);
143 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
144 $this->assertCount(0, $onlinecontacts);
145 $this->assertCount(1, $offlinecontacts);
146 $this->assertCount(3, $strangers);
148 // Noreply user being valid user.
149 core_user::reset_internal_users();
150 $CFG->noreplyuserid = $user3->id;
151 $noreplyuser = core_user::get_noreply_user();
152 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
153 $this->assertCount(0, $onlinecontacts);
154 $this->assertCount(1, $offlinecontacts);
155 $this->assertCount(2, $strangers);
159 * Test message_count_messages.
161 public function test_message_count_messages() {
162 global $DB;
164 // Create users to send and receive message.
165 $userfrom = $this->getDataGenerator()->create_user();
166 $userto = $this->getDataGenerator()->create_user();
168 message_post_message($userfrom, $userto, 'Message 1', FORMAT_PLAIN);
169 message_post_message($userfrom, $userto, 'Message 2', FORMAT_PLAIN);
170 message_post_message($userto, $userfrom, 'Message 3', FORMAT_PLAIN);
172 // Return 0 when no message.
173 $messages = array();
174 $this->assertEquals(0, message_count_messages($messages, 'Test', 'Test'));
176 // Check number of messages from userfrom and userto.
177 $messages = $this->messagesink->get_messages();
178 $this->assertEquals(2, message_count_messages($messages, 'useridfrom', $userfrom->id));
179 $this->assertEquals(1, message_count_messages($messages, 'useridfrom', $userto->id));
183 * Test message_count_unread_messages.
185 public function test_message_count_unread_messages() {
186 // Create users to send and receive message.
187 $userfrom1 = $this->getDataGenerator()->create_user();
188 $userfrom2 = $this->getDataGenerator()->create_user();
189 $userto = $this->getDataGenerator()->create_user();
191 $this->assertEquals(0, message_count_unread_messages($userto));
193 // Send fake messages.
194 $this->send_fake_message($userfrom1, $userto);
195 $this->send_fake_message($userfrom2, $userto);
197 $this->assertEquals(2, message_count_unread_messages($userto));
198 $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
202 * Test message_count_blocked_users.
205 public function test_message_count_blocked_users() {
206 // Set this user as the admin.
207 $this->setAdminUser();
209 // Create users to add to the admin's contact list.
210 $user1 = $this->getDataGenerator()->create_user();
211 $user2 = $this->getDataGenerator()->create_user();
213 $this->assertEquals(0, message_count_blocked_users());
215 // Add 1 blocked and 1 normal contact to admin's contact list.
216 message_add_contact($user1->id);
217 message_add_contact($user2->id, 1);
219 $this->assertEquals(0, message_count_blocked_users($user2));
220 $this->assertEquals(1, message_count_blocked_users());
224 * Test message_add_contact.
226 public function test_message_add_contact() {
227 // Set this user as the admin.
228 $this->setAdminUser();
230 // Create a user to add to the admin's contact list.
231 $user1 = $this->getDataGenerator()->create_user();
232 $user2 = $this->getDataGenerator()->create_user();
233 $user3 = $this->getDataGenerator()->create_user();
235 message_add_contact($user1->id);
236 message_add_contact($user2->id, 0);
237 // Add duplicate contact and make sure only 1 record exists.
238 message_add_contact($user2->id, 1);
240 $this->assertNotEmpty(message_get_contact($user1->id));
241 $this->assertNotEmpty(message_get_contact($user2->id));
242 $this->assertEquals(false, message_get_contact($user3->id));
243 $this->assertEquals(1, message_count_blocked_users());
247 * Test message_remove_contact.
249 public function test_message_remove_contact() {
250 // Set this user as the admin.
251 $this->setAdminUser();
253 // Create a user to add to the admin's contact list.
254 $user = $this->getDataGenerator()->create_user();
256 // Add the user to the admin's contact list.
257 message_add_contact($user->id);
258 $this->assertNotEmpty(message_get_contact($user->id));
260 // Remove user from admin's contact list.
261 message_remove_contact($user->id);
262 $this->assertEquals(false, message_get_contact($user->id));
266 * Test message_block_contact.
268 public function test_message_block_contact() {
269 // Set this user as the admin.
270 $this->setAdminUser();
272 // Create a user to add to the admin's contact list.
273 $user1 = $this->getDataGenerator()->create_user();
274 $user2 = $this->getDataGenerator()->create_user();
276 // Add users to the admin's contact list.
277 message_add_contact($user1->id);
278 message_add_contact($user2->id);
280 $this->assertEquals(0, message_count_blocked_users());
282 // Block 1 user.
283 message_block_contact($user2->id);
284 $this->assertEquals(1, message_count_blocked_users());
289 * Test message_unblock_contact.
291 public function test_message_unblock_contact() {
292 // Set this user as the admin.
293 $this->setAdminUser();
295 // Create a user to add to the admin's contact list.
296 $user1 = $this->getDataGenerator()->create_user();
297 $user2 = $this->getDataGenerator()->create_user();
299 // Add users to the admin's contact list.
300 message_add_contact($user1->id);
301 message_add_contact($user2->id, 1); // Add blocked contact.
303 $this->assertEquals(1, message_count_blocked_users());
305 // Unblock user.
306 message_unblock_contact($user2->id);
307 $this->assertEquals(0, message_count_blocked_users());
311 * Test message_search_users.
313 public function test_message_search_users() {
314 // Set this user as the admin.
315 $this->setAdminUser();
317 // Create a user to add to the admin's contact list.
318 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
319 $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
321 // Add users to the admin's contact list.
322 message_add_contact($user1->id);
323 message_add_contact($user2->id); // Add blocked contact.
325 $this->assertCount(1, message_search_users(0, 'Test1'));
326 $this->assertCount(2, message_search_users(0, 'Test'));
327 $this->assertCount(1, message_search_users(0, 'user1'));
328 $this->assertCount(2, message_search_users(0, 'user'));
332 * Test message_search.
334 public function test_message_search() {
335 global $USER;
337 // Set this user as the admin.
338 $this->setAdminUser();
340 // Create a user to add to the admin's contact list.
341 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
342 $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
344 // Send few messages, real (read).
345 message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
346 message_post_message($USER, $user1, 'Message 2', FORMAT_PLAIN);
347 message_post_message($USER, $user2, 'Message 3', FORMAT_PLAIN);
349 $this->assertCount(2, message_search(array('Message'), true, false));
350 $this->assertCount(3, message_search(array('Message'), true, true));
352 // Send fake message (not-read).
353 $this->send_fake_message($USER, $user1, 'Message 4');
354 $this->send_fake_message($user1, $USER, 'Message 5');
355 $this->assertCount(3, message_search(array('Message'), true, false));
356 $this->assertCount(5, message_search(array('Message'), true, true));
358 // If courseid given then should be 0.
359 $this->assertEquals(false, message_search(array('Message'), true, true, ''));
360 $this->assertEquals(false, message_search(array('Message'), true, true, 2));
361 $this->assertCount(5, message_search(array('Message'), true, true, SITEID));
365 * Test message_get_recent_conversations.
367 public function test_message_get_recent_conversations() {
368 global $DB, $USER;
370 // Set this user as the admin.
371 $this->setAdminUser();
373 // Create user's to send messages to/from.
374 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
375 $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
377 // Add a few messages that have been read and some that are unread.
378 $m1 = $this->send_fake_message($USER, $user1, 'Message 1'); // An unread message.
379 $m2 = $this->send_fake_message($user1, $USER, 'Message 2'); // An unread message.
380 $m3 = $this->send_fake_message($USER, $user1, 'Message 3'); // An unread message.
381 $m4 = message_post_message($USER, $user2, 'Message 4', FORMAT_PLAIN);
382 $m5 = message_post_message($user2, $USER, 'Message 5', FORMAT_PLAIN);
383 $m6 = message_post_message($USER, $user2, 'Message 6', FORMAT_PLAIN);
385 // We want to alter the timecreated values so we can ensure message_get_recent_conversations orders
386 // by timecreated, not the max id, to begin with. However, we also want more than one message to have
387 // the same timecreated value to ensure that when this happens we retrieve the one with the maximum id.
389 // Store the current time.
390 $time = time();
392 // Set the first and second unread messages to have the same timecreated value.
393 $updatemessage = new stdClass();
394 $updatemessage->id = $m1;
395 $updatemessage->timecreated = $time;
396 $DB->update_record('message', $updatemessage);
398 $updatemessage->id = $m2;
399 $updatemessage->timecreated = $time;
400 $DB->update_record('message', $updatemessage);
402 // Set the third unread message to have a timecreated value of 0.
403 $updatemessage->id = $m3;
404 $updatemessage->timecreated = 0;
405 $DB->update_record('message', $updatemessage);
407 // Set the first and second read messages to have the same timecreated value.
408 $updatemessage->id = $m4;
409 $updatemessage->timecreated = $time + 1;
410 $DB->update_record('message', $updatemessage);
412 $updatemessage->id = $m5;
413 $updatemessage->timecreated = $time + 1;
414 $DB->update_record('message', $updatemessage);
416 // Set the third read message to have a timecreated value of 0.
417 $updatemessage->id = $m6;
418 $updatemessage->timecreated = 0;
419 $DB->update_record('message_read', $updatemessage);
421 // Get the recent conversations for the current user.
422 $conversations = message_get_recent_conversations($USER);
424 // Confirm that we have received the messages with the maximum timecreated, rather than the max id.
425 $this->assertEquals('Message 2', $conversations[0]->fullmessage);
426 $this->assertEquals('Message 5', $conversations[1]->smallmessage);
430 * Test message_get_recent_notifications.
432 public function test_message_get_recent_notifications() {
433 global $DB, $USER;
435 // Set this user as the admin.
436 $this->setAdminUser();
438 // Create a user to send messages from.
439 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
441 // Add two messages - will mark them as notifications later.
442 $m1 = message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
443 $m2 = message_post_message($user1, $USER, 'Message 2', FORMAT_PLAIN);
445 // Mark the second message as a notification.
446 $updatemessage = new stdClass();
447 $updatemessage->id = $m2;
448 $updatemessage->notification = 1;
449 $DB->update_record('message_read', $updatemessage);
451 // Mark the first message as a notification and change the timecreated to 0.
452 $updatemessage->id = $m1;
453 $updatemessage->notification = 1;
454 $updatemessage->timecreated = 0;
455 $DB->update_record('message_read', $updatemessage);
457 $notifications = message_get_recent_notifications($USER);
459 // Get the messages.
460 $firstmessage = array_shift($notifications);
461 $secondmessage = array_shift($notifications);
463 // Confirm that we have received the notifications with the maximum timecreated, rather than the max id.
464 $this->assertEquals('Message 2', $firstmessage->smallmessage);
465 $this->assertEquals('Message 1', $secondmessage->smallmessage);