2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * External message functions unit tests
20 * @package core_message
22 * @copyright 2012 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
31 require_once($CFG->dirroot
. '/message/externallib.php');
33 use \core_message\tests\helper
as testhelper
;
35 class core_message_externallib_testcase
extends externallib_advanced_testcase
{
40 protected function setUp() {
43 require_once($CFG->dirroot
. '/message/lib.php');
47 * Send a fake message.
49 * {@link message_send()} does not support transaction, this function will simulate a message
50 * sent from a user to another. We should stop using it once {@link message_send()} will support
51 * transactions. This is not clean at all, this is just used to add rows to the table.
53 * @param stdClass $userfrom user object of the one sending the message.
54 * @param stdClass $userto user object of the one receiving the message.
55 * @param string $message message to send.
56 * @param int $notification is the message a notification.
57 * @param int $time the time the message was sent
59 protected function send_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) {
67 $record = new stdClass();
68 $record->useridfrom
= $userfrom->id
;
69 $record->useridto
= $userto->id
;
70 $record->subject
= 'No subject';
71 $record->fullmessage
= $message;
72 $record->smallmessage
= $message;
73 $record->timecreated
= $time;
75 return $DB->insert_record('notifications', $record);
78 if (!$conversationid = \core_message\api
::get_conversation_between_users([$userfrom->id
, $userto->id
])) {
79 $conversation = \core_message\api
::create_conversation(
80 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
86 $conversationid = $conversation->id
;
89 // Ok, send the message.
90 $record = new stdClass();
91 $record->useridfrom
= $userfrom->id
;
92 $record->conversationid
= $conversationid;
93 $record->subject
= 'No subject';
94 $record->fullmessage
= $message;
95 $record->smallmessage
= $message;
96 $record->timecreated
= $time;
98 return $DB->insert_record('messages', $record);
102 * Test send_instant_messages.
104 public function test_send_instant_messages() {
107 $this->resetAfterTest();
109 // Transactions used in tests, tell phpunit use alternative reset method.
110 $this->preventResetByRollback();
112 $user1 = self
::getDataGenerator()->create_user();
113 $user2 = self
::getDataGenerator()->create_user();
115 $this->setUser($user1);
117 // Create test message data.
119 $message1['touserid'] = $user2->id
;
120 $message1['text'] = 'the message.';
121 $message1['clientmsgid'] = 4;
122 $messages = array($message1);
124 $sentmessages = core_message_external
::send_instant_messages($messages);
125 $sentmessages = external_api
::clean_returnvalue(core_message_external
::send_instant_messages_returns(), $sentmessages);
127 get_string('usercantbemessaged', 'message', fullname(\core_user
::get_user($message1['touserid']))),
128 array_pop($sentmessages)['errormessage']
131 // Add the user1 as a contact.
132 \core_message\api
::add_contact($user1->id
, $user2->id
);
134 // Send message again. Now it should work properly.
135 $sentmessages = core_message_external
::send_instant_messages($messages);
136 // We need to execute the return values cleaning process to simulate the web service server.
137 $sentmessages = external_api
::clean_returnvalue(core_message_external
::send_instant_messages_returns(), $sentmessages);
139 $sentmessage = reset($sentmessages);
141 $sql = "SELECT m.*, mcm.userid as useridto
143 INNER JOIN {message_conversations} mc
144 ON m.conversationid = mc.id
145 INNER JOIN {message_conversation_members} mcm
146 ON mcm.conversationid = mc.id
147 WHERE mcm.userid != ?
149 $themessage = $DB->get_record_sql($sql, [$USER->id
, $sentmessage['msgid']]);
151 // Confirm that the message was inserted correctly.
152 $this->assertEquals($themessage->useridfrom
, $user1->id
);
153 $this->assertEquals($themessage->useridto
, $message1['touserid']);
154 $this->assertEquals($themessage->smallmessage
, $message1['text']);
155 $this->assertEquals($sentmessage['clientmsgid'], $message1['clientmsgid']);
159 * Test send_instant_messages to a user who has blocked you.
161 public function test_send_instant_messages_blocked_user() {
164 $this->resetAfterTest();
166 // Transactions used in tests, tell phpunit use alternative reset method.
167 $this->preventResetByRollback();
169 $user1 = self
::getDataGenerator()->create_user();
170 $user2 = self
::getDataGenerator()->create_user();
172 $this->setUser($user1);
174 \core_message\api
::block_user($user2->id
, $user1->id
);
176 // Create test message data.
178 $message1['touserid'] = $user2->id
;
179 $message1['text'] = 'the message.';
180 $message1['clientmsgid'] = 4;
181 $messages = array($message1);
183 $sentmessages = core_message_external
::send_instant_messages($messages);
184 $sentmessages = external_api
::clean_returnvalue(core_message_external
::send_instant_messages_returns(), $sentmessages);
186 $sentmessage = reset($sentmessages);
188 $this->assertEquals(get_string('usercantbemessaged', 'message', fullname($user2)), $sentmessage['errormessage']);
190 $this->assertEquals(0, $DB->count_records('messages'));
194 * Test send_instant_messages when sending a message to a non-contact who has blocked non-contacts.
196 public function test_send_instant_messages_block_non_contacts() {
199 $this->resetAfterTest(true);
201 // Transactions used in tests, tell phpunit use alternative reset method.
202 $this->preventResetByRollback();
204 $user1 = self
::getDataGenerator()->create_user();
205 $user2 = self
::getDataGenerator()->create_user();
207 $this->setUser($user1);
209 // Set the user preference so user 2 does not accept messages from non-contacts.
210 set_user_preference('message_blocknoncontacts', \core_message\api
::MESSAGE_PRIVACY_ONLYCONTACTS
, $user2);
212 // Create test message data.
214 $message1['touserid'] = $user2->id
;
215 $message1['text'] = 'the message.';
216 $message1['clientmsgid'] = 4;
217 $messages = array($message1);
219 $sentmessages = core_message_external
::send_instant_messages($messages);
220 $sentmessages = external_api
::clean_returnvalue(core_message_external
::send_instant_messages_returns(), $sentmessages);
222 $sentmessage = reset($sentmessages);
224 $this->assertEquals(get_string('usercantbemessaged', 'message', fullname($user2)), $sentmessage['errormessage']);
226 $this->assertEquals(0, $DB->count_records('messages'));
230 * Test send_instant_messages when sending a message to a contact who has blocked non-contacts.
232 public function test_send_instant_messages_block_non_contacts_but_am_contact() {
235 $this->resetAfterTest(true);
237 // Transactions used in tests, tell phpunit use alternative reset method.
238 $this->preventResetByRollback();
240 $user1 = self
::getDataGenerator()->create_user();
241 $user2 = self
::getDataGenerator()->create_user();
243 $this->setUser($user1);
245 // Set the user preference so user 2 does not accept messages from non-contacts.
246 set_user_preference('message_blocknoncontacts', \core_message\api
::MESSAGE_PRIVACY_ONLYCONTACTS
, $user2);
248 \core_message\api
::add_contact($user1->id
, $user2->id
);
250 // Create test message data.
252 $message1['touserid'] = $user2->id
;
253 $message1['text'] = 'the message.';
254 $message1['clientmsgid'] = 4;
255 $messages = array($message1);
257 $sentmessages = core_message_external
::send_instant_messages($messages);
258 $sentmessages = external_api
::clean_returnvalue(core_message_external
::send_instant_messages_returns(), $sentmessages);
260 $sentmessage = reset($sentmessages);
262 $sql = "SELECT m.*, mcm.userid as useridto
264 INNER JOIN {message_conversations} mc
265 ON m.conversationid = mc.id
266 INNER JOIN {message_conversation_members} mcm
267 ON mcm.conversationid = mc.id
268 WHERE mcm.userid != ?
270 $themessage = $DB->get_record_sql($sql, [$USER->id
, $sentmessage['msgid']]);
272 // Confirm that the message was inserted correctly.
273 $this->assertEquals($themessage->useridfrom
, $user1->id
);
274 $this->assertEquals($themessage->useridto
, $message1['touserid']);
275 $this->assertEquals($themessage->smallmessage
, $message1['text']);
276 $this->assertEquals($sentmessage['clientmsgid'], $message1['clientmsgid']);
280 * Test send_instant_messages with no capabilities
282 public function test_send_instant_messages_no_capability() {
285 $this->resetAfterTest(true);
287 // Transactions used in tests, tell phpunit use alternative reset method.
288 $this->preventResetByRollback();
290 $user1 = self
::getDataGenerator()->create_user();
291 $user2 = self
::getDataGenerator()->create_user();
293 $this->setUser($user1);
295 // Unset the required capabilities by the external function.
296 $contextid = context_system
::instance()->id
;
297 $userrole = $DB->get_record('role', array('shortname' => 'user'));
298 $this->unassignUserCapability('moodle/site:sendmessage', $contextid, $userrole->id
);
300 // Create test message data.
302 $message1['touserid'] = $user2->id
;
303 $message1['text'] = 'the message.';
304 $message1['clientmsgid'] = 4;
305 $messages = array($message1);
307 $this->expectException('required_capability_exception');
308 core_message_external
::send_instant_messages($messages);
312 * Test send_instant_messages when messaging is disabled.
314 public function test_send_instant_messages_messaging_disabled() {
317 $this->resetAfterTest(true);
319 // Transactions used in tests, tell phpunit use alternative reset method.
320 $this->preventResetByRollback();
322 $user1 = self
::getDataGenerator()->create_user();
323 $user2 = self
::getDataGenerator()->create_user();
325 $this->setUser($user1);
327 // Disable messaging.
330 // Create test message data.
332 $message1['touserid'] = $user2->id
;
333 $message1['text'] = 'the message.';
334 $message1['clientmsgid'] = 4;
335 $messages = array($message1);
337 $this->expectException('moodle_exception');
338 core_message_external
::send_instant_messages($messages);
342 * Test create_contacts.
346 public function test_create_contacts() {
347 $this->resetAfterTest(true);
349 $user1 = self
::getDataGenerator()->create_user();
350 $user2 = self
::getDataGenerator()->create_user();
351 $user3 = self
::getDataGenerator()->create_user();
352 $user4 = self
::getDataGenerator()->create_user();
353 $user5 = self
::getDataGenerator()->create_user();
354 $this->setUser($user1);
356 // Adding a contact who is already a contact.
357 $return = core_message_external
::create_contacts(array($user2->id
));
358 $return = external_api
::clean_returnvalue(core_message_external
::create_contacts_returns(), $return);
359 $this->assertEquals(array(), $return);
361 // Adding multiple contacts.
362 $return = core_message_external
::create_contacts(array($user3->id
, $user4->id
));
363 $return = external_api
::clean_returnvalue(core_message_external
::create_contacts_returns(), $return);
364 $this->assertEquals(array(), $return);
366 // Note: We should add real user checks in api L:2656.
368 // Try to add a contact to another user, should throw an exception.
369 // All assertions must be added before this point.
370 $this->expectException('required_capability_exception');
371 core_message_external
::create_contacts(array($user2->id
), $user3->id
);
375 * Test delete_contacts.
377 public function test_delete_contacts() {
378 $this->resetAfterTest(true);
380 $user1 = self
::getDataGenerator()->create_user();
381 $user2 = self
::getDataGenerator()->create_user();
382 $user3 = self
::getDataGenerator()->create_user();
383 $user4 = self
::getDataGenerator()->create_user();
384 $user5 = self
::getDataGenerator()->create_user();
385 $user6 = self
::getDataGenerator()->create_user();
386 $this->setUser($user1);
388 \core_message\api
::add_contact($user1->id
, $user3->id
);
389 \core_message\api
::add_contact($user1->id
, $user4->id
);
390 \core_message\api
::add_contact($user1->id
, $user5->id
);
391 \core_message\api
::add_contact($user1->id
, $user6->id
);
393 // Removing a non-contact.
394 $return = core_message_external
::delete_contacts(array($user2->id
));
395 $this->assertNull($return);
397 // Removing one contact.
398 $return = core_message_external
::delete_contacts(array($user3->id
));
399 $this->assertNull($return);
401 // Removing multiple contacts.
402 $return = core_message_external
::delete_contacts(array($user4->id
, $user5->id
));
403 $this->assertNull($return);
405 // Removing contact from unexisting user.
406 $return = core_message_external
::delete_contacts(array(99999));
407 $this->assertNull($return);
409 // Removing mixed valid and invalid data.
410 $return = core_message_external
::delete_contacts(array($user6->id
, 99999));
411 $this->assertNull($return);
413 // Try to delete a contact of another user contact list, should throw an exception.
414 // All assertions must be added before this point.
415 $this->expectException('required_capability_exception');
416 core_message_external
::delete_contacts(array($user2->id
), $user3->id
);
420 * Test block_contacts.
422 public function test_block_contacts() {
423 $this->resetAfterTest(true);
425 $user1 = self
::getDataGenerator()->create_user();
426 $user2 = self
::getDataGenerator()->create_user();
427 $user3 = self
::getDataGenerator()->create_user();
428 $user4 = self
::getDataGenerator()->create_user();
429 $user5 = self
::getDataGenerator()->create_user();
430 $this->setUser($user1);
432 \core_message\api
::add_contact($user1->id
, $user3->id
);
433 \core_message\api
::add_contact($user1->id
, $user4->id
);
434 \core_message\api
::add_contact($user1->id
, $user5->id
);
436 // Blocking a contact who is already a contact.
437 $return = core_message_external
::block_contacts(array($user2->id
));
438 $return = external_api
::clean_returnvalue(core_message_external
::block_contacts_returns(), $return);
439 $this->assertEquals(array(array(
441 'itemid' => $user2->id
,
442 'warningcode' => 'contactnotblocked',
443 'message' => 'The contact could not be blocked'
446 // Blocking multiple contacts.
447 $return = core_message_external
::block_contacts(array($user3->id
, $user4->id
));
448 $return = external_api
::clean_returnvalue(core_message_external
::block_contacts_returns(), $return);
449 $this->assertEquals(array(
452 'itemid' => $user3->id
,
453 'warningcode' => 'contactnotblocked',
454 'message' => 'The contact could not be blocked'
458 'itemid' => $user4->id
,
459 'warningcode' => 'contactnotblocked',
460 'message' => 'The contact could not be blocked'
464 // Blocking a non-existing user.
465 $return = core_message_external
::block_contacts(array(99999));
466 $return = external_api
::clean_returnvalue(core_message_external
::block_contacts_returns(), $return);
467 $this->assertCount(1, $return);
468 $return = array_pop($return);
469 $this->assertEquals($return['warningcode'], 'contactnotblocked');
470 $this->assertEquals($return['itemid'], 99999);
472 // Try to block a contact of another user contact list, should throw an exception.
473 // All assertions must be added before this point.
474 $this->expectException('required_capability_exception');
475 core_message_external
::block_contacts(array($user2->id
), $user3->id
);
479 * Test unblock_contacts.
481 public function test_unblock_contacts() {
482 $this->resetAfterTest(true);
484 $user1 = self
::getDataGenerator()->create_user();
485 $user2 = self
::getDataGenerator()->create_user();
486 $user3 = self
::getDataGenerator()->create_user();
487 $user4 = self
::getDataGenerator()->create_user();
488 $user5 = self
::getDataGenerator()->create_user();
489 $user6 = self
::getDataGenerator()->create_user();
490 $this->setUser($user1);
492 \core_message\api
::add_contact($user1->id
, $user3->id
);
493 \core_message\api
::add_contact($user1->id
, $user4->id
);
494 \core_message\api
::add_contact($user1->id
, $user5->id
);
495 \core_message\api
::add_contact($user1->id
, $user6->id
);
497 // Removing a non-contact.
498 $return = core_message_external
::unblock_contacts(array($user2->id
));
499 $this->assertNull($return);
501 // Removing one contact.
502 $return = core_message_external
::unblock_contacts(array($user3->id
));
503 $this->assertNull($return);
505 // Removing multiple contacts.
506 $return = core_message_external
::unblock_contacts(array($user4->id
, $user5->id
));
507 $this->assertNull($return);
509 // Removing contact from unexisting user.
510 $return = core_message_external
::unblock_contacts(array(99999));
511 $this->assertNull($return);
513 // Removing mixed valid and invalid data.
514 $return = core_message_external
::unblock_contacts(array($user6->id
, 99999));
515 $this->assertNull($return);
517 // Try to unblock a contact of another user contact list, should throw an exception.
518 // All assertions must be added before this point.
519 $this->expectException('required_capability_exception');
520 core_message_external
::unblock_contacts(array($user2->id
), $user3->id
);
524 * Test getting contact requests.
526 public function test_get_contact_requests() {
529 $this->resetAfterTest();
531 $user1 = self
::getDataGenerator()->create_user();
532 $user2 = self
::getDataGenerator()->create_user();
533 $user3 = self
::getDataGenerator()->create_user();
535 $this->setUser($user1);
537 // Block one user, their request should not show up.
538 \core_message\api
::block_user($user1->id
, $user3->id
);
540 \core_message\api
::create_contact_request($user2->id
, $user1->id
);
541 \core_message\api
::create_contact_request($user3->id
, $user1->id
);
543 $requests = core_message_external
::get_contact_requests($user1->id
);
544 $requests = external_api
::clean_returnvalue(core_message_external
::get_contact_requests_returns(), $requests);
546 $this->assertCount(1, $requests);
548 $request = reset($requests);
549 $userpicture = new \
user_picture($user2);
550 $profileimageurl = $userpicture->get_url($PAGE)->out(false);
552 $this->assertEquals($user2->id
, $request['id']);
553 $this->assertEquals(fullname($user2), $request['fullname']);
554 $this->assertArrayHasKey('profileimageurl', $request);
555 $this->assertArrayHasKey('profileimageurlsmall', $request);
556 $this->assertArrayHasKey('isonline', $request);
557 $this->assertArrayHasKey('showonlinestatus', $request);
558 $this->assertArrayHasKey('isblocked', $request);
559 $this->assertArrayHasKey('iscontact', $request);
563 * Test the get_contact_requests() function when the user has blocked the sender of the request.
565 public function test_get_contact_requests_blocked_sender() {
566 $this->resetAfterTest();
567 $user1 = self
::getDataGenerator()->create_user();
568 $user2 = self
::getDataGenerator()->create_user();
570 // User1 blocks User2.
571 \core_message\api
::block_user($user1->id
, $user2->id
);
573 // User2 tries to add User1 as a contact.
574 \core_message\api
::create_contact_request($user2->id
, $user1->id
);
576 // Verify we don't see the contact request from the blocked user User2 in the requests for User1.
577 $this->setUser($user1);
578 $requests = core_message_external
::get_contact_requests($user1->id
);
579 $requests = external_api
::clean_returnvalue(core_message_external
::get_contact_requests_returns(), $requests);
581 $this->assertCount(0, $requests);
585 * Test getting contact requests when there are none.
587 public function test_get_contact_requests_no_requests() {
588 $this->resetAfterTest();
590 $user1 = self
::getDataGenerator()->create_user();
592 $this->setUser($user1);
594 $requests = core_message_external
::get_contact_requests($user1->id
);
595 $requests = external_api
::clean_returnvalue(core_message_external
::get_contact_requests_returns(), $requests);
597 $this->assertEmpty($requests);
601 * Test getting contact requests with limits.
603 public function test_get_contact_requests_with_limits() {
604 $this->resetAfterTest();
606 $user1 = self
::getDataGenerator()->create_user();
607 $user2 = self
::getDataGenerator()->create_user();
608 $user3 = self
::getDataGenerator()->create_user();
610 $this->setUser($user1);
612 \core_message\api
::create_contact_request($user2->id
, $user1->id
);
613 \core_message\api
::create_contact_request($user3->id
, $user1->id
);
615 $requests = core_message_external
::get_contact_requests($user1->id
, 0, 1);
616 $requests = external_api
::clean_returnvalue(core_message_external
::get_contact_requests_returns(), $requests);
618 $this->assertCount(1, $requests);
622 * Test getting contact requests with messaging disabled.
624 public function test_get_contact_requests_messaging_disabled() {
627 $this->resetAfterTest();
629 // Create some skeleton data just so we can call the WS.
630 $user1 = self
::getDataGenerator()->create_user();
632 $this->setUser($user1);
634 // Disable messaging.
637 // Ensure an exception is thrown.
638 $this->expectException('moodle_exception');
639 core_message_external
::get_contact_requests($user1->id
);
643 * Test getting contact requests with no permission.
645 public function test_get_contact_requests_no_permission() {
646 $this->resetAfterTest();
648 // Create some skeleton data just so we can call the WS.
649 $user1 = self
::getDataGenerator()->create_user();
650 $user2 = self
::getDataGenerator()->create_user();
651 $user3 = self
::getDataGenerator()->create_user();
653 $this->setUser($user3);
655 // Ensure an exception is thrown.
656 $this->expectException('required_capability_exception');
657 core_message_external
::create_contact_request($user1->id
, $user2->id
);
661 * Test getting the number of received contact requests.
663 public function test_get_received_contact_requests_count() {
664 $this->resetAfterTest();
666 $user1 = self
::getDataGenerator()->create_user();
667 $user2 = self
::getDataGenerator()->create_user();
668 $user3 = self
::getDataGenerator()->create_user();
669 $user4 = self
::getDataGenerator()->create_user();
671 $this->setUser($user1);
673 $contactrequestnumber = core_message_external
::get_received_contact_requests_count($user1->id
);
674 $contactrequestnumber = external_api
::clean_returnvalue(
675 core_message_external
::get_received_contact_requests_count_returns(), $contactrequestnumber);
676 $this->assertEquals(0, $contactrequestnumber);
678 \core_message\api
::create_contact_request($user2->id
, $user1->id
);
680 $contactrequestnumber = core_message_external
::get_received_contact_requests_count($user1->id
);
681 $contactrequestnumber = external_api
::clean_returnvalue(
682 core_message_external
::get_received_contact_requests_count_returns(), $contactrequestnumber);
683 $this->assertEquals(1, $contactrequestnumber);
685 \core_message\api
::create_contact_request($user3->id
, $user1->id
);
687 $contactrequestnumber = core_message_external
::get_received_contact_requests_count($user1->id
);
688 $contactrequestnumber = external_api
::clean_returnvalue(
689 core_message_external
::get_received_contact_requests_count_returns(), $contactrequestnumber);
690 $this->assertEquals(2, $contactrequestnumber);
692 \core_message\api
::create_contact_request($user1->id
, $user4->id
);
694 // Web service should ignore sent requests.
695 $contactrequestnumber = core_message_external
::get_received_contact_requests_count($user1->id
);
696 $contactrequestnumber = external_api
::clean_returnvalue(
697 core_message_external
::get_received_contact_requests_count_returns(), $contactrequestnumber);
698 $this->assertEquals(2, $contactrequestnumber);
702 * Test the get_received_contact_requests_count() function when the user has blocked the sender of the request.
704 public function test_get_received_contact_requests_count_blocked_sender() {
705 $this->resetAfterTest();
706 $user1 = self
::getDataGenerator()->create_user();
707 $user2 = self
::getDataGenerator()->create_user();
709 // User1 blocks User2.
710 \core_message\api
::block_user($user1->id
, $user2->id
);
712 // User2 tries to add User1 as a contact.
713 \core_message\api
::create_contact_request($user2->id
, $user1->id
);
715 // Verify we don't see the contact request from the blocked user User2 in the count for User1.
716 $this->setUser($user1);
717 $contactrequestnumber = core_message_external
::get_received_contact_requests_count($user1->id
);
718 $contactrequestnumber = external_api
::clean_returnvalue(
719 core_message_external
::get_received_contact_requests_count_returns(), $contactrequestnumber);
720 $this->assertEquals(0, $contactrequestnumber);
724 * Test getting the number of received contact requests with no permissions.
726 public function test_get_received_contact_requests_count_no_permission() {
727 $this->resetAfterTest();
729 // Create some skeleton data just so we can call the WS.
730 $user1 = self
::getDataGenerator()->create_user();
731 $user2 = self
::getDataGenerator()->create_user();
733 $this->setUser($user2);
735 // Ensure an exception is thrown.
736 $this->expectException('required_capability_exception');
737 core_message_external
::get_received_contact_requests_count($user1->id
);
741 * Test getting the number of received contact requests with messaging disabled.
743 public function test_get_received_contact_requests_count_messaging_disabled() {
746 $this->resetAfterTest();
748 // Create some skeleton data just so we can call the WS.
749 $user1 = self
::getDataGenerator()->create_user();
751 $this->setUser($user1);
753 // Disable messaging.
756 // Ensure an exception is thrown.
757 $this->expectException('moodle_exception');
758 core_message_external
::get_received_contact_requests_count($user1->id
);
762 * Test creating a contact request.
764 public function test_create_contact_request() {
767 $this->resetAfterTest();
769 $user1 = self
::getDataGenerator()->create_user();
770 $user2 = self
::getDataGenerator()->create_user();
772 $this->setUser($user1);
774 // Allow users to message anyone site-wide.
775 $CFG->messagingallusers
= 1;
777 $return = core_message_external
::create_contact_request($user1->id
, $user2->id
);
778 $return = external_api
::clean_returnvalue(core_message_external
::create_contact_request_returns(), $return);
779 $this->assertEquals([], $return['warnings']);
781 $request = $DB->get_records('message_contact_requests');
783 $this->assertCount(1, $request);
785 $request = reset($request);
787 $this->assertEquals($request->id
, $return['request']['id']);
788 $this->assertEquals($request->userid
, $return['request']['userid']);
789 $this->assertEquals($request->requesteduserid
, $return['request']['requesteduserid']);
790 $this->assertEquals($request->timecreated
, $return['request']['timecreated']);
794 * Test creating a contact request when not allowed.
796 public function test_create_contact_request_not_allowed() {
799 $this->resetAfterTest();
801 $user1 = self
::getDataGenerator()->create_user();
802 $user2 = self
::getDataGenerator()->create_user();
804 $this->setUser($user1);
806 $CFG->messagingallusers
= 0;
808 $return = core_message_external
::create_contact_request($user1->id
, $user2->id
);
809 $return = external_api
::clean_returnvalue(core_message_external
::create_contact_request_returns(), $return);
811 $warning = reset($return['warnings']);
813 $this->assertEquals('user', $warning['item']);
814 $this->assertEquals($user2->id
, $warning['itemid']);
815 $this->assertEquals('cannotcreatecontactrequest', $warning['warningcode']);
816 $this->assertEquals('You are unable to create a contact request for this user', $warning['message']);
820 * Test creating a contact request with messaging disabled.
822 public function test_create_contact_request_messaging_disabled() {
825 $this->resetAfterTest();
827 // Create some skeleton data just so we can call the WS.
828 $user1 = self
::getDataGenerator()->create_user();
829 $user2 = self
::getDataGenerator()->create_user();
831 $this->setUser($user1);
833 // Disable messaging.
836 // Ensure an exception is thrown.
837 $this->expectException('moodle_exception');
838 core_message_external
::create_contact_request($user1->id
, $user2->id
);
842 * Test creating a contact request with no permission.
844 public function test_create_contact_request_no_permission() {
845 $this->resetAfterTest();
847 // Create some skeleton data just so we can call the WS.
848 $user1 = self
::getDataGenerator()->create_user();
849 $user2 = self
::getDataGenerator()->create_user();
850 $user3 = self
::getDataGenerator()->create_user();
852 $this->setUser($user3);
854 // Ensure an exception is thrown.
855 $this->expectException('required_capability_exception');
856 core_message_external
::create_contact_request($user1->id
, $user2->id
);
860 * Test confirming a contact request.
862 public function test_confirm_contact_request() {
865 $this->resetAfterTest();
867 $user1 = self
::getDataGenerator()->create_user();
868 $user2 = self
::getDataGenerator()->create_user();
870 $this->setUser($user1);
872 \core_message\api
::create_contact_request($user1->id
, $user2->id
);
874 $this->setUser($user2);
876 $return = core_message_external
::confirm_contact_request($user1->id
, $user2->id
);
877 $return = external_api
::clean_returnvalue(core_message_external
::confirm_contact_request_returns(), $return);
878 $this->assertEquals(array(), $return);
880 $this->assertEquals(0, $DB->count_records('message_contact_requests'));
882 $contact = $DB->get_records('message_contacts');
884 $this->assertCount(1, $contact);
886 $contact = reset($contact);
888 $this->assertEquals($user1->id
, $contact->userid
);
889 $this->assertEquals($user2->id
, $contact->contactid
);
893 * Test confirming a contact request with messaging disabled.
895 public function test_confirm_contact_request_messaging_disabled() {
898 $this->resetAfterTest();
900 // Create some skeleton data just so we can call the WS.
901 $user1 = self
::getDataGenerator()->create_user();
902 $user2 = self
::getDataGenerator()->create_user();
904 $this->setUser($user1);
906 // Disable messaging.
909 // Ensure an exception is thrown.
910 $this->expectException('moodle_exception');
911 core_message_external
::confirm_contact_request($user1->id
, $user2->id
);
915 * Test confirming a contact request with no permission.
917 public function test_confirm_contact_request_no_permission() {
918 $this->resetAfterTest();
920 // Create some skeleton data just so we can call the WS.
921 $user1 = self
::getDataGenerator()->create_user();
922 $user2 = self
::getDataGenerator()->create_user();
923 $user3 = self
::getDataGenerator()->create_user();
925 $this->setUser($user3);
927 // Ensure an exception is thrown.
928 $this->expectException('required_capability_exception');
929 core_message_external
::confirm_contact_request($user1->id
, $user2->id
);
933 * Test declining a contact request.
935 public function test_decline_contact_request() {
938 $this->resetAfterTest();
940 $user1 = self
::getDataGenerator()->create_user();
941 $user2 = self
::getDataGenerator()->create_user();
943 $this->setUser($user1);
945 \core_message\api
::create_contact_request($user1->id
, $user2->id
);
947 $this->setUser($user2);
949 $return = core_message_external
::decline_contact_request($user1->id
, $user2->id
);
950 $return = external_api
::clean_returnvalue(core_message_external
::decline_contact_request_returns(), $return);
951 $this->assertEquals(array(), $return);
953 $this->assertEquals(0, $DB->count_records('message_contact_requests'));
954 $this->assertEquals(0, $DB->count_records('message_contacts'));
958 * Test declining a contact request with messaging disabled.
960 public function test_decline_contact_request_messaging_disabled() {
963 $this->resetAfterTest();
965 // Create some skeleton data just so we can call the WS.
966 $user1 = self
::getDataGenerator()->create_user();
967 $user2 = self
::getDataGenerator()->create_user();
969 $this->setUser($user1);
971 // Disable messaging.
974 // Ensure an exception is thrown.
975 $this->expectException('moodle_exception');
976 core_message_external
::decline_contact_request($user1->id
, $user2->id
);
980 * Test declining a contact request with no permission.
982 public function test_decline_contact_request_no_permission() {
983 $this->resetAfterTest();
985 // Create some skeleton data just so we can call the WS.
986 $user1 = self
::getDataGenerator()->create_user();
987 $user2 = self
::getDataGenerator()->create_user();
988 $user3 = self
::getDataGenerator()->create_user();
990 $this->setUser($user3);
992 // Ensure an exception is thrown.
993 $this->expectException('required_capability_exception');
994 core_message_external
::decline_contact_request($user1->id
, $user2->id
);
998 * Test muting conversations.
1000 public function test_mute_conversations() {
1003 $this->resetAfterTest(true);
1005 $user1 = self
::getDataGenerator()->create_user();
1006 $user2 = self
::getDataGenerator()->create_user();
1008 $conversation = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
1009 [$user1->id
, $user2->id
]);
1011 $this->setUser($user1);
1013 // Muting a conversation.
1014 $return = core_message_external
::mute_conversations($user1->id
, [$conversation->id
]);
1015 $return = external_api
::clean_returnvalue(core_message_external
::mute_conversations_returns(), $return);
1016 $this->assertEquals(array(), $return);
1018 // Get list of muted conversations.
1019 $mca = $DB->get_record('message_conversation_actions', []);
1021 $this->assertEquals($user1->id
, $mca->userid
);
1022 $this->assertEquals($conversation->id
, $mca->conversationid
);
1023 $this->assertEquals(\core_message\api
::CONVERSATION_ACTION_MUTED
, $mca->action
);
1025 // Muting a conversation that is already muted.
1026 $return = core_message_external
::mute_conversations($user1->id
, [$conversation->id
]);
1027 $return = external_api
::clean_returnvalue(core_message_external
::mute_conversations_returns(), $return);
1028 $this->assertEquals(array(), $return);
1030 $this->assertEquals(1, $DB->count_records('message_conversation_actions'));
1034 * Test muting a conversation with messaging disabled.
1036 public function test_mute_conversations_messaging_disabled() {
1039 $this->resetAfterTest();
1041 // Create some skeleton data just so we can call the WS.
1042 $user1 = self
::getDataGenerator()->create_user();
1043 $user2 = self
::getDataGenerator()->create_user();
1045 $conversation = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
1046 [$user1->id
, $user2->id
]);
1048 $this->setUser($user1);
1050 // Disable messaging.
1051 $CFG->messaging
= 0;
1053 // Ensure an exception is thrown.
1054 $this->expectException('moodle_exception');
1055 core_message_external
::mute_conversations($user1->id
, [$conversation->id
]);
1059 * Test muting a conversation with no permission.
1061 public function test_mute_conversations_no_permission() {
1062 $this->resetAfterTest();
1064 // Create some skeleton data just so we can call the WS.
1065 $user1 = self
::getDataGenerator()->create_user();
1066 $user2 = self
::getDataGenerator()->create_user();
1067 $user3 = self
::getDataGenerator()->create_user();
1069 $conversation = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
1070 [$user1->id
, $user2->id
]);
1072 $this->setUser($user3);
1074 // Ensure an exception is thrown.
1075 $this->expectException('required_capability_exception');
1076 core_message_external
::mute_conversations($user1->id
, [$conversation->id
]);
1080 * Test unmuting conversations.
1082 public function test_unmute_conversations() {
1085 $this->resetAfterTest(true);
1087 $user1 = self
::getDataGenerator()->create_user();
1088 $user2 = self
::getDataGenerator()->create_user();
1090 $conversation = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
1091 [$user1->id
, $user2->id
]);
1093 $this->setUser($user1);
1095 // Mute the conversation.
1096 \core_message\api
::mute_conversation($user1->id
, $conversation->id
);
1098 // Unmuting a conversation.
1099 $return = core_message_external
::unmute_conversations($user1->id
, [$conversation->id
]);
1100 $return = external_api
::clean_returnvalue(core_message_external
::unmute_conversations_returns(), $return);
1101 $this->assertEquals(array(), $return);
1103 $this->assertEquals(0, $DB->count_records('message_conversation_actions'));
1105 // Unmuting a conversation which is already unmuted.
1106 $return = core_message_external
::unmute_conversations($user1->id
, [$conversation->id
]);
1107 $return = external_api
::clean_returnvalue(core_message_external
::unmute_conversations_returns(), $return);
1108 $this->assertEquals(array(), $return);
1110 $this->assertEquals(0, $DB->count_records('message_conversation_actions'));
1114 * Test unmuting a conversation with messaging disabled.
1116 public function test_unmute_conversation_messaging_disabled() {
1119 $this->resetAfterTest();
1121 // Create some skeleton data just so we can call the WS.
1122 $user1 = self
::getDataGenerator()->create_user();
1123 $user2 = self
::getDataGenerator()->create_user();
1125 $conversation = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
1126 [$user1->id
, $user2->id
]);
1128 $this->setUser($user1);
1130 // Disable messaging.
1131 $CFG->messaging
= 0;
1133 // Ensure an exception is thrown.
1134 $this->expectException('moodle_exception');
1135 core_message_external
::unmute_conversations($user1->id
, [$user2->id
]);
1139 * Test unmuting a conversation with no permission.
1141 public function test_unmute_conversation_no_permission() {
1142 $this->resetAfterTest();
1144 // Create some skeleton data just so we can call the WS.
1145 $user1 = self
::getDataGenerator()->create_user();
1146 $user2 = self
::getDataGenerator()->create_user();
1147 $user3 = self
::getDataGenerator()->create_user();
1149 $conversation = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
1150 [$user1->id
, $user2->id
]);
1152 $this->setUser($user3);
1154 // Ensure an exception is thrown.
1155 $this->expectException('required_capability_exception');
1156 core_message_external
::unmute_conversations($user1->id
, [$conversation->id
]);
1160 * Test blocking a user.
1162 public function test_block_user() {
1165 $this->resetAfterTest(true);
1167 $user1 = self
::getDataGenerator()->create_user();
1168 $user2 = self
::getDataGenerator()->create_user();
1170 $this->setUser($user1);
1173 $return = core_message_external
::block_user($user1->id
, $user2->id
);
1174 $return = external_api
::clean_returnvalue(core_message_external
::block_user_returns(), $return);
1175 $this->assertEquals(array(), $return);
1177 // Get list of blocked users.
1178 $record = $DB->get_record('message_users_blocked', []);
1180 $this->assertEquals($user1->id
, $record->userid
);
1181 $this->assertEquals($user2->id
, $record->blockeduserid
);
1183 // Blocking a user who is already blocked.
1184 $return = core_message_external
::block_user($user1->id
, $user2->id
);
1185 $return = external_api
::clean_returnvalue(core_message_external
::block_user_returns(), $return);
1186 $this->assertEquals(array(), $return);
1188 $this->assertEquals(1, $DB->count_records('message_users_blocked'));
1192 * Test blocking a user.
1194 public function test_block_user_when_ineffective() {
1197 $this->resetAfterTest(true);
1199 $user1 = self
::getDataGenerator()->create_user();
1200 $user2 = self
::getDataGenerator()->create_user();
1202 $this->setUser($user1);
1204 $authenticateduser = $DB->get_record('role', array('shortname' => 'user'));
1205 assign_capability('moodle/site:messageanyuser', CAP_ALLOW
, $authenticateduser->id
, context_system
::instance(), true);
1208 $return = core_message_external
::block_user($user1->id
, $user2->id
);
1209 $return = external_api
::clean_returnvalue(core_message_external
::block_user_returns(), $return);
1210 $this->assertEquals(array(), $return);
1212 $this->assertEquals(0, $DB->count_records('message_users_blocked'));
1216 * Test blocking a user with messaging disabled.
1218 public function test_block_user_messaging_disabled() {
1221 $this->resetAfterTest();
1223 // Create some skeleton data just so we can call the WS.
1224 $user1 = self
::getDataGenerator()->create_user();
1225 $user2 = self
::getDataGenerator()->create_user();
1227 $this->setUser($user1);
1229 // Disable messaging.
1230 $CFG->messaging
= 0;
1232 // Ensure an exception is thrown.
1233 $this->expectException('moodle_exception');
1234 core_message_external
::block_user($user1->id
, $user2->id
);
1238 * Test blocking a user with no permission.
1240 public function test_block_user_no_permission() {
1241 $this->resetAfterTest();
1243 // Create some skeleton data just so we can call the WS.
1244 $user1 = self
::getDataGenerator()->create_user();
1245 $user2 = self
::getDataGenerator()->create_user();
1246 $user3 = self
::getDataGenerator()->create_user();
1248 $this->setUser($user3);
1250 // Ensure an exception is thrown.
1251 $this->expectException('required_capability_exception');
1252 core_message_external
::block_user($user1->id
, $user2->id
);
1256 * Test unblocking a user.
1258 public function test_unblock_user() {
1261 $this->resetAfterTest(true);
1263 $user1 = self
::getDataGenerator()->create_user();
1264 $user2 = self
::getDataGenerator()->create_user();
1266 $this->setUser($user1);
1269 \core_message\api
::block_user($user1->id
, $user2->id
);
1271 // Unblocking a user.
1272 $return = core_message_external
::unblock_user($user1->id
, $user2->id
);
1273 $return = external_api
::clean_returnvalue(core_message_external
::unblock_user_returns(), $return);
1274 $this->assertEquals(array(), $return);
1276 $this->assertEquals(0, $DB->count_records('message_users_blocked'));
1278 // Unblocking a user who is already unblocked.
1279 $return = core_message_external
::unblock_user($user1->id
, $user2->id
);
1280 $return = external_api
::clean_returnvalue(core_message_external
::unblock_user_returns(), $return);
1281 $this->assertEquals(array(), $return);
1283 $this->assertEquals(0, $DB->count_records('message_users_blocked'));
1287 * Test unblocking a user with messaging disabled.
1289 public function test_unblock_user_messaging_disabled() {
1292 $this->resetAfterTest();
1294 // Create some skeleton data just so we can call the WS.
1295 $user1 = self
::getDataGenerator()->create_user();
1296 $user2 = self
::getDataGenerator()->create_user();
1298 $this->setUser($user1);
1300 // Disable messaging.
1301 $CFG->messaging
= 0;
1303 // Ensure an exception is thrown.
1304 $this->expectException('moodle_exception');
1305 core_message_external
::unblock_user($user1->id
, $user2->id
);
1309 * Test unblocking a user with no permission.
1311 public function test_unblock_user_no_permission() {
1312 $this->resetAfterTest();
1314 // Create some skeleton data just so we can call the WS.
1315 $user1 = self
::getDataGenerator()->create_user();
1316 $user2 = self
::getDataGenerator()->create_user();
1317 $user3 = self
::getDataGenerator()->create_user();
1319 $this->setUser($user3);
1321 // Ensure an exception is thrown.
1322 $this->expectException('required_capability_exception');
1323 core_message_external
::unblock_user($user1->id
, $user2->id
);
1327 * Test get_contacts.
1329 public function test_get_contacts() {
1330 $this->resetAfterTest(true);
1332 $user1 = self
::getDataGenerator()->create_user();
1333 $user_stranger = self
::getDataGenerator()->create_user();
1334 $user_offline1 = self
::getDataGenerator()->create_user();
1335 $user_offline2 = self
::getDataGenerator()->create_user();
1336 $user_offline3 = self
::getDataGenerator()->create_user();
1337 $user_online = new stdClass();
1338 $user_online->lastaccess
= time();
1339 $user_online = self
::getDataGenerator()->create_user($user_online);
1340 $user_blocked = self
::getDataGenerator()->create_user();
1341 $noreplyuser = core_user
::get_user(core_user
::NOREPLY_USER
);
1344 $this->setUser($user1);
1345 \core_message\api
::add_contact($user1->id
, $user_offline1->id
);
1346 \core_message\api
::add_contact($user1->id
, $user_offline2->id
);
1347 \core_message\api
::add_contact($user1->id
, $user_offline3->id
);
1348 \core_message\api
::add_contact($user1->id
, $user_online->id
);
1350 // User_stranger sends a couple of messages to user1.
1351 $this->send_message($user_stranger, $user1, 'Hello there!');
1352 $this->send_message($user_stranger, $user1, 'How you goin?');
1353 $this->send_message($user_stranger, $user1, 'Cya!');
1354 $this->send_message($noreplyuser, $user1, 'I am not a real user');
1356 // User_blocked sends a message to user1.
1357 $this->send_message($user_blocked, $user1, 'Here, have some spam.');
1359 // Retrieve the contacts of the user.
1360 $this->setUser($user1);
1361 $contacts = core_message_external
::get_contacts();
1362 $contacts = external_api
::clean_returnvalue(core_message_external
::get_contacts_returns(), $contacts);
1363 $this->assertCount(3, $contacts['offline']);
1364 $this->assertCount(1, $contacts['online']);
1365 $this->assertCount(3, $contacts['strangers']);
1366 core_message_external
::block_contacts(array($user_blocked->id
));
1367 $contacts = core_message_external
::get_contacts();
1368 $contacts = external_api
::clean_returnvalue(core_message_external
::get_contacts_returns(), $contacts);
1369 $this->assertCount(3, $contacts['offline']);
1370 $this->assertCount(1, $contacts['online']);
1371 $this->assertCount(2, $contacts['strangers']);
1373 // Checking some of the fields returned.
1374 $stranger = array_pop($contacts['strangers']);
1376 $this->assertEquals(core_user
::NOREPLY_USER
, $stranger['id']);
1377 $this->assertEquals(1, $stranger['unread']);
1379 // Check that deleted users are not returned.
1380 delete_user($user_offline1);
1381 delete_user($user_stranger);
1382 delete_user($user_online);
1383 $contacts = core_message_external
::get_contacts();
1384 $contacts = external_api
::clean_returnvalue(core_message_external
::get_contacts_returns(), $contacts);
1385 $this->assertCount(2, $contacts['offline']);
1386 $this->assertCount(0, $contacts['online']);
1387 $this->assertCount(1, $contacts['strangers']);
1391 * Test search_contacts.
1392 * @expectedException moodle_exception
1394 public function test_search_contacts() {
1396 $this->resetAfterTest(true);
1398 $course1 = $this->getDataGenerator()->create_course();
1399 $course2 = $this->getDataGenerator()->create_course();
1401 $user1 = new stdClass();
1402 $user1->firstname
= 'X';
1403 $user1->lastname
= 'X';
1404 $user1 = $this->getDataGenerator()->create_user($user1);
1405 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
1406 $this->getDataGenerator()->enrol_user($user1->id
, $course2->id
);
1408 $user2 = new stdClass();
1409 $user2->firstname
= 'Eric';
1410 $user2->lastname
= 'Cartman';
1411 $user2 = self
::getDataGenerator()->create_user($user2);
1412 $user3 = new stdClass();
1413 $user3->firstname
= 'Stan';
1414 $user3->lastname
= 'Marsh';
1415 $user3 = self
::getDataGenerator()->create_user($user3);
1416 self
::getDataGenerator()->enrol_user($user3->id
, $course1->id
);
1417 $user4 = new stdClass();
1418 $user4->firstname
= 'Kyle';
1419 $user4->lastname
= 'Broflovski';
1420 $user4 = self
::getDataGenerator()->create_user($user4);
1421 $user5 = new stdClass();
1422 $user5->firstname
= 'Kenny';
1423 $user5->lastname
= 'McCormick';
1424 $user5 = self
::getDataGenerator()->create_user($user5);
1425 self
::getDataGenerator()->enrol_user($user5->id
, $course2->id
);
1427 $this->setUser($user1);
1429 $results = core_message_external
::search_contacts('r');
1430 $results = external_api
::clean_returnvalue(core_message_external
::search_contacts_returns(), $results);
1431 $this->assertCount(5, $results); // Users 2 through 5 + admin
1433 $results = core_message_external
::search_contacts('r', true);
1434 $results = external_api
::clean_returnvalue(core_message_external
::search_contacts_returns(), $results);
1435 $this->assertCount(2, $results);
1437 $results = core_message_external
::search_contacts('Kyle', false);
1438 $results = external_api
::clean_returnvalue(core_message_external
::search_contacts_returns(), $results);
1439 $this->assertCount(1, $results);
1440 $result = reset($results);
1441 $this->assertEquals($user4->id
, $result['id']);
1443 $results = core_message_external
::search_contacts('y', false);
1444 $results = external_api
::clean_returnvalue(core_message_external
::search_contacts_returns(), $results);
1445 $this->assertCount(2, $results);
1447 $results = core_message_external
::search_contacts('y', true);
1448 $results = external_api
::clean_returnvalue(core_message_external
::search_contacts_returns(), $results);
1449 $this->assertCount(1, $results);
1450 $result = reset($results);
1451 $this->assertEquals($user5->id
, $result['id']);
1453 // Empty query, will throw an exception.
1454 $results = core_message_external
::search_contacts('');
1458 * Test get_messages.
1460 public function test_get_messages() {
1462 $this->resetAfterTest(true);
1464 $this->preventResetByRollback();
1465 // This mark the messages as read!.
1466 $sink = $this->redirectMessages();
1468 $user1 = self
::getDataGenerator()->create_user();
1469 $user2 = self
::getDataGenerator()->create_user();
1470 $user3 = self
::getDataGenerator()->create_user();
1472 $course = self
::getDataGenerator()->create_course();
1474 // Send a message from one user to another.
1475 message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE
);
1476 message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE
);
1477 message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE
);
1478 message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE
);
1479 message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE
);
1481 $this->setUser($user1);
1482 // Get read conversations from user1 to user2.
1483 $messages = core_message_external
::get_messages($user2->id
, $user1->id
, 'conversations', true, true, 0, 0);
1484 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1485 $this->assertCount(1, $messages['messages']);
1487 // Delete the message.
1488 $message = array_shift($messages['messages']);
1489 \core_message\api
::delete_message($user1->id
, $message['id']);
1491 $messages = core_message_external
::get_messages($user2->id
, $user1->id
, 'conversations', true, true, 0, 0);
1492 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1493 $this->assertCount(0, $messages['messages']);
1495 // Get unread conversations from user1 to user2.
1496 $messages = core_message_external
::get_messages($user2->id
, $user1->id
, 'conversations', false, true, 0, 0);
1497 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1498 $this->assertCount(0, $messages['messages']);
1500 // Get read messages send from user1.
1501 $messages = core_message_external
::get_messages(0, $user1->id
, 'conversations', true, true, 0, 0);
1502 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1503 $this->assertCount(1, $messages['messages']);
1505 $this->setUser($user2);
1506 // Get read conversations from any user to user2.
1507 $messages = core_message_external
::get_messages($user2->id
, 0, 'conversations', true, true, 0, 0);
1508 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1509 $this->assertCount(2, $messages['messages']);
1511 // Conversations from user3 to user2.
1512 $messages = core_message_external
::get_messages($user2->id
, $user3->id
, 'conversations', true, true, 0, 0);
1513 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1514 $this->assertCount(1, $messages['messages']);
1516 // Delete the message.
1517 $message = array_shift($messages['messages']);
1518 \core_message\api
::delete_message($user2->id
, $message['id']);
1520 $messages = core_message_external
::get_messages($user2->id
, $user3->id
, 'conversations', true, true, 0, 0);
1521 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1522 $this->assertCount(0, $messages['messages']);
1524 $this->setUser($user3);
1525 // Get read notifications received by user3.
1526 $messages = core_message_external
::get_messages($user3->id
, 0, 'notifications', true, true, 0, 0);
1527 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1528 $this->assertCount(0, $messages['messages']);
1530 // Now, create some notifications...
1531 // We are creating fake notifications but based on real ones.
1533 // This one comes from a disabled plugin's provider and therefore is not sent.
1534 $eventdata = new \core\message\
message();
1535 $eventdata->courseid
= $course->id
;
1536 $eventdata->notification
= 1;
1537 $eventdata->modulename
= 'moodle';
1538 $eventdata->component
= 'enrol_paypal';
1539 $eventdata->name
= 'paypal_enrolment';
1540 $eventdata->userfrom
= get_admin();
1541 $eventdata->userto
= $user1;
1542 $eventdata->subject
= "Moodle: PayPal payment";
1543 $eventdata->fullmessage
= "Your PayPal payment is pending.";
1544 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
1545 $eventdata->fullmessagehtml
= '';
1546 $eventdata->smallmessage
= '';
1547 message_send($eventdata);
1548 $this->assertDebuggingCalled('Attempt to send msg from a provider enrol_paypal/paypal_enrolment '.
1549 'that is inactive or not allowed for the user id='.$user1->id
);
1551 // This one omits notification = 1.
1552 $message = new \core\message\
message();
1553 $message->courseid
= $course->id
;
1554 $message->component
= 'enrol_manual';
1555 $message->name
= 'expiry_notification';
1556 $message->userfrom
= $user2;
1557 $message->userto
= $user1;
1558 $message->subject
= 'Test: This is not a notification but otherwise is valid';
1559 $message->fullmessage
= 'Test: Full message';
1560 $message->fullmessageformat
= FORMAT_MARKDOWN
;
1561 $message->fullmessagehtml
= markdown_to_html($message->fullmessage
);
1562 $message->smallmessage
= $message->subject
;
1563 $message->contexturlname
= $course->fullname
;
1564 $message->contexturl
= (string)new moodle_url('/course/view.php', array('id' => $course->id
));
1565 message_send($message);
1567 $message = new \core\message\
message();
1568 $message->courseid
= $course->id
;
1569 $message->notification
= 1;
1570 $message->component
= 'enrol_manual';
1571 $message->name
= 'expiry_notification';
1572 $message->userfrom
= $user2;
1573 $message->userto
= $user1;
1574 $message->subject
= 'Enrolment expired';
1575 $message->fullmessage
= 'Enrolment expired blah blah blah';
1576 $message->fullmessageformat
= FORMAT_MARKDOWN
;
1577 $message->fullmessagehtml
= markdown_to_html($message->fullmessage
);
1578 $message->smallmessage
= $message->subject
;
1579 $message->contexturlname
= $course->fullname
;
1580 $message->contexturl
= (string)new moodle_url('/course/view.php', array('id' => $course->id
));
1581 message_send($message);
1583 $userfrom = core_user
::get_noreply_user();
1584 $userfrom->maildisplay
= true;
1585 $eventdata = new \core\message\
message();
1586 $eventdata->courseid
= $course->id
;
1587 $eventdata->component
= 'moodle';
1588 $eventdata->name
= 'badgecreatornotice';
1589 $eventdata->userfrom
= $userfrom;
1590 $eventdata->userto
= $user1;
1591 $eventdata->notification
= 1;
1592 $eventdata->subject
= 'New badge';
1593 $eventdata->fullmessage
= format_text_email($eventdata->subject
, FORMAT_HTML
);
1594 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
1595 $eventdata->fullmessagehtml
= $eventdata->subject
;
1596 $eventdata->smallmessage
= $eventdata->subject
;
1597 message_send($eventdata);
1599 $eventdata = new \core\message\
message();
1600 $eventdata->courseid
= $course->id
;
1601 $eventdata->name
= 'submission';
1602 $eventdata->component
= 'mod_feedback';
1603 $eventdata->userfrom
= $user1;
1604 $eventdata->userto
= $user2;
1605 $eventdata->subject
= 'Feedback submitted';
1606 $eventdata->fullmessage
= 'Feedback submitted from an user';
1607 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
1608 $eventdata->fullmessagehtml
= '<strong>Feedback submitted</strong>';
1609 $eventdata->smallmessage
= '';
1610 $eventdata->customdata
= ['datakey' => 'data'];
1611 message_send($eventdata);
1613 $this->setUser($user1);
1614 // Get read notifications from any user to user1.
1615 $messages = core_message_external
::get_messages($user1->id
, 0, 'notifications', true, true, 0, 0);
1616 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1617 $this->assertCount(3, $messages['messages']);
1619 // Get one read notifications from any user to user1.
1620 $messages = core_message_external
::get_messages($user1->id
, 0, 'notifications', true, true, 0, 1);
1621 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1622 $this->assertCount(1, $messages['messages']);
1624 // Get unread notifications from any user to user1.
1625 $messages = core_message_external
::get_messages($user1->id
, 0, 'notifications', false, true, 0, 0);
1626 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1627 $this->assertCount(0, $messages['messages']);
1629 // Get read both type of messages from any user to user1.
1630 $messages = core_message_external
::get_messages($user1->id
, 0, 'both', true, true, 0, 0);
1631 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1632 $this->assertCount(4, $messages['messages']);
1634 // Get read notifications from no-reply-user to user1.
1635 $messages = core_message_external
::get_messages($user1->id
, $userfrom->id
, 'notifications', true, true, 0, 0);
1636 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1637 $this->assertCount(1, $messages['messages']);
1639 // Get notifications send by user1 to any user.
1640 $messages = core_message_external
::get_messages(0, $user1->id
, 'notifications', true, true, 0, 0);
1641 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1642 $this->assertCount(1, $messages['messages']);
1643 // Check we receive custom data as a unserialisable json.
1644 $this->assertObjectHasAttribute('datakey', json_decode($messages['messages'][0]['customdata']));
1645 $this->assertEquals('mod_feedback', $messages['messages'][0]['component']);
1646 $this->assertEquals('submission', $messages['messages'][0]['eventtype']);
1649 $CFG->messaging
= 0;
1651 $messages = core_message_external
::get_messages(0, $user1->id
, 'both', true, true, 0, 0);
1652 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1653 $this->assertCount(1, $messages['warnings']);
1657 // Messaging disabled.
1659 $messages = core_message_external
::get_messages(0, $user1->id
, 'conversations', true, true, 0, 0);
1660 $this->fail('Exception expected due messaging disabled.');
1661 } catch (moodle_exception
$e) {
1662 $this->assertEquals('disabled', $e->errorcode
);
1665 $CFG->messaging
= 1;
1669 $messages = core_message_external
::get_messages(0, 0, 'conversations', true, true, 0, 0);
1670 $this->fail('Exception expected due invalid users.');
1671 } catch (moodle_exception
$e) {
1672 $this->assertEquals('accessdenied', $e->errorcode
);
1675 // Invalid user ids.
1677 $messages = core_message_external
::get_messages(2500, 0, 'conversations', true, true, 0, 0);
1678 $this->fail('Exception expected due invalid users.');
1679 } catch (moodle_exception
$e) {
1680 $this->assertEquals('invaliduser', $e->errorcode
);
1683 // Invalid users (permissions).
1684 $this->setUser($user2);
1686 $messages = core_message_external
::get_messages(0, $user1->id
, 'conversations', true, true, 0, 0);
1687 $this->fail('Exception expected due invalid user.');
1688 } catch (moodle_exception
$e) {
1689 $this->assertEquals('accessdenied', $e->errorcode
);
1695 * Test get_messages where we want all messages from a user, sent to any user.
1697 public function test_get_messages_useridto_all() {
1698 $this->resetAfterTest(true);
1700 $user1 = self
::getDataGenerator()->create_user();
1701 $user2 = self
::getDataGenerator()->create_user();
1702 $user3 = self
::getDataGenerator()->create_user();
1704 $this->setUser($user1);
1706 // Send a message from user 1 to two other users.
1707 $this->send_message($user1, $user2, 'some random text 1', 0, 1);
1708 $this->send_message($user1, $user3, 'some random text 2', 0, 2);
1710 // Get messages sent from user 1.
1711 $messages = core_message_external
::get_messages(0, $user1->id
, 'conversations', false, false, 0, 0);
1712 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1714 // Confirm the data is correct.
1715 $messages = $messages['messages'];
1716 $this->assertCount(2, $messages);
1718 $message1 = array_shift($messages);
1719 $message2 = array_shift($messages);
1721 $this->assertEquals($user1->id
, $message1['useridfrom']);
1722 $this->assertEquals($user2->id
, $message1['useridto']);
1724 $this->assertEquals($user1->id
, $message2['useridfrom']);
1725 $this->assertEquals($user3->id
, $message2['useridto']);
1729 * Test get_messages where we want all messages to a user, sent by any user.
1731 public function test_get_messages_useridfrom_all() {
1732 $this->resetAfterTest();
1734 $user1 = self
::getDataGenerator()->create_user();
1735 $user2 = self
::getDataGenerator()->create_user();
1736 $user3 = self
::getDataGenerator()->create_user();
1738 $this->setUser($user1);
1740 // Send a message to user 1 from two other users.
1741 $this->send_message($user2, $user1, 'some random text 1', 0, 1);
1742 $this->send_message($user3, $user1, 'some random text 2', 0, 2);
1744 // Get messages sent to user 1.
1745 $messages = core_message_external
::get_messages($user1->id
, 0, 'conversations', false, false, 0, 0);
1746 $messages = external_api
::clean_returnvalue(core_message_external
::get_messages_returns(), $messages);
1748 // Confirm the data is correct.
1749 $messages = $messages['messages'];
1750 $this->assertCount(2, $messages);
1752 $message1 = array_shift($messages);
1753 $message2 = array_shift($messages);
1755 $this->assertEquals($user2->id
, $message1['useridfrom']);
1756 $this->assertEquals($user1->id
, $message1['useridto']);
1758 $this->assertEquals($user3->id
, $message2['useridfrom']);
1759 $this->assertEquals($user1->id
, $message2['useridto']);
1763 * Test get_blocked_users.
1765 public function test_get_blocked_users() {
1766 $this->resetAfterTest(true);
1768 $user1 = self
::getDataGenerator()->create_user();
1769 $userstranger = self
::getDataGenerator()->create_user();
1770 $useroffline1 = self
::getDataGenerator()->create_user();
1771 $useroffline2 = self
::getDataGenerator()->create_user();
1772 $userblocked = self
::getDataGenerator()->create_user();
1775 $this->setUser($user1);
1777 \core_message\api
::add_contact($user1->id
, $useroffline1->id
);
1778 \core_message\api
::add_contact($user1->id
, $useroffline2->id
);
1780 // The userstranger sends a couple of messages to user1.
1781 $this->send_message($userstranger, $user1, 'Hello there!');
1782 $this->send_message($userstranger, $user1, 'How you goin?');
1784 // The userblocked sends a message to user1.
1785 // Note that this user is not blocked at this point.
1786 $this->send_message($userblocked, $user1, 'Here, have some spam.');
1788 // Retrieve the list of blocked users.
1789 $this->setUser($user1);
1790 $blockedusers = core_message_external
::get_blocked_users($user1->id
);
1791 $blockedusers = external_api
::clean_returnvalue(core_message_external
::get_blocked_users_returns(), $blockedusers);
1792 $this->assertCount(0, $blockedusers['users']);
1794 // Block the $userblocked and retrieve again the list.
1795 core_message_external
::block_contacts(array($userblocked->id
));
1796 $blockedusers = core_message_external
::get_blocked_users($user1->id
);
1797 $blockedusers = external_api
::clean_returnvalue(core_message_external
::get_blocked_users_returns(), $blockedusers);
1798 $this->assertCount(1, $blockedusers['users']);
1800 // Remove the $userblocked and check that the list now is empty.
1801 delete_user($userblocked);
1802 $blockedusers = core_message_external
::get_blocked_users($user1->id
);
1803 $blockedusers = external_api
::clean_returnvalue(core_message_external
::get_blocked_users_returns(), $blockedusers);
1804 $this->assertCount(0, $blockedusers['users']);
1808 * Test mark_message_read.
1810 public function test_mark_message_read() {
1811 $this->resetAfterTest(true);
1813 $user1 = self
::getDataGenerator()->create_user();
1814 $user2 = self
::getDataGenerator()->create_user();
1815 $user3 = self
::getDataGenerator()->create_user();
1818 $this->setUser($user1);
1819 \core_message\api
::add_contact($user1->id
, $user2->id
);
1820 \core_message\api
::add_contact($user1->id
, $user3->id
);
1822 // The user2 sends a couple of messages to user1.
1823 $this->send_message($user2, $user1, 'Hello there!');
1824 $this->send_message($user2, $user1, 'How you goin?');
1825 $this->send_message($user3, $user1, 'How you goin?');
1826 $this->send_message($user3, $user2, 'How you goin?');
1828 // Retrieve all messages sent by user2 (they are currently unread).
1829 $lastmessages = message_get_messages($user1->id
, $user2->id
, 0, false);
1831 $messageids = array();
1832 foreach ($lastmessages as $m) {
1833 $messageid = core_message_external
::mark_message_read($m->id
, time());
1834 $messageids[] = external_api
::clean_returnvalue(core_message_external
::mark_message_read_returns(), $messageid);
1837 // Retrieve all messages sent (they are currently read).
1838 $lastmessages = message_get_messages($user1->id
, $user2->id
, 0, true);
1839 $this->assertCount(2, $lastmessages);
1840 $this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages);
1841 $this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages);
1843 // Retrieve all messages sent by any user (that are currently unread).
1844 $lastmessages = message_get_messages($user1->id
, 0, 0, false);
1845 $this->assertCount(1, $lastmessages);
1847 // Invalid message ids.
1849 $messageid = core_message_external
::mark_message_read(1337, time());
1850 $this->fail('Exception expected due invalid messageid.');
1851 } catch (dml_missing_record_exception
$e) {
1852 $this->assertEquals('invalidrecordunknown', $e->errorcode
);
1855 // A message to a different user.
1856 $lastmessages = message_get_messages($user2->id
, $user3->id
, 0, false);
1857 $messageid = array_pop($lastmessages)->id
;
1859 $messageid = core_message_external
::mark_message_read($messageid, time());
1860 $this->fail('Exception expected due invalid messageid.');
1861 } catch (invalid_parameter_exception
$e) {
1862 $this->assertEquals('invalidparameter', $e->errorcode
);
1867 * Test mark_notification_read.
1869 public function test_mark_notification_read() {
1870 $this->resetAfterTest(true);
1872 $user1 = self
::getDataGenerator()->create_user();
1873 $user2 = self
::getDataGenerator()->create_user();
1874 $user3 = self
::getDataGenerator()->create_user();
1877 $this->setUser($user1);
1878 \core_message\api
::add_contact($user1->id
, $user2->id
);
1879 \core_message\api
::add_contact($user1->id
, $user3->id
);
1881 // The user2 sends a couple of notifications to user1.
1882 $this->send_message($user2, $user1, 'Hello there!', 1);
1883 $this->send_message($user2, $user1, 'How you goin?', 1);
1884 $this->send_message($user3, $user1, 'How you goin?', 1);
1885 $this->send_message($user3, $user2, 'How you goin?', 1);
1887 // Retrieve all notifications sent by user2 (they are currently unread).
1888 $lastnotifications = message_get_messages($user1->id
, $user2->id
, 1, false);
1890 $notificationids = array();
1891 foreach ($lastnotifications as $n) {
1892 $notificationid = core_message_external
::mark_notification_read($n->id
, time());
1893 $notificationids[] = external_api
::clean_returnvalue(core_message_external
::mark_notification_read_returns(),
1897 // Retrieve all notifications sent (they are currently read).
1898 $lastnotifications = message_get_messages($user1->id
, $user2->id
, 1, true);
1899 $this->assertCount(2, $lastnotifications);
1900 $this->assertArrayHasKey($notificationids[1]['notificationid'], $lastnotifications);
1901 $this->assertArrayHasKey($notificationids[0]['notificationid'], $lastnotifications);
1903 // Retrieve all notifications sent by any user (that are currently unread).
1904 $lastnotifications = message_get_messages($user1->id
, 0, 1, false);
1905 $this->assertCount(1, $lastnotifications);
1907 // Invalid notification ids.
1909 $notificationid = core_message_external
::mark_notification_read(1337, time());
1910 $this->fail('Exception expected due invalid notificationid.');
1911 } catch (dml_missing_record_exception
$e) {
1912 $this->assertEquals('invalidrecord', $e->errorcode
);
1915 // A notification to a different user.
1916 $lastnotifications = message_get_messages($user2->id
, $user3->id
, 1, false);
1917 $notificationid = array_pop($lastnotifications)->id
;
1919 $notificationid = core_message_external
::mark_notification_read($notificationid, time());
1920 $this->fail('Exception expected due invalid notificationid.');
1921 } catch (invalid_parameter_exception
$e) {
1922 $this->assertEquals('invalidparameter', $e->errorcode
);
1927 * Test delete_message.
1929 public function test_delete_message() {
1931 $this->resetAfterTest(true);
1933 $user1 = self
::getDataGenerator()->create_user();
1934 $user2 = self
::getDataGenerator()->create_user();
1935 $user3 = self
::getDataGenerator()->create_user();
1936 $user4 = self
::getDataGenerator()->create_user();
1939 $this->setUser($user1);
1940 \core_message\api
::add_contact($user1->id
, $user2->id
);
1941 \core_message\api
::add_contact($user1->id
, $user3->id
);
1943 // User user1 does not interchange messages with user3.
1944 $m1to2 = message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE
);
1945 $m2to3 = message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE
);
1946 $m3to2 = message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE
);
1947 $m3to4 = message_post_message($user3, $user4, 'some random text 4', FORMAT_MOODLE
);
1949 // Retrieve all messages sent by user2 (they are currently unread).
1950 $lastmessages = message_get_messages($user1->id
, $user2->id
, 0, false);
1952 // Delete a message not read, as a user from.
1953 $result = core_message_external
::delete_message($m1to2, $user1->id
, false);
1954 $result = external_api
::clean_returnvalue(core_message_external
::delete_message_returns(), $result);
1955 $this->assertTrue($result['status']);
1956 $this->assertCount(0, $result['warnings']);
1957 $mua = $DB->get_record('message_user_actions', array('messageid' => $m1to2, 'userid' => $user1->id
));
1958 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua->action
);
1960 // Try to delete the same message again.
1961 $result = core_message_external
::delete_message($m1to2, $user1->id
, false);
1962 $result = external_api
::clean_returnvalue(core_message_external
::delete_message_returns(), $result);
1963 $this->assertFalse($result['status']);
1965 // Try to delete a message that does not belong to me.
1967 $messageid = core_message_external
::delete_message($m2to3, $user3->id
, false);
1968 $this->fail('Exception expected due invalid messageid.');
1969 } catch (moodle_exception
$e) {
1970 $this->assertEquals('You do not have permission to delete this message', $e->errorcode
);
1973 $this->setUser($user3);
1974 // Delete a message not read, as a user to.
1975 $result = core_message_external
::delete_message($m2to3, $user3->id
, false);
1976 $result = external_api
::clean_returnvalue(core_message_external
::delete_message_returns(), $result);
1977 $this->assertTrue($result['status']);
1978 $this->assertCount(0, $result['warnings']);
1979 $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m2to3, 'userid' => $user3->id
,
1980 'action' => \core_message\api
::MESSAGE_ACTION_DELETED
)));
1982 // Delete a message read.
1983 $message = $DB->get_record('messages', ['id' => $m3to2]);
1984 \core_message\api
::mark_message_as_read($user3->id
, $message, time());
1985 $result = core_message_external
::delete_message($m3to2, $user3->id
);
1986 $result = external_api
::clean_returnvalue(core_message_external
::delete_message_returns(), $result);
1987 $this->assertTrue($result['status']);
1988 $this->assertCount(0, $result['warnings']);
1989 $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to2, 'userid' => $user3->id
,
1990 'action' => \core_message\api
::MESSAGE_ACTION_DELETED
)));
1992 // Invalid message ids.
1994 $result = core_message_external
::delete_message(-1, $user1->id
);
1995 $this->fail('Exception expected due invalid messageid.');
1996 } catch (dml_missing_record_exception
$e) {
1997 $this->assertEquals('invalidrecord', $e->errorcode
);
2002 $result = core_message_external
::delete_message($m1to2, -1, false);
2003 $this->fail('Exception expected due invalid user.');
2004 } catch (moodle_exception
$e) {
2005 $this->assertEquals('invaliduser', $e->errorcode
);
2009 delete_user($user2);
2011 $result = core_message_external
::delete_message($m1to2, $user2->id
, false);
2012 $this->fail('Exception expected due invalid user.');
2013 } catch (moodle_exception
$e) {
2014 $this->assertEquals('userdeleted', $e->errorcode
);
2017 // Now, as an admin, try to delete any message.
2018 $this->setAdminUser();
2019 $result = core_message_external
::delete_message($m3to4, $user4->id
, false);
2020 $result = external_api
::clean_returnvalue(core_message_external
::delete_message_returns(), $result);
2021 $this->assertTrue($result['status']);
2022 $this->assertCount(0, $result['warnings']);
2023 $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to4, 'userid' => $user4->id
,
2024 'action' => \core_message\api
::MESSAGE_ACTION_DELETED
)));
2028 public function test_mark_all_notifications_as_read_invalid_user_exception() {
2029 $this->resetAfterTest(true);
2031 $this->expectException('moodle_exception');
2032 core_message_external
::mark_all_notifications_as_read(-2132131, 0);
2035 public function test_mark_all_notifications_as_read_access_denied_exception() {
2036 $this->resetAfterTest(true);
2038 $sender = $this->getDataGenerator()->create_user();
2039 $user = $this->getDataGenerator()->create_user();
2041 $this->setUser($user);
2042 $this->expectException('moodle_exception');
2043 core_message_external
::mark_all_notifications_as_read($sender->id
, 0);
2046 public function test_mark_all_notifications_as_read_missing_from_user_exception() {
2047 $this->resetAfterTest(true);
2049 $sender = $this->getDataGenerator()->create_user();
2051 $this->setUser($sender);
2052 $this->expectException('moodle_exception');
2053 core_message_external
::mark_all_notifications_as_read($sender->id
, 99999);
2056 public function test_mark_all_notifications_as_read() {
2059 $this->resetAfterTest(true);
2061 $sender1 = $this->getDataGenerator()->create_user();
2062 $sender2 = $this->getDataGenerator()->create_user();
2063 $sender3 = $this->getDataGenerator()->create_user();
2064 $recipient = $this->getDataGenerator()->create_user();
2066 $this->setUser($recipient);
2068 $this->send_message($sender1, $recipient, 'Notification', 1);
2069 $this->send_message($sender1, $recipient, 'Notification', 1);
2070 $this->send_message($sender2, $recipient, 'Notification', 1);
2071 $this->send_message($sender2, $recipient, 'Notification', 1);
2072 $this->send_message($sender3, $recipient, 'Notification', 1);
2073 $this->send_message($sender3, $recipient, 'Notification', 1);
2075 core_message_external
::mark_all_notifications_as_read($recipient->id
, $sender1->id
);
2076 $readnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', [$recipient->id
]);
2077 $unreadnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NULL', [$recipient->id
]);
2079 $this->assertCount(2, $readnotifications);
2080 $this->assertCount(4, $unreadnotifications);
2082 core_message_external
::mark_all_notifications_as_read($recipient->id
, 0);
2083 $readnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', [$recipient->id
]);
2084 $unreadnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NULL', [$recipient->id
]);
2086 $this->assertCount(6, $readnotifications);
2087 $this->assertCount(0, $unreadnotifications);
2090 public function test_mark_all_notifications_as_read_time_created_to() {
2093 $this->resetAfterTest(true);
2095 $sender1 = $this->getDataGenerator()->create_user();
2096 $sender2 = $this->getDataGenerator()->create_user();
2098 $recipient = $this->getDataGenerator()->create_user();
2099 $this->setUser($recipient);
2101 // Record messages as sent on one second intervals.
2104 $this->send_message($sender1, $recipient, 'Message 1', 1, $time);
2105 $this->send_message($sender2, $recipient, 'Message 2', 1, $time +
1);
2106 $this->send_message($sender1, $recipient, 'Message 3', 1, $time +
2);
2107 $this->send_message($sender2, $recipient, 'Message 4', 1, $time +
3);
2109 // Mark notifications sent from sender1 up until the second message; should only mark the first notification as read.
2110 core_message_external
::mark_all_notifications_as_read($recipient->id
, $sender1->id
, $time +
1);
2112 $params = [$recipient->id
];
2114 $this->assertEquals(1, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', $params));
2115 $this->assertEquals(3, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NULL', $params));
2117 // Mark all notifications as read from any sender up to the time the third message was sent.
2118 core_message_external
::mark_all_notifications_as_read($recipient->id
, 0, $time +
2);
2120 $this->assertEquals(3, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', $params));
2121 $this->assertEquals(1, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NULL', $params));
2123 // Mark all notifications as read from any sender with a time after all messages were sent.
2124 core_message_external
::mark_all_notifications_as_read($recipient->id
, 0, $time +
10);
2126 $this->assertEquals(4, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', $params));
2127 $this->assertEquals(0, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NULL', $params));
2131 * Test get_user_notification_preferences
2133 public function test_get_user_notification_preferences() {
2134 $this->resetAfterTest(true);
2136 $user = self
::getDataGenerator()->create_user();
2137 $this->setUser($user);
2139 // Set a couple of preferences to test.
2140 set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
2141 set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
2143 $prefs = core_message_external
::get_user_notification_preferences();
2144 $prefs = external_api
::clean_returnvalue(core_message_external
::get_user_notification_preferences_returns(), $prefs);
2145 // Check processors.
2146 $this->assertGreaterThanOrEqual(2, count($prefs['preferences']['processors']));
2147 $this->assertEquals($user->id
, $prefs['preferences']['userid']);
2149 // Check components.
2150 $this->assertGreaterThanOrEqual(8, count($prefs['preferences']['components']));
2152 // Check some preferences that we previously set.
2154 foreach ($prefs['preferences']['components'] as $component) {
2155 foreach ($component['notifications'] as $prefdata) {
2156 if ($prefdata['preferencekey'] != 'message_provider_mod_assign_assign_notification') {
2159 foreach ($prefdata['processors'] as $processor) {
2160 if ($processor['name'] == 'popup') {
2161 $this->assertTrue($processor['loggedin']['checked']);
2163 } else if ($processor['name'] == 'email') {
2164 $this->assertTrue($processor['loggedoff']['checked']);
2170 $this->assertEquals(2, $found);
2174 * Test get_user_notification_preferences permissions
2176 public function test_get_user_notification_preferences_permissions() {
2177 $this->resetAfterTest(true);
2179 $user = self
::getDataGenerator()->create_user();
2180 $otheruser = self
::getDataGenerator()->create_user();
2181 $this->setUser($user);
2183 $this->expectException('moodle_exception');
2184 $prefs = core_message_external
::get_user_notification_preferences($otheruser->id
);
2188 * Tests searching users in a course.
2190 public function test_data_for_messagearea_search_users_in_course() {
2191 $this->resetAfterTest(true);
2193 // Create some users.
2194 $user1 = new stdClass();
2195 $user1->firstname
= 'User';
2196 $user1->lastname
= 'One';
2197 $user1 = self
::getDataGenerator()->create_user($user1);
2199 // The person doing the search.
2200 $this->setUser($user1);
2202 // Set the second user's status to online by setting their last access to now.
2203 $user2 = new stdClass();
2204 $user2->firstname
= 'User';
2205 $user2->lastname
= 'Two';
2206 $user2->lastaccess
= time();
2207 $user2 = self
::getDataGenerator()->create_user($user2);
2209 // Block the second user.
2210 \core_message\api
::block_user($user1->id
, $user2->id
);
2212 $user3 = new stdClass();
2213 $user3->firstname
= 'User';
2214 $user3->lastname
= 'Three';
2215 $user3 = self
::getDataGenerator()->create_user($user3);
2218 $course1 = new stdClass();
2219 $course1->fullname
= 'Course';
2220 $course1->shortname
= 'One';
2221 $course1 = $this->getDataGenerator()->create_course();
2223 // Enrol the user we are doing the search for and one user in the course.
2224 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
2225 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
2227 // Perform a search.
2228 $result = core_message_external
::data_for_messagearea_search_users_in_course($user1->id
, $course1->id
, 'User');
2230 // We need to execute the return values cleaning process to simulate the web service.
2231 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_search_users_in_course_returns(),
2234 // Check that we only retrieved a user that was enrolled, and that the user performing the search was not returned.
2235 $users = $result['contacts'];
2236 $this->assertCount(1, $users);
2239 $this->assertEquals($user2->id
, $user['userid']);
2240 $this->assertEquals(fullname($user2), $user['fullname']);
2241 $this->assertFalse($user['ismessaging']);
2242 $this->assertFalse($user['sentfromcurrentuser']);
2243 $this->assertNull($user['lastmessage']);
2244 $this->assertNull($user['messageid']);
2245 $this->assertNull($user['isonline']);
2246 $this->assertFalse($user['isread']);
2247 $this->assertTrue($user['isblocked']);
2248 $this->assertNull($user['unreadcount']);
2252 * Tests searching users in course as another user.
2254 public function test_data_for_messagearea_search_users_in_course_as_other_user() {
2255 $this->resetAfterTest(true);
2257 // The person doing the search for another user.
2258 $this->setAdminUser();
2260 // Create some users.
2261 $user1 = new stdClass();
2262 $user1->firstname
= 'User';
2263 $user1->lastname
= 'One';
2264 $user1 = self
::getDataGenerator()->create_user($user1);
2266 $user2 = new stdClass();
2267 $user2->firstname
= 'User';
2268 $user2->lastname
= 'Two';
2269 $user2 = self
::getDataGenerator()->create_user($user2);
2271 $user3 = new stdClass();
2272 $user3->firstname
= 'User';
2273 $user3->lastname
= 'Three';
2274 $user3 = self
::getDataGenerator()->create_user($user3);
2277 $course1 = new stdClass();
2278 $course1->fullname
= 'Course';
2279 $course1->shortname
= 'One';
2280 $course1 = $this->getDataGenerator()->create_course();
2282 // Enrol the user we are doing the search for and one user in the course.
2283 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
2284 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
2286 // Perform a search.
2287 $result = core_message_external
::data_for_messagearea_search_users_in_course($user1->id
, $course1->id
, 'User');
2289 // We need to execute the return values cleaning process to simulate the web service server.
2290 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_search_users_in_course_returns(),
2293 // Check that we got the user enrolled, and that the user we are performing the search on behalf of was not returned.
2294 $users = $result['contacts'];
2295 $this->assertCount(1, $users);
2298 $this->assertEquals($user2->id
, $user['userid']);
2299 $this->assertEquals(fullname($user2), $user['fullname']);
2300 $this->assertFalse($user['ismessaging']);
2301 $this->assertFalse($user['sentfromcurrentuser']);
2302 $this->assertNull($user['lastmessage']);
2303 $this->assertNull($user['messageid']);
2304 $this->assertFalse($user['isonline']);
2305 $this->assertFalse($user['isread']);
2306 $this->assertFalse($user['isblocked']);
2307 $this->assertNull($user['unreadcount']);
2311 * Tests searching users in course as another user without the proper capabilities.
2313 public function test_data_for_messagearea_search_users_in_course_as_other_user_without_cap() {
2314 $this->resetAfterTest(true);
2316 // Create some users.
2317 $user1 = self
::getDataGenerator()->create_user();
2318 $user2 = self
::getDataGenerator()->create_user();
2320 // The person doing the search for another user.
2321 $this->setUser($user1);
2324 $course = $this->getDataGenerator()->create_course();
2326 // Ensure an exception is thrown.
2327 $this->expectException('moodle_exception');
2328 core_message_external
::data_for_messagearea_search_users_in_course($user2->id
, $course->id
, 'User');
2329 $this->assertDebuggingCalled();
2333 * Tests searching users in course with messaging disabled.
2335 public function test_data_for_messagearea_search_users_in_course_messaging_disabled() {
2338 $this->resetAfterTest(true);
2340 // Create some skeleton data just so we can call the WS..
2341 $user = self
::getDataGenerator()->create_user();
2342 $course = $this->getDataGenerator()->create_course();
2344 // The person doing the search for another user.
2345 $this->setUser($user);
2347 // Disable messaging.
2348 $CFG->messaging
= 0;
2350 // Ensure an exception is thrown.
2351 $this->expectException('moodle_exception');
2352 core_message_external
::data_for_messagearea_search_users_in_course($user->id
, $course->id
, 'User');
2353 $this->assertDebuggingCalled();
2357 * Tests searching users.
2359 public function test_data_for_messagearea_search_users() {
2360 $this->resetAfterTest(true);
2362 // Create some users.
2363 $user1 = new stdClass();
2364 $user1->firstname
= 'User';
2365 $user1->lastname
= 'One';
2366 $user1 = self
::getDataGenerator()->create_user($user1);
2368 // Set as the user performing the search.
2369 $this->setUser($user1);
2371 $user2 = new stdClass();
2372 $user2->firstname
= 'User search';
2373 $user2->lastname
= 'Two';
2374 $user2 = self
::getDataGenerator()->create_user($user2);
2376 $user3 = new stdClass();
2377 $user3->firstname
= 'User search';
2378 $user3->lastname
= 'Three';
2379 $user3 = self
::getDataGenerator()->create_user($user3);
2381 $user4 = new stdClass();
2382 $user4->firstname
= 'User';
2383 $user4->lastname
= 'Four';
2384 $user4 = self
::getDataGenerator()->create_user($user4);
2386 $user5 = new stdClass();
2387 $user5->firstname
= 'User search';
2388 $user5->lastname
= 'Five';
2389 $user5 = self
::getDataGenerator()->create_user($user5);
2391 $user6 = new stdClass();
2392 $user6->firstname
= 'User';
2393 $user6->lastname
= 'Six';
2394 $user6 = self
::getDataGenerator()->create_user($user6);
2396 // Create some courses.
2397 $course1 = new stdClass();
2398 $course1->fullname
= 'Course search';
2399 $course1->shortname
= 'One';
2400 $course1 = $this->getDataGenerator()->create_course($course1);
2402 $course2 = new stdClass();
2403 $course2->fullname
= 'Course';
2404 $course2->shortname
= 'Two';
2405 $course2 = $this->getDataGenerator()->create_course($course2);
2407 $course3 = new stdClass();
2408 $course3->fullname
= 'Course';
2409 $course3->shortname
= 'Three search';
2410 $course3 = $this->getDataGenerator()->create_course($course3);
2412 $course4 = new stdClass();
2413 $course4->fullname
= 'Course Four';
2414 $course4->shortname
= 'CF100';
2415 $course4 = $this->getDataGenerator()->create_course($course4);
2417 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
, 'student');
2418 $this->getDataGenerator()->enrol_user($user1->id
, $course2->id
, 'student');
2419 $this->getDataGenerator()->enrol_user($user1->id
, $course3->id
, 'student');
2421 // Add some users as contacts.
2422 \core_message\api
::add_contact($user1->id
, $user2->id
);
2423 \core_message\api
::add_contact($user1->id
, $user3->id
);
2424 \core_message\api
::add_contact($user1->id
, $user4->id
);
2426 // Perform a search $CFG->messagingallusers setting enabled.
2427 set_config('messagingallusers', 1);
2428 $result = core_message_external
::data_for_messagearea_search_users($user1->id
, 'search');
2430 // We need to execute the return values cleaning process to simulate the web service server.
2431 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_search_users_returns(),
2434 // Confirm that we returns contacts, courses and non-contacts.
2435 $contacts = $result['contacts'];
2436 $courses = $result['courses'];
2437 $noncontacts = $result['noncontacts'];
2439 // Check that we retrieved the correct contacts.
2440 $this->assertCount(2, $contacts);
2441 $this->assertEquals($user3->id
, $contacts[0]['userid']);
2442 $this->assertEquals($user2->id
, $contacts[1]['userid']);
2444 // Check that we retrieved the correct courses.
2445 $this->assertCount(2, $courses);
2446 $this->assertEquals($course3->id
, $courses[0]['id']);
2447 $this->assertEquals($course1->id
, $courses[1]['id']);
2449 // Check that we retrieved the correct non-contacts.
2450 $this->assertCount(1, $noncontacts);
2451 $this->assertEquals($user5->id
, $noncontacts[0]['userid']);
2455 * Tests searching users as another user.
2457 public function test_data_for_messagearea_search_users_as_other_user() {
2458 $this->resetAfterTest(true);
2460 // The person doing the search.
2461 $this->setAdminUser();
2463 // Create some users.
2464 $user1 = new stdClass();
2465 $user1->firstname
= 'User';
2466 $user1->lastname
= 'One';
2467 $user1 = self
::getDataGenerator()->create_user($user1);
2469 $user2 = new stdClass();
2470 $user2->firstname
= 'User search';
2471 $user2->lastname
= 'Two';
2472 $user2 = self
::getDataGenerator()->create_user($user2);
2474 $user3 = new stdClass();
2475 $user3->firstname
= 'User search';
2476 $user3->lastname
= 'Three';
2477 $user3 = self
::getDataGenerator()->create_user($user3);
2479 $user4 = new stdClass();
2480 $user4->firstname
= 'User';
2481 $user4->lastname
= 'Four';
2482 $user4 = self
::getDataGenerator()->create_user($user4);
2484 $user5 = new stdClass();
2485 $user5->firstname
= 'User search';
2486 $user5->lastname
= 'Five';
2487 $user5 = self
::getDataGenerator()->create_user($user5);
2489 $user6 = new stdClass();
2490 $user6->firstname
= 'User';
2491 $user6->lastname
= 'Six';
2492 $user6 = self
::getDataGenerator()->create_user($user6);
2494 // Create some courses.
2495 $course1 = new stdClass();
2496 $course1->fullname
= 'Course search';
2497 $course1->shortname
= 'One';
2498 $course1 = $this->getDataGenerator()->create_course($course1);
2500 $course2 = new stdClass();
2501 $course2->fullname
= 'Course';
2502 $course2->shortname
= 'Two';
2503 $course2 = $this->getDataGenerator()->create_course($course2);
2505 $course3 = new stdClass();
2506 $course3->fullname
= 'Course';
2507 $course3->shortname
= 'Three search';
2508 $course3 = $this->getDataGenerator()->create_course($course3);
2510 // Add some users as contacts.
2511 \core_message\api
::add_contact($user1->id
, $user2->id
);
2512 \core_message\api
::add_contact($user1->id
, $user3->id
);
2513 \core_message\api
::add_contact($user1->id
, $user4->id
);
2515 // Perform a search $CFG->messagingallusers setting enabled.
2516 set_config('messagingallusers', 1);
2517 $result = core_message_external
::data_for_messagearea_search_users($user1->id
, 'search');
2519 // We need to execute the return values cleaning process to simulate the web service server.
2520 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_search_users_returns(),
2523 // Confirm that we returns contacts, courses and non-contacts.
2524 $contacts = $result['contacts'];
2525 $courses = $result['courses'];
2526 $noncontacts = $result['noncontacts'];
2528 // Check that we retrieved the correct contacts.
2529 $this->assertCount(2, $contacts);
2530 $this->assertEquals($user3->id
, $contacts[0]['userid']);
2531 $this->assertEquals($user2->id
, $contacts[1]['userid']);
2533 // Check that we retrieved the correct courses.
2534 $this->assertCount(0, $courses);
2536 // Check that we retrieved the correct non-contacts.
2537 $this->assertCount(1, $noncontacts);
2538 $this->assertEquals($user5->id
, $noncontacts[0]['userid']);
2542 * Tests searching users as another user without the proper capabilities.
2544 public function test_data_for_messagearea_search_users_as_other_user_without_cap() {
2545 $this->resetAfterTest(true);
2547 // Create some users.
2548 $user1 = self
::getDataGenerator()->create_user();
2549 $user2 = self
::getDataGenerator()->create_user();
2551 // The person doing the search for another user.
2552 $this->setUser($user1);
2554 // Ensure an exception is thrown.
2555 $this->expectException('moodle_exception');
2556 core_message_external
::data_for_messagearea_search_users($user2->id
, 'User');
2557 $this->assertDebuggingCalled();
2561 * Tests searching users with messaging disabled.
2563 public function test_data_for_messagearea_search_users_messaging_disabled() {
2566 $this->resetAfterTest(true);
2568 // Create some skeleton data just so we can call the WS.
2569 $user = self
::getDataGenerator()->create_user();
2571 // The person doing the search.
2572 $this->setUser($user);
2574 // Disable messaging.
2575 $CFG->messaging
= 0;
2577 // Ensure an exception is thrown.
2578 $this->expectException('moodle_exception');
2579 core_message_external
::data_for_messagearea_search_users($user->id
, 'User');
2580 $this->assertDebuggingCalled();
2584 * Tests searching for users when site-wide messaging is disabled.
2586 * This test verifies that any contacts are returned, as well as any non-contacts whose profile we can view.
2587 * If checks this by placing some users in the same course, where default caps would permit a user to view another user's
2590 public function test_message_search_users_messagingallusers_disabled() {
2592 $this->resetAfterTest();
2594 // Create some users.
2596 foreach (range(1, 8) as $i) {
2597 $user = new stdClass();
2598 $user->firstname
= ($i == 4) ?
'User' : 'User search'; // Ensure the fourth user won't match the search term.
2599 $user->lastname
= $i;
2600 $user = $this->getDataGenerator()->create_user($user);
2604 // Enrol a few users in the same course, but leave them as non-contacts.
2605 $course1 = $this->getDataGenerator()->create_course();
2606 $course2 = $this->getDataGenerator()->create_course();
2608 $this->setAdminUser();
2609 $this->getDataGenerator()->enrol_user($users[1]->id
, $course1->id
);
2610 $this->getDataGenerator()->enrol_user($users[6]->id
, $course1->id
);
2611 $this->getDataGenerator()->enrol_user($users[7]->id
, $course1->id
);
2613 // Add some other users as contacts.
2614 \core_message\api
::add_contact($users[1]->id
, $users[2]->id
);
2615 \core_message\api
::add_contact($users[3]->id
, $users[1]->id
);
2616 \core_message\api
::add_contact($users[1]->id
, $users[4]->id
);
2618 // Enrol a user as a teacher in the course, and make the teacher role a course contact role.
2619 $this->getDataGenerator()->enrol_user($users[8]->id
, $course2->id
, 'editingteacher');
2620 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
2621 set_config('coursecontact', $teacherrole->id
);
2623 // Create individual conversations between some users, one contact and one non-contact.
2624 \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
2625 [$users[1]->id
, $users[2]->id
]);
2626 \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
2627 [$users[6]->id
, $users[1]->id
]);
2629 // Create a group conversation between 4 users, including a contact and a non-contact.
2630 \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
2631 [$users[1]->id
, $users[2]->id
, $users[4]->id
, $users[7]->id
], 'Project chat');
2633 // Set as the user performing the search.
2634 $this->setUser($users[1]);
2636 // Perform a search with $CFG->messagingallusers disabled.
2637 set_config('messagingallusers', 0);
2638 $result = core_message_external
::message_search_users($users[1]->id
, 'search');
2639 $result = external_api
::clean_returnvalue(core_message_external
::message_search_users_returns(), $result);
2641 // Confirm that we returns contacts and non-contacts.
2642 $this->assertArrayHasKey('contacts', $result);
2643 $this->assertArrayHasKey('noncontacts', $result);
2644 $contacts = $result['contacts'];
2645 $noncontacts = $result['noncontacts'];
2647 // Check that we retrieved the correct contacts.
2648 $this->assertCount(2, $contacts);
2649 $this->assertEquals($users[2]->id
, $contacts[0]['id']);
2650 $this->assertEquals($users[3]->id
, $contacts[1]['id']);
2652 // Verify the correct conversations were returned for the contacts.
2653 $this->assertCount(2, $contacts[0]['conversations']);
2654 // We can't rely on the ordering of conversations within the results, so sort by id first.
2655 usort($contacts[0]['conversations'], function($a, $b) {
2656 return $a['id'] < $b['id'];
2658 $this->assertEquals(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
, $contacts[0]['conversations'][0]['type']);
2659 $this->assertEquals(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
, $contacts[0]['conversations'][1]['type']);
2661 $this->assertCount(0, $contacts[1]['conversations']);
2663 // Check that we retrieved the correct non-contacts.
2664 // When site wide messaging is disabled, we expect to see only those users who we share a course with and whose profiles
2665 // are visible in that course. This excludes users like course contacts.
2666 $this->assertCount(3, $noncontacts);
2667 // Self-conversation first.
2668 $this->assertEquals($users[1]->id
, $noncontacts[0]['id']);
2669 $this->assertEquals($users[6]->id
, $noncontacts[1]['id']);
2670 $this->assertEquals($users[7]->id
, $noncontacts[2]['id']);
2672 // Verify the correct conversations were returned for the non-contacts.
2673 $this->assertCount(1, $noncontacts[1]['conversations']);
2674 $this->assertEquals(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
, $noncontacts[1]['conversations'][0]['type']);
2676 $this->assertCount(1, $noncontacts[2]['conversations']);
2677 $this->assertEquals(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
, $noncontacts[2]['conversations'][0]['type']);
2681 * Tests searching for users when site-wide messaging is enabled.
2683 * This test verifies that any contacts are returned, as well as any non-contacts, regardless of whether the searching user
2684 * can view their respective profile.
2686 public function test_message_search_users_messagingallusers_enabled() {
2688 $this->resetAfterTest();
2690 // Create some users.
2692 foreach (range(1, 9) as $i) {
2693 $user = new stdClass();
2694 $user->firstname
= ($i == 4) ?
'User' : 'User search'; // Ensure the fourth user won't match the search term.
2695 $user->lastname
= $i;
2696 $user = $this->getDataGenerator()->create_user($user);
2700 // Enrol a few users in the same course, but leave them as non-contacts.
2701 $course1 = $this->getDataGenerator()->create_course();
2702 $course2 = $this->getDataGenerator()->create_course();
2704 $this->setAdminUser();
2705 $this->getDataGenerator()->enrol_user($users[1]->id
, $course1->id
);
2706 $this->getDataGenerator()->enrol_user($users[6]->id
, $course1->id
);
2707 $this->getDataGenerator()->enrol_user($users[7]->id
, $course1->id
);
2709 // Add some other users as contacts.
2710 \core_message\api
::add_contact($users[1]->id
, $users[2]->id
);
2711 \core_message\api
::add_contact($users[3]->id
, $users[1]->id
);
2712 \core_message\api
::add_contact($users[1]->id
, $users[4]->id
);
2714 // Enrol a user as a teacher in the course, and make the teacher role a course contact role.
2715 $this->getDataGenerator()->enrol_user($users[9]->id
, $course2->id
, 'editingteacher');
2716 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
2717 set_config('coursecontact', $teacherrole->id
);
2719 // Create individual conversations between some users, one contact and one non-contact.
2720 \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
2721 [$users[1]->id
, $users[2]->id
]);
2722 \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
2723 [$users[6]->id
, $users[1]->id
]);
2725 // Create a group conversation between 5 users, including a contact and a non-contact, and a user NOT in a shared course.
2726 \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
2727 [$users[1]->id
, $users[2]->id
, $users[4]->id
, $users[7]->id
, $users[8]->id
], 'Project chat');
2729 // Set as the user performing the search.
2730 $this->setUser($users[1]);
2732 // Perform a search with $CFG->messagingallusers enabled.
2733 set_config('messagingallusers', 1);
2734 $result = core_message_external
::message_search_users($users[1]->id
, 'search');
2735 $result = external_api
::clean_returnvalue(core_message_external
::message_search_users_returns(), $result);
2737 // Confirm that we returns contacts and non-contacts.
2738 $this->assertArrayHasKey('contacts', $result);
2739 $this->assertArrayHasKey('noncontacts', $result);
2740 $contacts = $result['contacts'];
2741 $noncontacts = $result['noncontacts'];
2743 // Check that we retrieved the correct contacts.
2744 $this->assertCount(2, $contacts);
2745 $this->assertEquals($users[2]->id
, $contacts[0]['id']);
2746 $this->assertEquals($users[3]->id
, $contacts[1]['id']);
2748 // Verify the correct conversations were returned for the contacts.
2749 $this->assertCount(2, $contacts[0]['conversations']);
2750 // We can't rely on the ordering of conversations within the results, so sort by id first.
2751 usort($contacts[0]['conversations'], function($a, $b) {
2752 return $a['id'] < $b['id'];
2754 $this->assertEquals(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
, $contacts[0]['conversations'][0]['type']);
2755 $this->assertEquals(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
, $contacts[0]['conversations'][1]['type']);
2757 $this->assertCount(0, $contacts[1]['conversations']);
2759 // Check that we retrieved the correct non-contacts.
2760 // If site wide messaging is enabled, we expect to be able to search for any users whose profiles we can view.
2761 // In this case, as a student, that's the course contact for course2 and those noncontacts sharing a course with user1.
2762 $this->assertCount(4, $noncontacts);
2763 $this->assertEquals($users[1]->id
, $noncontacts[0]['id']);
2764 $this->assertEquals($users[6]->id
, $noncontacts[1]['id']);
2765 $this->assertEquals($users[7]->id
, $noncontacts[2]['id']);
2766 $this->assertEquals($users[9]->id
, $noncontacts[3]['id']);
2768 // Verify the correct conversations were returned for the non-contacts.
2769 $this->assertCount(1, $noncontacts[1]['conversations']);
2770 $this->assertCount(1, $noncontacts[2]['conversations']);
2771 $this->assertEquals(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
, $noncontacts[1]['conversations'][0]['type']);
2772 $this->assertEquals(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
, $noncontacts[2]['conversations'][0]['type']);
2773 $this->assertCount(0, $noncontacts[3]['conversations']);
2777 * Verify searching for users find themselves when they have self-conversations.
2779 public function test_message_search_users_self_conversations() {
2780 $this->resetAfterTest();
2782 // Create some users.
2783 $user1 = new stdClass();
2784 $user1->firstname
= 'User';
2785 $user1->lastname
= 'One';
2786 $user1 = $this->getDataGenerator()->create_user($user1);
2787 $user2 = new stdClass();
2788 $user2->firstname
= 'User';
2789 $user2->lastname
= 'Two';
2790 $user2 = $this->getDataGenerator()->create_user($user2);
2792 // Get self-conversation for user1.
2793 $sc1 = \core_message\api
::get_self_conversation($user1->id
);
2794 testhelper
::send_fake_message_to_conversation($user1, $sc1->id
, 'Hi myself!');
2796 // Perform a search as user1.
2797 $this->setUser($user1);
2798 $result = core_message_external
::message_search_users($user1->id
, 'One');
2799 $result = external_api
::clean_returnvalue(core_message_external
::message_search_users_returns(), $result);
2801 // Check results are empty.
2802 $this->assertCount(0, $result['contacts']);
2803 $this->assertCount(1, $result['noncontacts']);
2807 * Verify searching for users works even if no matching users from either contacts, or non-contacts can be found.
2809 public function test_message_search_users_with_empty_result() {
2810 $this->resetAfterTest();
2812 // Create some users, but make sure neither will match the search term.
2813 $user1 = new stdClass();
2814 $user1->firstname
= 'User';
2815 $user1->lastname
= 'One';
2816 $user1 = $this->getDataGenerator()->create_user($user1);
2817 $user2 = new stdClass();
2818 $user2->firstname
= 'User';
2819 $user2->lastname
= 'Two';
2820 $user2 = $this->getDataGenerator()->create_user($user2);
2822 // Perform a search as user1.
2823 $this->setUser($user1);
2824 $result = core_message_external
::message_search_users($user1->id
, 'search');
2825 $result = external_api
::clean_returnvalue(core_message_external
::message_search_users_returns(), $result);
2827 // Check results are empty.
2828 $this->assertCount(0, $result['contacts']);
2829 $this->assertCount(0, $result['noncontacts']);
2833 * Test verifying that limits and offsets work for both the contacts and non-contacts return data.
2835 public function test_message_search_users_limit_offset() {
2836 $this->resetAfterTest();
2840 foreach (range(1, 20) as $i) {
2841 $user = new stdClass();
2842 $user->firstname
= "User search";
2843 $user->lastname
= $i;
2844 $user = $this->getDataGenerator()->create_user($user);
2848 // Enrol the first 8 users in the same course, but leave them as non-contacts.
2849 $this->setAdminUser();
2850 $course1 = $this->getDataGenerator()->create_course();
2851 foreach (range(1, 8) as $i) {
2852 $this->getDataGenerator()->enrol_user($users[$i]->id
, $course1->id
);
2855 // Add 5 users, starting at the 11th user, as contacts for user1.
2856 foreach (range(11, 15) as $i) {
2857 \core_message\api
::add_contact($users[1]->id
, $users[$i]->id
);
2860 // Set as the user performing the search.
2861 $this->setUser($users[1]);
2863 // Search using a limit of 3.
2864 // This tests the case where we have more results than the limit for both contacts and non-contacts.
2865 $result = core_message_external
::message_search_users($users[1]->id
, 'search', 0, 3);
2866 $result = external_api
::clean_returnvalue(core_message_external
::message_search_users_returns(), $result);
2867 $contacts = $result['contacts'];
2868 $noncontacts = $result['noncontacts'];
2870 // Check that we retrieved the correct contacts.
2871 $this->assertCount(3, $contacts);
2872 $this->assertEquals($users[11]->id
, $contacts[0]['id']);
2873 $this->assertEquals($users[12]->id
, $contacts[1]['id']);
2874 $this->assertEquals($users[13]->id
, $contacts[2]['id']);
2876 // Check that we retrieved the correct non-contacts.
2877 // Consider first conversation is self-conversation.
2878 $this->assertCount(3, $noncontacts);
2879 $this->assertEquals($users[1]->id
, $noncontacts[0]['id']);
2880 $this->assertEquals($users[2]->id
, $noncontacts[1]['id']);
2881 $this->assertEquals($users[3]->id
, $noncontacts[2]['id']);
2883 // Now, offset to get the next batch of results.
2884 // We expect to see 2 contacts, and 3 non-contacts.
2885 $result = core_message_external
::message_search_users($users[1]->id
, 'search', 3, 3);
2886 $result = external_api
::clean_returnvalue(core_message_external
::message_search_users_returns(), $result);
2887 $contacts = $result['contacts'];
2888 $noncontacts = $result['noncontacts'];
2889 $this->assertCount(2, $contacts);
2890 $this->assertEquals($users[14]->id
, $contacts[0]['id']);
2891 $this->assertEquals($users[15]->id
, $contacts[1]['id']);
2893 $this->assertCount(3, $noncontacts);
2894 $this->assertEquals($users[4]->id
, $noncontacts[0]['id']);
2895 $this->assertEquals($users[5]->id
, $noncontacts[1]['id']);
2896 $this->assertEquals($users[6]->id
, $noncontacts[2]['id']);
2898 // Now, offset to get the next batch of results.
2899 // We expect to see 0 contacts, and 2 non-contacts.
2900 $result = core_message_external
::message_search_users($users[1]->id
, 'search', 6, 3);
2901 $result = external_api
::clean_returnvalue(core_message_external
::message_search_users_returns(), $result);
2902 $contacts = $result['contacts'];
2903 $noncontacts = $result['noncontacts'];
2904 $this->assertCount(0, $contacts);
2906 $this->assertCount(2, $noncontacts);
2907 $this->assertEquals($users[7]->id
, $noncontacts[0]['id']);
2908 $this->assertEquals($users[8]->id
, $noncontacts[1]['id']);
2912 * Tests searching users as another user having the 'moodle/user:viewdetails' capability.
2914 public function test_message_search_users_with_cap() {
2915 $this->resetAfterTest();
2918 // Create some users.
2920 foreach (range(1, 8) as $i) {
2921 $user = new stdClass();
2922 $user->firstname
= ($i == 4) ?
'User' : 'User search'; // Ensure the fourth user won't match the search term.
2923 $user->lastname
= $i;
2924 $user = $this->getDataGenerator()->create_user($user);
2928 // Enrol a few users in the same course, but leave them as non-contacts.
2929 $course1 = $this->getDataGenerator()->create_course();
2930 $this->setAdminUser();
2931 $this->getDataGenerator()->enrol_user($users[1]->id
, $course1->id
);
2932 $this->getDataGenerator()->enrol_user($users[6]->id
, $course1->id
);
2933 $this->getDataGenerator()->enrol_user($users[7]->id
, $course1->id
);
2935 // Add some other users as contacts.
2936 \core_message\api
::add_contact($users[1]->id
, $users[2]->id
);
2937 \core_message\api
::add_contact($users[3]->id
, $users[1]->id
);
2938 \core_message\api
::add_contact($users[1]->id
, $users[4]->id
);
2940 // Set as the user performing the search.
2941 $this->setUser($users[1]);
2943 // Grant the authenticated user role the capability 'user:viewdetails' at site context.
2944 $authenticatedrole = $DB->get_record('role', ['shortname' => 'user'], '*', MUST_EXIST
);
2945 assign_capability('moodle/user:viewdetails', CAP_ALLOW
, $authenticatedrole->id
, context_system
::instance());
2947 // Perform a search with $CFG->messagingallusers disabled.
2948 set_config('messagingallusers', 0);
2949 $result = core_message_external
::message_search_users($users[1]->id
, 'search');
2950 $result = external_api
::clean_returnvalue(core_message_external
::message_search_users_returns(), $result);
2951 $contacts = $result['contacts'];
2952 $noncontacts = $result['noncontacts'];
2954 // Check that we retrieved the correct contacts.
2955 $this->assertCount(2, $contacts);
2956 $this->assertEquals($users[2]->id
, $contacts[0]['id']);
2957 $this->assertEquals($users[3]->id
, $contacts[1]['id']);
2959 // Check that we retrieved the correct non-contacts.
2960 // Site-wide messaging is disabled, so we expect to be able to search for any users whose profile we can view.
2961 // Consider first conversations is self-conversation.
2962 $this->assertCount(3, $noncontacts);
2963 $this->assertEquals($users[1]->id
, $noncontacts[0]['id']);
2964 $this->assertEquals($users[6]->id
, $noncontacts[1]['id']);
2965 $this->assertEquals($users[7]->id
, $noncontacts[2]['id']);
2969 * Tests searching users as another user without the 'moodle/user:viewdetails' capability.
2971 public function test_message_search_users_without_cap() {
2972 $this->resetAfterTest();
2974 // Create some users.
2975 $user1 = $this->getDataGenerator()->create_user();
2976 $user2 = $this->getDataGenerator()->create_user();
2978 // The person doing the search for another user.
2979 $this->setUser($user1);
2981 // Ensure an exception is thrown.
2982 $this->expectException('moodle_exception');
2983 core_message_external
::message_search_users($user2->id
, 'User');
2984 $this->assertDebuggingCalled();
2988 * Tests searching users with messaging disabled.
2990 public function test_message_search_users_messaging_disabled() {
2991 $this->resetAfterTest();
2993 // Create some skeleton data just so we can call the WS.
2994 $user = $this->getDataGenerator()->create_user();
2996 // The person doing the search.
2997 $this->setUser($user);
2999 // Disable messaging.
3000 set_config('messaging', 0);
3002 // Ensure an exception is thrown.
3003 $this->expectException('moodle_exception');
3004 core_message_external
::message_search_users($user->id
, 'User');
3008 * Tests searching messages.
3010 public function test_messagearea_search_messages() {
3011 $this->resetAfterTest(true);
3013 // Create some users.
3014 $user1 = self
::getDataGenerator()->create_user();
3015 $user2 = self
::getDataGenerator()->create_user();
3017 // The person doing the search.
3018 $this->setUser($user1);
3020 // Send some messages back and forth.
3022 $this->send_message($user1, $user2, 'Yo!', 0, $time);
3023 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
3024 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
3025 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
3026 $convid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
3028 // Perform a search.
3029 $result = core_message_external
::data_for_messagearea_search_messages($user1->id
, 'o');
3031 // We need to execute the return values cleaning process to simulate the web service server.
3032 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_search_messages_returns(), $result);
3034 // Confirm the data is correct.
3035 $messages = $result['contacts'];
3036 $this->assertCount(2, $messages);
3038 $message1 = $messages[0];
3039 $message2 = $messages[1];
3041 $this->assertEquals($user2->id
, $message1['userid']);
3042 $this->assertEquals(fullname($user2), $message1['fullname']);
3043 $this->assertTrue($message1['ismessaging']);
3044 $this->assertFalse($message1['sentfromcurrentuser']);
3045 $this->assertEquals('Word.', $message1['lastmessage']);
3046 $this->assertNotEmpty($message1['messageid']);
3047 $this->assertNull($message1['isonline']);
3048 $this->assertFalse($message1['isread']);
3049 $this->assertFalse($message1['isblocked']);
3050 $this->assertNull($message1['unreadcount']);
3051 $this->assertEquals($convid, $message1['conversationid']);
3053 $this->assertEquals($user2->id
, $message2['userid']);
3054 $this->assertEquals(fullname($user2), $message2['fullname']);
3055 $this->assertTrue($message2['ismessaging']);
3056 $this->assertTrue($message2['sentfromcurrentuser']);
3057 $this->assertEquals('Yo!', $message2['lastmessage']);
3058 $this->assertNotEmpty($message2['messageid']);
3059 $this->assertNull($message2['isonline']);
3060 $this->assertTrue($message2['isread']);
3061 $this->assertFalse($message2['isblocked']);
3062 $this->assertNull($message2['unreadcount']);
3063 $this->assertEquals($convid, $message2['conversationid']);
3067 * Tests searching messages as another user.
3069 public function test_messagearea_search_messages_as_other_user() {
3070 $this->resetAfterTest(true);
3072 // The person doing the search.
3073 $this->setAdminUser();
3075 // Create some users.
3076 $user1 = self
::getDataGenerator()->create_user();
3077 $user2 = self
::getDataGenerator()->create_user();
3079 // Send some messages back and forth.
3081 $this->send_message($user1, $user2, 'Yo!', 0, $time);
3082 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
3083 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
3084 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
3086 // Perform a search.
3087 $result = core_message_external
::data_for_messagearea_search_messages($user1->id
, 'o');
3089 // We need to execute the return values cleaning process to simulate the web service server.
3090 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_search_messages_returns(),
3093 // Confirm the data is correct.
3094 $messages = $result['contacts'];
3095 $this->assertCount(2, $messages);
3097 $message1 = $messages[0];
3098 $message2 = $messages[1];
3100 $this->assertEquals($user2->id
, $message1['userid']);
3101 $this->assertEquals(fullname($user2), $message1['fullname']);
3102 $this->assertTrue($message1['ismessaging']);
3103 $this->assertFalse($message1['sentfromcurrentuser']);
3104 $this->assertEquals('Word.', $message1['lastmessage']);
3105 $this->assertNotEmpty($message1['messageid']);
3106 $this->assertFalse($message1['isonline']);
3107 $this->assertFalse($message1['isread']);
3108 $this->assertFalse($message1['isblocked']);
3109 $this->assertNull($message1['unreadcount']);
3111 $this->assertEquals($user2->id
, $message2['userid']);
3112 $this->assertEquals(fullname($user2), $message2['fullname']);
3113 $this->assertTrue($message2['ismessaging']);
3114 $this->assertTrue($message2['sentfromcurrentuser']);
3115 $this->assertEquals('Yo!', $message2['lastmessage']);
3116 $this->assertNotEmpty($message2['messageid']);
3117 $this->assertFalse($message2['isonline']);
3118 $this->assertTrue($message2['isread']);
3119 $this->assertFalse($message2['isblocked']);
3120 $this->assertNull($message2['unreadcount']);
3124 * Tests searching messages as another user without the proper capabilities.
3126 public function test_messagearea_search_messages_as_other_user_without_cap() {
3127 $this->resetAfterTest(true);
3129 // Create some users.
3130 $user1 = self
::getDataGenerator()->create_user();
3131 $user2 = self
::getDataGenerator()->create_user();
3133 // The person doing the search for another user.
3134 $this->setUser($user1);
3136 // Ensure an exception is thrown.
3137 $this->expectException('moodle_exception');
3138 core_message_external
::data_for_messagearea_search_messages($user2->id
, 'Search');
3142 * Tests searching messages with messaging disabled
3144 public function test_messagearea_search_messages_messaging_disabled() {
3147 $this->resetAfterTest(true);
3149 // Create some skeleton data just so we can call the WS.
3150 $user = self
::getDataGenerator()->create_user();
3152 // The person doing the search .
3153 $this->setUser($user);
3155 // Disable messaging.
3156 $CFG->messaging
= 0;
3158 // Ensure an exception is thrown.
3159 $this->expectException('moodle_exception');
3160 core_message_external
::data_for_messagearea_search_messages($user->id
, 'Search');
3164 * Tests retrieving conversations.
3166 public function test_messagearea_conversations() {
3167 $this->resetAfterTest(true);
3169 // Create some users.
3170 $user1 = self
::getDataGenerator()->create_user();
3171 $user2 = self
::getDataGenerator()->create_user();
3172 $user3 = self
::getDataGenerator()->create_user();
3173 $user4 = self
::getDataGenerator()->create_user();
3175 // The person retrieving the conversations.
3176 $this->setUser($user1);
3178 // Send some messages back and forth, have some different conversations with different users.
3180 $this->send_message($user1, $user2, 'Yo!', 0, $time);
3181 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
3182 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
3183 $messageid1 = $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
3185 $this->send_message($user1, $user3, 'Booyah', 0, $time +
4);
3186 $this->send_message($user3, $user1, 'Whaaat?', 0, $time +
5);
3187 $this->send_message($user1, $user3, 'Nothing.', 0, $time +
6);
3188 $messageid2 = $this->send_message($user3, $user1, 'Cool.', 0, $time +
7);
3190 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time +
8);
3191 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time +
9);
3192 $messageid3 = $this->send_message($user1, $user4, 'Dope.', 0, $time +
10);
3194 // Retrieve the conversations.
3195 $result = core_message_external
::data_for_messagearea_conversations($user1->id
);
3197 // We need to execute the return values cleaning process to simulate the web service server.
3198 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_conversations_returns(),
3201 // Confirm the data is correct.
3202 $messages = $result['contacts'];
3203 $this->assertCount(3, $messages);
3205 $message1 = $messages[0];
3206 $message2 = $messages[1];
3207 $message3 = $messages[2];
3209 $this->assertEquals($user4->id
, $message1['userid']);
3210 $this->assertTrue($message1['ismessaging']);
3211 $this->assertTrue($message1['sentfromcurrentuser']);
3212 $this->assertEquals('Dope.', $message1['lastmessage']);
3213 $this->assertEquals($messageid3, $message1['messageid']);
3214 $this->assertNull($message1['isonline']);
3215 $this->assertFalse($message1['isread']);
3216 $this->assertFalse($message1['isblocked']);
3217 $this->assertEquals(1, $message1['unreadcount']);
3219 $this->assertEquals($user3->id
, $message2['userid']);
3220 $this->assertTrue($message2['ismessaging']);
3221 $this->assertFalse($message2['sentfromcurrentuser']);
3222 $this->assertEquals('Cool.', $message2['lastmessage']);
3223 $this->assertEquals($messageid2, $message2['messageid']);
3224 $this->assertNull($message2['isonline']);
3225 $this->assertFalse($message2['isread']);
3226 $this->assertFalse($message2['isblocked']);
3227 $this->assertEquals(2, $message2['unreadcount']);
3229 $this->assertEquals($user2->id
, $message3['userid']);
3230 $this->assertTrue($message3['ismessaging']);
3231 $this->assertFalse($message3['sentfromcurrentuser']);
3232 $this->assertEquals('Word.', $message3['lastmessage']);
3233 $this->assertEquals($messageid1, $message3['messageid']);
3234 $this->assertNull($message3['isonline']);
3235 $this->assertFalse($message3['isread']);
3236 $this->assertFalse($message3['isblocked']);
3237 $this->assertEquals(2, $message3['unreadcount']);
3241 * Tests retrieving conversations as another user.
3243 public function test_messagearea_conversations_as_other_user() {
3244 $this->resetAfterTest(true);
3247 $this->setAdminUser();
3249 // Create some users.
3250 $user1 = self
::getDataGenerator()->create_user();
3251 $user2 = self
::getDataGenerator()->create_user();
3252 $user3 = self
::getDataGenerator()->create_user();
3253 $user4 = self
::getDataGenerator()->create_user();
3255 // Send some messages back and forth, have some different conversations with different users.
3257 $this->send_message($user1, $user2, 'Yo!', 0, $time);
3258 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
3259 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
3260 $messageid1 = $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
3262 $this->send_message($user1, $user3, 'Booyah', 0, $time +
4);
3263 $this->send_message($user3, $user1, 'Whaaat?', 0, $time +
5);
3264 $this->send_message($user1, $user3, 'Nothing.', 0, $time +
6);
3265 $messageid2 = $this->send_message($user3, $user1, 'Cool.', 0, $time +
7);
3267 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time +
8);
3268 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time +
9);
3269 $messageid3 = $this->send_message($user1, $user4, 'Dope.', 0, $time +
10);
3271 // Retrieve the conversations.
3272 $result = core_message_external
::data_for_messagearea_conversations($user1->id
);
3274 // We need to execute the return values cleaning process to simulate the web service server.
3275 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_conversations_returns(),
3278 // Confirm the data is correct.
3279 $messages = $result['contacts'];
3280 $this->assertCount(3, $messages);
3282 $message1 = $messages[0];
3283 $message2 = $messages[1];
3284 $message3 = $messages[2];
3286 $this->assertEquals($user4->id
, $message1['userid']);
3287 $this->assertTrue($message1['ismessaging']);
3288 $this->assertTrue($message1['sentfromcurrentuser']);
3289 $this->assertEquals('Dope.', $message1['lastmessage']);
3290 $this->assertEquals($messageid3, $message1['messageid']);
3291 $this->assertFalse($message1['isonline']);
3292 $this->assertFalse($message1['isread']);
3293 $this->assertFalse($message1['isblocked']);
3294 $this->assertEquals(1, $message1['unreadcount']);
3296 $this->assertEquals($user3->id
, $message2['userid']);
3297 $this->assertTrue($message2['ismessaging']);
3298 $this->assertFalse($message2['sentfromcurrentuser']);
3299 $this->assertEquals('Cool.', $message2['lastmessage']);
3300 $this->assertEquals($messageid2, $message2['messageid']);
3301 $this->assertFalse($message2['isonline']);
3302 $this->assertFalse($message2['isread']);
3303 $this->assertFalse($message2['isblocked']);
3304 $this->assertEquals(2, $message2['unreadcount']);
3306 $this->assertEquals($user2->id
, $message3['userid']);
3307 $this->assertTrue($message3['ismessaging']);
3308 $this->assertFalse($message3['sentfromcurrentuser']);
3309 $this->assertEquals('Word.', $message3['lastmessage']);
3310 $this->assertEquals($messageid1, $message3['messageid']);
3311 $this->assertFalse($message3['isonline']);
3312 $this->assertFalse($message3['isread']);
3313 $this->assertFalse($message3['isblocked']);
3314 $this->assertEquals(2, $message3['unreadcount']);
3318 * Tests retrieving conversations as another user without the proper capabilities.
3320 public function test_messagearea_conversations_as_other_user_without_cap() {
3321 $this->resetAfterTest(true);
3323 // Create some users.
3324 $user1 = self
::getDataGenerator()->create_user();
3325 $user2 = self
::getDataGenerator()->create_user();
3327 // The person retrieving the conversations for another user.
3328 $this->setUser($user1);
3330 // Ensure an exception is thrown.
3331 $this->expectException('moodle_exception');
3332 core_message_external
::data_for_messagearea_conversations($user2->id
);
3336 * Tests retrieving conversations with messaging disabled.
3338 public function test_messagearea_conversations_messaging_disabled() {
3341 $this->resetAfterTest(true);
3343 // Create some skeleton data just so we can call the WS.
3344 $user = self
::getDataGenerator()->create_user();
3346 // The person retrieving the conversations.
3347 $this->setUser($user);
3349 // Disable messaging.
3350 $CFG->messaging
= 0;
3352 // Ensure an exception is thrown.
3353 $this->expectException('moodle_exception');
3354 core_message_external
::data_for_messagearea_conversations($user->id
);
3358 * Tests retrieving contacts.
3360 public function test_messagearea_contacts() {
3361 $this->resetAfterTest(true);
3363 // Create some users.
3364 $user1 = self
::getDataGenerator()->create_user();
3367 $this->setUser($user1);
3369 $user2 = new stdClass();
3370 $user2->firstname
= 'User';
3371 $user2->lastname
= 'A';
3372 $user2 = self
::getDataGenerator()->create_user($user2);
3374 $user3 = new stdClass();
3375 $user3->firstname
= 'User';
3376 $user3->lastname
= 'B';
3377 $user3 = self
::getDataGenerator()->create_user($user3);
3379 $user4 = new stdClass();
3380 $user4->firstname
= 'User';
3381 $user4->lastname
= 'C';
3382 $user4 = self
::getDataGenerator()->create_user($user4);
3384 $user5 = new stdClass();
3385 $user5->firstname
= 'User';
3386 $user5->lastname
= 'D';
3387 $user5 = self
::getDataGenerator()->create_user($user5);
3389 // Add some users as contacts.
3390 \core_message\api
::add_contact($user1->id
, $user2->id
);
3391 \core_message\api
::add_contact($user1->id
, $user3->id
);
3392 \core_message\api
::add_contact($user1->id
, $user4->id
);
3394 // Retrieve the contacts.
3395 $result = core_message_external
::data_for_messagearea_contacts($user1->id
);
3397 // We need to execute the return values cleaning process to simulate the web service server.
3398 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_contacts_returns(),
3401 // Confirm the data is correct.
3402 $contacts = $result['contacts'];
3403 usort($contacts, ['static', 'sort_contacts']);
3404 $this->assertCount(3, $contacts);
3406 $contact1 = $contacts[0];
3407 $contact2 = $contacts[1];
3408 $contact3 = $contacts[2];
3410 $this->assertEquals($user2->id
, $contact1['userid']);
3411 $this->assertFalse($contact1['ismessaging']);
3412 $this->assertFalse($contact1['sentfromcurrentuser']);
3413 $this->assertNull($contact1['lastmessage']);
3414 $this->assertNull($contact1['messageid']);
3415 $this->assertNull($contact1['isonline']);
3416 $this->assertFalse($contact1['isread']);
3417 $this->assertFalse($contact1['isblocked']);
3418 $this->assertNull($contact1['unreadcount']);
3420 $this->assertEquals($user3->id
, $contact2['userid']);
3421 $this->assertFalse($contact2['ismessaging']);
3422 $this->assertFalse($contact2['sentfromcurrentuser']);
3423 $this->assertNull($contact2['lastmessage']);
3424 $this->assertNull($contact2['messageid']);
3425 $this->assertNull($contact2['isonline']);
3426 $this->assertFalse($contact2['isread']);
3427 $this->assertFalse($contact2['isblocked']);
3428 $this->assertNull($contact2['unreadcount']);
3430 $this->assertEquals($user4->id
, $contact3['userid']);
3431 $this->assertFalse($contact3['ismessaging']);
3432 $this->assertFalse($contact3['sentfromcurrentuser']);
3433 $this->assertNull($contact3['lastmessage']);
3434 $this->assertNull($contact3['messageid']);
3435 $this->assertNull($contact3['isonline']);
3436 $this->assertFalse($contact3['isread']);
3437 $this->assertFalse($contact3['isblocked']);
3438 $this->assertNull($contact3['unreadcount']);
3442 * Tests retrieving contacts as another user.
3444 public function test_messagearea_contacts_as_other_user() {
3445 $this->resetAfterTest(true);
3447 $this->setAdminUser();
3449 // Create some users.
3450 $user1 = self
::getDataGenerator()->create_user();
3452 $user2 = new stdClass();
3453 $user2->firstname
= 'User';
3454 $user2->lastname
= 'A';
3455 $user2 = self
::getDataGenerator()->create_user($user2);
3457 $user3 = new stdClass();
3458 $user3->firstname
= 'User';
3459 $user3->lastname
= 'B';
3460 $user3 = self
::getDataGenerator()->create_user($user3);
3462 $user4 = new stdClass();
3463 $user4->firstname
= 'User';
3464 $user4->lastname
= 'C';
3465 $user4 = self
::getDataGenerator()->create_user($user4);
3467 $user5 = new stdClass();
3468 $user5->firstname
= 'User';
3469 $user5->lastname
= 'D';
3470 $user5 = self
::getDataGenerator()->create_user($user5);
3472 // Add some users as contacts.
3473 \core_message\api
::add_contact($user1->id
, $user2->id
);
3474 \core_message\api
::add_contact($user1->id
, $user3->id
);
3475 \core_message\api
::add_contact($user1->id
, $user4->id
);
3477 // Retrieve the contacts.
3478 $result = core_message_external
::data_for_messagearea_contacts($user1->id
);
3480 // We need to execute the return values cleaning process to simulate the web service server.
3481 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_contacts_returns(),
3484 // Confirm the data is correct.
3485 $contacts = $result['contacts'];
3486 usort($contacts, ['static', 'sort_contacts']);
3487 $this->assertCount(3, $contacts);
3489 $contact1 = $contacts[0];
3490 $contact2 = $contacts[1];
3491 $contact3 = $contacts[2];
3493 $this->assertEquals($user2->id
, $contact1['userid']);
3494 $this->assertFalse($contact1['ismessaging']);
3495 $this->assertFalse($contact1['sentfromcurrentuser']);
3496 $this->assertNull($contact1['lastmessage']);
3497 $this->assertNull($contact1['messageid']);
3498 $this->assertFalse($contact1['isonline']);
3499 $this->assertFalse($contact1['isread']);
3500 $this->assertFalse($contact1['isblocked']);
3501 $this->assertNull($contact1['unreadcount']);
3503 $this->assertEquals($user3->id
, $contact2['userid']);
3504 $this->assertFalse($contact2['ismessaging']);
3505 $this->assertFalse($contact2['sentfromcurrentuser']);
3506 $this->assertNull($contact2['lastmessage']);
3507 $this->assertNull($contact2['messageid']);
3508 $this->assertFalse($contact2['isonline']);
3509 $this->assertFalse($contact2['isread']);
3510 $this->assertFalse($contact2['isblocked']);
3511 $this->assertNull($contact2['unreadcount']);
3513 $this->assertEquals($user4->id
, $contact3['userid']);
3514 $this->assertFalse($contact3['ismessaging']);
3515 $this->assertFalse($contact3['sentfromcurrentuser']);
3516 $this->assertNull($contact3['lastmessage']);
3517 $this->assertNull($contact3['messageid']);
3518 $this->assertFalse($contact3['isonline']);
3519 $this->assertFalse($contact3['isread']);
3520 $this->assertFalse($contact3['isblocked']);
3521 $this->assertNull($contact3['unreadcount']);
3525 * Tests retrieving contacts as another user without the proper capabilities.
3527 public function test_messagearea_contacts_as_other_user_without_cap() {
3528 $this->resetAfterTest(true);
3530 // Create some users.
3531 $user1 = self
::getDataGenerator()->create_user();
3532 $user2 = self
::getDataGenerator()->create_user();
3534 // The person retrieving the contacts for another user.
3535 $this->setUser($user1);
3537 // Perform the WS call and ensure an exception is thrown.
3538 $this->expectException('moodle_exception');
3539 core_message_external
::data_for_messagearea_contacts($user2->id
);
3543 * Tests retrieving contacts with messaging disabled.
3545 public function test_messagearea_contacts_messaging_disabled() {
3548 $this->resetAfterTest(true);
3550 // Create some skeleton data just so we can call the WS.
3551 $user = self
::getDataGenerator()->create_user();
3553 // The person retrieving the contacts.
3554 $this->setUser($user);
3556 // Disable messaging.
3557 $CFG->messaging
= 0;
3559 // Perform the WS call and ensure we are shown that it is disabled.
3560 $this->expectException('moodle_exception');
3561 core_message_external
::data_for_messagearea_contacts($user->id
);
3565 * Tests retrieving contacts.
3567 public function test_get_user_contacts() {
3568 $this->resetAfterTest(true);
3570 // Create some users.
3571 $user1 = self
::getDataGenerator()->create_user();
3574 $this->setUser($user1);
3576 $user2 = new stdClass();
3577 $user2->firstname
= 'User';
3578 $user2->lastname
= 'A';
3579 $user2 = self
::getDataGenerator()->create_user($user2);
3581 $user3 = new stdClass();
3582 $user3->firstname
= 'User';
3583 $user3->lastname
= 'B';
3584 $user3 = self
::getDataGenerator()->create_user($user3);
3586 $user4 = new stdClass();
3587 $user4->firstname
= 'User';
3588 $user4->lastname
= 'C';
3589 $user4 = self
::getDataGenerator()->create_user($user4);
3591 $user5 = new stdClass();
3592 $user5->firstname
= 'User';
3593 $user5->lastname
= 'D';
3594 $user5 = self
::getDataGenerator()->create_user($user5);
3596 // Add some users as contacts.
3597 \core_message\api
::add_contact($user1->id
, $user2->id
);
3598 \core_message\api
::add_contact($user1->id
, $user3->id
);
3599 \core_message\api
::add_contact($user1->id
, $user4->id
);
3601 // Retrieve the contacts.
3602 $result = core_message_external
::get_user_contacts($user1->id
);
3604 // We need to execute the return values cleaning process to simulate the web service server.
3605 $result = external_api
::clean_returnvalue(core_message_external
::get_user_contacts_returns(),
3608 // Confirm the data is correct.
3609 $contacts = $result;
3610 usort($contacts, ['static', 'sort_contacts_id']);
3611 $this->assertCount(3, $contacts);
3613 $contact1 = array_shift($contacts);
3614 $contact2 = array_shift($contacts);
3615 $contact3 = array_shift($contacts);
3617 $this->assertEquals($user2->id
, $contact1['id']);
3618 $this->assertEquals(fullname($user2), $contact1['fullname']);
3619 $this->assertTrue($contact1['iscontact']);
3621 $this->assertEquals($user3->id
, $contact2['id']);
3622 $this->assertEquals(fullname($user3), $contact2['fullname']);
3623 $this->assertTrue($contact2['iscontact']);
3625 $this->assertEquals($user4->id
, $contact3['id']);
3626 $this->assertEquals(fullname($user4), $contact3['fullname']);
3627 $this->assertTrue($contact3['iscontact']);
3631 * Tests retrieving contacts as another user.
3633 public function test_get_user_contacts_as_other_user() {
3634 $this->resetAfterTest(true);
3636 $this->setAdminUser();
3638 // Create some users.
3639 $user1 = self
::getDataGenerator()->create_user();
3641 $user2 = new stdClass();
3642 $user2->firstname
= 'User';
3643 $user2->lastname
= 'A';
3644 $user2 = self
::getDataGenerator()->create_user($user2);
3646 $user3 = new stdClass();
3647 $user3->firstname
= 'User';
3648 $user3->lastname
= 'B';
3649 $user3 = self
::getDataGenerator()->create_user($user3);
3651 $user4 = new stdClass();
3652 $user4->firstname
= 'User';
3653 $user4->lastname
= 'C';
3654 $user4 = self
::getDataGenerator()->create_user($user4);
3656 $user5 = new stdClass();
3657 $user5->firstname
= 'User';
3658 $user5->lastname
= 'D';
3659 $user5 = self
::getDataGenerator()->create_user($user5);
3661 // Add some users as contacts.
3662 \core_message\api
::add_contact($user1->id
, $user2->id
);
3663 \core_message\api
::add_contact($user1->id
, $user3->id
);
3664 \core_message\api
::add_contact($user1->id
, $user4->id
);
3666 // Retrieve the contacts.
3667 $result = core_message_external
::get_user_contacts($user1->id
);
3669 // We need to execute the return values cleaning process to simulate the web service server.
3670 $result = external_api
::clean_returnvalue(core_message_external
::get_user_contacts_returns(),
3673 // Confirm the data is correct.
3674 $contacts = $result;
3675 usort($contacts, ['static', 'sort_contacts_id']);
3676 $this->assertCount(3, $contacts);
3678 $contact1 = array_shift($contacts);
3679 $contact2 = array_shift($contacts);
3680 $contact3 = array_shift($contacts);
3682 $this->assertEquals($user2->id
, $contact1['id']);
3683 $this->assertEquals(fullname($user2), $contact1['fullname']);
3684 $this->assertTrue($contact1['iscontact']);
3686 $this->assertEquals($user3->id
, $contact2['id']);
3687 $this->assertEquals(fullname($user3), $contact2['fullname']);
3688 $this->assertTrue($contact2['iscontact']);
3690 $this->assertEquals($user4->id
, $contact3['id']);
3691 $this->assertEquals(fullname($user4), $contact3['fullname']);
3692 $this->assertTrue($contact3['iscontact']);
3696 * Tests retrieving contacts as another user without the proper capabilities.
3698 public function test_get_user_contacts_as_other_user_without_cap() {
3699 $this->resetAfterTest(true);
3701 // Create some users.
3702 $user1 = self
::getDataGenerator()->create_user();
3703 $user2 = self
::getDataGenerator()->create_user();
3705 // The person retrieving the contacts for another user.
3706 $this->setUser($user1);
3708 // Perform the WS call and ensure an exception is thrown.
3709 $this->expectException('moodle_exception');
3710 core_message_external
::get_user_contacts($user2->id
);
3714 * Tests retrieving contacts with messaging disabled.
3716 public function test_get_user_contacts_messaging_disabled() {
3719 $this->resetAfterTest(true);
3721 // Create some skeleton data just so we can call the WS.
3722 $user = self
::getDataGenerator()->create_user();
3724 // The person retrieving the contacts.
3725 $this->setUser($user);
3727 // Disable messaging.
3728 $CFG->messaging
= 0;
3730 // Perform the WS call and ensure we are shown that it is disabled.
3731 $this->expectException('moodle_exception');
3732 core_message_external
::get_user_contacts($user->id
);
3736 * Test getting contacts when there are no results.
3738 public function test_get_user_contacts_no_results() {
3739 $this->resetAfterTest();
3741 $user1 = self
::getDataGenerator()->create_user();
3743 $this->setUser($user1);
3745 $requests = core_message_external
::get_user_contacts($user1->id
);
3746 $requests = external_api
::clean_returnvalue(core_message_external
::get_user_contacts_returns(), $requests);
3748 $this->assertEmpty($requests);
3752 * Tests retrieving messages.
3754 public function test_messagearea_messages() {
3755 $this->resetAfterTest(true);
3757 // Create some users.
3758 $user1 = self
::getDataGenerator()->create_user();
3759 $user2 = self
::getDataGenerator()->create_user();
3761 // The person asking for the messages.
3762 $this->setUser($user1);
3764 // Send some messages back and forth.
3766 $this->send_message($user1, $user2, 'Yo!', 0, $time);
3767 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
3768 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
3769 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
3771 // Retrieve the messages.
3772 $result = core_message_external
::data_for_messagearea_messages($user1->id
, $user2->id
);
3774 // We need to execute the return values cleaning process to simulate the web service server.
3775 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_messages_returns(),
3778 // Check the results are correct.
3779 $this->assertTrue($result['iscurrentuser']);
3780 $this->assertEquals($user1->id
, $result['currentuserid']);
3781 $this->assertEquals($user2->id
, $result['otheruserid']);
3782 $this->assertEquals(fullname($user2), $result['otheruserfullname']);
3783 $this->assertNull($result['isonline']);
3785 // Confirm the message data is correct.
3786 $messages = $result['messages'];
3787 $this->assertCount(4, $messages);
3789 $message1 = $messages[0];
3790 $message2 = $messages[1];
3791 $message3 = $messages[2];
3792 $message4 = $messages[3];
3794 $this->assertEquals($user1->id
, $message1['useridfrom']);
3795 $this->assertEquals($user2->id
, $message1['useridto']);
3796 $this->assertTrue($message1['displayblocktime']);
3797 $this->assertContains('Yo!', $message1['text']);
3799 $this->assertEquals($user2->id
, $message2['useridfrom']);
3800 $this->assertEquals($user1->id
, $message2['useridto']);
3801 $this->assertFalse($message2['displayblocktime']);
3802 $this->assertContains('Sup mang?', $message2['text']);
3804 $this->assertEquals($user1->id
, $message3['useridfrom']);
3805 $this->assertEquals($user2->id
, $message3['useridto']);
3806 $this->assertFalse($message3['displayblocktime']);
3807 $this->assertContains('Writing PHPUnit tests!', $message3['text']);
3809 $this->assertEquals($user2->id
, $message4['useridfrom']);
3810 $this->assertEquals($user1->id
, $message4['useridto']);
3811 $this->assertFalse($message4['displayblocktime']);
3812 $this->assertContains('Word.', $message4['text']);
3816 * Tests retrieving messages.
3818 public function test_messagearea_messages_timefrom() {
3819 $this->resetAfterTest(true);
3821 // Create some users.
3822 $user1 = self
::getDataGenerator()->create_user();
3823 $user2 = self
::getDataGenerator()->create_user();
3825 // The person asking for the messages.
3826 $this->setUser($user1);
3828 // Send some messages back and forth.
3830 $this->send_message($user1, $user2, 'Message 1', 0, $time - 4);
3831 $this->send_message($user2, $user1, 'Message 2', 0, $time - 3);
3832 $this->send_message($user1, $user2, 'Message 3', 0, $time - 2);
3833 $this->send_message($user2, $user1, 'Message 4', 0, $time - 1);
3835 // Retrieve the messages from $time - 3, which should be the 3 most recent messages.
3836 $result = core_message_external
::data_for_messagearea_messages($user1->id
, $user2->id
, 0, 0, false, $time - 3);
3838 // We need to execute the return values cleaning process to simulate the web service server.
3839 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_messages_returns(),
3842 // Confirm the message data is correct. We shouldn't get 'Message 1' back.
3843 $messages = $result['messages'];
3844 $this->assertCount(3, $messages);
3846 $message1 = $messages[0];
3847 $message2 = $messages[1];
3848 $message3 = $messages[2];
3850 $this->assertContains('Message 2', $message1['text']);
3851 $this->assertContains('Message 3', $message2['text']);
3852 $this->assertContains('Message 4', $message3['text']);
3856 * Tests retrieving messages as another user.
3858 public function test_messagearea_messages_as_other_user() {
3859 $this->resetAfterTest(true);
3862 $this->setAdminUser();
3864 // Create some users.
3865 $user1 = self
::getDataGenerator()->create_user();
3866 $user2 = self
::getDataGenerator()->create_user();
3868 // Send some messages back and forth.
3870 $this->send_message($user1, $user2, 'Yo!', 0, $time);
3871 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
3872 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
3873 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
3875 // Retrieve the messages.
3876 $result = core_message_external
::data_for_messagearea_messages($user1->id
, $user2->id
);
3878 // We need to execute the return values cleaning process to simulate the web service server.
3879 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_messages_returns(),
3882 // Check the results are correct.
3883 $this->assertFalse($result['iscurrentuser']);
3884 $this->assertEquals($user1->id
, $result['currentuserid']);
3885 $this->assertEquals($user2->id
, $result['otheruserid']);
3886 $this->assertEquals(fullname($user2), $result['otheruserfullname']);
3887 $this->assertFalse($result['isonline']);
3889 // Confirm the message data is correct.
3890 $messages = $result['messages'];
3891 $this->assertCount(4, $messages);
3893 $message1 = $messages[0];
3894 $message2 = $messages[1];
3895 $message3 = $messages[2];
3896 $message4 = $messages[3];
3898 $this->assertEquals($user1->id
, $message1['useridfrom']);
3899 $this->assertEquals($user2->id
, $message1['useridto']);
3900 $this->assertTrue($message1['displayblocktime']);
3901 $this->assertContains('Yo!', $message1['text']);
3903 $this->assertEquals($user2->id
, $message2['useridfrom']);
3904 $this->assertEquals($user1->id
, $message2['useridto']);
3905 $this->assertFalse($message2['displayblocktime']);
3906 $this->assertContains('Sup mang?', $message2['text']);
3908 $this->assertEquals($user1->id
, $message3['useridfrom']);
3909 $this->assertEquals($user2->id
, $message3['useridto']);
3910 $this->assertFalse($message3['displayblocktime']);
3911 $this->assertContains('Writing PHPUnit tests!', $message3['text']);
3913 $this->assertEquals($user2->id
, $message4['useridfrom']);
3914 $this->assertEquals($user1->id
, $message4['useridto']);
3915 $this->assertFalse($message4['displayblocktime']);
3916 $this->assertContains('Word.', $message4['text']);
3920 * Tests retrieving messages as another user without the proper capabilities.
3922 public function test_messagearea_messages_as_other_user_without_cap() {
3923 $this->resetAfterTest(true);
3925 // Create some users.
3926 $user1 = self
::getDataGenerator()->create_user();
3927 $user2 = self
::getDataGenerator()->create_user();
3928 $user3 = self
::getDataGenerator()->create_user();
3930 // The person asking for the messages for another user.
3931 $this->setUser($user1);
3933 // Ensure an exception is thrown.
3934 $this->expectException('moodle_exception');
3935 core_message_external
::data_for_messagearea_messages($user2->id
, $user3->id
);
3939 * Tests retrieving messages with messaging disabled.
3941 public function test_messagearea_messages_messaging_disabled() {
3944 $this->resetAfterTest(true);
3946 // Create some skeleton data just so we can call the WS.
3947 $user1 = self
::getDataGenerator()->create_user();
3948 $user2 = self
::getDataGenerator()->create_user();
3950 // The person asking for the messages for another user.
3951 $this->setUser($user1);
3953 // Disable messaging.
3954 $CFG->messaging
= 0;
3956 // Ensure an exception is thrown.
3957 $this->expectException('moodle_exception');
3958 core_message_external
::data_for_messagearea_messages($user1->id
, $user2->id
);
3962 * Tests get_conversation_messages for retrieving messages.
3964 public function test_get_conversation_messages() {
3965 $this->resetAfterTest(true);
3967 // Create some users.
3968 $user1 = self
::getDataGenerator()->create_user();
3969 $user2 = self
::getDataGenerator()->create_user();
3970 $user3 = self
::getDataGenerator()->create_user();
3971 $user4 = self
::getDataGenerator()->create_user();
3972 $user5 = self
::getDataGenerator()->create_user();
3974 // Create group conversation.
3975 $conversation = \core_message\api
::create_conversation(
3976 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
3977 [$user1->id
, $user2->id
, $user3->id
, $user4->id
]
3980 // The person asking for the messages.
3981 $this->setUser($user1);
3983 // Send some messages back and forth.
3985 testhelper
::send_fake_message_to_conversation($user1, $conversation->id
, 'Yo!', $time);
3986 testhelper
::send_fake_message_to_conversation($user3, $conversation->id
, 'Sup mang?', $time +
1);
3987 testhelper
::send_fake_message_to_conversation($user2, $conversation->id
, 'Writing PHPUnit tests!', $time +
2);
3988 testhelper
::send_fake_message_to_conversation($user1, $conversation->id
, 'Word.', $time +
3);
3990 // Retrieve the messages.
3991 $result = core_message_external
::get_conversation_messages($user1->id
, $conversation->id
);
3993 // We need to execute the return values cleaning process to simulate the web service server.
3994 $result = external_api
::clean_returnvalue(core_message_external
::get_conversation_messages_returns(),
3997 // Check the results are correct.
3998 $this->assertEquals($conversation->id
, $result['id']);
4000 // Confirm the members data is correct.
4001 $members = $result['members'];
4002 $this->assertCount(3, $members);
4003 $membersid = [$members[0]['id'], $members[1]['id'], $members[2]['id']];
4004 $this->assertContains($user1->id
, $membersid);
4005 $this->assertContains($user2->id
, $membersid);
4006 $this->assertContains($user3->id
, $membersid);
4008 $membersfullnames = [$members[0]['fullname'], $members[1]['fullname'], $members[2]['fullname']];
4009 $this->assertContains(fullname($user1), $membersfullnames);
4010 $this->assertContains(fullname($user2), $membersfullnames);
4011 $this->assertContains(fullname($user3), $membersfullnames);
4013 // Confirm the messages data is correct.
4014 $messages = $result['messages'];
4015 $this->assertCount(4, $messages);
4017 $message1 = $messages[0];
4018 $message2 = $messages[1];
4019 $message3 = $messages[2];
4020 $message4 = $messages[3];
4022 $this->assertEquals($user1->id
, $message1['useridfrom']);
4023 $this->assertContains('Yo!', $message1['text']);
4025 $this->assertEquals($user3->id
, $message2['useridfrom']);
4026 $this->assertContains('Sup mang?', $message2['text']);
4028 $this->assertEquals($user2->id
, $message3['useridfrom']);
4029 $this->assertContains('Writing PHPUnit tests!', $message3['text']);
4031 $this->assertEquals($user1->id
, $message4['useridfrom']);
4032 $this->assertContains('Word.', $message4['text']);
4036 * Tests get_conversation_messages for retrieving messages using timefrom parameter.
4038 public function test_get_conversation_messages_timefrom() {
4039 $this->resetAfterTest(true);
4041 // Create some users.
4042 $user1 = self
::getDataGenerator()->create_user();
4043 $user2 = self
::getDataGenerator()->create_user();
4044 $user3 = self
::getDataGenerator()->create_user();
4045 $user4 = self
::getDataGenerator()->create_user();
4047 // Create group conversation.
4048 $conversation = \core_message\api
::create_conversation(
4049 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
4050 [$user1->id
, $user2->id
, $user3->id
]
4053 // The person asking for the messages.
4054 $this->setUser($user1);
4056 // Send some messages back and forth.
4058 testhelper
::send_fake_message_to_conversation($user1, $conversation->id
, 'Message 1', $time - 4);
4059 testhelper
::send_fake_message_to_conversation($user2, $conversation->id
, 'Message 2', $time - 3);
4060 testhelper
::send_fake_message_to_conversation($user2, $conversation->id
, 'Message 3', $time - 2);
4061 testhelper
::send_fake_message_to_conversation($user2, $conversation->id
, 'Message 4', $time - 1);
4063 // Retrieve the messages from $time - 3, which should be the 3 most recent messages.
4064 $result = core_message_external
::get_conversation_messages($user1->id
, $conversation->id
, 0, 0, false, $time - 3);
4066 // We need to execute the return values cleaning process to simulate the web service server.
4067 $result = external_api
::clean_returnvalue(core_message_external
::get_conversation_messages_returns(),
4070 // Check the results are correct.
4071 $this->assertEquals($conversation->id
, $result['id']);
4073 // Confirm the messages data is correct.
4074 $messages = $result['messages'];
4075 $this->assertCount(3, $messages);
4077 $message1 = $messages[0];
4078 $message2 = $messages[1];
4079 $message3 = $messages[2];
4081 $this->assertContains('Message 2', $message1['text']);
4082 $this->assertContains('Message 3', $message2['text']);
4083 $this->assertContains('Message 4', $message3['text']);
4085 // Confirm the members data is correct.
4086 $members = $result['members'];
4087 $this->assertCount(1, $members);
4088 $this->assertEquals($user2->id
, $members[0]['id']);
4092 * Tests get_conversation_messages for retrieving messages as another user.
4094 public function test_get_conversation_messages_as_other_user() {
4095 $this->resetAfterTest(true);
4098 $this->setAdminUser();
4100 // Create some users.
4101 $user1 = self
::getDataGenerator()->create_user();
4102 $user2 = self
::getDataGenerator()->create_user();
4103 $user3 = self
::getDataGenerator()->create_user();
4104 $user4 = self
::getDataGenerator()->create_user();
4106 // Create group conversation.
4107 $conversation = \core_message\api
::create_conversation(
4108 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
4109 [$user1->id
, $user2->id
, $user3->id
, $user4->id
]
4112 // Send some messages back and forth.
4114 testhelper
::send_fake_message_to_conversation($user1, $conversation->id
, 'Yo!', $time);
4115 testhelper
::send_fake_message_to_conversation($user3, $conversation->id
, 'Sup mang?', $time +
1);
4116 testhelper
::send_fake_message_to_conversation($user2, $conversation->id
, 'Writing PHPUnit tests!', $time +
2);
4117 testhelper
::send_fake_message_to_conversation($user1, $conversation->id
, 'Word.', $time +
3);
4119 // Retrieve the messages.
4120 $result = core_message_external
::get_conversation_messages($user1->id
, $conversation->id
);
4122 // We need to execute the return values cleaning process to simulate the web service server.
4123 $result = external_api
::clean_returnvalue(core_message_external
::get_conversation_messages_returns(),
4126 // Check the results are correct.
4127 $this->assertEquals($conversation->id
, $result['id']);
4129 // Confirm the members data is correct.
4130 $members = $result['members'];
4131 $this->assertCount(3, $members);
4132 $membersid = [$members[0]['id'], $members[1]['id'], $members[2]['id']];
4133 $this->assertContains($user1->id
, $membersid);
4134 $this->assertContains($user2->id
, $membersid);
4135 $this->assertContains($user3->id
, $membersid);
4137 // Confirm the message data is correct.
4138 $messages = $result['messages'];
4139 $this->assertCount(4, $messages);
4141 $message1 = $messages[0];
4142 $message2 = $messages[1];
4143 $message3 = $messages[2];
4144 $message4 = $messages[3];
4146 $this->assertEquals($user1->id
, $message1['useridfrom']);
4147 $this->assertContains('Yo!', $message1['text']);
4149 $this->assertEquals($user3->id
, $message2['useridfrom']);
4150 $this->assertContains('Sup mang?', $message2['text']);
4152 $this->assertEquals($user2->id
, $message3['useridfrom']);
4153 $this->assertContains('Writing PHPUnit tests!', $message3['text']);
4155 $this->assertEquals($user1->id
, $message4['useridfrom']);
4156 $this->assertContains('Word.', $message4['text']);
4160 * Tests get_conversation_messages for retrieving messages as another user without the proper capabilities.
4162 public function test_get_conversation_messages_as_other_user_without_cap() {
4163 $this->resetAfterTest(true);
4165 // Create some users.
4166 $user1 = self
::getDataGenerator()->create_user();
4167 $user2 = self
::getDataGenerator()->create_user();
4168 $user3 = self
::getDataGenerator()->create_user();
4169 $user4 = self
::getDataGenerator()->create_user();
4171 // Create group conversation.
4172 $conversation = \core_message\api
::create_conversation(
4173 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
4174 [$user1->id
, $user2->id
, $user3->id
, $user4->id
]
4177 // The person asking for the messages for another user.
4178 $this->setUser($user1);
4180 // Ensure an exception is thrown.
4181 $this->expectException('moodle_exception');
4182 core_message_external
::get_conversation_messages($user2->id
, $conversation->id
);
4186 * Tests get_conversation_messages for retrieving messages as another user not in the conversation.
4188 public function test_get_conversation_messages_as_user_not_in_conversation() {
4189 $this->resetAfterTest(true);
4191 // Create some users.
4192 $user1 = self
::getDataGenerator()->create_user();
4193 $user2 = self
::getDataGenerator()->create_user();
4194 $user3 = self
::getDataGenerator()->create_user(); // Not in group.
4196 // Create group conversation.
4197 $conversation = \core_message\api
::create_conversation(
4198 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
4199 [$user1->id
, $user2->id
]
4202 // The person asking for the messages for a conversation he does not belong to.
4203 $this->setUser($user3);
4205 // Ensure an exception is thrown.
4206 $this->expectExceptionMessage('User is not part of conversation.');
4207 core_message_external
::get_conversation_messages($user3->id
, $conversation->id
);
4211 * Tests get_conversation_messages for retrieving messages with messaging disabled.
4213 public function test_get_conversation_messages_messaging_disabled() {
4214 $this->resetAfterTest(true);
4216 // Create some skeleton data just so we can call the WS.
4217 $user1 = self
::getDataGenerator()->create_user();
4218 $user2 = self
::getDataGenerator()->create_user();
4219 $user3 = self
::getDataGenerator()->create_user();
4220 $user4 = self
::getDataGenerator()->create_user();
4222 // Create group conversation.
4223 $conversation = \core_message\api
::create_conversation(
4224 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
4225 [$user1->id
, $user2->id
, $user3->id
, $user4->id
]
4228 // The person asking for the messages for another user.
4229 $this->setUser($user1);
4231 // Disable messaging.
4232 set_config('messaging', 0);
4234 // Ensure an exception is thrown.
4235 $this->expectException('moodle_exception');
4236 core_message_external
::get_conversation_messages($user1->id
, $conversation->id
);
4240 * Tests retrieving most recent message.
4242 public function test_messagearea_get_most_recent_message() {
4243 $this->resetAfterTest(true);
4245 // Create some users.
4246 $user1 = self
::getDataGenerator()->create_user();
4247 $user2 = self
::getDataGenerator()->create_user();
4249 // The person doing the search.
4250 $this->setUser($user1);
4252 // Send some messages back and forth.
4254 $this->send_message($user1, $user2, 'Yo!', 0, $time);
4255 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4256 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4257 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4259 // Get the most recent message.
4260 $result = core_message_external
::data_for_messagearea_get_most_recent_message($user1->id
, $user2->id
);
4262 // We need to execute the return values cleaning process to simulate the web service server.
4263 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_get_most_recent_message_returns(),
4266 // Check the results are correct.
4267 $this->assertEquals($user2->id
, $result['useridfrom']);
4268 $this->assertEquals($user1->id
, $result['useridto']);
4269 $this->assertContains('Word.', $result['text']);
4273 * Tests retrieving most recent message as another user.
4275 public function test_messagearea_get_most_recent_message_as_other_user() {
4276 $this->resetAfterTest(true);
4278 // The person doing the search.
4279 $this->setAdminUser();
4281 // Create some users.
4282 $user1 = self
::getDataGenerator()->create_user();
4283 $user2 = self
::getDataGenerator()->create_user();
4285 // Send some messages back and forth.
4287 $this->send_message($user1, $user2, 'Yo!', 0, $time);
4288 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4289 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4290 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4292 // Get the most recent message.
4293 $result = core_message_external
::data_for_messagearea_get_most_recent_message($user1->id
, $user2->id
);
4295 // We need to execute the return values cleaning process to simulate the web service server.
4296 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_get_most_recent_message_returns(),
4299 // Check the results are correct.
4300 $this->assertEquals($user2->id
, $result['useridfrom']);
4301 $this->assertEquals($user1->id
, $result['useridto']);
4302 $this->assertContains('Word.', $result['text']);
4306 * Tests retrieving most recent message as another user without the proper capabilities.
4308 public function test_messagearea_get_most_recent_message_as_other_user_without_cap() {
4309 $this->resetAfterTest(true);
4311 // Create some users.
4312 $user1 = self
::getDataGenerator()->create_user();
4313 $user2 = self
::getDataGenerator()->create_user();
4314 $user3 = self
::getDataGenerator()->create_user();
4316 // The person asking for the most recent message for another user.
4317 $this->setUser($user1);
4319 // Ensure an exception is thrown.
4320 $this->expectException('moodle_exception');
4321 core_message_external
::data_for_messagearea_get_most_recent_message($user2->id
, $user3->id
);
4325 * Tests retrieving most recent message with messaging disabled.
4327 public function test_messagearea_get_most_recent_message_messaging_disabled() {
4330 $this->resetAfterTest(true);
4332 // Create some skeleton data just so we can call the WS.
4333 $user1 = self
::getDataGenerator()->create_user();
4334 $user2 = self
::getDataGenerator()->create_user();
4336 // The person asking for the most recent message.
4337 $this->setUser($user1);
4339 // Disable messaging.
4340 $CFG->messaging
= 0;
4342 // Ensure an exception is thrown.
4343 $this->expectException('moodle_exception');
4344 core_message_external
::data_for_messagearea_get_most_recent_message($user1->id
, $user2->id
);
4348 * Tests retrieving a user's profile.
4350 public function test_messagearea_get_profile() {
4351 $this->resetAfterTest(true);
4353 // Create some users.
4354 $user1 = self
::getDataGenerator()->create_user();
4355 $user2 = self
::getDataGenerator()->create_user();
4357 // The person asking for the profile information.
4358 $this->setUser($user1);
4361 $result = core_message_external
::data_for_messagearea_get_profile($user1->id
, $user2->id
);
4363 // We need to execute the return values cleaning process to simulate the web service server.
4364 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_get_profile_returns(),
4367 $this->assertEquals($user2->id
, $result['userid']);
4368 $this->assertEmpty($result['email']);
4369 $this->assertEmpty($result['country']);
4370 $this->assertEmpty($result['city']);
4371 $this->assertEquals(fullname($user2), $result['fullname']);
4372 $this->assertNull($result['isonline']);
4373 $this->assertFalse($result['isblocked']);
4374 $this->assertFalse($result['iscontact']);
4378 * Tests retrieving a user's profile as another user.
4380 public function test_messagearea_profile_as_other_user() {
4381 $this->resetAfterTest(true);
4383 // The person asking for the profile information.
4384 $this->setAdminUser();
4386 // Create some users.
4387 $user1 = self
::getDataGenerator()->create_user();
4389 $user2 = new stdClass();
4390 $user2->country
= 'AU';
4391 $user2->city
= 'Perth';
4392 $user2 = self
::getDataGenerator()->create_user($user2);
4395 $result = core_message_external
::data_for_messagearea_get_profile($user1->id
, $user2->id
);
4397 // We need to execute the return values cleaning process to simulate the web service server.
4398 $result = external_api
::clean_returnvalue(core_message_external
::data_for_messagearea_get_profile_returns(),
4401 $this->assertEquals($user2->id
, $result['userid']);
4402 $this->assertEquals($user2->email
, $result['email']);
4403 $this->assertEquals(get_string($user2->country
, 'countries'), $result['country']);
4404 $this->assertEquals($user2->city
, $result['city']);
4405 $this->assertEquals(fullname($user2), $result['fullname']);
4406 $this->assertFalse($result['isonline']);
4407 $this->assertFalse($result['isblocked']);
4408 $this->assertFalse($result['iscontact']);
4412 * Tests retrieving a user's profile as another user without the proper capabilities.
4414 public function test_messagearea_profile_as_other_user_without_cap() {
4415 $this->resetAfterTest(true);
4417 // Create some users.
4418 $user1 = self
::getDataGenerator()->create_user();
4419 $user2 = self
::getDataGenerator()->create_user();
4420 $user3 = self
::getDataGenerator()->create_user();
4422 // The person asking for the profile information for another user.
4423 $this->setUser($user1);
4425 // Ensure an exception is thrown.
4426 $this->expectException('moodle_exception');
4427 core_message_external
::data_for_messagearea_get_profile($user2->id
, $user3->id
);
4431 * Tests retrieving a user's profile with messaging disabled.
4433 public function test_messagearea_profile_messaging_disabled() {
4436 $this->resetAfterTest(true);
4438 // Create some skeleton data just so we can call the WS.
4439 $user1 = self
::getDataGenerator()->create_user();
4440 $user2 = self
::getDataGenerator()->create_user();
4442 // The person asking for the profile information.
4443 $this->setUser($user1);
4445 // Disable messaging.
4446 $CFG->messaging
= 0;
4448 // Ensure an exception is thrown.
4449 $this->expectException('moodle_exception');
4450 core_message_external
::data_for_messagearea_get_profile($user1->id
, $user2->id
);
4454 * Test marking all message as read with an invalid user.
4456 public function test_mark_all_messages_as_read_invalid_user_exception() {
4457 $this->resetAfterTest(true);
4459 $this->expectException('moodle_exception');
4460 core_message_external
::mark_all_messages_as_read(-2132131, 0);
4464 * Test marking all message as read without proper access.
4466 public function test_mark_all_messages_as_read_access_denied_exception() {
4467 $this->resetAfterTest(true);
4469 $sender = $this->getDataGenerator()->create_user();
4470 $user = $this->getDataGenerator()->create_user();
4472 $this->setUser($user);
4473 $this->expectException('moodle_exception');
4474 core_message_external
::mark_all_messages_as_read($sender->id
, 0);
4478 * Test marking all message as read with missing from user.
4480 public function test_mark_all_messages_as_read_missing_from_user_exception() {
4481 $this->resetAfterTest(true);
4483 $sender = $this->getDataGenerator()->create_user();
4485 $this->setUser($sender);
4486 $this->expectException('moodle_exception');
4487 core_message_external
::mark_all_messages_as_read($sender->id
, 99999);
4491 * Test marking all message as read.
4493 public function test_mark_all_messages_as_read() {
4496 $this->resetAfterTest(true);
4498 $sender1 = $this->getDataGenerator()->create_user();
4499 $sender2 = $this->getDataGenerator()->create_user();
4500 $sender3 = $this->getDataGenerator()->create_user();
4501 $recipient = $this->getDataGenerator()->create_user();
4503 $this->setUser($recipient);
4505 $this->send_message($sender1, $recipient, 'Message');
4506 $this->send_message($sender1, $recipient, 'Message');
4507 $this->send_message($sender2, $recipient, 'Message');
4508 $this->send_message($sender2, $recipient, 'Message');
4509 $this->send_message($sender3, $recipient, 'Message');
4510 $this->send_message($sender3, $recipient, 'Message');
4512 core_message_external
::mark_all_messages_as_read($recipient->id
, $sender1->id
);
4513 $this->assertEquals(2, $DB->count_records('message_user_actions'));
4515 core_message_external
::mark_all_messages_as_read($recipient->id
, 0);
4516 $this->assertEquals(6, $DB->count_records('message_user_actions'));
4520 * Test marking all conversation messages as read with an invalid user.
4522 public function test_mark_all_conversation_messages_as_read_invalid_user_exception() {
4523 $this->resetAfterTest(true);
4525 $user1 = self
::getDataGenerator()->create_user();
4526 $user2 = self
::getDataGenerator()->create_user();
4528 // Send some messages back and forth.
4530 $this->send_message($user1, $user2, 'Yo!', 0, $time);
4531 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4532 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4533 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4535 $conversationid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
4537 $this->expectException('moodle_exception');
4538 core_message_external
::mark_all_conversation_messages_as_read(-2132131, $conversationid);
4542 * Test marking all conversation messages as read without proper access.
4544 public function test_mark_all_conversation_messages_as_read_access_denied_exception() {
4545 $this->resetAfterTest(true);
4547 $user1 = self
::getDataGenerator()->create_user();
4548 $user2 = self
::getDataGenerator()->create_user();
4549 $user3 = self
::getDataGenerator()->create_user();
4551 // Send some messages back and forth.
4553 $this->send_message($user1, $user2, 'Yo!', 0, $time);
4554 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4555 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4556 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4558 $conversationid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
4560 // User 3 is not in the conversation.
4561 $this->expectException('moodle_exception');
4562 core_message_external
::mark_all_conversation_messages_as_read($user3->id
, $conversationid);
4566 * Test marking all conversation messages as read for another user.
4568 public function test_mark_all_conversation_messages_as_read_wrong_user() {
4569 $this->resetAfterTest(true);
4571 $user1 = self
::getDataGenerator()->create_user();
4572 $user2 = self
::getDataGenerator()->create_user();
4574 // Send some messages back and forth.
4576 $this->send_message($user1, $user2, 'Yo!', 0, $time);
4577 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4578 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4579 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4581 $conversationid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
4583 // Can't mark the messages as read for user 2.
4584 $this->setUser($user1);
4585 $this->expectException('moodle_exception');
4586 core_message_external
::mark_all_conversation_messages_as_read($user2->id
, $conversationid);
4590 * Test marking all conversation messages as admin.
4592 public function test_mark_all_conversation_messages_as_admin() {
4595 $this->resetAfterTest(true);
4597 $user1 = self
::getDataGenerator()->create_user();
4598 $user2 = self
::getDataGenerator()->create_user();
4600 // Send some messages back and forth.
4602 $this->send_message($user1, $user2, 'Yo!', 0, $time);
4603 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4604 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4605 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4607 $conversationid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
4609 // Admin can do anything.
4610 $this->setAdminUser();
4611 core_message_external
::mark_all_conversation_messages_as_read($user2->id
, $conversationid);
4612 $this->assertEquals(2, $DB->count_records('message_user_actions'));
4616 * Test marking all conversation messages.
4618 public function test_mark_all_conversation_messages_as_read() {
4621 $this->resetAfterTest(true);
4623 $user1 = self
::getDataGenerator()->create_user();
4624 $user2 = self
::getDataGenerator()->create_user();
4626 // Send some messages back and forth.
4628 $this->send_message($user1, $user2, 'Yo!', 0, $time);
4629 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4630 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4631 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4633 $conversationid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
4635 // We are the user we want to mark the messages for and we are in the conversation, all good.
4636 $this->setUser($user1);
4637 core_message_external
::mark_all_conversation_messages_as_read($user1->id
, $conversationid);
4638 $this->assertEquals(2, $DB->count_records('message_user_actions'));
4642 * Test getting unread conversation count.
4644 public function test_get_unread_conversations_count() {
4645 $this->resetAfterTest(true);
4647 // Create some users.
4648 $user1 = self
::getDataGenerator()->create_user();
4649 $user2 = self
::getDataGenerator()->create_user();
4650 $user3 = self
::getDataGenerator()->create_user();
4651 $user4 = self
::getDataGenerator()->create_user();
4653 // The person wanting the conversation count.
4654 $this->setUser($user1);
4656 // Send some messages back and forth, have some different conversations with different users.
4657 $this->send_message($user1, $user2, 'Yo!');
4658 $this->send_message($user2, $user1, 'Sup mang?');
4659 $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
4660 $this->send_message($user2, $user1, 'Word.');
4662 $this->send_message($user1, $user3, 'Booyah');
4663 $this->send_message($user3, $user1, 'Whaaat?');
4664 $this->send_message($user1, $user3, 'Nothing.');
4665 $this->send_message($user3, $user1, 'Cool.');
4667 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
4668 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
4669 $this->send_message($user1, $user4, 'Dope.');
4671 // Get the unread conversation count.
4672 $result = core_message_external
::get_unread_conversations_count($user1->id
);
4674 // We need to execute the return values cleaning process to simulate the web service server.
4675 $result = external_api
::clean_returnvalue(core_message_external
::get_unread_conversations_count_returns(),
4678 $this->assertEquals(3, $result);
4682 * Test getting unread conversation count as other user.
4684 public function test_get_unread_conversations_count_as_other_user() {
4685 $this->resetAfterTest(true);
4687 // The person wanting the conversation count.
4688 $this->setAdminUser();
4690 // Create some users.
4691 $user1 = self
::getDataGenerator()->create_user();
4692 $user2 = self
::getDataGenerator()->create_user();
4693 $user3 = self
::getDataGenerator()->create_user();
4694 $user4 = self
::getDataGenerator()->create_user();
4696 // Send some messages back and forth, have some different conversations with different users.
4697 $this->send_message($user1, $user2, 'Yo!');
4698 $this->send_message($user2, $user1, 'Sup mang?');
4699 $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
4700 $this->send_message($user2, $user1, 'Word.');
4702 $this->send_message($user1, $user3, 'Booyah');
4703 $this->send_message($user3, $user1, 'Whaaat?');
4704 $this->send_message($user1, $user3, 'Nothing.');
4705 $this->send_message($user3, $user1, 'Cool.');
4707 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
4708 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
4709 $this->send_message($user1, $user4, 'Dope.');
4711 // Get the unread conversation count.
4712 $result = core_message_external
::get_unread_conversations_count($user1->id
);
4714 // We need to execute the return values cleaning process to simulate the web service server.
4715 $result = external_api
::clean_returnvalue(core_message_external
::get_unread_conversations_count_returns(),
4718 $this->assertEquals(3, $result);
4722 * Test getting unread conversation count as other user without proper capability.
4724 public function test_get_unread_conversations_count_as_other_user_without_cap() {
4725 $this->resetAfterTest(true);
4727 // Create some users.
4728 $user1 = self
::getDataGenerator()->create_user();
4729 $user2 = self
::getDataGenerator()->create_user();
4731 // The person wanting the conversation count.
4732 $this->setUser($user1);
4734 // Ensure an exception is thrown.
4735 $this->expectException('moodle_exception');
4736 core_message_external
::get_unread_conversations_count($user2->id
);
4740 * Test deleting conversation.
4742 public function test_delete_conversation() {
4745 $this->resetAfterTest(true);
4747 // Create some users.
4748 $user1 = self
::getDataGenerator()->create_user();
4749 $user2 = self
::getDataGenerator()->create_user();
4751 // The person wanting to delete the conversation.
4752 $this->setUser($user1);
4754 // Send some messages back and forth.
4756 $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time);
4757 $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4758 $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4759 $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4761 // Delete the conversation.
4762 core_message_external
::delete_conversation($user1->id
, $user2->id
);
4764 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
4765 $this->assertCount(4, $muas);
4769 $mua1 = array_shift($muas);
4770 $mua2 = array_shift($muas);
4771 $mua3 = array_shift($muas);
4772 $mua4 = array_shift($muas);
4774 $this->assertEquals($user1->id
, $mua1->userid
);
4775 $this->assertEquals($m1id, $mua1->messageid
);
4776 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua1->action
);
4778 $this->assertEquals($user1->id
, $mua2->userid
);
4779 $this->assertEquals($m2id, $mua2->messageid
);
4780 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua2->action
);
4782 $this->assertEquals($user1->id
, $mua3->userid
);
4783 $this->assertEquals($m3id, $mua3->messageid
);
4784 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua3->action
);
4786 $this->assertEquals($user1->id
, $mua4->userid
);
4787 $this->assertEquals($m4id, $mua4->messageid
);
4788 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua4->action
);
4792 * Test deleting conversation as other user.
4794 public function test_delete_conversation_as_other_user() {
4797 $this->resetAfterTest(true);
4799 $this->setAdminUser();
4801 // Create some users.
4802 $user1 = self
::getDataGenerator()->create_user();
4803 $user2 = self
::getDataGenerator()->create_user();
4805 // Send some messages back and forth.
4807 $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time);
4808 $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4809 $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4810 $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4812 // Delete the conversation.
4813 core_message_external
::delete_conversation($user1->id
, $user2->id
);
4815 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
4816 $this->assertCount(4, $muas);
4820 $mua1 = array_shift($muas);
4821 $mua2 = array_shift($muas);
4822 $mua3 = array_shift($muas);
4823 $mua4 = array_shift($muas);
4825 $this->assertEquals($user1->id
, $mua1->userid
);
4826 $this->assertEquals($m1id, $mua1->messageid
);
4827 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua1->action
);
4829 $this->assertEquals($user1->id
, $mua2->userid
);
4830 $this->assertEquals($m2id, $mua2->messageid
);
4831 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua2->action
);
4833 $this->assertEquals($user1->id
, $mua3->userid
);
4834 $this->assertEquals($m3id, $mua3->messageid
);
4835 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua3->action
);
4837 $this->assertEquals($user1->id
, $mua4->userid
);
4838 $this->assertEquals($m4id, $mua4->messageid
);
4839 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua4->action
);
4843 * Test deleting conversation as other user without proper capability.
4845 public function test_delete_conversation_as_other_user_without_cap() {
4846 $this->resetAfterTest(true);
4848 // Create some users.
4849 $user1 = self
::getDataGenerator()->create_user();
4850 $user2 = self
::getDataGenerator()->create_user();
4851 $user3 = self
::getDataGenerator()->create_user();
4853 // Send some messages back and forth.
4855 $this->send_message($user1, $user2, 'Yo!', 0, $time);
4856 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4857 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4858 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4860 // The person wanting to delete the conversation.
4861 $this->setUser($user3);
4863 // Ensure an exception is thrown.
4864 $this->expectException('moodle_exception');
4865 core_message_external
::delete_conversation($user1->id
, $user2->id
);
4869 * Test deleting conversation with messaging disabled.
4871 public function test_delete_conversation_messaging_disabled() {
4874 $this->resetAfterTest(true);
4876 // Create some users.
4877 $user1 = self
::getDataGenerator()->create_user();
4878 $user2 = self
::getDataGenerator()->create_user();
4880 // The person wanting to delete the conversation.
4881 $this->setUser($user1);
4883 // Disable messaging.
4884 $CFG->messaging
= 0;
4886 // Ensure an exception is thrown.
4887 $this->expectException('moodle_exception');
4888 core_message_external
::delete_conversation($user1->id
, $user2->id
);
4892 * Test deleting conversations.
4894 public function test_delete_conversations_by_id() {
4897 $this->resetAfterTest(true);
4899 // Create some users.
4900 $user1 = self
::getDataGenerator()->create_user();
4901 $user2 = self
::getDataGenerator()->create_user();
4903 // The person wanting to delete the conversation.
4904 $this->setUser($user1);
4906 // Send some messages back and forth.
4908 $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time);
4909 $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4910 $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4911 $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4913 $conversationid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
4915 // Delete the conversation.
4916 core_message_external
::delete_conversations_by_id($user1->id
, [$conversationid]);
4918 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
4919 $this->assertCount(4, $muas);
4923 $mua1 = array_shift($muas);
4924 $mua2 = array_shift($muas);
4925 $mua3 = array_shift($muas);
4926 $mua4 = array_shift($muas);
4928 $this->assertEquals($user1->id
, $mua1->userid
);
4929 $this->assertEquals($m1id, $mua1->messageid
);
4930 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua1->action
);
4932 $this->assertEquals($user1->id
, $mua2->userid
);
4933 $this->assertEquals($m2id, $mua2->messageid
);
4934 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua2->action
);
4936 $this->assertEquals($user1->id
, $mua3->userid
);
4937 $this->assertEquals($m3id, $mua3->messageid
);
4938 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua3->action
);
4940 $this->assertEquals($user1->id
, $mua4->userid
);
4941 $this->assertEquals($m4id, $mua4->messageid
);
4942 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua4->action
);
4946 * Test deleting conversations as other user.
4948 public function test_delete_conversations_by_id_as_other_user() {
4951 $this->resetAfterTest(true);
4953 $this->setAdminUser();
4955 // Create some users.
4956 $user1 = self
::getDataGenerator()->create_user();
4957 $user2 = self
::getDataGenerator()->create_user();
4959 // Send some messages back and forth.
4961 $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time);
4962 $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
4963 $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
4964 $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
4966 $conversationid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
4968 // Delete the conversation.
4969 core_message_external
::delete_conversations_by_id($user1->id
, [$conversationid]);
4971 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
4972 $this->assertCount(4, $muas);
4976 $mua1 = array_shift($muas);
4977 $mua2 = array_shift($muas);
4978 $mua3 = array_shift($muas);
4979 $mua4 = array_shift($muas);
4981 $this->assertEquals($user1->id
, $mua1->userid
);
4982 $this->assertEquals($m1id, $mua1->messageid
);
4983 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua1->action
);
4985 $this->assertEquals($user1->id
, $mua2->userid
);
4986 $this->assertEquals($m2id, $mua2->messageid
);
4987 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua2->action
);
4989 $this->assertEquals($user1->id
, $mua3->userid
);
4990 $this->assertEquals($m3id, $mua3->messageid
);
4991 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua3->action
);
4993 $this->assertEquals($user1->id
, $mua4->userid
);
4994 $this->assertEquals($m4id, $mua4->messageid
);
4995 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua4->action
);
4999 * Test deleting conversations as other user without proper capability.
5001 public function test_delete_conversations_by_id_as_other_user_without_cap() {
5002 $this->resetAfterTest(true);
5004 // Create some users.
5005 $user1 = self
::getDataGenerator()->create_user();
5006 $user2 = self
::getDataGenerator()->create_user();
5007 $user3 = self
::getDataGenerator()->create_user();
5009 // Send some messages back and forth.
5011 $this->send_message($user1, $user2, 'Yo!', 0, $time);
5012 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
5013 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
5014 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
5016 $conversationid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
5018 // The person wanting to delete the conversation.
5019 $this->setUser($user3);
5021 // Ensure an exception is thrown.
5022 $this->expectException('moodle_exception');
5023 core_message_external
::delete_conversations_by_id($user1->id
, [$conversationid]);
5027 * Test deleting conversations with messaging disabled.
5029 public function test_delete_conversations_by_id_messaging_disabled() {
5032 $this->resetAfterTest(true);
5034 // Create some users.
5035 $user1 = self
::getDataGenerator()->create_user();
5036 $user2 = self
::getDataGenerator()->create_user();
5038 // Send some messages back and forth.
5040 $this->send_message($user1, $user2, 'Yo!', 0, $time);
5041 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
5042 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
5043 $this->send_message($user2, $user1, 'Word.', 0, $time +
3);
5045 $conversationid = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
5047 // The person wanting to delete the conversation.
5048 $this->setUser($user1);
5050 // Disable messaging.
5051 $CFG->messaging
= 0;
5053 // Ensure an exception is thrown.
5054 $this->expectException('moodle_exception');
5055 core_message_external
::delete_conversations_by_id($user1->id
, [$conversationid]);
5059 * Test get message processor.
5061 public function test_get_message_processor() {
5062 $this->resetAfterTest(true);
5065 $user1 = self
::getDataGenerator()->create_user();
5067 // Set you as the user.
5068 $this->setUser($user1);
5070 // Get the message processors.
5071 $result = core_message_external
::get_message_processor($user1->id
, 'popup');
5073 // We need to execute the return values cleaning process to simulate the web service server.
5074 $result = external_api
::clean_returnvalue(core_message_external
::get_message_processor_returns(), $result);
5076 $this->assertNotEmpty($result['systemconfigured']);
5077 $this->assertNotEmpty($result['userconfigured']);
5081 * Test get_user_notification_preferences
5083 public function test_get_user_message_preferences() {
5084 $this->resetAfterTest(true);
5086 $user = self
::getDataGenerator()->create_user();
5087 $this->setUser($user);
5089 // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
5090 set_config('messagingallusers', true);
5092 // Set a couple of preferences to test.
5093 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'email', $user);
5094 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user);
5095 set_user_preference('message_blocknoncontacts', \core_message\api
::MESSAGE_PRIVACY_SITE
, $user);
5097 $prefs = core_message_external
::get_user_message_preferences();
5098 $prefs = external_api
::clean_returnvalue(core_message_external
::get_user_message_preferences_returns(), $prefs);
5099 $this->assertEquals($user->id
, $prefs['preferences']['userid']);
5101 // Check components.
5102 $this->assertCount(1, $prefs['preferences']['components']);
5103 $this->assertEquals(\core_message\api
::MESSAGE_PRIVACY_SITE
, $prefs['blocknoncontacts']);
5105 // Check some preferences that we previously set.
5107 foreach ($prefs['preferences']['components'] as $component) {
5108 foreach ($component['notifications'] as $prefdata) {
5109 if ($prefdata['preferencekey'] != 'message_provider_moodle_instantmessage') {
5112 foreach ($prefdata['processors'] as $processor) {
5113 if ($processor['name'] == 'email') {
5114 $this->assertTrue($processor['loggedin']['checked']);
5115 $this->assertTrue($processor['loggedoff']['checked']);
5121 $this->assertTrue($found);
5125 * Test get_user_message_preferences permissions
5127 public function test_get_user_message_preferences_permissions() {
5128 $this->resetAfterTest(true);
5130 $user = self
::getDataGenerator()->create_user();
5131 $otheruser = self
::getDataGenerator()->create_user();
5132 $this->setUser($user);
5134 $this->expectException('moodle_exception');
5135 $prefs = core_message_external
::get_user_message_preferences($otheruser->id
);
5139 * Comparison function for sorting contacts.
5145 protected static function sort_contacts($a, $b) {
5146 return $a['userid'] > $b['userid'];
5150 * Comparison function for sorting contacts.
5156 protected static function sort_contacts_id($a, $b) {
5157 return $a['id'] > $b['id'];
5161 * Test verifying that conversations can be marked as favourite conversations.
5163 public function test_set_favourite_conversations_basic() {
5164 $this->resetAfterTest();
5166 $user1 = self
::getDataGenerator()->create_user();
5167 $user2 = self
::getDataGenerator()->create_user();
5168 $user3 = self
::getDataGenerator()->create_user();
5169 $user4 = self
::getDataGenerator()->create_user();
5171 $this->setUser($user1);
5173 // Now, create some conversations.
5175 $this->send_message($user1, $user2, 'Yo!', 0, $time);
5176 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
5177 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
5179 $this->send_message($user1, $user3, 'Booyah');
5180 $this->send_message($user3, $user1, 'Whaaat?');
5181 $this->send_message($user1, $user3, 'Nothing.');
5183 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
5184 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
5186 // Favourite 2 conversations as user 1.
5187 $conversation1 = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
5188 $conversation2 = \core_message\api
::get_conversation_between_users([$user1->id
, $user3->id
]);
5189 $result = core_message_external
::set_favourite_conversations($user1->id
, [$conversation1, $conversation2]);
5191 // We need to execute the return values cleaning process to simulate the web service server.
5192 $result = external_api
::clean_returnvalue(core_message_external
::set_favourite_conversations_returns(), $result);
5193 $this->assertCount(0, $result);
5197 * Test confirming that a user can't favourite a conversation on behalf of another user.
5199 public function test_set_favourite_conversations_another_users_conversation() {
5200 $this->resetAfterTest();
5202 $user1 = self
::getDataGenerator()->create_user();
5203 $user2 = self
::getDataGenerator()->create_user();
5204 $user3 = self
::getDataGenerator()->create_user();
5206 $this->setUser($user3);
5208 // Now, create some conversations.
5210 $this->send_message($user1, $user2, 'Yo!', 0, $time);
5211 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
5212 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
5214 $this->send_message($user1, $user3, 'Booyah');
5215 $this->send_message($user3, $user1, 'Whaaat?');
5216 $this->send_message($user1, $user3, 'Nothing.');
5218 // Try to favourite conversation 1 for user 2, as user3.
5219 $conversation1 = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
5220 $this->expectException(\moodle_exception
::class);
5221 $result = core_message_external
::set_favourite_conversations($user2->id
, [$conversation1]);
5225 * Test confirming that a user can't mark a conversation as their own favourite if it's a conversation they're not a member of.
5227 public function test_set_favourite_conversations_non_member() {
5228 $this->resetAfterTest();
5230 $user1 = self
::getDataGenerator()->create_user();
5231 $user2 = self
::getDataGenerator()->create_user();
5232 $user3 = self
::getDataGenerator()->create_user();
5234 $this->setUser($user3);
5236 // Now, create some conversations.
5238 $this->send_message($user1, $user2, 'Yo!', 0, $time);
5239 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
5240 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
5242 $this->send_message($user1, $user3, 'Booyah');
5243 $this->send_message($user3, $user1, 'Whaaat?');
5244 $this->send_message($user1, $user3, 'Nothing.');
5246 // Try to favourite conversation 1 as user 3.
5247 $conversation1 = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
5248 $conversation2 = \core_message\api
::get_conversation_between_users([$user1->id
, $user3->id
]);
5249 $this->expectException(\moodle_exception
::class);
5250 $result = core_message_external
::set_favourite_conversations($user3->id
, [$conversation1]);
5254 * Test confirming that a user can't favourite a non-existent conversation.
5256 public function test_set_favourite_conversations_non_existent_conversation() {
5257 $this->resetAfterTest();
5259 $user1 = self
::getDataGenerator()->create_user();
5260 $this->setUser($user1);
5262 // Try to favourite a non-existent conversation.
5263 $this->expectException(\moodle_exception
::class);
5264 $result = core_message_external
::set_favourite_conversations($user1->id
, [0]);
5268 * Test confirming that a user can unset a favourite conversation, or list of favourite conversations.
5270 public function test_unset_favourite_conversations_basic() {
5271 $this->resetAfterTest();
5273 $user1 = self
::getDataGenerator()->create_user();
5274 $user2 = self
::getDataGenerator()->create_user();
5275 $user3 = self
::getDataGenerator()->create_user();
5276 $user4 = self
::getDataGenerator()->create_user();
5278 $this->setUser($user1);
5280 // Now, create some conversations.
5282 $this->send_message($user1, $user2, 'Yo!', 0, $time);
5283 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
5284 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
5286 $this->send_message($user1, $user3, 'Booyah');
5287 $this->send_message($user3, $user1, 'Whaaat?');
5288 $this->send_message($user1, $user3, 'Nothing.');
5290 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
5291 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
5293 // Favourite 2 conversations as user 1.
5294 $conversation1 = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
5295 $conversation2 = \core_message\api
::get_conversation_between_users([$user1->id
, $user3->id
]);
5296 \core_message\api
::set_favourite_conversation($conversation1, $user1->id
);
5297 \core_message\api
::set_favourite_conversation($conversation2, $user1->id
);
5298 // Consider first conversations is self-conversation.
5299 $this->assertCount(3, \core_message\api
::get_conversations($user1->id
, 0, 20, null, true));
5301 // Unset favourite self-conversation.
5302 $selfconversation = \core_message\api
::get_self_conversation($user1->id
);
5303 $result = core_message_external
::unset_favourite_conversations($user1->id
, [$selfconversation->id
]);
5304 $this->assertCount(2, \core_message\api
::get_conversations($user1->id
, 0, 20, null, true));
5306 // Now, using the web service, unset the favourite conversations.
5307 $result = core_message_external
::unset_favourite_conversations($user1->id
, [$conversation1, $conversation2]);
5309 // We need to execute the return values cleaning process to simulate the web service server.
5310 $result = external_api
::clean_returnvalue(core_message_external
::unset_favourite_conversations_returns(), $result);
5311 $this->assertCount(0, $result);
5315 * Test confirming that a user can't unfavourite a conversation for another user.
5317 public function test_unset_favourite_conversations_another_users_conversation() {
5318 $this->resetAfterTest();
5320 $user1 = self
::getDataGenerator()->create_user();
5321 $user2 = self
::getDataGenerator()->create_user();
5322 $user3 = self
::getDataGenerator()->create_user();
5324 $this->setUser($user3);
5326 // Now, create some conversations.
5328 $this->send_message($user1, $user2, 'Yo!', 0, $time);
5329 $this->send_message($user2, $user1, 'Sup mang?', 0, $time +
1);
5330 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time +
2);
5332 $this->send_message($user1, $user3, 'Booyah');
5333 $this->send_message($user3, $user1, 'Whaaat?');
5334 $this->send_message($user1, $user3, 'Nothing.');
5336 // Favourite conversation 1 for user1. The current user ($USER) isn't checked for this action.
5337 $conversation1 = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
5338 \core_message\api
::set_favourite_conversation($conversation1, $user1->id
);
5339 // Consider first conversations is self-conversation.
5340 $this->assertCount(2, \core_message\api
::get_conversations($user1->id
, 0, 20, null, true));
5342 // Try to unfavourite conversation 1 for user 2, as user3.
5343 $conversation1 = \core_message\api
::get_conversation_between_users([$user1->id
, $user2->id
]);
5344 $this->expectException(\moodle_exception
::class);
5345 $result = core_message_external
::unset_favourite_conversations($user2->id
, [$conversation1]);
5349 * Test confirming that a user can't unfavourite a non-existent conversation.
5351 public function test_unset_favourite_conversations_non_existent_conversation() {
5352 $this->resetAfterTest();
5354 $user1 = self
::getDataGenerator()->create_user();
5355 $this->setUser($user1);
5357 // Try to unfavourite a non-existent conversation.
5358 $this->expectException(\moodle_exception
::class);
5359 $result = core_message_external
::unset_favourite_conversations($user1->id
, [0]);
5363 * Helper to seed the database with initial state.
5365 protected function create_conversation_test_data() {
5366 // Create some users.
5367 $user1 = self
::getDataGenerator()->create_user();
5368 $user2 = self
::getDataGenerator()->create_user();
5369 $user3 = self
::getDataGenerator()->create_user();
5370 $user4 = self
::getDataGenerator()->create_user();
5374 // Create some conversations. We want:
5375 // 1) At least one of each type (group, individual) of which user1 IS a member and DID send the most recent message.
5376 // 2) At least one of each type (group, individual) of which user1 IS a member and DID NOT send the most recent message.
5377 // 3) At least one of each type (group, individual) of which user1 IS NOT a member.
5378 // 4) At least two group conversation having 0 messages, of which user1 IS a member (To confirm conversationid ordering).
5379 // 5) At least one group conversation having 0 messages, of which user1 IS NOT a member.
5381 // Individual conversation, user1 is a member, last message from other user.
5382 $ic1 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
5383 [$user1->id
, $user2->id
]);
5384 testhelper
::send_fake_message_to_conversation($user1, $ic1->id
, 'Message 1', $time);
5385 testhelper
::send_fake_message_to_conversation($user2, $ic1->id
, 'Message 2', $time +
1);
5387 // Individual conversation, user1 is a member, last message from user1.
5388 $ic2 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
5389 [$user1->id
, $user3->id
]);
5390 testhelper
::send_fake_message_to_conversation($user3, $ic2->id
, 'Message 3', $time +
2);
5391 testhelper
::send_fake_message_to_conversation($user1, $ic2->id
, 'Message 4', $time +
3);
5393 // Individual conversation, user1 is not a member.
5394 $ic3 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
5395 [$user2->id
, $user3->id
]);
5396 testhelper
::send_fake_message_to_conversation($user2, $ic3->id
, 'Message 5', $time +
4);
5397 testhelper
::send_fake_message_to_conversation($user3, $ic3->id
, 'Message 6', $time +
5);
5399 // Group conversation, user1 is not a member.
5400 $gc1 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
5401 [$user2->id
, $user3->id
, $user4->id
], 'Project discussions');
5402 testhelper
::send_fake_message_to_conversation($user2, $gc1->id
, 'Message 7', $time +
6);
5403 testhelper
::send_fake_message_to_conversation($user4, $gc1->id
, 'Message 8', $time +
7);
5405 // Group conversation, user1 is a member, last message from another user.
5406 $gc2 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
5407 [$user1->id
, $user3->id
, $user4->id
], 'Group chat');
5408 testhelper
::send_fake_message_to_conversation($user1, $gc2->id
, 'Message 9', $time +
8);
5409 testhelper
::send_fake_message_to_conversation($user3, $gc2->id
, 'Message 10', $time +
9);
5410 testhelper
::send_fake_message_to_conversation($user4, $gc2->id
, 'Message 11', $time +
10);
5412 // Group conversation, user1 is a member, last message from user1.
5413 $gc3 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
5414 [$user1->id
, $user2->id
, $user3->id
, $user4->id
], 'Group chat again!');
5415 testhelper
::send_fake_message_to_conversation($user4, $gc3->id
, 'Message 12', $time +
11);
5416 testhelper
::send_fake_message_to_conversation($user3, $gc3->id
, 'Message 13', $time +
12);
5417 testhelper
::send_fake_message_to_conversation($user1, $gc3->id
, 'Message 14', $time +
13);
5419 // Empty group conversations (x2), user1 is a member.
5420 $gc4 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
5421 [$user1->id
, $user2->id
, $user3->id
], 'Empty group');
5422 $gc5 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
5423 [$user1->id
, $user2->id
, $user4->id
], 'Another empty group');
5425 // Empty group conversation, user1 is NOT a member.
5426 $gc6 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
5427 [$user2->id
, $user3->id
, $user4->id
], 'Empty group 3');
5429 return [$user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6];
5433 * Test confirming the basic use of get_conversations, with no limits, nor type or favourite restrictions.
5435 public function test_get_conversations_no_restrictions() {
5436 $this->resetAfterTest(true);
5438 // Get a bunch of conversations, some group, some individual and in different states.
5439 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5440 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5442 // The user making the request.
5443 $this->setUser($user1);
5445 // Get all conversations for user1.
5446 $result = core_message_external
::get_conversations($user1->id
);
5447 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5448 $conversations = $result['conversations'];
5450 $selfconversation = \core_message\api
::get_self_conversation($user1->id
);
5452 // Verify there are 7 conversations: 2 individual, 2 group with message, 2 group without messages
5453 // and a self-conversation.
5454 // The conversations with the most recent messages should be listed first, followed by the most newly created
5455 // conversations without messages.
5456 $this->assertCount(7, $conversations);
5457 $this->assertEquals($gc3->id
, $conversations[0]['id']);
5458 $this->assertEquals($gc2->id
, $conversations[1]['id']);
5459 $this->assertEquals($ic2->id
, $conversations[2]['id']);
5460 $this->assertEquals($ic1->id
, $conversations[3]['id']);
5461 $this->assertEquals($gc5->id
, $conversations[4]['id']);
5462 $this->assertEquals($gc4->id
, $conversations[5]['id']);
5463 $this->assertEquals($selfconversation->id
, $conversations[6]['id']);
5465 foreach ($conversations as $conv) {
5466 $this->assertArrayHasKey('id', $conv);
5467 $this->assertArrayHasKey('name', $conv);
5468 $this->assertArrayHasKey('subname', $conv);
5469 $this->assertArrayHasKey('imageurl', $conv);
5470 $this->assertArrayHasKey('type', $conv);
5471 $this->assertArrayHasKey('membercount', $conv);
5472 $this->assertArrayHasKey('isfavourite', $conv);
5473 $this->assertArrayHasKey('isread', $conv);
5474 $this->assertArrayHasKey('unreadcount', $conv);
5475 $this->assertArrayHasKey('members', $conv);
5476 foreach ($conv['members'] as $member) {
5477 $this->assertArrayHasKey('id', $member);
5478 $this->assertArrayHasKey('fullname', $member);
5479 $this->assertArrayHasKey('profileimageurl', $member);
5480 $this->assertArrayHasKey('profileimageurlsmall', $member);
5481 $this->assertArrayHasKey('isonline', $member);
5482 $this->assertArrayHasKey('showonlinestatus', $member);
5483 $this->assertArrayHasKey('isblocked', $member);
5484 $this->assertArrayHasKey('iscontact', $member);
5485 $this->assertArrayHasKey('isdeleted', $member);
5486 $this->assertArrayHasKey('canmessage', $member);
5487 $this->assertArrayHasKey('requirescontact', $member);
5488 $this->assertArrayHasKey('contactrequests', $member);
5490 $this->assertArrayHasKey('messages', $conv);
5491 foreach ($conv['messages'] as $message) {
5492 $this->assertArrayHasKey('id', $message);
5493 $this->assertArrayHasKey('useridfrom', $message);
5494 $this->assertArrayHasKey('text', $message);
5495 $this->assertArrayHasKey('timecreated', $message);
5501 * Test verifying that html format messages are supported, and that message_format_message_text() is being called appropriately.
5503 public function test_get_conversations_message_format() {
5504 $this->resetAfterTest();
5507 // Create some users.
5508 $user1 = self
::getDataGenerator()->create_user();
5509 $user2 = self
::getDataGenerator()->create_user();
5511 // Create conversation.
5512 $conversation = \core_message\api
::create_conversation(
5513 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
5514 [$user1->id
, $user2->id
]
5517 // Send some messages back and forth.
5519 testhelper
::send_fake_message_to_conversation($user2, $conversation->id
, 'Sup mang?', $time +
1);
5520 $mid = testhelper
::send_fake_message_to_conversation($user1, $conversation->id
, '<a href="#">A link</a>', $time +
2);
5521 $message = $DB->get_record('messages', ['id' => $mid]);
5523 // The user in scope.
5524 $this->setUser($user1);
5526 // Verify the format of the html message.
5527 $expectedmessagetext = message_format_message_text($message);
5528 $result = core_message_external
::get_conversations($user1->id
);
5529 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5530 $conversations = $result['conversations'];
5531 $messages = $conversations[0]['messages'];
5532 $this->assertEquals($expectedmessagetext, $messages[0]['text']);
5536 * Tests retrieving conversations with a limit and offset to ensure pagination works correctly.
5538 public function test_get_conversations_limit_offset() {
5539 $this->resetAfterTest(true);
5541 // Get a bunch of conversations, some group, some individual and in different states.
5542 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5543 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5545 // The user making the request.
5546 $this->setUser($user1);
5548 // Get all conversations for user1.
5549 $result = core_message_external
::get_conversations($user1->id
, 0, 1);
5550 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5551 $conversations = $result['conversations'];
5553 // Verify the first conversation.
5554 $this->assertCount(1, $conversations);
5555 $conversation = array_shift($conversations);
5556 $this->assertEquals($gc3->id
, $conversation['id']);
5558 // Verify the next conversation.
5559 $result = core_message_external
::get_conversations($user1->id
, 1, 1);
5560 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5561 $conversations = $result['conversations'];
5562 $this->assertCount(1, $conversations);
5563 $this->assertEquals($gc2->id
, $conversations[0]['id']);
5565 // Verify the next conversation.
5566 $result = core_message_external
::get_conversations($user1->id
, 2, 1);
5567 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5568 $conversations = $result['conversations'];
5569 $this->assertCount(1, $conversations);
5570 $this->assertEquals($ic2->id
, $conversations[0]['id']);
5572 // Skip one and get both empty conversations.
5573 $result = core_message_external
::get_conversations($user1->id
, 4, 2);
5574 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5575 $conversations = $result['conversations'];
5576 $this->assertCount(2, $conversations);
5577 $this->assertEquals($gc5->id
, $conversations[0]['id']);
5578 $this->assertEmpty($conversations[0]['messages']);
5579 $this->assertEquals($gc4->id
, $conversations[1]['id']);
5580 $this->assertEmpty($conversations[1]['messages']);
5582 // Ask for an offset that doesn't exist and verify no conversations are returned.
5583 $conversations = \core_message\api
::get_conversations($user1->id
, 10, 1);
5584 $this->assertCount(0, $conversations);
5588 * Test verifying the type filtering behaviour of the get_conversations external method.
5590 public function test_get_conversations_type_filter() {
5591 $this->resetAfterTest(true);
5593 // Get a bunch of conversations, some group, some individual and in different states.
5594 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5595 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5597 // The user making the request.
5598 $this->setUser($user1);
5600 // Verify we can ask for only individual conversations.
5601 $result = core_message_external
::get_conversations($user1->id
, 0, 20,
5602 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
);
5603 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5604 $conversations = $result['conversations'];
5605 $this->assertCount(2, $conversations);
5607 // Verify we can ask for only group conversations.
5608 $result = core_message_external
::get_conversations($user1->id
, 0, 20,
5609 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
);
5610 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5611 $conversations = $result['conversations'];
5612 $this->assertCount(4, $conversations);
5614 // Verify an exception is thrown if an unrecognized type is specified.
5615 $this->expectException(\moodle_exception
::class);
5616 core_message_external
::get_conversations($user1->id
, 0, 20, 0);
5620 * Tests retrieving conversations when a 'self' conversation exists.
5622 public function test_get_conversations_self_conversations() {
5624 $this->resetAfterTest();
5626 // Create a conversation between one user and themself.
5627 $user1 = self
::getDataGenerator()->create_user();
5628 $conversation = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
,
5630 testhelper
::send_fake_message_to_conversation($user1, $conversation->id
, 'Test message to self!');
5632 // Verify we are in a 'self' conversation state.
5633 $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation->id
]);
5634 $this->assertCount(1, $members);
5635 $member = array_pop($members);
5636 $this->assertEquals($user1->id
, $member->userid
);
5638 // Verify this conversation is returned by the method.
5639 $this->setUser($user1);
5640 $result = core_message_external
::get_conversations($user1->id
, 0, 20);
5641 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5642 $conversations = $result['conversations'];
5643 $this->assertCount(1, $conversations);
5647 * Tests retrieving conversations when a conversation contains a deleted user.
5649 public function test_get_conversations_deleted_user() {
5650 $this->resetAfterTest(true);
5652 // Get a bunch of conversations, some group, some individual and in different states.
5653 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5654 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5656 // The user making the request.
5657 $this->setUser($user1);
5659 $selfconversation = \core_message\api
::get_self_conversation($user1->id
);
5661 // Delete the second user and retrieve the conversations.
5662 // We should have 6 still, as conversations with soft-deleted users are still returned.
5663 // Group conversations are also present, albeit with less members.
5664 delete_user($user2);
5665 $result = core_message_external
::get_conversations($user1->id
);
5666 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5667 $conversations = $result['conversations'];
5668 $this->assertCount(7, $conversations);
5669 $this->assertEquals($gc3->id
, $conversations[0]['id']);
5670 $this->assertcount(1, $conversations[0]['members']);
5671 $this->assertEquals($gc2->id
, $conversations[1]['id']);
5672 $this->assertcount(1, $conversations[1]['members']);
5673 $this->assertEquals($ic2->id
, $conversations[2]['id']);
5674 $this->assertEquals($ic1->id
, $conversations[3]['id']);
5675 $this->assertEquals($gc5->id
, $conversations[4]['id']);
5676 $this->assertEquals($gc4->id
, $conversations[5]['id']);
5677 $this->assertEquals($selfconversation->id
, $conversations[6]['id']);
5679 // Delete a user from a group conversation where that user had sent the most recent message.
5680 // This user will still be present in the members array, as will the message in the messages array.
5681 delete_user($user4);
5682 $result = core_message_external
::get_conversations($user1->id
);
5683 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5684 $conversations = $result['conversations'];
5685 $this->assertCount(7, $conversations);
5686 $this->assertEquals($gc2->id
, $conversations[1]['id']);
5687 $this->assertcount(1, $conversations[1]['members']);
5688 $this->assertEquals($user4->id
, $conversations[1]['members'][0]['id']);
5689 $this->assertcount(1, $conversations[1]['messages']);
5690 $this->assertEquals($user4->id
, $conversations[1]['messages'][0]['useridfrom']);
5692 // Delete the third user and retrieve the conversations.
5693 // We should have 7 still (including self-conversation), as conversations with soft-deleted users are still returned.
5694 // Group conversations are also present, albeit with less members.
5695 delete_user($user3);
5696 $result = core_message_external
::get_conversations($user1->id
);
5697 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5698 $conversations = $result['conversations'];
5699 $this->assertCount(7, $conversations);
5700 $this->assertEquals($gc3->id
, $conversations[0]['id']);
5701 $this->assertcount(1, $conversations[0]['members']);
5702 $this->assertEquals($gc2->id
, $conversations[1]['id']);
5703 $this->assertcount(1, $conversations[1]['members']);
5704 $this->assertEquals($ic2->id
, $conversations[2]['id']);
5705 $this->assertEquals($ic1->id
, $conversations[3]['id']);
5706 $this->assertEquals($gc5->id
, $conversations[4]['id']);
5707 $this->assertEquals($gc4->id
, $conversations[5]['id']);
5708 $this->assertEquals($selfconversation->id
, $conversations[6]['id']);
5712 * Tests retrieving conversations when a conversation contains a deleted from the database user.
5714 public function test_get_conversations_deleted_user_from_database() {
5717 $this->resetAfterTest();
5719 $user1 = self
::getDataGenerator()->create_user();
5720 $user2 = self
::getDataGenerator()->create_user();
5721 $user3 = self
::getDataGenerator()->create_user();
5723 $conversation1 = \core_message\api
::create_conversation(
5724 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
5729 'Individual conversation 1'
5732 testhelper
::send_fake_message_to_conversation($user1, $conversation1->id
, 'A');
5733 testhelper
::send_fake_message_to_conversation($user2, $conversation1->id
, 'B');
5734 testhelper
::send_fake_message_to_conversation($user1, $conversation1->id
, 'C');
5736 $conversation2 = \core_message\api
::create_conversation(
5737 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
5742 'Individual conversation 2'
5745 testhelper
::send_fake_message_to_conversation($user1, $conversation2->id
, 'A');
5746 testhelper
::send_fake_message_to_conversation($user3, $conversation2->id
, 'B');
5747 testhelper
::send_fake_message_to_conversation($user1, $conversation2->id
, 'C');
5749 $this->setUser($user1);
5751 // Delete the second user (from DB as well as this could happen in the past).
5752 delete_user($user2);
5753 $DB->delete_records('user', ['id' => $user2->id
]);
5754 $result = core_message_external
::get_conversations($user1->id
, 0, 20, 1, false);
5755 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5757 $conversation = $result['conversations'];
5759 $this->assertCount(1, $conversation);
5761 $conversation = reset($conversation);
5763 $this->assertEquals($conversation2->id
, $conversation['id']);
5767 * Test verifying the behaviour of get_conversations() when fetching favourite conversations.
5769 public function test_get_conversations_favourite_conversations() {
5770 $this->resetAfterTest(true);
5772 // Get a bunch of conversations, some group, some individual and in different states.
5773 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
5774 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
5776 // The user making the request.
5777 $this->setUser($user1);
5779 // Unset favourite self-conversation.
5780 $selfconversation = \core_message\api
::get_self_conversation($user1->id
);
5781 \core_message\api
::unset_favourite_conversation($selfconversation->id
, $user1->id
);
5783 // Try to get ONLY favourite conversations, when no favourites exist.
5784 $result = core_message_external
::get_conversations($user1->id
, 0, 20, null, true);
5785 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5786 $conversations = $result['conversations'];
5787 $this->assertEquals([], $conversations);
5789 // Try to get NO favourite conversations, when no favourites exist.
5790 $result = core_message_external
::get_conversations($user1->id
, 0, 20, null, false);
5791 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5792 $conversations = $result['conversations'];
5793 // Consider first conversations is self-conversation.
5794 $this->assertCount(7, $conversations);
5796 // Mark a few conversations as favourites.
5797 \core_message\api
::set_favourite_conversation($ic1->id
, $user1->id
);
5798 \core_message\api
::set_favourite_conversation($gc2->id
, $user1->id
);
5799 \core_message\api
::set_favourite_conversation($gc5->id
, $user1->id
);
5801 // Get the conversations, first with no restrictions, confirming the favourite status of the conversations.
5802 $result = core_message_external
::get_conversations($user1->id
);
5803 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5804 $conversations = $result['conversations'];
5805 $this->assertCount(7, $conversations);
5806 foreach ($conversations as $conv) {
5807 if (in_array($conv['id'], [$ic1->id
, $gc2->id
, $gc5->id
])) {
5808 $this->assertTrue($conv['isfavourite']);
5812 // Now, get ONLY favourite conversations.
5813 $result = core_message_external
::get_conversations($user1->id
, 0, 20, null, true);
5814 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5815 $conversations = $result['conversations'];
5816 $this->assertCount(3, $conversations);
5817 foreach ($conversations as $conv) {
5818 $this->assertTrue($conv['isfavourite']);
5821 // Now, try ONLY favourites of type 'group'.
5822 $conversations = \core_message\api
::get_conversations($user1->id
, 0, 20,
5823 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
, true);
5824 $this->assertCount(2, $conversations);
5825 foreach ($conversations as $conv) {
5826 $this->assertTrue($conv->isfavourite
);
5829 // And NO favourite conversations.
5830 $result = core_message_external
::get_conversations($user1->id
, 0, 20, null, false);
5831 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5832 $conversations = $result['conversations'];
5833 $this->assertCount(4, $conversations);
5834 foreach ($conversations as $conv) {
5835 $this->assertFalse($conv['isfavourite']);
5840 * Test verifying that group linked conversations are returned and contain a subname matching the course name.
5842 public function test_get_conversations_group_linked() {
5843 $this->resetAfterTest();
5846 // Create some users.
5847 $user1 = self
::getDataGenerator()->create_user();
5848 $user2 = self
::getDataGenerator()->create_user();
5849 $user3 = self
::getDataGenerator()->create_user();
5851 $course1 = $this->getDataGenerator()->create_course();
5853 // Create a group with a linked conversation.
5854 $this->setAdminUser();
5855 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
5856 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
5857 $this->getDataGenerator()->enrol_user($user3->id
, $course1->id
);
5858 $group1 = $this->getDataGenerator()->create_group([
5859 'courseid' => $course1->id
,
5860 'enablemessaging' => 1,
5861 'picturepath' => $CFG->dirroot
. '/lib/tests/fixtures/gd-logo.png'
5864 // Add users to group1.
5865 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
5866 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
5868 $result = core_message_external
::get_conversations($user1->id
, 0, 20, null, false);
5869 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5870 $conversations = $result['conversations'];
5872 $this->assertEquals(2, $conversations[0]['membercount']);
5873 $this->assertEquals($course1->shortname
, $conversations[0]['subname']);
5874 $groupimageurl = get_group_picture_url($group1, $group1->courseid
, true);
5875 $this->assertEquals($groupimageurl, $conversations[0]['imageurl']);
5877 // Now, disable the conversation linked to the group and verify it's no longer returned.
5878 $DB->set_field('message_conversations', 'enabled', 0, ['id' => $conversations[0]['id']]);
5879 $result = core_message_external
::get_conversations($user1->id
, 0, 20, null, false);
5880 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5881 $conversations = $result['conversations'];
5882 $this->assertCount(0, $conversations);
5886 * Test verifying get_conversations when there are users in a group and/or individual conversation. The reason this
5887 * test is performed is because we do not need as much data for group conversations (saving DB calls), so we want
5888 * to confirm this happens.
5890 public function test_get_conversations_user_in_group_and_individual_chat() {
5891 $this->resetAfterTest();
5893 $user1 = self
::getDataGenerator()->create_user();
5894 $user2 = self
::getDataGenerator()->create_user();
5895 $user3 = self
::getDataGenerator()->create_user();
5897 $conversation = \core_message\api
::create_conversation(
5898 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
5903 'Individual conversation'
5906 testhelper
::send_fake_message_to_conversation($user1, $conversation->id
);
5908 $conversation = \core_message\api
::create_conversation(
5909 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
5914 'Group conversation'
5917 testhelper
::send_fake_message_to_conversation($user1, $conversation->id
);
5919 \core_message\api
::create_contact_request($user1->id
, $user2->id
);
5920 \core_message\api
::create_contact_request($user1->id
, $user3->id
);
5922 $this->setUser($user2);
5923 $result = core_message_external
::get_conversations($user2->id
);
5924 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5925 $conversations = $result['conversations'];
5927 $groupconversation = array_shift($conversations);
5928 $individualconversation = array_shift($conversations);
5930 $this->assertEquals('Group conversation', $groupconversation['name']);
5931 $this->assertEquals('Individual conversation', $individualconversation['name']);
5933 $this->assertCount(1, $groupconversation['members']);
5934 $this->assertCount(1, $individualconversation['members']);
5936 $groupmember = reset($groupconversation['members']);
5937 $this->assertNull($groupmember['requirescontact']);
5938 $this->assertNull($groupmember['canmessage']);
5939 $this->assertEmpty($groupmember['contactrequests']);
5941 $individualmember = reset($individualconversation['members']);
5942 $this->assertNotNull($individualmember['requirescontact']);
5943 $this->assertNotNull($individualmember['canmessage']);
5944 $this->assertNotEmpty($individualmember['contactrequests']);
5948 * Test verifying get_conversations identifies if a conversation is muted or not.
5950 public function test_get_conversations_some_muted() {
5951 $this->resetAfterTest();
5953 // Create some users.
5954 $user1 = self
::getDataGenerator()->create_user();
5955 $user2 = self
::getDataGenerator()->create_user();
5956 $user3 = self
::getDataGenerator()->create_user();
5958 $conversation1 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
5959 [$user1->id
, $user2->id
]);
5960 testhelper
::send_fake_message_to_conversation($user1, $conversation1->id
, 'Message 1');
5961 testhelper
::send_fake_message_to_conversation($user2, $conversation1->id
, 'Message 2');
5962 \core_message\api
::mute_conversation($user1->id
, $conversation1->id
);
5964 $conversation2 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
5965 [$user1->id
, $user3->id
]);
5966 testhelper
::send_fake_message_to_conversation($user1, $conversation2->id
, 'Message 1');
5967 testhelper
::send_fake_message_to_conversation($user2, $conversation2->id
, 'Message 2');
5969 $conversation3 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
5970 [$user1->id
, $user2->id
]);
5971 \core_message\api
::mute_conversation($user1->id
, $conversation3->id
);
5973 $conversation4 = \core_message\api
::create_conversation(\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
5974 [$user1->id
, $user3->id
]);
5976 $this->setUser($user1);
5977 $result = core_message_external
::get_conversations($user1->id
);
5978 $result = external_api
::clean_returnvalue(core_message_external
::get_conversations_returns(), $result);
5979 $conversations = $result['conversations'];
5981 usort($conversations, function($first, $second){
5982 return $first['id'] > $second['id'];
5985 $selfconversation = array_shift($conversations);
5986 $conv1 = array_shift($conversations);
5987 $conv2 = array_shift($conversations);
5988 $conv3 = array_shift($conversations);
5989 $conv4 = array_shift($conversations);
5991 $this->assertTrue($conv1['ismuted']);
5992 $this->assertFalse($conv2['ismuted']);
5993 $this->assertTrue($conv3['ismuted']);
5994 $this->assertFalse($conv4['ismuted']);
5998 * Test returning members in a conversation with no contact requests.
6000 public function test_get_conversation_members_messaging_disabled() {
6003 $this->resetAfterTest();
6005 $CFG->messaging
= 0;
6007 $this->expectException('moodle_exception');
6008 core_message_external
::get_conversation_members(1, 2);
6012 * Test returning members in a conversation with no contact requests.
6014 public function test_get_conversation_members_wrong_user() {
6015 $this->resetAfterTest();
6017 $user1 = self
::getDataGenerator()->create_user();
6018 $user2 = self
::getDataGenerator()->create_user();
6020 $this->setUser($user2);
6022 $this->expectException('moodle_exception');
6023 core_message_external
::get_conversation_members($user1->id
, 2);
6027 * Test returning members in a conversation with no contact requests.
6029 public function test_get_conversation_members() {
6030 $this->resetAfterTest();
6032 $lastaccess = new stdClass();
6033 $lastaccess->lastaccess
= time();
6035 $user1 = self
::getDataGenerator()->create_user($lastaccess);
6036 $user2 = self
::getDataGenerator()->create_user();
6037 $user3 = self
::getDataGenerator()->create_user();
6039 // This user will not be in the conversation, but a contact request will exist for them.
6040 $user4 = self
::getDataGenerator()->create_user();
6042 // Add some contact requests.
6043 \core_message\api
::create_contact_request($user1->id
, $user3->id
);
6044 \core_message\api
::create_contact_request($user1->id
, $user4->id
);
6045 \core_message\api
::create_contact_request($user2->id
, $user3->id
);
6047 // User 1 and 2 are already contacts.
6048 \core_message\api
::add_contact($user1->id
, $user2->id
);
6050 // User 1 has blocked user 3.
6051 \core_message\api
::block_user($user1->id
, $user3->id
);
6053 $conversation = \core_message\api
::create_conversation(
6054 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
6061 $conversationid = $conversation->id
;
6063 $this->setAdminUser();
6065 $members = core_message_external
::get_conversation_members($user1->id
, $conversationid, false);
6066 external_api
::clean_returnvalue(core_message_external
::get_conversation_members_returns(), $members);
6070 $this->assertCount(3, $members);
6071 $member1 = array_shift($members);
6072 $member2 = array_shift($members);
6073 $member3 = array_shift($members);
6075 // Confirm the standard fields are OK.
6076 $this->assertEquals($user1->id
, $member1->id
);
6077 $this->assertEquals(fullname($user1), $member1->fullname
);
6078 $this->assertEquals(true, $member1->isonline
);
6079 $this->assertEquals(true, $member1->showonlinestatus
);
6080 $this->assertEquals(false, $member1->iscontact
);
6081 $this->assertEquals(false, $member1->isblocked
);
6082 $this->assertObjectHasAttribute('contactrequests', $member1);
6083 $this->assertEmpty($member1->contactrequests
);
6085 $this->assertEquals($user2->id
, $member2->id
);
6086 $this->assertEquals(fullname($user2), $member2->fullname
);
6087 $this->assertEquals(false, $member2->isonline
);
6088 $this->assertEquals(true, $member2->showonlinestatus
);
6089 $this->assertEquals(true, $member2->iscontact
);
6090 $this->assertEquals(false, $member2->isblocked
);
6091 $this->assertObjectHasAttribute('contactrequests', $member2);
6092 $this->assertEmpty($member2->contactrequests
);
6094 $this->assertEquals($user3->id
, $member3->id
);
6095 $this->assertEquals(fullname($user3), $member3->fullname
);
6096 $this->assertEquals(false, $member3->isonline
);
6097 $this->assertEquals(true, $member3->showonlinestatus
);
6098 $this->assertEquals(false, $member3->iscontact
);
6099 $this->assertEquals(true, $member3->isblocked
);
6100 $this->assertObjectHasAttribute('contactrequests', $member3);
6101 $this->assertEmpty($member3->contactrequests
);
6105 * Test returning members in a conversation with contact requests.
6107 public function test_get_conversation_members_with_contact_requests() {
6108 $this->resetAfterTest();
6110 $lastaccess = new stdClass();
6111 $lastaccess->lastaccess
= time();
6113 $user1 = self
::getDataGenerator()->create_user($lastaccess);
6114 $user2 = self
::getDataGenerator()->create_user();
6115 $user3 = self
::getDataGenerator()->create_user();
6117 // This user will not be in the conversation, but a contact request will exist for them.
6118 $user4 = self
::getDataGenerator()->create_user();
6120 // Add some contact requests.
6121 \core_message\api
::create_contact_request($user1->id
, $user2->id
);
6122 \core_message\api
::create_contact_request($user1->id
, $user3->id
);
6123 \core_message\api
::create_contact_request($user1->id
, $user4->id
);
6124 \core_message\api
::create_contact_request($user2->id
, $user3->id
);
6126 // User 1 and 2 are already contacts.
6127 \core_message\api
::add_contact($user1->id
, $user2->id
);
6128 // User 1 has blocked user 3.
6129 \core_message\api
::block_user($user1->id
, $user3->id
);
6131 $conversation = \core_message\api
::create_conversation(
6132 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
6139 $conversationid = $conversation->id
;
6141 $this->setAdminUser();
6143 $members = core_message_external
::get_conversation_members($user1->id
, $conversationid, true);
6144 external_api
::clean_returnvalue(core_message_external
::get_conversation_members_returns(), $members);
6148 $this->assertCount(3, $members);
6149 $member1 = array_shift($members);
6150 $member2 = array_shift($members);
6151 $member3 = array_shift($members);
6153 // Confirm the standard fields are OK.
6154 $this->assertEquals($user1->id
, $member1->id
);
6155 $this->assertEquals(fullname($user1), $member1->fullname
);
6156 $this->assertEquals(true, $member1->isonline
);
6157 $this->assertEquals(true, $member1->showonlinestatus
);
6158 $this->assertEquals(false, $member1->iscontact
);
6159 $this->assertEquals(false, $member1->isblocked
);
6160 $this->assertCount(2, $member1->contactrequests
);
6162 $this->assertEquals($user2->id
, $member2->id
);
6163 $this->assertEquals(fullname($user2), $member2->fullname
);
6164 $this->assertEquals(false, $member2->isonline
);
6165 $this->assertEquals(true, $member2->showonlinestatus
);
6166 $this->assertEquals(true, $member2->iscontact
);
6167 $this->assertEquals(false, $member2->isblocked
);
6168 $this->assertCount(1, $member2->contactrequests
);
6170 $this->assertEquals($user3->id
, $member3->id
);
6171 $this->assertEquals(fullname($user3), $member3->fullname
);
6172 $this->assertEquals(false, $member3->isonline
);
6173 $this->assertEquals(true, $member3->showonlinestatus
);
6174 $this->assertEquals(false, $member3->iscontact
);
6175 $this->assertEquals(true, $member3->isblocked
);
6176 $this->assertCount(1, $member3->contactrequests
);
6178 // Confirm the contact requests are OK.
6179 $request1 = array_shift($member1->contactrequests
);
6180 $request2 = array_shift($member1->contactrequests
);
6182 $this->assertEquals($user1->id
, $request1->userid
);
6183 $this->assertEquals($user2->id
, $request1->requesteduserid
);
6185 $this->assertEquals($user1->id
, $request2->userid
);
6186 $this->assertEquals($user3->id
, $request2->requesteduserid
);
6188 $request1 = array_shift($member2->contactrequests
);
6190 $this->assertEquals($user1->id
, $request1->userid
);
6191 $this->assertEquals($user2->id
, $request1->requesteduserid
);
6193 $request1 = array_shift($member3->contactrequests
);
6195 $this->assertEquals($user1->id
, $request1->userid
);
6196 $this->assertEquals($user3->id
, $request1->requesteduserid
);
6200 * Test returning members in a conversation when you are not a member.
6202 public function test_get_conversation_members_not_a_member() {
6203 $this->resetAfterTest();
6205 $user1 = self
::getDataGenerator()->create_user();
6206 $user2 = self
::getDataGenerator()->create_user();
6208 // This user will not be in the conversation.
6209 $user3 = self
::getDataGenerator()->create_user();
6211 $conversation = \core_message\api
::create_conversation(
6212 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
6218 $conversationid = $conversation->id
;
6220 $this->setUser($user3);
6222 $this->expectException('moodle_exception');
6223 core_message_external
::get_conversation_members($user3->id
, $conversationid);
6227 * Test verifying multiple messages can be sent to an individual conversation.
6229 public function test_send_messages_to_conversation_individual() {
6230 $this->resetAfterTest(true);
6232 // Get a bunch of conversations, some group, some individual and in different states.
6233 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
6234 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
6236 // Enrol the users in the same course, so the default privacy controls (course + contacts) can be used.
6237 $course1 = $this->getDataGenerator()->create_course();
6238 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
6239 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
6240 $this->getDataGenerator()->enrol_user($user3->id
, $course1->id
);
6241 $this->getDataGenerator()->enrol_user($user4->id
, $course1->id
);
6243 // The user making the request.
6244 $this->setUser($user1);
6246 // Try to send a message as user1 to a conversation user1 is a a part of.
6249 'text' => 'a message from user 1',
6250 'textformat' => FORMAT_MOODLE
6253 'text' => 'another message from user 1',
6254 'textformat' => FORMAT_MOODLE
6257 // Redirect messages.
6258 // This marks messages as read, but we can still observe and verify the number of conversation recipients,
6259 // based on the message_viewed events generated as part of marking the message as read for each user.
6260 $this->preventResetByRollback();
6261 $sink = $this->redirectMessages();
6262 $writtenmessages = core_message_external
::send_messages_to_conversation($ic1->id
, $messages);
6264 external_api
::clean_returnvalue(core_message_external
::send_messages_to_conversation_returns(), $writtenmessages);
6266 $this->assertCount(2, $writtenmessages);
6267 $this->assertObjectHasAttribute('id', $writtenmessages[0]);
6268 $this->assertEquals($user1->id
, $writtenmessages[0]->useridfrom
);
6269 $this->assertEquals('<p>a message from user 1</p>', $writtenmessages[0]->text
);
6270 $this->assertNotEmpty($writtenmessages[0]->timecreated
);
6272 $this->assertObjectHasAttribute('id', $writtenmessages[1]);
6273 $this->assertEquals($user1->id
, $writtenmessages[1]->useridfrom
);
6274 $this->assertEquals('<p>another message from user 1</p>', $writtenmessages[1]->text
);
6275 $this->assertNotEmpty($writtenmessages[1]->timecreated
);
6279 * Test verifying multiple messages can be sent to an group conversation.
6281 public function test_send_messages_to_conversation_group() {
6282 $this->resetAfterTest(true);
6284 // Get a bunch of conversations, some group, some individual and in different states.
6285 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
6286 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
6288 // Enrol the users in the same course, so the default privacy controls (course + contacts) can be used.
6289 $course1 = $this->getDataGenerator()->create_course();
6290 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
6291 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
6292 $this->getDataGenerator()->enrol_user($user3->id
, $course1->id
);
6293 $this->getDataGenerator()->enrol_user($user4->id
, $course1->id
);
6295 // The user making the request.
6296 $this->setUser($user1);
6298 // Try to send a message as user1 to a conversation user1 is a a part of.
6301 'text' => 'a message from user 1 to group conv',
6302 'textformat' => FORMAT_MOODLE
6305 'text' => 'another message from user 1 to group conv',
6306 'textformat' => FORMAT_MOODLE
6309 // Redirect messages.
6310 // This marks messages as read, but we can still observe and verify the number of conversation recipients,
6311 // based on the message_viewed events generated as part of marking the message as read for each user.
6312 $this->preventResetByRollback();
6313 $sink = $this->redirectMessages();
6314 $writtenmessages = core_message_external
::send_messages_to_conversation($gc2->id
, $messages);
6316 external_api
::clean_returnvalue(core_message_external
::send_messages_to_conversation_returns(), $writtenmessages);
6318 $this->assertCount(2, $writtenmessages);
6319 $this->assertObjectHasAttribute('id', $writtenmessages[0]);
6320 $this->assertEquals($user1->id
, $writtenmessages[0]->useridfrom
);
6321 $this->assertEquals('<p>a message from user 1 to group conv</p>', $writtenmessages[0]->text
);
6322 $this->assertNotEmpty($writtenmessages[0]->timecreated
);
6324 $this->assertObjectHasAttribute('id', $writtenmessages[1]);
6325 $this->assertEquals($user1->id
, $writtenmessages[1]->useridfrom
);
6326 $this->assertEquals('<p>another message from user 1 to group conv</p>', $writtenmessages[1]->text
);
6327 $this->assertNotEmpty($writtenmessages[1]->timecreated
);
6331 * Test verifying multiple messages can not be sent to a non existent conversation.
6333 public function test_send_messages_to_conversation_non_existent_conversation() {
6334 $this->resetAfterTest(true);
6336 // Get a bunch of conversations, some group, some individual and in different states.
6337 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
6338 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
6340 // The user making the request.
6341 $this->setUser($user1);
6343 // Try to send a message as user1 to a conversation user1 is a a part of.
6346 'text' => 'a message from user 1',
6347 'textformat' => FORMAT_MOODLE
6350 'text' => 'another message from user 1',
6351 'textformat' => FORMAT_MOODLE
6354 $this->expectException(\moodle_exception
::class);
6355 $writtenmessages = core_message_external
::send_messages_to_conversation(0, $messages);
6359 * Test verifying multiple messages can not be sent to a conversation by a non-member.
6361 public function test_send_messages_to_conversation_non_member() {
6362 $this->resetAfterTest(true);
6364 // Get a bunch of conversations, some group, some individual and in different states.
6365 list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
6366 $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
6368 // Enrol the users in the same course, so the default privacy controls (course + contacts) can be used.
6369 $course1 = $this->getDataGenerator()->create_course();
6370 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
6371 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
6372 $this->getDataGenerator()->enrol_user($user3->id
, $course1->id
);
6373 $this->getDataGenerator()->enrol_user($user4->id
, $course1->id
);
6375 // The user making the request. This user is not a member of group conversation 1 (gc1).
6376 $this->setUser($user1);
6378 // Try to send a message as user1 to a conversation user1 is a a part of.
6381 'text' => 'a message from user 1 to group conv',
6382 'textformat' => FORMAT_MOODLE
6385 'text' => 'another message from user 1 to group conv',
6386 'textformat' => FORMAT_MOODLE
6389 $this->expectException(\moodle_exception
::class);
6390 $writtenmessages = core_message_external
::send_messages_to_conversation($gc1->id
, $messages);
6394 * Test getting a conversation that doesn't exist.
6396 public function test_get_conversation_no_conversation() {
6397 $this->resetAfterTest();
6399 $user1 = self
::getDataGenerator()->create_user();
6400 $user2 = self
::getDataGenerator()->create_user();
6402 $name = 'lol conversation';
6403 $conversation = \core_message\api
::create_conversation(
6404 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
6411 $conversationid = $conversation->id
;
6413 $this->setUser($user1);
6415 $this->expectException('moodle_exception');
6416 $conv = core_message_external
::get_conversation($user1->id
, $conversationid +
1);
6417 external_api
::clean_returnvalue(core_message_external
::get_conversation_returns(), $conv);
6421 * Test verifying that the correct favourite information is returned for a non-linked converastion at user context.
6423 public function test_get_conversation_favourited() {
6424 $this->resetAfterTest();
6426 $user1 = self
::getDataGenerator()->create_user();
6427 $user2 = self
::getDataGenerator()->create_user();
6429 // Create a conversation between the 2 users.
6430 $conversation = \core_message\api
::create_conversation(
6431 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
6436 'An individual conversation'
6439 // Favourite the conversation as user 1 only.
6440 \core_message\api
::set_favourite_conversation($conversation->id
, $user1->id
);
6442 // Get the conversation for user1 and confirm it's favourited.
6443 $this->setUser($user1);
6444 $conv = core_message_external
::get_conversation($user1->id
, $conversation->id
);
6445 $conv = external_api
::clean_returnvalue(core_message_external
::get_conversation_returns(), $conv);
6446 $this->assertTrue($conv['isfavourite']);
6448 // Get the conversation for user2 and confirm it's NOT favourited.
6449 $this->setUser($user2);
6450 $conv = core_message_external
::get_conversation($user2->id
, $conversation->id
);
6451 $conv = external_api
::clean_returnvalue(core_message_external
::get_conversation_returns(), $conv);
6452 $this->assertFalse($conv['isfavourite']);
6456 * Test verifying that the correct favourite information is returned for a group-linked conversation at course context.
6458 public function test_get_conversation_favourited_group_linked() {
6459 $this->resetAfterTest();
6462 $user1 = self
::getDataGenerator()->create_user();
6463 $user2 = self
::getDataGenerator()->create_user();
6464 $user3 = self
::getDataGenerator()->create_user();
6466 $course1 = $this->getDataGenerator()->create_course();
6467 $course1context = \context_course
::instance($course1->id
);
6469 // Create a group with a linked conversation and a valid image.
6470 $this->setAdminUser();
6471 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
6472 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
6473 $this->getDataGenerator()->enrol_user($user3->id
, $course1->id
);
6474 $group1 = $this->getDataGenerator()->create_group([
6475 'courseid' => $course1->id
,
6476 'enablemessaging' => 1
6479 // Add users to group1.
6480 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
6481 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
6483 // Verify that the conversation is a group linked conversation in the course context.
6484 $conversationrecord = $DB->get_record('message_conversations', ['component' => 'core_group', 'itemtype' => 'groups']);
6485 $this->assertEquals($course1context->id
, $conversationrecord->contextid
);
6487 // Favourite the conversation as user 1 only.
6488 \core_message\api
::set_favourite_conversation($conversationrecord->id
, $user1->id
);
6490 // Get the conversation for user1 and confirm it's favourited.
6491 $this->setUser($user1);
6492 $conv = core_message_external
::get_conversation($user1->id
, $conversationrecord->id
);
6493 $conv = external_api
::clean_returnvalue(core_message_external
::get_conversation_returns(), $conv);
6494 $this->assertTrue($conv['isfavourite']);
6496 // Get the conversation for user2 and confirm it's NOT favourited.
6497 $this->setUser($user2);
6498 $conv = core_message_external
::get_conversation($user2->id
, $conversationrecord->id
);
6499 $conv = external_api
::clean_returnvalue(core_message_external
::get_conversation_returns(), $conv);
6500 $this->assertFalse($conv['isfavourite']);
6504 * Test getting a conversation with no messages.
6506 public function test_get_conversation_no_messages() {
6507 $this->resetAfterTest();
6509 $user1 = self
::getDataGenerator()->create_user();
6510 $user2 = self
::getDataGenerator()->create_user();
6512 $name = 'lol conversation';
6513 $conversation = \core_message\api
::create_conversation(
6514 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
6521 $conversationid = $conversation->id
;
6523 $this->setUser($user1);
6525 $conv = core_message_external
::get_conversation($user1->id
, $conversationid);
6526 external_api
::clean_returnvalue(core_message_external
::get_conversation_returns(), $conv);
6528 $conv = (array) $conv;
6529 $this->assertEquals($conversationid, $conv['id']);
6530 $this->assertEquals($name, $conv['name']);
6531 $this->assertArrayHasKey('subname', $conv);
6532 $this->assertEquals(\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
, $conv['type']);
6533 $this->assertEquals(2, $conv['membercount']);
6534 $this->assertEquals(false, $conv['isfavourite']);
6535 $this->assertEquals(true, $conv['isread']);
6536 $this->assertEquals(0, $conv['unreadcount']);
6537 $this->assertCount(1, $conv['members']);
6538 foreach ($conv['members'] as $member) {
6539 $member = (array) $member;
6540 $this->assertArrayHasKey('id', $member);
6541 $this->assertArrayHasKey('fullname', $member);
6542 $this->assertArrayHasKey('profileimageurl', $member);
6543 $this->assertArrayHasKey('profileimageurlsmall', $member);
6544 $this->assertArrayHasKey('isonline', $member);
6545 $this->assertArrayHasKey('showonlinestatus', $member);
6546 $this->assertArrayHasKey('isblocked', $member);
6547 $this->assertArrayHasKey('iscontact', $member);
6549 $this->assertEmpty($conv['messages']);
6553 * Test getting a conversation with messages.
6555 public function test_get_conversation_with_messages() {
6556 $this->resetAfterTest();
6558 $user1 = self
::getDataGenerator()->create_user();
6559 $user2 = self
::getDataGenerator()->create_user();
6560 $user3 = self
::getDataGenerator()->create_user();
6562 // Some random conversation.
6563 $otherconversation = \core_message\api
::create_conversation(
6564 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
6571 $conversation = \core_message\api
::create_conversation(
6572 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
6578 $conversationid = $conversation->id
;
6581 $message1id = testhelper
::send_fake_message_to_conversation($user1, $conversation->id
, 'A', $time - 10);
6582 $message2id = testhelper
::send_fake_message_to_conversation($user2, $conversation->id
, 'B', $time - 5);
6583 $message3id = testhelper
::send_fake_message_to_conversation($user1, $conversation->id
, 'C', $time);
6585 // Add some messages to the other convo to make sure they aren't included.
6586 testhelper
::send_fake_message_to_conversation($user1, $otherconversation->id
, 'foo');
6588 $this->setUser($user1);
6590 // Test newest first.
6591 $conv = core_message_external
::get_conversation(
6602 external_api
::clean_returnvalue(core_message_external
::get_conversation_returns(), $conv);
6604 $conv = (array) $conv;
6605 $this->assertEquals(false, $conv['isread']);
6606 $this->assertEquals(1, $conv['unreadcount']);
6607 $this->assertCount(3, $conv['messages']);
6608 $this->assertEquals($message3id, $conv['messages'][0]->id
);
6609 $this->assertEquals($user1->id
, $conv['messages'][0]->useridfrom
);
6610 $this->assertEquals($message2id, $conv['messages'][1]->id
);
6611 $this->assertEquals($user2->id
, $conv['messages'][1]->useridfrom
);
6612 $this->assertEquals($message1id, $conv['messages'][2]->id
);
6613 $this->assertEquals($user1->id
, $conv['messages'][2]->useridfrom
);
6615 // Test newest last.
6616 $conv = core_message_external
::get_conversation(
6627 external_api
::clean_returnvalue(core_message_external
::get_conversation_returns(), $conv);
6629 $conv = (array) $conv;
6630 $this->assertCount(3, $conv['messages']);
6631 $this->assertEquals($message3id, $conv['messages'][2]->id
);
6632 $this->assertEquals($user1->id
, $conv['messages'][2]->useridfrom
);
6633 $this->assertEquals($message2id, $conv['messages'][1]->id
);
6634 $this->assertEquals($user2->id
, $conv['messages'][1]->useridfrom
);
6635 $this->assertEquals($message1id, $conv['messages'][0]->id
);
6636 $this->assertEquals($user1->id
, $conv['messages'][0]->useridfrom
);
6638 // Test message offest and limit.
6639 $conv = core_message_external
::get_conversation(
6650 external_api
::clean_returnvalue(core_message_external
::get_conversation_returns(), $conv);
6652 $conv = (array) $conv;
6653 $this->assertCount(1, $conv['messages']);
6654 $this->assertEquals($message2id, $conv['messages'][0]->id
);
6655 $this->assertEquals($user2->id
, $conv['messages'][0]->useridfrom
);
6659 * Data provider for test_get_conversation_counts().
6661 public function test_get_conversation_counts_test_cases() {
6662 $typeindividual = \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
;
6663 $typegroup = \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
;
6664 $typeself = \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
;
6665 list($user1, $user2, $user3, $user4, $user5, $user6, $user7, $user8) = [0, 1, 2, 3, 4, 5, 6, 7];
6668 'type' => $typeindividual,
6669 'users' => [$user1, $user2],
6670 'messages' => [$user1, $user2, $user2],
6671 'favourites' => [$user1],
6672 'enabled' => null // Individual conversations cannot be disabled.
6675 'type' => $typeindividual,
6676 'users' => [$user1, $user3],
6677 'messages' => [$user1, $user3, $user1],
6679 'enabled' => null // Individual conversations cannot be disabled.
6682 'type' => $typegroup,
6683 'users' => [$user1, $user2, $user3, $user4],
6684 'messages' => [$user1, $user2, $user3, $user4],
6689 'type' => $typegroup,
6690 'users' => [$user2, $user3, $user4],
6691 'messages' => [$user2, $user3, $user4],
6696 'type' => $typegroup,
6697 'users' => [$user6, $user7],
6698 'messages' => [$user6, $user7, $user7],
6699 'favourites' => [$user6],
6703 'type' => $typeself,
6704 'users' => [$user8],
6705 'messages' => [$user8],
6707 'enabled' => null // Individual conversations cannot be disabled.
6712 'No conversations' => [
6713 'conversationConfigs' => $conversations,
6714 'deletemessagesuser' => null,
6715 'deletemessages' => [],
6716 'arguments' => [$user5],
6717 'expectedcounts' => ['favourites' => 1, 'types' => [
6718 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6719 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 0,
6720 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6722 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6723 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6724 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 0,
6725 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6727 'deletedusers' => []
6729 'No individual conversations, 2 group conversations' => [
6730 'conversationConfigs' => $conversations,
6731 'deletemessagesuser' => null,
6732 'deletemessages' => [],
6733 'arguments' => [$user4],
6734 'expectedcounts' => ['favourites' => 1, 'types' => [
6735 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6736 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6737 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6739 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6740 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6741 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6742 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6744 'deletedusers' => []
6746 '2 individual conversations (one favourited), 1 group conversation' => [
6747 'conversationConfigs' => $conversations,
6748 'deletemessagesuser' => null,
6749 'deletemessages' => [],
6750 'arguments' => [$user1],
6751 'expectedcounts' => ['favourites' => 2, 'types' => [
6752 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6753 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6754 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6756 'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6757 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6758 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6759 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6761 'deletedusers' => []
6763 '1 individual conversation, 2 group conversations' => [
6764 'conversationConfigs' => $conversations,
6765 'deletemessagesuser' => null,
6766 'deletemessages' => [],
6767 'arguments' => [$user2],
6768 'expectedcounts' => ['favourites' => 1, 'types' => [
6769 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6770 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6771 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6773 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6774 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6775 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6776 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6778 'deletedusers' => []
6780 '2 group conversations only' => [
6781 'conversationConfigs' => $conversations,
6782 'deletemessagesuser' => null,
6783 'deletemessages' => [],
6784 'arguments' => [$user4],
6785 'expectedcounts' => ['favourites' => 1, 'types' => [
6786 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6787 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6788 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6790 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6791 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6792 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6793 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6795 'deletedusers' => []
6797 'All conversation types, delete a message from individual favourited, messages remaining' => [
6798 'conversationConfigs' => $conversations,
6799 'deletemessagesuser' => $user1,
6800 'deletemessages' => [0],
6801 'arguments' => [$user1],
6802 'expectedcounts' => ['favourites' => 2, 'types' => [
6803 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6804 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6805 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6807 'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6808 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6809 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6810 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6812 'deletedusers' => []
6814 'All conversation types, delete a message from individual non-favourited, messages remaining' => [
6815 'conversationConfigs' => $conversations,
6816 'deletemessagesuser' => $user1,
6817 'deletemessages' => [3],
6818 'arguments' => [$user1],
6819 'expectedcounts' => ['favourites' => 2, 'types' => [
6820 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6821 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6822 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6824 'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6825 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6826 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6827 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6829 'deletedusers' => []
6831 'All conversation types, delete all messages from individual favourited, no messages remaining' => [
6832 'conversationConfigs' => $conversations,
6833 'deletemessagesuser' => $user1,
6834 'deletemessages' => [0, 1, 2],
6835 'arguments' => [$user1],
6836 'expectedcounts' => ['favourites' => 1, 'types' => [
6837 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6838 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6839 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6841 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6842 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6843 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6844 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6846 'deletedusers' => []
6848 'All conversation types, delete all messages from individual non-favourited, no messages remaining' => [
6849 'conversationConfigs' => $conversations,
6850 'deletemessagesuser' => $user1,
6851 'deletemessages' => [3, 4, 5],
6852 'arguments' => [$user1],
6853 'expectedcounts' => ['favourites' => 2, 'types' => [
6854 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6855 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6856 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6858 'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6859 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6860 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6861 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6863 'deletedusers' => []
6865 'All conversation types, delete all messages from individual favourited, no messages remaining, different user' => [
6866 'conversationConfigs' => $conversations,
6867 'deletemessagesuser' => $user1,
6868 'deletemessages' => [0, 1, 2],
6869 'arguments' => [$user2],
6870 'expectedcounts' => ['favourites' => 1, 'types' => [
6871 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6872 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6873 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6875 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6876 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6877 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6878 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6880 'deletedusers' => []
6882 'All conversation types, delete all messages from individual non-favourited, no messages remaining, different user' => [
6883 'conversationConfigs' => $conversations,
6884 'deletemessagesuser' => $user1,
6885 'deletemessages' => [3, 4, 5],
6886 'arguments' => [$user3],
6887 'expectedcounts' => ['favourites' => 1, 'types' => [
6888 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6889 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6890 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6892 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6893 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6894 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 2,
6895 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6897 'deletedusers' => []
6899 'All conversation types, delete some messages from group non-favourited, messages remaining,' => [
6900 'conversationConfigs' => $conversations,
6901 'deletemessagesuser' => $user1,
6902 'deletemessages' => [6, 7],
6903 'arguments' => [$user1],
6904 'expectedcounts' => ['favourites' => 2, 'types' => [
6905 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6906 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6907 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6909 'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6910 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6911 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6912 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6914 'deletedusers' => []
6916 'All conversation types, delete all messages from group non-favourited, no messages remaining,' => [
6917 'conversationConfigs' => $conversations,
6918 'deletemessagesuser' => $user1,
6919 'deletemessages' => [6, 7, 8, 9],
6920 'arguments' => [$user1],
6921 'expectedcounts' => ['favourites' => 2, 'types' => [
6922 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6923 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6924 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6926 'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6927 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6928 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 0,
6929 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6931 'deletedusers' => []
6933 'All conversation types, another user soft deleted' => [
6934 'conversationConfigs' => $conversations,
6935 'deletemessagesuser' => null,
6936 'deletemessages' => [],
6937 'arguments' => [$user1],
6938 'expectedcounts' => ['favourites' => 2, 'types' => [
6939 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6940 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6941 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6943 'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6944 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6945 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6946 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6948 'deletedusers' => [$user2]
6950 'All conversation types, all group users soft deleted' => [
6951 'conversationConfigs' => $conversations,
6952 'deletemessagesuser' => null,
6953 'deletemessages' => [],
6954 'arguments' => [$user1],
6955 'expectedcounts' => ['favourites' => 2, 'types' => [
6956 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6957 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6958 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6960 'expectedunreadcounts' => ['favourites' => 1, 'types' => [
6961 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 1,
6962 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 1,
6963 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6965 'deletedusers' => [$user2, $user3, $user4]
6967 'Group conversation which is disabled, favourited' => [
6968 'conversationConfigs' => $conversations,
6969 'deletemessagesuser' => null,
6970 'deletemessages' => [],
6971 'arguments' => [$user6],
6972 'expectedcounts' => ['favourites' => 1, 'types' => [
6973 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6974 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 0,
6975 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6977 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6978 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6979 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 0,
6980 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6982 'deletedusers' => []
6984 'Group conversation which is disabled, non-favourited' => [
6985 'conversationConfigs' => $conversations,
6986 'deletemessagesuser' => null,
6987 'deletemessages' => [],
6988 'arguments' => [$user7],
6989 'expectedcounts' => ['favourites' => 1, 'types' => [
6990 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6991 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 0,
6992 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6994 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
6995 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
6996 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 0,
6997 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
6999 'deletedusers' => []
7001 'Conversation with self' => [
7002 'conversationConfigs' => $conversations,
7003 'deletemessagesuser' => null,
7004 'deletemessages' => [],
7005 'arguments' => [$user8],
7006 'expectedcounts' => ['favourites' => 0, 'types' => [
7007 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
7008 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 0,
7009 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 1
7011 'expectedunreadcounts' => ['favourites' => 0, 'types' => [
7012 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
=> 0,
7013 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
=> 0,
7014 \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
=> 0
7016 'deletedusers' => []
7022 * Test the get_conversation_counts() function.
7024 * @dataProvider test_get_conversation_counts_test_cases()
7025 * @param array $conversationconfigs Conversations to create
7026 * @param int $deletemessagesuser The user who is deleting the messages
7027 * @param array $deletemessages The list of messages to delete (by index)
7028 * @param array $arguments Arguments for the count conversations function
7029 * @param array $expectedcounts the expected conversation counts
7030 * @param array $expectedunreadcounts the expected unread conversation counts
7031 * @param array $deletedusers the array of users to soft delete.
7033 public function test_get_conversation_counts(
7034 $conversationconfigs,
7035 $deletemessagesuser,
7039 $expectedunreadcounts,
7042 $this->resetAfterTest();
7043 $generator = $this->getDataGenerator();
7045 $generator->create_user(),
7046 $generator->create_user(),
7047 $generator->create_user(),
7048 $generator->create_user(),
7049 $generator->create_user(),
7050 $generator->create_user(),
7051 $generator->create_user(),
7052 $generator->create_user()
7055 $deleteuser = !is_null($deletemessagesuser) ?
$users[$deletemessagesuser] : null;
7056 $this->setUser($users[$arguments[0]]);
7057 $arguments[0] = $users[$arguments[0]]->id
;
7058 $systemcontext = \context_system
::instance();
7059 $conversations = [];
7062 foreach ($conversationconfigs as $config) {
7063 $conversation = \core_message\api
::create_conversation(
7065 array_map(function($userindex) use ($users) {
7066 return $users[$userindex]->id
;
7067 }, $config['users']),
7069 ($config['enabled'] ??
true)
7072 foreach ($config['messages'] as $userfromindex) {
7073 $userfrom = $users[$userfromindex];
7074 $messageids[] = testhelper
::send_fake_message_to_conversation($userfrom, $conversation->id
);
7077 // Remove the self conversations created by the generator,
7078 // so we can choose to set that ourself and honour the original intention of the test.
7079 $userids = array_map(function($userindex) use ($users) {
7080 return $users[$userindex]->id
;
7081 }, $config['users']);
7082 foreach ($userids as $userid) {
7083 if ($conversation->type
== \core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
) {
7084 \core_message\api
::unset_favourite_conversation($conversation->id
, $userid);
7088 foreach ($config['favourites'] as $userfromindex) {
7089 $userfrom = $users[$userfromindex];
7090 $usercontext = \context_user
::instance($userfrom->id
);
7091 $ufservice = \core_favourites\service_factory
::get_service_for_user_context($usercontext);
7092 $ufservice->create_favourite('core_message', 'message_conversations', $conversation->id
, $systemcontext);
7095 $conversations[] = $conversation;
7098 foreach ($deletemessages as $messageindex) {
7099 \core_message\api
::delete_message($deleteuser->id
, $messageids[$messageindex]);
7102 foreach ($deletedusers as $deleteduser) {
7103 delete_user($users[$deleteduser]);
7106 $counts = core_message_external
::get_conversation_counts(...$arguments);
7107 $counts = external_api
::clean_returnvalue(core_message_external
::get_conversation_counts_returns(), $counts);
7109 $this->assertEquals($expectedcounts['favourites'], $counts['favourites']);
7110 $this->assertEquals($expectedcounts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
],
7111 $counts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
]);
7112 $this->assertEquals($expectedcounts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
],
7113 $counts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
]);
7114 $this->assertEquals($expectedcounts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
],
7115 $counts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
]);
7119 * Test the get_unread_conversation_counts() function.
7121 * @dataProvider test_get_conversation_counts_test_cases()
7122 * @param array $conversationconfigs Conversations to create
7123 * @param int $deletemessagesuser The user who is deleting the messages
7124 * @param array $deletemessages The list of messages to delete (by index)
7125 * @param array $arguments Arguments for the count conversations function
7126 * @param array $expectedcounts the expected conversation counts
7127 * @param array $expectedunreadcounts the expected unread conversation counts
7128 * @param array $deletedusers the list of users to soft-delete.
7130 public function test_get_unread_conversation_counts(
7131 $conversationconfigs,
7132 $deletemessagesuser,
7136 $expectedunreadcounts,
7139 $this->resetAfterTest();
7140 $generator = $this->getDataGenerator();
7142 $generator->create_user(),
7143 $generator->create_user(),
7144 $generator->create_user(),
7145 $generator->create_user(),
7146 $generator->create_user(),
7147 $generator->create_user(),
7148 $generator->create_user(),
7149 $generator->create_user()
7152 $deleteuser = !is_null($deletemessagesuser) ?
$users[$deletemessagesuser] : null;
7153 $this->setUser($users[$arguments[0]]);
7154 $arguments[0] = $users[$arguments[0]]->id
;
7155 $systemcontext = \context_system
::instance();
7156 $conversations = [];
7159 foreach ($conversationconfigs as $config) {
7160 $conversation = \core_message\api
::create_conversation(
7162 array_map(function($userindex) use ($users) {
7163 return $users[$userindex]->id
;
7164 }, $config['users']),
7166 ($config['enabled'] ??
true)
7169 foreach ($config['messages'] as $userfromindex) {
7170 $userfrom = $users[$userfromindex];
7171 $messageids[] = testhelper
::send_fake_message_to_conversation($userfrom, $conversation->id
);
7174 foreach ($config['favourites'] as $userfromindex) {
7175 $userfrom = $users[$userfromindex];
7176 $usercontext = \context_user
::instance($userfrom->id
);
7177 $ufservice = \core_favourites\service_factory
::get_service_for_user_context($usercontext);
7178 $ufservice->create_favourite('core_message', 'message_conversations', $conversation->id
, $systemcontext);
7181 $conversations[] = $conversation;
7184 foreach ($deletemessages as $messageindex) {
7185 \core_message\api
::delete_message($deleteuser->id
, $messageids[$messageindex]);
7188 foreach ($deletedusers as $deleteduser) {
7189 delete_user($users[$deleteduser]);
7192 $counts = core_message_external
::get_unread_conversation_counts(...$arguments);
7193 $counts = external_api
::clean_returnvalue(core_message_external
::get_unread_conversation_counts_returns(), $counts);
7195 $this->assertEquals($expectedunreadcounts['favourites'], $counts['favourites']);
7196 $this->assertEquals($expectedunreadcounts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
],
7197 $counts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
]);
7198 $this->assertEquals($expectedunreadcounts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
],
7199 $counts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
]);
7200 $this->assertEquals($expectedunreadcounts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
],
7201 $counts['types'][\core_message\api
::MESSAGE_CONVERSATION_TYPE_SELF
]);
7205 * Test delete_message for all users.
7207 public function test_delete_message_for_all_users() {
7210 $this->resetAfterTest(true);
7212 // Create fake data to test it.
7213 list($user1, $user2, $user3, $convgroup, $convindividual) = $this->create_delete_message_test_data();
7215 // Send message as user1 to group conversation.
7216 $messageid1 = testhelper
::send_fake_message_to_conversation($user1, $convgroup->id
);
7217 $messageid2 = testhelper
::send_fake_message_to_conversation($user2, $convgroup->id
);
7219 // User1 deletes the first message for all users of group conversation.
7220 // First, we have to allow user1 (Teacher) can delete messages for all users.
7221 $editingteacher = $DB->get_record('role', ['shortname' => 'editingteacher']);
7222 assign_capability('moodle/site:deleteanymessage', CAP_ALLOW
, $editingteacher->id
, context_system
::instance());
7224 $this->setUser($user1);
7226 // Now, user1 deletes message for all users.
7227 $return = core_message_external
::delete_message_for_all_users($messageid1, $user1->id
);
7228 $return = external_api
::clean_returnvalue(core_message_external
::delete_message_for_all_users_returns(), $return);
7229 // Check if everything is ok.
7230 $this->assertEquals(array(), $return);
7232 // Check we have 3 records on message_user_actions with the mark MESSAGE_ACTION_DELETED.
7233 $muas = $DB->get_records('message_user_actions', array('messageid' => $messageid1), 'userid ASC');
7234 $this->assertCount(3, $muas);
7235 $mua1 = array_shift($muas);
7236 $mua2 = array_shift($muas);
7237 $mua3 = array_shift($muas);
7239 $this->assertEquals($user1->id
, $mua1->userid
);
7240 $this->assertEquals($messageid1, $mua1->messageid
);
7241 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua1->action
);
7242 $this->assertEquals($user2->id
, $mua2->userid
);
7243 $this->assertEquals($messageid1, $mua2->messageid
);
7244 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua2->action
);
7245 $this->assertEquals($user3->id
, $mua3->userid
);
7246 $this->assertEquals($messageid1, $mua3->messageid
);
7247 $this->assertEquals(\core_message\api
::MESSAGE_ACTION_DELETED
, $mua3->action
);
7251 * Test delete_message for all users with messaging disabled.
7253 public function test_delete_message_for_all_users_messaging_disabled() {
7256 $this->resetAfterTest();
7258 // Create fake data to test it.
7259 list($user1, $user2, $user3, $convgroup, $convindividual) = $this->create_delete_message_test_data();
7261 // Send message as user1 to group conversation.
7262 $messageid = testhelper
::send_fake_message_to_conversation($user1, $convgroup->id
);
7264 $this->setUser($user1);
7266 // Disable messaging.
7267 $CFG->messaging
= 0;
7269 // Ensure an exception is thrown.
7270 $this->expectException('moodle_exception');
7271 core_message_external
::delete_message_for_all_users($messageid, $user1->id
);
7275 * Test delete_message for all users with no permission.
7277 public function test_delete_message_for_all_users_no_permission() {
7278 $this->resetAfterTest();
7280 // Create fake data to test it.
7281 list($user1, $user2, $user3, $convgroup, $convindividual) = $this->create_delete_message_test_data();
7283 // Send message as user1 to group conversation.
7284 $messageid = testhelper
::send_fake_message_to_conversation($user1, $convgroup->id
);
7286 $this->setUser($user2);
7288 // Try as user2 to delete a message for all users without permission to do it.
7289 $this->expectException('moodle_exception');
7290 $this->expectExceptionMessage('You do not have permission to delete this message for everyone.');
7291 core_message_external
::delete_message_for_all_users($messageid, $user2->id
);
7295 * Test delete_message for all users in a private conversation.
7297 public function test_delete_message_for_all_users_private_conversation() {
7300 $this->resetAfterTest();
7302 // Create fake data to test it.
7303 list($user1, $user2, $user3, $convgroup, $convindividual) = $this->create_delete_message_test_data();
7305 // Send message as user1 to private conversation.
7306 $messageid = testhelper
::send_fake_message_to_conversation($user1, $convindividual->id
);
7308 // First, we have to allow user1 (Teacher) can delete messages for all users.
7309 $editingteacher = $DB->get_record('role', ['shortname' => 'editingteacher']);
7310 assign_capability('moodle/site:deleteanymessage', CAP_ALLOW
, $editingteacher->id
, context_system
::instance());
7312 $this->setUser($user1);
7314 // Try as user1 to delete a private message for all users on individual conversation.
7315 // User1 should not delete message for all users in a private conversations despite being a teacher.
7316 // Because is a teacher in a course and not in a system context.
7317 $this->expectException('moodle_exception');
7318 $this->expectExceptionMessage('You do not have permission to delete this message for everyone.');
7319 core_message_external
::delete_message_for_all_users($messageid, $user1->id
);
7323 * Helper to seed the database with initial state with data.
7325 protected function create_delete_message_test_data() {
7326 // Create some users.
7327 $user1 = self
::getDataGenerator()->create_user();
7328 $user2 = self
::getDataGenerator()->create_user();
7329 $user3 = self
::getDataGenerator()->create_user();
7331 // Create a course and enrol the users.
7332 $course = $this->getDataGenerator()->create_course();
7333 $coursecontext = context_course
::instance($course->id
);
7334 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
, 'editingteacher');
7335 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
, 'student');
7336 $this->getDataGenerator()->enrol_user($user3->id
, $course->id
, 'student');
7338 // Create a group and added the users into.
7339 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
7340 groups_add_member($group1->id
, $user1->id
);
7341 groups_add_member($group1->id
, $user2->id
);
7342 groups_add_member($group1->id
, $user3->id
);
7344 // Create a group conversation linked with the course.
7345 $convgroup = \core_message\api
::create_conversation(
7346 \core_message\api
::MESSAGE_CONVERSATION_TYPE_GROUP
,
7347 [$user1->id
, $user2->id
, $user3->id
],
7348 'Group test delete for everyone', \core_message\api
::MESSAGE_CONVERSATION_ENABLED
,
7352 context_course
::instance($course->id
)->id
7355 // Create and individual conversation.
7356 $convindividual = \core_message\api
::create_conversation(
7357 \core_message\api
::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL
,
7358 [$user1->id
, $user2->id
]
7361 return [$user1, $user2, $user3, $convgroup, $convindividual];