37abe4dd2e68df7e726074d85e7278f6cf9ac786
[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         // We need to execute the return values cleaning process to simulate the web service server.
90         $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
92         $themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));
94         // Confirm that the message was inserted correctly.
95         $this->assertEquals($themessage->useridfrom, $USER->id);
96         $this->assertEquals($themessage->useridto, $message1['touserid']);
97         $this->assertEquals($themessage->smallmessage, $message1['text']);
98         $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
99     }
101     /**
102      * Test create_contacts.
103      */
104     public function test_create_contacts() {
105         $this->resetAfterTest(true);
107         $user1 = self::getDataGenerator()->create_user();
108         $user2 = self::getDataGenerator()->create_user();
109         $user3 = self::getDataGenerator()->create_user();
110         $user4 = self::getDataGenerator()->create_user();
111         $user5 = self::getDataGenerator()->create_user();
112         $this->setUser($user1);
114         // Adding a contact.
115         $return = core_message_external::create_contacts(array($user2->id));
116         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
117         $this->assertEquals(array(), $return);
119         // Adding a contact who is already a contact.
120         $return = core_message_external::create_contacts(array($user2->id));
121         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
122         $this->assertEquals(array(), $return);
124         // Adding multiple contacts.
125         $return = core_message_external::create_contacts(array($user3->id, $user4->id));
126         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
127         $this->assertEquals(array(), $return);
129         // Adding a non-existing user.
130         $return = core_message_external::create_contacts(array(99999));
131         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
132         $this->assertCount(1, $return);
133         $return = array_pop($return);
134         $this->assertEquals($return['warningcode'], 'contactnotcreated');
135         $this->assertEquals($return['itemid'], 99999);
137         // Adding contacts with valid and invalid parameters.
138         $return = core_message_external::create_contacts(array($user5->id, 99999));
139         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
140         $this->assertCount(1, $return);
141         $return = array_pop($return);
142         $this->assertEquals($return['warningcode'], 'contactnotcreated');
143         $this->assertEquals($return['itemid'], 99999);
144     }
146     /**
147      * Test delete_contacts.
148      */
149     public function test_delete_contacts() {
150         $this->resetAfterTest(true);
152         $user1 = self::getDataGenerator()->create_user();
153         $user2 = self::getDataGenerator()->create_user();
154         $user3 = self::getDataGenerator()->create_user();
155         $user4 = self::getDataGenerator()->create_user();
156         $user5 = self::getDataGenerator()->create_user();
157         $user6 = self::getDataGenerator()->create_user();
158         $this->setUser($user1);
159         $this->assertEquals(array(), core_message_external::create_contacts(
160             array($user3->id, $user4->id, $user5->id, $user6->id)));
162         // Removing a non-contact.
163         $return = core_message_external::delete_contacts(array($user2->id));
164         $this->assertNull($return);
166         // Removing one contact.
167         $return = core_message_external::delete_contacts(array($user3->id));
168         $this->assertNull($return);
170         // Removing multiple contacts.
171         $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
172         $this->assertNull($return);
174         // Removing contact from unexisting user.
175         $return = core_message_external::delete_contacts(array(99999));
176         $this->assertNull($return);
178         // Removing mixed valid and invalid data.
179         $return = core_message_external::delete_contacts(array($user6->id, 99999));
180         $this->assertNull($return);
181     }
183     /**
184      * Test block_contacts.
185      */
186     public function test_block_contacts() {
187         $this->resetAfterTest(true);
189         $user1 = self::getDataGenerator()->create_user();
190         $user2 = self::getDataGenerator()->create_user();
191         $user3 = self::getDataGenerator()->create_user();
192         $user4 = self::getDataGenerator()->create_user();
193         $user5 = self::getDataGenerator()->create_user();
194         $this->setUser($user1);
195         $this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
197         // Blocking a contact.
198         $return = core_message_external::block_contacts(array($user2->id));
199         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
200         $this->assertEquals(array(), $return);
202         // Blocking a contact who is already a contact.
203         $return = core_message_external::block_contacts(array($user2->id));
204         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
205         $this->assertEquals(array(), $return);
207         // Blocking multiple contacts.
208         $return = core_message_external::block_contacts(array($user3->id, $user4->id));
209         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
210         $this->assertEquals(array(), $return);
212         // Blocking a non-existing user.
213         $return = core_message_external::block_contacts(array(99999));
214         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
215         $this->assertCount(1, $return);
216         $return = array_pop($return);
217         $this->assertEquals($return['warningcode'], 'contactnotblocked');
218         $this->assertEquals($return['itemid'], 99999);
220         // Blocking contacts with valid and invalid parameters.
221         $return = core_message_external::block_contacts(array($user5->id, 99999));
222         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
223         $this->assertCount(1, $return);
224         $return = array_pop($return);
225         $this->assertEquals($return['warningcode'], 'contactnotblocked');
226         $this->assertEquals($return['itemid'], 99999);
227     }
229     /**
230      * Test unblock_contacts.
231      */
232     public function test_unblock_contacts() {
233         $this->resetAfterTest(true);
235         $user1 = self::getDataGenerator()->create_user();
236         $user2 = self::getDataGenerator()->create_user();
237         $user3 = self::getDataGenerator()->create_user();
238         $user4 = self::getDataGenerator()->create_user();
239         $user5 = self::getDataGenerator()->create_user();
240         $user6 = self::getDataGenerator()->create_user();
241         $this->setUser($user1);
242         $this->assertEquals(array(), core_message_external::create_contacts(
243             array($user3->id, $user4->id, $user5->id, $user6->id)));
245         // Removing a non-contact.
246         $return = core_message_external::unblock_contacts(array($user2->id));
247         $this->assertNull($return);
249         // Removing one contact.
250         $return = core_message_external::unblock_contacts(array($user3->id));
251         $this->assertNull($return);
253         // Removing multiple contacts.
254         $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
255         $this->assertNull($return);
257         // Removing contact from unexisting user.
258         $return = core_message_external::unblock_contacts(array(99999));
259         $this->assertNull($return);
261         // Removing mixed valid and invalid data.
262         $return = core_message_external::unblock_contacts(array($user6->id, 99999));
263         $this->assertNull($return);
265     }
267     /**
268      * Test get_contacts.
269      */
270     public function test_get_contacts() {
271         $this->resetAfterTest(true);
273         $user1 = self::getDataGenerator()->create_user();
274         $user_stranger = self::getDataGenerator()->create_user();
275         $user_offline1 = self::getDataGenerator()->create_user();
276         $user_offline2 = self::getDataGenerator()->create_user();
277         $user_offline3 = self::getDataGenerator()->create_user();
278         $user_online = new stdClass();
279         $user_online->lastaccess = time();
280         $user_online = self::getDataGenerator()->create_user($user_online);
281         $user_blocked = self::getDataGenerator()->create_user();
283         // Login as user1.
284         $this->setUser($user1);
285         $this->assertEquals(array(), core_message_external::create_contacts(
286             array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
288         // User_stranger sends a couple of messages to user1.
289         $this->send_message($user_stranger, $user1, 'Hello there!');
290         $this->send_message($user_stranger, $user1, 'How you goin?');
291         $this->send_message($user_stranger, $user1, 'Cya!');
293         // User_blocked sends a message to user1.
294         $this->send_message($user_blocked, $user1, 'Here, have some spam.');
296         // Retrieve the contacts of the user.
297         $this->setUser($user1);
298         $contacts = core_message_external::get_contacts();
299         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
300         $this->assertCount(3, $contacts['offline']);
301         $this->assertCount(1, $contacts['online']);
302         $this->assertCount(2, $contacts['strangers']);
303         core_message_external::block_contacts(array($user_blocked->id));
304         $contacts = core_message_external::get_contacts();
305         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
306         $this->assertCount(3, $contacts['offline']);
307         $this->assertCount(1, $contacts['online']);
308         $this->assertCount(1, $contacts['strangers']);
310         // Checking some of the fields returned.
311         $stranger = array_pop($contacts['strangers']);
312         $this->assertEquals($user_stranger->id, $stranger['id']);
313         $this->assertEquals(3, $stranger['unread']);
314     }
316     /**
317      * Test search_contacts.
318      */
319     public function test_search_contacts() {
320         global $DB;
321         $this->resetAfterTest(true);
323         $course1 = $this->getDataGenerator()->create_course();
324         $course2 = $this->getDataGenerator()->create_course();
326         $user1 = new stdClass();
327         $user1->firstname = 'X';
328         $user1->lastname = 'X';
329         $user1 = $this->getDataGenerator()->create_user($user1);
330         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
331         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
333         $user2 = new stdClass();
334         $user2->firstname = 'Eric';
335         $user2->lastname = 'Cartman';
336         $user2 = self::getDataGenerator()->create_user($user2);
337         $user3 = new stdClass();
338         $user3->firstname = 'Stan';
339         $user3->lastname = 'Marsh';
340         $user3 = self::getDataGenerator()->create_user($user3);
341         self::getDataGenerator()->enrol_user($user3->id, $course1->id);
342         $user4 = new stdClass();
343         $user4->firstname = 'Kyle';
344         $user4->lastname = 'Broflovski';
345         $user4 = self::getDataGenerator()->create_user($user4);
346         $user5 = new stdClass();
347         $user5->firstname = 'Kenny';
348         $user5->lastname = 'McCormick';
349         $user5 = self::getDataGenerator()->create_user($user5);
350         self::getDataGenerator()->enrol_user($user5->id, $course2->id);
352         // Searching for users, keep in mind that 'Admin User' and 'Guest user' can be returned for now.
353         // See MDL-37164 which should fix that. Once fixed, remove the +2's.
354         $this->setUser($user1);
355         $results = core_message_external::search_contacts('r');
356         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
357         $this->assertCount(4 + 2, $results);
358         $results = core_message_external::search_contacts('r', true);
359         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
360         $this->assertCount(2, $results);
361         $results = core_message_external::search_contacts('Kyle', false);
362         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
363         $this->assertCount(1, $results);
364         $result = reset($results);
365         $this->assertEquals($user4->id, $result['id']);
366         $results = core_message_external::search_contacts('y', false);
367         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
368         $this->assertCount(2, $results);
369         $results = core_message_external::search_contacts('y', true);
370         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
371         $this->assertCount(1, $results);
372         $result = reset($results);
373         $this->assertEquals($user5->id, $result['id']);
375         // Empty query, will throw an exception.
376         $this->setExpectedException('moodle_exception');
377         $results = core_message_external::search_contacts('');
378     }