Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  use \core_message\tests\helper as testhelper;
  32  
  33  /**
  34   * Test api's in message lib.
  35   *
  36   * @package core_message
  37   * @category test
  38   * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
  39   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class core_message_messagelib_testcase extends advanced_testcase {
  42  
  43      /** @var phpunit_message_sink keep track of messages. */
  44      protected $messagesink = null;
  45  
  46      /**
  47       * Test set up.
  48       *
  49       * This is executed before running any test in this file.
  50       */
  51      public function setUp(): void {
  52          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
  53          $this->messagesink = $this->redirectMessages();
  54          $this->resetAfterTest();
  55      }
  56  
  57      /**
  58       * Send a fake message.
  59       *
  60       * {@link message_send()} does not support transaction, this function will simulate a message
  61       * sent from a user to another. We should stop using it once {@link message_send()} will support
  62       * transactions. This is not clean at all, this is just used to add rows to the table.
  63       *
  64       * @param stdClass $userfrom user object of the one sending the message.
  65       * @param stdClass $userto user object of the one receiving the message.
  66       * @param string $message message to send.
  67       * @param int $notification if the message is a notification.
  68       * @param int $time the time the message was sent
  69       * @return int the id of the message
  70       */
  71      protected function send_fake_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) {
  72          global $DB;
  73  
  74          if (empty($time)) {
  75              $time = time();
  76          }
  77  
  78          if ($notification) {
  79              $record = new stdClass();
  80              $record->useridfrom = $userfrom->id;
  81              $record->useridto = $userto->id;
  82              $record->subject = 'No subject';
  83              $record->fullmessage = $message;
  84              $record->smallmessage = $message;
  85              $record->timecreated = $time;
  86  
  87              return $DB->insert_record('notifications', $record);
  88          }
  89  
  90          if ($userfrom->id == $userto->id) {
  91              // It's a self conversation.
  92              $conversation = \core_message\api::get_self_conversation($userfrom->id);
  93              if (empty($conversation)) {
  94                  $conversation = \core_message\api::create_conversation(
  95                      \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
  96                      [$userfrom->id]
  97                  );
  98              }
  99              $conversationid = $conversation->id;
 100          } else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
 101              // It's an individual conversation between two different users.
 102              $conversation = \core_message\api::create_conversation(
 103                  \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
 104                  [
 105                      $userfrom->id,
 106                      $userto->id
 107                  ]
 108              );
 109              $conversationid = $conversation->id;
 110          }
 111  
 112          // Ok, send the message.
 113          $record = new stdClass();
 114          $record->useridfrom = $userfrom->id;
 115          $record->conversationid = $conversationid;
 116          $record->subject = 'No subject';
 117          $record->fullmessage = $message;
 118          $record->smallmessage = $message;
 119          $record->timecreated = $time;
 120  
 121          return $DB->insert_record('messages', $record);
 122      }
 123  
 124      /**
 125       * Test message_get_blocked_users throws an exception because has been removed.
 126       */
 127      public function test_message_get_blocked_users() {
 128          $this->expectException('coding_exception');
 129          $this->expectExceptionMessage(
 130              'message_get_blocked_users() has been removed, please use \core_message\api::get_blocked_users() instead.'
 131          );
 132          message_get_blocked_users();
 133      }
 134  
 135      /**
 136       * Test message_get_contacts throws an exception because has been removed.
 137       */
 138      public function test_message_get_contacts() {
 139          $this->expectException('coding_exception');
 140          $this->expectExceptionMessage('message_get_contacts() has been removed.');
 141          message_get_contacts();
 142      }
 143  
 144      /**
 145       * Test message_count_unread_messages.
 146       * TODO: MDL-69643
 147       */
 148      public function test_message_count_unread_messages() {
 149          // Create users to send and receive message.
 150          $userfrom1 = $this->getDataGenerator()->create_user();
 151          $userfrom2 = $this->getDataGenerator()->create_user();
 152          $userto = $this->getDataGenerator()->create_user();
 153  
 154          $this->assertEquals(0, message_count_unread_messages($userto));
 155          $this->assertDebuggingCalled();
 156  
 157          // Send fake messages.
 158          $this->send_fake_message($userfrom1, $userto);
 159          $this->send_fake_message($userfrom2, $userto);
 160  
 161          $this->assertEquals(2, message_count_unread_messages($userto));
 162          $this->assertDebuggingCalled();
 163  
 164          $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
 165          $this->assertDebuggingCalled();
 166      }
 167  
 168      /**
 169       * Test message_count_unread_messages with read messages.
 170       */
 171      public function test_message_count_unread_messages_with_read_messages() {
 172          global $DB;
 173  
 174          // Create users to send and receive messages.
 175          $userfrom1 = $this->getDataGenerator()->create_user();
 176          $userfrom2 = $this->getDataGenerator()->create_user();
 177          $userto = $this->getDataGenerator()->create_user();
 178  
 179          $this->assertEquals(0, message_count_unread_messages($userto));
 180  
 181          // Send fake messages.
 182          $messageid = $this->send_fake_message($userfrom1, $userto);
 183          $this->send_fake_message($userfrom2, $userto);
 184  
 185          // Mark message as read.
 186          $message = $DB->get_record('messages', ['id' => $messageid]);
 187          \core_message\api::mark_message_as_read($userto->id, $message);
 188  
 189          // Should only count the messages that weren't read by the current user.
 190          $this->assertEquals(1, message_count_unread_messages($userto));
 191          $this->assertDebuggingCalledCount(2);
 192  
 193          $this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
 194          $this->assertDebuggingCalled();
 195      }
 196  
 197      /**
 198       * Test message_count_unread_messages with deleted messages.
 199       */
 200      public function test_message_count_unread_messages_with_deleted_messages() {
 201          global $DB;
 202  
 203          // Create users to send and receive messages.
 204          $userfrom1 = $this->getDataGenerator()->create_user();
 205          $userfrom2 = $this->getDataGenerator()->create_user();
 206          $userto = $this->getDataGenerator()->create_user();
 207  
 208          $this->assertEquals(0, message_count_unread_messages($userto));
 209          $this->assertDebuggingCalled();
 210  
 211          // Send fake messages.
 212          $messageid = $this->send_fake_message($userfrom1, $userto);
 213          $this->send_fake_message($userfrom2, $userto);
 214  
 215          // Delete a message.
 216          \core_message\api::delete_message($userto->id, $messageid);
 217  
 218          // Should only count the messages that weren't deleted by the current user.
 219          $this->assertEquals(1, message_count_unread_messages($userto));
 220          $this->assertDebuggingCalled();
 221          $this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
 222          $this->assertDebuggingCalled();
 223      }
 224  
 225      /**
 226       * Test message_count_unread_messages with sent messages.
 227       */
 228      public function test_message_count_unread_messages_with_sent_messages() {
 229          $userfrom = $this->getDataGenerator()->create_user();
 230          $userto = $this->getDataGenerator()->create_user();
 231  
 232          $this->send_fake_message($userfrom, $userto);
 233  
 234          // Ensure an exception is thrown.
 235          $this->assertEquals(0, message_count_unread_messages($userfrom));
 236          $this->assertDebuggingCalled();
 237      }
 238  
 239      /**
 240       * Test message_search_users.
 241       */
 242      public function test_message_search_users() {
 243          global $USER;
 244  
 245          // Set this user as the admin.
 246          $this->setAdminUser();
 247  
 248          // Create a user to add to the admin's contact list.
 249          $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
 250          $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
 251  
 252          // Add users to the admin's contact list.
 253          \core_message\api::add_contact($USER->id, $user1->id);
 254          \core_message\api::add_contact($USER->id, $user2->id);
 255  
 256          $this->assertCount(1, message_search_users(0, 'Test1'));
 257          $this->assertCount(2, message_search_users(0, 'Test'));
 258          $this->assertCount(1, message_search_users(0, 'user1'));
 259          $this->assertCount(2, message_search_users(0, 'user'));
 260      }
 261  
 262      /**
 263       * Test message_get_messages.
 264       */
 265      public function test_message_get_messages() {
 266          $this->resetAfterTest(true);
 267  
 268          // Set this user as the admin.
 269          $this->setAdminUser();
 270  
 271          $user1 = self::getDataGenerator()->create_user();
 272          $user2 = self::getDataGenerator()->create_user();
 273          $user3 = self::getDataGenerator()->create_user();
 274  
 275          \core_message\api::add_contact($user1->id, $user2->id);
 276          \core_message\api::add_contact($user1->id, $user3->id);
 277  
 278          // Create some individual conversations.
 279          $ic1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
 280              [$user1->id, $user2->id]);
 281          $ic2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
 282              [$user1->id, $user3->id]);
 283  
 284          // Send some messages to individual conversations.
 285          $im1 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 1');
 286          $im2 = testhelper::send_fake_message_to_conversation($user2, $ic1->id, 'Message 2');
 287          $im3 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 3');
 288          $im4 = testhelper::send_fake_message_to_conversation($user1, $ic2->id, 'Message 4');
 289  
 290          // Retrieve all messages sent from user1 to user2.
 291          $lastmessages = message_get_messages($user2->id, $user1->id, 0, false);
 292          $this->assertCount(2, $lastmessages);
 293          $this->assertArrayHasKey($im1, $lastmessages);
 294          $this->assertArrayHasKey($im3, $lastmessages);
 295  
 296          // Create some group conversations.
 297          $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
 298              [$user1->id, $user2->id, $user3->id], 'Group chat');
 299  
 300          // Send some messages to group conversations.
 301          $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1');
 302  
 303          // Retrieve all messages sent from user1 to user2 (the result should be the same as before, because only individual
 304          // conversations should be considered by the message_get_messages function).
 305          $lastmessages = message_get_messages($user2->id, $user1->id, 0, false);
 306          $this->assertCount(2, $lastmessages);
 307          $this->assertArrayHasKey($im1, $lastmessages);
 308          $this->assertArrayHasKey($im3, $lastmessages);
 309      }
 310  
 311      /**
 312       * Test message_get_messages with only group conversations between users.
 313       */
 314      public function test_message_get_messages_only_group_conversations() {
 315          $this->resetAfterTest(true);
 316  
 317          // Set this user as the admin.
 318          $this->setAdminUser();
 319  
 320          $user1 = self::getDataGenerator()->create_user();
 321          $user2 = self::getDataGenerator()->create_user();
 322          $user3 = self::getDataGenerator()->create_user();
 323  
 324          // Create some group conversations.
 325          $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
 326              [$user1->id, $user2->id, $user3->id], 'Group chat');
 327  
 328          // Send some messages to group conversations.
 329          $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1');
 330          $gm2 = testhelper::send_fake_message_to_conversation($user2, $gc1->id, 'Group message 2');
 331  
 332          // Retrieve all messages sent from user1 to user2. There shouldn't be messages, because only individual
 333          // conversations should be considered by the message_get_messages function.
 334          $lastmessages = message_get_messages($user2->id, $user1->id, 0, false);
 335          $this->assertCount(0, $lastmessages);
 336      }
 337  
 338  }