See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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() { 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 */ 147 public function test_message_count_unread_messages() { 148 // Create users to send and receive message. 149 $userfrom1 = $this->getDataGenerator()->create_user(); 150 $userfrom2 = $this->getDataGenerator()->create_user(); 151 $userto = $this->getDataGenerator()->create_user(); 152 153 $this->assertEquals(0, message_count_unread_messages($userto)); 154 155 // Send fake messages. 156 $this->send_fake_message($userfrom1, $userto); 157 $this->send_fake_message($userfrom2, $userto); 158 159 $this->assertEquals(2, message_count_unread_messages($userto)); 160 $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1)); 161 } 162 163 /** 164 * Test message_count_unread_messages with read messages. 165 */ 166 public function test_message_count_unread_messages_with_read_messages() { 167 global $DB; 168 169 // Create users to send and receive messages. 170 $userfrom1 = $this->getDataGenerator()->create_user(); 171 $userfrom2 = $this->getDataGenerator()->create_user(); 172 $userto = $this->getDataGenerator()->create_user(); 173 174 $this->assertEquals(0, message_count_unread_messages($userto)); 175 176 // Send fake messages. 177 $messageid = $this->send_fake_message($userfrom1, $userto); 178 $this->send_fake_message($userfrom2, $userto); 179 180 // Mark message as read. 181 $message = $DB->get_record('messages', ['id' => $messageid]); 182 \core_message\api::mark_message_as_read($userto->id, $message); 183 184 // Should only count the messages that weren't read by the current user. 185 $this->assertEquals(1, message_count_unread_messages($userto)); 186 $this->assertEquals(0, message_count_unread_messages($userto, $userfrom1)); 187 } 188 189 /** 190 * Test message_count_unread_messages with deleted messages. 191 */ 192 public function test_message_count_unread_messages_with_deleted_messages() { 193 global $DB; 194 195 // Create users to send and receive messages. 196 $userfrom1 = $this->getDataGenerator()->create_user(); 197 $userfrom2 = $this->getDataGenerator()->create_user(); 198 $userto = $this->getDataGenerator()->create_user(); 199 200 $this->assertEquals(0, message_count_unread_messages($userto)); 201 202 // Send fake messages. 203 $messageid = $this->send_fake_message($userfrom1, $userto); 204 $this->send_fake_message($userfrom2, $userto); 205 206 // Delete a message. 207 \core_message\api::delete_message($userto->id, $messageid); 208 209 // Should only count the messages that weren't deleted by the current user. 210 $this->assertEquals(1, message_count_unread_messages($userto)); 211 $this->assertEquals(0, message_count_unread_messages($userto, $userfrom1)); 212 } 213 214 /** 215 * Test message_count_unread_messages with sent messages. 216 */ 217 public function test_message_count_unread_messages_with_sent_messages() { 218 $userfrom = $this->getDataGenerator()->create_user(); 219 $userto = $this->getDataGenerator()->create_user(); 220 221 $this->send_fake_message($userfrom, $userto); 222 223 $this->assertEquals(0, message_count_unread_messages($userfrom)); 224 } 225 226 /** 227 * Test message_add_contact. 228 */ 229 public function test_message_add_contact() { 230 global $DB, $USER; 231 232 // Set this user as the admin. 233 $this->setAdminUser(); 234 235 // Create a user to add to the admin's contact list. 236 $user1 = $this->getDataGenerator()->create_user(); 237 $user2 = $this->getDataGenerator()->create_user(); 238 239 message_add_contact($user1->id); 240 $this->assertDebuggingCalled(); 241 $this->assertEquals(1, $DB->count_records('message_contact_requests')); 242 243 message_add_contact($user2->id, 1); 244 $this->assertDebuggingCalled(); 245 $this->assertEquals(1, $DB->count_records('message_users_blocked')); 246 247 message_add_contact($user2->id, 0); 248 $this->assertDebuggingCalled(); 249 $this->assertEquals(0, $DB->count_records('message_users_blocked')); 250 } 251 252 /** 253 * Test message_remove_contact. 254 */ 255 public function test_message_remove_contact() { 256 global $USER; 257 258 // Set this user as the admin. 259 $this->setAdminUser(); 260 261 // Create a user to add to the admin's contact list. 262 $user = $this->getDataGenerator()->create_user(); 263 264 // Add the user to the admin's contact list. 265 \core_message\api::add_contact($USER->id, $user->id); 266 267 // Remove user from admin's contact list. 268 message_remove_contact($user->id); 269 $this->assertDebuggingCalled(); 270 $this->assertEquals(false, message_get_contact($user->id)); 271 $this->assertDebuggingCalled(); 272 } 273 274 /** 275 * Test message_block_contact. 276 */ 277 public function test_message_block_contact() { 278 global $USER; 279 280 // Set this user as the admin. 281 $this->setAdminUser(); 282 283 // Create a user to add to the admin's contact list. 284 $user1 = $this->getDataGenerator()->create_user(); 285 $user2 = $this->getDataGenerator()->create_user(); 286 287 // Add users to the admin's contact list. 288 \core_message\api::add_contact($USER->id, $user1->id); 289 \core_message\api::add_contact($USER->id, $user2->id); 290 291 $this->assertEquals(0, \core_message\api::count_blocked_users()); 292 293 // Block 1 user. 294 message_block_contact($user2->id); 295 $this->assertDebuggingCalled(); 296 $this->assertEquals(1, \core_message\api::count_blocked_users()); 297 298 } 299 300 /** 301 * Test message_unblock_contact. 302 */ 303 public function test_message_unblock_contact() { 304 global $USER; 305 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 312 // Add users to the admin's blocked list. 313 \core_message\api::block_user($USER->id, $user1->id); 314 $this->assertEquals(1, \core_message\api::count_blocked_users()); 315 316 // Unblock user. 317 message_unblock_contact($user1->id); 318 $this->assertDebuggingCalled(); 319 $this->assertEquals(0, \core_message\api::count_blocked_users()); 320 } 321 322 /** 323 * Test message_search_users. 324 */ 325 public function test_message_search_users() { 326 global $USER; 327 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 \core_message\api::add_contact($USER->id, $user1->id); 337 \core_message\api::add_contact($USER->id, $user2->id); 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_get_messages. 347 */ 348 public function test_message_get_messages() { 349 $this->resetAfterTest(true); 350 351 // Set this user as the admin. 352 $this->setAdminUser(); 353 354 $user1 = self::getDataGenerator()->create_user(); 355 $user2 = self::getDataGenerator()->create_user(); 356 $user3 = self::getDataGenerator()->create_user(); 357 358 \core_message\api::add_contact($user1->id, $user2->id); 359 \core_message\api::add_contact($user1->id, $user3->id); 360 361 // Create some individual conversations. 362 $ic1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, 363 [$user1->id, $user2->id]); 364 $ic2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, 365 [$user1->id, $user3->id]); 366 367 // Send some messages to individual conversations. 368 $im1 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 1'); 369 $im2 = testhelper::send_fake_message_to_conversation($user2, $ic1->id, 'Message 2'); 370 $im3 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 3'); 371 $im4 = testhelper::send_fake_message_to_conversation($user1, $ic2->id, 'Message 4'); 372 373 // Retrieve all messages sent from user1 to user2. 374 $lastmessages = message_get_messages($user2->id, $user1->id, 0, false); 375 $this->assertCount(2, $lastmessages); 376 $this->assertArrayHasKey($im1, $lastmessages); 377 $this->assertArrayHasKey($im3, $lastmessages); 378 379 // Create some group conversations. 380 $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, 381 [$user1->id, $user2->id, $user3->id], 'Group chat'); 382 383 // Send some messages to group conversations. 384 $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1'); 385 386 // Retrieve all messages sent from user1 to user2 (the result should be the same as before, because only individual 387 // conversations should be considered by the message_get_messages function). 388 $lastmessages = message_get_messages($user2->id, $user1->id, 0, false); 389 $this->assertCount(2, $lastmessages); 390 $this->assertArrayHasKey($im1, $lastmessages); 391 $this->assertArrayHasKey($im3, $lastmessages); 392 } 393 394 /** 395 * Test message_get_messages with only group conversations between users. 396 */ 397 public function test_message_get_messages_only_group_conversations() { 398 $this->resetAfterTest(true); 399 400 // Set this user as the admin. 401 $this->setAdminUser(); 402 403 $user1 = self::getDataGenerator()->create_user(); 404 $user2 = self::getDataGenerator()->create_user(); 405 $user3 = self::getDataGenerator()->create_user(); 406 407 // Create some group conversations. 408 $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, 409 [$user1->id, $user2->id, $user3->id], 'Group chat'); 410 411 // Send some messages to group conversations. 412 $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1'); 413 $gm2 = testhelper::send_fake_message_to_conversation($user2, $gc1->id, 'Group message 2'); 414 415 // Retrieve all messages sent from user1 to user2. There shouldn't be messages, because only individual 416 // conversations should be considered by the message_get_messages function. 417 $lastmessages = message_get_messages($user2->id, $user1->id, 0, false); 418 $this->assertCount(0, $lastmessages); 419 } 420 421 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body