Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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  namespace core_message;
  18  
  19  use core_message\tests\helper as testhelper;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot . '/message/lib.php');
  25  
  26  /**
  27   * Test api's in message lib.
  28   *
  29   * @package core_message
  30   * @category test
  31   * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class messagelib_test extends \advanced_testcase {
  35  
  36      /** @var phpunit_message_sink keep track of messages. */
  37      protected $messagesink = null;
  38  
  39      /**
  40       * Test set up.
  41       *
  42       * This is executed before running any test in this file.
  43       */
  44      public function setUp(): void {
  45          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
  46          $this->messagesink = $this->redirectMessages();
  47          $this->resetAfterTest();
  48      }
  49  
  50      /**
  51       * Send a fake message.
  52       *
  53       * {@link message_send()} does not support transaction, this function will simulate a message
  54       * sent from a user to another. We should stop using it once {@link message_send()} will support
  55       * transactions. This is not clean at all, this is just used to add rows to the table.
  56       *
  57       * @param \stdClass $userfrom user object of the one sending the message.
  58       * @param \stdClass $userto user object of the one receiving the message.
  59       * @param string $message message to send.
  60       * @param int $notification if the message is a notification.
  61       * @param int $time the time the message was sent
  62       * @return int the id of the message
  63       */
  64      protected function send_fake_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) {
  65          global $DB;
  66  
  67          if (empty($time)) {
  68              $time = time();
  69          }
  70  
  71          if ($notification) {
  72              $record = new \stdClass();
  73              $record->useridfrom = $userfrom->id;
  74              $record->useridto = $userto->id;
  75              $record->subject = 'No subject';
  76              $record->fullmessage = $message;
  77              $record->smallmessage = $message;
  78              $record->timecreated = $time;
  79  
  80              return $DB->insert_record('notifications', $record);
  81          }
  82  
  83          if ($userfrom->id == $userto->id) {
  84              // It's a self conversation.
  85              $conversation = \core_message\api::get_self_conversation($userfrom->id);
  86              if (empty($conversation)) {
  87                  $conversation = \core_message\api::create_conversation(
  88                      \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
  89                      [$userfrom->id]
  90                  );
  91              }
  92              $conversationid = $conversation->id;
  93          } else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
  94              // It's an individual conversation between two different users.
  95              $conversation = \core_message\api::create_conversation(
  96                  \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
  97                  [
  98                      $userfrom->id,
  99                      $userto->id
 100                  ]
 101              );
 102              $conversationid = $conversation->id;
 103          }
 104  
 105          // Ok, send the message.
 106          $record = new \stdClass();
 107          $record->useridfrom = $userfrom->id;
 108          $record->conversationid = $conversationid;
 109          $record->subject = 'No subject';
 110          $record->fullmessage = $message;
 111          $record->smallmessage = $message;
 112          $record->timecreated = $time;
 113  
 114          return $DB->insert_record('messages', $record);
 115      }
 116  
 117      /**
 118       * Test message_get_blocked_users throws an exception because has been removed.
 119       */
 120      public function test_message_get_blocked_users() {
 121          $this->expectException('coding_exception');
 122          $this->expectExceptionMessage(
 123              'message_get_blocked_users() has been removed, please use \core_message\api::get_blocked_users() instead.'
 124          );
 125          message_get_blocked_users();
 126      }
 127  
 128      /**
 129       * Test message_get_contacts throws an exception because has been removed.
 130       */
 131      public function test_message_get_contacts() {
 132          $this->expectException('coding_exception');
 133          $this->expectExceptionMessage('message_get_contacts() has been removed.');
 134          message_get_contacts();
 135      }
 136  
 137      /**
 138       * Test message_search_users.
 139       */
 140      public function test_message_search_users() {
 141          global $USER;
 142  
 143          // Set this user as the admin.
 144          $this->setAdminUser();
 145  
 146          // Create a user to add to the admin's contact list.
 147          $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
 148          $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
 149  
 150          // Add users to the admin's contact list.
 151          \core_message\api::add_contact($USER->id, $user1->id);
 152          \core_message\api::add_contact($USER->id, $user2->id);
 153  
 154          $this->assertCount(1, message_search_users(0, 'Test1'));
 155          $this->assertCount(2, message_search_users(0, 'Test'));
 156          $this->assertCount(1, message_search_users(0, 'user1'));
 157          $this->assertCount(2, message_search_users(0, 'user'));
 158      }
 159  
 160      /**
 161       * Test message_get_messages.
 162       */
 163      public function test_message_get_messages() {
 164          global $DB;
 165  
 166          $this->resetAfterTest(true);
 167  
 168          // Set this user as the admin.
 169          $this->setAdminUser();
 170  
 171          $user1 = self::getDataGenerator()->create_user();
 172          $user2 = self::getDataGenerator()->create_user();
 173          $user3 = self::getDataGenerator()->create_user();
 174  
 175          \core_message\api::add_contact($user1->id, $user2->id);
 176          \core_message\api::add_contact($user1->id, $user3->id);
 177  
 178          // Create some individual conversations.
 179          $ic1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
 180              [$user1->id, $user2->id]);
 181          $ic2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
 182              [$user1->id, $user3->id]);
 183  
 184          // Send some messages to individual conversations.
 185          $im1 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 1');
 186          $im2 = testhelper::send_fake_message_to_conversation($user2, $ic1->id, 'Message 2');
 187          $im3 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 3');
 188          $im4 = testhelper::send_fake_message_to_conversation($user1, $ic2->id, 'Message 4');
 189  
 190          // Mark a message as read by user2.
 191          $message = $DB->get_record('messages', ['id' => $im1]);
 192          \core_message\api::mark_message_as_read($user2->id, $message);
 193  
 194          // Retrieve unread messages sent from user1 to user2.
 195          $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_UNREAD);
 196          $this->assertCount(1, $lastmessages);
 197          $this->assertArrayHasKey($im3, $lastmessages);
 198  
 199          // Get only read messages.
 200          $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ);
 201          $this->assertCount(1, $lastmessages);
 202          $this->assertArrayHasKey($im1, $lastmessages);
 203  
 204          // Get both read and unread.
 205          $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ_AND_UNREAD);
 206          $this->assertCount(2, $lastmessages);
 207          $this->assertArrayHasKey($im1, $lastmessages);
 208          $this->assertArrayHasKey($im3, $lastmessages);
 209  
 210          // Repeat retrieve read/unread messages but using a bool to test backwards compatibility.
 211          $lastmessages = message_get_messages($user2->id, $user1->id, 0, false);
 212          $this->assertCount(1, $lastmessages);
 213          $this->assertArrayHasKey($im3, $lastmessages);
 214  
 215          $lastmessages = message_get_messages($user2->id, $user1->id, 0, true);
 216          $this->assertCount(1, $lastmessages);
 217          $this->assertArrayHasKey($im1, $lastmessages);
 218  
 219          // Create some group conversations.
 220          $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
 221              [$user1->id, $user2->id, $user3->id], 'Group chat');
 222  
 223          // Send some messages to group conversations.
 224          $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1');
 225  
 226          // Retrieve all messages sent from user1 to user2 (the result should be the same as before, because only individual
 227          // conversations should be considered by the message_get_messages function).
 228          $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ_AND_UNREAD);
 229          $this->assertCount(2, $lastmessages);
 230          $this->assertArrayHasKey($im1, $lastmessages);
 231          $this->assertArrayHasKey($im3, $lastmessages);
 232      }
 233  
 234      /**
 235       * Test message_get_messages with only group conversations between users.
 236       */
 237      public function test_message_get_messages_only_group_conversations() {
 238          $this->resetAfterTest(true);
 239  
 240          // Set this user as the admin.
 241          $this->setAdminUser();
 242  
 243          $user1 = self::getDataGenerator()->create_user();
 244          $user2 = self::getDataGenerator()->create_user();
 245          $user3 = self::getDataGenerator()->create_user();
 246  
 247          // Create some group conversations.
 248          $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
 249              [$user1->id, $user2->id, $user3->id], 'Group chat');
 250  
 251          // Send some messages to group conversations.
 252          $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1');
 253          $gm2 = testhelper::send_fake_message_to_conversation($user2, $gc1->id, 'Group message 2');
 254  
 255          // Retrieve all messages sent from user1 to user2. There shouldn't be messages, because only individual
 256          // conversations should be considered by the message_get_messages function.
 257          $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ_AND_UNREAD);
 258          $this->assertCount(0, $lastmessages);
 259      }
 260  
 261  }