MDL-54708 message: add backend APIs for notifications popover
[moodle.git] / message / tests / messagelib_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 api's in message lib.
19  *
20  * @package core_message
21  * @category test
22  * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
23  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/message/lib.php');
31 /**
32  * Test api's in message lib.
33  *
34  * @package core_message
35  * @category test
36  * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
37  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class core_message_messagelib_testcase extends advanced_testcase {
41     /** @var phpunit_message_sink keep track of messages. */
42     protected $messagesink = null;
44     /**
45      * Test set up.
46      *
47      * This is executed before running any test in this file.
48      */
49     public function setUp() {
50         $this->preventResetByRollback(); // Messaging is not compatible with transactions.
51         $this->messagesink = $this->redirectMessages();
52         $this->resetAfterTest();
53     }
55     /**
56      * Send a fake message.
57      *
58      * {@link message_send()} does not support transaction, this function will simulate a message
59      * sent from a user to another. We should stop using it once {@link message_send()} will support
60      * transactions. This is not clean at all, this is just used to add rows to the table.
61      *
62      * @param stdClass $userfrom user object of the one sending the message.
63      * @param stdClass $userto user object of the one receiving the message.
64      * @param string $message message to send.
65      * @return int the id of the message
66      */
67     protected function send_fake_message($userfrom, $userto, $message = 'Hello world!') {
68         global $DB;
70         $record = new stdClass();
71         $record->useridfrom = $userfrom->id;
72         $record->useridto = $userto->id;
73         $record->subject = 'No subject';
74         $record->fullmessage = $message;
75         $record->smallmessage = $message;
76         $record->timecreated = time();
78         return $DB->insert_record('message', $record);
79     }
81     /**
82      * Send a fake unread notification.
83      *
84      * {@link message_send()} does not support transaction, this function will simulate a message
85      * sent from a user to another. We should stop using it once {@link message_send()} will support
86      * transactions. This is not clean at all, this is just used to add rows to the table.
87      *
88      * @param stdClass $userfrom user object of the one sending the message.
89      * @param stdClass $userto user object of the one receiving the message.
90      * @param string $message message to send.
91      * @param int $timecreated time the message was created.
92      * @return int the id of the message
93      */
94     protected function send_fake_unread_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
95         global $DB;
97         $record = new stdClass();
98         $record->useridfrom = $userfrom->id;
99         $record->useridto = $userto->id;
100         $record->notification = 1;
101         $record->subject = 'No subject';
102         $record->fullmessage = $message;
103         $record->smallmessage = $message;
104         $record->timecreated = $timecreated ? $timecreated : time();
106         return $DB->insert_record('message', $record);
107     }
109     /**
110      * Send a fake read notification.
111      *
112      * {@link message_send()} does not support transaction, this function will simulate a message
113      * sent from a user to another. We should stop using it once {@link message_send()} will support
114      * transactions. This is not clean at all, this is just used to add rows to the table.
115      *
116      * @param stdClass $userfrom user object of the one sending the message.
117      * @param stdClass $userto user object of the one receiving the message.
118      * @param string $message message to send.
119      * @param int $timecreated time the message was created.
120      * @return int the id of the message
121      */
122     protected function send_fake_read_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0, $timeread = 0) {
123         global $DB;
125         $record = new stdClass();
126         $record->useridfrom = $userfrom->id;
127         $record->useridto = $userto->id;
128         $record->notification = 1;
129         $record->subject = 'No subject';
130         $record->fullmessage = $message;
131         $record->smallmessage = $message;
132         $record->timecreated = $timecreated ? $timecreated : time();
133         $record->timeread = $timeread ? $timeread : time();
135         return $DB->insert_record('message_read', $record);
136     }
138     /**
139      * Test message_get_blocked_users.
140      */
141     public function test_message_get_blocked_users() {
142         // Set this user as the admin.
143         $this->setAdminUser();
145         // Create a user to add to the admin's contact list.
146         $user1 = $this->getDataGenerator()->create_user();
147         $user2 = $this->getDataGenerator()->create_user();
149         // Add users to the admin's contact list.
150         message_add_contact($user1->id);
151         message_add_contact($user2->id, 1);
153         $this->assertCount(1, message_get_blocked_users());
155         // Block other user.
156         message_block_contact($user1->id);
157         $this->assertCount(2, message_get_blocked_users());
159         // Test deleting users.
160         delete_user($user1);
161         $this->assertCount(1, message_get_blocked_users());
162     }
164     /**
165      * Test message_get_contacts.
166      */
167     public function test_message_get_contacts() {
168         global $USER, $CFG;
170         // Set this user as the admin.
171         $this->setAdminUser();
173         $noreplyuser = core_user::get_noreply_user();
174         $supportuser = core_user::get_support_user();
176         // Create a user to add to the admin's contact list.
177         $user1 = $this->getDataGenerator()->create_user();
178         $user2 = $this->getDataGenerator()->create_user();
179         $user3 = $this->getDataGenerator()->create_user(); // Stranger.
181         // Add users to the admin's contact list.
182         message_add_contact($user1->id);
183         message_add_contact($user2->id);
185         // Send some messages.
186         $this->send_fake_message($user1, $USER);
187         $this->send_fake_message($user2, $USER);
188         $this->send_fake_message($user3, $USER);
190         list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
191         $this->assertCount(0, $onlinecontacts);
192         $this->assertCount(2, $offlinecontacts);
193         $this->assertCount(1, $strangers);
195         // Send message from noreply and support users.
196         $this->send_fake_message($noreplyuser, $USER);
197         $this->send_fake_message($supportuser, $USER);
198         list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
199         $this->assertCount(0, $onlinecontacts);
200         $this->assertCount(2, $offlinecontacts);
201         $this->assertCount(3, $strangers);
203         // Block 1 user.
204         message_block_contact($user2->id);
205         list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
206         $this->assertCount(0, $onlinecontacts);
207         $this->assertCount(1, $offlinecontacts);
208         $this->assertCount(3, $strangers);
210         // Noreply user being valid user.
211         core_user::reset_internal_users();
212         $CFG->noreplyuserid = $user3->id;
213         $noreplyuser = core_user::get_noreply_user();
214         list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
215         $this->assertCount(0, $onlinecontacts);
216         $this->assertCount(1, $offlinecontacts);
217         $this->assertCount(2, $strangers);
219         // Test deleting users.
220         delete_user($user1);
221         delete_user($user3);
223         list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
224         $this->assertCount(0, $onlinecontacts);
225         $this->assertCount(0, $offlinecontacts);
226         $this->assertCount(1, $strangers);
227     }
229     /**
230      * Test message_count_messages.
231      */
232     public function test_message_count_messages() {
233         global $DB;
235         // Create users to send and receive message.
236         $userfrom = $this->getDataGenerator()->create_user();
237         $userto = $this->getDataGenerator()->create_user();
239         message_post_message($userfrom, $userto, 'Message 1', FORMAT_PLAIN);
240         message_post_message($userfrom, $userto, 'Message 2', FORMAT_PLAIN);
241         message_post_message($userto, $userfrom, 'Message 3', FORMAT_PLAIN);
243         // Return 0 when no message.
244         $messages = array();
245         $this->assertEquals(0, message_count_messages($messages, 'Test', 'Test'));
247         // Check number of messages from userfrom and userto.
248         $messages = $this->messagesink->get_messages();
249         $this->assertEquals(2, message_count_messages($messages, 'useridfrom', $userfrom->id));
250         $this->assertEquals(1, message_count_messages($messages, 'useridfrom', $userto->id));
251     }
253     /**
254      * Test message_count_unread_messages.
255      */
256     public function test_message_count_unread_messages() {
257         // Create users to send and receive message.
258         $userfrom1 = $this->getDataGenerator()->create_user();
259         $userfrom2 = $this->getDataGenerator()->create_user();
260         $userto = $this->getDataGenerator()->create_user();
262         $this->assertEquals(0, message_count_unread_messages($userto));
264         // Send fake messages.
265         $this->send_fake_message($userfrom1, $userto);
266         $this->send_fake_message($userfrom2, $userto);
268         $this->assertEquals(2, message_count_unread_messages($userto));
269         $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
270     }
272     /**
273      * Test message_count_blocked_users.
274      *
275      */
276     public function test_message_count_blocked_users() {
277         // Set this user as the admin.
278         $this->setAdminUser();
280         // Create users to add to the admin's contact list.
281         $user1 = $this->getDataGenerator()->create_user();
282         $user2 = $this->getDataGenerator()->create_user();
284         $this->assertEquals(0, message_count_blocked_users());
286         // Add 1 blocked and 1 normal contact to admin's contact list.
287         message_add_contact($user1->id);
288         message_add_contact($user2->id, 1);
290         $this->assertEquals(0, message_count_blocked_users($user2));
291         $this->assertEquals(1, message_count_blocked_users());
292     }
294     /**
295      * Test message_add_contact.
296      */
297     public function test_message_add_contact() {
298         // Set this user as the admin.
299         $this->setAdminUser();
301         // Create a user to add to the admin's contact list.
302         $user1 = $this->getDataGenerator()->create_user();
303         $user2 = $this->getDataGenerator()->create_user();
304         $user3 = $this->getDataGenerator()->create_user();
306         message_add_contact($user1->id);
307         message_add_contact($user2->id, 0);
308         // Add duplicate contact and make sure only 1 record exists.
309         message_add_contact($user2->id, 1);
311         $this->assertNotEmpty(message_get_contact($user1->id));
312         $this->assertNotEmpty(message_get_contact($user2->id));
313         $this->assertEquals(false, message_get_contact($user3->id));
314         $this->assertEquals(1, message_count_blocked_users());
315     }
317     /**
318      * Test message_remove_contact.
319      */
320     public function test_message_remove_contact() {
321         // Set this user as the admin.
322         $this->setAdminUser();
324         // Create a user to add to the admin's contact list.
325         $user = $this->getDataGenerator()->create_user();
327         // Add the user to the admin's contact list.
328         message_add_contact($user->id);
329         $this->assertNotEmpty(message_get_contact($user->id));
331         // Remove user from admin's contact list.
332         message_remove_contact($user->id);
333         $this->assertEquals(false, message_get_contact($user->id));
334     }
336     /**
337      * Test message_block_contact.
338      */
339     public function test_message_block_contact() {
340         // Set this user as the admin.
341         $this->setAdminUser();
343         // Create a user to add to the admin's contact list.
344         $user1 = $this->getDataGenerator()->create_user();
345         $user2 = $this->getDataGenerator()->create_user();
347         // Add users to the admin's contact list.
348         message_add_contact($user1->id);
349         message_add_contact($user2->id);
351         $this->assertEquals(0, message_count_blocked_users());
353         // Block 1 user.
354         message_block_contact($user2->id);
355         $this->assertEquals(1, message_count_blocked_users());
357     }
359     /**
360      * Test message_unblock_contact.
361      */
362     public function test_message_unblock_contact() {
363         // Set this user as the admin.
364         $this->setAdminUser();
366         // Create a user to add to the admin's contact list.
367         $user1 = $this->getDataGenerator()->create_user();
368         $user2 = $this->getDataGenerator()->create_user();
370         // Add users to the admin's contact list.
371         message_add_contact($user1->id);
372         message_add_contact($user2->id, 1); // Add blocked contact.
374         $this->assertEquals(1, message_count_blocked_users());
376         // Unblock user.
377         message_unblock_contact($user2->id);
378         $this->assertEquals(0, message_count_blocked_users());
379     }
381     /**
382      * Test message_search_users.
383      */
384     public function test_message_search_users() {
385         // Set this user as the admin.
386         $this->setAdminUser();
388         // Create a user to add to the admin's contact list.
389         $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
390         $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
392         // Add users to the admin's contact list.
393         message_add_contact($user1->id);
394         message_add_contact($user2->id); // Add blocked contact.
396         $this->assertCount(1, message_search_users(0, 'Test1'));
397         $this->assertCount(2, message_search_users(0, 'Test'));
398         $this->assertCount(1, message_search_users(0, 'user1'));
399         $this->assertCount(2, message_search_users(0, 'user'));
400     }
402     /**
403      * Test message_search.
404      */
405     public function test_message_search() {
406         global $USER;
408         // Set this user as the admin.
409         $this->setAdminUser();
411         // Create a user to add to the admin's contact list.
412         $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
413         $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
415         // Send few messages, real (read).
416         message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
417         message_post_message($USER, $user1, 'Message 2', FORMAT_PLAIN);
418         message_post_message($USER, $user2, 'Message 3', FORMAT_PLAIN);
420         $this->assertCount(2, message_search(array('Message'), true, false));
421         $this->assertCount(3, message_search(array('Message'), true, true));
423         // Send fake message (not-read).
424         $this->send_fake_message($USER, $user1, 'Message 4');
425         $this->send_fake_message($user1, $USER, 'Message 5');
426         $this->assertCount(3, message_search(array('Message'), true, false));
427         $this->assertCount(5, message_search(array('Message'), true, true));
429         // If courseid given then should be 0.
430         $this->assertEquals(false, message_search(array('Message'), true, true, ''));
431         $this->assertEquals(false, message_search(array('Message'), true, true, 2));
432         $this->assertCount(5, message_search(array('Message'), true, true, SITEID));
433     }
435     /**
436      * The data provider for message_get_recent_conversations.
437      *
438      * This provides sets of data to for testing.
439      * @return array
440      */
441     public function message_get_recent_conversations_provider() {
442         return array(
443             'Test that conversations with messages contacts is correctly ordered.' => array(
444                 'users' => array(
445                     'user1',
446                     'user2',
447                     'user3',
448                 ),
449                 'contacts' => array(
450                 ),
451                 'messages' => array(
452                     array(
453                         'from'          => 'user1',
454                         'to'            => 'user2',
455                         'state'         => 'unread',
456                         'subject'       => 'S1',
457                     ),
458                     array(
459                         'from'          => 'user2',
460                         'to'            => 'user1',
461                         'state'         => 'unread',
462                         'subject'       => 'S2',
463                     ),
464                     array(
465                         'from'          => 'user1',
466                         'to'            => 'user2',
467                         'state'         => 'unread',
468                         'timecreated'   => 0,
469                         'subject'       => 'S3',
470                     ),
471                     array(
472                         'from'          => 'user1',
473                         'to'            => 'user3',
474                         'state'         => 'read',
475                         'timemodifier'  => 1,
476                         'subject'       => 'S4',
477                     ),
478                     array(
479                         'from'          => 'user3',
480                         'to'            => 'user1',
481                         'state'         => 'read',
482                         'timemodifier'  => 1,
483                         'subject'       => 'S5',
484                     ),
485                     array(
486                         'from'          => 'user1',
487                         'to'            => 'user3',
488                         'state'         => 'read',
489                         'timecreated'   => 0,
490                         'subject'       => 'S6',
491                     ),
492                 ),
493                 'expectations' => array(
494                     'user1' => array(
495                         // User1 has conversed most recently with user3. The most recent message is M5.
496                         array(
497                             'messageposition'   => 0,
498                             'with'              => 'user3',
499                             'subject'           => 'S5',
500                         ),
501                         // User1 has also conversed with user2. The most recent message is S2.
502                         array(
503                             'messageposition'   => 1,
504                             'with'              => 'user2',
505                             'subject'           => 'S2',
506                         ),
507                     ),
508                     'user2' => array(
509                         // User2 has only conversed with user1. Their most recent shared message was S2.
510                         array(
511                             'messageposition'   => 0,
512                             'with'              => 'user1',
513                             'subject'           => 'S2',
514                         ),
515                     ),
516                     'user3' => array(
517                         // User3 has only conversed with user1. Their most recent shared message was S5.
518                         array(
519                             'messageposition'   => 0,
520                             'with'              => 'user1',
521                             'subject'           => 'S5',
522                         ),
523                     ),
524                 ),
525             ),
526             'Test that users with contacts and messages to self work as expected' => array(
527                 'users' => array(
528                     'user1',
529                     'user2',
530                     'user3',
531                 ),
532                 'contacts' => array(
533                     'user1' => array(
534                         'user2' => 0,
535                         'user3' => 0,
536                     ),
537                     'user2' => array(
538                         'user3' => 0,
539                     ),
540                 ),
541                 'messages' => array(
542                     array(
543                         'from'          => 'user1',
544                         'to'            => 'user1',
545                         'state'         => 'unread',
546                         'subject'       => 'S1',
547                     ),
548                     array(
549                         'from'          => 'user1',
550                         'to'            => 'user1',
551                         'state'         => 'unread',
552                         'subject'       => 'S2',
553                     ),
554                 ),
555                 'expectations' => array(
556                     'user1' => array(
557                         // User1 has conversed most recently with user1. The most recent message is S2.
558                         array(
559                             'messageposition'   => 0,
560                             'with'              => 'user1',
561                             'subject'           => 'S2',
562                         ),
563                     ),
564                 ),
565             ),
566             'Test conversations with a single user, where some messages are read and some are not.' => array(
567                 'users' => array(
568                     'user1',
569                     'user2',
570                 ),
571                 'contacts' => array(
572                 ),
573                 'messages' => array(
574                     array(
575                         'from'          => 'user1',
576                         'to'            => 'user2',
577                         'state'         => 'read',
578                         'subject'       => 'S1',
579                     ),
580                     array(
581                         'from'          => 'user2',
582                         'to'            => 'user1',
583                         'state'         => 'read',
584                         'subject'       => 'S2',
585                     ),
586                     array(
587                         'from'          => 'user1',
588                         'to'            => 'user2',
589                         'state'         => 'unread',
590                         'timemodifier'  => 1,
591                         'subject'       => 'S3',
592                     ),
593                     array(
594                         'from'          => 'user1',
595                         'to'            => 'user2',
596                         'state'         => 'unread',
597                         'timemodifier'  => 1,
598                         'subject'       => 'S4',
599                     ),
600                 ),
601                 'expectations' => array(
602                     // The most recent message between user1 and user2 was S4.
603                     'user1' => array(
604                         array(
605                             'messageposition'   => 0,
606                             'with'              => 'user2',
607                             'subject'           => 'S4',
608                         ),
609                     ),
610                     'user2' => array(
611                         // The most recent message between user1 and user2 was S4.
612                         array(
613                             'messageposition'   => 0,
614                             'with'              => 'user1',
615                             'subject'           => 'S4',
616                         ),
617                     ),
618                 ),
619             ),
620             'Test conversations with a single user, where some messages are read and some are not, and messages ' .
621             'are out of order' => array(
622             // This can happen through a combination of factors including multi-master DB replication with messages
623             // read somehow (e.g. API).
624                 'users' => array(
625                     'user1',
626                     'user2',
627                 ),
628                 'contacts' => array(
629                 ),
630                 'messages' => array(
631                     array(
632                         'from'          => 'user1',
633                         'to'            => 'user2',
634                         'state'         => 'read',
635                         'subject'       => 'S1',
636                         'timemodifier'  => 1,
637                     ),
638                     array(
639                         'from'          => 'user2',
640                         'to'            => 'user1',
641                         'state'         => 'read',
642                         'subject'       => 'S2',
643                         'timemodifier'  => 2,
644                     ),
645                     array(
646                         'from'          => 'user1',
647                         'to'            => 'user2',
648                         'state'         => 'unread',
649                         'subject'       => 'S3',
650                     ),
651                     array(
652                         'from'          => 'user1',
653                         'to'            => 'user2',
654                         'state'         => 'unread',
655                         'subject'       => 'S4',
656                     ),
657                 ),
658                 'expectations' => array(
659                     // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
660                     'user1' => array(
661                         array(
662                             'messageposition'   => 0,
663                             'with'              => 'user2',
664                             'subject'           => 'S2',
665                         ),
666                     ),
667                     'user2' => array(
668                         array(
669                             'messageposition'   => 0,
670                             'with'              => 'user1',
671                             'subject'           => 'S2',
672                         ),
673                     ),
674                 ),
675             ),
676         );
677     }
679     /**
680      * Test message_get_recent_conversations with a mixture of messages.
681      *
682      * @dataProvider message_get_recent_conversations_provider
683      * @param array $usersdata The list of users to create for this test.
684      * @param array $messagesdata The list of messages to create.
685      * @param array $expectations The list of expected outcomes.
686      */
687     public function test_message_get_recent_conversations($usersdata, $contacts, $messagesdata, $expectations) {
688         global $DB;
690         // Create all of the users.
691         $users = array();
692         foreach ($usersdata as $username) {
693             $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
694         }
696         foreach ($contacts as $username => $contact) {
697             foreach ($contact as $contactname => $blocked) {
698                 $record = new stdClass();
699                 $record->userid     = $users[$username]->id;
700                 $record->contactid  = $users[$contactname]->id;
701                 $record->blocked    = $blocked;
702                 $record->id = $DB->insert_record('message_contacts', $record);
703             }
704         }
706         $defaulttimecreated = time();
707         foreach ($messagesdata as $messagedata) {
708             $from       = $users[$messagedata['from']];
709             $to         = $users[$messagedata['to']];
710             $subject    = $messagedata['subject'];
712             if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
713                 $table = 'message';
714                 $messageid = $this->send_fake_message($from, $to, $subject, FORMAT_PLAIN);
715             } else {
716                 // If there is no state, or the state is not 'unread', assume the message is read.
717                 $table = 'message_read';
718                 $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN);
719             }
721             $updatemessage = new stdClass();
722             $updatemessage->id = $messageid;
723             if (isset($messagedata['timecreated'])) {
724                 $updatemessage->timecreated = $messagedata['timecreated'];
725             } else if (isset($messagedata['timemodifier'])) {
726                 $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier'];
727             } else {
728                 $updatemessage->timecreated = $defaulttimecreated;
729             }
730             $DB->update_record($table, $updatemessage);
731         }
733         foreach ($expectations as $username => $data) {
734             // Get the recent conversations for the specified user.
735             $user = $users[$username];
736             $conversations = message_get_recent_conversations($user);
737             foreach ($data as $expectation) {
738                 $otheruser = $users[$expectation['with']];
739                 $conversation = $conversations[$expectation['messageposition']];
740                 $this->assertEquals($otheruser->id, $conversation->id);
741                 $this->assertEquals($expectation['subject'], $conversation->smallmessage);
742             }
743         }
744     }
746     /**
747      * Test message_get_recent_notifications.
748      */
749     public function test_message_get_recent_notifications() {
750         global $DB, $USER;
752         // Set this user as the admin.
753         $this->setAdminUser();
755         // Create a user to send messages from.
756         $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
758         // Add two messages - will mark them as notifications later.
759         $m1 = message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
760         $m2 = message_post_message($user1, $USER, 'Message 2', FORMAT_PLAIN);
762         // Mark the second message as a notification.
763         $updatemessage = new stdClass();
764         $updatemessage->id = $m2;
765         $updatemessage->notification = 1;
766         $DB->update_record('message_read', $updatemessage);
768         // Mark the first message as a notification and change the timecreated to 0.
769         $updatemessage->id = $m1;
770         $updatemessage->notification = 1;
771         $updatemessage->timecreated = 0;
772         $DB->update_record('message_read', $updatemessage);
774         $notifications = message_get_recent_notifications($USER);
776         // Get the messages.
777         $firstmessage = array_shift($notifications);
778         $secondmessage = array_shift($notifications);
780         // Confirm that we have received the notifications with the maximum timecreated, rather than the max id.
781         $this->assertEquals('Message 2', $firstmessage->smallmessage);
782         $this->assertEquals('Message 1', $secondmessage->smallmessage);
783     }
785     /**
786      * Test that message_can_post_message returns false if the sender does not have the
787      * moode/site:sendmessage capability.
788      */
789     public function test_message_can_post_message_returns_false_without_capability() {
790         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
791         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
792         $context = context_system::instance();
793         $roleid = $this->getDataGenerator()->create_role();
794         $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
796         assign_capability('moodle/site:sendmessage', CAP_PROHIBIT, $roleid, $context);
798         $this->assertFalse(message_can_post_message($recipient, $sender));
799     }
801     /**
802      * Test that message_can_post_message returns false if the receiver only accepts
803      * messages from contacts and the sender isn't a contact.
804      */
805     public function test_message_can_post_message_returns_false_non_contact_blocked() {
806         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
807         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
809         set_user_preference('message_blocknoncontacts', true, $recipient);
811         $this->assertFalse(message_can_post_message($recipient, $sender));
812     }
814     /**
815      * Test that message_can_post_message returns false if the receiver has blocked the
816      * sender from messaging them.
817      */
818     public function test_message_can_post_message_returns_false_if_blocked() {
819         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
820         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
822         $this->setUser($recipient);
823         message_block_contact($sender->id);
825         $this->assertFalse(message_can_post_message($recipient, $sender));
826     }
828     /**
829      * Test that message_can_post_message returns false if the receiver has blocked the
830      * sender from messaging them.
831      */
832     public function test_message_can_post_message_returns_true() {
833         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
834         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
836         $this->assertTrue(message_can_post_message($recipient, $sender));
837     }
839     /**
840      * Test that message_is_user_non_contact_blocked returns false if the recipient allows
841      * messages from non-contacts.
842      */
843     public function test_message_is_user_non_contact_blocked_false_without_preference() {
844         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
845         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
847         set_user_preference('message_blocknoncontacts', false, $recipient);
849         $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
850     }
852     /**
853      * Test that message_is_user_non_contact_blocked returns true if the recipient doesn't
854      * allow messages from non-contacts and the sender isn't a contact.
855      */
856     public function test_message_is_user_non_contact_blocked_true_with_preference() {
857         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
858         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
860         set_user_preference('message_blocknoncontacts', true, $recipient);
862         $this->assertTrue(message_is_user_non_contact_blocked($recipient, $sender));
863     }
865     /**
866      * Test that message_is_user_non_contact_blocked returns false if the recipient doesn't
867      * allow messages from non-contacts but the sender is a contact.
868      */
869     public function test_message_is_user_non_contact_blocked_false_with_if_contact() {
870         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
871         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
873         $this->setUser($recipient);
874         set_user_preference('message_blocknoncontacts', true, $recipient);
875         message_add_contact($sender->id);
877         $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
878     }
880     /**
881      * Test that message_is_user_blocked returns false if the sender is not a contact of
882      * the recipient.
883      */
884     public function test_message_is_user_blocked_false_no_contact() {
885         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
886         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
888         $this->assertFalse(message_is_user_blocked($recipient, $sender));
889     }
891     /**
892      * Test that message_is_user_blocked returns false if the sender is a contact that is
893      * blocked by the recipient but has the moodle/site:readallmessages capability.
894      */
895     public function test_message_is_user_blocked_false_if_readallmessages() {
896         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
897         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
899         $this->setUser($recipient);
900         message_block_contact($sender->id);
902         $context = context_system::instance();
903         $roleid = $this->getDataGenerator()->create_role();
904         $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
906         assign_capability('moodle/site:readallmessages', CAP_ALLOW, $roleid, $context);
908         $this->assertFalse(message_is_user_blocked($recipient, $sender));
909     }
911     /**
912      * Test that message_is_user_blocked returns true if the sender is a contact that is
913      * blocked by the recipient and does not have the moodle/site:readallmessages capability.
914      */
915     public function test_message_is_user_blocked_true_if_blocked() {
916         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
917         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
919         $this->setUser($recipient);
920         message_block_contact($sender->id);
922         $context = context_system::instance();
923         $roleid = $this->getDataGenerator()->create_role();
924         $this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
926         assign_capability('moodle/site:readallmessages', CAP_PROHIBIT, $roleid, $context);
928         $this->assertTrue(message_is_user_blocked($recipient, $sender));
929     }
931     /**
932      * Test that the message_get_notifications function will return only read notifications if requested.
933      */
934     public function test_message_get_notifications_read_only() {
935         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
936         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
938         $this->send_fake_read_notification($sender, $recipient, 'Message 1', 2);
939         $this->send_fake_read_notification($sender, $recipient, 'Message 2', 4);
941         $notifications = message_get_notifications($recipient->id, 0, MESSAGE_READ);
943         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
944         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
946         // Check if we request read and unread but there are only read messages, it should
947         // still return those correctly.
948         $notifications = message_get_notifications($recipient->id, 0, '');
950         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
951         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
952     }
954     /**
955      * Test that the message_get_notifications function will return only unread notifications if requested.
956      */
957     public function test_message_get_notifications_unread_only() {
958         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
959         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
961         $this->send_fake_unread_notification($sender, $recipient, 'Message 1', 2);
962         $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 4);
964         $notifications = message_get_notifications($recipient->id, 0, MESSAGE_UNREAD);
966         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
967         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
969         // Check if we request read and unread but there are only read messages, it should
970         // still return those correctly.
971         $notifications = message_get_notifications($recipient->id, 0, '');
973         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
974         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
975     }
977     /**
978      * Test that the message_get_notifications function will return the correct notifications when both
979      * read and unread notifications are included.
980      */
981     public function test_message_get_notifications_mixed() {
982         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
983         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
985         $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
986         $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
987         $this->send_fake_read_notification($sender, $recipient, 'Message 3', 3, 1);
988         $this->send_fake_read_notification($sender, $recipient, 'Message 4', 3, 2);
989         $this->send_fake_unread_notification($sender, $recipient, 'Message 5', 4);
991         $notifications = message_get_notifications($recipient->id, 0);
993         $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
994         $this->assertEquals($notifications[1]->fullmessage, 'Message 4');
995         $this->assertEquals($notifications[2]->fullmessage, 'Message 3');
996         $this->assertEquals($notifications[3]->fullmessage, 'Message 2');
997         $this->assertEquals($notifications[4]->fullmessage, 'Message 1');
999         $notifications = message_get_notifications($recipient->id, 0, MESSAGE_READ);
1001         $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
1002         $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
1003         $this->assertEquals($notifications[2]->fullmessage, 'Message 1');
1005         $notifications = message_get_notifications($recipient->id, 0, MESSAGE_UNREAD);
1007         $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
1008         $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
1009     }
1011     /**
1012      * Test that the message_get_notifications function works correctly with limiting and offsetting
1013      * the result set if requested.
1014      */
1015     public function test_message_get_notifications_all_with_limit_and_offset() {
1016         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
1017         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
1019         $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
1020         $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
1021         $this->send_fake_read_notification($sender, $recipient, 'Message 3', 3, 1);
1022         $this->send_fake_read_notification($sender, $recipient, 'Message 4', 3, 2);
1023         $this->send_fake_unread_notification($sender, $recipient, 'Message 5', 4);
1024         $this->send_fake_unread_notification($sender, $recipient, 'Message 6', 5);
1026         $notifications = message_get_notifications($recipient->id, 0, '', false, false, 'DESC', 2, 0);
1028         $this->assertEquals($notifications[0]->fullmessage, 'Message 6');
1029         $this->assertEquals($notifications[1]->fullmessage, 'Message 5');
1031         $notifications = message_get_notifications($recipient->id, 0, '', false, false, 'DESC', 2, 2);
1033         $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
1034         $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
1036         $notifications = message_get_notifications($recipient->id, 0, '', false, false, 'DESC', 0, 3);
1038         $this->assertEquals($notifications[0]->fullmessage, 'Message 3');
1039         $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
1040         $this->assertEquals($notifications[2]->fullmessage, 'Message 1');
1041     }
1043     /**
1044      * Test that the message_get_notifications function returns correct values if specifying
1045      * a sender.
1046      */
1047     public function test_message_get_notifications_multiple_senders() {
1048         $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
1049         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
1050         $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
1051         $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
1053         $this->send_fake_read_notification($sender1, $recipient1, 'Message 1', 1);
1054         $this->send_fake_unread_notification($sender1, $recipient1, 'Message 2', 2);
1055         $this->send_fake_read_notification($sender1, $recipient2, 'Message 3', 3);
1056         $this->send_fake_unread_notification($sender1, $recipient2, 'Message 4', 4);
1057         $this->send_fake_read_notification($sender2, $recipient1, 'Message 5', 5);
1058         $this->send_fake_unread_notification($sender2, $recipient1, 'Message 6', 6);
1059         $this->send_fake_read_notification($sender2, $recipient2, 'Message 7', 7);
1060         $this->send_fake_unread_notification($sender2, $recipient2, 'Message 8', 8);
1062         $notifications = message_get_notifications(0, $sender1->id, '', false, false, 'DESC');
1064         $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
1065         $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
1066         $this->assertEquals($notifications[2]->fullmessage, 'Message 2');
1067         $this->assertEquals($notifications[3]->fullmessage, 'Message 1');
1069         $notifications = message_get_notifications(0, $sender1->id, '', false, false, 'DESC', 2, 2);
1071         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
1072         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
1074         $notifications = message_get_notifications($recipient1->id, $sender1->id, '', false, false, 'DESC');
1076         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
1077         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
1078     }
1080     /**
1081      * Test that the message_get_notifications function returns embedded user details for the
1082      * sender if requested.
1083      */
1084     public function test_message_get_notifications_embed_sender() {
1085         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
1086         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
1088         $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
1089         $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
1091         $notifications = message_get_notifications(0, $sender->id, '', false, true, 'DESC');
1093         $func = function($type) {
1094             return function($notification) use ($type) {
1095                 $user = new stdClass();
1096                 $user = username_load_fields_from_object($user, $notification, $type);
1097                 return $user;
1098             };
1099         };
1100         $senders = array_map($func('userfrom'), $notifications);
1101         $recipients = array_map($func('userto'), $notifications);
1103         $this->assertEquals($senders[0]->firstname, 'Test1');
1104         $this->assertEquals($senders[0]->lastname, 'User1');
1105         $this->assertEquals($senders[1]->firstname, 'Test1');
1106         $this->assertEquals($senders[1]->lastname, 'User1');
1108         // Make sure we didn't get recipient details when they weren't requested.
1109         $this->assertEmpty($recipients[0]->firstname);
1110         $this->assertEmpty($recipients[0]->lastname);
1111         $this->assertEmpty($recipients[1]->firstname);
1112         $this->assertEmpty($recipients[1]->lastname);
1113     }
1115     /**
1116      * Test that the message_get_notifications function returns embedded user details for the
1117      * recipient if requested.
1118      */
1119     public function test_message_get_notifications_embed_recipient() {
1120         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
1121         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
1123         $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
1124         $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
1126         $notifications = message_get_notifications(0, $sender->id, '', true, false, 'DESC');
1128         $func = function($type) {
1129             return function($notification) use ($type) {
1130                 $user = new stdClass();
1131                 $user = username_load_fields_from_object($user, $notification, $type);
1132                 return $user;
1133             };
1134         };
1135         $senders = array_map($func('userfrom'), $notifications);
1136         $recipients = array_map($func('userto'), $notifications);
1138         $this->assertEquals($recipients[0]->firstname, 'Test2');
1139         $this->assertEquals($recipients[0]->lastname, 'User2');
1140         $this->assertEquals($recipients[1]->firstname, 'Test2');
1141         $this->assertEquals($recipients[1]->lastname, 'User2');
1143         // Make sure we didn't get sender details when they weren't requested.
1144         $this->assertEmpty($senders[0]->firstname);
1145         $this->assertEmpty($senders[0]->lastname);
1146         $this->assertEmpty($senders[1]->firstname);
1147         $this->assertEmpty($senders[1]->lastname);
1148     }
1150     /**
1151      * Test that the message_get_notifications function returns embedded all user details.
1152      */
1153     public function test_message_get_notifications_embed_both() {
1154         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
1155         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
1157         $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
1158         $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
1160         $notifications = message_get_notifications(0, $sender->id, '', true, true, 'DESC');
1162         $func = function($type) {
1163             return function($notification) use ($type) {
1164                 $user = new stdClass();
1165                 $user = username_load_fields_from_object($user, $notification, $type);
1166                 return $user;
1167             };
1168         };
1169         $senders = array_map($func('userfrom'), $notifications);
1170         $recipients = array_map($func('userto'), $notifications);
1172         $this->assertEquals($recipients[0]->firstname, 'Test2');
1173         $this->assertEquals($recipients[0]->lastname, 'User2');
1174         $this->assertEquals($recipients[1]->firstname, 'Test2');
1175         $this->assertEquals($recipients[1]->lastname, 'User2');
1177         // Make sure we didn't get sender details when they weren't requested.
1178         $this->assertEquals($senders[0]->firstname, 'Test1');
1179         $this->assertEquals($senders[0]->lastname, 'User1');
1180         $this->assertEquals($senders[1]->firstname, 'Test1');
1181         $this->assertEquals($senders[1]->lastname, 'User1');
1182     }
1184     /**
1185      * Test message_count_unread_notifications.
1186      */
1187     public function test_message_count_unread_notifications() {
1188         $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
1189         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
1190         $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
1191         $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
1193         $this->send_fake_unread_notification($sender1, $recipient1);
1194         $this->send_fake_unread_notification($sender1, $recipient1);
1195         $this->send_fake_unread_notification($sender1, $recipient2);
1196         $this->send_fake_unread_notification($sender2, $recipient1);
1197         $this->send_fake_unread_notification($sender2, $recipient2);
1198         $this->send_fake_unread_notification($sender2, $recipient2);
1200         $this->assertEquals(message_count_unread_notifications($recipient1->id, $sender1->id), 2);
1201         $this->assertEquals(message_count_unread_notifications($recipient2->id, $sender1->id), 1);
1202         $this->assertEquals(message_count_unread_notifications($recipient1->id, $sender2->id), 1);
1203         $this->assertEquals(message_count_unread_notifications($recipient2->id, $sender2->id), 2);
1204         $this->assertEquals(message_count_unread_notifications($recipient1->id, 0), 3);
1205         $this->assertEquals(message_count_unread_notifications($recipient2->id, 0), 3);
1206     }
1209     public function test_message_mark_all_read_for_user_touser() {
1210         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
1211         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
1213         $this->send_fake_unread_notification($sender, $recipient);
1214         $this->send_fake_unread_notification($sender, $recipient);
1215         $this->send_fake_unread_notification($sender, $recipient);
1216         $this->send_fake_message($sender, $recipient);
1217         $this->send_fake_message($sender, $recipient);
1218         $this->send_fake_message($sender, $recipient);
1220         message_mark_all_read_for_user($recipient->id);
1221         $this->assertEquals(message_count_unread_messages($recipient), 0);
1222     }
1224     public function test_message_mark_all_read_for_user_touser_with_fromuser() {
1225         $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
1226         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
1227         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
1229         $this->send_fake_unread_notification($sender1, $recipient);
1230         $this->send_fake_unread_notification($sender1, $recipient);
1231         $this->send_fake_unread_notification($sender1, $recipient);
1232         $this->send_fake_message($sender1, $recipient);
1233         $this->send_fake_message($sender1, $recipient);
1234         $this->send_fake_message($sender1, $recipient);
1235         $this->send_fake_unread_notification($sender2, $recipient);
1236         $this->send_fake_unread_notification($sender2, $recipient);
1237         $this->send_fake_unread_notification($sender2, $recipient);
1238         $this->send_fake_message($sender2, $recipient);
1239         $this->send_fake_message($sender2, $recipient);
1240         $this->send_fake_message($sender2, $recipient);
1242         message_mark_all_read_for_user($recipient->id, $sender1->id);
1243         $this->assertEquals(message_count_unread_messages($recipient), 6);
1244     }
1246     public function test_message_mark_all_read_for_user_touser_with_type() {
1247         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
1248         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
1250         $this->send_fake_unread_notification($sender, $recipient);
1251         $this->send_fake_unread_notification($sender, $recipient);
1252         $this->send_fake_unread_notification($sender, $recipient);
1253         $this->send_fake_message($sender, $recipient);
1254         $this->send_fake_message($sender, $recipient);
1255         $this->send_fake_message($sender, $recipient);
1257         message_mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
1258         $this->assertEquals(message_count_unread_messages($recipient), 3);
1260         message_mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
1261         $this->assertEquals(message_count_unread_messages($recipient), 0);
1262     }