[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_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 */ 39 class 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. 65 * @return int the id of the message 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; 75 $record->smallmessage = $message; 76 $record->timecreated = time(); 77 78 return $DB->insert_record('message', $record); 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()); 101 102 // Test deleting users. 103 delete_user($user1); 104 $this->assertCount(1, message_get_blocked_users()); 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); 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); 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 } 377 378 /** 379 * The data provider for message_get_recent_conversations. 380 * 381 * This provides sets of data to for testing. 382 * @return array 383 */ 384 public function message_get_recent_conversations_provider() { 385 return array( 386 'Test that conversations with messages contacts is correctly ordered.' => array( 387 'users' => array( 388 'user1', 389 'user2', 390 'user3', 391 ), 392 'contacts' => array( 393 ), 394 'messages' => array( 395 array( 396 'from' => 'user1', 397 'to' => 'user2', 398 'state' => 'unread', 399 'subject' => 'S1', 400 ), 401 array( 402 'from' => 'user2', 403 'to' => 'user1', 404 'state' => 'unread', 405 'subject' => 'S2', 406 ), 407 array( 408 'from' => 'user1', 409 'to' => 'user2', 410 'state' => 'unread', 411 'timecreated' => 0, 412 'subject' => 'S3', 413 ), 414 array( 415 'from' => 'user1', 416 'to' => 'user3', 417 'state' => 'read', 418 'timemodifier' => 1, 419 'subject' => 'S4', 420 ), 421 array( 422 'from' => 'user3', 423 'to' => 'user1', 424 'state' => 'read', 425 'timemodifier' => 1, 426 'subject' => 'S5', 427 ), 428 array( 429 'from' => 'user1', 430 'to' => 'user3', 431 'state' => 'read', 432 'timecreated' => 0, 433 'subject' => 'S6', 434 ), 435 ), 436 'expectations' => array( 437 'user1' => array( 438 // User1 has conversed most recently with user3. The most recent message is M5. 439 array( 440 'messageposition' => 0, 441 'with' => 'user3', 442 'subject' => 'S5', 443 ), 444 // User1 has also conversed with user2. The most recent message is S2. 445 array( 446 'messageposition' => 1, 447 'with' => 'user2', 448 'subject' => 'S2', 449 ), 450 ), 451 'user2' => array( 452 // User2 has only conversed with user1. Their most recent shared message was S2. 453 array( 454 'messageposition' => 0, 455 'with' => 'user1', 456 'subject' => 'S2', 457 ), 458 ), 459 'user3' => array( 460 // User3 has only conversed with user1. Their most recent shared message was S5. 461 array( 462 'messageposition' => 0, 463 'with' => 'user1', 464 'subject' => 'S5', 465 ), 466 ), 467 ), 468 ), 469 'Test that users with contacts and messages to self work as expected' => array( 470 'users' => array( 471 'user1', 472 'user2', 473 'user3', 474 ), 475 'contacts' => array( 476 'user1' => array( 477 'user2' => 0, 478 'user3' => 0, 479 ), 480 'user2' => array( 481 'user3' => 0, 482 ), 483 ), 484 'messages' => array( 485 array( 486 'from' => 'user1', 487 'to' => 'user1', 488 'state' => 'unread', 489 'subject' => 'S1', 490 ), 491 array( 492 'from' => 'user1', 493 'to' => 'user1', 494 'state' => 'unread', 495 'subject' => 'S2', 496 ), 497 ), 498 'expectations' => array( 499 'user1' => array( 500 // User1 has conversed most recently with user1. The most recent message is S2. 501 array( 502 'messageposition' => 0, 503 'with' => 'user1', 504 'subject' => 'S2', 505 ), 506 ), 507 ), 508 ), 509 'Test conversations with a single user, where some messages are read and some are not.' => array( 510 'users' => array( 511 'user1', 512 'user2', 513 ), 514 'contacts' => array( 515 ), 516 'messages' => array( 517 array( 518 'from' => 'user1', 519 'to' => 'user2', 520 'state' => 'read', 521 'subject' => 'S1', 522 ), 523 array( 524 'from' => 'user2', 525 'to' => 'user1', 526 'state' => 'read', 527 'subject' => 'S2', 528 ), 529 array( 530 'from' => 'user1', 531 'to' => 'user2', 532 'state' => 'unread', 533 'timemodifier' => 1, 534 'subject' => 'S3', 535 ), 536 array( 537 'from' => 'user1', 538 'to' => 'user2', 539 'state' => 'unread', 540 'timemodifier' => 1, 541 'subject' => 'S4', 542 ), 543 ), 544 'expectations' => array( 545 // The most recent message between user1 and user2 was S4. 546 'user1' => array( 547 array( 548 'messageposition' => 0, 549 'with' => 'user2', 550 'subject' => 'S4', 551 ), 552 ), 553 'user2' => array( 554 // The most recent message between user1 and user2 was S4. 555 array( 556 'messageposition' => 0, 557 'with' => 'user1', 558 'subject' => 'S4', 559 ), 560 ), 561 ), 562 ), 563 'Test conversations with a single user, where some messages are read and some are not, and messages ' . 564 'are out of order' => array( 565 // This can happen through a combination of factors including multi-master DB replication with messages 566 // read somehow (e.g. API). 567 'users' => array( 568 'user1', 569 'user2', 570 ), 571 'contacts' => array( 572 ), 573 'messages' => array( 574 array( 575 'from' => 'user1', 576 'to' => 'user2', 577 'state' => 'read', 578 'subject' => 'S1', 579 'timemodifier' => 1, 580 ), 581 array( 582 'from' => 'user2', 583 'to' => 'user1', 584 'state' => 'read', 585 'subject' => 'S2', 586 'timemodifier' => 2, 587 ), 588 array( 589 'from' => 'user1', 590 'to' => 'user2', 591 'state' => 'unread', 592 'subject' => 'S3', 593 ), 594 array( 595 'from' => 'user1', 596 'to' => 'user2', 597 'state' => 'unread', 598 'subject' => 'S4', 599 ), 600 ), 601 'expectations' => array( 602 // The most recent message between user1 and user2 was S2, even though later IDs have not been read. 603 'user1' => array( 604 array( 605 'messageposition' => 0, 606 'with' => 'user2', 607 'subject' => 'S2', 608 ), 609 ), 610 'user2' => array( 611 array( 612 'messageposition' => 0, 613 'with' => 'user1', 614 'subject' => 'S2', 615 ), 616 ), 617 ), 618 ), 619 ); 620 } 621 622 /** 623 * Test message_get_recent_conversations with a mixture of messages. 624 * 625 * @dataProvider message_get_recent_conversations_provider 626 * @param array $usersdata The list of users to create for this test. 627 * @param array $messagesdata The list of messages to create. 628 * @param array $expectations The list of expected outcomes. 629 */ 630 public function test_message_get_recent_conversations($usersdata, $contacts, $messagesdata, $expectations) { 631 global $DB; 632 633 // Create all of the users. 634 $users = array(); 635 foreach ($usersdata as $username) { 636 $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username)); 637 } 638 639 foreach ($contacts as $username => $contact) { 640 foreach ($contact as $contactname => $blocked) { 641 $record = new stdClass(); 642 $record->userid = $users[$username]->id; 643 $record->contactid = $users[$contactname]->id; 644 $record->blocked = $blocked; 645 $record->id = $DB->insert_record('message_contacts', $record); 646 } 647 } 648 649 $defaulttimecreated = time(); 650 foreach ($messagesdata as $messagedata) { 651 $from = $users[$messagedata['from']]; 652 $to = $users[$messagedata['to']]; 653 $subject = $messagedata['subject']; 654 655 if (isset($messagedata['state']) && $messagedata['state'] == 'unread') { 656 $table = 'message'; 657 $messageid = $this->send_fake_message($from, $to, $subject, FORMAT_PLAIN); 658 } else { 659 // If there is no state, or the state is not 'unread', assume the message is read. 660 $table = 'message_read'; 661 $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN); 662 } 663 664 $updatemessage = new stdClass(); 665 $updatemessage->id = $messageid; 666 if (isset($messagedata['timecreated'])) { 667 $updatemessage->timecreated = $messagedata['timecreated']; 668 } else if (isset($messagedata['timemodifier'])) { 669 $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier']; 670 } else { 671 $updatemessage->timecreated = $defaulttimecreated; 672 } 673 $DB->update_record($table, $updatemessage); 674 } 675 676 foreach ($expectations as $username => $data) { 677 // Get the recent conversations for the specified user. 678 $user = $users[$username]; 679 $conversations = message_get_recent_conversations($user); 680 foreach ($data as $expectation) { 681 $otheruser = $users[$expectation['with']]; 682 $conversation = $conversations[$expectation['messageposition']]; 683 $this->assertEquals($otheruser->id, $conversation->id); 684 $this->assertEquals($expectation['subject'], $conversation->smallmessage); 685 } 686 } 687 } 688 689 /** 690 * Test message_get_recent_notifications. 691 */ 692 public function test_message_get_recent_notifications() { 693 global $DB, $USER; 694 695 // Set this user as the admin. 696 $this->setAdminUser(); 697 698 // Create a user to send messages from. 699 $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1')); 700 701 // Add two messages - will mark them as notifications later. 702 $m1 = message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN); 703 $m2 = message_post_message($user1, $USER, 'Message 2', FORMAT_PLAIN); 704 705 // Mark the second message as a notification. 706 $updatemessage = new stdClass(); 707 $updatemessage->id = $m2; 708 $updatemessage->notification = 1; 709 $DB->update_record('message_read', $updatemessage); 710 711 // Mark the first message as a notification and change the timecreated to 0. 712 $updatemessage->id = $m1; 713 $updatemessage->notification = 1; 714 $updatemessage->timecreated = 0; 715 $DB->update_record('message_read', $updatemessage); 716 717 $notifications = message_get_recent_notifications($USER); 718 719 // Get the messages. 720 $firstmessage = array_shift($notifications); 721 $secondmessage = array_shift($notifications); 722 723 // Confirm that we have received the notifications with the maximum timecreated, rather than the max id. 724 $this->assertEquals('Message 2', $firstmessage->smallmessage); 725 $this->assertEquals('Message 1', $secondmessage->smallmessage); 726 } 727 728 /** 729 * Test that message_can_post_message returns false if the sender does not have the 730 * moode/site:sendmessage capability. 731 */ 732 public function test_message_can_post_message_returns_false_without_capability() { 733 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 734 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 735 $context = context_system::instance(); 736 $roleid = $this->getDataGenerator()->create_role(); 737 $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id); 738 739 assign_capability('moodle/site:sendmessage', CAP_PROHIBIT, $roleid, $context); 740 741 $this->assertFalse(message_can_post_message($recipient, $sender)); 742 } 743 744 /** 745 * Test that message_can_post_message returns false if the receiver only accepts 746 * messages from contacts and the sender isn't a contact. 747 */ 748 public function test_message_can_post_message_returns_false_non_contact_blocked() { 749 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 750 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 751 752 set_user_preference('message_blocknoncontacts', true, $recipient); 753 754 $this->assertFalse(message_can_post_message($recipient, $sender)); 755 } 756 757 /** 758 * Test that message_can_post_message returns false if the receiver has blocked the 759 * sender from messaging them. 760 */ 761 public function test_message_can_post_message_returns_false_if_blocked() { 762 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 763 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 764 765 $this->setUser($recipient); 766 message_block_contact($sender->id); 767 768 $this->assertFalse(message_can_post_message($recipient, $sender)); 769 } 770 771 /** 772 * Test that message_can_post_message returns false if the receiver has blocked the 773 * sender from messaging them. 774 */ 775 public function test_message_can_post_message_returns_true() { 776 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 777 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 778 779 $this->assertTrue(message_can_post_message($recipient, $sender)); 780 } 781 782 /** 783 * Test that message_is_user_non_contact_blocked returns false if the recipient allows 784 * messages from non-contacts. 785 */ 786 public function test_message_is_user_non_contact_blocked_false_without_preference() { 787 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 788 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 789 790 set_user_preference('message_blocknoncontacts', false, $recipient); 791 792 $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender)); 793 } 794 795 /** 796 * Test that message_is_user_non_contact_blocked returns true if the recipient doesn't 797 * allow messages from non-contacts and the sender isn't a contact. 798 */ 799 public function test_message_is_user_non_contact_blocked_true_with_preference() { 800 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 801 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 802 803 set_user_preference('message_blocknoncontacts', true, $recipient); 804 805 $this->assertTrue(message_is_user_non_contact_blocked($recipient, $sender)); 806 } 807 808 /** 809 * Test that message_is_user_non_contact_blocked returns false if the recipient doesn't 810 * allow messages from non-contacts but the sender is a contact. 811 */ 812 public function test_message_is_user_non_contact_blocked_false_with_if_contact() { 813 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 814 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 815 816 $this->setUser($recipient); 817 set_user_preference('message_blocknoncontacts', true, $recipient); 818 message_add_contact($sender->id); 819 820 $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender)); 821 } 822 823 /** 824 * Test that message_is_user_blocked returns false if the sender is not a contact of 825 * the recipient. 826 */ 827 public function test_message_is_user_blocked_false_no_contact() { 828 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 829 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 830 831 $this->assertFalse(message_is_user_blocked($recipient, $sender)); 832 } 833 834 /** 835 * Test that message_is_user_blocked returns false if the sender is a contact that is 836 * blocked by the recipient but has the moodle/site:readallmessages capability. 837 */ 838 public function test_message_is_user_blocked_false_if_readallmessages() { 839 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 840 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 841 842 $this->setUser($recipient); 843 message_block_contact($sender->id); 844 845 $context = context_system::instance(); 846 $roleid = $this->getDataGenerator()->create_role(); 847 $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id); 848 849 assign_capability('moodle/site:readallmessages', CAP_ALLOW, $roleid, $context); 850 851 $this->assertFalse(message_is_user_blocked($recipient, $sender)); 852 } 853 854 /** 855 * Test that message_is_user_blocked returns true if the sender is a contact that is 856 * blocked by the recipient and does not have the moodle/site:readallmessages capability. 857 */ 858 public function test_message_is_user_blocked_true_if_blocked() { 859 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); 860 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); 861 862 $this->setUser($recipient); 863 message_block_contact($sender->id); 864 865 $context = context_system::instance(); 866 $roleid = $this->getDataGenerator()->create_role(); 867 $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id); 868 869 assign_capability('moodle/site:readallmessages', CAP_PROHIBIT, $roleid, $context); 870 871 $this->assertTrue(message_is_user_blocked($recipient, $sender)); 872 } 873 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |