[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/message/tests/ -> messagelib_test.php (source)

   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 <[email protected]>
  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 <[email protected]>
  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->timecreated = time();
  76  
  77          return $DB->insert_record('message', $record);
  78      }
  79  
  80      /**
  81       * Test message_get_blocked_users.
  82       */
  83      public function test_message_get_blocked_users() {
  84          // Set this user as the admin.
  85          $this->setAdminUser();
  86  
  87          // Create a user to add to the admin's contact list.
  88          $user1 = $this->getDataGenerator()->create_user();
  89          $user2 = $this->getDataGenerator()->create_user();
  90  
  91          // Add users to the admin's contact list.
  92          message_add_contact($user1->id);
  93          message_add_contact($user2->id, 1);
  94  
  95          $this->assertCount(1, message_get_blocked_users());
  96  
  97          // Block other user.
  98          message_block_contact($user1->id);
  99          $this->assertCount(2, message_get_blocked_users());
 100      }
 101  
 102      /**
 103       * Test message_get_contacts.
 104       */
 105      public function test_message_get_contacts() {
 106          global $USER, $CFG;
 107  
 108          // Set this user as the admin.
 109          $this->setAdminUser();
 110  
 111          $noreplyuser = core_user::get_noreply_user();
 112          $supportuser = core_user::get_support_user();
 113  
 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.
 118  
 119          // Add users to the admin's contact list.
 120          message_add_contact($user1->id);
 121          message_add_contact($user2->id);
 122  
 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);
 127  
 128          list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
 129          $this->assertCount(0, $onlinecontacts);
 130          $this->assertCount(2, $offlinecontacts);
 131          $this->assertCount(1, $strangers);
 132  
 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);
 140  
 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);
 147  
 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);
 156      }
 157  
 158      /**
 159       * Test message_count_messages.
 160       */
 161      public function test_message_count_messages() {
 162          global $DB;
 163  
 164          // Create users to send and receive message.
 165          $userfrom = $this->getDataGenerator()->create_user();
 166          $userto = $this->getDataGenerator()->create_user();
 167  
 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);
 171  
 172          // Return 0 when no message.
 173          $messages = array();
 174          $this->assertEquals(0, message_count_messages($messages, 'Test', 'Test'));
 175  
 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));
 180      }
 181  
 182      /**
 183       * Test message_count_unread_messages.
 184       */
 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();
 190  
 191          $this->assertEquals(0, message_count_unread_messages($userto));
 192  
 193          // Send fake messages.
 194          $this->send_fake_message($userfrom1, $userto);
 195          $this->send_fake_message($userfrom2, $userto);
 196  
 197          $this->assertEquals(2, message_count_unread_messages($userto));
 198          $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
 199      }
 200  
 201      /**
 202       * Test message_count_blocked_users.
 203       *
 204       */
 205      public function test_message_count_blocked_users() {
 206          // Set this user as the admin.
 207          $this->setAdminUser();
 208  
 209          // Create users to add to the admin's contact list.
 210          $user1 = $this->getDataGenerator()->create_user();
 211          $user2 = $this->getDataGenerator()->create_user();
 212  
 213          $this->assertEquals(0, message_count_blocked_users());
 214  
 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);
 218  
 219          $this->assertEquals(0, message_count_blocked_users($user2));
 220          $this->assertEquals(1, message_count_blocked_users());
 221      }
 222  
 223      /**
 224       * Test message_add_contact.
 225       */
 226      public function test_message_add_contact() {
 227          // Set this user as the admin.
 228          $this->setAdminUser();
 229  
 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();
 234  
 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);
 239  
 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());
 244      }
 245  
 246      /**
 247       * Test message_remove_contact.
 248       */
 249      public function test_message_remove_contact() {
 250          // Set this user as the admin.
 251          $this->setAdminUser();
 252  
 253          // Create a user to add to the admin's contact list.
 254          $user = $this->getDataGenerator()->create_user();
 255  
 256          // Add the user to the admin's contact list.
 257          message_add_contact($user->id);
 258          $this->assertNotEmpty(message_get_contact($user->id));
 259  
 260          // Remove user from admin's contact list.
 261          message_remove_contact($user->id);
 262          $this->assertEquals(false, message_get_contact($user->id));
 263      }
 264  
 265      /**
 266       * Test message_block_contact.
 267       */
 268      public function test_message_block_contact() {
 269          // Set this user as the admin.
 270          $this->setAdminUser();
 271  
 272          // Create a user to add to the admin's contact list.
 273          $user1 = $this->getDataGenerator()->create_user();
 274          $user2 = $this->getDataGenerator()->create_user();
 275  
 276          // Add users to the admin's contact list.
 277          message_add_contact($user1->id);
 278          message_add_contact($user2->id);
 279  
 280          $this->assertEquals(0, message_count_blocked_users());
 281  
 282          // Block 1 user.
 283          message_block_contact($user2->id);
 284          $this->assertEquals(1, message_count_blocked_users());
 285  
 286      }
 287  
 288      /**
 289       * Test message_unblock_contact.
 290       */
 291      public function test_message_unblock_contact() {
 292          // Set this user as the admin.
 293          $this->setAdminUser();
 294  
 295          // Create a user to add to the admin's contact list.
 296          $user1 = $this->getDataGenerator()->create_user();
 297          $user2 = $this->getDataGenerator()->create_user();
 298  
 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.
 302  
 303          $this->assertEquals(1, message_count_blocked_users());
 304  
 305          // Unblock user.
 306          message_unblock_contact($user2->id);
 307          $this->assertEquals(0, message_count_blocked_users());
 308      }
 309  
 310      /**
 311       * Test message_search_users.
 312       */
 313      public function test_message_search_users() {
 314          // Set this user as the admin.
 315          $this->setAdminUser();
 316  
 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'));
 320  
 321          // Add users to the admin's contact list.
 322          message_add_contact($user1->id);
 323          message_add_contact($user2->id); // Add blocked contact.
 324  
 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'));
 329      }
 330  
 331      /**
 332       * Test message_search.
 333       */
 334      public function test_message_search() {
 335          global $USER;
 336  
 337          // Set this user as the admin.
 338          $this->setAdminUser();
 339  
 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'));
 343  
 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);
 348  
 349          $this->assertCount(2, message_search(array('Message'), true, false));
 350          $this->assertCount(3, message_search(array('Message'), true, true));
 351  
 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));
 357  
 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));
 362      }
 363  
 364      /**
 365       * Test message_get_recent_conversations.
 366       */
 367      public function test_message_get_recent_conversations() {
 368          global $DB, $USER;
 369  
 370          // Set this user as the admin.
 371          $this->setAdminUser();
 372  
 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'));
 376  
 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);
 384  
 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.
 388  
 389          // Store the current time.
 390          $time = time();
 391  
 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);
 397  
 398          $updatemessage->id = $m2;
 399          $updatemessage->timecreated = $time;
 400          $DB->update_record('message', $updatemessage);
 401  
 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);
 406  
 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);
 411  
 412          $updatemessage->id = $m5;
 413          $updatemessage->timecreated = $time + 1;
 414          $DB->update_record('message', $updatemessage);
 415  
 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);
 420  
 421          // Get the recent conversations for the current user.
 422          $conversations = message_get_recent_conversations($USER);
 423  
 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);
 427      }
 428  
 429      /**
 430       * Test message_get_recent_notifications.
 431       */
 432      public function test_message_get_recent_notifications() {
 433          global $DB, $USER;
 434  
 435          // Set this user as the admin.
 436          $this->setAdminUser();
 437  
 438          // Create a user to send messages from.
 439          $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
 440  
 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);
 444  
 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);
 450  
 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);
 456  
 457          $notifications = message_get_recent_notifications($USER);
 458  
 459          // Get the messages.
 460          $firstmessage = array_shift($notifications);
 461          $secondmessage = array_shift($notifications);
 462  
 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);
 466      }
 467  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1