8883993e13b10a9205e9926102856d92e37efe89
[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->assertEquals(message_count_unread_messages($recipient), 0);
55     }
57     public function test_mark_all_read_for_user_touser_with_fromuser() {
58         $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
59         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
60         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
62         $this->send_fake_message($sender1, $recipient, 'Notification', 1);
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);
66         $this->send_fake_message($sender1, $recipient);
67         $this->send_fake_message($sender1, $recipient);
68         $this->send_fake_message($sender2, $recipient, 'Notification', 1);
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);
72         $this->send_fake_message($sender2, $recipient);
73         $this->send_fake_message($sender2, $recipient);
75         \core_message\api::mark_all_read_for_user($recipient->id, $sender1->id);
76         $this->assertEquals(message_count_unread_messages($recipient), 6);
77     }
79     public function test_mark_all_read_for_user_touser_with_type() {
80         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
81         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
83         $this->send_fake_message($sender, $recipient, 'Notification', 1);
84         $this->send_fake_message($sender, $recipient, 'Notification', 1);
85         $this->send_fake_message($sender, $recipient, 'Notification', 1);
86         $this->send_fake_message($sender, $recipient);
87         $this->send_fake_message($sender, $recipient);
88         $this->send_fake_message($sender, $recipient);
90         \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
91         $this->assertEquals(message_count_unread_messages($recipient), 3);
93         \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
94         $this->assertEquals(message_count_unread_messages($recipient), 0);
95     }
97     /**
98      * Test count_blocked_users.
99      */
100     public function test_count_blocked_users() {
101         // Set this user as the admin.
102         $this->setAdminUser();
104         // Create users to add to the admin's contact list.
105         $user1 = $this->getDataGenerator()->create_user();
106         $user2 = $this->getDataGenerator()->create_user();
108         $this->assertEquals(0, \core_message\api::count_blocked_users());
110         // Add 1 blocked and 1 normal contact to admin's contact list.
111         message_add_contact($user1->id);
112         message_add_contact($user2->id, 1);
114         $this->assertEquals(0, \core_message\api::count_blocked_users($user2));
115         $this->assertEquals(1, \core_message\api::count_blocked_users());
116     }
118     /**
119      * Tests searching users in a course.
120      */
121     public function test_search_users_in_course() {
122         // Create some users.
123         $user1 = new stdClass();
124         $user1->firstname = 'User';
125         $user1->lastname = 'One';
126         $user1 = self::getDataGenerator()->create_user($user1);
128         // The person doing the search.
129         $this->setUser($user1);
131         // Second user is going to have their last access set to now, so they are online.
132         $user2 = new stdClass();
133         $user2->firstname = 'User';
134         $user2->lastname = 'Two';
135         $user2->lastaccess = time();
136         $user2 = self::getDataGenerator()->create_user($user2);
138         // Block the second user.
139         message_block_contact($user2->id, $user1->id);
141         $user3 = new stdClass();
142         $user3->firstname = 'User';
143         $user3->lastname = 'Three';
144         $user3 = self::getDataGenerator()->create_user($user3);
146         // Create a course.
147         $course1 = new stdClass();
148         $course1->fullname = 'Course';
149         $course1->shortname = 'One';
150         $course1 = $this->getDataGenerator()->create_course($course1);
152         // Enrol the searcher and one user in the course.
153         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
154         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
156         // Perform a search.
157         $results = \core_message\api::search_users_in_course($user1->id, $course1->id, 'User');
159         $this->assertEquals(1, count($results));
161         $user = $results[0];
162         $this->assertEquals($user2->id, $user->userid);
163         $this->assertEquals(fullname($user2), $user->fullname);
164         $this->assertFalse($user->ismessaging);
165         $this->assertNull($user->lastmessage);
166         $this->assertNull($user->messageid);
167         $this->assertTrue($user->isonline);
168         $this->assertFalse($user->isread);
169         $this->assertTrue($user->isblocked);
170         $this->assertNull($user->unreadcount);
171     }
173     /**
174      * Tests searching users.
175      */
176     public function test_search_users() {
177         global $DB;
179         // Create some users.
180         $user1 = new stdClass();
181         $user1->firstname = 'User';
182         $user1->lastname = 'One';
183         $user1 = self::getDataGenerator()->create_user($user1);
185         // Set as the user performing the search.
186         $this->setUser($user1);
188         $user2 = new stdClass();
189         $user2->firstname = 'User search';
190         $user2->lastname = 'Two';
191         $user2 = self::getDataGenerator()->create_user($user2);
193         $user3 = new stdClass();
194         $user3->firstname = 'User search';
195         $user3->lastname = 'Three';
196         $user3 = self::getDataGenerator()->create_user($user3);
198         $user4 = new stdClass();
199         $user4->firstname = 'User';
200         $user4->lastname = 'Four';
201         $user4 = self::getDataGenerator()->create_user($user4);
203         $user5 = new stdClass();
204         $user5->firstname = 'User search';
205         $user5->lastname = 'Five';
206         $user5 = self::getDataGenerator()->create_user($user5);
208         $user6 = new stdClass();
209         $user6->firstname = 'User';
210         $user6->lastname = 'Six';
211         $user6 = self::getDataGenerator()->create_user($user6);
213         // Create some courses.
214         $course1 = new stdClass();
215         $course1->fullname = 'Course search';
216         $course1->shortname = 'One';
217         $course1 = $this->getDataGenerator()->create_course($course1);
219         $course2 = new stdClass();
220         $course2->fullname = 'Course';
221         $course2->shortname = 'Two';
222         $course2 = $this->getDataGenerator()->create_course($course2);
224         $course3 = new stdClass();
225         $course3->fullname = 'Course';
226         $course3->shortname = 'Three search';
227         $course3 = $this->getDataGenerator()->create_course($course3);
229         $course4 = new stdClass();
230         $course4->fullname = 'Course Four';
231         $course4->shortname = 'CF100';
232         $course4 = $this->getDataGenerator()->create_course($course4);
234         $course5 = new stdClass();
235         $course5->fullname = 'Course';
236         $course5->shortname = 'Five search';
237         $course5 = $this->getDataGenerator()->create_course($course5);
239         $role = $DB->get_record('role', ['shortname' => 'student']);
240         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $role->id);
241         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $role->id);
242         $this->getDataGenerator()->enrol_user($user1->id, $course3->id, $role->id);
243         $this->getDataGenerator()->enrol_user($user1->id, $course5->id, $role->id);
245         // Add some users as contacts.
246         message_add_contact($user2->id, 0, $user1->id);
247         message_add_contact($user3->id, 0, $user1->id);
248         message_add_contact($user4->id, 0, $user1->id);
250         // Remove the viewparticipants capability from one of the courses.
251         $course5context = context_course::instance($course5->id);
252         assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $role->id, $course5context->id);
253         $course5context->mark_dirty();
255         // Perform a search.
256         list($contacts, $courses, $noncontacts) = \core_message\api::search_users($user1->id, 'search');
258         // Check that we retrieved the correct contacts.
259         $this->assertEquals(2, count($contacts));
260         $this->assertEquals($user3->id, $contacts[0]->userid);
261         $this->assertEquals($user2->id, $contacts[1]->userid);
263         // Check that we retrieved the correct courses.
264         $this->assertEquals(2, count($courses));
265         $this->assertEquals($course3->id, $courses[0]->id);
266         $this->assertEquals($course1->id, $courses[1]->id);
268         // Check that we retrieved the correct non-contacts.
269         $this->assertEquals(1, count($noncontacts));
270         $this->assertEquals($user5->id, $noncontacts[0]->userid);
271     }
273     /**
274      * Tests searching messages.
275      */
276     public function test_search_messages() {
277         // Create some users.
278         $user1 = self::getDataGenerator()->create_user();
279         $user2 = self::getDataGenerator()->create_user();
281         // The person doing the search.
282         $this->setUser($user1);
284         // Send some messages back and forth.
285         $time = 1;
286         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time);
287         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 1);
288         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
289         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 3);
291         // Perform a search.
292         $messages = \core_message\api::search_messages($user1->id, 'o');
294         // Confirm the data is correct.
295         $this->assertEquals(2, count($messages));
297         $message1 = $messages[0];
298         $message2 = $messages[1];
300         $this->assertEquals($user2->id, $message1->userid);
301         $this->assertEquals($user2->id, $message1->useridfrom);
302         $this->assertEquals(fullname($user2), $message1->fullname);
303         $this->assertTrue($message1->ismessaging);
304         $this->assertEquals('Word.', $message1->lastmessage);
305         $this->assertNotEmpty($message1->messageid);
306         $this->assertFalse($message1->isonline);
307         $this->assertFalse($message1->isread);
308         $this->assertFalse($message1->isblocked);
309         $this->assertNull($message1->unreadcount);
311         $this->assertEquals($user2->id, $message2->userid);
312         $this->assertEquals($user1->id, $message2->useridfrom);
313         $this->assertEquals(fullname($user2), $message2->fullname);
314         $this->assertTrue($message2->ismessaging);
315         $this->assertEquals('Yo!', $message2->lastmessage);
316         $this->assertNotEmpty($message2->messageid);
317         $this->assertFalse($message2->isonline);
318         $this->assertTrue($message2->isread);
319         $this->assertFalse($message2->isblocked);
320         $this->assertNull($message2->unreadcount);
321     }
323     /**
324      * Tests retrieving conversations.
325      */
326     public function test_get_conversations() {
327         // Create some users.
328         $user1 = self::getDataGenerator()->create_user();
329         $user2 = self::getDataGenerator()->create_user();
330         $user3 = self::getDataGenerator()->create_user();
331         $user4 = self::getDataGenerator()->create_user();
333         // The person doing the search.
334         $this->setUser($user1);
336         // Send some messages back and forth, have some different conversations with different users.
337         $time = 1;
338         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
339         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
340         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
341         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
343         $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
344         $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
345         $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
346         $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
348         $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
349         $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
350         $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
352         // Retrieve the conversations.
353         $conversations = \core_message\api::get_conversations($user1->id);
355         // Confirm the data is correct.
356         $this->assertEquals(3, count($conversations));
358         $message1 = array_shift($conversations);
359         $message2 = array_shift($conversations);
360         $message3 = array_shift($conversations);
362         $this->assertEquals($user4->id, $message1->userid);
363         $this->assertEquals($user1->id, $message1->useridfrom);
364         $this->assertTrue($message1->ismessaging);
365         $this->assertEquals('Dope.', $message1->lastmessage);
366         $this->assertNull($message1->messageid);
367         $this->assertFalse($message1->isonline);
368         $this->assertTrue($message1->isread);
369         $this->assertFalse($message1->isblocked);
370         $this->assertEquals(0, $message1->unreadcount);
372         $this->assertEquals($user3->id, $message2->userid);
373         $this->assertEquals($user3->id, $message2->useridfrom);
374         $this->assertTrue($message2->ismessaging);
375         $this->assertEquals('Cool.', $message2->lastmessage);
376         $this->assertNull($message2->messageid);
377         $this->assertFalse($message2->isonline);
378         $this->assertFalse($message2->isread);
379         $this->assertFalse($message2->isblocked);
380         $this->assertEquals(2, $message2->unreadcount);
382         $this->assertEquals($user2->id, $message3->userid);
383         $this->assertEquals($user2->id, $message3->useridfrom);
384         $this->assertTrue($message3->ismessaging);
385         $this->assertEquals('Word.', $message3->lastmessage);
386         $this->assertNull($message3->messageid);
387         $this->assertFalse($message3->isonline);
388         $this->assertFalse($message3->isread);
389         $this->assertFalse($message3->isblocked);
390         $this->assertEquals(2, $message3->unreadcount);
391     }
393     /**
394      * Tests retrieving contacts.
395      */
396     public function test_get_contacts() {
397         // Create some users.
398         $user1 = self::getDataGenerator()->create_user();
400         // Set as the user.
401         $this->setUser($user1);
403         $user2 = new stdClass();
404         $user2->firstname = 'User';
405         $user2->lastname = 'A';
406         $user2 = self::getDataGenerator()->create_user($user2);
408         $user3 = new stdClass();
409         $user3->firstname = 'User';
410         $user3->lastname = 'B';
411         $user3 = self::getDataGenerator()->create_user($user3);
413         $user4 = new stdClass();
414         $user4->firstname = 'User';
415         $user4->lastname = 'C';
416         $user4 = self::getDataGenerator()->create_user($user4);
418         $user5 = new stdClass();
419         $user5->firstname = 'User';
420         $user5->lastname = 'D';
421         $user5 = self::getDataGenerator()->create_user($user5);
423         // Add some users as contacts.
424         message_add_contact($user2->id, 0, $user1->id);
425         message_add_contact($user3->id, 0, $user1->id);
426         message_add_contact($user4->id, 0, $user1->id);
428         // Retrieve the contacts.
429         $contacts = \core_message\api::get_contacts($user1->id);
431         // Confirm the data is correct.
432         $this->assertEquals(3, count($contacts));
434         $contact1 = $contacts[0];
435         $contact2 = $contacts[1];
436         $contact3 = $contacts[2];
438         $this->assertEquals($user2->id, $contact1->userid);
439         $this->assertEmpty($contact1->useridfrom);
440         $this->assertFalse($contact1->ismessaging);
441         $this->assertNull($contact1->lastmessage);
442         $this->assertNull($contact1->messageid);
443         $this->assertFalse($contact1->isonline);
444         $this->assertFalse($contact1->isread);
445         $this->assertFalse($contact1->isblocked);
446         $this->assertNull($contact1->unreadcount);
448         $this->assertEquals($user3->id, $contact2->userid);
449         $this->assertEmpty($contact2->useridfrom);
450         $this->assertFalse($contact2->ismessaging);
451         $this->assertNull($contact2->lastmessage);
452         $this->assertNull($contact2->messageid);
453         $this->assertFalse($contact2->isonline);
454         $this->assertFalse($contact2->isread);
455         $this->assertFalse($contact2->isblocked);
456         $this->assertNull($contact2->unreadcount);
458         $this->assertEquals($user4->id, $contact3->userid);
459         $this->assertEmpty($contact3->useridfrom);
460         $this->assertFalse($contact3->ismessaging);
461         $this->assertNull($contact3->lastmessage);
462         $this->assertNull($contact3->messageid);
463         $this->assertFalse($contact3->isonline);
464         $this->assertFalse($contact3->isread);
465         $this->assertFalse($contact3->isblocked);
466         $this->assertNull($contact3->unreadcount);
467     }
469     /**
470      * Tests retrieving messages.
471      */
472     public function test_get_messages() {
473         // Create some users.
474         $user1 = self::getDataGenerator()->create_user();
475         $user2 = self::getDataGenerator()->create_user();
477         // The person doing the search.
478         $this->setUser($user1);
480         // Send some messages back and forth.
481         $time = 1;
482         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
483         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
484         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
485         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
487         // Retrieve the messages.
488         $messages = \core_message\api::get_messages($user1->id, $user2->id);
490         // Confirm the message data is correct.
491         $this->assertEquals(4, count($messages));
493         $message1 = $messages[0];
494         $message2 = $messages[1];
495         $message3 = $messages[2];
496         $message4 = $messages[3];
498         $this->assertEquals($user1->id, $message1->useridfrom);
499         $this->assertEquals($user2->id, $message1->useridto);
500         $this->assertTrue($message1->displayblocktime);
501         $this->assertContains('Yo!', $message1->text);
503         $this->assertEquals($user2->id, $message2->useridfrom);
504         $this->assertEquals($user1->id, $message2->useridto);
505         $this->assertFalse($message2->displayblocktime);
506         $this->assertContains('Sup mang?', $message2->text);
508         $this->assertEquals($user1->id, $message3->useridfrom);
509         $this->assertEquals($user2->id, $message3->useridto);
510         $this->assertFalse($message3->displayblocktime);
511         $this->assertContains('Writing PHPUnit tests!', $message3->text);
513         $this->assertEquals($user2->id, $message4->useridfrom);
514         $this->assertEquals($user1->id, $message4->useridto);
515         $this->assertFalse($message4->displayblocktime);
516         $this->assertContains('Word.', $message4->text);
517     }
519     /**
520      * Tests retrieving most recent message.
521      */
522     public function test_get_most_recent_message() {
523         // Create some users.
524         $user1 = self::getDataGenerator()->create_user();
525         $user2 = self::getDataGenerator()->create_user();
527         // The person doing the search.
528         $this->setUser($user1);
530         // Send some messages back and forth.
531         $time = 1;
532         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
533         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
534         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
535         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
537         // Retrieve the most recent messages.
538         $message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
540         // Check the results are correct.
541         $this->assertEquals($user2->id, $message->useridfrom);
542         $this->assertEquals($user1->id, $message->useridto);
543         $this->assertContains('Word.', $message->text);
544     }
546     /**
547      * Tests retrieving a user's profile.
548      */
549     public function test_get_profile() {
550         // Create some users.
551         $user1 = self::getDataGenerator()->create_user();
553         $user2 = new stdClass();
554         $user2->country = 'AU';
555         $user2->city = 'Perth';
556         $user2 = self::getDataGenerator()->create_user($user2);
558         // The person doing the search.
559         $this->setUser($user1);
561         // Get the profile.
562         $profile = \core_message\api::get_profile($user1->id, $user2->id);
564         $this->assertEquals($user2->id, $profile->userid);
565         $this->assertEmpty($profile->email);
566         $this->assertEmpty($profile->country);
567         $this->assertEmpty($profile->city);
568         $this->assertEquals(fullname($user2), $profile->fullname);
569         $this->assertFalse($profile->isonline);
570         $this->assertFalse($profile->isblocked);
571         $this->assertFalse($profile->iscontact);
572     }
574     /**
575      * Tests retrieving a user's profile.
576      */
577     public function test_get_profile_as_admin() {
578         // The person doing the search.
579         $this->setAdminUser();
581         // Create some users.
582         $user1 = self::getDataGenerator()->create_user();
584         $user2 = new stdClass();
585         $user2->country = 'AU';
586         $user2->city = 'Perth';
587         $user2 = self::getDataGenerator()->create_user($user2);
589         // Get the profile.
590         $profile = \core_message\api::get_profile($user1->id, $user2->id);
592         $this->assertEquals($user2->id, $profile->userid);
593         $this->assertEquals($user2->email, $profile->email);
594         $this->assertEquals($user2->country, $profile->country);
595         $this->assertEquals($user2->city, $profile->city);
596         $this->assertEquals(fullname($user2), $profile->fullname);
597         $this->assertFalse($profile->isonline);
598         $this->assertFalse($profile->isblocked);
599         $this->assertFalse($profile->iscontact);
600     }
602     /**
603      * Tests checking if a user can delete a conversation.
604      */
605     public function test_can_delete_conversation() {
606         // Set as the admin.
607         $this->setAdminUser();
609         // Create some users.
610         $user1 = self::getDataGenerator()->create_user();
611         $user2 = self::getDataGenerator()->create_user();
613         // The admin can do anything.
614         $this->assertTrue(\core_message\api::can_delete_conversation($user1->id));
616         // Set as the user 1.
617         $this->setUser($user1);
619         // They can delete their own messages.
620         $this->assertTrue(\core_message\api::can_delete_conversation($user1->id));
622         // They can't delete someone elses.
623         $this->assertFalse(\core_message\api::can_delete_conversation($user2->id));
624     }
626     /**
627      * Tests deleting a conversation.
628      */
629     public function test_delete_conversation() {
630         global $DB;
632         // Create some users.
633         $user1 = self::getDataGenerator()->create_user();
634         $user2 = self::getDataGenerator()->create_user();
636         // The person doing the search.
637         $this->setUser($user1);
639         // Send some messages back and forth.
640         $time = 1;
641         $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
642         $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
643         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
644         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
646         // Delete the conversation as user 1.
647         \core_message\api::delete_conversation($user1->id, $user2->id);
649         $messages = $DB->get_records('message', array(), 'timecreated ASC');
650         $this->assertCount(4, $messages);
652         $message1 = array_shift($messages);
653         $message2 = array_shift($messages);
654         $message3 = array_shift($messages);
655         $message4 = array_shift($messages);
657         $this->assertNotEmpty($message1->timeuserfromdeleted);
658         $this->assertEmpty($message1->timeusertodeleted);
660         $this->assertEmpty($message2->timeuserfromdeleted);
661         $this->assertNotEmpty($message2->timeusertodeleted);
663         $this->assertNotEmpty($message3->timeuserfromdeleted);
664         $this->assertEmpty($message3->timeusertodeleted);
666         $this->assertEmpty($message4->timeuserfromdeleted);
667         $this->assertNotEmpty($message4->timeusertodeleted);
669     }
671     /**
672      * Tests counting unread conversations.
673      */
674     public function test_count_unread_conversations() {
675         $this->resetAfterTest(true);
677         // Create some users.
678         $user1 = self::getDataGenerator()->create_user();
679         $user2 = self::getDataGenerator()->create_user();
680         $user3 = self::getDataGenerator()->create_user();
681         $user4 = self::getDataGenerator()->create_user();
683         // The person wanting the conversation count.
684         $this->setUser($user1);
686         // Send some messages back and forth, have some different conversations with different users.
687         $this->send_fake_message($user1, $user2, 'Yo!');
688         $this->send_fake_message($user2, $user1, 'Sup mang?');
689         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
690         $this->send_fake_message($user2, $user1, 'Word.');
692         $this->send_fake_message($user1, $user3, 'Booyah');
693         $this->send_fake_message($user3, $user1, 'Whaaat?');
694         $this->send_fake_message($user1, $user3, 'Nothing.');
695         $this->send_fake_message($user3, $user1, 'Cool.');
697         $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
698         $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
699         $this->send_fake_message($user1, $user4, 'Dope.');
701         // Check the amount for the current user.
702         $this->assertEquals(3, core_message\api::count_unread_conversations());
704         // Check the amount for the second user.
705         $this->assertEquals(1, core_message\api::count_unread_conversations($user2));
706     }
708     /**
709      * Tests deleting a conversation.
710      */
711     public function test_get_all_message_preferences() {
712         $user = self::getDataGenerator()->create_user();
713         $this->setUser($user);
715         // Set a couple of preferences to test.
716         set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
717         set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
719         $processors = get_message_processors();
720         $providers = message_get_providers_for_user($user->id);
721         $prefs = \core_message\api::get_all_message_preferences($processors, $providers, $user);
723         $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedin['popup']);
724         $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']);
725     }
727     /**
728      * Tests the user can post a message.
729      */
730     public function test_can_post_message() {
731         // Create some users.
732         $user1 = self::getDataGenerator()->create_user();
733         $user2 = self::getDataGenerator()->create_user();
735         // Set as the user 1.
736         $this->setUser($user1);
738         // They can post to someone else.
739         $this->assertTrue(\core_message\api::can_post_message($user2));
740     }
742     /**
743      * Tests the user can't post a message without proper capability.
744      */
745     public function test_can_post_message_without_cap() {
746         global $DB;
748         // Create some users.
749         $user1 = self::getDataGenerator()->create_user();
750         $user2 = self::getDataGenerator()->create_user();
752         // Set as the user 1.
753         $this->setUser($user1);
755         // Remove the capability to send a message.
756         $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
757         unassign_capability('moodle/site:sendmessage', $roleids['user'],
758             context_system::instance());
760         // Check that we can not post a message without the capability.
761         $this->assertFalse(\core_message\api::can_post_message($user2));
762     }
764     /**
765      * Tests the user can't post a message if they are not a contact and the user
766      * has requested messages only from contacts.
767      */
768     public function test_can_post_message_when_not_contact() {
769         // Create some users.
770         $user1 = self::getDataGenerator()->create_user();
771         $user2 = self::getDataGenerator()->create_user();
773         // Set as the first user.
774         $this->setUser($user1);
776         // Set the second user's preference to not receive messages from non-contacts.
777         set_user_preference('message_blocknoncontacts', 1, $user2->id);
779         // Check that we can not send user 2 a message.
780         $this->assertFalse(\core_message\api::can_post_message($user2));
781     }
783     /**
784      * Tests the user can't post a message if they are blocked.
785      */
786     public function test_can_post_message_when_blocked() {
787         // Create some users.
788         $user1 = self::getDataGenerator()->create_user();
789         $user2 = self::getDataGenerator()->create_user();
791         // Set the user.
792         $this->setUser($user1);
794         // Block the second user.
795         message_block_contact($user2->id);
797         // Check that the second user can no longer send the first user a message.
798         $this->assertFalse(\core_message\api::can_post_message($user1, $user2));
799     }
801     /**
802      * Tests that when blocking messages from non-contacts is enabled that
803      * non-contacts trying to send a message return false.
804      */
805     public function test_is_user_non_contact_blocked() {
806         // Create some users.
807         $user1 = self::getDataGenerator()->create_user();
808         $user2 = self::getDataGenerator()->create_user();
810         // Set as the first user.
811         $this->setUser($user1);
813         // User hasn't sent their preference to block non-contacts, so should return false.
814         $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
816         // Set the second user's preference to not receive messages from non-contacts.
817         set_user_preference('message_blocknoncontacts', 1, $user2->id);
819         // Check that the return result is now true.
820         $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
822         // Add the first user as a contact for the second user.
823         message_add_contact($user1->id, 0, $user2->id);
825         // Check that the return result is now false.
826         $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
827     }
829     /**
830      * Tests that we return true when a user is blocked, or false
831      * if they are not blocked.
832      */
833     public function test_is_user_blocked() {
834         // Create some users.
835         $user1 = self::getDataGenerator()->create_user();
836         $user2 = self::getDataGenerator()->create_user();
838         // Set the user.
839         $this->setUser($user1);
841         // User shouldn't be blocked.
842         $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
844         // Block the user.
845         message_block_contact($user2->id);
847         // User should be blocked.
848         $this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
849     }
851     /**
852      * Tests that the admin is not blocked even if someone has chosen to block them.
853      */
854     public function test_is_user_blocked_as_admin() {
855         // Create a user.
856         $user1 = self::getDataGenerator()->create_user();
858         // Set the user.
859         $this->setUser($user1);
861         // Block the admin user.
862         message_block_contact(2);
864         // Now change to the admin user.
865         $this->setAdminUser();
867         // As the admin you should still be able to send messages to the user.
868         $this->assertFalse(\core_message\api::is_user_blocked($user1->id));
869     }
871     /*
872      * Tes get_message_processor api.
873      */
874     public function test_get_message_processor() {
875         $processors = get_message_processors();
876         if (empty($processors)) {
877             $this->markTestSkipped("No message processors found");
878         }
880         list($name, $processor) = each($processors);
881         $testprocessor = \core_message\api::get_message_processor($name);
882         $this->assertEquals($processor->name, $testprocessor->name);
883         $this->assertEquals($processor->enabled, $testprocessor->enabled);
884         $this->assertEquals($processor->available, $testprocessor->available);
885         $this->assertEquals($processor->configured, $testprocessor->configured);
887         // Disable processor and test.
888         \core_message\api::update_processor_status($testprocessor, 0);
889         $testprocessor = \core_message\api::get_message_processor($name, true);
890         $this->assertEmpty($testprocessor);
891         $testprocessor = \core_message\api::get_message_processor($name);
892         $this->assertEquals($processor->name, $testprocessor->name);
893         $this->assertEquals(0, $testprocessor->enabled);
895         // Enable again and test.
896         \core_message\api::update_processor_status($testprocessor, 1);
897         $testprocessor = \core_message\api::get_message_processor($name, true);
898         $this->assertEquals($processor->name, $testprocessor->name);
899         $this->assertEquals(1, $testprocessor->enabled);
900         $testprocessor = \core_message\api::get_message_processor($name);
901         $this->assertEquals($processor->name, $testprocessor->name);
902         $this->assertEquals(1, $testprocessor->enabled);
903     }
905     /**
906      * Test method update_processor_status.
907      */
908     public function test_update_processor_status() {
909         $processors = get_message_processors();
910         if (empty($processors)) {
911             $this->markTestSkipped("No message processors found");
912         }
913         list($name, $testprocessor) = each($processors);
915         // Enable.
916         \core_message\api::update_processor_status($testprocessor, 1);
917         $testprocessor = \core_message\api::get_message_processor($name);
918         $this->assertEquals(1, $testprocessor->enabled);
920         // Disable.
921         \core_message\api::update_processor_status($testprocessor, 0);
922         $testprocessor = \core_message\api::get_message_processor($name);
923         $this->assertEquals(0, $testprocessor->enabled);
925         // Enable again.
926         \core_message\api::update_processor_status($testprocessor, 1);
927         $testprocessor = \core_message\api::get_message_processor($name);
928         $this->assertEquals(1, $testprocessor->enabled);
929     }
931     /**
932      * Test method is_user_enabled.
933      */
934     public function is_user_enabled() {
935         $processors = get_message_processors();
936         if (empty($processors)) {
937             $this->markTestSkipped("No message processors found");
938         }
939         list($name, $testprocessor) = each($processors);
941         // Enable.
942         \core_message\api::update_processor_status($testprocessor, 1);
943         $status = \core_message\api::is_processor_enabled($name);
944         $this->assertEquals(1, $status);
946         // Disable.
947         \core_message\api::update_processor_status($testprocessor, 0);
948         $status = \core_message\api::is_processor_enabled($name);
949         $this->assertEquals(0, $status);
951         // Enable again.
952         \core_message\api::update_processor_status($testprocessor, 1);
953         $status = \core_message\api::is_processor_enabled($name);
954         $this->assertEquals(1, $status);
955     }
957     /**
958      * Test retrieving messages by providing a minimum timecreated value.
959      */
960     public function test_get_messages_time_from_only() {
961         // Create some users.
962         $user1 = self::getDataGenerator()->create_user();
963         $user2 = self::getDataGenerator()->create_user();
965         // The person doing the search.
966         $this->setUser($user1);
968         // Send some messages back and forth.
969         $time = 1;
970         $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
971         $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
972         $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
973         $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
975         // Retrieve the messages from $time, which should be all of them.
976         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
978         // Confirm the message data is correct.
979         $this->assertEquals(4, count($messages));
981         $message1 = $messages[0];
982         $message2 = $messages[1];
983         $message3 = $messages[2];
984         $message4 = $messages[3];
986         $this->assertContains('Message 1', $message1->text);
987         $this->assertContains('Message 2', $message2->text);
988         $this->assertContains('Message 3', $message3->text);
989         $this->assertContains('Message 4', $message4->text);
991         // Retrieve the messages from $time + 3, which should only be the 2 last messages.
992         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
994         // Confirm the message data is correct.
995         $this->assertEquals(2, count($messages));
997         $message1 = $messages[0];
998         $message2 = $messages[1];
1000         $this->assertContains('Message 3', $message1->text);
1001         $this->assertContains('Message 4', $message2->text);
1002     }
1004     /**
1005      * Test retrieving messages by providing a maximum timecreated value.
1006      */
1007     public function test_get_messages_time_to_only() {
1008         // Create some users.
1009         $user1 = self::getDataGenerator()->create_user();
1010         $user2 = self::getDataGenerator()->create_user();
1012         // The person doing the search.
1013         $this->setUser($user1);
1015         // Send some messages back and forth.
1016         $time = 1;
1017         $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1018         $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1019         $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1020         $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1022         // Retrieve the messages up until $time + 4, which should be all of them.
1023         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
1025         // Confirm the message data is correct.
1026         $this->assertEquals(4, count($messages));
1028         $message1 = $messages[0];
1029         $message2 = $messages[1];
1030         $message3 = $messages[2];
1031         $message4 = $messages[3];
1033         $this->assertContains('Message 1', $message1->text);
1034         $this->assertContains('Message 2', $message2->text);
1035         $this->assertContains('Message 3', $message3->text);
1036         $this->assertContains('Message 4', $message4->text);
1038         // Retrieve the messages up until $time + 2, which should be the first two.
1039         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
1041         // Confirm the message data is correct.
1042         $this->assertEquals(2, count($messages));
1044         $message1 = $messages[0];
1045         $message2 = $messages[1];
1047         $this->assertContains('Message 1', $message1->text);
1048         $this->assertContains('Message 2', $message2->text);
1049     }
1051     /**
1052      * Test retrieving messages by providing a minimum and maximum timecreated value.
1053      */
1054     public function test_get_messages_time_from_and_to() {
1055         // Create some users.
1056         $user1 = self::getDataGenerator()->create_user();
1057         $user2 = self::getDataGenerator()->create_user();
1059         // The person doing the search.
1060         $this->setUser($user1);
1062         // Send some messages back and forth.
1063         $time = 1;
1064         $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1065         $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1066         $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1067         $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1069         // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
1070         $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
1072         // Confirm the message data is correct.
1073         $this->assertEquals(2, count($messages));
1075         $message1 = $messages[0];
1076         $message2 = $messages[1];
1078         $this->assertContains('Message 2', $message1->text);
1079         $this->assertContains('Message 3', $message2->text);
1080     }