MDL-52347 behat: fix random failures on fast computer
[moodle.git] / message / tests / messagelib_test.php
CommitLineData
66d212fe
RT
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.
13//
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/>.
16
17/**
18 * Test api's in message lib.
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot . '/message/lib.php');
30
31/**
32 * Test api's in message lib.
33 *
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
38 */
39class core_message_messagelib_testcase extends advanced_testcase {
40
41 /** @var phpunit_message_sink keep track of messages. */
42 protected $messagesink = null;
43
44 /**
45 * Test set up.
46 *
47 * This is executed before running any test in this file.
48 */
49 public function setUp() {
50 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
51 $this->messagesink = $this->redirectMessages();
52 $this->resetAfterTest();
53 }
54
55 /**
56 * Send a fake message.
57 *
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.
61 *
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.
068df636 65 * @return int the id of the message
66d212fe
RT
66 */
67 protected function send_fake_message($userfrom, $userto, $message = 'Hello world!') {
68 global $DB;
69
70 $record = new stdClass();
71 $record->useridfrom = $userfrom->id;
72 $record->useridto = $userto->id;
73 $record->subject = 'No subject';
74 $record->fullmessage = $message;
6c10b1cc 75 $record->smallmessage = $message;
66d212fe 76 $record->timecreated = time();
068df636
MN
77
78 return $DB->insert_record('message', $record);
66d212fe
RT
79 }
80
81 /**
82 * Test message_get_blocked_users.
83 */
84 public function test_message_get_blocked_users() {
85 // Set this user as the admin.
86 $this->setAdminUser();
87
88 // Create a user to add to the admin's contact list.
89 $user1 = $this->getDataGenerator()->create_user();
90 $user2 = $this->getDataGenerator()->create_user();
91
92 // Add users to the admin's contact list.
93 message_add_contact($user1->id);
94 message_add_contact($user2->id, 1);
95
96 $this->assertCount(1, message_get_blocked_users());
97
98 // Block other user.
99 message_block_contact($user1->id);
100 $this->assertCount(2, message_get_blocked_users());
01393790
JL
101
102 // Test deleting users.
103 delete_user($user1);
104 $this->assertCount(1, message_get_blocked_users());
66d212fe
RT
105 }
106
107 /**
108 * Test message_get_contacts.
109 */
110 public function test_message_get_contacts() {
111 global $USER, $CFG;
112
113 // Set this user as the admin.
114 $this->setAdminUser();
115
116 $noreplyuser = core_user::get_noreply_user();
117 $supportuser = core_user::get_support_user();
118
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.
123
124 // Add users to the admin's contact list.
125 message_add_contact($user1->id);
126 message_add_contact($user2->id);
127
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);
132
133 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
134 $this->assertCount(0, $onlinecontacts);
135 $this->assertCount(2, $offlinecontacts);
136 $this->assertCount(1, $strangers);
137
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);
145
146 // Block 1 user.
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);
152
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);
01393790
JL
161
162 // Test deleting users.
163 delete_user($user1);
164 delete_user($user3);
165
166 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
167 $this->assertCount(0, $onlinecontacts);
168 $this->assertCount(0, $offlinecontacts);
169 $this->assertCount(1, $strangers);
66d212fe
RT
170 }
171
172 /**
173 * Test message_count_messages.
174 */
175 public function test_message_count_messages() {
176 global $DB;
177
178 // Create users to send and receive message.
179 $userfrom = $this->getDataGenerator()->create_user();
180 $userto = $this->getDataGenerator()->create_user();
181
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);
185
186 // Return 0 when no message.
187 $messages = array();
188 $this->assertEquals(0, message_count_messages($messages, 'Test', 'Test'));
189
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));
194 }
195
196 /**
197 * Test message_count_unread_messages.
198 */
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();
204
205 $this->assertEquals(0, message_count_unread_messages($userto));
206
207 // Send fake messages.
208 $this->send_fake_message($userfrom1, $userto);
209 $this->send_fake_message($userfrom2, $userto);
210
211 $this->assertEquals(2, message_count_unread_messages($userto));
212 $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
213 }
214
215 /**
216 * Test message_count_blocked_users.
217 *
218 */
219 public function test_message_count_blocked_users() {
220 // Set this user as the admin.
221 $this->setAdminUser();
222
223 // Create users to add to the admin's contact list.
224 $user1 = $this->getDataGenerator()->create_user();
225 $user2 = $this->getDataGenerator()->create_user();
226
227 $this->assertEquals(0, message_count_blocked_users());
228
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);
232
233 $this->assertEquals(0, message_count_blocked_users($user2));
234 $this->assertEquals(1, message_count_blocked_users());
235 }
236
237 /**
238 * Test message_add_contact.
239 */
240 public function test_message_add_contact() {
241 // Set this user as the admin.
242 $this->setAdminUser();
243
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();
248
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);
253
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());
258 }
259
260 /**
261 * Test message_remove_contact.
262 */
263 public function test_message_remove_contact() {
264 // Set this user as the admin.
265 $this->setAdminUser();
266
267 // Create a user to add to the admin's contact list.
268 $user = $this->getDataGenerator()->create_user();
269
270 // Add the user to the admin's contact list.
271 message_add_contact($user->id);
272 $this->assertNotEmpty(message_get_contact($user->id));
273
274 // Remove user from admin's contact list.
275 message_remove_contact($user->id);
276 $this->assertEquals(false, message_get_contact($user->id));
277 }
278
279 /**
280 * Test message_block_contact.
281 */
282 public function test_message_block_contact() {
283 // Set this user as the admin.
284 $this->setAdminUser();
285
286 // Create a user to add to the admin's contact list.
287 $user1 = $this->getDataGenerator()->create_user();
288 $user2 = $this->getDataGenerator()->create_user();
289
290 // Add users to the admin's contact list.
291 message_add_contact($user1->id);
292 message_add_contact($user2->id);
293
294 $this->assertEquals(0, message_count_blocked_users());
295
296 // Block 1 user.
297 message_block_contact($user2->id);
298 $this->assertEquals(1, message_count_blocked_users());
299
300 }
301
302 /**
303 * Test message_unblock_contact.
304 */
305 public function test_message_unblock_contact() {
306 // Set this user as the admin.
307 $this->setAdminUser();
308
309 // Create a user to add to the admin's contact list.
310 $user1 = $this->getDataGenerator()->create_user();
311 $user2 = $this->getDataGenerator()->create_user();
312
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.
316
317 $this->assertEquals(1, message_count_blocked_users());
318
319 // Unblock user.
320 message_unblock_contact($user2->id);
321 $this->assertEquals(0, message_count_blocked_users());
322 }
323
324 /**
325 * Test message_search_users.
326 */
327 public function test_message_search_users() {
328 // Set this user as the admin.
329 $this->setAdminUser();
330
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'));
334
335 // Add users to the admin's contact list.
336 message_add_contact($user1->id);
337 message_add_contact($user2->id); // Add blocked contact.
338
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'));
343 }
344
345 /**
346 * Test message_search.
347 */
348 public function test_message_search() {
349 global $USER;
350
351 // Set this user as the admin.
352 $this->setAdminUser();
353
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'));
357
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);
362
363 $this->assertCount(2, message_search(array('Message'), true, false));
364 $this->assertCount(3, message_search(array('Message'), true, true));
365
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));
371
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));
376 }
068df636
MN
377
378 /**
6c10b1cc
AN
379 * The data provider for message_get_recent_conversations.
380 *
381 * This provides sets of data to for testing.
382 * @return array
068df636 383 */
6c10b1cc
AN
384 public function message_get_recent_conversations_provider() {
385 return array(
386 array(
387 // Test that conversations with multiple contacts is correctly ordered.
388 'users' => array(
389 'user1',
390 'user2',
391 'user3',
392 ),
393 'messages' => array(
394 array(
395 'from' => 'user1',
396 'to' => 'user2',
397 'state' => 'unread',
398 'subject' => 'S1',
399 ),
400 array(
401 'from' => 'user2',
402 'to' => 'user1',
403 'state' => 'unread',
404 'subject' => 'S2',
405 ),
406 array(
407 'from' => 'user1',
408 'to' => 'user2',
409 'state' => 'unread',
410 'timecreated' => 0,
411 'subject' => 'S3',
412 ),
413 array(
414 'from' => 'user1',
415 'to' => 'user3',
416 'state' => 'read',
417 'timemodifier' => 1,
418 'subject' => 'S4',
419 ),
420 array(
421 'from' => 'user3',
422 'to' => 'user1',
423 'state' => 'read',
424 'timemodifier' => 1,
425 'subject' => 'S5',
426 ),
427 array(
428 'from' => 'user1',
429 'to' => 'user3',
430 'state' => 'read',
431 'timecreated' => 0,
432 'subject' => 'S6',
433 ),
434 ),
435 'expectations' => array(
436 'user1' => array(
437 // User1 has conversed most recently with user3. The most recent message is M5.
438 array(
439 'messageposition' => 0,
440 'with' => 'user3',
441 'subject' => 'S5',
442 ),
443 // User1 has also conversed with user2. The most recent message is S2.
444 array(
445 'messageposition' => 1,
446 'with' => 'user2',
447 'subject' => 'S2',
448 ),
449 ),
450 'user2' => array(
451 // User2 has only conversed with user1. Their most recent shared message was S2.
452 array(
453 'messageposition' => 0,
454 'with' => 'user1',
455 'subject' => 'S2',
456 ),
457 ),
458 'user3' => array(
459 // User3 has only conversed with user1. Their most recent shared message was S5.
460 array(
461 'messageposition' => 0,
462 'with' => 'user1',
463 'subject' => 'S5',
464 ),
465 ),
466 ),
467 ),
468 array(
469 // Test conversations with a single user, where some messages are read and some are not.
470 'users' => array(
471 'user1',
472 'user2',
473 ),
474 'messages' => array(
475 array(
476 'from' => 'user1',
477 'to' => 'user2',
478 'state' => 'read',
479 'subject' => 'S1',
480 ),
481 array(
482 'from' => 'user2',
483 'to' => 'user1',
484 'state' => 'read',
485 'subject' => 'S2',
486 ),
487 array(
488 'from' => 'user1',
489 'to' => 'user2',
490 'state' => 'unread',
491 'timemodifier' => 1,
492 'subject' => 'S3',
493 ),
494 array(
495 'from' => 'user1',
496 'to' => 'user2',
497 'state' => 'unread',
498 'timemodifier' => 1,
499 'subject' => 'S4',
500 ),
501 ),
502 'expectations' => array(
503 // The most recent message between user1 and user2 was S4.
504 'user1' => array(
505 array(
506 'messageposition' => 0,
507 'with' => 'user2',
508 'subject' => 'S4',
509 ),
510 ),
511 'user2' => array(
512 // The most recent message between user1 and user2 was S4.
513 array(
514 'messageposition' => 0,
515 'with' => 'user1',
516 'subject' => 'S4',
517 ),
518 ),
519 ),
520 ),
521 array(
522 // Test conversations with a single user, where some messages are read and some are not, and messages
523 // are out of order.
524 // This can happen through a combination of factors including multi-master DB replication with messages
525 // read somehow (e.g. API).
526 'users' => array(
527 'user1',
528 'user2',
529 ),
530 'messages' => array(
531 array(
532 'from' => 'user1',
533 'to' => 'user2',
534 'state' => 'read',
535 'subject' => 'S1',
536 'timemodifier' => 1,
537 ),
538 array(
539 'from' => 'user2',
540 'to' => 'user1',
541 'state' => 'read',
542 'subject' => 'S2',
543 'timemodifier' => 2,
544 ),
545 array(
546 'from' => 'user1',
547 'to' => 'user2',
548 'state' => 'unread',
549 'subject' => 'S3',
550 ),
551 array(
552 'from' => 'user1',
553 'to' => 'user2',
554 'state' => 'unread',
555 'subject' => 'S4',
556 ),
557 ),
558 'expectations' => array(
559 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
560 'user1' => array(
561 array(
562 'messageposition' => 0,
563 'with' => 'user2',
564 'subject' => 'S2',
565 ),
566 ),
567 'user2' => array(
568 array(
569 'messageposition' => 0,
570 'with' => 'user1',
571 'subject' => 'S2',
572 ),
573 ),
574 ),
575 ),
576 );
577 }
068df636 578
6c10b1cc
AN
579 /**
580 * Test message_get_recent_conversations with a mixture of messages.
581 *
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.
586 */
587 public function test_message_get_recent_conversations($usersdata, $messagesdata, $expectations) {
588 global $DB;
068df636 589
6c10b1cc
AN
590 // Create all of the users.
591 $users = array();
592 foreach ($usersdata as $username) {
593 $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
594 }
595
596 $defaulttimecreated = time();
597 foreach ($messagesdata as $messagedata) {
598 $from = $users[$messagedata['from']];
599 $to = $users[$messagedata['to']];
600 $subject = $messagedata['subject'];
601
602 if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
603 $table = 'message';
604 $messageid = $this->send_fake_message($from, $to, $subject, FORMAT_PLAIN);
605 } else {
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);
609 }
610
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'];
617 } else {
618 $updatemessage->timecreated = $defaulttimecreated;
619 }
620 $DB->update_record($table, $updatemessage);
621 }
622
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);
632 }
633 }
068df636
MN
634 }
635
636 /**
637 * Test message_get_recent_notifications.
638 */
639 public function test_message_get_recent_notifications() {
640 global $DB, $USER;
641
642 // Set this user as the admin.
643 $this->setAdminUser();
644
645 // Create a user to send messages from.
646 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
647
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);
651
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);
657
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);
663
664 $notifications = message_get_recent_notifications($USER);
665
666 // Get the messages.
667 $firstmessage = array_shift($notifications);
668 $secondmessage = array_shift($notifications);
669
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);
673 }
ceb689b9
RW
674
675 /**
676 * Test that message_can_post_message returns false if the sender does not have the
677 * moode/site:sendmessage capability.
678 */
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);
685
686 assign_capability('moodle/site:sendmessage', CAP_PROHIBIT, $roleid, $context);
687
688 $this->assertFalse(message_can_post_message($recipient, $sender));
689 }
690
691 /**
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.
694 */
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'));
698
699 set_user_preference('message_blocknoncontacts', true, $recipient);
700
701 $this->assertFalse(message_can_post_message($recipient, $sender));
702 }
703
704 /**
705 * Test that message_can_post_message returns false if the receiver has blocked the
706 * sender from messaging them.
707 */
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'));
711
712 $this->setUser($recipient);
713 message_block_contact($sender->id);
714
715 $this->assertFalse(message_can_post_message($recipient, $sender));
716 }
717
718 /**
719 * Test that message_can_post_message returns false if the receiver has blocked the
720 * sender from messaging them.
721 */
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'));
725
726 $this->assertTrue(message_can_post_message($recipient, $sender));
727 }
728
729 /**
730 * Test that message_is_user_non_contact_blocked returns false if the recipient allows
731 * messages from non-contacts.
732 */
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'));
736
737 set_user_preference('message_blocknoncontacts', false, $recipient);
738
739 $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
740 }
741
742 /**
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.
745 */
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'));
749
750 set_user_preference('message_blocknoncontacts', true, $recipient);
751
752 $this->assertTrue(message_is_user_non_contact_blocked($recipient, $sender));
753 }
754
755 /**
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.
758 */
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'));
762
763 $this->setUser($recipient);
764 set_user_preference('message_blocknoncontacts', true, $recipient);
765 message_add_contact($sender->id);
766
767 $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
768 }
769
770 /**
771 * Test that message_is_user_blocked returns false if the sender is not a contact of
772 * the recipient.
773 */
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'));
777
778 $this->assertFalse(message_is_user_blocked($recipient, $sender));
779 }
780
781 /**
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.
784 */
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'));
788
789 $this->setUser($recipient);
790 message_block_contact($sender->id);
791
792 $context = context_system::instance();
793 $roleid = $this->getDataGenerator()->create_role();
794 $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
795
796 assign_capability('moodle/site:readallmessages', CAP_ALLOW, $roleid, $context);
797
798 $this->assertFalse(message_is_user_blocked($recipient, $sender));
799 }
800
801 /**
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.
804 */
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'));
808
809 $this->setUser($recipient);
810 message_block_contact($sender->id);
811
812 $context = context_system::instance();
813 $roleid = $this->getDataGenerator()->create_role();
814 $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
815
816 assign_capability('moodle/site:readallmessages', CAP_PROHIBIT, $roleid, $context);
817
818 $this->assertTrue(message_is_user_blocked($recipient, $sender));
819 }
66d212fe 820}