Merge branch 'MDL-63632-master' of git://github.com/andrewnicols/moodle
[moodle.git] / message / tests / api_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Test message API.
19  *
20  * @package core_message
21  * @category test
22  * @copyright 2016 Mark Nelson <markn@moodle.com>
23  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
32 /**
33  * Test message API.
34  *
35  * @package core_message
36  * @category test
37  * @copyright 2016 Mark Nelson <markn@moodle.com>
38  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class core_message_api_testcase extends core_message_messagelib_testcase {
42     public function test_mark_all_read_for_user_touser() {
43         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
44         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
46         $this->send_fake_message($sender, $recipient, 'Notification', 1);
47         $this->send_fake_message($sender, $recipient, 'Notification', 1);
48         $this->send_fake_message($sender, $recipient, 'Notification', 1);
49         $this->send_fake_message($sender, $recipient);
50         $this->send_fake_message($sender, $recipient);
51         $this->send_fake_message($sender, $recipient);
53         \core_message\api::mark_all_read_for_user($recipient->id);
54         $this->assertDebuggingCalled();
55         $this->assertEquals(message_count_unread_messages($recipient), 0);
56     }
58     public function test_mark_all_read_for_user_touser_with_fromuser() {
59         $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
60         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
61         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
63         $this->send_fake_message($sender1, $recipient, 'Notification', 1);
64         $this->send_fake_message($sender1, $recipient, 'Notification', 1);
65         $this->send_fake_message($sender1, $recipient, 'Notification', 1);
66         $this->send_fake_message($sender1, $recipient);
67         $this->send_fake_message($sender1, $recipient);
68         $this->send_fake_message($sender1, $recipient);
69         $this->send_fake_message($sender2, $recipient, 'Notification', 1);
70         $this->send_fake_message($sender2, $recipient, 'Notification', 1);
71         $this->send_fake_message($sender2, $recipient, 'Notification', 1);
72         $this->send_fake_message($sender2, $recipient);
73         $this->send_fake_message($sender2, $recipient);
74         $this->send_fake_message($sender2, $recipient);
76         \core_message\api::mark_all_read_for_user($recipient->id, $sender1->id);
77         $this->assertDebuggingCalled();
78         $this->assertEquals(message_count_unread_messages($recipient), 3);
79     }
81     public function test_mark_all_read_for_user_touser_with_type() {
82         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
83         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
85         $this->send_fake_message($sender, $recipient, 'Notification', 1);
86         $this->send_fake_message($sender, $recipient, 'Notification', 1);
87         $this->send_fake_message($sender, $recipient, 'Notification', 1);
88         $this->send_fake_message($sender, $recipient);
89         $this->send_fake_message($sender, $recipient);
90         $this->send_fake_message($sender, $recipient);
92         \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
93         $this->assertDebuggingCalled();
94         $this->assertEquals(message_count_unread_messages($recipient), 3);
96         \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
97         $this->assertDebuggingCalled();
98         $this->assertEquals(message_count_unread_messages($recipient), 0);
99     }
101     /**
102      * Test count_blocked_users.
103      */
104     public function test_count_blocked_users() {
105         global $USER;
107         // Set this user as the admin.
108         $this->setAdminUser();
110         // Create user to add to the admin's block list.
111         $user1 = $this->getDataGenerator()->create_user();
112         $user2 = $this->getDataGenerator()->create_user();
114         $this->assertEquals(0, \core_message\api::count_blocked_users());
116         // Add 1 blocked user to admin's blocked user list.
117         \core_message\api::block_user($USER->id, $user1->id);
119         $this->assertEquals(0, \core_message\api::count_blocked_users($user1));
120         $this->assertEquals(1, \core_message\api::count_blocked_users());
121     }
123     /**
124      * Tests searching users in a course.
125      */
126     public function test_search_users_in_course() {
127         // Create some users.
128         $user1 = new stdClass();
129         $user1->firstname = 'User';
130         $user1->lastname = 'One';
131         $user1 = self::getDataGenerator()->create_user($user1);
133         // The person doing the search.
134         $this->setUser($user1);
136         // Second user is going to have their last access set to now, so they are online.
137         $user2 = new stdClass();
138         $user2->firstname = 'User';
139         $user2->lastname = 'Two';
140         $user2->lastaccess = time();
141         $user2 = self::getDataGenerator()->create_user($user2);
143         // Block the second user.
144         \core_message\api::block_user($user1->id, $user2->id);
146         $user3 = new stdClass();
147         $user3->firstname = 'User';
148         $user3->lastname = 'Three';
149         $user3 = self::getDataGenerator()->create_user($user3);
151         // Create a course.
152         $course1 = new stdClass();
153         $course1->fullname = 'Course';
154         $course1->shortname = 'One';
155         $course1 = $this->getDataGenerator()->create_course($course1);
157         // Enrol the searcher and one user in the course.
158         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
159         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
161         // Perform a search.
162         $results = \core_message\api::search_users_in_course($user1->id, $course1->id, 'User');
164         $this->assertEquals(1, count($results));
166         $user = $results[0];
167         $this->assertEquals($user2->id, $user->userid);
168         $this->assertEquals(fullname($user2), $user->fullname);
169         $this->assertFalse($user->ismessaging);
170         $this->assertNull($user->lastmessage);
171         $this->assertNull($user->messageid);
172         $this->assertNull($user->isonline);
173         $this->assertFalse($user->isread);
174         $this->assertTrue($user->isblocked);
175         $this->assertNull($user->unreadcount);
176     }
178     /**
179      * Tests searching users.
180      */
181     public function test_search_users() {
182         global $DB;
184         // Create some users.
185         $user1 = new stdClass();
186         $user1->firstname = 'User';
187         $user1->lastname = 'One';
188         $user1 = self::getDataGenerator()->create_user($user1);
190         // Set as the user performing the search.
191         $this->setUser($user1);
193         $user2 = new stdClass();
194         $user2->firstname = 'User search';
195         $user2->lastname = 'Two';
196         $user2 = self::getDataGenerator()->create_user($user2);
198         $user3 = new stdClass();
199         $user3->firstname = 'User search';
200         $user3->lastname = 'Three';
201         $user3 = self::getDataGenerator()->create_user($user3);
203         $user4 = new stdClass();
204         $user4->firstname = 'User';
205         $user4->lastname = 'Four';
206         $user4 = self::getDataGenerator()->create_user($user4);
208         $user5 = new stdClass();
209         $user5->firstname = 'User search';
210         $user5->lastname = 'Five';
211         $user5 = self::getDataGenerator()->create_user($user5);
213         $user6 = new stdClass();
214         $user6->firstname = 'User';
215         $user6->lastname = 'Six';
216         $user6 = self::getDataGenerator()->create_user($user6);
218         // Create some courses.
219         $course1 = new stdClass();
220         $course1->fullname = 'Course search';
221         $course1->shortname = 'One';
222         $course1 = $this->getDataGenerator()->create_course($course1);
224         $course2 = new stdClass();
225         $course2->fullname = 'Course';
226         $course2->shortname = 'Two';
227         $course2 = $this->getDataGenerator()->create_course($course2);
229         $course3 = new stdClass();
230         $course3->fullname = 'Course';
231         $course3->shortname = 'Three search';
232         $course3 = $this->getDataGenerator()->create_course($course3);
234         $course4 = new stdClass();
235         $course4->fullname = 'Course Four';
236         $course4->shortname = 'CF100';
237         $course4 = $this->getDataGenerator()->create_course($course4);
239         $course5 = new stdClass();
240         $course5->fullname = 'Course';
241         $course5->shortname = 'Five search';
242         $course5 = $this->getDataGenerator()->create_course($course5);
244         $role = $DB->get_record('role', ['shortname' => 'student']);
245         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $role->id);
246         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $role->id);
247         $this->getDataGenerator()->enrol_user($user1->id, $course3->id, $role->id);
248         $this->getDataGenerator()->enrol_user($user1->id, $course5->id, $role->id);
250         // Add some users as contacts.
251         \core_message\api::add_contact($user1->id, $user2->id);
252         \core_message\api::add_contact($user1->id, $user3->id);
253         \core_message\api::add_contact($user1->id, $user4->id);
255         // Remove the viewparticipants capability from one of the courses.
256         $course5context = context_course::instance($course5->id);
257         assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $role->id, $course5context->id);
259         // Perform a search.
260         list($contacts, $courses, $noncontacts) = \core_message\api::search_users($user1->id, 'search');
262         // Check that we retrieved the correct contacts.
263         $this->assertEquals(2, count($contacts));
264         $this->assertEquals($user3->id, $contacts[0]->userid);
265         $this->assertEquals($user2->id, $contacts[1]->userid);
267         // Check that we retrieved the correct courses.
268         $this->assertEquals(2, count($courses));
269         $this->assertEquals($course3->id, $courses[0]->id);
270         $this->assertEquals($course1->id, $courses[1]->id);
272         // Check that we retrieved the correct non-contacts.
273         $this->assertEquals(1, count($noncontacts));
274         $this->assertEquals($user5->id, $noncontacts[0]->userid);
275     }
277     /**
278      * Tests searching messages.
279      */
280     public function test_search_messages() {
281         // Create some users.
282         $user1 = self::getDataGenerator()->create_user();
283         $user2 = self::getDataGenerator()->create_user();
284         $user3 = self::getDataGenerator()->create_user();
286         // The person doing the search.
287         $this->setUser($user1);
289         // Send some messages back and forth.
290         $time = 1;
291         $this->send_fake_message($user3, $user1, 'Don\'t block me.', 0, $time);
292         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
293         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
294         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
295         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
297         // Block user 3.
298         \core_message\api::block_user($user1->id, $user3->id);
300         // Perform a search.
301         $messages = \core_message\api::search_messages($user1->id, 'o');
303         // Confirm the data is correct.
304         $this->assertEquals(3, count($messages));
306         $message1 = $messages[0];
307         $message2 = $messages[1];
308         $message3 = $messages[2];
310         $this->assertEquals($user2->id, $message1->userid);
311         $this->assertEquals($user2->id, $message1->useridfrom);
312         $this->assertEquals(fullname($user2), $message1->fullname);
313         $this->assertTrue($message1->ismessaging);
314         $this->assertEquals('Word.', $message1->lastmessage);
315         $this->assertNotEmpty($message1->messageid);
316         $this->assertNull($message1->isonline);
317         $this->assertFalse($message1->isread);
318         $this->assertFalse($message1->isblocked);
319         $this->assertNull($message1->unreadcount);
321         $this->assertEquals($user2->id, $message2->userid);
322         $this->assertEquals($user1->id, $message2->useridfrom);
323         $this->assertEquals(fullname($user2), $message2->fullname);
324         $this->assertTrue($message2->ismessaging);
325         $this->assertEquals('Yo!', $message2->lastmessage);
326         $this->assertNotEmpty($message2->messageid);
327         $this->assertNull($message2->isonline);
328         $this->assertTrue($message2->isread);
329         $this->assertFalse($message2->isblocked);
330         $this->assertNull($message2->unreadcount);
332         $this->assertEquals($user3->id, $message3->userid);
333         $this->assertEquals($user3->id, $message3->useridfrom);
334         $this->assertEquals(fullname($user3), $message3->fullname);
335         $this->assertTrue($message3->ismessaging);
336         $this->assertEquals('Don\'t block me.', $message3->lastmessage);
337         $this->assertNotEmpty($message3->messageid);
338         $this->assertNull($message3->isonline);
339         $this->assertFalse($message3->isread);
340         $this->assertTrue($message3->isblocked);
341         $this->assertNull($message3->unreadcount);
342     }
344     /**
345      * Tests retrieving conversations.
346      */
347     public function test_get_conversations() {
348         // Create some users.
349         $user1 = self::getDataGenerator()->create_user();
350         $user2 = self::getDataGenerator()->create_user();
351         $user3 = self::getDataGenerator()->create_user();
352         $user4 = self::getDataGenerator()->create_user();
354         // The person doing the search.
355         $this->setUser($user1);
357         // No conversations yet.
358         $this->assertEquals([], \core_message\api::get_conversations($user1->id));
360         // Send some messages back and forth, have some different conversations with different users.
361         $time = 1;
362         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
363         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
364         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
365         $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
367         $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
368         $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
369         $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
370         $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
372         $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
373         $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
374         $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
376         // Retrieve the conversations.
377         $conversations = \core_message\api::get_conversations($user1->id);
379         // Confirm the data is correct.
380         $this->assertEquals(3, count($conversations));
382         $message1 = array_shift($conversations);
383         $message2 = array_shift($conversations);
384         $message3 = array_shift($conversations);
386         $this->assertEquals($user4->id, $message1->userid);
387         $this->assertEquals($user1->id, $message1->useridfrom);
388         $this->assertTrue($message1->ismessaging);
389         $this->assertEquals('Dope.', $message1->lastmessage);
390         $this->assertEquals($messageid3, $message1->messageid);
391         $this->assertNull($message1->isonline);
392         $this->assertFalse($message1->isread);
393         $this->assertFalse($message1->isblocked);
394         $this->assertEquals(1, $message1->unreadcount);
396         $this->assertEquals($user3->id, $message2->userid);
397         $this->assertEquals($user3->id, $message2->useridfrom);
398         $this->assertTrue($message2->ismessaging);
399         $this->assertEquals('Cool.', $message2->lastmessage);
400         $this->assertEquals($messageid2, $message2->messageid);
401         $this->assertNull($message2->isonline);
402         $this->assertFalse($message2->isread);
403         $this->assertFalse($message2->isblocked);
404         $this->assertEquals(2, $message2->unreadcount);
406         $this->assertEquals($user2->id, $message3->userid);
407         $this->assertEquals($user2->id, $message3->useridfrom);
408         $this->assertTrue($message3->ismessaging);
409         $this->assertEquals('Word.', $message3->lastmessage);
410         $this->assertEquals($messageid1, $message3->messageid);
411         $this->assertNull($message3->isonline);
412         $this->assertFalse($message3->isread);
413         $this->assertFalse($message3->isblocked);
414         $this->assertEquals(2, $message3->unreadcount);
415     }
417     /**
418      * Tests retrieving conversations with a limit and offset to ensure pagination works correctly.
419      */
420     public function test_get_conversations_limit_offset() {
421         // Create some users.
422         $user1 = self::getDataGenerator()->create_user();
423         $user2 = self::getDataGenerator()->create_user();
424         $user3 = self::getDataGenerator()->create_user();
425         $user4 = self::getDataGenerator()->create_user();
427         // The person doing the search.
428         $this->setUser($user1);
430         // Send some messages back and forth, have some different conversations with different users.
431         $time = 1;
432         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
433         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
434         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
435         $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
437         $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
438         $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
439         $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
440         $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
442         $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
443         $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
444         $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
446         // Retrieve the conversations.
447         $conversations = \core_message\api::get_conversations($user1->id, 1, 1);
449         // We should only have one conversation because of the limit.
450         $this->assertCount(1, $conversations);
452         $conversation = array_shift($conversations);
454         $this->assertEquals($user3->id, $conversation->userid);
455         $this->assertEquals($user3->id, $conversation->useridfrom);
456         $this->assertTrue($conversation->ismessaging);
457         $this->assertEquals('Cool.', $conversation->lastmessage);
458         $this->assertEquals($messageid2, $conversation->messageid);
459         $this->assertNull($conversation->isonline);
460         $this->assertFalse($conversation->isread);
461         $this->assertFalse($conversation->isblocked);
462         $this->assertEquals(2, $conversation->unreadcount);
464         // Retrieve the next conversation.
465         $conversations = \core_message\api::get_conversations($user1->id, 2, 1);
467         // We should only have one conversation because of the limit.
468         $this->assertCount(1, $conversations);
470         $conversation = array_shift($conversations);
472         $this->assertEquals($user2->id, $conversation->userid);
473         $this->assertEquals($user2->id, $conversation->useridfrom);
474         $this->assertTrue($conversation->ismessaging);
475         $this->assertEquals('Word.', $conversation->lastmessage);
476         $this->assertEquals($messageid1, $conversation->messageid);
477         $this->assertNull($conversation->isonline);
478         $this->assertFalse($conversation->isread);
479         $this->assertFalse($conversation->isblocked);
480         $this->assertEquals(2, $conversation->unreadcount);
482         // Ask for an offset that doesn't exist.
483         $conversations = \core_message\api::get_conversations($user1->id, 4, 1);
485         // We should not get any conversations back.
486         $this->assertCount(0, $conversations);
487     }
489     /**
490      * Tests retrieving conversations when a conversation contains a deleted user.
491      */
492     public function test_get_conversations_with_deleted_user() {
493         // Create some users.
494         $user1 = self::getDataGenerator()->create_user();
495         $user2 = self::getDataGenerator()->create_user();
496         $user3 = self::getDataGenerator()->create_user();
498         // Send some messages back and forth, have some different conversations with different users.
499         $time = 1;
500         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
501         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
502         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
503         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
505         $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
506         $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
507         $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
508         $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
510         // Delete the second user.
511         delete_user($user2);
513         // Retrieve the conversations.
514         $conversations = \core_message\api::get_conversations($user1->id);
516         // We should only have one conversation because the other user was deleted.
517         $this->assertCount(1, $conversations);
519         // Confirm the conversation is from the non-deleted user.
520         $conversation = reset($conversations);
521         $this->assertEquals($user3->id, $conversation->userid);
522     }
524    /**
525     * The data provider for get_conversations_mixed.
526     *
527     * This provides sets of data to for testing.
528     * @return array
529     */
530    public function get_conversations_mixed_provider() {
531        return array(
532             'Test that conversations with messages contacts is correctly ordered.' => array(
533                 'users' => array(
534                     'user1',
535                     'user2',
536                     'user3',
537                 ),
538                 'contacts' => array(
539                 ),
540                 'messages' => array(
541                     array(
542                         'from'          => 'user1',
543                         'to'            => 'user2',
544                         'state'         => 'unread',
545                         'subject'       => 'S1',
546                     ),
547                     array(
548                         'from'          => 'user2',
549                         'to'            => 'user1',
550                         'state'         => 'unread',
551                         'subject'       => 'S2',
552                     ),
553                     array(
554                         'from'          => 'user1',
555                         'to'            => 'user2',
556                         'state'         => 'unread',
557                         'timecreated'   => 0,
558                         'subject'       => 'S3',
559                     ),
560                     array(
561                         'from'          => 'user1',
562                         'to'            => 'user3',
563                         'state'         => 'read',
564                         'timemodifier'  => 1,
565                         'subject'       => 'S4',
566                     ),
567                     array(
568                         'from'          => 'user3',
569                         'to'            => 'user1',
570                         'state'         => 'read',
571                         'timemodifier'  => 1,
572                         'subject'       => 'S5',
573                     ),
574                     array(
575                         'from'          => 'user1',
576                         'to'            => 'user3',
577                         'state'         => 'read',
578                         'timecreated'   => 0,
579                         'subject'       => 'S6',
580                     ),
581                 ),
582                 'expectations' => array(
583                     'user1' => array(
584                         // User1 has conversed most recently with user3. The most recent message is M5.
585                         array(
586                             'messageposition'   => 0,
587                             'with'              => 'user3',
588                             'subject'           => 'S5',
589                             'unreadcount'       => 0,
590                         ),
591                         // User1 has also conversed with user2. The most recent message is S2.
592                         array(
593                             'messageposition'   => 1,
594                             'with'              => 'user2',
595                             'subject'           => 'S2',
596                             'unreadcount'       => 1,
597                         ),
598                     ),
599                     'user2' => array(
600                         // User2 has only conversed with user1. Their most recent shared message was S2.
601                         array(
602                             'messageposition'   => 0,
603                             'with'              => 'user1',
604                             'subject'           => 'S2',
605                             'unreadcount'       => 2,
606                         ),
607                     ),
608                     'user3' => array(
609                         // User3 has only conversed with user1. Their most recent shared message was S5.
610                         array(
611                             'messageposition'   => 0,
612                             'with'              => 'user1',
613                             'subject'           => 'S5',
614                             'unreadcount'       => 0,
615                         ),
616                     ),
617                 ),
618             ),
619             'Test that users with contacts and messages to self work as expected' => array(
620                 'users' => array(
621                     'user1',
622                     'user2',
623                     'user3',
624                 ),
625                 'contacts' => array(
626                     'user1' => array(
627                         'user2' => 0,
628                         'user3' => 0,
629                     ),
630                     'user2' => array(
631                         'user3' => 0,
632                     ),
633                 ),
634                 'messages' => array(
635                     array(
636                         'from'          => 'user1',
637                         'to'            => 'user1',
638                         'state'         => 'unread',
639                         'subject'       => 'S1',
640                     ),
641                     array(
642                         'from'          => 'user1',
643                         'to'            => 'user1',
644                         'state'         => 'unread',
645                         'subject'       => 'S2',
646                     ),
647                 ),
648                 'expectations' => array(
649                     'user1' => array(
650                         // User1 has conversed most recently with user1. The most recent message is S2.
651                         array(
652                             'messageposition'   => 0,
653                             'with'              => 'user1',
654                             'subject'           => 'S2',
655                             'unreadcount'       => 0, // Messages sent to and from the same user are counted as read.
656                         ),
657                     ),
658                 ),
659             ),
660             'Test conversations with a single user, where some messages are read and some are not.' => array(
661                 'users' => array(
662                     'user1',
663                     'user2',
664                 ),
665                 'contacts' => array(
666                 ),
667                 'messages' => array(
668                     array(
669                         'from'          => 'user1',
670                         'to'            => 'user2',
671                         'state'         => 'read',
672                         'subject'       => 'S1',
673                     ),
674                     array(
675                         'from'          => 'user2',
676                         'to'            => 'user1',
677                         'state'         => 'read',
678                         'subject'       => 'S2',
679                     ),
680                     array(
681                         'from'          => 'user1',
682                         'to'            => 'user2',
683                         'state'         => 'unread',
684                         'timemodifier'  => 1,
685                         'subject'       => 'S3',
686                     ),
687                     array(
688                         'from'          => 'user1',
689                         'to'            => 'user2',
690                         'state'         => 'unread',
691                         'timemodifier'  => 1,
692                         'subject'       => 'S4',
693                     ),
694                 ),
695                 'expectations' => array(
696                     // The most recent message between user1 and user2 was S4.
697                     'user1' => array(
698                         array(
699                             'messageposition'   => 0,
700                             'with'              => 'user2',
701                             'subject'           => 'S4',
702                             'unreadcount'       => 0,
703                         ),
704                     ),
705                     'user2' => array(
706                         // The most recent message between user1 and user2 was S4.
707                         array(
708                             'messageposition'   => 0,
709                             'with'              => 'user1',
710                             'subject'           => 'S4',
711                             'unreadcount'       => 2,
712                         ),
713                     ),
714                 ),
715             ),
716             'Test conversations with a single user, where some messages are read and some are not, and messages ' .
717             'are out of order' => array(
718             // This can happen through a combination of factors including multi-master DB replication with messages
719             // read somehow (e.g. API).
720                 'users' => array(
721                     'user1',
722                     'user2',
723                 ),
724                 'contacts' => array(
725                 ),
726                 'messages' => array(
727                     array(
728                         'from'          => 'user1',
729                         'to'            => 'user2',
730                         'state'         => 'read',
731                         'subject'       => 'S1',
732                         'timemodifier'  => 1,
733                     ),
734                     array(
735                         'from'          => 'user2',
736                         'to'            => 'user1',
737                         'state'         => 'read',
738                         'subject'       => 'S2',
739                         'timemodifier'  => 2,
740                     ),
741                     array(
742                         'from'          => 'user1',
743                         'to'            => 'user2',
744                         'state'         => 'unread',
745                         'subject'       => 'S3',
746                     ),
747                     array(
748                         'from'          => 'user1',
749                         'to'            => 'user2',
750                         'state'         => 'unread',
751                         'subject'       => 'S4',
752                     ),
753                 ),
754                 'expectations' => array(
755                     // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
756                     'user1' => array(
757                         array(
758                             'messageposition'   => 0,
759                             'with'              => 'user2',
760                             'subject'           => 'S2',
761                             'unreadcount'       => 0,
762                         ),
763                     ),
764                     'user2' => array(
765                         array(
766                             'messageposition'   => 0,
767                             'with'              => 'user1',
768                             'subject'           => 'S2',
769                             'unreadcount'       => 2
770                         ),
771                     ),
772                 ),
773             ),
774             'Test unread message count is correct for both users' => array(
775                 'users' => array(
776                     'user1',
777                     'user2',
778                 ),
779                 'contacts' => array(
780                 ),
781                 'messages' => array(
782                     array(
783                         'from'          => 'user1',
784                         'to'            => 'user2',
785                         'state'         => 'read',
786                         'subject'       => 'S1',
787                         'timemodifier'  => 1,
788                     ),
789                     array(
790                         'from'          => 'user2',
791                         'to'            => 'user1',
792                         'state'         => 'read',
793                         'subject'       => 'S2',
794                         'timemodifier'  => 2,
795                     ),
796                     array(
797                         'from'          => 'user1',
798                         'to'            => 'user2',
799                         'state'         => 'read',
800                         'subject'       => 'S3',
801                         'timemodifier'  => 3,
802                     ),
803                     array(
804                         'from'          => 'user1',
805                         'to'            => 'user2',
806                         'state'         => 'read',
807                         'subject'       => 'S4',
808                         'timemodifier'  => 4,
809                     ),
810                     array(
811                         'from'          => 'user1',
812                         'to'            => 'user2',
813                         'state'         => 'unread',
814                         'subject'       => 'S5',
815                         'timemodifier'  => 5,
816                     ),
817                     array(
818                         'from'          => 'user2',
819                         'to'            => 'user1',
820                         'state'         => 'unread',
821                         'subject'       => 'S6',
822                         'timemodifier'  => 6,
823                     ),
824                     array(
825                         'from'          => 'user1',
826                         'to'            => 'user2',
827                         'state'         => 'unread',
828                         'subject'       => 'S7',
829                         'timemodifier'  => 7,
830                     ),
831                     array(
832                         'from'          => 'user1',
833                         'to'            => 'user2',
834                         'state'         => 'unread',
835                         'subject'       => 'S8',
836                         'timemodifier'  => 8,
837                     ),
838                 ),
839                 'expectations' => array(
840                     // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
841                     'user1' => array(
842                         array(
843                             'messageposition'   => 0,
844                             'with'              => 'user2',
845                             'subject'           => 'S8',
846                             'unreadcount'       => 1,
847                         ),
848                     ),
849                     'user2' => array(
850                         array(
851                             'messageposition'   => 0,
852                             'with'              => 'user1',
853                             'subject'           => 'S8',
854                             'unreadcount'       => 3,
855                         ),
856                     ),
857                 ),
858             ),
859         );
860     }
862     /**
863      * Test get_conversations with a mixture of messages.
864      *
865      * @dataProvider get_conversations_mixed_provider
866      * @param array $usersdata The list of users to create for this test.
867      * @param array $messagesdata The list of messages to create.
868      * @param array $expectations The list of expected outcomes.
869      */
870     public function test_get_conversations_mixed($usersdata, $contacts, $messagesdata, $expectations) {
871         global $DB;
873         // Create all of the users.
874         $users = array();
875         foreach ($usersdata as $username) {
876             $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
877         }
879         foreach ($contacts as $username => $contact) {
880             foreach ($contact as $contactname => $blocked) {
881                 $record = new stdClass();
882                 $record->userid     = $users[$username]->id;
883                 $record->contactid  = $users[$contactname]->id;
884                 $record->blocked    = $blocked;
885                 $record->id = $DB->insert_record('message_contacts', $record);
886             }
887         }
889         $defaulttimecreated = time();
890         foreach ($messagesdata as $messagedata) {
891             $from       = $users[$messagedata['from']];
892             $to         = $users[$messagedata['to']];
893             $subject    = $messagedata['subject'];
895             if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
896                 $messageid = $this->send_fake_message($from, $to, $subject);
897             } else {
898                 // If there is no state, or the state is not 'unread', assume the message is read.
899                 $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN);
900             }
902             $updatemessage = new stdClass();
903             $updatemessage->id = $messageid;
904             if (isset($messagedata['timecreated'])) {
905                 $updatemessage->timecreated = $messagedata['timecreated'];
906             } else if (isset($messagedata['timemodifier'])) {
907                 $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier'];
908             } else {
909                 $updatemessage->timecreated = $defaulttimecreated;
910             }
912             $DB->update_record('messages', $updatemessage);
913         }
915         foreach ($expectations as $username => $data) {
916             // Get the recent conversations for the specified user.
917             $user = $users[$username];
918             $conversations = array_values(\core_message\api::get_conversations($user->id));
919             foreach ($data as $expectation) {
920                 $otheruser = $users[$expectation['with']];
921                 $conversation = $conversations[$expectation['messageposition']];
922                 $this->assertEquals($otheruser->id, $conversation->userid);
923                 $this->assertEquals($expectation['subject'], $conversation->lastmessage);
924                 $this->assertEquals($expectation['unreadcount'], $conversation->unreadcount);
925             }
926         }
927     }
929     /**
930      * Tests retrieving contacts.
931      */
932     public function test_get_contacts() {
933         // Create some users.
934         $user1 = self::getDataGenerator()->create_user();
936         // Set as the user.
937         $this->setUser($user1);
939         $user2 = new stdClass();
940         $user2->firstname = 'User';
941         $user2->lastname = 'A';
942         $user2 = self::getDataGenerator()->create_user($user2);
944         $user3 = new stdClass();
945         $user3->firstname = 'User';
946         $user3->lastname = 'B';
947         $user3 = self::getDataGenerator()->create_user($user3);
949         $user4 = new stdClass();
950         $user4->firstname = 'User';
951         $user4->lastname = 'C';
952         $user4 = self::getDataGenerator()->create_user($user4);
954         $user5 = new stdClass();
955         $user5->firstname = 'User';
956         $user5->lastname = 'D';
957         $user5 = self::getDataGenerator()->create_user($user5);
959         // Add some users as contacts.
960         \core_message\api::add_contact($user1->id, $user2->id);
961         \core_message\api::add_contact($user1->id, $user3->id);
962         \core_message\api::add_contact($user1->id, $user4->id);
964         // Retrieve the contacts.
965         $contacts = \core_message\api::get_contacts($user1->id);
967         // Confirm the data is correct.
968         $this->assertEquals(3, count($contacts));
969         usort($contacts, ['static', 'sort_contacts']);
971         $contact1 = $contacts[0];
972         $contact2 = $contacts[1];
973         $contact3 = $contacts[2];
975         $this->assertEquals($user2->id, $contact1->userid);
976         $this->assertEmpty($contact1->useridfrom);
977         $this->assertFalse($contact1->ismessaging);
978         $this->assertNull($contact1->lastmessage);
979         $this->assertNull($contact1->messageid);
980         $this->assertNull($contact1->isonline);
981         $this->assertFalse($contact1->isread);
982         $this->assertFalse($contact1->isblocked);
983         $this->assertNull($contact1->unreadcount);
985         $this->assertEquals($user3->id, $contact2->userid);
986         $this->assertEmpty($contact2->useridfrom);
987         $this->assertFalse($contact2->ismessaging);
988         $this->assertNull($contact2->lastmessage);
989         $this->assertNull($contact2->messageid);
990         $this->assertNull($contact2->isonline);
991         $this->assertFalse($contact2->isread);
992         $this->assertFalse($contact2->isblocked);
993         $this->assertNull($contact2->unreadcount);
995         $this->assertEquals($user4->id, $contact3->userid);
996         $this->assertEmpty($contact3->useridfrom);
997         $this->assertFalse($contact3->ismessaging);
998         $this->assertNull($contact3->lastmessage);
999         $this->assertNull($contact3->messageid);
1000         $this->assertNull($contact3->isonline);
1001         $this->assertFalse($contact3->isread);
1002         $this->assertFalse($contact3->isblocked);
1003         $this->assertNull($contact3->unreadcount);
1004     }
1006     /**
1007      * Tests retrieving messages.
1008      */
1009     public function test_get_messages() {
1010         // Create some users.
1011         $user1 = self::getDataGenerator()->create_user();
1012         $user2 = self::getDataGenerator()->create_user();
1014         // The person doing the search.
1015         $this->setUser($user1);
1017         // Send some messages back and forth.
1018         $time = 1;
1019         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1020         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1021         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1022         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1024         // Retrieve the messages.
1025         $messages = \core_message\api::get_messages($user1->id, $user2->id);
1027         // Confirm the message data is correct.
1028         $this->assertEquals(4, count($messages));
1030         $message1 = $messages[0];
1031         $message2 = $messages[1];
1032         $message3 = $messages[2];
1033         $message4 = $messages[3];
1035         $this->assertEquals($user1->id, $message1->useridfrom);
1036         $this->assertEquals($user2->id, $message1->useridto);
1037         $this->assertTrue($message1->displayblocktime);
1038         $this->assertContains('Yo!', $message1->text);
1040         $this->assertEquals($user2->id, $message2->useridfrom);
1041         $this->assertEquals($user1->id, $message2->useridto);
1042         $this->assertFalse($message2->displayblocktime);
1043         $this->assertContains('Sup mang?', $message2->text);
1045         $this->assertEquals($user1->id, $message3->useridfrom);
1046         $this->assertEquals($user2->id, $message3->useridto);
1047         $this->assertFalse($message3->displayblocktime);
1048         $this->assertContains('Writing PHPUnit tests!', $message3->text);
1050         $this->assertEquals($user2->id, $message4->useridfrom);
1051         $this->assertEquals($user1->id, $message4->useridto);
1052         $this->assertFalse($message4->displayblocktime);
1053         $this->assertContains('Word.', $message4->text);
1054     }
1056     /**
1057      * Tests retrieving most recent message.
1058      */
1059     public function test_get_most_recent_message() {
1060         // Create some users.
1061         $user1 = self::getDataGenerator()->create_user();
1062         $user2 = self::getDataGenerator()->create_user();
1064         // The person doing the search.
1065         $this->setUser($user1);
1067         // Send some messages back and forth.
1068         $time = 1;
1069         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1070         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1071         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1072         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1074         // Retrieve the most recent messages.
1075         $message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
1077         // Check the results are correct.
1078         $this->assertEquals($user2->id, $message->useridfrom);
1079         $this->assertEquals($user1->id, $message->useridto);
1080         $this->assertContains('Word.', $message->text);
1081     }
1083     /**
1084      * Tests retrieving a user's profile.
1085      */
1086     public function test_get_profile() {
1087         // Create some users.
1088         $user1 = self::getDataGenerator()->create_user();
1090         $user2 = new stdClass();
1091         $user2->country = 'AU';
1092         $user2->city = 'Perth';
1093         $user2 = self::getDataGenerator()->create_user($user2);
1095         // The person doing the search.
1096         $this->setUser($user1);
1098         // Get the profile.
1099         $profile = \core_message\api::get_profile($user1->id, $user2->id);
1101         $this->assertEquals($user2->id, $profile->userid);
1102         $this->assertEmpty($profile->email);
1103         $this->assertEmpty($profile->country);
1104         $this->assertEmpty($profile->city);
1105         $this->assertEquals(fullname($user2), $profile->fullname);
1106         $this->assertNull($profile->isonline);
1107         $this->assertFalse($profile->isblocked);
1108         $this->assertFalse($profile->iscontact);
1109     }
1111     /**
1112      * Tests retrieving a user's profile.
1113      */
1114     public function test_get_profile_as_admin() {
1115         // The person doing the search.
1116         $this->setAdminUser();
1118         // Create some users.
1119         $user1 = self::getDataGenerator()->create_user();
1121         $user2 = new stdClass();
1122         $user2->country = 'AU';
1123         $user2->city = 'Perth';
1124         $user2 = self::getDataGenerator()->create_user($user2);
1126         // Get the profile.
1127         $profile = \core_message\api::get_profile($user1->id, $user2->id);
1129         $this->assertEquals($user2->id, $profile->userid);
1130         $this->assertEquals($user2->email, $profile->email);
1131         $this->assertEquals($user2->country, $profile->country);
1132         $this->assertEquals($user2->city, $profile->city);
1133         $this->assertEquals(fullname($user2), $profile->fullname);
1134         $this->assertFalse($profile->isonline);
1135         $this->assertFalse($profile->isblocked);
1136         $this->assertFalse($profile->iscontact);
1137     }
1139     /**
1140      * Tests checking if a user can delete a conversation.
1141      */
1142     public function test_can_delete_conversation() {
1143         // Set as the admin.
1144         $this->setAdminUser();
1146         // Create some users.
1147         $user1 = self::getDataGenerator()->create_user();
1148         $user2 = self::getDataGenerator()->create_user();
1150         // Send some messages back and forth.
1151         $time = 1;
1152         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1153         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1154         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1155         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1157         $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1159         // The admin can do anything.
1160         $this->assertTrue(\core_message\api::can_delete_conversation($user1->id, $conversationid));
1162         // Set as the user 1.
1163         $this->setUser($user1);
1165         // They can delete their own messages.
1166         $this->assertTrue(\core_message\api::can_delete_conversation($user1->id, $conversationid));
1168         // They can't delete someone elses.
1169         $this->assertFalse(\core_message\api::can_delete_conversation($user2->id, $conversationid));
1170     }
1172     /**
1173      * Tests deleting a conversation.
1174      */
1175     public function test_delete_conversation() {
1176         global $DB;
1178         // Create some users.
1179         $user1 = self::getDataGenerator()->create_user();
1180         $user2 = self::getDataGenerator()->create_user();
1182         // The person doing the search.
1183         $this->setUser($user1);
1185         // Send some messages back and forth.
1186         $time = 1;
1187         $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1188         $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1189         $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1190         $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1192         // Delete the conversation as user 1.
1193         \core_message\api::delete_conversation($user1->id, $user2->id);
1194         $this->assertDebuggingCalled();
1196         $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
1197         $this->assertCount(4, $muas);
1198         // Sort by id.
1199         ksort($muas);
1201         $mua1 = array_shift($muas);
1202         $mua2 = array_shift($muas);
1203         $mua3 = array_shift($muas);
1204         $mua4 = array_shift($muas);
1206         $this->assertEquals($user1->id, $mua1->userid);
1207         $this->assertEquals($m1id, $mua1->messageid);
1208         $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
1210         $this->assertEquals($user1->id, $mua2->userid);
1211         $this->assertEquals($m2id, $mua2->messageid);
1212         $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
1214         $this->assertEquals($user1->id, $mua3->userid);
1215         $this->assertEquals($m3id, $mua3->messageid);
1216         $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
1218         $this->assertEquals($user1->id, $mua4->userid);
1219         $this->assertEquals($m4id, $mua4->messageid);
1220         $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
1221     }
1223     /**
1224      * Tests deleting a conversation by conversation id.
1225      */
1226     public function test_delete_conversation_by_id() {
1227         global $DB;
1229         // Create some users.
1230         $user1 = self::getDataGenerator()->create_user();
1231         $user2 = self::getDataGenerator()->create_user();
1233         // The person doing the search.
1234         $this->setUser($user1);
1236         // Send some messages back and forth.
1237         $time = 1;
1238         $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1239         $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1240         $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1241         $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1243         // Delete the conversation as user 1.
1244         $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1245         \core_message\api::delete_conversation_by_id($user1->id, $conversationid);
1247         $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
1248         $this->assertCount(4, $muas);
1249         // Sort by id.
1250         ksort($muas);
1252         $mua1 = array_shift($muas);
1253         $mua2 = array_shift($muas);
1254         $mua3 = array_shift($muas);
1255         $mua4 = array_shift($muas);
1257         $this->assertEquals($user1->id, $mua1->userid);
1258         $this->assertEquals($m1id, $mua1->messageid);
1259         $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
1261         $this->assertEquals($user1->id, $mua2->userid);
1262         $this->assertEquals($m2id, $mua2->messageid);
1263         $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
1265         $this->assertEquals($user1->id, $mua3->userid);
1266         $this->assertEquals($m3id, $mua3->messageid);
1267         $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
1269         $this->assertEquals($user1->id, $mua4->userid);
1270         $this->assertEquals($m4id, $mua4->messageid);
1271         $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
1272     }
1274     /**
1275      * Tests counting unread conversations.
1276      */
1277     public function test_count_unread_conversations() {
1278         $this->resetAfterTest(true);
1280         // Create some users.
1281         $user1 = self::getDataGenerator()->create_user();
1282         $user2 = self::getDataGenerator()->create_user();
1283         $user3 = self::getDataGenerator()->create_user();
1284         $user4 = self::getDataGenerator()->create_user();
1286         // The person wanting the conversation count.
1287         $this->setUser($user1);
1289         // Send some messages back and forth, have some different conversations with different users.
1290         $this->send_fake_message($user1, $user2, 'Yo!');
1291         $this->send_fake_message($user2, $user1, 'Sup mang?');
1292         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
1293         $this->send_fake_message($user2, $user1, 'Word.');
1295         $this->send_fake_message($user1, $user3, 'Booyah');
1296         $this->send_fake_message($user3, $user1, 'Whaaat?');
1297         $this->send_fake_message($user1, $user3, 'Nothing.');
1298         $this->send_fake_message($user3, $user1, 'Cool.');
1300         $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
1301         $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
1302         $this->send_fake_message($user1, $user4, 'Dope.');
1304         // Check the amount for the current user.
1305         $this->assertEquals(3, core_message\api::count_unread_conversations());
1307         // Check the amount for the second user.
1308         $this->assertEquals(1, core_message\api::count_unread_conversations($user2));
1309     }
1311     /**
1312      * Tests deleting a conversation.
1313      */
1314     public function test_get_all_message_preferences() {
1315         $user = self::getDataGenerator()->create_user();
1316         $this->setUser($user);
1318         // Set a couple of preferences to test.
1319         set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
1320         set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
1322         $processors = get_message_processors();
1323         $providers = message_get_providers_for_user($user->id);
1324         $prefs = \core_message\api::get_all_message_preferences($processors, $providers, $user);
1326         $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedin['popup']);
1327         $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']);
1328     }
1330     /**
1331      * Tests the user can post a message.
1332      */
1333     public function test_can_post_message() {
1334         // Create some users.
1335         $user1 = self::getDataGenerator()->create_user();
1336         $user2 = self::getDataGenerator()->create_user();
1338         // Set as the first user.
1339         $this->setUser($user1);
1341         // With the default privacy setting, users can't message them.
1342         $this->assertFalse(\core_message\api::can_post_message($user2));
1344         // Enrol users to the same course.
1345         $course = $this->getDataGenerator()->create_course();
1346         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1347         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
1348         // After enrolling users to the course, they should be able to message them with the default privacy setting.
1349         $this->assertTrue(\core_message\api::can_post_message($user2));
1350     }
1352     /**
1353      * Tests the user can't post a message without proper capability.
1354      */
1355     public function test_can_post_message_without_cap() {
1356         global $DB;
1358         // Create some users.
1359         $user1 = self::getDataGenerator()->create_user();
1360         $user2 = self::getDataGenerator()->create_user();
1362         // Set as the user 1.
1363         $this->setUser($user1);
1365         // Remove the capability to send a message.
1366         $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
1367         unassign_capability('moodle/site:sendmessage', $roleids['user'],
1368             context_system::instance());
1370         // Check that we can not post a message without the capability.
1371         $this->assertFalse(\core_message\api::can_post_message($user2));
1372     }
1374     /**
1375      * Tests the user can post a message when they are contact.
1376      */
1377     public function test_can_post_message_when_contact() {
1378         // Create some users.
1379         $user1 = self::getDataGenerator()->create_user();
1380         $user2 = self::getDataGenerator()->create_user();
1382         // Set as the first user.
1383         $this->setUser($user1);
1385         // Check that we can not send user2 a message.
1386         $this->assertFalse(\core_message\api::can_post_message($user2));
1388         // Add users as contacts.
1389         \core_message\api::add_contact($user1->id, $user2->id);
1391         // Check that the return result is now true.
1392         $this->assertTrue(\core_message\api::can_post_message($user2));
1393     }
1395     /**
1396      * Tests the user can't post a message if they are not a contact and the user
1397      * has requested messages only from contacts.
1398      */
1399     public function test_can_post_message_when_not_contact() {
1400         // Create some users.
1401         $user1 = self::getDataGenerator()->create_user();
1402         $user2 = self::getDataGenerator()->create_user();
1404         // Set as the first user.
1405         $this->setUser($user1);
1407         // Set the second user's preference to not receive messages from non-contacts.
1408         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1410         // Check that we can not send user 2 a message.
1411         $this->assertFalse(\core_message\api::can_post_message($user2));
1412     }
1414     /**
1415      * Tests the user can't post a message if they are blocked.
1416      */
1417     public function test_can_post_message_when_blocked() {
1418         // Create some users.
1419         $user1 = self::getDataGenerator()->create_user();
1420         $user2 = self::getDataGenerator()->create_user();
1422         // Set the user.
1423         $this->setUser($user1);
1425         // Block the second user.
1426         \core_message\api::block_user($user1->id, $user2->id);
1428         // Check that the second user can no longer send the first user a message.
1429         $this->assertFalse(\core_message\api::can_post_message($user1, $user2));
1430     }
1432     /**
1433      * Tests the user can post a message when site-wide messaging setting is enabled,
1434      * even if they are not a contact and are not members of the same course.
1435      */
1436     public function test_can_post_message_site_messaging_setting() {
1437         // Create some users.
1438         $user1 = self::getDataGenerator()->create_user();
1439         $user2 = self::getDataGenerator()->create_user();
1441         // Set as the first user.
1442         $this->setUser($user1);
1444         // Set the second user's preference to receive messages from everybody. As site-wide messaging setting
1445         // is disabled by default, the value will be changed to MESSAGE_PRIVACY_COURSEMEMBER.
1446         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user2->id);
1447         $this->assertFalse(\core_message\api::can_post_message($user2));
1449         // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
1450         set_config('messagingallusers', true);
1451         // Check that we can send user2 a message.
1452         $this->assertTrue(\core_message\api::can_post_message($user2));
1453     }
1455     /**
1456      * Tests get_user_privacy_messaging_preference method.
1457      */
1458     public function test_get_user_privacy_messaging_preference() {
1459         // Create some users.
1460         $user1 = self::getDataGenerator()->create_user();
1461         $user2 = self::getDataGenerator()->create_user();
1462         $user3 = self::getDataGenerator()->create_user();
1464         // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
1465         set_config('messagingallusers', true);
1467         // Set some user preferences.
1468         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user1->id);
1469         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1471         // Check the returned value for each user.
1472         $this->assertEquals(
1473             \core_message\api::MESSAGE_PRIVACY_SITE,
1474             \core_message\api::get_user_privacy_messaging_preference($user1->id)
1475         );
1476         $this->assertEquals(
1477             \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
1478             \core_message\api::get_user_privacy_messaging_preference($user2->id)
1479         );
1480         $this->assertEquals(
1481             \core_message\api::MESSAGE_PRIVACY_SITE,
1482             \core_message\api::get_user_privacy_messaging_preference($user3->id)
1483         );
1485         // Disable site-wide messagging privacy setting. The user will be able to receive messages from members of their course.
1486         set_config('messagingallusers', false);
1488         // Check the returned value for each user.
1489         $this->assertEquals(
1490             \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
1491             \core_message\api::get_user_privacy_messaging_preference($user1->id)
1492         );
1493         $this->assertEquals(
1494             \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
1495             \core_message\api::get_user_privacy_messaging_preference($user2->id)
1496         );
1497         $this->assertEquals(
1498             \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
1499             \core_message\api::get_user_privacy_messaging_preference($user3->id)
1500         );
1501     }
1503     /**
1504      * Tests that when blocking messages from non-contacts is enabled that
1505      * non-contacts trying to send a message return false.
1506      */
1507     public function test_is_user_non_contact_blocked() {
1508         // Create some users.
1509         $user1 = self::getDataGenerator()->create_user();
1510         $user2 = self::getDataGenerator()->create_user();
1512         // Set as the first user.
1513         $this->setUser($user1);
1515         // By default, user only can be messaged by contacts and members of any of his/her courses.
1516         $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
1518         // Enable all users privacy messaging and check now the default user's preference has been set to allow receiving
1519         // messages from everybody.
1520         set_config('messagingallusers', true);
1521         // Check that the return result is now false because any site user can contact him/her.
1522         $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1524         // Set the second user's preference to not receive messages from non-contacts.
1525         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1526         // Check that the return result is still true (because is even more restricted).
1527         $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
1529         // Add the first user as a contact for the second user.
1530         \core_message\api::add_contact($user2->id, $user1->id);
1532         // Check that the return result is now false.
1533         $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1535         // Set the second user's preference to receive messages from course members.
1536         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER, $user2->id);
1537         // Check that the return result is still false (because $user1 is still his/her contact).
1538         $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1539     }
1541     /**
1542      * Tests that we return true when a user is blocked, or false
1543      * if they are not blocked.
1544      */
1545     public function test_is_user_blocked() {
1546         // Create some users.
1547         $user1 = self::getDataGenerator()->create_user();
1548         $user2 = self::getDataGenerator()->create_user();
1550         // Set the user.
1551         $this->setUser($user1);
1553         // User shouldn't be blocked.
1554         $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1555         $this->assertDebuggingCalled();
1557         // Block the user.
1558         \core_message\api::block_user($user1->id, $user2->id);
1560         // User should be blocked.
1561         $this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
1562         $this->assertDebuggingCalled();
1564         // Unblock the user.
1565         \core_message\api::unblock_user($user1->id, $user2->id);
1566         $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1567         $this->assertDebuggingCalled();
1568     }
1570     /**
1571      * Tests that the admin is not blocked even if someone has chosen to block them.
1572      */
1573     public function test_is_user_blocked_as_admin() {
1574         // Create a user.
1575         $user1 = self::getDataGenerator()->create_user();
1577         // Set the user.
1578         $this->setUser($user1);
1580         // Block the admin user.
1581         \core_message\api::block_user($user1->id, 2);
1583         // Now change to the admin user.
1584         $this->setAdminUser();
1586         // As the admin you should still be able to send messages to the user.
1587         $this->assertFalse(\core_message\api::is_user_blocked($user1->id));
1588         $this->assertDebuggingCalled();
1589     }
1591     /*
1592      * Tes get_message_processor api.
1593      */
1594     public function test_get_message_processor() {
1595         $processors = get_message_processors(true);
1596         if (empty($processors)) {
1597             $this->markTestSkipped("No message processors found");
1598         }
1600         $name = key($processors);
1601         $processor = current($processors);
1602         $testprocessor = \core_message\api::get_message_processor($name);
1603         $this->assertEquals($processor->name, $testprocessor->name);
1604         $this->assertEquals($processor->enabled, $testprocessor->enabled);
1605         $this->assertEquals($processor->available, $testprocessor->available);
1606         $this->assertEquals($processor->configured, $testprocessor->configured);
1608         // Disable processor and test.
1609         \core_message\api::update_processor_status($testprocessor, 0);
1610         $testprocessor = \core_message\api::get_message_processor($name, true);
1611         $this->assertEmpty($testprocessor);
1612         $testprocessor = \core_message\api::get_message_processor($name);
1613         $this->assertEquals($processor->name, $testprocessor->name);
1614         $this->assertEquals(0, $testprocessor->enabled);
1616         // Enable again and test.
1617         \core_message\api::update_processor_status($testprocessor, 1);
1618         $testprocessor = \core_message\api::get_message_processor($name, true);
1619         $this->assertEquals($processor->name, $testprocessor->name);
1620         $this->assertEquals(1, $testprocessor->enabled);
1621         $testprocessor = \core_message\api::get_message_processor($name);
1622         $this->assertEquals($processor->name, $testprocessor->name);
1623         $this->assertEquals(1, $testprocessor->enabled);
1624     }
1626     /**
1627      * Test method update_processor_status.
1628      */
1629     public function test_update_processor_status() {
1630         $processors = get_message_processors();
1631         if (empty($processors)) {
1632             $this->markTestSkipped("No message processors found");
1633         }
1634         $name = key($processors);
1635         $testprocessor = current($processors);
1637         // Enable.
1638         \core_message\api::update_processor_status($testprocessor, 1);
1639         $testprocessor = \core_message\api::get_message_processor($name);
1640         $this->assertEquals(1, $testprocessor->enabled);
1642         // Disable.
1643         \core_message\api::update_processor_status($testprocessor, 0);
1644         $testprocessor = \core_message\api::get_message_processor($name);
1645         $this->assertEquals(0, $testprocessor->enabled);
1647         // Enable again.
1648         \core_message\api::update_processor_status($testprocessor, 1);
1649         $testprocessor = \core_message\api::get_message_processor($name);
1650         $this->assertEquals(1, $testprocessor->enabled);
1651     }
1653     /**
1654      * Test method is_user_enabled.
1655      */
1656     public function is_user_enabled() {
1657         $processors = get_message_processors();
1658         if (empty($processors)) {
1659             $this->markTestSkipped("No message processors found");
1660         }
1661         $name = key($processors);
1662         $testprocessor = current($processors);
1664         // Enable.
1665         \core_message\api::update_processor_status($testprocessor, 1);
1666         $status = \core_message\api::is_processor_enabled($name);
1667         $this->assertEquals(1, $status);
1669         // Disable.
1670         \core_message\api::update_processor_status($testprocessor, 0);
1671         $status = \core_message\api::is_processor_enabled($name);
1672         $this->assertEquals(0, $status);
1674         // Enable again.
1675         \core_message\api::update_processor_status($testprocessor, 1);
1676         $status = \core_message\api::is_processor_enabled($name);
1677         $this->assertEquals(1, $status);
1678     }
1680     /**
1681      * Test retrieving messages by providing a minimum timecreated value.
1682      */
1683     public function test_get_messages_time_from_only() {
1684         // Create some users.
1685         $user1 = self::getDataGenerator()->create_user();
1686         $user2 = self::getDataGenerator()->create_user();
1688         // The person doing the search.
1689         $this->setUser($user1);
1691         // Send some messages back and forth.
1692         $time = 1;
1693         $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1694         $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1695         $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1696         $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1698         // Retrieve the messages from $time, which should be all of them.
1699         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
1701         // Confirm the message data is correct.
1702         $this->assertEquals(4, count($messages));
1704         $message1 = $messages[0];
1705         $message2 = $messages[1];
1706         $message3 = $messages[2];
1707         $message4 = $messages[3];
1709         $this->assertContains('Message 1', $message1->text);
1710         $this->assertContains('Message 2', $message2->text);
1711         $this->assertContains('Message 3', $message3->text);
1712         $this->assertContains('Message 4', $message4->text);
1714         // Retrieve the messages from $time + 3, which should only be the 2 last messages.
1715         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
1717         // Confirm the message data is correct.
1718         $this->assertEquals(2, count($messages));
1720         $message1 = $messages[0];
1721         $message2 = $messages[1];
1723         $this->assertContains('Message 3', $message1->text);
1724         $this->assertContains('Message 4', $message2->text);
1725     }
1727     /**
1728      * Test retrieving messages by providing a maximum timecreated value.
1729      */
1730     public function test_get_messages_time_to_only() {
1731         // Create some users.
1732         $user1 = self::getDataGenerator()->create_user();
1733         $user2 = self::getDataGenerator()->create_user();
1735         // The person doing the search.
1736         $this->setUser($user1);
1738         // Send some messages back and forth.
1739         $time = 1;
1740         $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1741         $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1742         $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1743         $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1745         // Retrieve the messages up until $time + 4, which should be all of them.
1746         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
1748         // Confirm the message data is correct.
1749         $this->assertEquals(4, count($messages));
1751         $message1 = $messages[0];
1752         $message2 = $messages[1];
1753         $message3 = $messages[2];
1754         $message4 = $messages[3];
1756         $this->assertContains('Message 1', $message1->text);
1757         $this->assertContains('Message 2', $message2->text);
1758         $this->assertContains('Message 3', $message3->text);
1759         $this->assertContains('Message 4', $message4->text);
1761         // Retrieve the messages up until $time + 2, which should be the first two.
1762         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
1764         // Confirm the message data is correct.
1765         $this->assertEquals(2, count($messages));
1767         $message1 = $messages[0];
1768         $message2 = $messages[1];
1770         $this->assertContains('Message 1', $message1->text);
1771         $this->assertContains('Message 2', $message2->text);
1772     }
1774     /**
1775      * Test retrieving messages by providing a minimum and maximum timecreated value.
1776      */
1777     public function test_get_messages_time_from_and_to() {
1778         // Create some users.
1779         $user1 = self::getDataGenerator()->create_user();
1780         $user2 = self::getDataGenerator()->create_user();
1782         // The person doing the search.
1783         $this->setUser($user1);
1785         // Send some messages back and forth.
1786         $time = 1;
1787         $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1788         $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1789         $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1790         $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1792         // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
1793         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
1795         // Confirm the message data is correct.
1796         $this->assertEquals(2, count($messages));
1798         $message1 = $messages[0];
1799         $message2 = $messages[1];
1801         $this->assertContains('Message 2', $message1->text);
1802         $this->assertContains('Message 3', $message2->text);
1803     }
1805     /**
1806      * Test returning blocked users.
1807      */
1808     public function test_get_blocked_users() {
1809         global $USER;
1811         // Set this user as the admin.
1812         $this->setAdminUser();
1814         // Create a user to add to the admin's contact list.
1815         $user1 = $this->getDataGenerator()->create_user();
1816         $user2 = $this->getDataGenerator()->create_user();
1818         // Add users to the admin's contact list.
1819         \core_message\api::block_user($USER->id, $user2->id);
1821         $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
1823         // Block other user.
1824         \core_message\api::block_user($USER->id, $user1->id);
1825         $this->assertCount(2, \core_message\api::get_blocked_users($USER->id));
1827         // Test deleting users.
1828         delete_user($user1);
1829         $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
1830     }
1832     /**
1833      * Test returning contacts with unread message count.
1834      */
1835     public function test_get_contacts_with_unread_message_count() {
1836         global $DB;
1838         $user1 = self::getDataGenerator()->create_user();
1839         $user2 = self::getDataGenerator()->create_user();
1840         $user3 = self::getDataGenerator()->create_user();
1841         $user4 = self::getDataGenerator()->create_user();
1843         // Add the users to each of their contacts.
1844         \core_message\api::add_contact($user1->id, $user2->id);
1845         \core_message\api::add_contact($user2->id, $user3->id);
1847         $this->send_fake_message($user1, $user2);
1848         $this->send_fake_message($user1, $user2);
1849         $this->send_fake_message($user1, $user2);
1850         $message4id = $this->send_fake_message($user1, $user2);
1852         $this->send_fake_message($user3, $user2);
1853         $message6id = $this->send_fake_message($user3, $user2);
1854         $this->send_fake_message($user3, $user2);
1855         $this->send_fake_message($user3, $user2);
1856         $this->send_fake_message($user3, $user2);
1858         // Send a message that should never be included as the user is not a contact.
1859         $this->send_fake_message($user4, $user2);
1861         // Get the contacts and the unread message count.
1862         $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1864         // Confirm the size is correct.
1865         $this->assertCount(2, $messages);
1866         ksort($messages);
1868         $messageinfo1 = array_shift($messages);
1869         $messageinfo2 = array_shift($messages);
1871         $this->assertEquals($user1->id, $messageinfo1->id);
1872         $this->assertEquals(4, $messageinfo1->messagecount);
1873         $this->assertEquals($user3->id, $messageinfo2->id);
1874         $this->assertEquals(5, $messageinfo2->messagecount);
1876         // Mark some of the messages as read.
1877         $m4 = $DB->get_record('messages', ['id' => $message4id]);
1878         $m6 = $DB->get_record('messages', ['id' => $message6id]);
1879         \core_message\api::mark_message_as_read($user2->id, $m4);
1880         \core_message\api::mark_message_as_read($user2->id, $m6);
1882         // Get the contacts and the unread message count.
1883         $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1885         // Confirm the size is correct.
1886         $this->assertCount(2, $messages);
1887         ksort($messages);
1889         // Confirm read messages are not included.
1890         $messageinfo1 = array_shift($messages);
1891         $messageinfo2 = array_shift($messages);
1892         $this->assertEquals($user1->id, $messageinfo1->id);
1893         $this->assertEquals(3, $messageinfo1->messagecount);
1894         $this->assertEquals($user3->id, $messageinfo2->id);
1895         $this->assertEquals(4, $messageinfo2->messagecount);
1897         // Now, let's populate the database with messages from user2 to user 1.
1898         $this->send_fake_message($user2, $user1);
1899         $this->send_fake_message($user2, $user1);
1900         $messageid = $this->send_fake_message($user2, $user1);
1902         // Send a message that should never be included as the user is not a contact.
1903         $this->send_fake_message($user4, $user1);
1905         // Get the contacts and the unread message count.
1906         $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
1908         // Confirm the size is correct.
1909         $this->assertCount(1, $messages);
1910         $messageinfo1 = array_shift($messages);
1911         $this->assertEquals($user2->id, $messageinfo1->id);
1912         $this->assertEquals(3, $messageinfo1->messagecount);
1914         // Mark the last message as read.
1915         $m = $DB->get_record('messages', ['id' => $messageid]);
1916         \core_message\api::mark_message_as_read($user1->id, $m);
1918         $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
1920         // Confirm the size is correct.
1921         $this->assertCount(1, $messages);
1923         // Confirm read messages are not included.
1924         $messageinfo1 = array_shift($messages);
1925         $this->assertEquals($user2->id, $messageinfo1->id);
1926         $this->assertEquals(2, $messageinfo1->messagecount);
1927     }
1929     /**
1930      * Test returning contacts with unread message count when there are no messages.
1931      */
1932     public function test_get_contacts_with_unread_message_count_no_messages() {
1933         $user1 = self::getDataGenerator()->create_user();
1934         $user2 = self::getDataGenerator()->create_user();
1936         // Add the users to each of their contacts.
1937         \core_message\api::add_contact($user2->id, $user1->id);
1939         // Check we get the correct message count.
1940         $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1942         // Confirm the size is correct.
1943         $this->assertCount(1, $messages);
1945         $messageinfo = array_shift($messages);
1947         $this->assertEquals($user1->id, $messageinfo->id);
1948         $this->assertEquals(0, $messageinfo->messagecount);
1949     }
1951     /**
1952      * Test returning non-contacts with unread message count.
1953      */
1954     public function test_get_non_contacts_with_unread_message_count() {
1955         global $DB;
1957         $user1 = self::getDataGenerator()->create_user();
1958         $user2 = self::getDataGenerator()->create_user();
1959         $user3 = self::getDataGenerator()->create_user();
1960         $user4 = self::getDataGenerator()->create_user();
1962         // Add a user to the contact list of the users we are testing this function with.
1963         \core_message\api::add_contact($user1->id, $user4->id);
1964         \core_message\api::add_contact($user2->id, $user4->id);
1966         $this->send_fake_message($user1, $user2);
1967         $this->send_fake_message($user1, $user2);
1968         $this->send_fake_message($user1, $user2);
1969         $message4id = $this->send_fake_message($user1, $user2);
1971         $this->send_fake_message($user3, $user2);
1972         $message6id = $this->send_fake_message($user3, $user2);
1973         $this->send_fake_message($user3, $user2);
1974         $this->send_fake_message($user3, $user2);
1975         $this->send_fake_message($user3, $user2);
1977         // Send a message that should never be included as the user is a contact.
1978         $this->send_fake_message($user4, $user2);
1980         // Get the non-contacts and the unread message count.
1981         $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
1983         // Check we get the correct message count.
1984         ksort($messages);
1985         $this->assertCount(2, $messages);
1986         $messageinfo1 = array_shift($messages);
1987         $messageinfo2 = array_shift($messages);
1988         $this->assertEquals($user1->id, $messageinfo1->id);
1989         $this->assertEquals(4, $messageinfo1->messagecount);
1990         $this->assertEquals($user3->id, $messageinfo2->id);
1991         $this->assertEquals(5, $messageinfo2->messagecount);
1993         // Mark some of the messages as read.
1994         $m4 = $DB->get_record('messages', ['id' => $message4id]);
1995         $m6 = $DB->get_record('messages', ['id' => $message6id]);
1996         \core_message\api::mark_message_as_read($user2->id, $m4);
1997         \core_message\api::mark_message_as_read($user2->id, $m6);
1999         // Get the non-contacts and the unread message count.
2000         $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
2002         // Check the marked message is not returned in the message count.
2003         ksort($messages);
2004         $this->assertCount(2, $messages);
2005         $messageinfo1 = array_shift($messages);
2006         $messageinfo2 = array_shift($messages);
2007         $this->assertEquals($user1->id, $messageinfo1->id);
2008         $this->assertEquals(3, $messageinfo1->messagecount);
2009         $this->assertEquals($user3->id, $messageinfo2->id);
2010         $this->assertEquals(4, $messageinfo2->messagecount);
2012         // Now, let's populate the database with messages from user2 to user 1.
2013         $this->send_fake_message($user2, $user1);
2014         $this->send_fake_message($user2, $user1);
2015         $messageid = $this->send_fake_message($user2, $user1);
2017         // Send a message that should never be included as the user is a contact.
2018         $this->send_fake_message($user4, $user1);
2020         // Get the non-contacts and the unread message count.
2021         $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
2023         // Confirm the size is correct.
2024         $this->assertCount(1, $messages);
2025         $messageinfo1 = array_shift($messages);
2026         $this->assertEquals($user2->id, $messageinfo1->id);
2027         $this->assertEquals(3, $messageinfo1->messagecount);
2029         // Mark the last message as read.
2030         $m = $DB->get_record('messages', ['id' => $messageid]);
2031         \core_message\api::mark_message_as_read($user1->id, $m);
2033         // Get the non-contacts and the unread message count.
2034         $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
2036         // Check the marked message is not returned in the message count.
2037         $this->assertCount(1, $messages);
2038         $messageinfo1 = array_shift($messages);
2039         $this->assertEquals($user2->id, $messageinfo1->id);
2040         $this->assertEquals(2, $messageinfo1->messagecount);
2041     }
2043     /**
2044      * Test marking a message as read.
2045      */
2046     public function test_mark_message_as_read() {
2047         global $DB;
2049         $user1 = self::getDataGenerator()->create_user();
2050         $user2 = self::getDataGenerator()->create_user();
2052         $this->send_fake_message($user1, $user2);
2053         $m2id = $this->send_fake_message($user1, $user2);
2054         $this->send_fake_message($user2, $user1);
2055         $m4id = $this->send_fake_message($user2, $user1);
2057         $m2 = $DB->get_record('messages', ['id' => $m2id]);
2058         $m4 = $DB->get_record('messages', ['id' => $m4id]);
2059         \core_message\api::mark_message_as_read($user2->id, $m2, 11);
2060         \core_message\api::mark_message_as_read($user1->id, $m4, 12);
2062         // Confirm there are two user actions.
2063         $muas = $DB->get_records('message_user_actions', [], 'timecreated ASC');
2064         $this->assertEquals(2, count($muas));
2066         // Confirm they are correct.
2067         $mua1 = array_shift($muas);
2068         $mua2 = array_shift($muas);
2070         // Confirm first action.
2071         $this->assertEquals($user2->id, $mua1->userid);
2072         $this->assertEquals($m2id, $mua1->messageid);
2073         $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua1->action);
2074         $this->assertEquals(11, $mua1->timecreated);
2076         // Confirm second action.
2077         $this->assertEquals($user1->id, $mua2->userid);
2078         $this->assertEquals($m4id, $mua2->messageid);
2079         $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua2->action);
2080         $this->assertEquals(12, $mua2->timecreated);
2081     }
2083     /**
2084      * Test marking a notification as read.
2085      */
2086     public function test_mark_notification_as_read() {
2087         global $DB;
2089         $user1 = self::getDataGenerator()->create_user();
2090         $user2 = self::getDataGenerator()->create_user();
2092         $this->send_fake_message($user1, $user2, 'Notification 1', 1);
2093         $n2id = $this->send_fake_message($user1, $user2, 'Notification 2', 1);
2094         $this->send_fake_message($user2, $user1, 'Notification 3', 1);
2095         $n4id = $this->send_fake_message($user2, $user1, 'Notification 4', 1);
2097         $n2 = $DB->get_record('notifications', ['id' => $n2id]);
2098         $n4 = $DB->get_record('notifications', ['id' => $n4id]);
2100         \core_message\api::mark_notification_as_read($n2, 11);
2101         \core_message\api::mark_notification_as_read($n4, 12);
2103         // Retrieve the notifications.
2104         $n2 = $DB->get_record('notifications', ['id' => $n2id]);
2105         $n4 = $DB->get_record('notifications', ['id' => $n4id]);
2107         // Confirm they have been marked as read.
2108         $this->assertEquals(11, $n2->timeread);
2109         $this->assertEquals(12, $n4->timeread);
2110     }
2112     /**
2113      * Test a conversation is not returned if there is none.
2114      */
2115     public function test_get_conversation_between_users_no_conversation() {
2116         $user1 = self::getDataGenerator()->create_user();
2117         $user2 = self::getDataGenerator()->create_user();
2119         $this->assertFalse(\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
2120     }
2122     /**
2123      * Test we can return a conversation that exists between users.
2124      */
2125     public function test_get_conversation_between_users_with_existing_conversation() {
2126         $user1 = self::getDataGenerator()->create_user();
2127         $user2 = self::getDataGenerator()->create_user();
2129         $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
2131         $this->assertEquals($conversationid,
2132             \core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
2133     }
2135     /**
2136      * Test can create a contact request.
2137      */
2138     public function test_can_create_contact_request() {
2139         global $CFG;
2141         $user1 = self::getDataGenerator()->create_user();
2142         $user2 = self::getDataGenerator()->create_user();
2144         // Disable messaging.
2145         $CFG->messaging = 0;
2146         $this->assertFalse(\core_message\api::can_create_contact($user1->id, $user2->id));
2148         // Re-enable messaging.
2149         $CFG->messaging = 1;
2151         // Allow users to message anyone site-wide.
2152         $CFG->messagingallusers = 1;
2153         $this->assertTrue(\core_message\api::can_create_contact($user1->id, $user2->id));
2155         // Disallow users from messaging anyone site-wide.
2156         $CFG->messagingallusers = 0;
2157         $this->assertFalse(\core_message\api::can_create_contact($user1->id, $user2->id));
2159         // Put the users in the same course so a contact request should be possible.
2160         $course = self::getDataGenerator()->create_course();
2161         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
2162         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
2163         $this->assertTrue(\core_message\api::can_create_contact($user1->id, $user2->id));
2164     }
2166     /**
2167      * Test creating a contact request.
2168      */
2169     public function test_create_contact_request() {
2170         global $DB;
2172         $user1 = self::getDataGenerator()->create_user();
2173         $user2 = self::getDataGenerator()->create_user();
2175         \core_message\api::create_contact_request($user1->id, $user2->id);
2177         $request = $DB->get_records('message_contact_requests');
2179         $this->assertCount(1, $request);
2181         $request = reset($request);
2183         $this->assertEquals($user1->id, $request->userid);
2184         $this->assertEquals($user2->id, $request->requesteduserid);
2185     }
2187     /**
2188      * Test confirming a contact request.
2189      */
2190     public function test_confirm_contact_request() {
2191         global $DB;
2193         $user1 = self::getDataGenerator()->create_user();
2194         $user2 = self::getDataGenerator()->create_user();
2196         \core_message\api::create_contact_request($user1->id, $user2->id);
2198         \core_message\api::confirm_contact_request($user1->id, $user2->id);
2200         $this->assertEquals(0, $DB->count_records('message_contact_requests'));
2202         $contact = $DB->get_records('message_contacts');
2204         $this->assertCount(1, $contact);
2206         $contact = reset($contact);
2208         $this->assertEquals($user1->id, $contact->userid);
2209         $this->assertEquals($user2->id, $contact->contactid);
2210     }
2212     /**
2213      * Test declining a contact request.
2214      */
2215     public function test_decline_contact_request() {
2216         global $DB;
2218         $user1 = self::getDataGenerator()->create_user();
2219         $user2 = self::getDataGenerator()->create_user();
2221         \core_message\api::create_contact_request($user1->id, $user2->id);
2223         \core_message\api::decline_contact_request($user1->id, $user2->id);
2225         $this->assertEquals(0, $DB->count_records('message_contact_requests'));
2226         $this->assertEquals(0, $DB->count_records('message_contacts'));
2227     }
2229     /**
2230      * Test retrieving contact requests.
2231      */
2232     public function test_get_contact_requests() {
2233         $user1 = self::getDataGenerator()->create_user();
2234         $user2 = self::getDataGenerator()->create_user();
2235         $user3 = self::getDataGenerator()->create_user();
2237         // Block one user, their request should not show up.
2238         \core_message\api::block_user($user1->id, $user3->id);
2240         \core_message\api::create_contact_request($user2->id, $user1->id);
2241         \core_message\api::create_contact_request($user3->id, $user1->id);
2243         $requests = \core_message\api::get_contact_requests($user1->id);
2245         $this->assertCount(1, $requests);
2247         $request = reset($requests);
2249         $this->assertEquals($user2->id, $request->id);
2250         $this->assertEquals($user2->picture, $request->picture);
2251         $this->assertEquals($user2->firstname, $request->firstname);
2252         $this->assertEquals($user2->lastname, $request->lastname);
2253         $this->assertEquals($user2->firstnamephonetic, $request->firstnamephonetic);
2254         $this->assertEquals($user2->lastnamephonetic, $request->lastnamephonetic);
2255         $this->assertEquals($user2->middlename, $request->middlename);
2256         $this->assertEquals($user2->alternatename, $request->alternatename);
2257         $this->assertEquals($user2->email, $request->email);
2258     }
2260     /**
2261      * Test adding contacts.
2262      */
2263     public function test_add_contact() {
2264         global $DB;
2266         $user1 = self::getDataGenerator()->create_user();
2267         $user2 = self::getDataGenerator()->create_user();
2269         \core_message\api::add_contact($user1->id, $user2->id);
2271         $contact = $DB->get_records('message_contacts');
2273         $this->assertCount(1, $contact);
2275         $contact = reset($contact);
2277         $this->assertEquals($user1->id, $contact->userid);
2278         $this->assertEquals($user2->id, $contact->contactid);
2279     }
2281     /**
2282      * Test removing contacts.
2283      */
2284     public function test_remove_contact() {
2285         global $DB;
2287         $user1 = self::getDataGenerator()->create_user();
2288         $user2 = self::getDataGenerator()->create_user();
2290         \core_message\api::add_contact($user1->id, $user2->id);
2291         \core_message\api::remove_contact($user1->id, $user2->id);
2293         $this->assertEquals(0, $DB->count_records('message_contacts'));
2294     }
2296     /**
2297      * Test blocking users.
2298      */
2299     public function test_block_user() {
2300         global $DB;
2302         $user1 = self::getDataGenerator()->create_user();
2303         $user2 = self::getDataGenerator()->create_user();
2305         \core_message\api::block_user($user1->id, $user2->id);
2307         $blockedusers = $DB->get_records('message_users_blocked');
2309         $this->assertCount(1, $blockedusers);
2311         $blockeduser = reset($blockedusers);
2313         $this->assertEquals($user1->id, $blockeduser->userid);
2314         $this->assertEquals($user2->id, $blockeduser->blockeduserid);
2315     }
2317     /**
2318      * Test unblocking users.
2319      */
2320     public function test_unblock_user() {
2321         global $DB;
2323         $user1 = self::getDataGenerator()->create_user();
2324         $user2 = self::getDataGenerator()->create_user();
2326         \core_message\api::block_user($user1->id, $user2->id);
2327         \core_message\api::unblock_user($user1->id, $user2->id);
2329         $this->assertEquals(0, $DB->count_records('message_users_blocked'));
2330     }
2332     /**
2333      * Test is contact check.
2334      */
2335     public function test_is_contact() {
2336         $user1 = self::getDataGenerator()->create_user();
2337         $user2 = self::getDataGenerator()->create_user();
2338         $user3 = self::getDataGenerator()->create_user();
2340         \core_message\api::add_contact($user1->id, $user2->id);
2342         $this->assertTrue(\core_message\api::is_contact($user1->id, $user2->id));
2343         $this->assertTrue(\core_message\api::is_contact($user2->id, $user1->id));
2344         $this->assertFalse(\core_message\api::is_contact($user2->id, $user3->id));
2345     }
2347     /**
2348      * Test get contact.
2349      */
2350     public function test_get_contact() {
2351         $user1 = self::getDataGenerator()->create_user();
2352         $user2 = self::getDataGenerator()->create_user();
2354         \core_message\api::add_contact($user1->id, $user2->id);
2356         $contact = \core_message\api::get_contact($user1->id, $user2->id);
2358         $this->assertEquals($user1->id, $contact->userid);
2359         $this->assertEquals($user2->id, $contact->contactid);
2360     }
2362     /**
2363      * Test is blocked checked.
2364      */
2365     public function test_is_blocked() {
2366         $user1 = self::getDataGenerator()->create_user();
2367         $user2 = self::getDataGenerator()->create_user();
2369         $this->assertFalse(\core_message\api::is_blocked($user1->id, $user2->id));
2370         $this->assertFalse(\core_message\api::is_blocked($user2->id, $user1->id));
2372         \core_message\api::block_user($user1->id, $user2->id);
2374         $this->assertTrue(\core_message\api::is_blocked($user1->id, $user2->id));
2375         $this->assertFalse(\core_message\api::is_blocked($user2->id, $user1->id));
2376     }
2378     /**
2379      * Test the contact request exist check.
2380      */
2381     public function test_does_contact_request_exist() {
2382         $user1 = self::getDataGenerator()->create_user();
2383         $user2 = self::getDataGenerator()->create_user();
2385         $this->assertFalse(\core_message\api::does_contact_request_exist($user1->id, $user2->id));
2386         $this->assertFalse(\core_message\api::does_contact_request_exist($user2->id, $user1->id));
2388         \core_message\api::create_contact_request($user1->id, $user2->id);
2390         $this->assertTrue(\core_message\api::does_contact_request_exist($user1->id, $user2->id));
2391         $this->assertTrue(\core_message\api::does_contact_request_exist($user2->id, $user1->id));
2392     }
2394     /**
2395      * Test the user in conversation check.
2396      */
2397     public function test_is_user_in_conversation() {
2398         $user1 = self::getDataGenerator()->create_user();
2399         $user2 = self::getDataGenerator()->create_user();
2401         $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
2403         $this->assertTrue(\core_message\api::is_user_in_conversation($user1->id, $conversationid));
2404     }
2406     /**
2407      * Test the user in conversation check when they are not.
2408      */
2409     public function test_is_user_in_conversation_when_not() {
2410         $user1 = self::getDataGenerator()->create_user();
2411         $user2 = self::getDataGenerator()->create_user();
2412         $user3 = self::getDataGenerator()->create_user();
2414         $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
2416         $this->assertFalse(\core_message\api::is_user_in_conversation($user3->id, $conversationid));
2417     }
2419     /**
2420      * Comparison function for sorting contacts.
2421      *
2422      * @param stdClass $a
2423      * @param stdClass $b
2424      * @return bool
2425      */
2426     protected static function sort_contacts($a, $b) {
2427         return $a->userid > $b->userid;
2428     }