c77e6b7e23ff96ed7e2cb75c472fbee23a158747
[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);
164     }
166     /**
167      * Test delete_contacts.
168      */
169     public function test_delete_contacts() {
170         $this->resetAfterTest(true);
172         $user1 = self::getDataGenerator()->create_user();
173         $user2 = self::getDataGenerator()->create_user();
174         $user3 = self::getDataGenerator()->create_user();
175         $user4 = self::getDataGenerator()->create_user();
176         $user5 = self::getDataGenerator()->create_user();
177         $user6 = self::getDataGenerator()->create_user();
178         $this->setUser($user1);
179         $this->assertEquals(array(), core_message_external::create_contacts(
180             array($user3->id, $user4->id, $user5->id, $user6->id)));
182         // Removing a non-contact.
183         $return = core_message_external::delete_contacts(array($user2->id));
184         $this->assertNull($return);
186         // Removing one contact.
187         $return = core_message_external::delete_contacts(array($user3->id));
188         $this->assertNull($return);
190         // Removing multiple contacts.
191         $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
192         $this->assertNull($return);
194         // Removing contact from unexisting user.
195         $return = core_message_external::delete_contacts(array(99999));
196         $this->assertNull($return);
198         // Removing mixed valid and invalid data.
199         $return = core_message_external::delete_contacts(array($user6->id, 99999));
200         $this->assertNull($return);
201     }
203     /**
204      * Test block_contacts.
205      */
206     public function test_block_contacts() {
207         $this->resetAfterTest(true);
209         $user1 = self::getDataGenerator()->create_user();
210         $user2 = self::getDataGenerator()->create_user();
211         $user3 = self::getDataGenerator()->create_user();
212         $user4 = self::getDataGenerator()->create_user();
213         $user5 = self::getDataGenerator()->create_user();
214         $this->setUser($user1);
215         $this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
217         // Blocking a contact.
218         $return = core_message_external::block_contacts(array($user2->id));
219         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
220         $this->assertEquals(array(), $return);
222         // Blocking a contact who is already a contact.
223         $return = core_message_external::block_contacts(array($user2->id));
224         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
225         $this->assertEquals(array(), $return);
227         // Blocking multiple contacts.
228         $return = core_message_external::block_contacts(array($user3->id, $user4->id));
229         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
230         $this->assertEquals(array(), $return);
232         // Blocking a non-existing user.
233         $return = core_message_external::block_contacts(array(99999));
234         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
235         $this->assertCount(1, $return);
236         $return = array_pop($return);
237         $this->assertEquals($return['warningcode'], 'contactnotblocked');
238         $this->assertEquals($return['itemid'], 99999);
240         // Blocking contacts with valid and invalid parameters.
241         $return = core_message_external::block_contacts(array($user5->id, 99999));
242         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
243         $this->assertCount(1, $return);
244         $return = array_pop($return);
245         $this->assertEquals($return['warningcode'], 'contactnotblocked');
246         $this->assertEquals($return['itemid'], 99999);
247     }
249     /**
250      * Test unblock_contacts.
251      */
252     public function test_unblock_contacts() {
253         $this->resetAfterTest(true);
255         $user1 = self::getDataGenerator()->create_user();
256         $user2 = self::getDataGenerator()->create_user();
257         $user3 = self::getDataGenerator()->create_user();
258         $user4 = self::getDataGenerator()->create_user();
259         $user5 = self::getDataGenerator()->create_user();
260         $user6 = self::getDataGenerator()->create_user();
261         $this->setUser($user1);
262         $this->assertEquals(array(), core_message_external::create_contacts(
263             array($user3->id, $user4->id, $user5->id, $user6->id)));
265         // Removing a non-contact.
266         $return = core_message_external::unblock_contacts(array($user2->id));
267         $this->assertNull($return);
269         // Removing one contact.
270         $return = core_message_external::unblock_contacts(array($user3->id));
271         $this->assertNull($return);
273         // Removing multiple contacts.
274         $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
275         $this->assertNull($return);
277         // Removing contact from unexisting user.
278         $return = core_message_external::unblock_contacts(array(99999));
279         $this->assertNull($return);
281         // Removing mixed valid and invalid data.
282         $return = core_message_external::unblock_contacts(array($user6->id, 99999));
283         $this->assertNull($return);
285     }
287     /**
288      * Test get_contacts.
289      */
290     public function test_get_contacts() {
291         $this->resetAfterTest(true);
293         $user1 = self::getDataGenerator()->create_user();
294         $user_stranger = self::getDataGenerator()->create_user();
295         $user_offline1 = self::getDataGenerator()->create_user();
296         $user_offline2 = self::getDataGenerator()->create_user();
297         $user_offline3 = self::getDataGenerator()->create_user();
298         $user_online = new stdClass();
299         $user_online->lastaccess = time();
300         $user_online = self::getDataGenerator()->create_user($user_online);
301         $user_blocked = self::getDataGenerator()->create_user();
302         $noreplyuser = core_user::get_user(core_user::NOREPLY_USER);
304         // Login as user1.
305         $this->setUser($user1);
306         $this->assertEquals(array(), core_message_external::create_contacts(
307             array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
309         // User_stranger sends a couple of messages to user1.
310         $this->send_message($user_stranger, $user1, 'Hello there!');
311         $this->send_message($user_stranger, $user1, 'How you goin?');
312         $this->send_message($user_stranger, $user1, 'Cya!');
313         $this->send_message($noreplyuser, $user1, 'I am not a real user');
315         // User_blocked sends a message to user1.
316         $this->send_message($user_blocked, $user1, 'Here, have some spam.');
318         // Retrieve the contacts of the user.
319         $this->setUser($user1);
320         $contacts = core_message_external::get_contacts();
321         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
322         $this->assertCount(3, $contacts['offline']);
323         $this->assertCount(1, $contacts['online']);
324         $this->assertCount(3, $contacts['strangers']);
325         core_message_external::block_contacts(array($user_blocked->id));
326         $contacts = core_message_external::get_contacts();
327         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
328         $this->assertCount(3, $contacts['offline']);
329         $this->assertCount(1, $contacts['online']);
330         $this->assertCount(2, $contacts['strangers']);
332         // Checking some of the fields returned.
333         $stranger = array_pop($contacts['strangers']);
335         $this->assertEquals(core_user::NOREPLY_USER, $stranger['id']);
336         $this->assertEquals(1, $stranger['unread']);
338         // Check that deleted users are not returned.
339         delete_user($user_offline1);
340         delete_user($user_stranger);
341         delete_user($user_online);
342         $contacts = core_message_external::get_contacts();
343         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
344         $this->assertCount(2, $contacts['offline']);
345         $this->assertCount(0, $contacts['online']);
346         $this->assertCount(1, $contacts['strangers']);
347     }
349     /**
350      * Test search_contacts.
351      * @expectedException moodle_exception
352      */
353     public function test_search_contacts() {
354         global $DB;
355         $this->resetAfterTest(true);
357         $course1 = $this->getDataGenerator()->create_course();
358         $course2 = $this->getDataGenerator()->create_course();
360         $user1 = new stdClass();
361         $user1->firstname = 'X';
362         $user1->lastname = 'X';
363         $user1 = $this->getDataGenerator()->create_user($user1);
364         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
365         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
367         $user2 = new stdClass();
368         $user2->firstname = 'Eric';
369         $user2->lastname = 'Cartman';
370         $user2 = self::getDataGenerator()->create_user($user2);
371         $user3 = new stdClass();
372         $user3->firstname = 'Stan';
373         $user3->lastname = 'Marsh';
374         $user3 = self::getDataGenerator()->create_user($user3);
375         self::getDataGenerator()->enrol_user($user3->id, $course1->id);
376         $user4 = new stdClass();
377         $user4->firstname = 'Kyle';
378         $user4->lastname = 'Broflovski';
379         $user4 = self::getDataGenerator()->create_user($user4);
380         $user5 = new stdClass();
381         $user5->firstname = 'Kenny';
382         $user5->lastname = 'McCormick';
383         $user5 = self::getDataGenerator()->create_user($user5);
384         self::getDataGenerator()->enrol_user($user5->id, $course2->id);
386         $this->setUser($user1);
388         $results = core_message_external::search_contacts('r');
389         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
390         $this->assertCount(5, $results); // Users 2 through 5 + admin
392         $results = core_message_external::search_contacts('r', true);
393         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
394         $this->assertCount(2, $results);
396         $results = core_message_external::search_contacts('Kyle', false);
397         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
398         $this->assertCount(1, $results);
399         $result = reset($results);
400         $this->assertEquals($user4->id, $result['id']);
402         $results = core_message_external::search_contacts('y', false);
403         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
404         $this->assertCount(2, $results);
406         $results = core_message_external::search_contacts('y', true);
407         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
408         $this->assertCount(1, $results);
409         $result = reset($results);
410         $this->assertEquals($user5->id, $result['id']);
412         // Empty query, will throw an exception.
413         $results = core_message_external::search_contacts('');
414     }
416     /**
417      * Test get_messages.
418      */
419     public function test_get_messages() {
420         global $CFG, $DB;
421         $this->resetAfterTest(true);
423         $this->preventResetByRollback();
424         // This mark the messages as read!.
425         $sink = $this->redirectMessages();
427         $user1 = self::getDataGenerator()->create_user();
428         $user2 = self::getDataGenerator()->create_user();
429         $user3 = self::getDataGenerator()->create_user();
431         $course = self::getDataGenerator()->create_course();
433         // Send a message from one user to another.
434         message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
435         message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE);
436         message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
437         message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
438         message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE);
440         $this->setUser($user1);
441         // Get read conversations from user1 to user2.
442         $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
443         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
444         $this->assertCount(1, $messages['messages']);
446         // Delete the message.
447         $message = array_shift($messages['messages']);
448         $messagetobedeleted = $DB->get_record('message_read', array('id' => $message['id']));
449         message_delete_message($messagetobedeleted, $user1->id);
451         $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
452         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
453         $this->assertCount(0, $messages['messages']);
455         // Get unread conversations from user1 to user2.
456         $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0);
457         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
458         $this->assertCount(0, $messages['messages']);
460         // Get read messages send from user1.
461         $messages = core_message_external::get_messages(0, $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         $this->setUser($user2);
466         // Get read conversations from any user to user2.
467         $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0);
468         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
469         $this->assertCount(2, $messages['messages']);
471         // Conversations from user3 to user2.
472         $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
473         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
474         $this->assertCount(1, $messages['messages']);
476         // Delete the message.
477         $message = array_shift($messages['messages']);
478         $messagetobedeleted = $DB->get_record('message_read', array('id' => $message['id']));
479         message_delete_message($messagetobedeleted, $user2->id);
481         $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
482         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
483         $this->assertCount(0, $messages['messages']);
485         $this->setUser($user3);
486         // Get read notifications received by user3.
487         $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0);
488         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
489         $this->assertCount(0, $messages['messages']);
491         // Now, create some notifications...
492         // We are creating fake notifications but based on real ones.
494         // This one omits notification = 1.
495         $eventdata = new \core\message\message();
496         $eventdata->courseid          = $course->id;
497         $eventdata->modulename        = 'moodle';
498         $eventdata->component         = 'enrol_paypal';
499         $eventdata->name              = 'paypal_enrolment';
500         $eventdata->userfrom          = get_admin();
501         $eventdata->userto            = $user1;
502         $eventdata->subject           = "Moodle: PayPal payment";
503         $eventdata->fullmessage       = "Your PayPal payment is pending.";
504         $eventdata->fullmessageformat = FORMAT_PLAIN;
505         $eventdata->fullmessagehtml   = '';
506         $eventdata->smallmessage      = '';
507         message_send($eventdata);
509         $message = new \core\message\message();
510         $message->courseid          = $course->id;
511         $message->notification      = 1;
512         $message->component         = 'enrol_manual';
513         $message->name              = 'expiry_notification';
514         $message->userfrom          = $user2;
515         $message->userto            = $user1;
516         $message->subject           = 'Enrolment expired';
517         $message->fullmessage       = 'Enrolment expired blah blah blah';
518         $message->fullmessageformat = FORMAT_MARKDOWN;
519         $message->fullmessagehtml   = markdown_to_html($message->fullmessage);
520         $message->smallmessage      = $message->subject;
521         $message->contexturlname    = $course->fullname;
522         $message->contexturl        = (string)new moodle_url('/course/view.php', array('id' => $course->id));
523         message_send($message);
525         $userfrom = core_user::get_noreply_user();
526         $userfrom->maildisplay = true;
527         $eventdata = new \core\message\message();
528         $eventdata->courseid          = $course->id;
529         $eventdata->component         = 'moodle';
530         $eventdata->name              = 'badgecreatornotice';
531         $eventdata->userfrom          = $userfrom;
532         $eventdata->userto            = $user1;
533         $eventdata->notification      = 1;
534         $eventdata->subject           = 'New badge';
535         $eventdata->fullmessage       = format_text_email($eventdata->subject, FORMAT_HTML);
536         $eventdata->fullmessageformat = FORMAT_PLAIN;
537         $eventdata->fullmessagehtml   = $eventdata->subject;
538         $eventdata->smallmessage      = $eventdata->subject;
539         message_send($eventdata);
541         $eventdata = new \core\message\message();
542         $eventdata->courseid         = $course->id;
543         $eventdata->name             = 'submission';
544         $eventdata->component        = 'mod_feedback';
545         $eventdata->userfrom         = $user1;
546         $eventdata->userto           = $user2;
547         $eventdata->subject          = 'Feedback submitted';
548         $eventdata->fullmessage      = 'Feedback submitted from an user';
549         $eventdata->fullmessageformat = FORMAT_PLAIN;
550         $eventdata->fullmessagehtml  = '<strong>Feedback submitted</strong>';
551         $eventdata->smallmessage     = '';
552         message_send($eventdata);
554         $this->setUser($user1);
555         // Get read notifications from any user to user1.
556         $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0);
557         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
558         $this->assertCount(3, $messages['messages']);
560         // Get one read notifications from any user to user1.
561         $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1);
562         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
563         $this->assertCount(1, $messages['messages']);
565         // Get unread notifications from any user to user1.
566         $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0);
567         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
568         $this->assertCount(0, $messages['messages']);
570         // Get read both type of messages from any user to user1.
571         $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0);
572         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
573         $this->assertCount(4, $messages['messages']);
575         // Get read notifications from no-reply-user to user1.
576         $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0);
577         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
578         $this->assertCount(1, $messages['messages']);
580         // Get notifications send by user1 to any user.
581         $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0);
582         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
583         $this->assertCount(1, $messages['messages']);
585         // Test warnings.
586         $CFG->messaging = 0;
588         $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0);
589         $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
590         $this->assertCount(1, $messages['warnings']);
592         // Test exceptions.
594         // Messaging disabled.
595         try {
596             $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
597             $this->fail('Exception expected due messaging disabled.');
598         } catch (moodle_exception $e) {
599             $this->assertEquals('disabled', $e->errorcode);
600         }
602         $CFG->messaging = 1;
604         // Invalid users.
605         try {
606             $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0);
607             $this->fail('Exception expected due invalid users.');
608         } catch (moodle_exception $e) {
609             $this->assertEquals('accessdenied', $e->errorcode);
610         }
612         // Invalid user ids.
613         try {
614             $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0);
615             $this->fail('Exception expected due invalid users.');
616         } catch (moodle_exception $e) {
617             $this->assertEquals('invaliduser', $e->errorcode);
618         }
620         // Invalid users (permissions).
621         $this->setUser($user2);
622         try {
623             $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
624             $this->fail('Exception expected due invalid user.');
625         } catch (moodle_exception $e) {
626             $this->assertEquals('accessdenied', $e->errorcode);
627         }
629     }
631     /**
632      * Test get_blocked_users.
633      */
634     public function test_get_blocked_users() {
635         $this->resetAfterTest(true);
637         $user1 = self::getDataGenerator()->create_user();
638         $userstranger = self::getDataGenerator()->create_user();
639         $useroffline1 = self::getDataGenerator()->create_user();
640         $useroffline2 = self::getDataGenerator()->create_user();
641         $userblocked = self::getDataGenerator()->create_user();
643         // Login as user1.
644         $this->setUser($user1);
645         $this->assertEquals(array(), core_message_external::create_contacts(
646             array($useroffline1->id, $useroffline2->id)));
648         // The userstranger sends a couple of messages to user1.
649         $this->send_message($userstranger, $user1, 'Hello there!');
650         $this->send_message($userstranger, $user1, 'How you goin?');
652         // The userblocked sends a message to user1.
653         // Note that this user is not blocked at this point.
654         $this->send_message($userblocked, $user1, 'Here, have some spam.');
656         // Retrieve the list of blocked users.
657         $this->setUser($user1);
658         $blockedusers = core_message_external::get_blocked_users($user1->id);
659         $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
660         $this->assertCount(0, $blockedusers['users']);
662         // Block the $userblocked and retrieve again the list.
663         core_message_external::block_contacts(array($userblocked->id));
664         $blockedusers = core_message_external::get_blocked_users($user1->id);
665         $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
666         $this->assertCount(1, $blockedusers['users']);
668         // Remove the $userblocked and check that the list now is empty.
669         delete_user($userblocked);
670         $blockedusers = core_message_external::get_blocked_users($user1->id);
671         $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
672         $this->assertCount(0, $blockedusers['users']);
674     }
676     /**
677      * Test mark_message_read.
678      */
679     public function test_mark_message_read() {
680         $this->resetAfterTest(true);
682         $user1 = self::getDataGenerator()->create_user();
683         $user2 = self::getDataGenerator()->create_user();
684         $user3 = self::getDataGenerator()->create_user();
686         // Login as user1.
687         $this->setUser($user1);
688         $this->assertEquals(array(), core_message_external::create_contacts(
689             array($user2->id, $user3->id)));
691         // The user2 sends a couple of messages to user1.
692         $this->send_message($user2, $user1, 'Hello there!');
693         $this->send_message($user2, $user1, 'How you goin?');
694         $this->send_message($user3, $user1, 'How you goin?');
695         $this->send_message($user3, $user2, 'How you goin?');
697         // Retrieve all messages sent by user2 (they are currently unread).
698         $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
700         $messageids = array();
701         foreach ($lastmessages as $m) {
702             $messageid = core_message_external::mark_message_read($m->id, time());
703             $messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid);
704         }
706         // Retrieve all messages sent (they are currently read).
707         $lastmessages = message_get_messages($user1->id, $user2->id, 0, true);
708         $this->assertCount(2, $lastmessages);
709         $this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages);
710         $this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages);
712         // Retrieve all messages sent by any user (that are currently unread).
713         $lastmessages = message_get_messages($user1->id, 0, 0, false);
714         $this->assertCount(1, $lastmessages);
716         // Invalid message ids.
717         try {
718             $messageid = core_message_external::mark_message_read($messageids[0]['messageid'] * 2, time());
719             $this->fail('Exception expected due invalid messageid.');
720         } catch (dml_missing_record_exception $e) {
721             $this->assertEquals('invalidrecord', $e->errorcode);
722         }
724         // A message to a different user.
725         $lastmessages = message_get_messages($user2->id, $user3->id, 0, false);
726         $messageid = array_pop($lastmessages)->id;
727         try {
728             $messageid = core_message_external::mark_message_read($messageid, time());
729             $this->fail('Exception expected due invalid messageid.');
730         } catch (invalid_parameter_exception $e) {
731             $this->assertEquals('invalidparameter', $e->errorcode);
732         }
734     }
736     /**
737      * Test delete_message.
738      */
739     public function test_delete_message() {
740         global $DB;
741         $this->resetAfterTest(true);
743         $user1 = self::getDataGenerator()->create_user();
744         $user2 = self::getDataGenerator()->create_user();
745         $user3 = self::getDataGenerator()->create_user();
746         $user4 = self::getDataGenerator()->create_user();
748         // Login as user1.
749         $this->setUser($user1);
750         $this->assertEquals(array(), core_message_external::create_contacts(array($user2->id, $user3->id)));
752         // User user1 does not interchange messages with user3.
753         $m1to2 = message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
754         $m2to3 = message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
755         $m3to2 = message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
756         $m3to4 = message_post_message($user3, $user4, 'some random text 4', FORMAT_MOODLE);
758         // Retrieve all messages sent by user2 (they are currently unread).
759         $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
761         // Delete a message not read, as a user from.
762         $result = core_message_external::delete_message($m1to2, $user1->id, false);
763         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
764         $this->assertTrue($result['status']);
765         $this->assertCount(0, $result['warnings']);
766         $deletedmessage = $DB->get_record('message', array('id' => $m1to2));
767         $this->assertNotEquals(0, $deletedmessage->timeuserfromdeleted);
769         // Try to delete the same message again.
770         $result = core_message_external::delete_message($m1to2, $user1->id, false);
771         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
772         $this->assertFalse($result['status']);
774         // Try to delete a message that does not belong to me.
775         try {
776             $messageid = core_message_external::delete_message($m2to3, $user3->id, false);
777             $this->fail('Exception expected due invalid messageid.');
778         } catch (moodle_exception $e) {
779             $this->assertEquals('You do not have permission to delete this message', $e->errorcode);
780         }
782         $this->setUser($user3);
783         // Delete a message not read, as a user to.
784         $result = core_message_external::delete_message($m2to3, $user3->id, false);
785         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
786         $this->assertTrue($result['status']);
787         $this->assertCount(0, $result['warnings']);
788         $deletedmessage = $DB->get_record('message', array('id' => $m2to3));
789         $this->assertNotEquals(0, $deletedmessage->timeusertodeleted);
791         // Delete a message read.
792         $message = $DB->get_record('message', array('id' => $m3to2));
793         $messageid = message_mark_message_read($message, time());
794         $result = core_message_external::delete_message($messageid, $user3->id);
795         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
796         $this->assertTrue($result['status']);
797         $this->assertCount(0, $result['warnings']);
798         $deletedmessage = $DB->get_record('message_read', array('id' => $messageid));
799         $this->assertNotEquals(0, $deletedmessage->timeuserfromdeleted);
801         // Invalid message ids.
802         try {
803             $result = core_message_external::delete_message(-1, $user1->id);
804             $this->fail('Exception expected due invalid messageid.');
805         } catch (dml_missing_record_exception $e) {
806             $this->assertEquals('invalidrecord', $e->errorcode);
807         }
809         // Invalid user.
810         try {
811             $result = core_message_external::delete_message($m1to2, -1, false);
812             $this->fail('Exception expected due invalid user.');
813         } catch (moodle_exception $e) {
814             $this->assertEquals('invaliduser', $e->errorcode);
815         }
817         // Not active user.
818         delete_user($user2);
819         try {
820             $result = core_message_external::delete_message($m1to2, $user2->id, false);
821             $this->fail('Exception expected due invalid user.');
822         } catch (moodle_exception $e) {
823             $this->assertEquals('userdeleted', $e->errorcode);
824         }
826         // Now, as an admin, try to delete any message.
827         $this->setAdminUser();
828         $result = core_message_external::delete_message($m3to4, $user4->id, false);
829         $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
830         $this->assertTrue($result['status']);
831         $this->assertCount(0, $result['warnings']);
832         $deletedmessage = $DB->get_record('message', array('id' => $m3to4));
833         $this->assertNotEquals(0, $deletedmessage->timeusertodeleted);
835     }
837     public function test_mark_all_notifications_as_read_invalid_user_exception() {
838         $this->resetAfterTest(true);
840         $this->expectException('moodle_exception');
841         core_message_external::mark_all_notifications_as_read(-2132131, 0);
842     }
844     public function test_mark_all_notifications_as_read_access_denied_exception() {
845         $this->resetAfterTest(true);
847         $sender = $this->getDataGenerator()->create_user();
848         $user = $this->getDataGenerator()->create_user();
850         $this->setUser($user);
851         $this->expectException('moodle_exception');
852         core_message_external::mark_all_notifications_as_read($sender->id, 0);
853     }
855     public function test_mark_all_notifications_as_read_missing_from_user_exception() {
856         $this->resetAfterTest(true);
858         $sender = $this->getDataGenerator()->create_user();
860         $this->setUser($sender);
861         $this->expectException('moodle_exception');
862         core_message_external::mark_all_notifications_as_read($sender->id, 99999);
863     }
865     public function test_mark_all_notifications_as_read() {
866         global $DB;
868         $this->resetAfterTest(true);
870         $sender1 = $this->getDataGenerator()->create_user();
871         $sender2 = $this->getDataGenerator()->create_user();
872         $sender3 = $this->getDataGenerator()->create_user();
873         $recipient = $this->getDataGenerator()->create_user();
875         $this->setUser($recipient);
877         $this->send_message($sender1, $recipient, 'Notification', 1);
878         $this->send_message($sender1, $recipient, 'Notification', 1);
879         $this->send_message($sender2, $recipient, 'Notification', 1);
880         $this->send_message($sender2, $recipient, 'Notification', 1);
881         $this->send_message($sender3, $recipient, 'Notification', 1);
882         $this->send_message($sender3, $recipient, 'Notification', 1);
884         core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id);
885         $readnotifications = $DB->get_recordset('message_read', ['useridto' => $recipient->id]);
886         $unreadnotifications = $DB->get_recordset('message', ['useridto' => $recipient->id]);
888         $this->assertCount(2, $readnotifications);
889         $this->assertCount(4, $unreadnotifications);
891         core_message_external::mark_all_notifications_as_read($recipient->id, 0);
892         $readnotifications = $DB->get_recordset('message_read', ['useridto' => $recipient->id]);
893         $unreadnotifications = $DB->get_recordset('message', ['useridto' => $recipient->id]);
895         $this->assertCount(6, $readnotifications);
896         $this->assertCount(0, $unreadnotifications);
897     }
899     /**
900      * Test get_user_notification_preferences
901      */
902     public function test_get_user_notification_preferences() {
903         $this->resetAfterTest(true);
905         $user = self::getDataGenerator()->create_user();
906         $this->setUser($user);
908         // Set a couple of preferences to test.
909         set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
910         set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
912         $prefs = core_message_external::get_user_notification_preferences();
913         $prefs = external_api::clean_returnvalue(core_message_external::get_user_notification_preferences_returns(), $prefs);
914         // Check processors.
915         $this->assertCount(2, $prefs['preferences']['processors']);
916         $this->assertEquals($user->id, $prefs['preferences']['userid']);
918         // Check components.
919         $this->assertCount(8, $prefs['preferences']['components']);
921         // Check some preferences that we previously set.
922         $found = 0;
923         foreach ($prefs['preferences']['components'] as $component) {
924             foreach ($component['notifications'] as $prefdata) {
925                 if ($prefdata['preferencekey'] != 'message_provider_mod_assign_assign_notification') {
926                     continue;
927                 }
928                 foreach ($prefdata['processors'] as $processor) {
929                     if ($processor['name'] == 'popup') {
930                         $this->assertTrue($processor['loggedin']['checked']);
931                         $found++;
932                     } else if ($processor['name'] == 'email') {
933                         $this->assertTrue($processor['loggedoff']['checked']);
934                         $found++;
935                     }
936                 }
937             }
938         }
939         $this->assertEquals(2, $found);
940     }
942     /**
943      * Test get_user_notification_preferences permissions
944      */
945     public function test_get_user_notification_preferences_permissions() {
946         $this->resetAfterTest(true);
948         $user = self::getDataGenerator()->create_user();
949         $otheruser = self::getDataGenerator()->create_user();
950         $this->setUser($user);
952         $this->expectException('moodle_exception');
953         $prefs = core_message_external::get_user_notification_preferences($otheruser->id);
954     }
956     /**
957      * Tests searching users in a course.
958      */
959     public function test_messagearea_search_users_in_course() {
960         $this->resetAfterTest(true);
962         // Create some users.
963         $user1 = new stdClass();
964         $user1->firstname = 'User';
965         $user1->lastname = 'One';
966         $user1 = self::getDataGenerator()->create_user($user1);
968         // The person doing the search.
969         $this->setUser($user1);
971         // Set the second user's status to online by setting their last access to now.
972         $user2 = new stdClass();
973         $user2->firstname = 'User';
974         $user2->lastname = 'Two';
975         $user2->lastaccess = time();
976         $user2 = self::getDataGenerator()->create_user($user2);
978         // Block the second user.
979         message_block_contact($user2->id, $user1->id);
981         $user3 = new stdClass();
982         $user3->firstname = 'User';
983         $user3->lastname = 'Three';
984         $user3 = self::getDataGenerator()->create_user($user3);
986         // Create a course.
987         $course1 = new stdClass();
988         $course1->fullname = 'Course';
989         $course1->shortname = 'One';
990         $course1 = $this->getDataGenerator()->create_course();
992         // Enrol the user we are doing the search for and one user in the course.
993         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
994         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
996         // Perform a search.
997         $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User');
999         // We need to execute the return values cleaning process to simulate the web service.
1000         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(),
1001             $result);
1003         // Check that we only retrieved a user that was enrolled, and that the user performing the search was not returned.
1004         $users = $result['contacts'];
1005         $this->assertCount(1, $users);
1007         $user = $users[0];
1008         $this->assertEquals($user2->id, $user['userid']);
1009         $this->assertEquals(fullname($user2), $user['fullname']);
1010         $this->assertFalse($user['ismessaging']);
1011         $this->assertFalse($user['sentfromcurrentuser']);
1012         $this->assertNull($user['lastmessage']);
1013         $this->assertNull($user['messageid']);
1014         $this->assertTrue($user['isonline']);
1015         $this->assertFalse($user['isread']);
1016         $this->assertTrue($user['isblocked']);
1017         $this->assertNull($user['unreadcount']);
1018     }
1020     /**
1021      * Tests searching users in course as another user.
1022      */
1023     public function test_messagearea_search_users_in_course_as_other_user() {
1024         $this->resetAfterTest(true);
1026         // The person doing the search for another user.
1027         $this->setAdminUser();
1029         // Create some users.
1030         $user1 = new stdClass();
1031         $user1->firstname = 'User';
1032         $user1->lastname = 'One';
1033         $user1 = self::getDataGenerator()->create_user($user1);
1035         $user2 = new stdClass();
1036         $user2->firstname = 'User';
1037         $user2->lastname = 'Two';
1038         $user2 = self::getDataGenerator()->create_user($user2);
1040         $user3 = new stdClass();
1041         $user3->firstname = 'User';
1042         $user3->lastname = 'Three';
1043         $user3 = self::getDataGenerator()->create_user($user3);
1045         // Create a course.
1046         $course1 = new stdClass();
1047         $course1->fullname = 'Course';
1048         $course1->shortname = 'One';
1049         $course1 = $this->getDataGenerator()->create_course();
1051         // Enrol the user we are doing the search for and one user in the course.
1052         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1053         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1055         // Perform a search.
1056         $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User');
1058         // We need to execute the return values cleaning process to simulate the web service server.
1059         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(),
1060             $result);
1062         // Check that we got the user enrolled, and that the user we are performing the search on behalf of was not returned.
1063         $users = $result['contacts'];
1064         $this->assertCount(1, $users);
1066         $user = $users[0];
1067         $this->assertEquals($user2->id, $user['userid']);
1068         $this->assertEquals(fullname($user2), $user['fullname']);
1069         $this->assertFalse($user['ismessaging']);
1070         $this->assertFalse($user['sentfromcurrentuser']);
1071         $this->assertNull($user['lastmessage']);
1072         $this->assertNull($user['messageid']);
1073         $this->assertFalse($user['isonline']);
1074         $this->assertFalse($user['isread']);
1075         $this->assertFalse($user['isblocked']);
1076         $this->assertNull($user['unreadcount']);
1077     }
1079     /**
1080      * Tests searching users in course as another user without the proper capabilities.
1081      */
1082     public function test_messagearea_search_users_in_course_as_other_user_without_cap() {
1083         $this->resetAfterTest(true);
1085         // Create some users.
1086         $user1 = self::getDataGenerator()->create_user();
1087         $user2 = self::getDataGenerator()->create_user();
1089         // The person doing the search for another user.
1090         $this->setUser($user1);
1092         // Create a course.
1093         $course = $this->getDataGenerator()->create_course();
1095         // Ensure an exception is thrown.
1096         $this->expectException('moodle_exception');
1097         core_message_external::data_for_messagearea_search_users_in_course($user2->id, $course->id, 'User');
1098     }
1100     /**
1101      * Tests searching users in course with messaging disabled.
1102      */
1103     public function test_messagearea_search_users_in_course_messaging_disabled() {
1104         global $CFG;
1106         $this->resetAfterTest(true);
1108         // Create some skeleton data just so we can call the WS..
1109         $user = self::getDataGenerator()->create_user();
1110         $course = $this->getDataGenerator()->create_course();
1112         // The person doing the search for another user.
1113         $this->setUser($user);
1115         // Disable messaging.
1116         $CFG->messaging = 0;
1118         // Ensure an exception is thrown.
1119         $this->expectException('moodle_exception');
1120         core_message_external::data_for_messagearea_search_users_in_course($user->id, $course->id, 'User');
1121     }
1123     /**
1124      * Tests searching users.
1125      */
1126     public function test_messagearea_search_users() {
1127         $this->resetAfterTest(true);
1129         // Create some users.
1130         $user1 = new stdClass();
1131         $user1->firstname = 'User';
1132         $user1->lastname = 'One';
1133         $user1 = self::getDataGenerator()->create_user($user1);
1135         // Set as the user performing the search.
1136         $this->setUser($user1);
1138         $user2 = new stdClass();
1139         $user2->firstname = 'User search';
1140         $user2->lastname = 'Two';
1141         $user2 = self::getDataGenerator()->create_user($user2);
1143         $user3 = new stdClass();
1144         $user3->firstname = 'User search';
1145         $user3->lastname = 'Three';
1146         $user3 = self::getDataGenerator()->create_user($user3);
1148         $user4 = new stdClass();
1149         $user4->firstname = 'User';
1150         $user4->lastname = 'Four';
1151         $user4 = self::getDataGenerator()->create_user($user4);
1153         $user5 = new stdClass();
1154         $user5->firstname = 'User search';
1155         $user5->lastname = 'Five';
1156         $user5 = self::getDataGenerator()->create_user($user5);
1158         $user6 = new stdClass();
1159         $user6->firstname = 'User';
1160         $user6->lastname = 'Six';
1161         $user6 = self::getDataGenerator()->create_user($user6);
1163         // Create some courses.
1164         $course1 = new stdClass();
1165         $course1->fullname = 'Course search';
1166         $course1->shortname = 'One';
1167         $course1 = $this->getDataGenerator()->create_course($course1);
1169         $course2 = new stdClass();
1170         $course2->fullname = 'Course';
1171         $course2->shortname = 'Two';
1172         $course2 = $this->getDataGenerator()->create_course($course2);
1174         $course3 = new stdClass();
1175         $course3->fullname = 'Course';
1176         $course3->shortname = 'Three search';
1177         $course3 = $this->getDataGenerator()->create_course($course3);
1179         // Add some users as contacts.
1180         message_add_contact($user2->id, 0, $user1->id);
1181         message_add_contact($user3->id, 0, $user1->id);
1182         message_add_contact($user4->id, 0, $user1->id);
1184         // Perform a search.
1185         $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
1187         // We need to execute the return values cleaning process to simulate the web service server.
1188         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
1189             $result);
1191         // Confirm that we returns contacts, courses and non-contacts.
1192         $contacts = $result['contacts'];
1193         $courses = $result['courses'];
1194         $noncontacts = $result['noncontacts'];
1196         // Check that we retrieved the correct contacts.
1197         $this->assertCount(2, $contacts);
1198         $this->assertEquals($user3->id, $contacts[0]['userid']);
1199         $this->assertEquals($user2->id, $contacts[1]['userid']);
1201         // Check that we retrieved the correct courses.
1202         $this->assertCount(2, $courses);
1203         $this->assertEquals($course3->id, $courses[0]['id']);
1204         $this->assertEquals($course1->id, $courses[1]['id']);
1206         // Check that we retrieved the correct non-contacts.
1207         $this->assertCount(1, $noncontacts);
1208         $this->assertEquals($user5->id, $noncontacts[0]['userid']);
1209     }
1211     /**
1212      * Tests searching users as another user.
1213      */
1214     public function test_messagearea_search_users_as_other_user() {
1215         $this->resetAfterTest(true);
1217         // The person doing the search.
1218         $this->setAdminUser();
1220         // Create some users.
1221         $user1 = new stdClass();
1222         $user1->firstname = 'User';
1223         $user1->lastname = 'One';
1224         $user1 = self::getDataGenerator()->create_user($user1);
1226         $user2 = new stdClass();
1227         $user2->firstname = 'User search';
1228         $user2->lastname = 'Two';
1229         $user2 = self::getDataGenerator()->create_user($user2);
1231         $user3 = new stdClass();
1232         $user3->firstname = 'User search';
1233         $user3->lastname = 'Three';
1234         $user3 = self::getDataGenerator()->create_user($user3);
1236         $user4 = new stdClass();
1237         $user4->firstname = 'User';
1238         $user4->lastname = 'Four';
1239         $user4 = self::getDataGenerator()->create_user($user4);
1241         $user5 = new stdClass();
1242         $user5->firstname = 'User search';
1243         $user5->lastname = 'Five';
1244         $user5 = self::getDataGenerator()->create_user($user5);
1246         $user6 = new stdClass();
1247         $user6->firstname = 'User';
1248         $user6->lastname = 'Six';
1249         $user6 = self::getDataGenerator()->create_user($user6);
1251         // Create some courses.
1252         $course1 = new stdClass();
1253         $course1->fullname = 'Course search';
1254         $course1->shortname = 'One';
1255         $course1 = $this->getDataGenerator()->create_course($course1);
1257         $course2 = new stdClass();
1258         $course2->fullname = 'Course';
1259         $course2->shortname = 'Two';
1260         $course2 = $this->getDataGenerator()->create_course($course2);
1262         $course3 = new stdClass();
1263         $course3->fullname = 'Course';
1264         $course3->shortname = 'Three search';
1265         $course3 = $this->getDataGenerator()->create_course($course3);
1267         // Add some users as contacts.
1268         message_add_contact($user2->id, 0, $user1->id);
1269         message_add_contact($user3->id, 0, $user1->id);
1270         message_add_contact($user4->id, 0, $user1->id);
1272         // Perform a search.
1273         $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
1275         // We need to execute the return values cleaning process to simulate the web service server.
1276         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
1277             $result);
1279         // Confirm that we returns contacts, courses and non-contacts.
1280         $contacts = $result['contacts'];
1281         $courses = $result['courses'];
1282         $noncontacts = $result['noncontacts'];
1284         // Check that we retrieved the correct contacts.
1285         $this->assertCount(2, $contacts);
1286         $this->assertEquals($user3->id, $contacts[0]['userid']);
1287         $this->assertEquals($user2->id, $contacts[1]['userid']);
1289         // Check that we retrieved the correct courses.
1290         $this->assertCount(2, $courses);
1291         $this->assertEquals($course3->id, $courses[0]['id']);
1292         $this->assertEquals($course1->id, $courses[1]['id']);
1294         // Check that we retrieved the correct non-contacts.
1295         $this->assertCount(1, $noncontacts);
1296         $this->assertEquals($user5->id, $noncontacts[0]['userid']);
1297     }
1299     /**
1300      * Tests searching users as another user without the proper capabilities.
1301      */
1302     public function test_messagearea_search_users_as_other_user_without_cap() {
1303         $this->resetAfterTest(true);
1305         // Create some users.
1306         $user1 = self::getDataGenerator()->create_user();
1307         $user2 = self::getDataGenerator()->create_user();
1309         // The person doing the search for another user.
1310         $this->setUser($user1);
1312         // Ensure an exception is thrown.
1313         $this->expectException('moodle_exception');
1314         core_message_external::data_for_messagearea_search_users($user2->id, 'User');
1315     }
1317     /**
1318      * Tests searching users with messaging disabled.
1319      */
1320     public function test_messagearea_search_users_messaging_disabled() {
1321         global $CFG;
1323         $this->resetAfterTest(true);
1325         // Create some skeleton data just so we can call the WS.
1326         $user = self::getDataGenerator()->create_user();
1328         // The person doing the search.
1329         $this->setUser($user);
1331         // Disable messaging.
1332         $CFG->messaging = 0;
1334         // Ensure an exception is thrown.
1335         $this->expectException('moodle_exception');
1336         core_message_external::data_for_messagearea_search_users($user->id, 'User');
1337     }
1339     /**
1340      * Tests searching messages.
1341      */
1342     public function test_messagearea_search_messages() {
1343         $this->resetAfterTest(true);
1345         // Create some users.
1346         $user1 = self::getDataGenerator()->create_user();
1347         $user2 = self::getDataGenerator()->create_user();
1349         // The person doing the search.
1350         $this->setUser($user1);
1352         // Send some messages back and forth.
1353         $time = time();
1354         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1355         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1356         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1357         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1359         // Perform a search.
1360         $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
1362         // We need to execute the return values cleaning process to simulate the web service server.
1363         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
1364             $result);
1366         // Confirm the data is correct.
1367         $messages = $result['contacts'];
1368         $this->assertCount(2, $messages);
1370         $message1 = $messages[0];
1371         $message2 = $messages[1];
1373         $this->assertEquals($user2->id, $message1['userid']);
1374         $this->assertEquals(fullname($user2), $message1['fullname']);
1375         $this->assertTrue($message1['ismessaging']);
1376         $this->assertFalse($message1['sentfromcurrentuser']);
1377         $this->assertEquals('Word.', $message1['lastmessage']);
1378         $this->assertNotEmpty($message1['messageid']);
1379         $this->assertFalse($message1['isonline']);
1380         $this->assertFalse($message1['isread']);
1381         $this->assertFalse($message1['isblocked']);
1382         $this->assertNull($message1['unreadcount']);
1384         $this->assertEquals($user2->id, $message2['userid']);
1385         $this->assertEquals(fullname($user2), $message2['fullname']);
1386         $this->assertTrue($message2['ismessaging']);
1387         $this->assertTrue($message2['sentfromcurrentuser']);
1388         $this->assertEquals('Yo!', $message2['lastmessage']);
1389         $this->assertNotEmpty($message2['messageid']);
1390         $this->assertFalse($message2['isonline']);
1391         $this->assertTrue($message2['isread']);
1392         $this->assertFalse($message2['isblocked']);
1393         $this->assertNull($message2['unreadcount']);
1394     }
1396     /**
1397      * Tests searching messages as another user.
1398      */
1399     public function test_messagearea_search_messages_as_other_user() {
1400         $this->resetAfterTest(true);
1402         // The person doing the search.
1403         $this->setAdminUser();
1405         // Create some users.
1406         $user1 = self::getDataGenerator()->create_user();
1407         $user2 = self::getDataGenerator()->create_user();
1409         // Send some messages back and forth.
1410         $time = time();
1411         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1412         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1413         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1414         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1416         // Perform a search.
1417         $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
1419         // We need to execute the return values cleaning process to simulate the web service server.
1420         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
1421             $result);
1423         // Confirm the data is correct.
1424         $messages = $result['contacts'];
1425         $this->assertCount(2, $messages);
1427         $message1 = $messages[0];
1428         $message2 = $messages[1];
1430         $this->assertEquals($user2->id, $message1['userid']);
1431         $this->assertEquals(fullname($user2), $message1['fullname']);
1432         $this->assertTrue($message1['ismessaging']);
1433         $this->assertFalse($message1['sentfromcurrentuser']);
1434         $this->assertEquals('Word.', $message1['lastmessage']);
1435         $this->assertNotEmpty($message1['messageid']);
1436         $this->assertFalse($message1['isonline']);
1437         $this->assertFalse($message1['isread']);
1438         $this->assertFalse($message1['isblocked']);
1439         $this->assertNull($message1['unreadcount']);
1441         $this->assertEquals($user2->id, $message2['userid']);
1442         $this->assertEquals(fullname($user2), $message2['fullname']);
1443         $this->assertTrue($message2['ismessaging']);
1444         $this->assertTrue($message2['sentfromcurrentuser']);
1445         $this->assertEquals('Yo!', $message2['lastmessage']);
1446         $this->assertNotEmpty($message2['messageid']);
1447         $this->assertFalse($message2['isonline']);
1448         $this->assertTrue($message2['isread']);
1449         $this->assertFalse($message2['isblocked']);
1450         $this->assertNull($message2['unreadcount']);
1451     }
1453     /**
1454      * Tests searching messages as another user without the proper capabilities.
1455      */
1456     public function test_messagearea_search_messages_as_other_user_without_cap() {
1457         $this->resetAfterTest(true);
1459         // Create some users.
1460         $user1 = self::getDataGenerator()->create_user();
1461         $user2 = self::getDataGenerator()->create_user();
1463         // The person doing the search for another user.
1464         $this->setUser($user1);
1466         // Ensure an exception is thrown.
1467         $this->expectException('moodle_exception');
1468         core_message_external::data_for_messagearea_search_messages($user2->id, 'Search');
1469     }
1471     /**
1472      * Tests searching messages with messaging disabled
1473      */
1474     public function test_messagearea_search_messages_messaging_disabled() {
1475         global $CFG;
1477         $this->resetAfterTest(true);
1479         // Create some skeleton data just so we can call the WS.
1480         $user = self::getDataGenerator()->create_user();
1482         // The person doing the search .
1483         $this->setUser($user);
1485         // Disable messaging.
1486         $CFG->messaging = 0;
1488         // Ensure an exception is thrown.
1489         $this->expectException('moodle_exception');
1490         core_message_external::data_for_messagearea_search_messages($user->id, 'Search');
1491     }
1493     /**
1494      * Tests retrieving conversations.
1495      */
1496     public function test_messagearea_conversations() {
1497         $this->resetAfterTest(true);
1499         // Create some users.
1500         $user1 = self::getDataGenerator()->create_user();
1501         $user2 = self::getDataGenerator()->create_user();
1502         $user3 = self::getDataGenerator()->create_user();
1503         $user4 = self::getDataGenerator()->create_user();
1505         // The person retrieving the conversations.
1506         $this->setUser($user1);
1508         // Send some messages back and forth, have some different conversations with different users.
1509         $time = time();
1510         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1511         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1512         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1513         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1515         $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
1516         $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
1517         $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
1518         $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
1520         $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
1521         $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
1522         $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
1524         // Retrieve the conversations.
1525         $result = core_message_external::data_for_messagearea_conversations($user1->id);
1527         // We need to execute the return values cleaning process to simulate the web service server.
1528         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
1529             $result);
1531         // Confirm the data is correct.
1532         $messages = $result['contacts'];
1533         $this->assertCount(3, $messages);
1535         $message1 = $messages[0];
1536         $message2 = $messages[1];
1537         $message3 = $messages[2];
1539         $this->assertEquals($user4->id, $message1['userid']);
1540         $this->assertTrue($message1['ismessaging']);
1541         $this->assertTrue($message1['sentfromcurrentuser']);
1542         $this->assertEquals('Dope.', $message1['lastmessage']);
1543         $this->assertNull($message1['messageid']);
1544         $this->assertFalse($message1['isonline']);
1545         $this->assertTrue($message1['isread']);
1546         $this->assertFalse($message1['isblocked']);
1547         $this->assertEquals(0, $message1['unreadcount']);
1549         $this->assertEquals($user3->id, $message2['userid']);
1550         $this->assertTrue($message2['ismessaging']);
1551         $this->assertFalse($message2['sentfromcurrentuser']);
1552         $this->assertEquals('Cool.', $message2['lastmessage']);
1553         $this->assertNull($message2['messageid']);
1554         $this->assertFalse($message2['isonline']);
1555         $this->assertFalse($message2['isread']);
1556         $this->assertFalse($message2['isblocked']);
1557         $this->assertEquals(2, $message2['unreadcount']);
1559         $this->assertEquals($user2->id, $message3['userid']);
1560         $this->assertTrue($message3['ismessaging']);
1561         $this->assertFalse($message3['sentfromcurrentuser']);
1562         $this->assertEquals('Word.', $message3['lastmessage']);
1563         $this->assertNull($message3['messageid']);
1564         $this->assertFalse($message3['isonline']);
1565         $this->assertFalse($message3['isread']);
1566         $this->assertFalse($message3['isblocked']);
1567         $this->assertEquals(2, $message3['unreadcount']);
1568     }
1570     /**
1571      * Tests retrieving conversations as another user.
1572      */
1573     public function test_messagearea_conversations_as_other_user() {
1574         $this->resetAfterTest(true);
1576         // Set as admin.
1577         $this->setAdminUser();
1579         // Create some users.
1580         $user1 = self::getDataGenerator()->create_user();
1581         $user2 = self::getDataGenerator()->create_user();
1582         $user3 = self::getDataGenerator()->create_user();
1583         $user4 = self::getDataGenerator()->create_user();
1585         // Send some messages back and forth, have some different conversations with different users.
1586         $time = time();
1587         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1588         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1589         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1590         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1592         $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
1593         $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
1594         $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
1595         $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
1597         $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
1598         $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
1599         $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
1601         // Retrieve the conversations.
1602         $result = core_message_external::data_for_messagearea_conversations($user1->id);
1604         // We need to execute the return values cleaning process to simulate the web service server.
1605         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
1606             $result);
1608         // Confirm the data is correct.
1609         $messages = $result['contacts'];
1610         $this->assertCount(3, $messages);
1612         $message1 = $messages[0];
1613         $message2 = $messages[1];
1614         $message3 = $messages[2];
1616         $this->assertEquals($user4->id, $message1['userid']);
1617         $this->assertTrue($message1['ismessaging']);
1618         $this->assertTrue($message1['sentfromcurrentuser']);
1619         $this->assertEquals('Dope.', $message1['lastmessage']);
1620         $this->assertNull($message1['messageid']);
1621         $this->assertFalse($message1['isonline']);
1622         $this->assertTrue($message1['isread']);
1623         $this->assertFalse($message1['isblocked']);
1624         $this->assertEquals(0, $message1['unreadcount']);
1626         $this->assertEquals($user3->id, $message2['userid']);
1627         $this->assertTrue($message2['ismessaging']);
1628         $this->assertFalse($message2['sentfromcurrentuser']);
1629         $this->assertEquals('Cool.', $message2['lastmessage']);
1630         $this->assertNull($message2['messageid']);
1631         $this->assertFalse($message2['isonline']);
1632         $this->assertFalse($message2['isread']);
1633         $this->assertFalse($message2['isblocked']);
1634         $this->assertEquals(2, $message2['unreadcount']);
1636         $this->assertEquals($user2->id, $message3['userid']);
1637         $this->assertTrue($message3['ismessaging']);
1638         $this->assertFalse($message3['sentfromcurrentuser']);
1639         $this->assertEquals('Word.', $message3['lastmessage']);
1640         $this->assertNull($message3['messageid']);
1641         $this->assertFalse($message3['isonline']);
1642         $this->assertFalse($message3['isread']);
1643         $this->assertFalse($message3['isblocked']);
1644         $this->assertEquals(2, $message3['unreadcount']);
1645     }
1647     /**
1648      * Tests retrieving conversations as another user without the proper capabilities.
1649      */
1650     public function test_messagearea_conversations_as_other_user_without_cap() {
1651         $this->resetAfterTest(true);
1653         // Create some users.
1654         $user1 = self::getDataGenerator()->create_user();
1655         $user2 = self::getDataGenerator()->create_user();
1657         // The person retrieving the conversations for another user.
1658         $this->setUser($user1);
1660         // Ensure an exception is thrown.
1661         $this->expectException('moodle_exception');
1662         core_message_external::data_for_messagearea_conversations($user2->id);
1663     }
1665     /**
1666      * Tests retrieving conversations with messaging disabled.
1667      */
1668     public function test_messagearea_conversations_messaging_disabled() {
1669         global $CFG;
1671         $this->resetAfterTest(true);
1673         // Create some skeleton data just so we can call the WS.
1674         $user = self::getDataGenerator()->create_user();
1676         // The person retrieving the conversations.
1677         $this->setUser($user);
1679         // Disable messaging.
1680         $CFG->messaging = 0;
1682         // Ensure an exception is thrown.
1683         $this->expectException('moodle_exception');
1684         core_message_external::data_for_messagearea_conversations($user->id);
1685     }
1687     /**
1688      * Tests retrieving contacts.
1689      */
1690     public function test_messagearea_contacts() {
1691         $this->resetAfterTest(true);
1693         // Create some users.
1694         $user1 = self::getDataGenerator()->create_user();
1696         // Set as the user.
1697         $this->setUser($user1);
1699         $user2 = new stdClass();
1700         $user2->firstname = 'User';
1701         $user2->lastname = 'A';
1702         $user2 = self::getDataGenerator()->create_user($user2);
1704         $user3 = new stdClass();
1705         $user3->firstname = 'User';
1706         $user3->lastname = 'B';
1707         $user3 = self::getDataGenerator()->create_user($user3);
1709         $user4 = new stdClass();
1710         $user4->firstname = 'User';
1711         $user4->lastname = 'C';
1712         $user4 = self::getDataGenerator()->create_user($user4);
1714         $user5 = new stdClass();
1715         $user5->firstname = 'User';
1716         $user5->lastname = 'D';
1717         $user5 = self::getDataGenerator()->create_user($user5);
1719         // Add some users as contacts.
1720         message_add_contact($user2->id, 0, $user1->id);
1721         message_add_contact($user3->id, 0, $user1->id);
1722         message_add_contact($user4->id, 0, $user1->id);
1724         // Retrieve the contacts.
1725         $result = core_message_external::data_for_messagearea_contacts($user1->id);
1727         // We need to execute the return values cleaning process to simulate the web service server.
1728         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
1729             $result);
1731         // Confirm the data is correct.
1732         $contacts = $result['contacts'];
1733         $this->assertCount(3, $contacts);
1735         $contact1 = $contacts[0];
1736         $contact2 = $contacts[1];
1737         $contact3 = $contacts[2];
1739         $this->assertEquals($user2->id, $contact1['userid']);
1740         $this->assertFalse($contact1['ismessaging']);
1741         $this->assertFalse($contact1['sentfromcurrentuser']);
1742         $this->assertNull($contact1['lastmessage']);
1743         $this->assertNull($contact1['messageid']);
1744         $this->assertFalse($contact1['isonline']);
1745         $this->assertFalse($contact1['isread']);
1746         $this->assertFalse($contact1['isblocked']);
1747         $this->assertNull($contact1['unreadcount']);
1749         $this->assertEquals($user3->id, $contact2['userid']);
1750         $this->assertFalse($contact2['ismessaging']);
1751         $this->assertFalse($contact2['sentfromcurrentuser']);
1752         $this->assertNull($contact2['lastmessage']);
1753         $this->assertNull($contact2['messageid']);
1754         $this->assertFalse($contact2['isonline']);
1755         $this->assertFalse($contact2['isread']);
1756         $this->assertFalse($contact2['isblocked']);
1757         $this->assertNull($contact2['unreadcount']);
1759         $this->assertEquals($user4->id, $contact3['userid']);
1760         $this->assertFalse($contact3['ismessaging']);
1761         $this->assertFalse($contact3['sentfromcurrentuser']);
1762         $this->assertNull($contact3['lastmessage']);
1763         $this->assertNull($contact3['messageid']);
1764         $this->assertFalse($contact3['isonline']);
1765         $this->assertFalse($contact3['isread']);
1766         $this->assertFalse($contact3['isblocked']);
1767         $this->assertNull($contact3['unreadcount']);
1768     }
1770     /**
1771      * Tests retrieving contacts as another user.
1772      */
1773     public function test_messagearea_contacts_as_other_user() {
1774         $this->resetAfterTest(true);
1776         $this->setAdminUser();
1778         // Create some users.
1779         $user1 = self::getDataGenerator()->create_user();
1781         $user2 = new stdClass();
1782         $user2->firstname = 'User';
1783         $user2->lastname = 'A';
1784         $user2 = self::getDataGenerator()->create_user($user2);
1786         $user3 = new stdClass();
1787         $user3->firstname = 'User';
1788         $user3->lastname = 'B';
1789         $user3 = self::getDataGenerator()->create_user($user3);
1791         $user4 = new stdClass();
1792         $user4->firstname = 'User';
1793         $user4->lastname = 'C';
1794         $user4 = self::getDataGenerator()->create_user($user4);
1796         $user5 = new stdClass();
1797         $user5->firstname = 'User';
1798         $user5->lastname = 'D';
1799         $user5 = self::getDataGenerator()->create_user($user5);
1801         // Add some users as contacts.
1802         message_add_contact($user2->id, 0, $user1->id);
1803         message_add_contact($user3->id, 0, $user1->id);
1804         message_add_contact($user4->id, 0, $user1->id);
1806         // Retrieve the contacts.
1807         $result = core_message_external::data_for_messagearea_contacts($user1->id);
1809         // We need to execute the return values cleaning process to simulate the web service server.
1810         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
1811             $result);
1813         // Confirm the data is correct.
1814         $contacts = $result['contacts'];
1815         $this->assertCount(3, $contacts);
1817         $contact1 = $contacts[0];
1818         $contact2 = $contacts[1];
1819         $contact3 = $contacts[2];
1821         $this->assertEquals($user2->id, $contact1['userid']);
1822         $this->assertFalse($contact1['ismessaging']);
1823         $this->assertFalse($contact1['sentfromcurrentuser']);
1824         $this->assertNull($contact1['lastmessage']);
1825         $this->assertNull($contact1['messageid']);
1826         $this->assertFalse($contact1['isonline']);
1827         $this->assertFalse($contact1['isread']);
1828         $this->assertFalse($contact1['isblocked']);
1829         $this->assertNull($contact1['unreadcount']);
1831         $this->assertEquals($user3->id, $contact2['userid']);
1832         $this->assertFalse($contact2['ismessaging']);
1833         $this->assertFalse($contact2['sentfromcurrentuser']);
1834         $this->assertNull($contact2['lastmessage']);
1835         $this->assertNull($contact2['messageid']);
1836         $this->assertFalse($contact2['isonline']);
1837         $this->assertFalse($contact2['isread']);
1838         $this->assertFalse($contact2['isblocked']);
1839         $this->assertNull($contact2['unreadcount']);
1841         $this->assertEquals($user4->id, $contact3['userid']);
1842         $this->assertFalse($contact3['ismessaging']);
1843         $this->assertFalse($contact3['sentfromcurrentuser']);
1844         $this->assertNull($contact3['lastmessage']);
1845         $this->assertNull($contact3['messageid']);
1846         $this->assertFalse($contact3['isonline']);
1847         $this->assertFalse($contact3['isread']);
1848         $this->assertFalse($contact3['isblocked']);
1849         $this->assertNull($contact3['unreadcount']);
1850     }
1852     /**
1853      * Tests retrieving contacts as another user without the proper capabilities.
1854      */
1855     public function test_messagearea_contacts_as_other_user_without_cap() {
1856         $this->resetAfterTest(true);
1858         // Create some users.
1859         $user1 = self::getDataGenerator()->create_user();
1860         $user2 = self::getDataGenerator()->create_user();
1862         // The person retrieving the contacts for another user.
1863         $this->setUser($user1);
1865         // Perform the WS call and ensure an exception is thrown.
1866         $this->expectException('moodle_exception');
1867         core_message_external::data_for_messagearea_contacts($user2->id);
1868     }
1870     /**
1871      * Tests retrieving contacts with messaging disabled.
1872      */
1873     public function test_messagearea_contacts_messaging_disabled() {
1874         global $CFG;
1876         $this->resetAfterTest(true);
1878         // Create some skeleton data just so we can call the WS.
1879         $user = self::getDataGenerator()->create_user();
1881         // The person retrieving the contacts.
1882         $this->setUser($user);
1884         // Disable messaging.
1885         $CFG->messaging = 0;
1887         // Perform the WS call and ensure we are shown that it is disabled.
1888         $this->expectException('moodle_exception');
1889         core_message_external::data_for_messagearea_contacts($user->id);
1890     }
1892     /**
1893      * Tests retrieving messages.
1894      */
1895     public function test_messagearea_messages() {
1896         $this->resetAfterTest(true);
1898         // Create some users.
1899         $user1 = self::getDataGenerator()->create_user();
1900         $user2 = self::getDataGenerator()->create_user();
1902         // The person asking for the messages.
1903         $this->setUser($user1);
1905         // Send some messages back and forth.
1906         $time = time();
1907         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1908         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1909         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1910         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1912         // Retrieve the messages.
1913         $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
1915         // We need to execute the return values cleaning process to simulate the web service server.
1916         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
1917             $result);
1919         // Check the results are correct.
1920         $this->assertTrue($result['iscurrentuser']);
1921         $this->assertEquals($user1->id, $result['currentuserid']);
1922         $this->assertEquals($user2->id, $result['otheruserid']);
1923         $this->assertEquals(fullname($user2), $result['otheruserfullname']);
1924         $this->assertFalse($result['isonline']);
1926         // Confirm the message data is correct.
1927         $messages = $result['messages'];
1928         $this->assertCount(4, $messages);
1930         $message1 = $messages[0];
1931         $message2 = $messages[1];
1932         $message3 = $messages[2];
1933         $message4 = $messages[3];
1935         $this->assertEquals($user1->id, $message1['useridfrom']);
1936         $this->assertEquals($user2->id, $message1['useridto']);
1937         $this->assertTrue($message1['displayblocktime']);
1938         $this->assertContains('Yo!', $message1['text']);
1940         $this->assertEquals($user2->id, $message2['useridfrom']);
1941         $this->assertEquals($user1->id, $message2['useridto']);
1942         $this->assertFalse($message2['displayblocktime']);
1943         $this->assertContains('Sup mang?', $message2['text']);
1945         $this->assertEquals($user1->id, $message3['useridfrom']);
1946         $this->assertEquals($user2->id, $message3['useridto']);
1947         $this->assertFalse($message3['displayblocktime']);
1948         $this->assertContains('Writing PHPUnit tests!', $message3['text']);
1950         $this->assertEquals($user2->id, $message4['useridfrom']);
1951         $this->assertEquals($user1->id, $message4['useridto']);
1952         $this->assertFalse($message4['displayblocktime']);
1953         $this->assertContains('Word.', $message4['text']);
1954     }
1956     /**
1957      * Tests retrieving messages as another user.
1958      */
1959     public function test_messagearea_messages_as_other_user() {
1960         $this->resetAfterTest(true);
1962         // Set as admin.
1963         $this->setAdminUser();
1965         // Create some users.
1966         $user1 = self::getDataGenerator()->create_user();
1967         $user2 = self::getDataGenerator()->create_user();
1969         // Send some messages back and forth.
1970         $time = time();
1971         $this->send_message($user1, $user2, 'Yo!', 0, $time);
1972         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1973         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1974         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1976         // Retrieve the messages.
1977         $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
1979         // We need to execute the return values cleaning process to simulate the web service server.
1980         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
1981             $result);
1983         // Check the results are correct.
1984         $this->assertFalse($result['iscurrentuser']);
1985         $this->assertEquals($user1->id, $result['currentuserid']);
1986         $this->assertEquals($user2->id, $result['otheruserid']);
1987         $this->assertEquals(fullname($user2), $result['otheruserfullname']);
1988         $this->assertFalse($result['isonline']);
1990         // Confirm the message data is correct.
1991         $messages = $result['messages'];
1992         $this->assertCount(4, $messages);
1994         $message1 = $messages[0];
1995         $message2 = $messages[1];
1996         $message3 = $messages[2];
1997         $message4 = $messages[3];
1999         $this->assertEquals($user1->id, $message1['useridfrom']);
2000         $this->assertEquals($user2->id, $message1['useridto']);
2001         $this->assertTrue($message1['displayblocktime']);
2002         $this->assertContains('Yo!', $message1['text']);
2004         $this->assertEquals($user2->id, $message2['useridfrom']);
2005         $this->assertEquals($user1->id, $message2['useridto']);
2006         $this->assertFalse($message2['displayblocktime']);
2007         $this->assertContains('Sup mang?', $message2['text']);
2009         $this->assertEquals($user1->id, $message3['useridfrom']);
2010         $this->assertEquals($user2->id, $message3['useridto']);
2011         $this->assertFalse($message3['displayblocktime']);
2012         $this->assertContains('Writing PHPUnit tests!', $message3['text']);
2014         $this->assertEquals($user2->id, $message4['useridfrom']);
2015         $this->assertEquals($user1->id, $message4['useridto']);
2016         $this->assertFalse($message4['displayblocktime']);
2017         $this->assertContains('Word.', $message4['text']);
2018     }
2020     /**
2021      * Tests retrieving messages as another user without the proper capabilities.
2022      */
2023     public function test_messagearea_messages_as_other_user_without_cap() {
2024         $this->resetAfterTest(true);
2026         // Create some users.
2027         $user1 = self::getDataGenerator()->create_user();
2028         $user2 = self::getDataGenerator()->create_user();
2029         $user3 = self::getDataGenerator()->create_user();
2031         // The person asking for the messages for another user.
2032         $this->setUser($user1);
2034         // Ensure an exception is thrown.
2035         $this->expectException('moodle_exception');
2036         core_message_external::data_for_messagearea_messages($user2->id, $user3->id);
2037     }
2039     /**
2040      * Tests retrieving messages with messaging disabled.
2041      */
2042     public function test_messagearea_messages_messaging_disabled() {
2043         global $CFG;
2045         $this->resetAfterTest(true);
2047         // Create some skeleton data just so we can call the WS.
2048         $user1 = self::getDataGenerator()->create_user();
2049         $user2 = self::getDataGenerator()->create_user();
2051         // The person asking for the messages for another user.
2052         $this->setUser($user1);
2054         // Disable messaging.
2055         $CFG->messaging = 0;
2057         // Ensure an exception is thrown.
2058         $this->expectException('moodle_exception');
2059         core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
2060     }
2062     /**
2063      * Tests retrieving most recent message.
2064      */
2065     public function test_messagearea_get_most_recent_message() {
2066         $this->resetAfterTest(true);
2068         // Create some users.
2069         $user1 = self::getDataGenerator()->create_user();
2070         $user2 = self::getDataGenerator()->create_user();
2072         // The person doing the search.
2073         $this->setUser($user1);
2075         // Send some messages back and forth.
2076         $time = time();
2077         $this->send_message($user1, $user2, 'Yo!', 0, $time);
2078         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2079         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2080         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2082         // Get the most recent message.
2083         $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2085         // We need to execute the return values cleaning process to simulate the web service server.
2086         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
2087             $result);
2089         // Check the results are correct.
2090         $this->assertEquals($user2->id, $result['useridfrom']);
2091         $this->assertEquals($user1->id, $result['useridto']);
2092         $this->assertContains('Word.', $result['text']);
2093     }
2095     /**
2096      * Tests retrieving most recent message as another user.
2097      */
2098     public function test_messagearea_get_most_recent_message_as_other_user() {
2099         $this->resetAfterTest(true);
2101         // The person doing the search.
2102         $this->setAdminUser();
2104         // Create some users.
2105         $user1 = self::getDataGenerator()->create_user();
2106         $user2 = self::getDataGenerator()->create_user();
2108         // Send some messages back and forth.
2109         $time = time();
2110         $this->send_message($user1, $user2, 'Yo!', 0, $time);
2111         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2112         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2113         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2115         // Get the most recent message.
2116         $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2118         // We need to execute the return values cleaning process to simulate the web service server.
2119         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
2120             $result);
2122         // Check the results are correct.
2123         $this->assertEquals($user2->id, $result['useridfrom']);
2124         $this->assertEquals($user1->id, $result['useridto']);
2125         $this->assertContains('Word.', $result['text']);
2126     }
2128     /**
2129      * Tests retrieving most recent message as another user without the proper capabilities.
2130      */
2131     public function test_messagearea_get_most_recent_message_as_other_user_without_cap() {
2132         $this->resetAfterTest(true);
2134         // Create some users.
2135         $user1 = self::getDataGenerator()->create_user();
2136         $user2 = self::getDataGenerator()->create_user();
2137         $user3 = self::getDataGenerator()->create_user();
2139         // The person asking for the most recent message for another user.
2140         $this->setUser($user1);
2142         // Ensure an exception is thrown.
2143         $this->expectException('moodle_exception');
2144         core_message_external::data_for_messagearea_get_most_recent_message($user2->id, $user3->id);
2145     }
2147     /**
2148      * Tests retrieving most recent message with messaging disabled.
2149      */
2150     public function test_messagearea_get_most_recent_message_messaging_disabled() {
2151         global $CFG;
2153         $this->resetAfterTest(true);
2155         // Create some skeleton data just so we can call the WS.
2156         $user1 = self::getDataGenerator()->create_user();
2157         $user2 = self::getDataGenerator()->create_user();
2159         // The person asking for the most recent message.
2160         $this->setUser($user1);
2162         // Disable messaging.
2163         $CFG->messaging = 0;
2165         // Ensure an exception is thrown.
2166         $this->expectException('moodle_exception');
2167         core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2168     }
2170     /**
2171      * Tests retrieving a user's profile.
2172      */
2173     public function test_messagearea_get_profile() {
2174         $this->resetAfterTest(true);
2176         // Create some users.
2177         $user1 = self::getDataGenerator()->create_user();
2178         $user2 = self::getDataGenerator()->create_user();
2180         // The person asking for the profile information.
2181         $this->setUser($user1);
2183         // Get the profile.
2184         $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2186         // We need to execute the return values cleaning process to simulate the web service server.
2187         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(),
2188             $result);
2190         $this->assertEquals($user2->id, $result['userid']);
2191         $this->assertEmpty($result['email']);
2192         $this->assertEmpty($result['country']);
2193         $this->assertEmpty($result['city']);
2194         $this->assertEquals(fullname($user2), $result['fullname']);
2195         $this->assertFalse($result['isonline']);
2196         $this->assertFalse($result['isblocked']);
2197         $this->assertFalse($result['iscontact']);
2198     }
2200     /**
2201      * Tests retrieving a user's profile as another user.
2202      */
2203     public function test_messagearea_profile_as_other_user() {
2204         $this->resetAfterTest(true);
2206         // The person asking for the profile information.
2207         $this->setAdminUser();
2209         // Create some users.
2210         $user1 = self::getDataGenerator()->create_user();
2212         $user2 = new stdClass();
2213         $user2->country = 'AU';
2214         $user2->city = 'Perth';
2215         $user2 = self::getDataGenerator()->create_user($user2);
2217         // Get the profile.
2218         $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2220         // We need to execute the return values cleaning process to simulate the web service server.
2221         $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(),
2222             $result);
2224         $this->assertEquals($user2->id, $result['userid']);
2225         $this->assertEquals($user2->email, $result['email']);
2226         $this->assertEquals(get_string($user2->country, 'countries'), $result['country']);
2227         $this->assertEquals($user2->city, $result['city']);
2228         $this->assertEquals(fullname($user2), $result['fullname']);
2229         $this->assertFalse($result['isonline']);
2230         $this->assertFalse($result['isblocked']);
2231         $this->assertFalse($result['iscontact']);
2232     }
2234     /**
2235      * Tests retrieving a user's profile as another user without the proper capabilities.
2236      */
2237     public function test_messagearea_profile_as_other_user_without_cap() {
2238         $this->resetAfterTest(true);
2240         // Create some users.
2241         $user1 = self::getDataGenerator()->create_user();
2242         $user2 = self::getDataGenerator()->create_user();
2243         $user3 = self::getDataGenerator()->create_user();
2245         // The person asking for the profile information for another user.
2246         $this->setUser($user1);
2248         // Ensure an exception is thrown.
2249         $this->expectException('moodle_exception');
2250         core_message_external::data_for_messagearea_get_profile($user2->id, $user3->id);
2251     }
2253     /**
2254      * Tests retrieving a user's profile with messaging disabled.
2255      */
2256     public function test_messagearea_profile_messaging_disabled() {
2257         global $CFG;
2259         $this->resetAfterTest(true);
2261         // Create some skeleton data just so we can call the WS.
2262         $user1 = self::getDataGenerator()->create_user();
2263         $user2 = self::getDataGenerator()->create_user();
2265         // The person asking for the profile information.
2266         $this->setUser($user1);
2268         // Disable messaging.
2269         $CFG->messaging = 0;
2271         // Ensure an exception is thrown.
2272         $this->expectException('moodle_exception');
2273         core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2274     }
2276     /**
2277      * Test marking all message as read with an invalid user.
2278      */
2279     public function test_mark_all_messages_as_read_invalid_user_exception() {
2280         $this->resetAfterTest(true);
2282         $this->expectException('moodle_exception');
2283         core_message_external::mark_all_messages_as_read(-2132131, 0);
2284     }
2286     /**
2287      * Test marking all message as read without proper access.
2288      */
2289     public function test_mark_all_messages_as_read_access_denied_exception() {
2290         $this->resetAfterTest(true);
2292         $sender = $this->getDataGenerator()->create_user();
2293         $user = $this->getDataGenerator()->create_user();
2295         $this->setUser($user);
2296         $this->expectException('moodle_exception');
2297         core_message_external::mark_all_messages_as_read($sender->id, 0);
2298     }
2300     /**
2301      * Test marking all message as read with missing from user.
2302      */
2303     public function test_mark_all_messages_as_read_missing_from_user_exception() {
2304         $this->resetAfterTest(true);
2306         $sender = $this->getDataGenerator()->create_user();
2308         $this->setUser($sender);
2309         $this->expectException('moodle_exception');
2310         core_message_external::mark_all_messages_as_read($sender->id, 99999);
2311     }
2313     /**
2314      * Test marking all message as read.
2315      */
2316     public function test_mark_all_messages_as_read() {
2317         global $DB;
2319         $this->resetAfterTest(true);
2321         $sender1 = $this->getDataGenerator()->create_user();
2322         $sender2 = $this->getDataGenerator()->create_user();
2323         $sender3 = $this->getDataGenerator()->create_user();
2324         $recipient = $this->getDataGenerator()->create_user();
2326         $this->setUser($recipient);
2328         $this->send_message($sender1, $recipient, 'Message');
2329         $this->send_message($sender1, $recipient, 'Message');
2330         $this->send_message($sender2, $recipient, 'Message');
2331         $this->send_message($sender2, $recipient, 'Message');
2332         $this->send_message($sender3, $recipient, 'Message');
2333         $this->send_message($sender3, $recipient, 'Message');
2335         core_message_external::mark_all_messages_as_read($recipient->id, $sender1->id);
2336         $readnotifications = $DB->get_records('message_read', array('useridto' => $recipient->id));
2337         $unreadnotifications = $DB->get_records('message', array('useridto' => $recipient->id));
2339         $this->assertCount(2, $readnotifications);
2340         $this->assertCount(4, $unreadnotifications);
2342         core_message_external::mark_all_messages_as_read($recipient->id, 0);
2343         $readnotifications = $DB->get_records('message_read', array('useridto' => $recipient->id));
2344         $unreadnotifications = $DB->get_records('message', array('useridto' => $recipient->id));
2346         $this->assertCount(6, $readnotifications);
2347         $this->assertCount(0, $unreadnotifications);
2348     }
2350     /**
2351      * Test getting unread conversation count.
2352      */
2353     public function test_get_unread_conversations_count() {
2354         $this->resetAfterTest(true);
2356         // Create some users.
2357         $user1 = self::getDataGenerator()->create_user();
2358         $user2 = self::getDataGenerator()->create_user();
2359         $user3 = self::getDataGenerator()->create_user();
2360         $user4 = self::getDataGenerator()->create_user();
2362         // The person wanting the conversation count.
2363         $this->setUser($user1);
2365         // Send some messages back and forth, have some different conversations with different users.
2366         $this->send_message($user1, $user2, 'Yo!');
2367         $this->send_message($user2, $user1, 'Sup mang?');
2368         $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
2369         $this->send_message($user2, $user1, 'Word.');
2371         $this->send_message($user1, $user3, 'Booyah');
2372         $this->send_message($user3, $user1, 'Whaaat?');
2373         $this->send_message($user1, $user3, 'Nothing.');
2374         $this->send_message($user3, $user1, 'Cool.');
2376         $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
2377         $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
2378         $this->send_message($user1, $user4, 'Dope.');
2380         // Get the unread conversation count.
2381         $result = core_message_external::get_unread_conversations_count($user1->id);
2383         // We need to execute the return values cleaning process to simulate the web service server.
2384         $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(),
2385             $result);
2387         $this->assertEquals(3, $result);
2388     }
2390     /**
2391      * Test getting unread conversation count as other user.
2392      */
2393     public function test_get_unread_conversations_count_as_other_user() {
2394         $this->resetAfterTest(true);
2396         // The person wanting the conversation count.
2397         $this->setAdminUser();
2399         // Create some users.
2400         $user1 = self::getDataGenerator()->create_user();
2401         $user2 = self::getDataGenerator()->create_user();
2402         $user3 = self::getDataGenerator()->create_user();
2403         $user4 = self::getDataGenerator()->create_user();
2405         // Send some messages back and forth, have some different conversations with different users.
2406         $this->send_message($user1, $user2, 'Yo!');
2407         $this->send_message($user2, $user1, 'Sup mang?');
2408         $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
2409         $this->send_message($user2, $user1, 'Word.');
2411         $this->send_message($user1, $user3, 'Booyah');
2412         $this->send_message($user3, $user1, 'Whaaat?');
2413         $this->send_message($user1, $user3, 'Nothing.');
2414         $this->send_message($user3, $user1, 'Cool.');
2416         $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
2417         $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
2418         $this->send_message($user1, $user4, 'Dope.');
2420         // Get the unread conversation count.
2421         $result = core_message_external::get_unread_conversations_count($user1->id);
2423         // We need to execute the return values cleaning process to simulate the web service server.
2424         $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(),
2425             $result);
2427         $this->assertEquals(3, $result);
2428     }
2430     /**
2431      * Test getting unread conversation count as other user without proper capability.
2432      */
2433     public function test_get_unread_conversations_count_as_other_user_without_cap() {
2434         $this->resetAfterTest(true);
2436         // Create some users.
2437         $user1 = self::getDataGenerator()->create_user();
2438         $user2 = self::getDataGenerator()->create_user();
2440         // The person wanting the conversation count.
2441         $this->setUser($user1);
2443         // Ensure an exception is thrown.
2444         $this->expectException('moodle_exception');
2445         core_message_external::get_unread_conversations_count($user2->id);
2446     }
2448     /**
2449      * Test deleting conversation.
2450      */
2451     public function test_delete_conversation() {
2452         global $DB;
2454         $this->resetAfterTest(true);
2456         // Create some users.
2457         $user1 = self::getDataGenerator()->create_user();
2458         $user2 = self::getDataGenerator()->create_user();
2460         // The person wanting to delete the conversation.
2461         $this->setUser($user1);
2463         // Send some messages back and forth.
2464         $time = time();
2465         $this->send_message($user1, $user2, 'Yo!', 0, $time);
2466         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2467         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2468         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2470         // Delete the conversation.
2471         core_message_external::delete_conversation($user1->id, $user2->id);
2473         $messages = $DB->get_records('message', array(), 'timecreated ASC');
2474         $this->assertCount(4, $messages);
2476         $message1 = array_shift($messages);
2477         $message2 = array_shift($messages);
2478         $message3 = array_shift($messages);
2479         $message4 = array_shift($messages);
2481         $this->assertNotEmpty($message1->timeuserfromdeleted);
2482         $this->assertEmpty($message1->timeusertodeleted);
2484         $this->assertEmpty($message2->timeuserfromdeleted);
2485         $this->assertNotEmpty($message2->timeusertodeleted);
2487         $this->assertNotEmpty($message3->timeuserfromdeleted);
2488         $this->assertEmpty($message3->timeusertodeleted);
2490         $this->assertEmpty($message4->timeuserfromdeleted);
2491         $this->assertNotEmpty($message4->timeusertodeleted);
2492     }
2494     /**
2495      * Test deleting conversation as other user.
2496      */
2497     public function test_delete_conversation_as_other_user() {
2498         global $DB;
2500         $this->resetAfterTest(true);
2502         $this->setAdminUser();
2504         // Create some users.
2505         $user1 = self::getDataGenerator()->create_user();
2506         $user2 = self::getDataGenerator()->create_user();
2508         // Send some messages back and forth.
2509         $time = time();
2510         $this->send_message($user1, $user2, 'Yo!', 0, $time);
2511         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2512         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2513         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2515         // Delete the conversation.
2516         core_message_external::delete_conversation($user1->id, $user2->id);
2518         $messages = $DB->get_records('message', array(), 'timecreated ASC');
2519         $this->assertCount(4, $messages);
2521         $message1 = array_shift($messages);
2522         $message2 = array_shift($messages);
2523         $message3 = array_shift($messages);
2524         $message4 = array_shift($messages);
2526         $this->assertNotEmpty($message1->timeuserfromdeleted);
2527         $this->assertEmpty($message1->timeusertodeleted);
2529         $this->assertEmpty($message2->timeuserfromdeleted);
2530         $this->assertNotEmpty($message2->timeusertodeleted);
2532         $this->assertNotEmpty($message3->timeuserfromdeleted);
2533         $this->assertEmpty($message3->timeusertodeleted);
2535         $this->assertEmpty($message4->timeuserfromdeleted);
2536         $this->assertNotEmpty($message4->timeusertodeleted);
2537     }
2539     /**
2540      * Test deleting conversation as other user without proper capability.
2541      */
2542     public function test_delete_conversation_as_other_user_without_cap() {
2543         $this->resetAfterTest(true);
2545         // Create some users.
2546         $user1 = self::getDataGenerator()->create_user();
2547         $user2 = self::getDataGenerator()->create_user();
2548         $user3 = self::getDataGenerator()->create_user();
2550         // The person wanting to delete the conversation.
2551         $this->setUser($user3);
2553         // Ensure an exception is thrown.
2554         $this->expectException('moodle_exception');
2555         core_message_external::delete_conversation($user1->id, $user2->id);
2556     }
2558     /**
2559      * Test deleting conversation with messaging disabled.
2560      */
2561     public function test_delete_conversation_messaging_disabled() {
2562         global $CFG;
2564         $this->resetAfterTest(true);
2566         // Create some users.
2567         $user1 = self::getDataGenerator()->create_user();
2568         $user2 = self::getDataGenerator()->create_user();
2570         // The person wanting to delete the conversation.
2571         $this->setUser($user1);
2573         // Disable messaging.
2574         $CFG->messaging = 0;
2576         // Ensure an exception is thrown.
2577         $this->expectException('moodle_exception');
2578         core_message_external::delete_conversation($user1->id, $user2->id);
2579     }
2581     /**
2582      * Test get message processor.
2583      */
2584     public function test_get_message_processor() {
2585         $this->resetAfterTest(true);
2587         // Create a user.
2588         $user1 = self::getDataGenerator()->create_user();
2590         // Set you as the user.
2591         $this->setUser($user1);
2593         // Get the message processors.
2594         $result = core_message_external::get_message_processor($user1->id, 'popup');
2596         // We need to execute the return values cleaning process to simulate the web service server.
2597         $result = external_api::clean_returnvalue(core_message_external::get_message_processor_returns(), $result);
2599         $this->assertNotEmpty($result['systemconfigured']);
2600         $this->assertNotEmpty($result['userconfigured']);
2601     }