MDL-30070 message: Web Services for contact list
[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_external_testcase extends externallib_advanced_testcase {
35     /**
36      * Send a fake message.
37      *
38      * {@link message_send()} does not support transaction, this function will simulate a message
39      * sent from a user to another. We should stop using it once {@link message_send()} will support
40      * transactions. This is not clean at all, this is just used to add rows to the table.
41      *
42      * @param stdClass $userfrom user object of the one sending the message.
43      * @param stdClass $userto user object of the one receiving the message.
44      * @param string $message message to send.
45      */
46     protected function send_message($userfrom, $userto, $message = 'Hello world!') {
47         global $DB;
48         $record = new stdClass();
49         $record->useridfrom = $userfrom->id;
50         $record->useridto = $userto->id;
51         $record->subject = 'No subject';
52         $record->fullmessage = $message;
53         $record->timecreated = time();
54         $insert = $DB->insert_record('message', $record);
55     }
57     /**
58      * Test send_instant_messages
59      */
60     public function test_send_instant_messages() {
62         global $DB, $USER, $CFG;
64         $this->resetAfterTest(true);
66         // Turn off all message processors (so nothing is really sent)
67         require_once($CFG->dirroot . '/message/lib.php');
68         $messageprocessors = get_message_processors();
69         foreach($messageprocessors as $messageprocessor) {
70             $messageprocessor->enabled = 0;
71             $DB->update_record('message_processors', $messageprocessor);
72         }
74         // Set the required capabilities by the external function
75         $contextid = context_system::instance()->id;
76         $roleid = $this->assignUserCapability('moodle/site:sendmessage', $contextid);
78         $user1 = self::getDataGenerator()->create_user();
80         // Create test message data.
81         $message1 = array();
82         $message1['touserid'] = $user1->id;
83         $message1['text'] = 'the message.';
84         $message1['clientmsgid'] = 4;
85         $messages = array($message1);
87         $sentmessages = core_message_external::send_instant_messages($messages);
89         $themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));
91         // Confirm that the message was inserted correctly.
92         $this->assertEquals($themessage->useridfrom, $USER->id);
93         $this->assertEquals($themessage->useridto, $message1['touserid']);
94         $this->assertEquals($themessage->smallmessage, $message1['text']);
95         $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
96     }
98     /**
99      * Test create_contacts.
100      */
101     public function test_create_contacts() {
102         $this->resetAfterTest(true);
104         $user1 = self::getDataGenerator()->create_user();
105         $user2 = self::getDataGenerator()->create_user();
106         $user3 = self::getDataGenerator()->create_user();
107         $user4 = self::getDataGenerator()->create_user();
108         $user5 = self::getDataGenerator()->create_user();
109         $this->setUser($user1);
111         // Adding a contact.
112         $return = core_message_external::create_contacts(array($user2->id));
113         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
114         $this->assertEquals(array(), $return);
116         // Adding a contact who is already a contact.
117         $return = core_message_external::create_contacts(array($user2->id));
118         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
119         $this->assertEquals(array(), $return);
121         // Adding multiple contacts.
122         $return = core_message_external::create_contacts(array($user3->id, $user4->id));
123         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
124         $this->assertEquals(array(), $return);
126         // Adding a non-existing user.
127         $return = core_message_external::create_contacts(array(99999));
128         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
129         $this->assertCount(1, $return);
130         $return = array_pop($return);
131         $this->assertEquals($return['warningcode'], 'contactnotcreated');
132         $this->assertEquals($return['itemid'], 99999);
134         // Adding contacts with valid and invalid parameters.
135         $return = core_message_external::create_contacts(array($user5->id, 99999));
136         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
137         $this->assertCount(1, $return);
138         $return = array_pop($return);
139         $this->assertEquals($return['warningcode'], 'contactnotcreated');
140         $this->assertEquals($return['itemid'], 99999);
141     }
143     /**
144      * Test delete_contacts.
145      */
146     public function test_delete_contacts() {
147         $this->resetAfterTest(true);
149         $user1 = self::getDataGenerator()->create_user();
150         $user2 = self::getDataGenerator()->create_user();
151         $user3 = self::getDataGenerator()->create_user();
152         $user4 = self::getDataGenerator()->create_user();
153         $user5 = self::getDataGenerator()->create_user();
154         $user6 = self::getDataGenerator()->create_user();
155         $this->setUser($user1);
156         $this->assertEquals(array(), core_message_external::create_contacts(
157             array($user3->id, $user4->id, $user5->id, $user6->id)));
159         // Removing a non-contact.
160         $return = core_message_external::delete_contacts(array($user2->id));
161         $this->assertNull($return);
163         // Removing one contact.
164         $return = core_message_external::delete_contacts(array($user3->id));
165         $this->assertNull($return);
167         // Removing multiple contacts.
168         $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
169         $this->assertNull($return);
171         // Removing contact from unexisting user.
172         $return = core_message_external::delete_contacts(array(99999));
173         $this->assertNull($return);
175         // Removing mixed valid and invalid data.
176         $return = core_message_external::delete_contacts(array($user6->id, 99999));
177         $this->assertNull($return);
178     }
180     /**
181      * Test block_contacts.
182      */
183     public function test_block_contacts() {
184         $this->resetAfterTest(true);
186         $user1 = self::getDataGenerator()->create_user();
187         $user2 = self::getDataGenerator()->create_user();
188         $user3 = self::getDataGenerator()->create_user();
189         $user4 = self::getDataGenerator()->create_user();
190         $user5 = self::getDataGenerator()->create_user();
191         $this->setUser($user1);
192         $this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
194         // Blocking a contact.
195         $return = core_message_external::block_contacts(array($user2->id));
196         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
197         $this->assertEquals(array(), $return);
199         // Blocking a contact who is already a contact.
200         $return = core_message_external::block_contacts(array($user2->id));
201         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
202         $this->assertEquals(array(), $return);
204         // Blocking multiple contacts.
205         $return = core_message_external::block_contacts(array($user3->id, $user4->id));
206         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
207         $this->assertEquals(array(), $return);
209         // Blocking a non-existing user.
210         $return = core_message_external::block_contacts(array(99999));
211         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
212         $this->assertCount(1, $return);
213         $return = array_pop($return);
214         $this->assertEquals($return['warningcode'], 'contactnotblocked');
215         $this->assertEquals($return['itemid'], 99999);
217         // Blocking contacts with valid and invalid parameters.
218         $return = core_message_external::block_contacts(array($user5->id, 99999));
219         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
220         $this->assertCount(1, $return);
221         $return = array_pop($return);
222         $this->assertEquals($return['warningcode'], 'contactnotblocked');
223         $this->assertEquals($return['itemid'], 99999);
224     }
226     /**
227      * Test unblock_contacts.
228      */
229     public function test_unblock_contacts() {
230         $this->resetAfterTest(true);
232         $user1 = self::getDataGenerator()->create_user();
233         $user2 = self::getDataGenerator()->create_user();
234         $user3 = self::getDataGenerator()->create_user();
235         $user4 = self::getDataGenerator()->create_user();
236         $user5 = self::getDataGenerator()->create_user();
237         $user6 = self::getDataGenerator()->create_user();
238         $this->setUser($user1);
239         $this->assertEquals(array(), core_message_external::create_contacts(
240             array($user3->id, $user4->id, $user5->id, $user6->id)));
242         // Removing a non-contact.
243         $return = core_message_external::unblock_contacts(array($user2->id));
244         $this->assertNull($return);
246         // Removing one contact.
247         $return = core_message_external::unblock_contacts(array($user3->id));
248         $this->assertNull($return);
250         // Removing multiple contacts.
251         $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
252         $this->assertNull($return);
254         // Removing contact from unexisting user.
255         $return = core_message_external::unblock_contacts(array(99999));
256         $this->assertNull($return);
258         // Removing mixed valid and invalid data.
259         $return = core_message_external::unblock_contacts(array($user6->id, 99999));
260         $this->assertNull($return);
262     }
264     /**
265      * Test get_contacts.
266      */
267     public function test_get_contacts() {
268         $this->resetAfterTest(true);
270         $user1 = self::getDataGenerator()->create_user();
271         $user_stranger = self::getDataGenerator()->create_user();
272         $user_offline1 = self::getDataGenerator()->create_user();
273         $user_offline2 = self::getDataGenerator()->create_user();
274         $user_offline3 = self::getDataGenerator()->create_user();
275         $user_online = new stdClass();
276         $user_online->lastaccess = time();
277         $user_online = self::getDataGenerator()->create_user($user_online);
278         $user_blocked = self::getDataGenerator()->create_user();
280         // Login as user1.
281         $this->setUser($user1);
282         $this->assertEquals(array(), core_message_external::create_contacts(
283             array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
285         // User_stranger sends a couple of messages to user1.
286         $this->send_message($user_stranger, $user1, 'Hello there!');
287         $this->send_message($user_stranger, $user1, 'How you goin?');
288         $this->send_message($user_stranger, $user1, 'Cya!');
290         // User_blocked sends a message to user1.
291         $this->send_message($user_blocked, $user1, 'Here, have some spam.');
293         // Retrieve the contacts of the user.
294         $this->setUser($user1);
295         $contacts = core_message_external::get_contacts();
296         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
297         $this->assertCount(3, $contacts['offline']);
298         $this->assertCount(1, $contacts['online']);
299         $this->assertCount(2, $contacts['strangers']);
300         core_message_external::block_contacts(array($user_blocked->id));
301         $contacts = core_message_external::get_contacts();
302         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
303         $this->assertCount(3, $contacts['offline']);
304         $this->assertCount(1, $contacts['online']);
305         $this->assertCount(1, $contacts['strangers']);
307         // Checking some of the fields returned.
308         $stranger = array_pop($contacts['strangers']);
309         $this->assertEquals($user_stranger->id, $stranger['id']);
310         $this->assertEquals(3, $stranger['unread']);
311     }
313     /**
314      * Test search_contacts.
315      */
316     public function test_search_contacts() {
317         global $DB;
318         $this->resetAfterTest(true);
320         $course1 = $this->getDataGenerator()->create_course();
321         $course2 = $this->getDataGenerator()->create_course();
323         $user1 = new stdClass();
324         $user1->firstname = 'X';
325         $user1->lastname = 'X';
326         $user1 = $this->getDataGenerator()->create_user($user1);
327         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
328         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
330         $user2 = new stdClass();
331         $user2->firstname = 'Eric';
332         $user2->lastname = 'Cartman';
333         $user2 = self::getDataGenerator()->create_user($user2);
334         $user3 = new stdClass();
335         $user3->firstname = 'Stan';
336         $user3->lastname = 'Marsh';
337         $user3 = self::getDataGenerator()->create_user($user3);
338         self::getDataGenerator()->enrol_user($user3->id, $course1->id);
339         $user4 = new stdClass();
340         $user4->firstname = 'Kyle';
341         $user4->lastname = 'Broflovski';
342         $user4 = self::getDataGenerator()->create_user($user4);
343         $user5 = new stdClass();
344         $user5->firstname = 'Kenny';
345         $user5->lastname = 'McCormick';
346         $user5 = self::getDataGenerator()->create_user($user5);
347         self::getDataGenerator()->enrol_user($user5->id, $course2->id);
349         // Searching for users, keep in mind that 'Admin User' and 'Guest user' can be returned for now.
350         // See MDL-37164 which should fix that. Once fixed, remove the +2's.
351         $this->setUser($user1);
352         $results = core_message_external::search_contacts('r');
353         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
354         $this->assertCount(4 + 2, $results);
355         $results = core_message_external::search_contacts('r', true);
356         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
357         $this->assertCount(2, $results);
358         $results = core_message_external::search_contacts('Kyle', false);
359         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
360         $this->assertCount(1, $results);
361         $result = reset($results);
362         $this->assertEquals($user4->id, $result['id']);
363         $results = core_message_external::search_contacts('y', false);
364         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
365         $this->assertCount(2, $results);
366         $results = core_message_external::search_contacts('y', true);
367         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
368         $this->assertCount(1, $results);
369         $result = reset($results);
370         $this->assertEquals($user5->id, $result['id']);
372         // Empty query, will throw an exception.
373         $this->setExpectedException('moodle_exception');
374         $results = core_message_external::search_contacts('');
375     }