3bcb7d76025820006eb3c39154edb308fe39f30d
[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_sendmessage_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         // By default, user only can be messaged by contacts and members of any of his/her courses.
1445         $this->assertFalse(\core_message\api::can_post_message($user2));
1447         // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
1448         set_config('messagingallusers', true);
1450         // Set the second user's preference to receive messages from everybody.
1451         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user2->id);
1453         // Check that we can send user2 a message.
1454         $this->assertTrue(\core_message\api::can_post_message($user2));
1456         // Disable site-wide messagging privacy setting. The user will be able to receive messages from contacts
1457         // and members sharing a course with her.
1458         set_config('messagingallusers', false);
1460         // As site-wide messaging setting is disabled, the value for user2 will be changed to MESSAGE_PRIVACY_COURSEMEMBER.
1461         $this->assertFalse(\core_message\api::can_post_message($user2));
1463         // Enrol users to the same course.
1464         $course = $this->getDataGenerator()->create_course();
1465         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1466         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
1467         // Check that we can send user2 a message because they are sharing a course.
1468         $this->assertTrue(\core_message\api::can_post_message($user2));
1470         // Set the second user's preference to receive messages only from contacts.
1471         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1472         // Check that now the user2 can't be contacted because user1 is not their contact.
1473         $this->assertFalse(\core_message\api::can_post_message($user2));
1475         // Make contacts user1 and user2.
1476         \core_message\api::add_contact($user2->id, $user1->id);
1477         // Check that we can send user2 a message because they are contacts.
1478         $this->assertTrue(\core_message\api::can_post_message($user2));
1479     }
1481     /**
1482      * Tests the user with the messageanyuser capability can post a message.
1483      */
1484     public function test_can_post_message_with_messageanyuser_cap() {
1485         global $DB;
1487         // Create some users.
1488         $teacher1 = self::getDataGenerator()->create_user();
1489         $student1 = self::getDataGenerator()->create_user();
1490         $student2 = self::getDataGenerator()->create_user();
1492         // Create users not enrolled in any course.
1493         $user1 = self::getDataGenerator()->create_user();
1495         // Create a course.
1496         $course1 = $this->getDataGenerator()->create_course();
1498         // Enrol the users in the course.
1499         $this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, 'editingteacher');
1500         $this->getDataGenerator()->enrol_user($student1->id, $course1->id, 'student');
1501         $this->getDataGenerator()->enrol_user($student2->id, $course1->id, 'student');
1503         // Set some student preferences to not receive messages from non-contacts.
1504         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $student1->id);
1506         // Check that we can send student1 a message because teacher has the messageanyuser cap by default.
1507         $this->assertTrue(\core_message\api::can_post_message($student1, $teacher1));
1508         // Check that the teacher can't contact user1 because it's not his teacher.
1509         $this->assertFalse(\core_message\api::can_post_message($user1, $teacher1));
1511         // Remove the messageanyuser capability from the course1 for teachers.
1512         $coursecontext = context_course::instance($course1->id);
1513         $teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
1514         assign_capability('moodle/site:messageanyuser', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id);
1515         $coursecontext->mark_dirty();
1517         // Check that we can't send user1 a message because they are not contacts.
1518         $this->assertFalse(\core_message\api::can_post_message($student1, $teacher1));
1519         // However, teacher can message student2 because they are sharing a course.
1520         $this->assertTrue(\core_message\api::can_post_message($student2, $teacher1));
1521     }
1523     /**
1524      * Tests get_user_privacy_messaging_preference method.
1525      */
1526     public function test_get_user_privacy_messaging_preference() {
1527         // Create some users.
1528         $user1 = self::getDataGenerator()->create_user();
1529         $user2 = self::getDataGenerator()->create_user();
1530         $user3 = self::getDataGenerator()->create_user();
1532         // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
1533         set_config('messagingallusers', true);
1535         // Set some user preferences.
1536         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user1->id);
1537         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1539         // Check the returned value for each user.
1540         $this->assertEquals(
1541             \core_message\api::MESSAGE_PRIVACY_SITE,
1542             \core_message\api::get_user_privacy_messaging_preference($user1->id)
1543         );
1544         $this->assertEquals(
1545             \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
1546             \core_message\api::get_user_privacy_messaging_preference($user2->id)
1547         );
1548         $this->assertEquals(
1549             \core_message\api::MESSAGE_PRIVACY_SITE,
1550             \core_message\api::get_user_privacy_messaging_preference($user3->id)
1551         );
1553         // Disable site-wide messagging privacy setting. The user will be able to receive messages from members of their course.
1554         set_config('messagingallusers', false);
1556         // Check the returned value for each user.
1557         $this->assertEquals(
1558             \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
1559             \core_message\api::get_user_privacy_messaging_preference($user1->id)
1560         );
1561         $this->assertEquals(
1562             \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
1563             \core_message\api::get_user_privacy_messaging_preference($user2->id)
1564         );
1565         $this->assertEquals(
1566             \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
1567             \core_message\api::get_user_privacy_messaging_preference($user3->id)
1568         );
1569     }
1571     /**
1572      * Tests that when blocking messages from non-contacts is enabled that
1573      * non-contacts trying to send a message return false.
1574      */
1575     public function test_is_user_non_contact_blocked() {
1576         // Create some users.
1577         $user1 = self::getDataGenerator()->create_user();
1578         $user2 = self::getDataGenerator()->create_user();
1580         // Set as the first user.
1581         $this->setUser($user1);
1583         // By default, user only can be messaged by contacts and members of any of his/her courses.
1584         $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
1586         // Enable all users privacy messaging and check now the default user's preference has been set to allow receiving
1587         // messages from everybody.
1588         set_config('messagingallusers', true);
1589         // Check that the return result is now false because any site user can contact him/her.
1590         $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1592         // Set the second user's preference to not receive messages from non-contacts.
1593         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
1594         // Check that the return result is still true (because is even more restricted).
1595         $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
1597         // Add the first user as a contact for the second user.
1598         \core_message\api::add_contact($user2->id, $user1->id);
1600         // Check that the return result is now false.
1601         $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1603         // Set the second user's preference to receive messages from course members.
1604         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER, $user2->id);
1605         // Check that the return result is still false (because $user1 is still his/her contact).
1606         $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1607     }
1609     /**
1610      * Tests that we return true when a user is blocked, or false
1611      * if they are not blocked.
1612      */
1613     public function test_is_user_blocked() {
1614         // Create some users.
1615         $user1 = self::getDataGenerator()->create_user();
1616         $user2 = self::getDataGenerator()->create_user();
1618         // Set the user.
1619         $this->setUser($user1);
1621         // User shouldn't be blocked.
1622         $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1623         $this->assertDebuggingCalled();
1625         // Block the user.
1626         \core_message\api::block_user($user1->id, $user2->id);
1628         // User should be blocked.
1629         $this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
1630         $this->assertDebuggingCalled();
1632         // Unblock the user.
1633         \core_message\api::unblock_user($user1->id, $user2->id);
1634         $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1635         $this->assertDebuggingCalled();
1636     }
1638     /**
1639      * Tests that the admin is not blocked even if someone has chosen to block them.
1640      */
1641     public function test_is_user_blocked_as_admin() {
1642         // Create a user.
1643         $user1 = self::getDataGenerator()->create_user();
1645         // Set the user.
1646         $this->setUser($user1);
1648         // Block the admin user.
1649         \core_message\api::block_user($user1->id, 2);
1651         // Now change to the admin user.
1652         $this->setAdminUser();
1654         // As the admin you should still be able to send messages to the user.
1655         $this->assertFalse(\core_message\api::is_user_blocked($user1->id));
1656         $this->assertDebuggingCalled();
1657     }
1659     /*
1660      * Tes get_message_processor api.
1661      */
1662     public function test_get_message_processor() {
1663         $processors = get_message_processors(true);
1664         if (empty($processors)) {
1665             $this->markTestSkipped("No message processors found");
1666         }
1668         $name = key($processors);
1669         $processor = current($processors);
1670         $testprocessor = \core_message\api::get_message_processor($name);
1671         $this->assertEquals($processor->name, $testprocessor->name);
1672         $this->assertEquals($processor->enabled, $testprocessor->enabled);
1673         $this->assertEquals($processor->available, $testprocessor->available);
1674         $this->assertEquals($processor->configured, $testprocessor->configured);
1676         // Disable processor and test.
1677         \core_message\api::update_processor_status($testprocessor, 0);
1678         $testprocessor = \core_message\api::get_message_processor($name, true);
1679         $this->assertEmpty($testprocessor);
1680         $testprocessor = \core_message\api::get_message_processor($name);
1681         $this->assertEquals($processor->name, $testprocessor->name);
1682         $this->assertEquals(0, $testprocessor->enabled);
1684         // Enable again and test.
1685         \core_message\api::update_processor_status($testprocessor, 1);
1686         $testprocessor = \core_message\api::get_message_processor($name, true);
1687         $this->assertEquals($processor->name, $testprocessor->name);
1688         $this->assertEquals(1, $testprocessor->enabled);
1689         $testprocessor = \core_message\api::get_message_processor($name);
1690         $this->assertEquals($processor->name, $testprocessor->name);
1691         $this->assertEquals(1, $testprocessor->enabled);
1692     }
1694     /**
1695      * Test method update_processor_status.
1696      */
1697     public function test_update_processor_status() {
1698         $processors = get_message_processors();
1699         if (empty($processors)) {
1700             $this->markTestSkipped("No message processors found");
1701         }
1702         $name = key($processors);
1703         $testprocessor = current($processors);
1705         // Enable.
1706         \core_message\api::update_processor_status($testprocessor, 1);
1707         $testprocessor = \core_message\api::get_message_processor($name);
1708         $this->assertEquals(1, $testprocessor->enabled);
1710         // Disable.
1711         \core_message\api::update_processor_status($testprocessor, 0);
1712         $testprocessor = \core_message\api::get_message_processor($name);
1713         $this->assertEquals(0, $testprocessor->enabled);
1715         // Enable again.
1716         \core_message\api::update_processor_status($testprocessor, 1);
1717         $testprocessor = \core_message\api::get_message_processor($name);
1718         $this->assertEquals(1, $testprocessor->enabled);
1719     }
1721     /**
1722      * Test method is_user_enabled.
1723      */
1724     public function is_user_enabled() {
1725         $processors = get_message_processors();
1726         if (empty($processors)) {
1727             $this->markTestSkipped("No message processors found");
1728         }
1729         $name = key($processors);
1730         $testprocessor = current($processors);
1732         // Enable.
1733         \core_message\api::update_processor_status($testprocessor, 1);
1734         $status = \core_message\api::is_processor_enabled($name);
1735         $this->assertEquals(1, $status);
1737         // Disable.
1738         \core_message\api::update_processor_status($testprocessor, 0);
1739         $status = \core_message\api::is_processor_enabled($name);
1740         $this->assertEquals(0, $status);
1742         // Enable again.
1743         \core_message\api::update_processor_status($testprocessor, 1);
1744         $status = \core_message\api::is_processor_enabled($name);
1745         $this->assertEquals(1, $status);
1746     }
1748     /**
1749      * Test retrieving messages by providing a minimum timecreated value.
1750      */
1751     public function test_get_messages_time_from_only() {
1752         // Create some users.
1753         $user1 = self::getDataGenerator()->create_user();
1754         $user2 = self::getDataGenerator()->create_user();
1756         // The person doing the search.
1757         $this->setUser($user1);
1759         // Send some messages back and forth.
1760         $time = 1;
1761         $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1762         $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1763         $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1764         $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1766         // Retrieve the messages from $time, which should be all of them.
1767         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
1769         // Confirm the message data is correct.
1770         $this->assertEquals(4, count($messages));
1772         $message1 = $messages[0];
1773         $message2 = $messages[1];
1774         $message3 = $messages[2];
1775         $message4 = $messages[3];
1777         $this->assertContains('Message 1', $message1->text);
1778         $this->assertContains('Message 2', $message2->text);
1779         $this->assertContains('Message 3', $message3->text);
1780         $this->assertContains('Message 4', $message4->text);
1782         // Retrieve the messages from $time + 3, which should only be the 2 last messages.
1783         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
1785         // Confirm the message data is correct.
1786         $this->assertEquals(2, count($messages));
1788         $message1 = $messages[0];
1789         $message2 = $messages[1];
1791         $this->assertContains('Message 3', $message1->text);
1792         $this->assertContains('Message 4', $message2->text);
1793     }
1795     /**
1796      * Test retrieving messages by providing a maximum timecreated value.
1797      */
1798     public function test_get_messages_time_to_only() {
1799         // Create some users.
1800         $user1 = self::getDataGenerator()->create_user();
1801         $user2 = self::getDataGenerator()->create_user();
1803         // The person doing the search.
1804         $this->setUser($user1);
1806         // Send some messages back and forth.
1807         $time = 1;
1808         $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1809         $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1810         $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1811         $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1813         // Retrieve the messages up until $time + 4, which should be all of them.
1814         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
1816         // Confirm the message data is correct.
1817         $this->assertEquals(4, count($messages));
1819         $message1 = $messages[0];
1820         $message2 = $messages[1];
1821         $message3 = $messages[2];
1822         $message4 = $messages[3];
1824         $this->assertContains('Message 1', $message1->text);
1825         $this->assertContains('Message 2', $message2->text);
1826         $this->assertContains('Message 3', $message3->text);
1827         $this->assertContains('Message 4', $message4->text);
1829         // Retrieve the messages up until $time + 2, which should be the first two.
1830         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
1832         // Confirm the message data is correct.
1833         $this->assertEquals(2, count($messages));
1835         $message1 = $messages[0];
1836         $message2 = $messages[1];
1838         $this->assertContains('Message 1', $message1->text);
1839         $this->assertContains('Message 2', $message2->text);
1840     }
1842     /**
1843      * Test retrieving messages by providing a minimum and maximum timecreated value.
1844      */
1845     public function test_get_messages_time_from_and_to() {
1846         // Create some users.
1847         $user1 = self::getDataGenerator()->create_user();
1848         $user2 = self::getDataGenerator()->create_user();
1850         // The person doing the search.
1851         $this->setUser($user1);
1853         // Send some messages back and forth.
1854         $time = 1;
1855         $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1856         $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1857         $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1858         $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1860         // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
1861         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
1863         // Confirm the message data is correct.
1864         $this->assertEquals(2, count($messages));
1866         $message1 = $messages[0];
1867         $message2 = $messages[1];
1869         $this->assertContains('Message 2', $message1->text);
1870         $this->assertContains('Message 3', $message2->text);
1871     }
1873     /**
1874      * Test returning blocked users.
1875      */
1876     public function test_get_blocked_users() {
1877         global $USER;
1879         // Set this user as the admin.
1880         $this->setAdminUser();
1882         // Create a user to add to the admin's contact list.
1883         $user1 = $this->getDataGenerator()->create_user();
1884         $user2 = $this->getDataGenerator()->create_user();
1886         // Add users to the admin's contact list.
1887         \core_message\api::block_user($USER->id, $user2->id);
1889         $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
1891         // Block other user.
1892         \core_message\api::block_user($USER->id, $user1->id);
1893         $this->assertCount(2, \core_message\api::get_blocked_users($USER->id));
1895         // Test deleting users.
1896         delete_user($user1);
1897         $this->assertCount(1, \core_message\api::get_blocked_users($USER->id));
1898     }
1900     /**
1901      * Test returning contacts with unread message count.
1902      */
1903     public function test_get_contacts_with_unread_message_count() {
1904         global $DB;
1906         $user1 = self::getDataGenerator()->create_user();
1907         $user2 = self::getDataGenerator()->create_user();
1908         $user3 = self::getDataGenerator()->create_user();
1909         $user4 = self::getDataGenerator()->create_user();
1911         // Add the users to each of their contacts.
1912         \core_message\api::add_contact($user1->id, $user2->id);
1913         \core_message\api::add_contact($user2->id, $user3->id);
1915         $this->send_fake_message($user1, $user2);
1916         $this->send_fake_message($user1, $user2);
1917         $this->send_fake_message($user1, $user2);
1918         $message4id = $this->send_fake_message($user1, $user2);
1920         $this->send_fake_message($user3, $user2);
1921         $message6id = $this->send_fake_message($user3, $user2);
1922         $this->send_fake_message($user3, $user2);
1923         $this->send_fake_message($user3, $user2);
1924         $this->send_fake_message($user3, $user2);
1926         // Send a message that should never be included as the user is not a contact.
1927         $this->send_fake_message($user4, $user2);
1929         // Get the contacts and the unread message count.
1930         $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1932         // Confirm the size is correct.
1933         $this->assertCount(2, $messages);
1934         ksort($messages);
1936         $messageinfo1 = array_shift($messages);
1937         $messageinfo2 = array_shift($messages);
1939         $this->assertEquals($user1->id, $messageinfo1->id);
1940         $this->assertEquals(4, $messageinfo1->messagecount);
1941         $this->assertEquals($user3->id, $messageinfo2->id);
1942         $this->assertEquals(5, $messageinfo2->messagecount);
1944         // Mark some of the messages as read.
1945         $m4 = $DB->get_record('messages', ['id' => $message4id]);
1946         $m6 = $DB->get_record('messages', ['id' => $message6id]);
1947         \core_message\api::mark_message_as_read($user2->id, $m4);
1948         \core_message\api::mark_message_as_read($user2->id, $m6);
1950         // Get the contacts and the unread message count.
1951         $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
1953         // Confirm the size is correct.
1954         $this->assertCount(2, $messages);
1955         ksort($messages);
1957         // Confirm read messages are not included.
1958         $messageinfo1 = array_shift($messages);
1959         $messageinfo2 = array_shift($messages);
1960         $this->assertEquals($user1->id, $messageinfo1->id);
1961         $this->assertEquals(3, $messageinfo1->messagecount);
1962         $this->assertEquals($user3->id, $messageinfo2->id);
1963         $this->assertEquals(4, $messageinfo2->messagecount);
1965         // Now, let's populate the database with messages from user2 to user 1.
1966         $this->send_fake_message($user2, $user1);
1967         $this->send_fake_message($user2, $user1);
1968         $messageid = $this->send_fake_message($user2, $user1);
1970         // Send a message that should never be included as the user is not a contact.
1971         $this->send_fake_message($user4, $user1);
1973         // Get the contacts and the unread message count.
1974         $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
1976         // Confirm the size is correct.
1977         $this->assertCount(1, $messages);
1978         $messageinfo1 = array_shift($messages);
1979         $this->assertEquals($user2->id, $messageinfo1->id);
1980         $this->assertEquals(3, $messageinfo1->messagecount);
1982         // Mark the last message as read.
1983         $m = $DB->get_record('messages', ['id' => $messageid]);
1984         \core_message\api::mark_message_as_read($user1->id, $m);
1986         $messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
1988         // Confirm the size is correct.
1989         $this->assertCount(1, $messages);
1991         // Confirm read messages are not included.
1992         $messageinfo1 = array_shift($messages);
1993         $this->assertEquals($user2->id, $messageinfo1->id);
1994         $this->assertEquals(2, $messageinfo1->messagecount);
1995     }
1997     /**
1998      * Test returning contacts with unread message count when there are no messages.
1999      */
2000     public function test_get_contacts_with_unread_message_count_no_messages() {
2001         $user1 = self::getDataGenerator()->create_user();
2002         $user2 = self::getDataGenerator()->create_user();
2004         // Add the users to each of their contacts.
2005         \core_message\api::add_contact($user2->id, $user1->id);
2007         // Check we get the correct message count.
2008         $messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
2010         // Confirm the size is correct.
2011         $this->assertCount(1, $messages);
2013         $messageinfo = array_shift($messages);
2015         $this->assertEquals($user1->id, $messageinfo->id);
2016         $this->assertEquals(0, $messageinfo->messagecount);
2017     }
2019     /**
2020      * Test returning non-contacts with unread message count.
2021      */
2022     public function test_get_non_contacts_with_unread_message_count() {
2023         global $DB;
2025         $user1 = self::getDataGenerator()->create_user();
2026         $user2 = self::getDataGenerator()->create_user();
2027         $user3 = self::getDataGenerator()->create_user();
2028         $user4 = self::getDataGenerator()->create_user();
2030         // Add a user to the contact list of the users we are testing this function with.
2031         \core_message\api::add_contact($user1->id, $user4->id);
2032         \core_message\api::add_contact($user2->id, $user4->id);
2034         $this->send_fake_message($user1, $user2);
2035         $this->send_fake_message($user1, $user2);
2036         $this->send_fake_message($user1, $user2);
2037         $message4id = $this->send_fake_message($user1, $user2);
2039         $this->send_fake_message($user3, $user2);
2040         $message6id = $this->send_fake_message($user3, $user2);
2041         $this->send_fake_message($user3, $user2);
2042         $this->send_fake_message($user3, $user2);
2043         $this->send_fake_message($user3, $user2);
2045         // Send a message that should never be included as the user is a contact.
2046         $this->send_fake_message($user4, $user2);
2048         // Get the non-contacts and the unread message count.
2049         $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
2051         // Check we get the correct message count.
2052         ksort($messages);
2053         $this->assertCount(2, $messages);
2054         $messageinfo1 = array_shift($messages);
2055         $messageinfo2 = array_shift($messages);
2056         $this->assertEquals($user1->id, $messageinfo1->id);
2057         $this->assertEquals(4, $messageinfo1->messagecount);
2058         $this->assertEquals($user3->id, $messageinfo2->id);
2059         $this->assertEquals(5, $messageinfo2->messagecount);
2061         // Mark some of the messages as read.
2062         $m4 = $DB->get_record('messages', ['id' => $message4id]);
2063         $m6 = $DB->get_record('messages', ['id' => $message6id]);
2064         \core_message\api::mark_message_as_read($user2->id, $m4);
2065         \core_message\api::mark_message_as_read($user2->id, $m6);
2067         // Get the non-contacts and the unread message count.
2068         $messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
2070         // Check the marked message is not returned in the message count.
2071         ksort($messages);
2072         $this->assertCount(2, $messages);
2073         $messageinfo1 = array_shift($messages);
2074         $messageinfo2 = array_shift($messages);
2075         $this->assertEquals($user1->id, $messageinfo1->id);
2076         $this->assertEquals(3, $messageinfo1->messagecount);
2077         $this->assertEquals($user3->id, $messageinfo2->id);
2078         $this->assertEquals(4, $messageinfo2->messagecount);
2080         // Now, let's populate the database with messages from user2 to user 1.
2081         $this->send_fake_message($user2, $user1);
2082         $this->send_fake_message($user2, $user1);
2083         $messageid = $this->send_fake_message($user2, $user1);
2085         // Send a message that should never be included as the user is a contact.
2086         $this->send_fake_message($user4, $user1);
2088         // Get the non-contacts and the unread message count.
2089         $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
2091         // Confirm the size is correct.
2092         $this->assertCount(1, $messages);
2093         $messageinfo1 = array_shift($messages);
2094         $this->assertEquals($user2->id, $messageinfo1->id);
2095         $this->assertEquals(3, $messageinfo1->messagecount);
2097         // Mark the last message as read.
2098         $m = $DB->get_record('messages', ['id' => $messageid]);
2099         \core_message\api::mark_message_as_read($user1->id, $m);
2101         // Get the non-contacts and the unread message count.
2102         $messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
2104         // Check the marked message is not returned in the message count.
2105         $this->assertCount(1, $messages);
2106         $messageinfo1 = array_shift($messages);
2107         $this->assertEquals($user2->id, $messageinfo1->id);
2108         $this->assertEquals(2, $messageinfo1->messagecount);
2109     }
2111     /**
2112      * Test marking a message as read.
2113      */
2114     public function test_mark_message_as_read() {
2115         global $DB;
2117         $user1 = self::getDataGenerator()->create_user();
2118         $user2 = self::getDataGenerator()->create_user();
2120         $this->send_fake_message($user1, $user2);
2121         $m2id = $this->send_fake_message($user1, $user2);
2122         $this->send_fake_message($user2, $user1);
2123         $m4id = $this->send_fake_message($user2, $user1);
2125         $m2 = $DB->get_record('messages', ['id' => $m2id]);
2126         $m4 = $DB->get_record('messages', ['id' => $m4id]);
2127         \core_message\api::mark_message_as_read($user2->id, $m2, 11);
2128         \core_message\api::mark_message_as_read($user1->id, $m4, 12);
2130         // Confirm there are two user actions.
2131         $muas = $DB->get_records('message_user_actions', [], 'timecreated ASC');
2132         $this->assertEquals(2, count($muas));
2134         // Confirm they are correct.
2135         $mua1 = array_shift($muas);
2136         $mua2 = array_shift($muas);
2138         // Confirm first action.
2139         $this->assertEquals($user2->id, $mua1->userid);
2140         $this->assertEquals($m2id, $mua1->messageid);
2141         $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua1->action);
2142         $this->assertEquals(11, $mua1->timecreated);
2144         // Confirm second action.
2145         $this->assertEquals($user1->id, $mua2->userid);
2146         $this->assertEquals($m4id, $mua2->messageid);
2147         $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua2->action);
2148         $this->assertEquals(12, $mua2->timecreated);
2149     }
2151     /**
2152      * Test marking a notification as read.
2153      */
2154     public function test_mark_notification_as_read() {
2155         global $DB;
2157         $user1 = self::getDataGenerator()->create_user();
2158         $user2 = self::getDataGenerator()->create_user();
2160         $this->send_fake_message($user1, $user2, 'Notification 1', 1);
2161         $n2id = $this->send_fake_message($user1, $user2, 'Notification 2', 1);
2162         $this->send_fake_message($user2, $user1, 'Notification 3', 1);
2163         $n4id = $this->send_fake_message($user2, $user1, 'Notification 4', 1);
2165         $n2 = $DB->get_record('notifications', ['id' => $n2id]);
2166         $n4 = $DB->get_record('notifications', ['id' => $n4id]);
2168         \core_message\api::mark_notification_as_read($n2, 11);
2169         \core_message\api::mark_notification_as_read($n4, 12);
2171         // Retrieve the notifications.
2172         $n2 = $DB->get_record('notifications', ['id' => $n2id]);
2173         $n4 = $DB->get_record('notifications', ['id' => $n4id]);
2175         // Confirm they have been marked as read.
2176         $this->assertEquals(11, $n2->timeread);
2177         $this->assertEquals(12, $n4->timeread);
2178     }
2180     /**
2181      * Test a conversation is not returned if there is none.
2182      */
2183     public function test_get_conversation_between_users_no_conversation() {
2184         $user1 = self::getDataGenerator()->create_user();
2185         $user2 = self::getDataGenerator()->create_user();
2187         $this->assertFalse(\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
2188     }
2190     /**
2191      * Test we can return a conversation that exists between users.
2192      */
2193     public function test_get_conversation_between_users_with_existing_conversation() {
2194         $user1 = self::getDataGenerator()->create_user();
2195         $user2 = self::getDataGenerator()->create_user();
2197         $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
2199         $this->assertEquals($conversationid,
2200             \core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
2201     }
2203     /**
2204      * Test can create a contact request.
2205      */
2206     public function test_can_create_contact_request() {
2207         global $CFG;
2209         $user1 = self::getDataGenerator()->create_user();
2210         $user2 = self::getDataGenerator()->create_user();
2212         // Disable messaging.
2213         $CFG->messaging = 0;
2214         $this->assertFalse(\core_message\api::can_create_contact($user1->id, $user2->id));
2216         // Re-enable messaging.
2217         $CFG->messaging = 1;
2219         // Allow users to message anyone site-wide.
2220         $CFG->messagingallusers = 1;
2221         $this->assertTrue(\core_message\api::can_create_contact($user1->id, $user2->id));
2223         // Disallow users from messaging anyone site-wide.
2224         $CFG->messagingallusers = 0;
2225         $this->assertFalse(\core_message\api::can_create_contact($user1->id, $user2->id));
2227         // Put the users in the same course so a contact request should be possible.
2228         $course = self::getDataGenerator()->create_course();
2229         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
2230         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
2231         $this->assertTrue(\core_message\api::can_create_contact($user1->id, $user2->id));
2232     }
2234     /**
2235      * Test creating a contact request.
2236      */
2237     public function test_create_contact_request() {
2238         global $DB;
2240         $user1 = self::getDataGenerator()->create_user();
2241         $user2 = self::getDataGenerator()->create_user();
2243         \core_message\api::create_contact_request($user1->id, $user2->id);
2245         $request = $DB->get_records('message_contact_requests');
2247         $this->assertCount(1, $request);
2249         $request = reset($request);
2251         $this->assertEquals($user1->id, $request->userid);
2252         $this->assertEquals($user2->id, $request->requesteduserid);
2253     }
2255     /**
2256      * Test confirming a contact request.
2257      */
2258     public function test_confirm_contact_request() {
2259         global $DB;
2261         $user1 = self::getDataGenerator()->create_user();
2262         $user2 = self::getDataGenerator()->create_user();
2264         \core_message\api::create_contact_request($user1->id, $user2->id);
2266         \core_message\api::confirm_contact_request($user1->id, $user2->id);
2268         $this->assertEquals(0, $DB->count_records('message_contact_requests'));
2270         $contact = $DB->get_records('message_contacts');
2272         $this->assertCount(1, $contact);
2274         $contact = reset($contact);
2276         $this->assertEquals($user1->id, $contact->userid);
2277         $this->assertEquals($user2->id, $contact->contactid);
2278     }
2280     /**
2281      * Test declining a contact request.
2282      */
2283     public function test_decline_contact_request() {
2284         global $DB;
2286         $user1 = self::getDataGenerator()->create_user();
2287         $user2 = self::getDataGenerator()->create_user();
2289         \core_message\api::create_contact_request($user1->id, $user2->id);
2291         \core_message\api::decline_contact_request($user1->id, $user2->id);
2293         $this->assertEquals(0, $DB->count_records('message_contact_requests'));
2294         $this->assertEquals(0, $DB->count_records('message_contacts'));
2295     }
2297     /**
2298      * Test retrieving contact requests.
2299      */
2300     public function test_get_contact_requests() {
2301         $user1 = self::getDataGenerator()->create_user();
2302         $user2 = self::getDataGenerator()->create_user();
2303         $user3 = self::getDataGenerator()->create_user();
2305         // Block one user, their request should not show up.
2306         \core_message\api::block_user($user1->id, $user3->id);
2308         \core_message\api::create_contact_request($user2->id, $user1->id);
2309         \core_message\api::create_contact_request($user3->id, $user1->id);
2311         $requests = \core_message\api::get_contact_requests($user1->id);
2313         $this->assertCount(1, $requests);
2315         $request = reset($requests);
2317         $this->assertEquals($user2->id, $request->id);
2318         $this->assertEquals($user2->picture, $request->picture);
2319         $this->assertEquals($user2->firstname, $request->firstname);
2320         $this->assertEquals($user2->lastname, $request->lastname);
2321         $this->assertEquals($user2->firstnamephonetic, $request->firstnamephonetic);
2322         $this->assertEquals($user2->lastnamephonetic, $request->lastnamephonetic);
2323         $this->assertEquals($user2->middlename, $request->middlename);
2324         $this->assertEquals($user2->alternatename, $request->alternatename);
2325         $this->assertEquals($user2->email, $request->email);
2326     }
2328     /**
2329      * Test adding contacts.
2330      */
2331     public function test_add_contact() {
2332         global $DB;
2334         $user1 = self::getDataGenerator()->create_user();
2335         $user2 = self::getDataGenerator()->create_user();
2337         \core_message\api::add_contact($user1->id, $user2->id);
2339         $contact = $DB->get_records('message_contacts');
2341         $this->assertCount(1, $contact);
2343         $contact = reset($contact);
2345         $this->assertEquals($user1->id, $contact->userid);
2346         $this->assertEquals($user2->id, $contact->contactid);
2347     }
2349     /**
2350      * Test removing contacts.
2351      */
2352     public function test_remove_contact() {
2353         global $DB;
2355         $user1 = self::getDataGenerator()->create_user();
2356         $user2 = self::getDataGenerator()->create_user();
2358         \core_message\api::add_contact($user1->id, $user2->id);
2359         \core_message\api::remove_contact($user1->id, $user2->id);
2361         $this->assertEquals(0, $DB->count_records('message_contacts'));
2362     }
2364     /**
2365      * Test blocking users.
2366      */
2367     public function test_block_user() {
2368         global $DB;
2370         $user1 = self::getDataGenerator()->create_user();
2371         $user2 = self::getDataGenerator()->create_user();
2373         \core_message\api::block_user($user1->id, $user2->id);
2375         $blockedusers = $DB->get_records('message_users_blocked');
2377         $this->assertCount(1, $blockedusers);
2379         $blockeduser = reset($blockedusers);
2381         $this->assertEquals($user1->id, $blockeduser->userid);
2382         $this->assertEquals($user2->id, $blockeduser->blockeduserid);
2383     }
2385     /**
2386      * Test unblocking users.
2387      */
2388     public function test_unblock_user() {
2389         global $DB;
2391         $user1 = self::getDataGenerator()->create_user();
2392         $user2 = self::getDataGenerator()->create_user();
2394         \core_message\api::block_user($user1->id, $user2->id);
2395         \core_message\api::unblock_user($user1->id, $user2->id);
2397         $this->assertEquals(0, $DB->count_records('message_users_blocked'));
2398     }
2400     /**
2401      * Test is contact check.
2402      */
2403     public function test_is_contact() {
2404         $user1 = self::getDataGenerator()->create_user();
2405         $user2 = self::getDataGenerator()->create_user();
2406         $user3 = self::getDataGenerator()->create_user();
2408         \core_message\api::add_contact($user1->id, $user2->id);
2410         $this->assertTrue(\core_message\api::is_contact($user1->id, $user2->id));
2411         $this->assertTrue(\core_message\api::is_contact($user2->id, $user1->id));
2412         $this->assertFalse(\core_message\api::is_contact($user2->id, $user3->id));
2413     }
2415     /**
2416      * Test get contact.
2417      */
2418     public function test_get_contact() {
2419         $user1 = self::getDataGenerator()->create_user();
2420         $user2 = self::getDataGenerator()->create_user();
2422         \core_message\api::add_contact($user1->id, $user2->id);
2424         $contact = \core_message\api::get_contact($user1->id, $user2->id);
2426         $this->assertEquals($user1->id, $contact->userid);
2427         $this->assertEquals($user2->id, $contact->contactid);
2428     }
2430     /**
2431      * Test is blocked checked.
2432      */
2433     public function test_is_blocked() {
2434         $user1 = self::getDataGenerator()->create_user();
2435         $user2 = self::getDataGenerator()->create_user();
2437         $this->assertFalse(\core_message\api::is_blocked($user1->id, $user2->id));
2438         $this->assertFalse(\core_message\api::is_blocked($user2->id, $user1->id));
2440         \core_message\api::block_user($user1->id, $user2->id);
2442         $this->assertTrue(\core_message\api::is_blocked($user1->id, $user2->id));
2443         $this->assertFalse(\core_message\api::is_blocked($user2->id, $user1->id));
2444     }
2446     /**
2447      * Test the contact request exist check.
2448      */
2449     public function test_does_contact_request_exist() {
2450         $user1 = self::getDataGenerator()->create_user();
2451         $user2 = self::getDataGenerator()->create_user();
2453         $this->assertFalse(\core_message\api::does_contact_request_exist($user1->id, $user2->id));
2454         $this->assertFalse(\core_message\api::does_contact_request_exist($user2->id, $user1->id));
2456         \core_message\api::create_contact_request($user1->id, $user2->id);
2458         $this->assertTrue(\core_message\api::does_contact_request_exist($user1->id, $user2->id));
2459         $this->assertTrue(\core_message\api::does_contact_request_exist($user2->id, $user1->id));
2460     }
2462     /**
2463      * Test the user in conversation check.
2464      */
2465     public function test_is_user_in_conversation() {
2466         $user1 = self::getDataGenerator()->create_user();
2467         $user2 = self::getDataGenerator()->create_user();
2469         $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
2471         $this->assertTrue(\core_message\api::is_user_in_conversation($user1->id, $conversationid));
2472     }
2474     /**
2475      * Test the user in conversation check when they are not.
2476      */
2477     public function test_is_user_in_conversation_when_not() {
2478         $user1 = self::getDataGenerator()->create_user();
2479         $user2 = self::getDataGenerator()->create_user();
2480         $user3 = self::getDataGenerator()->create_user();
2482         $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
2484         $this->assertFalse(\core_message\api::is_user_in_conversation($user3->id, $conversationid));
2485     }
2487     /**
2488      * Comparison function for sorting contacts.
2489      *
2490      * @param stdClass $a
2491      * @param stdClass $b
2492      * @return bool
2493      */
2494     protected static function sort_contacts($a, $b) {
2495         return $a->userid > $b->userid;
2496     }