Merge branch 'MDL-47162' of git://github.com/merrill-oakland/moodle
[moodle.git] / message / tests / externallib_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  * External message functions unit tests
19  *
20  * @package    core_message
21  * @category   external
22  * @copyright  2012 Jerome Mouneyrac
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 . '/webservice/tests/helpers.php');
31 require_once($CFG->dirroot . '/message/externallib.php');
33 class core_message_externallib_testcase extends externallib_advanced_testcase {
35     /**
36      * Tests set up
37      */
38     protected function setUp() {
39         global $CFG;
41         require_once($CFG->dirroot . '/message/lib.php');
42     }
44     /**
45      * Send a fake message.
46      *
47      * {@link message_send()} does not support transaction, this function will simulate a message
48      * sent from a user to another. We should stop using it once {@link message_send()} will support
49      * transactions. This is not clean at all, this is just used to add rows to the table.
50      *
51      * @param stdClass $userfrom user object of the one sending the message.
52      * @param stdClass $userto user object of the one receiving the message.
53      * @param string $message message to send.
54      * @param int $notification is the message a notification.
55      * @param int $time the time the message was sent
56      */
57     protected function send_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) {
58         global $DB;
60         if (empty($time)) {
61             $time = time();
62         }
64         $record = new stdClass();
65         $record->useridfrom = $userfrom->id;
66         $record->useridto = $userto->id;
67         $record->subject = 'No subject';
68         $record->smallmessage = $message;
69         $record->fullmessage = $message;
70         $record->timecreated = $time;
71         $record->notification = $notification;
72         $DB->insert_record('message', $record);
73     }
75     /**
76      * Test send_instant_messages
77      */
78     public function test_send_instant_messages() {
80         global $DB, $USER, $CFG;
82         $this->resetAfterTest(true);
83         // Transactions used in tests, tell phpunit use alternative reset method.
84         $this->preventResetByRollback();
86         // Turn off all message processors (so nothing is really sent)
87         require_once($CFG->dirroot . '/message/lib.php');
88         $messageprocessors = get_message_processors();
89         foreach($messageprocessors as $messageprocessor) {
90             $messageprocessor->enabled = 0;
91             $DB->update_record('message_processors', $messageprocessor);
92         }
94         // Set the required capabilities by the external function
95         $contextid = context_system::instance()->id;
96         $roleid = $this->assignUserCapability('moodle/site:sendmessage', $contextid);
98         $user1 = self::getDataGenerator()->create_user();
100         // Create test message data.
101         $message1 = array();
102         $message1['touserid'] = $user1->id;
103         $message1['text'] = 'the message.';
104         $message1['clientmsgid'] = 4;
105         $messages = array($message1);
107         $sentmessages = core_message_external::send_instant_messages($messages);
109         // We need to execute the return values cleaning process to simulate the web service server.
110         $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
112         $themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));
114         // Confirm that the message was inserted correctly.
115         $this->assertEquals($themessage->useridfrom, $USER->id);
116         $this->assertEquals($themessage->useridto, $message1['touserid']);
117         $this->assertEquals($themessage->smallmessage, $message1['text']);
118         $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
119     }
121     /**
122      * Test create_contacts.
123      */
124     public function test_create_contacts() {
125         $this->resetAfterTest(true);
127         $user1 = self::getDataGenerator()->create_user();
128         $user2 = self::getDataGenerator()->create_user();
129         $user3 = self::getDataGenerator()->create_user();
130         $user4 = self::getDataGenerator()->create_user();
131         $user5 = self::getDataGenerator()->create_user();
132         $this->setUser($user1);
134         // Adding a contact.
135         $return = core_message_external::create_contacts(array($user2->id));
136         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
137         $this->assertEquals(array(), $return);
139         // Adding a contact who is already a contact.
140         $return = core_message_external::create_contacts(array($user2->id));
141         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
142         $this->assertEquals(array(), $return);
144         // Adding multiple contacts.
145         $return = core_message_external::create_contacts(array($user3->id, $user4->id));
146         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
147         $this->assertEquals(array(), $return);
149         // Adding a non-existing user.
150         $return = core_message_external::create_contacts(array(99999));
151         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
152         $this->assertCount(1, $return);
153         $return = array_pop($return);
154         $this->assertEquals($return['warningcode'], 'contactnotcreated');
155         $this->assertEquals($return['itemid'], 99999);
157         // Adding contacts with valid and invalid parameters.
158         $return = core_message_external::create_contacts(array($user5->id, 99999));
159         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
160         $this->assertCount(1, $return);
161         $return = array_pop($return);
162         $this->assertEquals($return['warningcode'], 'contactnotcreated');
163         $this->assertEquals($return['itemid'], 99999);
165         // Try to add a contact to another user, should throw an exception.
166         // All assertions must be added before this point.
167         $this->expectException('required_capability_exception');
168         core_message_external::create_contacts(array($user2->id), $user3->id);
169     }
171     /**
172      * Test delete_contacts.
173      */
174     public function test_delete_contacts() {
175         $this->resetAfterTest(true);
177         $user1 = self::getDataGenerator()->create_user();
178         $user2 = self::getDataGenerator()->create_user();
179         $user3 = self::getDataGenerator()->create_user();
180         $user4 = self::getDataGenerator()->create_user();
181         $user5 = self::getDataGenerator()->create_user();
182         $user6 = self::getDataGenerator()->create_user();
183         $this->setUser($user1);
184         $this->assertEquals(array(), core_message_external::create_contacts(
185             array($user3->id, $user4->id, $user5->id, $user6->id)));
187         // Removing a non-contact.
188         $return = core_message_external::delete_contacts(array($user2->id));
189         $this->assertNull($return);
191         // Removing one contact.
192         $return = core_message_external::delete_contacts(array($user3->id));
193         $this->assertNull($return);
195         // Removing multiple contacts.
196         $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
197         $this->assertNull($return);
199         // Removing contact from unexisting user.
200         $return = core_message_external::delete_contacts(array(99999));
201         $this->assertNull($return);
203         // Removing mixed valid and invalid data.
204         $return = core_message_external::delete_contacts(array($user6->id, 99999));
205         $this->assertNull($return);
207         // Try to delete a contact of another user contact list, should throw an exception.
208         // All assertions must be added before this point.
209         $this->expectException('required_capability_exception');
210         core_message_external::delete_contacts(array($user2->id), $user3->id);
211     }
213     /**
214      * Test block_contacts.
215      */
216     public function test_block_contacts() {
217         $this->resetAfterTest(true);
219         $user1 = self::getDataGenerator()->create_user();
220         $user2 = self::getDataGenerator()->create_user();
221         $user3 = self::getDataGenerator()->create_user();
222         $user4 = self::getDataGenerator()->create_user();
223         $user5 = self::getDataGenerator()->create_user();
224         $this->setUser($user1);
225         $this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
227         // Blocking a contact.
228         $return = core_message_external::block_contacts(array($user2->id));
229         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
230         $this->assertEquals(array(), $return);
232         // Blocking a contact who is already a contact.
233         $return = core_message_external::block_contacts(array($user2->id));
234         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
235         $this->assertEquals(array(), $return);
237         // Blocking multiple contacts.
238         $return = core_message_external::block_contacts(array($user3->id, $user4->id));
239         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
240         $this->assertEquals(array(), $return);
242         // Blocking a non-existing user.
243         $return = core_message_external::block_contacts(array(99999));
244         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
245         $this->assertCount(1, $return);
246         $return = array_pop($return);
247         $this->assertEquals($return['warningcode'], 'contactnotblocked');
248         $this->assertEquals($return['itemid'], 99999);
250         // Blocking contacts with valid and invalid parameters.
251         $return = core_message_external::block_contacts(array($user5->id, 99999));
252         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
253         $this->assertCount(1, $return);
254         $return = array_pop($return);
255         $this->assertEquals($return['warningcode'], 'contactnotblocked');
256         $this->assertEquals($return['itemid'], 99999);
258         // Try to block a contact of another user contact list, should throw an exception.
259         // All assertions must be added before this point.
260         $this->expectException('required_capability_exception');
261         core_message_external::block_contacts(array($user2->id), $user3->id);
262     }
264     /**
265      * Test unblock_contacts.
266      */
267     public function test_unblock_contacts() {
268         $this->resetAfterTest(true);
270         $user1 = self::getDataGenerator()->create_user();
271         $user2 = self::getDataGenerator()->create_user();
272         $user3 = self::getDataGenerator()->create_user();
273         $user4 = self::getDataGenerator()->create_user();
274         $user5 = self::getDataGenerator()->create_user();
275         $user6 = self::getDataGenerator()->create_user();
276         $this->setUser($user1);
277         $this->assertEquals(array(), core_message_external::create_contacts(
278             array($user3->id, $user4->id, $user5->id, $user6->id)));
280         // Removing a non-contact.
281         $return = core_message_external::unblock_contacts(array($user2->id));
282         $this->assertNull($return);
284         // Removing one contact.
285         $return = core_message_external::unblock_contacts(array($user3->id));
286         $this->assertNull($return);
288         // Removing multiple contacts.
289         $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
290         $this->assertNull($return);
292         // Removing contact from unexisting user.
293         $return = core_message_external::unblock_contacts(array(99999));
294         $this->assertNull($return);
296         // Removing mixed valid and invalid data.
297         $return = core_message_external::unblock_contacts(array($user6->id, 99999));
298         $this->assertNull($return);
300         // Try to unblock a contact of another user contact list, should throw an exception.
301         // All assertions must be added before this point.
302         $this->expectException('required_capability_exception');
303         core_message_external::unblock_contacts(array($user2->id), $user3->id);
304     }
306     /**
307      * Test get_contacts.
308      */
309     public function test_get_contacts() {
310         $this->resetAfterTest(true);
312         $user1 = self::getDataGenerator()->create_user();
313         $user_stranger = self::getDataGenerator()->create_user();
314         $user_offline1 = self::getDataGenerator()->create_user();
315         $user_offline2 = self::getDataGenerator()->create_user();
316         $user_offline3 = self::getDataGenerator()->create_user();
317         $user_online = new stdClass();
318         $user_online->lastaccess = time();
319         $user_online = self::getDataGenerator()->create_user($user_online);
320         $user_blocked = self::getDataGenerator()->create_user();
321         $noreplyuser = core_user::get_user(core_user::NOREPLY_USER);
323         // Login as user1.
324         $this->setUser($user1);
325         $this->assertEquals(array(), core_message_external::create_contacts(
326             array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
328         // User_stranger sends a couple of messages to user1.
329         $this->send_message($user_stranger, $user1, 'Hello there!');
330         $this->send_message($user_stranger, $user1, 'How you goin?');
331         $this->send_message($user_stranger, $user1, 'Cya!');
332         $this->send_message($noreplyuser, $user1, 'I am not a real user');
334         // User_blocked sends a message to user1.
335         $this->send_message($user_blocked, $user1, 'Here, have some spam.');
337         // Retrieve the contacts of the user.
338         $this->setUser($user1);
339         $contacts = core_message_external::get_contacts();
340         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
341         $this->assertCount(3, $contacts['offline']);
342         $this->assertCount(1, $contacts['online']);
343         $this->assertCount(3, $contacts['strangers']);
344         core_message_external::block_contacts(array($user_blocked->id));
345         $contacts = core_message_external::get_contacts();
346         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
347         $this->assertCount(3, $contacts['offline']);
348         $this->assertCount(1, $contacts['online']);
349         $this->assertCount(2, $contacts['strangers']);
351         // Checking some of the fields returned.
352         $stranger = array_pop($contacts['strangers']);
354         $this->assertEquals(core_user::NOREPLY_USER, $stranger['id']);
355         $this->assertEquals(1, $stranger['unread']);
357         // Check that deleted users are not returned.
358         delete_user($user_offline1);
359         delete_user($user_stranger);
360         delete_user($user_online);
361         $contacts = core_message_external::get_contacts();
362         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
363         $this->assertCount(2, $contacts['offline']);
364         $this->assertCount(0, $contacts['online']);
365         $this->assertCount(1, $contacts['strangers']);
366     }
368     /**
369      * Test search_contacts.
370      * @expectedException moodle_exception
371      */
372     public function test_search_contacts() {
373         global $DB;
374         $this->resetAfterTest(true);
376         $course1 = $this->getDataGenerator()->create_course();
377         $course2 = $this->getDataGenerator()->create_course();
379         $user1 = new stdClass();
380         $user1->firstname = 'X';
381         $user1->lastname = 'X';
382         $user1 = $this->getDataGenerator()->create_user($user1);
383         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
384         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
386         $user2 = new stdClass();
387         $user2->firstname = 'Eric';
388         $user2->lastname = 'Cartman';
389         $user2 = self::getDataGenerator()->create_user($user2);
390         $user3 = new stdClass();
391         $user3->firstname = 'Stan';
392         $user3->lastname = 'Marsh';
393         $user3 = self::getDataGenerator()->create_user($user3);
394         self::getDataGenerator()->enrol_user($user3->id, $course1->id);
395         $user4 = new stdClass();
396         $user4->firstname = 'Kyle';
397         $user4->lastname = 'Broflovski';
398         $user4 = self::getDataGenerator()->create_user($user4);
399         $user5 = new stdClass();
400         $user5->firstname = 'Kenny';
401         $user5->lastname = 'McCormick';
402         $user5 = self::getDataGenerator()->create_user($user5);
403         self::getDataGenerator()->enrol_user($user5->id, $course2->id);
405         $this->setUser($user1);
407         $results = core_message_external::search_contacts('r');
408         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
409         $this->assertCount(5, $results); // Users 2 through 5 + admin
411         $results = core_message_external::search_contacts('r', true);
412         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
413         $this->assertCount(2, $results);
415         $results = core_message_external::search_contacts('Kyle', false);
416         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
417         $this->assertCount(1, $results);
418         $result = reset($results);
419         $this->assertEquals($user4->id, $result['id']);
421         $results = core_message_external::search_contacts('y', false);
422         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
423         $this->assertCount(2, $results);
425         $results = core_message_external::search_contacts('y', true);
426         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
427         $this->assertCount(1, $results);
428         $result = reset($results);
429         $this->assertEquals($user5->id, $result['id']);
431         // Empty query, will throw an exception.
432         $results = core_message_external::search_contacts('');
433     }
435     /**
436      * Test get_messages.
437      */
438     public function test_get_messages() {
439         global $CFG, $DB;
440         $this->resetAfterTest(true);
442         $this->preventResetByRollback();
443         // This mark the messages as read!.
444         $sink = $this->redirectMessages();
446         $user1 = self::getDataGenerator()->create_user();
447         $user2 = self::getDataGenerator()->create_user();
448         $user3 = self::getDataGenerator()->create_user();
450         $course = self::getDataGenerator()->create_course();
452         // Send a message from one user to another.
453         message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
454         message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE);
455         message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
456         message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
457         message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE);
459         $this->setUser($user1);
460         // Get read conversations from user1 to user2.
461         $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
462         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
463         $this->assertCount(1, $messages['messages']);
465         // Delete the message.
466         $message = array_shift($messages['messages']);
467         $messagetobedeleted = $DB->get_record('message_read', array('id' => $message['id']));
468         message_delete_message($messagetobedeleted, $user1->id);
470         $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
471         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
472         $this->assertCount(0, $messages['messages']);
474         // Get unread conversations from user1 to user2.
475         $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0);
476         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
477         $this->assertCount(0, $messages['messages']);
479         // Get read messages send from user1.
480         $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
481         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
482         $this->assertCount(1, $messages['messages']);
484         $this->setUser($user2);
485         // Get read conversations from any user to user2.
486         $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0);
487         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
488         $this->assertCount(2, $messages['messages']);
490         // Conversations from user3 to user2.
491         $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
492         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
493         $this->assertCount(1, $messages['messages']);
495         // Delete the message.
496         $message = array_shift($messages['messages']);
497         $messagetobedeleted = $DB->get_record('message_read', array('id' => $message['id']));
498         message_delete_message($messagetobedeleted, $user2->id);
500         $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
501         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
502         $this->assertCount(0, $messages['messages']);
504         $this->setUser($user3);
505         // Get read notifications received by user3.
506         $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0);
507         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
508         $this->assertCount(0, $messages['messages']);
510         // Now, create some notifications...
511         // We are creating fake notifications but based on real ones.
513         // This one omits notification = 1.
514         $eventdata = new \core\message\message();
515         $eventdata->courseid          = $course->id;
516         $eventdata->modulename        = 'moodle';
517         $eventdata->component         = 'enrol_paypal';
518         $eventdata->name              = 'paypal_enrolment';
519         $eventdata->userfrom          = get_admin();
520         $eventdata->userto            = $user1;
521         $eventdata->subject           = "Moodle: PayPal payment";
522         $eventdata->fullmessage       = "Your PayPal payment is pending.";
523         $eventdata->fullmessageformat = FORMAT_PLAIN;
524         $eventdata->fullmessagehtml   = '';
525         $eventdata->smallmessage      = '';
526         message_send($eventdata);
528         $message = new \core\message\message();
529         $message->courseid          = $course->id;
530         $message->notification      = 1;
531         $message->component         = 'enrol_manual';
532         $message->name              = 'expiry_notification';
533         $message->userfrom          = $user2;
534         $message->userto            = $user1;
535         $message->subject           = 'Enrolment expired';
536         $message->fullmessage       = 'Enrolment expired blah blah blah';
537         $message->fullmessageformat = FORMAT_MARKDOWN;
538         $message->fullmessagehtml   = markdown_to_html($message->fullmessage);
539         $message->smallmessage      = $message->subject;
540         $message->contexturlname    = $course->fullname;
541         $message->contexturl        = (string)new moodle_url('/course/view.php', array('id' => $course->id));
542         message_send($message);
544         $userfrom = core_user::get_noreply_user();
545         $userfrom->maildisplay = true;
546         $eventdata = new \core\message\message();
547         $eventdata->courseid          = $course->id;
548         $eventdata->component         = 'moodle';
549         $eventdata->name              = 'badgecreatornotice';
550         $eventdata->userfrom          = $userfrom;
551         $eventdata->userto            = $user1;
552         $eventdata->notification      = 1;
553         $eventdata->subject           = 'New badge';
554         $eventdata->fullmessage       = format_text_email($eventdata->subject, FORMAT_HTML);
555         $eventdata->fullmessageformat = FORMAT_PLAIN;
556         $eventdata->fullmessagehtml   = $eventdata->subject;
557         $eventdata->smallmessage      = $eventdata->subject;
558         message_send($eventdata);
560         $eventdata = new \core\message\message();
561         $eventdata->courseid         = $course->id;
562         $eventdata->name             = 'submission';
563         $eventdata->component        = 'mod_feedback';
564         $eventdata->userfrom         = $user1;
565         $eventdata->userto           = $user2;
566         $eventdata->subject          = 'Feedback submitted';
567         $eventdata->fullmessage      = 'Feedback submitted from an user';
568         $eventdata->fullmessageformat = FORMAT_PLAIN;
569         $eventdata->fullmessagehtml  = '<strong>Feedback submitted</strong>';
570         $eventdata->smallmessage     = '';
571         message_send($eventdata);
573         $this->setUser($user1);
574         // Get read notifications from any user to user1.
575         $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0);
576         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
577         $this->assertCount(3, $messages['messages']);
579         // Get one read notifications from any user to user1.
580         $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1);
581         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
582         $this->assertCount(1, $messages['messages']);
584         // Get unread notifications from any user to user1.
585         $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0);
586         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
587         $this->assertCount(0, $messages['messages']);
589         // Get read both type of messages from any user to user1.
590         $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0);
591         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
592         $this->assertCount(4, $messages['messages']);
594         // Get read notifications from no-reply-user to user1.
595         $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0);
596         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
597         $this->assertCount(1, $messages['messages']);
599         // Get notifications send by user1 to any user.
600         $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0);
601         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
602         $this->assertCount(1, $messages['messages']);
604         // Test warnings.
605         $CFG->messaging = 0;
607         $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0);
608         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
609         $this->assertCount(1, $messages['warnings']);
611         // Test exceptions.
613         // Messaging disabled.
614         try {
615             $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
616             $this->fail('Exception expected due messaging disabled.');
617         } catch (moodle_exception $e) {
618             $this->assertEquals('disabled', $e->errorcode);
619         }
621         $CFG->messaging = 1;
623         // Invalid users.
624         try {
625             $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0);
626             $this->fail('Exception expected due invalid users.');
627         } catch (moodle_exception $e) {
628             $this->assertEquals('accessdenied', $e->errorcode);
629         }
631         // Invalid user ids.
632         try {
633             $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0);
634             $this->fail('Exception expected due invalid users.');
635         } catch (moodle_exception $e) {
636             $this->assertEquals('invaliduser', $e->errorcode);
637         }
639         // Invalid users (permissions).
640         $this->setUser($user2);
641         try {
642             $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
643             $this->fail('Exception expected due invalid user.');
644         } catch (moodle_exception $e) {
645             $this->assertEquals('accessdenied', $e->errorcode);
646         }
648     }
650     /**
651      * Test get_blocked_users.
652      */
653     public function test_get_blocked_users() {
654         $this->resetAfterTest(true);
656         $user1 = self::getDataGenerator()->create_user();
657         $userstranger = self::getDataGenerator()->create_user();
658         $useroffline1 = self::getDataGenerator()->create_user();
659         $useroffline2 = self::getDataGenerator()->create_user();
660         $userblocked = self::getDataGenerator()->create_user();
662         // Login as user1.
663         $this->setUser($user1);
664         $this->assertEquals(array(), core_message_external::create_contacts(
665             array($useroffline1->id, $useroffline2->id)));
667         // The userstranger sends a couple of messages to user1.
668         $this->send_message($userstranger, $user1, 'Hello there!');
669         $this->send_message($userstranger, $user1, 'How you goin?');
671         // The userblocked sends a message to user1.
672         // Note that this user is not blocked at this point.
673         $this->send_message($userblocked, $user1, 'Here, have some spam.');
675         // Retrieve the list of blocked users.
676         $this->setUser($user1);
677         $blockedusers = core_message_external::get_blocked_users($user1->id);
678         $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
679         $this->assertCount(0, $blockedusers['users']);
681         // Block the $userblocked and retrieve again the list.
682         core_message_external::block_contacts(array($userblocked->id));
683         $blockedusers = core_message_external::get_blocked_users($user1->id);
684         $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
685         $this->assertCount(1, $blockedusers['users']);
687         // Remove the $userblocked and check that the list now is empty.
688         delete_user($userblocked);
689         $blockedusers = core_message_external::get_blocked_users($user1->id);
690         $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
691         $this->assertCount(0, $blockedusers['users']);
693     }
695     /**
696      * Test mark_message_read.
697      */
698     public function test_mark_message_read() {
699         $this->resetAfterTest(true);
701         $user1 = self::getDataGenerator()->create_user();
702         $user2 = self::getDataGenerator()->create_user();
703         $user3 = self::getDataGenerator()->create_user();
705         // Login as user1.
706         $this->setUser($user1);
707         $this->assertEquals(array(), core_message_external::create_contacts(
708             array($user2->id, $user3->id)));
710         // The user2 sends a couple of messages to user1.
711         $this->send_message($user2, $user1, 'Hello there!');
712         $this->send_message($user2, $user1, 'How you goin?');
713         $this->send_message($user3, $user1, 'How you goin?');
714         $this->send_message($user3, $user2, 'How you goin?');
716         // Retrieve all messages sent by user2 (they are currently unread).
717         $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
719         $messageids = array();
720         foreach ($lastmessages as $m) {
721             $messageid = core_message_external::mark_message_read($m->id, time());
722             $messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid);
723         }
725         // Retrieve all messages sent (they are currently read).
726         $lastmessages = message_get_messages($user1->id, $user2->id, 0, true);
727         $this->assertCount(2, $lastmessages);
728         $this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages);
729         $this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages);
731         // Retrieve all messages sent by any user (that are currently unread).
732         $lastmessages = message_get_messages($user1->id, 0, 0, false);
733         $this->assertCount(1, $lastmessages);
735         // Invalid message ids.
736         try {
737             $messageid = core_message_external::mark_message_read($messageids[0]['messageid'] * 2, time());
738             $this->fail('Exception expected due invalid messageid.');
739         } catch (dml_missing_record_exception $e) {
740             $this->assertEquals('invalidrecord', $e->errorcode);
741         }
743         // A message to a different user.
744         $lastmessages = message_get_messages($user2->id, $user3->id, 0, false);
745         $messageid = array_pop($lastmessages)->id;
746         try {
747             $messageid = core_message_external::mark_message_read($messageid, time());
748             $this->fail('Exception expected due invalid messageid.');
749         } catch (invalid_parameter_exception $e) {
750             $this->assertEquals('invalidparameter', $e->errorcode);
751         }
753     }
755     /**
756      * Test delete_message.
757      */
758     public function test_delete_message() {
759         global $DB;
760         $this->resetAfterTest(true);
762         $user1 = self::getDataGenerator()->create_user();
763         $user2 = self::getDataGenerator()->create_user();
764         $user3 = self::getDataGenerator()->create_user();
765         $user4 = self::getDataGenerator()->create_user();
767         // Login as user1.
768         $this->setUser($user1);
769         $this->assertEquals(array(), core_message_external::create_contacts(array($user2->id, $user3->id)));
771         // User user1 does not interchange messages with user3.
772         $m1to2 = message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
773         $m2to3 = message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
774         $m3to2 = message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
775         $m3to4 = message_post_message($user3, $user4, 'some random text 4', FORMAT_MOODLE);
777         // Retrieve all messages sent by user2 (they are currently unread).
778         $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
780         // Delete a message not read, as a user from.
781         $result = core_message_external::delete_message($m1to2, $user1->id, false);
782         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
783         $this->assertTrue($result['status']);
784         $this->assertCount(0, $result['warnings']);
785         $deletedmessage = $DB->get_record('message', array('id' => $m1to2));
786         $this->assertNotEquals(0, $deletedmessage->timeuserfromdeleted);
788         // Try to delete the same message again.
789         $result = core_message_external::delete_message($m1to2, $user1->id, false);
790         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
791         $this->assertFalse($result['status']);
793         // Try to delete a message that does not belong to me.
794         try {
795             $messageid = core_message_external::delete_message($m2to3, $user3->id, false);
796             $this->fail('Exception expected due invalid messageid.');
797         } catch (moodle_exception $e) {
798             $this->assertEquals('You do not have permission to delete this message', $e->errorcode);
799         }
801         $this->setUser($user3);
802         // Delete a message not read, as a user to.
803         $result = core_message_external::delete_message($m2to3, $user3->id, false);
804         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
805         $this->assertTrue($result['status']);
806         $this->assertCount(0, $result['warnings']);
807         $deletedmessage = $DB->get_record('message', array('id' => $m2to3));
808         $this->assertNotEquals(0, $deletedmessage->timeusertodeleted);
810         // Delete a message read.
811         $message = $DB->get_record('message', array('id' => $m3to2));
812         $messageid = message_mark_message_read($message, time());
813         $result = core_message_external::delete_message($messageid, $user3->id);
814         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
815         $this->assertTrue($result['status']);
816         $this->assertCount(0, $result['warnings']);
817         $deletedmessage = $DB->get_record('message_read', array('id' => $messageid));
818         $this->assertNotEquals(0, $deletedmessage->timeuserfromdeleted);
820         // Invalid message ids.
821         try {
822             $result = core_message_external::delete_message(-1, $user1->id);
823             $this->fail('Exception expected due invalid messageid.');
824         } catch (dml_missing_record_exception $e) {
825             $this->assertEquals('invalidrecord', $e->errorcode);
826         }
828         // Invalid user.
829         try {
830             $result = core_message_external::delete_message($m1to2, -1, false);
831             $this->fail('Exception expected due invalid user.');
832         } catch (moodle_exception $e) {
833             $this->assertEquals('invaliduser', $e->errorcode);
834         }
836         // Not active user.
837         delete_user($user2);
838         try {
839             $result = core_message_external::delete_message($m1to2, $user2->id, false);
840             $this->fail('Exception expected due invalid user.');
841         } catch (moodle_exception $e) {
842             $this->assertEquals('userdeleted', $e->errorcode);
843         }
845         // Now, as an admin, try to delete any message.
846         $this->setAdminUser();
847         $result = core_message_external::delete_message($m3to4, $user4->id, false);
848         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
849         $this->assertTrue($result['status']);
850         $this->assertCount(0, $result['warnings']);
851         $deletedmessage = $DB->get_record('message', array('id' => $m3to4));
852         $this->assertNotEquals(0, $deletedmessage->timeusertodeleted);
854     }
856     public function test_mark_all_notifications_as_read_invalid_user_exception() {
857         $this->resetAfterTest(true);
859         $this->expectException('moodle_exception');
860         core_message_external::mark_all_notifications_as_read(-2132131, 0);
861     }
863     public function test_mark_all_notifications_as_read_access_denied_exception() {
864         $this->resetAfterTest(true);
866         $sender = $this->getDataGenerator()->create_user();
867         $user = $this->getDataGenerator()->create_user();
869         $this->setUser($user);
870         $this->expectException('moodle_exception');
871         core_message_external::mark_all_notifications_as_read($sender->id, 0);
872     }
874     public function test_mark_all_notifications_as_read_missing_from_user_exception() {
875         $this->resetAfterTest(true);
877         $sender = $this->getDataGenerator()->create_user();
879         $this->setUser($sender);
880         $this->expectException('moodle_exception');
881         core_message_external::mark_all_notifications_as_read($sender->id, 99999);
882     }
884     public function test_mark_all_notifications_as_read() {
885         global $DB;
887         $this->resetAfterTest(true);
889         $sender1 = $this->getDataGenerator()->create_user();
890         $sender2 = $this->getDataGenerator()->create_user();
891         $sender3 = $this->getDataGenerator()->create_user();
892         $recipient = $this->getDataGenerator()->create_user();
894         $this->setUser($recipient);
896         $this->send_message($sender1, $recipient, 'Notification', 1);
897         $this->send_message($sender1, $recipient, 'Notification', 1);
898         $this->send_message($sender2, $recipient, 'Notification', 1);
899         $this->send_message($sender2, $recipient, 'Notification', 1);
900         $this->send_message($sender3, $recipient, 'Notification', 1);
901         $this->send_message($sender3, $recipient, 'Notification', 1);
903         core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id);
904         $readnotifications = $DB->get_recordset('message_read', ['useridto' => $recipient->id]);
905         $unreadnotifications = $DB->get_recordset('message', ['useridto' => $recipient->id]);
907         $this->assertCount(2, $readnotifications);
908         $this->assertCount(4, $unreadnotifications);
910         core_message_external::mark_all_notifications_as_read($recipient->id, 0);
911         $readnotifications = $DB->get_recordset('message_read', ['useridto' => $recipient->id]);
912         $unreadnotifications = $DB->get_recordset('message', ['useridto' => $recipient->id]);
914         $this->assertCount(6, $readnotifications);
915         $this->assertCount(0, $unreadnotifications);
916     }
918     /**
919      * Test get_user_notification_preferences
920      */
921     public function test_get_user_notification_preferences() {
922         $this->resetAfterTest(true);
924         $user = self::getDataGenerator()->create_user();
925         $this->setUser($user);
927         // Set a couple of preferences to test.
928         set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
929         set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
931         $prefs = core_message_external::get_user_notification_preferences();
932         $prefs = external_api::clean_returnvalue(core_message_external::get_user_notification_preferences_returns(), $prefs);
933         // Check processors.
934         $this->assertCount(2, $prefs['preferences']['processors']);
935         $this->assertEquals($user->id, $prefs['preferences']['userid']);
937         // Check components.
938         $this->assertCount(8, $prefs['preferences']['components']);
940         // Check some preferences that we previously set.
941         $found = 0;
942         foreach ($prefs['preferences']['components'] as $component) {
943             foreach ($component['notifications'] as $prefdata) {
944                 if ($prefdata['preferencekey'] != 'message_provider_mod_assign_assign_notification') {
945                     continue;
946                 }
947                 foreach ($prefdata['processors'] as $processor) {
948                     if ($processor['name'] == 'popup') {
949                         $this->assertTrue($processor['loggedin']['checked']);
950                         $found++;
951                     } else if ($processor['name'] == 'email') {
952                         $this->assertTrue($processor['loggedoff']['checked']);
953                         $found++;
954                     }
955                 }
956             }
957         }
958         $this->assertEquals(2, $found);
959     }
961     /**
962      * Test get_user_notification_preferences permissions
963      */
964     public function test_get_user_notification_preferences_permissions() {
965         $this->resetAfterTest(true);
967         $user = self::getDataGenerator()->create_user();
968         $otheruser = self::getDataGenerator()->create_user();
969         $this->setUser($user);
971         $this->expectException('moodle_exception');
972         $prefs = core_message_external::get_user_notification_preferences($otheruser->id);
973     }
975     /**
976      * Tests searching users in a course.
977      */
978     public function test_messagearea_search_users_in_course() {
979         $this->resetAfterTest(true);
981         // Create some users.
982         $user1 = new stdClass();
983         $user1->firstname = 'User';
984         $user1->lastname = 'One';
985         $user1 = self::getDataGenerator()->create_user($user1);
987         // The person doing the search.
988         $this->setUser($user1);
990         // Set the second user's status to online by setting their last access to now.
991         $user2 = new stdClass();
992         $user2->firstname = 'User';
993         $user2->lastname = 'Two';
994         $user2->lastaccess = time();
995         $user2 = self::getDataGenerator()->create_user($user2);
997         // Block the second user.
998         message_block_contact($user2->id, $user1->id);
1000         $user3 = new stdClass();
1001         $user3->firstname = 'User';
1002         $user3->lastname = 'Three';
1003         $user3 = self::getDataGenerator()->create_user($user3);
1005         // Create a course.
1006         $course1 = new stdClass();
1007         $course1->fullname = 'Course';
1008         $course1->shortname = 'One';
1009         $course1 = $this->getDataGenerator()->create_course();
1011         // Enrol the user we are doing the search for and one user in the course.
1012         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1013         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1015         // Perform a search.
1016         $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User');
1018         // We need to execute the return values cleaning process to simulate the web service.
1019         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(),
1020             $result);
1022         // Check that we only retrieved a user that was enrolled, and that the user performing the search was not returned.
1023         $users = $result['contacts'];
1024         $this->assertCount(1, $users);
1026         $user = $users[0];
1027         $this->assertEquals($user2->id, $user['userid']);
1028         $this->assertEquals(fullname($user2), $user['fullname']);
1029         $this->assertFalse($user['ismessaging']);
1030         $this->assertFalse($user['sentfromcurrentuser']);
1031         $this->assertNull($user['lastmessage']);
1032         $this->assertNull($user['messageid']);
1033         $this->assertTrue($user['isonline']);
1034         $this->assertFalse($user['isread']);
1035         $this->assertTrue($user['isblocked']);
1036         $this->assertNull($user['unreadcount']);
1037     }
1039     /**
1040      * Tests searching users in course as another user.
1041      */
1042     public function test_messagearea_search_users_in_course_as_other_user() {
1043         $this->resetAfterTest(true);
1045         // The person doing the search for another user.
1046         $this->setAdminUser();
1048         // Create some users.
1049         $user1 = new stdClass();
1050         $user1->firstname = 'User';
1051         $user1->lastname = 'One';
1052         $user1 = self::getDataGenerator()->create_user($user1);
1054         $user2 = new stdClass();
1055         $user2->firstname = 'User';
1056         $user2->lastname = 'Two';
1057         $user2 = self::getDataGenerator()->create_user($user2);
1059         $user3 = new stdClass();
1060         $user3->firstname = 'User';
1061         $user3->lastname = 'Three';
1062         $user3 = self::getDataGenerator()->create_user($user3);
1064         // Create a course.
1065         $course1 = new stdClass();
1066         $course1->fullname = 'Course';
1067         $course1->shortname = 'One';
1068         $course1 = $this->getDataGenerator()->create_course();
1070         // Enrol the user we are doing the search for and one user in the course.
1071         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1072         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1074         // Perform a search.
1075         $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User');
1077         // We need to execute the return values cleaning process to simulate the web service server.
1078         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(),
1079             $result);
1081         // Check that we got the user enrolled, and that the user we are performing the search on behalf of was not returned.
1082         $users = $result['contacts'];
1083         $this->assertCount(1, $users);
1085         $user = $users[0];
1086         $this->assertEquals($user2->id, $user['userid']);
1087         $this->assertEquals(fullname($user2), $user['fullname']);
1088         $this->assertFalse($user['ismessaging']);
1089         $this->assertFalse($user['sentfromcurrentuser']);
1090         $this->assertNull($user['lastmessage']);
1091         $this->assertNull($user['messageid']);
1092         $this->assertFalse($user['isonline']);
1093         $this->assertFalse($user['isread']);
1094         $this->assertFalse($user['isblocked']);
1095         $this->assertNull($user['unreadcount']);
1096     }
1098     /**
1099      * Tests searching users in course as another user without the proper capabilities.
1100      */
1101     public function test_messagearea_search_users_in_course_as_other_user_without_cap() {
1102         $this->resetAfterTest(true);
1104         // Create some users.
1105         $user1 = self::getDataGenerator()->create_user();
1106         $user2 = self::getDataGenerator()->create_user();
1108         // The person doing the search for another user.
1109         $this->setUser($user1);
1111         // Create a course.
1112         $course = $this->getDataGenerator()->create_course();
1114         // Ensure an exception is thrown.
1115         $this->expectException('moodle_exception');
1116         core_message_external::data_for_messagearea_search_users_in_course($user2->id, $course->id, 'User');
1117     }
1119     /**
1120      * Tests searching users in course with messaging disabled.
1121      */
1122     public function test_messagearea_search_users_in_course_messaging_disabled() {
1123         global $CFG;
1125         $this->resetAfterTest(true);
1127         // Create some skeleton data just so we can call the WS..
1128         $user = self::getDataGenerator()->create_user();
1129         $course = $this->getDataGenerator()->create_course();
1131         // The person doing the search for another user.
1132         $this->setUser($user);
1134         // Disable messaging.
1135         $CFG->messaging = 0;
1137         // Ensure an exception is thrown.
1138         $this->expectException('moodle_exception');
1139         core_message_external::data_for_messagearea_search_users_in_course($user->id, $course->id, 'User');
1140     }
1142     /**
1143      * Tests searching users.
1144      */
1145     public function test_messagearea_search_users() {
1146         $this->resetAfterTest(true);
1148         // Create some users.
1149         $user1 = new stdClass();
1150         $user1->firstname = 'User';
1151         $user1->lastname = 'One';
1152         $user1 = self::getDataGenerator()->create_user($user1);
1154         // Set as the user performing the search.
1155         $this->setUser($user1);
1157         $user2 = new stdClass();
1158         $user2->firstname = 'User search';
1159         $user2->lastname = 'Two';
1160         $user2 = self::getDataGenerator()->create_user($user2);
1162         $user3 = new stdClass();
1163         $user3->firstname = 'User search';
1164         $user3->lastname = 'Three';
1165         $user3 = self::getDataGenerator()->create_user($user3);
1167         $user4 = new stdClass();
1168         $user4->firstname = 'User';
1169         $user4->lastname = 'Four';
1170         $user4 = self::getDataGenerator()->create_user($user4);
1172         $user5 = new stdClass();
1173         $user5->firstname = 'User search';
1174         $user5->lastname = 'Five';
1175         $user5 = self::getDataGenerator()->create_user($user5);
1177         $user6 = new stdClass();
1178         $user6->firstname = 'User';
1179         $user6->lastname = 'Six';
1180         $user6 = self::getDataGenerator()->create_user($user6);
1182         // Create some courses.
1183         $course1 = new stdClass();
1184         $course1->fullname = 'Course search';
1185         $course1->shortname = 'One';
1186         $course1 = $this->getDataGenerator()->create_course($course1);
1188         $course2 = new stdClass();
1189         $course2->fullname = 'Course';
1190         $course2->shortname = 'Two';
1191         $course2 = $this->getDataGenerator()->create_course($course2);
1193         $course3 = new stdClass();
1194         $course3->fullname = 'Course';
1195         $course3->shortname = 'Three search';
1196         $course3 = $this->getDataGenerator()->create_course($course3);
1198         $course4 = new stdClass();
1199         $course4->fullname = 'Course Four';
1200         $course4->shortname = 'CF100';
1201         $course4 = $this->getDataGenerator()->create_course($course4);
1203         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
1204         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'student');
1205         $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 'student');
1207         // Add some users as contacts.
1208         message_add_contact($user2->id, 0, $user1->id);
1209         message_add_contact($user3->id, 0, $user1->id);
1210         message_add_contact($user4->id, 0, $user1->id);
1212         // Perform a search.
1213         $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
1215         // We need to execute the return values cleaning process to simulate the web service server.
1216         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
1217             $result);
1219         // Confirm that we returns contacts, courses and non-contacts.
1220         $contacts = $result['contacts'];
1221         $courses = $result['courses'];
1222         $noncontacts = $result['noncontacts'];
1224         // Check that we retrieved the correct contacts.
1225         $this->assertCount(2, $contacts);
1226         $this->assertEquals($user3->id, $contacts[0]['userid']);
1227         $this->assertEquals($user2->id, $contacts[1]['userid']);
1229         // Check that we retrieved the correct courses.
1230         $this->assertCount(2, $courses);
1231         $this->assertEquals($course3->id, $courses[0]['id']);
1232         $this->assertEquals($course1->id, $courses[1]['id']);
1234         // Check that we retrieved the correct non-contacts.
1235         $this->assertCount(1, $noncontacts);
1236         $this->assertEquals($user5->id, $noncontacts[0]['userid']);
1237     }
1239     /**
1240      * Tests searching users as another user.
1241      */
1242     public function test_messagearea_search_users_as_other_user() {
1243         $this->resetAfterTest(true);
1245         // The person doing the search.
1246         $this->setAdminUser();
1248         // Create some users.
1249         $user1 = new stdClass();
1250         $user1->firstname = 'User';
1251         $user1->lastname = 'One';
1252         $user1 = self::getDataGenerator()->create_user($user1);
1254         $user2 = new stdClass();
1255         $user2->firstname = 'User search';
1256         $user2->lastname = 'Two';
1257         $user2 = self::getDataGenerator()->create_user($user2);
1259         $user3 = new stdClass();
1260         $user3->firstname = 'User search';
1261         $user3->lastname = 'Three';
1262         $user3 = self::getDataGenerator()->create_user($user3);
1264         $user4 = new stdClass();
1265         $user4->firstname = 'User';
1266         $user4->lastname = 'Four';
1267         $user4 = self::getDataGenerator()->create_user($user4);
1269         $user5 = new stdClass();
1270         $user5->firstname = 'User search';
1271         $user5->lastname = 'Five';
1272         $user5 = self::getDataGenerator()->create_user($user5);
1274         $user6 = new stdClass();
1275         $user6->firstname = 'User';
1276         $user6->lastname = 'Six';
1277         $user6 = self::getDataGenerator()->create_user($user6);
1279         // Create some courses.
1280         $course1 = new stdClass();
1281         $course1->fullname = 'Course search';
1282         $course1->shortname = 'One';
1283         $course1 = $this->getDataGenerator()->create_course($course1);
1285         $course2 = new stdClass();
1286         $course2->fullname = 'Course';
1287         $course2->shortname = 'Two';
1288         $course2 = $this->getDataGenerator()->create_course($course2);
1290         $course3 = new stdClass();
1291         $course3->fullname = 'Course';
1292         $course3->shortname = 'Three search';
1293         $course3 = $this->getDataGenerator()->create_course($course3);
1295         // Add some users as contacts.
1296         message_add_contact($user2->id, 0, $user1->id);
1297         message_add_contact($user3->id, 0, $user1->id);
1298         message_add_contact($user4->id, 0, $user1->id);
1300         // Perform a search.
1301         $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
1303         // We need to execute the return values cleaning process to simulate the web service server.
1304         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
1305             $result);
1307         // Confirm that we returns contacts, courses and non-contacts.
1308         $contacts = $result['contacts'];
1309         $courses = $result['courses'];
1310         $noncontacts = $result['noncontacts'];
1312         // Check that we retrieved the correct contacts.
1313         $this->assertCount(2, $contacts);
1314         $this->assertEquals($user3->id, $contacts[0]['userid']);
1315         $this->assertEquals($user2->id, $contacts[1]['userid']);
1317         // Check that we retrieved the correct courses.
1318         $this->assertCount(0, $courses);
1320         // Check that we retrieved the correct non-contacts.
1321         $this->assertCount(1, $noncontacts);
1322         $this->assertEquals($user5->id, $noncontacts[0]['userid']);
1323     }
1325     /**
1326      * Tests searching users as another user without the proper capabilities.
1327      */
1328     public function test_messagearea_search_users_as_other_user_without_cap() {
1329         $this->resetAfterTest(true);
1331         // Create some users.
1332         $user1 = self::getDataGenerator()->create_user();
1333         $user2 = self::getDataGenerator()->create_user();
1335         // The person doing the search for another user.
1336         $this->setUser($user1);
1338         // Ensure an exception is thrown.
1339         $this->expectException('moodle_exception');
1340         core_message_external::data_for_messagearea_search_users($user2->id, 'User');
1341     }
1343     /**
1344      * Tests searching users with messaging disabled.
1345      */
1346     public function test_messagearea_search_users_messaging_disabled() {
1347         global $CFG;
1349         $this->resetAfterTest(true);
1351         // Create some skeleton data just so we can call the WS.
1352         $user = self::getDataGenerator()->create_user();
1354         // The person doing the search.
1355         $this->setUser($user);
1357         // Disable messaging.
1358         $CFG->messaging = 0;
1360         // Ensure an exception is thrown.
1361         $this->expectException('moodle_exception');
1362         core_message_external::data_for_messagearea_search_users($user->id, 'User');
1363     }
1365     /**
1366      * Tests searching messages.
1367      */
1368     public function test_messagearea_search_messages() {
1369         $this->resetAfterTest(true);
1371         // Create some users.
1372         $user1 = self::getDataGenerator()->create_user();
1373         $user2 = self::getDataGenerator()->create_user();
1375         // The person doing the search.
1376         $this->setUser($user1);
1378         // Send some messages back and forth.
1379         $time = time();
1380         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1381         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1382         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1383         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1385         // Perform a search.
1386         $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
1388         // We need to execute the return values cleaning process to simulate the web service server.
1389         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
1390             $result);
1392         // Confirm the data is correct.
1393         $messages = $result['contacts'];
1394         $this->assertCount(2, $messages);
1396         $message1 = $messages[0];
1397         $message2 = $messages[1];
1399         $this->assertEquals($user2->id, $message1['userid']);
1400         $this->assertEquals(fullname($user2), $message1['fullname']);
1401         $this->assertTrue($message1['ismessaging']);
1402         $this->assertFalse($message1['sentfromcurrentuser']);
1403         $this->assertEquals('Word.', $message1['lastmessage']);
1404         $this->assertNotEmpty($message1['messageid']);
1405         $this->assertFalse($message1['isonline']);
1406         $this->assertFalse($message1['isread']);
1407         $this->assertFalse($message1['isblocked']);
1408         $this->assertNull($message1['unreadcount']);
1410         $this->assertEquals($user2->id, $message2['userid']);
1411         $this->assertEquals(fullname($user2), $message2['fullname']);
1412         $this->assertTrue($message2['ismessaging']);
1413         $this->assertTrue($message2['sentfromcurrentuser']);
1414         $this->assertEquals('Yo!', $message2['lastmessage']);
1415         $this->assertNotEmpty($message2['messageid']);
1416         $this->assertFalse($message2['isonline']);
1417         $this->assertTrue($message2['isread']);
1418         $this->assertFalse($message2['isblocked']);
1419         $this->assertNull($message2['unreadcount']);
1420     }
1422     /**
1423      * Tests searching messages as another user.
1424      */
1425     public function test_messagearea_search_messages_as_other_user() {
1426         $this->resetAfterTest(true);
1428         // The person doing the search.
1429         $this->setAdminUser();
1431         // Create some users.
1432         $user1 = self::getDataGenerator()->create_user();
1433         $user2 = self::getDataGenerator()->create_user();
1435         // Send some messages back and forth.
1436         $time = time();
1437         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1438         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1439         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1440         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1442         // Perform a search.
1443         $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
1445         // We need to execute the return values cleaning process to simulate the web service server.
1446         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
1447             $result);
1449         // Confirm the data is correct.
1450         $messages = $result['contacts'];
1451         $this->assertCount(2, $messages);
1453         $message1 = $messages[0];
1454         $message2 = $messages[1];
1456         $this->assertEquals($user2->id, $message1['userid']);
1457         $this->assertEquals(fullname($user2), $message1['fullname']);
1458         $this->assertTrue($message1['ismessaging']);
1459         $this->assertFalse($message1['sentfromcurrentuser']);
1460         $this->assertEquals('Word.', $message1['lastmessage']);
1461         $this->assertNotEmpty($message1['messageid']);
1462         $this->assertFalse($message1['isonline']);
1463         $this->assertFalse($message1['isread']);
1464         $this->assertFalse($message1['isblocked']);
1465         $this->assertNull($message1['unreadcount']);
1467         $this->assertEquals($user2->id, $message2['userid']);
1468         $this->assertEquals(fullname($user2), $message2['fullname']);
1469         $this->assertTrue($message2['ismessaging']);
1470         $this->assertTrue($message2['sentfromcurrentuser']);
1471         $this->assertEquals('Yo!', $message2['lastmessage']);
1472         $this->assertNotEmpty($message2['messageid']);
1473         $this->assertFalse($message2['isonline']);
1474         $this->assertTrue($message2['isread']);
1475         $this->assertFalse($message2['isblocked']);
1476         $this->assertNull($message2['unreadcount']);
1477     }
1479     /**
1480      * Tests searching messages as another user without the proper capabilities.
1481      */
1482     public function test_messagearea_search_messages_as_other_user_without_cap() {
1483         $this->resetAfterTest(true);
1485         // Create some users.
1486         $user1 = self::getDataGenerator()->create_user();
1487         $user2 = self::getDataGenerator()->create_user();
1489         // The person doing the search for another user.
1490         $this->setUser($user1);
1492         // Ensure an exception is thrown.
1493         $this->expectException('moodle_exception');
1494         core_message_external::data_for_messagearea_search_messages($user2->id, 'Search');
1495     }
1497     /**
1498      * Tests searching messages with messaging disabled
1499      */
1500     public function test_messagearea_search_messages_messaging_disabled() {
1501         global $CFG;
1503         $this->resetAfterTest(true);
1505         // Create some skeleton data just so we can call the WS.
1506         $user = self::getDataGenerator()->create_user();
1508         // The person doing the search .
1509         $this->setUser($user);
1511         // Disable messaging.
1512         $CFG->messaging = 0;
1514         // Ensure an exception is thrown.
1515         $this->expectException('moodle_exception');
1516         core_message_external::data_for_messagearea_search_messages($user->id, 'Search');
1517     }
1519     /**
1520      * Tests retrieving conversations.
1521      */
1522     public function test_messagearea_conversations() {
1523         $this->resetAfterTest(true);
1525         // Create some users.
1526         $user1 = self::getDataGenerator()->create_user();
1527         $user2 = self::getDataGenerator()->create_user();
1528         $user3 = self::getDataGenerator()->create_user();
1529         $user4 = self::getDataGenerator()->create_user();
1531         // The person retrieving the conversations.
1532         $this->setUser($user1);
1534         // Send some messages back and forth, have some different conversations with different users.
1535         $time = time();
1536         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1537         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1538         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1539         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1541         $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
1542         $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
1543         $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
1544         $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
1546         $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
1547         $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
1548         $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
1550         // Retrieve the conversations.
1551         $result = core_message_external::data_for_messagearea_conversations($user1->id);
1553         // We need to execute the return values cleaning process to simulate the web service server.
1554         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
1555             $result);
1557         // Confirm the data is correct.
1558         $messages = $result['contacts'];
1559         $this->assertCount(3, $messages);
1561         $message1 = $messages[0];
1562         $message2 = $messages[1];
1563         $message3 = $messages[2];
1565         $this->assertEquals($user4->id, $message1['userid']);
1566         $this->assertTrue($message1['ismessaging']);
1567         $this->assertTrue($message1['sentfromcurrentuser']);
1568         $this->assertEquals('Dope.', $message1['lastmessage']);
1569         $this->assertNull($message1['messageid']);
1570         $this->assertFalse($message1['isonline']);
1571         $this->assertTrue($message1['isread']);
1572         $this->assertFalse($message1['isblocked']);
1573         $this->assertEquals(0, $message1['unreadcount']);
1575         $this->assertEquals($user3->id, $message2['userid']);
1576         $this->assertTrue($message2['ismessaging']);
1577         $this->assertFalse($message2['sentfromcurrentuser']);
1578         $this->assertEquals('Cool.', $message2['lastmessage']);
1579         $this->assertNull($message2['messageid']);
1580         $this->assertFalse($message2['isonline']);
1581         $this->assertFalse($message2['isread']);
1582         $this->assertFalse($message2['isblocked']);
1583         $this->assertEquals(2, $message2['unreadcount']);
1585         $this->assertEquals($user2->id, $message3['userid']);
1586         $this->assertTrue($message3['ismessaging']);
1587         $this->assertFalse($message3['sentfromcurrentuser']);
1588         $this->assertEquals('Word.', $message3['lastmessage']);
1589         $this->assertNull($message3['messageid']);
1590         $this->assertFalse($message3['isonline']);
1591         $this->assertFalse($message3['isread']);
1592         $this->assertFalse($message3['isblocked']);
1593         $this->assertEquals(2, $message3['unreadcount']);
1594     }
1596     /**
1597      * Tests retrieving conversations as another user.
1598      */
1599     public function test_messagearea_conversations_as_other_user() {
1600         $this->resetAfterTest(true);
1602         // Set as admin.
1603         $this->setAdminUser();
1605         // Create some users.
1606         $user1 = self::getDataGenerator()->create_user();
1607         $user2 = self::getDataGenerator()->create_user();
1608         $user3 = self::getDataGenerator()->create_user();
1609         $user4 = self::getDataGenerator()->create_user();
1611         // Send some messages back and forth, have some different conversations with different users.
1612         $time = time();
1613         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1614         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1615         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1616         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1618         $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
1619         $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
1620         $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
1621         $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
1623         $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
1624         $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
1625         $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
1627         // Retrieve the conversations.
1628         $result = core_message_external::data_for_messagearea_conversations($user1->id);
1630         // We need to execute the return values cleaning process to simulate the web service server.
1631         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
1632             $result);
1634         // Confirm the data is correct.
1635         $messages = $result['contacts'];
1636         $this->assertCount(3, $messages);
1638         $message1 = $messages[0];
1639         $message2 = $messages[1];
1640         $message3 = $messages[2];
1642         $this->assertEquals($user4->id, $message1['userid']);
1643         $this->assertTrue($message1['ismessaging']);
1644         $this->assertTrue($message1['sentfromcurrentuser']);
1645         $this->assertEquals('Dope.', $message1['lastmessage']);
1646         $this->assertNull($message1['messageid']);
1647         $this->assertFalse($message1['isonline']);
1648         $this->assertTrue($message1['isread']);
1649         $this->assertFalse($message1['isblocked']);
1650         $this->assertEquals(0, $message1['unreadcount']);
1652         $this->assertEquals($user3->id, $message2['userid']);
1653         $this->assertTrue($message2['ismessaging']);
1654         $this->assertFalse($message2['sentfromcurrentuser']);
1655         $this->assertEquals('Cool.', $message2['lastmessage']);
1656         $this->assertNull($message2['messageid']);
1657         $this->assertFalse($message2['isonline']);
1658         $this->assertFalse($message2['isread']);
1659         $this->assertFalse($message2['isblocked']);
1660         $this->assertEquals(2, $message2['unreadcount']);
1662         $this->assertEquals($user2->id, $message3['userid']);
1663         $this->assertTrue($message3['ismessaging']);
1664         $this->assertFalse($message3['sentfromcurrentuser']);
1665         $this->assertEquals('Word.', $message3['lastmessage']);
1666         $this->assertNull($message3['messageid']);
1667         $this->assertFalse($message3['isonline']);
1668         $this->assertFalse($message3['isread']);
1669         $this->assertFalse($message3['isblocked']);
1670         $this->assertEquals(2, $message3['unreadcount']);
1671     }
1673     /**
1674      * Tests retrieving conversations as another user without the proper capabilities.
1675      */
1676     public function test_messagearea_conversations_as_other_user_without_cap() {
1677         $this->resetAfterTest(true);
1679         // Create some users.
1680         $user1 = self::getDataGenerator()->create_user();
1681         $user2 = self::getDataGenerator()->create_user();
1683         // The person retrieving the conversations for another user.
1684         $this->setUser($user1);
1686         // Ensure an exception is thrown.
1687         $this->expectException('moodle_exception');
1688         core_message_external::data_for_messagearea_conversations($user2->id);
1689     }
1691     /**
1692      * Tests retrieving conversations with messaging disabled.
1693      */
1694     public function test_messagearea_conversations_messaging_disabled() {
1695         global $CFG;
1697         $this->resetAfterTest(true);
1699         // Create some skeleton data just so we can call the WS.
1700         $user = self::getDataGenerator()->create_user();
1702         // The person retrieving the conversations.
1703         $this->setUser($user);
1705         // Disable messaging.
1706         $CFG->messaging = 0;
1708         // Ensure an exception is thrown.
1709         $this->expectException('moodle_exception');
1710         core_message_external::data_for_messagearea_conversations($user->id);
1711     }
1713     /**
1714      * Tests retrieving contacts.
1715      */
1716     public function test_messagearea_contacts() {
1717         $this->resetAfterTest(true);
1719         // Create some users.
1720         $user1 = self::getDataGenerator()->create_user();
1722         // Set as the user.
1723         $this->setUser($user1);
1725         $user2 = new stdClass();
1726         $user2->firstname = 'User';
1727         $user2->lastname = 'A';
1728         $user2 = self::getDataGenerator()->create_user($user2);
1730         $user3 = new stdClass();
1731         $user3->firstname = 'User';
1732         $user3->lastname = 'B';
1733         $user3 = self::getDataGenerator()->create_user($user3);
1735         $user4 = new stdClass();
1736         $user4->firstname = 'User';
1737         $user4->lastname = 'C';
1738         $user4 = self::getDataGenerator()->create_user($user4);
1740         $user5 = new stdClass();
1741         $user5->firstname = 'User';
1742         $user5->lastname = 'D';
1743         $user5 = self::getDataGenerator()->create_user($user5);
1745         // Add some users as contacts.
1746         message_add_contact($user2->id, 0, $user1->id);
1747         message_add_contact($user3->id, 0, $user1->id);
1748         message_add_contact($user4->id, 0, $user1->id);
1750         // Retrieve the contacts.
1751         $result = core_message_external::data_for_messagearea_contacts($user1->id);
1753         // We need to execute the return values cleaning process to simulate the web service server.
1754         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
1755             $result);
1757         // Confirm the data is correct.
1758         $contacts = $result['contacts'];
1759         $this->assertCount(3, $contacts);
1761         $contact1 = $contacts[0];
1762         $contact2 = $contacts[1];
1763         $contact3 = $contacts[2];
1765         $this->assertEquals($user2->id, $contact1['userid']);
1766         $this->assertFalse($contact1['ismessaging']);
1767         $this->assertFalse($contact1['sentfromcurrentuser']);
1768         $this->assertNull($contact1['lastmessage']);
1769         $this->assertNull($contact1['messageid']);
1770         $this->assertFalse($contact1['isonline']);
1771         $this->assertFalse($contact1['isread']);
1772         $this->assertFalse($contact1['isblocked']);
1773         $this->assertNull($contact1['unreadcount']);
1775         $this->assertEquals($user3->id, $contact2['userid']);
1776         $this->assertFalse($contact2['ismessaging']);
1777         $this->assertFalse($contact2['sentfromcurrentuser']);
1778         $this->assertNull($contact2['lastmessage']);
1779         $this->assertNull($contact2['messageid']);
1780         $this->assertFalse($contact2['isonline']);
1781         $this->assertFalse($contact2['isread']);
1782         $this->assertFalse($contact2['isblocked']);
1783         $this->assertNull($contact2['unreadcount']);
1785         $this->assertEquals($user4->id, $contact3['userid']);
1786         $this->assertFalse($contact3['ismessaging']);
1787         $this->assertFalse($contact3['sentfromcurrentuser']);
1788         $this->assertNull($contact3['lastmessage']);
1789         $this->assertNull($contact3['messageid']);
1790         $this->assertFalse($contact3['isonline']);
1791         $this->assertFalse($contact3['isread']);
1792         $this->assertFalse($contact3['isblocked']);
1793         $this->assertNull($contact3['unreadcount']);
1794     }
1796     /**
1797      * Tests retrieving contacts as another user.
1798      */
1799     public function test_messagearea_contacts_as_other_user() {
1800         $this->resetAfterTest(true);
1802         $this->setAdminUser();
1804         // Create some users.
1805         $user1 = self::getDataGenerator()->create_user();
1807         $user2 = new stdClass();
1808         $user2->firstname = 'User';
1809         $user2->lastname = 'A';
1810         $user2 = self::getDataGenerator()->create_user($user2);
1812         $user3 = new stdClass();
1813         $user3->firstname = 'User';
1814         $user3->lastname = 'B';
1815         $user3 = self::getDataGenerator()->create_user($user3);
1817         $user4 = new stdClass();
1818         $user4->firstname = 'User';
1819         $user4->lastname = 'C';
1820         $user4 = self::getDataGenerator()->create_user($user4);
1822         $user5 = new stdClass();
1823         $user5->firstname = 'User';
1824         $user5->lastname = 'D';
1825         $user5 = self::getDataGenerator()->create_user($user5);
1827         // Add some users as contacts.
1828         message_add_contact($user2->id, 0, $user1->id);
1829         message_add_contact($user3->id, 0, $user1->id);
1830         message_add_contact($user4->id, 0, $user1->id);
1832         // Retrieve the contacts.
1833         $result = core_message_external::data_for_messagearea_contacts($user1->id);
1835         // We need to execute the return values cleaning process to simulate the web service server.
1836         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
1837             $result);
1839         // Confirm the data is correct.
1840         $contacts = $result['contacts'];
1841         $this->assertCount(3, $contacts);
1843         $contact1 = $contacts[0];
1844         $contact2 = $contacts[1];
1845         $contact3 = $contacts[2];
1847         $this->assertEquals($user2->id, $contact1['userid']);
1848         $this->assertFalse($contact1['ismessaging']);
1849         $this->assertFalse($contact1['sentfromcurrentuser']);
1850         $this->assertNull($contact1['lastmessage']);
1851         $this->assertNull($contact1['messageid']);
1852         $this->assertFalse($contact1['isonline']);
1853         $this->assertFalse($contact1['isread']);
1854         $this->assertFalse($contact1['isblocked']);
1855         $this->assertNull($contact1['unreadcount']);
1857         $this->assertEquals($user3->id, $contact2['userid']);
1858         $this->assertFalse($contact2['ismessaging']);
1859         $this->assertFalse($contact2['sentfromcurrentuser']);
1860         $this->assertNull($contact2['lastmessage']);
1861         $this->assertNull($contact2['messageid']);
1862         $this->assertFalse($contact2['isonline']);
1863         $this->assertFalse($contact2['isread']);
1864         $this->assertFalse($contact2['isblocked']);
1865         $this->assertNull($contact2['unreadcount']);
1867         $this->assertEquals($user4->id, $contact3['userid']);
1868         $this->assertFalse($contact3['ismessaging']);
1869         $this->assertFalse($contact3['sentfromcurrentuser']);
1870         $this->assertNull($contact3['lastmessage']);
1871         $this->assertNull($contact3['messageid']);
1872         $this->assertFalse($contact3['isonline']);
1873         $this->assertFalse($contact3['isread']);
1874         $this->assertFalse($contact3['isblocked']);
1875         $this->assertNull($contact3['unreadcount']);
1876     }
1878     /**
1879      * Tests retrieving contacts as another user without the proper capabilities.
1880      */
1881     public function test_messagearea_contacts_as_other_user_without_cap() {
1882         $this->resetAfterTest(true);
1884         // Create some users.
1885         $user1 = self::getDataGenerator()->create_user();
1886         $user2 = self::getDataGenerator()->create_user();
1888         // The person retrieving the contacts for another user.
1889         $this->setUser($user1);
1891         // Perform the WS call and ensure an exception is thrown.
1892         $this->expectException('moodle_exception');
1893         core_message_external::data_for_messagearea_contacts($user2->id);
1894     }
1896     /**
1897      * Tests retrieving contacts with messaging disabled.
1898      */
1899     public function test_messagearea_contacts_messaging_disabled() {
1900         global $CFG;
1902         $this->resetAfterTest(true);
1904         // Create some skeleton data just so we can call the WS.
1905         $user = self::getDataGenerator()->create_user();
1907         // The person retrieving the contacts.
1908         $this->setUser($user);
1910         // Disable messaging.
1911         $CFG->messaging = 0;
1913         // Perform the WS call and ensure we are shown that it is disabled.
1914         $this->expectException('moodle_exception');
1915         core_message_external::data_for_messagearea_contacts($user->id);
1916     }
1918     /**
1919      * Tests retrieving messages.
1920      */
1921     public function test_messagearea_messages() {
1922         $this->resetAfterTest(true);
1924         // Create some users.
1925         $user1 = self::getDataGenerator()->create_user();
1926         $user2 = self::getDataGenerator()->create_user();
1928         // The person asking for the messages.
1929         $this->setUser($user1);
1931         // Send some messages back and forth.
1932         $time = time();
1933         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1934         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1935         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1936         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1938         // Retrieve the messages.
1939         $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
1941         // We need to execute the return values cleaning process to simulate the web service server.
1942         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
1943             $result);
1945         // Check the results are correct.
1946         $this->assertTrue($result['iscurrentuser']);
1947         $this->assertEquals($user1->id, $result['currentuserid']);
1948         $this->assertEquals($user2->id, $result['otheruserid']);
1949         $this->assertEquals(fullname($user2), $result['otheruserfullname']);
1950         $this->assertFalse($result['isonline']);
1952         // Confirm the message data is correct.
1953         $messages = $result['messages'];
1954         $this->assertCount(4, $messages);
1956         $message1 = $messages[0];
1957         $message2 = $messages[1];
1958         $message3 = $messages[2];
1959         $message4 = $messages[3];
1961         $this->assertEquals($user1->id, $message1['useridfrom']);
1962         $this->assertEquals($user2->id, $message1['useridto']);
1963         $this->assertTrue($message1['displayblocktime']);
1964         $this->assertContains('Yo!', $message1['text']);
1966         $this->assertEquals($user2->id, $message2['useridfrom']);
1967         $this->assertEquals($user1->id, $message2['useridto']);
1968         $this->assertFalse($message2['displayblocktime']);
1969         $this->assertContains('Sup mang?', $message2['text']);
1971         $this->assertEquals($user1->id, $message3['useridfrom']);
1972         $this->assertEquals($user2->id, $message3['useridto']);
1973         $this->assertFalse($message3['displayblocktime']);
1974         $this->assertContains('Writing PHPUnit tests!', $message3['text']);
1976         $this->assertEquals($user2->id, $message4['useridfrom']);
1977         $this->assertEquals($user1->id, $message4['useridto']);
1978         $this->assertFalse($message4['displayblocktime']);
1979         $this->assertContains('Word.', $message4['text']);
1980     }
1982     /**
1983      * Tests retrieving messages as another user.
1984      */
1985     public function test_messagearea_messages_as_other_user() {
1986         $this->resetAfterTest(true);
1988         // Set as admin.
1989         $this->setAdminUser();
1991         // Create some users.
1992         $user1 = self::getDataGenerator()->create_user();
1993         $user2 = self::getDataGenerator()->create_user();
1995         // Send some messages back and forth.
1996         $time = time();
1997         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1998         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1999         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2000         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2002         // Retrieve the messages.
2003         $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
2005         // We need to execute the return values cleaning process to simulate the web service server.
2006         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
2007             $result);
2009         // Check the results are correct.
2010         $this->assertFalse($result['iscurrentuser']);
2011         $this->assertEquals($user1->id, $result['currentuserid']);
2012         $this->assertEquals($user2->id, $result['otheruserid']);
2013         $this->assertEquals(fullname($user2), $result['otheruserfullname']);
2014         $this->assertFalse($result['isonline']);
2016         // Confirm the message data is correct.
2017         $messages = $result['messages'];
2018         $this->assertCount(4, $messages);
2020         $message1 = $messages[0];
2021         $message2 = $messages[1];
2022         $message3 = $messages[2];
2023         $message4 = $messages[3];
2025         $this->assertEquals($user1->id, $message1['useridfrom']);
2026         $this->assertEquals($user2->id, $message1['useridto']);
2027         $this->assertTrue($message1['displayblocktime']);
2028         $this->assertContains('Yo!', $message1['text']);
2030         $this->assertEquals($user2->id, $message2['useridfrom']);
2031         $this->assertEquals($user1->id, $message2['useridto']);
2032         $this->assertFalse($message2['displayblocktime']);
2033         $this->assertContains('Sup mang?', $message2['text']);
2035         $this->assertEquals($user1->id, $message3['useridfrom']);
2036         $this->assertEquals($user2->id, $message3['useridto']);
2037         $this->assertFalse($message3['displayblocktime']);
2038         $this->assertContains('Writing PHPUnit tests!', $message3['text']);
2040         $this->assertEquals($user2->id, $message4['useridfrom']);
2041         $this->assertEquals($user1->id, $message4['useridto']);
2042         $this->assertFalse($message4['displayblocktime']);
2043         $this->assertContains('Word.', $message4['text']);
2044     }
2046     /**
2047      * Tests retrieving messages as another user without the proper capabilities.
2048      */
2049     public function test_messagearea_messages_as_other_user_without_cap() {
2050         $this->resetAfterTest(true);
2052         // Create some users.
2053         $user1 = self::getDataGenerator()->create_user();
2054         $user2 = self::getDataGenerator()->create_user();
2055         $user3 = self::getDataGenerator()->create_user();
2057         // The person asking for the messages for another user.
2058         $this->setUser($user1);
2060         // Ensure an exception is thrown.
2061         $this->expectException('moodle_exception');
2062         core_message_external::data_for_messagearea_messages($user2->id, $user3->id);
2063     }
2065     /**
2066      * Tests retrieving messages with messaging disabled.
2067      */
2068     public function test_messagearea_messages_messaging_disabled() {
2069         global $CFG;
2071         $this->resetAfterTest(true);
2073         // Create some skeleton data just so we can call the WS.
2074         $user1 = self::getDataGenerator()->create_user();
2075         $user2 = self::getDataGenerator()->create_user();
2077         // The person asking for the messages for another user.
2078         $this->setUser($user1);
2080         // Disable messaging.
2081         $CFG->messaging = 0;
2083         // Ensure an exception is thrown.
2084         $this->expectException('moodle_exception');
2085         core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
2086     }
2088     /**
2089      * Tests retrieving most recent message.
2090      */
2091     public function test_messagearea_get_most_recent_message() {
2092         $this->resetAfterTest(true);
2094         // Create some users.
2095         $user1 = self::getDataGenerator()->create_user();
2096         $user2 = self::getDataGenerator()->create_user();
2098         // The person doing the search.
2099         $this->setUser($user1);
2101         // Send some messages back and forth.
2102         $time = time();
2103         $this->send_message($user1, $user2, 'Yo!', 0, $time);
2104         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2105         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2106         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2108         // Get the most recent message.
2109         $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2111         // We need to execute the return values cleaning process to simulate the web service server.
2112         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
2113             $result);
2115         // Check the results are correct.
2116         $this->assertEquals($user2->id, $result['useridfrom']);
2117         $this->assertEquals($user1->id, $result['useridto']);
2118         $this->assertContains('Word.', $result['text']);
2119     }
2121     /**
2122      * Tests retrieving most recent message as another user.
2123      */
2124     public function test_messagearea_get_most_recent_message_as_other_user() {
2125         $this->resetAfterTest(true);
2127         // The person doing the search.
2128         $this->setAdminUser();
2130         // Create some users.
2131         $user1 = self::getDataGenerator()->create_user();
2132         $user2 = self::getDataGenerator()->create_user();
2134         // Send some messages back and forth.
2135         $time = time();
2136         $this->send_message($user1, $user2, 'Yo!', 0, $time);
2137         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2138         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2139         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2141         // Get the most recent message.
2142         $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2144         // We need to execute the return values cleaning process to simulate the web service server.
2145         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
2146             $result);
2148         // Check the results are correct.
2149         $this->assertEquals($user2->id, $result['useridfrom']);
2150         $this->assertEquals($user1->id, $result['useridto']);
2151         $this->assertContains('Word.', $result['text']);
2152     }
2154     /**
2155      * Tests retrieving most recent message as another user without the proper capabilities.
2156      */
2157     public function test_messagearea_get_most_recent_message_as_other_user_without_cap() {
2158         $this->resetAfterTest(true);
2160         // Create some users.
2161         $user1 = self::getDataGenerator()->create_user();
2162         $user2 = self::getDataGenerator()->create_user();
2163         $user3 = self::getDataGenerator()->create_user();
2165         // The person asking for the most recent message for another user.
2166         $this->setUser($user1);
2168         // Ensure an exception is thrown.
2169         $this->expectException('moodle_exception');
2170         core_message_external::data_for_messagearea_get_most_recent_message($user2->id, $user3->id);
2171     }
2173     /**
2174      * Tests retrieving most recent message with messaging disabled.
2175      */
2176     public function test_messagearea_get_most_recent_message_messaging_disabled() {
2177         global $CFG;
2179         $this->resetAfterTest(true);
2181         // Create some skeleton data just so we can call the WS.
2182         $user1 = self::getDataGenerator()->create_user();
2183         $user2 = self::getDataGenerator()->create_user();
2185         // The person asking for the most recent message.
2186         $this->setUser($user1);
2188         // Disable messaging.
2189         $CFG->messaging = 0;
2191         // Ensure an exception is thrown.
2192         $this->expectException('moodle_exception');
2193         core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2194     }
2196     /**
2197      * Tests retrieving a user's profile.
2198      */
2199     public function test_messagearea_get_profile() {
2200         $this->resetAfterTest(true);
2202         // Create some users.
2203         $user1 = self::getDataGenerator()->create_user();
2204         $user2 = self::getDataGenerator()->create_user();
2206         // The person asking for the profile information.
2207         $this->setUser($user1);
2209         // Get the profile.
2210         $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2212         // We need to execute the return values cleaning process to simulate the web service server.
2213         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(),
2214             $result);
2216         $this->assertEquals($user2->id, $result['userid']);
2217         $this->assertEmpty($result['email']);
2218         $this->assertEmpty($result['country']);
2219         $this->assertEmpty($result['city']);
2220         $this->assertEquals(fullname($user2), $result['fullname']);
2221         $this->assertFalse($result['isonline']);
2222         $this->assertFalse($result['isblocked']);
2223         $this->assertFalse($result['iscontact']);
2224     }
2226     /**
2227      * Tests retrieving a user's profile as another user.
2228      */
2229     public function test_messagearea_profile_as_other_user() {
2230         $this->resetAfterTest(true);
2232         // The person asking for the profile information.
2233         $this->setAdminUser();
2235         // Create some users.
2236         $user1 = self::getDataGenerator()->create_user();
2238         $user2 = new stdClass();
2239         $user2->country = 'AU';
2240         $user2->city = 'Perth';
2241         $user2 = self::getDataGenerator()->create_user($user2);
2243         // Get the profile.
2244         $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2246         // We need to execute the return values cleaning process to simulate the web service server.
2247         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(),
2248             $result);
2250         $this->assertEquals($user2->id, $result['userid']);
2251         $this->assertEquals($user2->email, $result['email']);
2252         $this->assertEquals(get_string($user2->country, 'countries'), $result['country']);
2253         $this->assertEquals($user2->city, $result['city']);
2254         $this->assertEquals(fullname($user2), $result['fullname']);
2255         $this->assertFalse($result['isonline']);
2256         $this->assertFalse($result['isblocked']);
2257         $this->assertFalse($result['iscontact']);
2258     }
2260     /**
2261      * Tests retrieving a user's profile as another user without the proper capabilities.
2262      */
2263     public function test_messagearea_profile_as_other_user_without_cap() {
2264         $this->resetAfterTest(true);
2266         // Create some users.
2267         $user1 = self::getDataGenerator()->create_user();
2268         $user2 = self::getDataGenerator()->create_user();
2269         $user3 = self::getDataGenerator()->create_user();
2271         // The person asking for the profile information for another user.
2272         $this->setUser($user1);
2274         // Ensure an exception is thrown.
2275         $this->expectException('moodle_exception');
2276         core_message_external::data_for_messagearea_get_profile($user2->id, $user3->id);
2277     }
2279     /**
2280      * Tests retrieving a user's profile with messaging disabled.
2281      */
2282     public function test_messagearea_profile_messaging_disabled() {
2283         global $CFG;
2285         $this->resetAfterTest(true);
2287         // Create some skeleton data just so we can call the WS.
2288         $user1 = self::getDataGenerator()->create_user();
2289         $user2 = self::getDataGenerator()->create_user();
2291         // The person asking for the profile information.
2292         $this->setUser($user1);
2294         // Disable messaging.
2295         $CFG->messaging = 0;
2297         // Ensure an exception is thrown.
2298         $this->expectException('moodle_exception');
2299         core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2300     }
2302     /**
2303      * Test marking all message as read with an invalid user.
2304      */
2305     public function test_mark_all_messages_as_read_invalid_user_exception() {
2306         $this->resetAfterTest(true);
2308         $this->expectException('moodle_exception');
2309         core_message_external::mark_all_messages_as_read(-2132131, 0);
2310     }
2312     /**
2313      * Test marking all message as read without proper access.
2314      */
2315     public function test_mark_all_messages_as_read_access_denied_exception() {
2316         $this->resetAfterTest(true);
2318         $sender = $this->getDataGenerator()->create_user();
2319         $user = $this->getDataGenerator()->create_user();
2321         $this->setUser($user);
2322         $this->expectException('moodle_exception');
2323         core_message_external::mark_all_messages_as_read($sender->id, 0);
2324     }
2326     /**
2327      * Test marking all message as read with missing from user.
2328      */
2329     public function test_mark_all_messages_as_read_missing_from_user_exception() {
2330         $this->resetAfterTest(true);
2332         $sender = $this->getDataGenerator()->create_user();
2334         $this->setUser($sender);
2335         $this->expectException('moodle_exception');
2336         core_message_external::mark_all_messages_as_read($sender->id, 99999);
2337     }
2339     /**
2340      * Test marking all message as read.
2341      */
2342     public function test_mark_all_messages_as_read() {
2343         global $DB;
2345         $this->resetAfterTest(true);
2347         $sender1 = $this->getDataGenerator()->create_user();
2348         $sender2 = $this->getDataGenerator()->create_user();
2349         $sender3 = $this->getDataGenerator()->create_user();
2350         $recipient = $this->getDataGenerator()->create_user();
2352         $this->setUser($recipient);
2354         $this->send_message($sender1, $recipient, 'Message');
2355         $this->send_message($sender1, $recipient, 'Message');
2356         $this->send_message($sender2, $recipient, 'Message');
2357         $this->send_message($sender2, $recipient, 'Message');
2358         $this->send_message($sender3, $recipient, 'Message');
2359         $this->send_message($sender3, $recipient, 'Message');
2361         core_message_external::mark_all_messages_as_read($recipient->id, $sender1->id);
2362         $readnotifications = $DB->get_records('message_read', array('useridto' => $recipient->id));
2363         $unreadnotifications = $DB->get_records('message', array('useridto' => $recipient->id));
2365         $this->assertCount(2, $readnotifications);
2366         $this->assertCount(4, $unreadnotifications);
2368         core_message_external::mark_all_messages_as_read($recipient->id, 0);
2369         $readnotifications = $DB->get_records('message_read', array('useridto' => $recipient->id));
2370         $unreadnotifications = $DB->get_records('message', array('useridto' => $recipient->id));
2372         $this->assertCount(6, $readnotifications);
2373         $this->assertCount(0, $unreadnotifications);
2374     }
2376     /**
2377      * Test getting unread conversation count.
2378      */
2379     public function test_get_unread_conversations_count() {
2380         $this->resetAfterTest(true);
2382         // Create some users.
2383         $user1 = self::getDataGenerator()->create_user();
2384         $user2 = self::getDataGenerator()->create_user();
2385         $user3 = self::getDataGenerator()->create_user();
2386         $user4 = self::getDataGenerator()->create_user();
2388         // The person wanting the conversation count.
2389         $this->setUser($user1);
2391         // Send some messages back and forth, have some different conversations with different users.
2392         $this->send_message($user1, $user2, 'Yo!');
2393         $this->send_message($user2, $user1, 'Sup mang?');
2394         $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
2395         $this->send_message($user2, $user1, 'Word.');
2397         $this->send_message($user1, $user3, 'Booyah');
2398         $this->send_message($user3, $user1, 'Whaaat?');
2399         $this->send_message($user1, $user3, 'Nothing.');
2400         $this->send_message($user3, $user1, 'Cool.');
2402         $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
2403         $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
2404         $this->send_message($user1, $user4, 'Dope.');
2406         // Get the unread conversation count.
2407         $result = core_message_external::get_unread_conversations_count($user1->id);
2409         // We need to execute the return values cleaning process to simulate the web service server.
2410         $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(),
2411             $result);
2413         $this->assertEquals(3, $result);
2414     }
2416     /**
2417      * Test getting unread conversation count as other user.
2418      */
2419     public function test_get_unread_conversations_count_as_other_user() {
2420         $this->resetAfterTest(true);
2422         // The person wanting the conversation count.
2423         $this->setAdminUser();
2425         // Create some users.
2426         $user1 = self::getDataGenerator()->create_user();
2427         $user2 = self::getDataGenerator()->create_user();
2428         $user3 = self::getDataGenerator()->create_user();
2429         $user4 = self::getDataGenerator()->create_user();
2431         // Send some messages back and forth, have some different conversations with different users.
2432         $this->send_message($user1, $user2, 'Yo!');
2433         $this->send_message($user2, $user1, 'Sup mang?');
2434         $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
2435         $this->send_message($user2, $user1, 'Word.');
2437         $this->send_message($user1, $user3, 'Booyah');
2438         $this->send_message($user3, $user1, 'Whaaat?');
2439         $this->send_message($user1, $user3, 'Nothing.');
2440         $this->send_message($user3, $user1, 'Cool.');
2442         $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
2443         $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
2444         $this->send_message($user1, $user4, 'Dope.');
2446         // Get the unread conversation count.
2447         $result = core_message_external::get_unread_conversations_count($user1->id);
2449         // We need to execute the return values cleaning process to simulate the web service server.
2450         $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(),
2451             $result);
2453         $this->assertEquals(3, $result);
2454     }
2456     /**
2457      * Test getting unread conversation count as other user without proper capability.
2458      */
2459     public function test_get_unread_conversations_count_as_other_user_without_cap() {
2460         $this->resetAfterTest(true);
2462         // Create some users.
2463         $user1 = self::getDataGenerator()->create_user();
2464         $user2 = self::getDataGenerator()->create_user();
2466         // The person wanting the conversation count.
2467         $this->setUser($user1);
2469         // Ensure an exception is thrown.
2470         $this->expectException('moodle_exception');
2471         core_message_external::get_unread_conversations_count($user2->id);
2472     }
2474     /**
2475      * Test deleting conversation.
2476      */
2477     public function test_delete_conversation() {
2478         global $DB;
2480         $this->resetAfterTest(true);
2482         // Create some users.
2483         $user1 = self::getDataGenerator()->create_user();
2484         $user2 = self::getDataGenerator()->create_user();
2486         // The person wanting to delete the conversation.
2487         $this->setUser($user1);
2489         // Send some messages back and forth.
2490         $time = time();
2491         $this->send_message($user1, $user2, 'Yo!', 0, $time);
2492         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2493         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2494         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2496         // Delete the conversation.
2497         core_message_external::delete_conversation($user1->id, $user2->id);
2499         $messages = $DB->get_records('message', array(), 'timecreated ASC');
2500         $this->assertCount(4, $messages);
2502         $message1 = array_shift($messages);
2503         $message2 = array_shift($messages);
2504         $message3 = array_shift($messages);
2505         $message4 = array_shift($messages);
2507         $this->assertNotEmpty($message1->timeuserfromdeleted);
2508         $this->assertEmpty($message1->timeusertodeleted);
2510         $this->assertEmpty($message2->timeuserfromdeleted);
2511         $this->assertNotEmpty($message2->timeusertodeleted);
2513         $this->assertNotEmpty($message3->timeuserfromdeleted);
2514         $this->assertEmpty($message3->timeusertodeleted);
2516         $this->assertEmpty($message4->timeuserfromdeleted);
2517         $this->assertNotEmpty($message4->timeusertodeleted);
2518     }
2520     /**
2521      * Test deleting conversation as other user.
2522      */
2523     public function test_delete_conversation_as_other_user() {
2524         global $DB;
2526         $this->resetAfterTest(true);
2528         $this->setAdminUser();
2530         // Create some users.
2531         $user1 = self::getDataGenerator()->create_user();
2532         $user2 = self::getDataGenerator()->create_user();
2534         // Send some messages back and forth.
2535         $time = time();
2536         $this->send_message($user1, $user2, 'Yo!', 0, $time);
2537         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2538         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2539         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2541         // Delete the conversation.
2542         core_message_external::delete_conversation($user1->id, $user2->id);
2544         $messages = $DB->get_records('message', array(), 'timecreated ASC');
2545         $this->assertCount(4, $messages);
2547         $message1 = array_shift($messages);
2548         $message2 = array_shift($messages);
2549         $message3 = array_shift($messages);
2550         $message4 = array_shift($messages);
2552         $this->assertNotEmpty($message1->timeuserfromdeleted);
2553         $this->assertEmpty($message1->timeusertodeleted);
2555         $this->assertEmpty($message2->timeuserfromdeleted);
2556         $this->assertNotEmpty($message2->timeusertodeleted);
2558         $this->assertNotEmpty($message3->timeuserfromdeleted);
2559         $this->assertEmpty($message3->timeusertodeleted);
2561         $this->assertEmpty($message4->timeuserfromdeleted);
2562         $this->assertNotEmpty($message4->timeusertodeleted);
2563     }
2565     /**
2566      * Test deleting conversation as other user without proper capability.
2567      */
2568     public function test_delete_conversation_as_other_user_without_cap() {
2569         $this->resetAfterTest(true);
2571         // Create some users.
2572         $user1 = self::getDataGenerator()->create_user();
2573         $user2 = self::getDataGenerator()->create_user();
2574         $user3 = self::getDataGenerator()->create_user();
2576         // The person wanting to delete the conversation.
2577         $this->setUser($user3);
2579         // Ensure an exception is thrown.
2580         $this->expectException('moodle_exception');
2581         core_message_external::delete_conversation($user1->id, $user2->id);
2582     }
2584     /**
2585      * Test deleting conversation with messaging disabled.
2586      */
2587     public function test_delete_conversation_messaging_disabled() {
2588         global $CFG;
2590         $this->resetAfterTest(true);
2592         // Create some users.
2593         $user1 = self::getDataGenerator()->create_user();
2594         $user2 = self::getDataGenerator()->create_user();
2596         // The person wanting to delete the conversation.
2597         $this->setUser($user1);
2599         // Disable messaging.
2600         $CFG->messaging = 0;
2602         // Ensure an exception is thrown.
2603         $this->expectException('moodle_exception');
2604         core_message_external::delete_conversation($user1->id, $user2->id);
2605     }
2607     /**
2608      * Test get message processor.
2609      */
2610     public function test_get_message_processor() {
2611         $this->resetAfterTest(true);
2613         // Create a user.
2614         $user1 = self::getDataGenerator()->create_user();
2616         // Set you as the user.
2617         $this->setUser($user1);
2619         // Get the message processors.
2620         $result = core_message_external::get_message_processor($user1->id, 'popup');
2622         // We need to execute the return values cleaning process to simulate the web service server.
2623         $result = external_api::clean_returnvalue(core_message_external::get_message_processor_returns(), $result);
2625         $this->assertNotEmpty($result['systemconfigured']);
2626         $this->assertNotEmpty($result['userconfigured']);
2627     }