MDL-37086 phpunit: use alt reset method.
[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);
65         // Transactions used in tests, tell phpunit use alternative reset method.
66         $this->preventResetByRollback();
68         // Turn off all message processors (so nothing is really sent)
69         require_once($CFG->dirroot . '/message/lib.php');
70         $messageprocessors = get_message_processors();
71         foreach($messageprocessors as $messageprocessor) {
72             $messageprocessor->enabled = 0;
73             $DB->update_record('message_processors', $messageprocessor);
74         }
76         // Set the required capabilities by the external function
77         $contextid = context_system::instance()->id;
78         $roleid = $this->assignUserCapability('moodle/site:sendmessage', $contextid);
80         $user1 = self::getDataGenerator()->create_user();
82         // Create test message data.
83         $message1 = array();
84         $message1['touserid'] = $user1->id;
85         $message1['text'] = 'the message.';
86         $message1['clientmsgid'] = 4;
87         $messages = array($message1);
89         $sentmessages = core_message_external::send_instant_messages($messages);
91         // We need to execute the return values cleaning process to simulate the web service server.
92         $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
94         $themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));
96         // Confirm that the message was inserted correctly.
97         $this->assertEquals($themessage->useridfrom, $USER->id);
98         $this->assertEquals($themessage->useridto, $message1['touserid']);
99         $this->assertEquals($themessage->smallmessage, $message1['text']);
100         $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
101     }
103     /**
104      * Test create_contacts.
105      */
106     public function test_create_contacts() {
107         $this->resetAfterTest(true);
109         $user1 = self::getDataGenerator()->create_user();
110         $user2 = self::getDataGenerator()->create_user();
111         $user3 = self::getDataGenerator()->create_user();
112         $user4 = self::getDataGenerator()->create_user();
113         $user5 = self::getDataGenerator()->create_user();
114         $this->setUser($user1);
116         // Adding 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 a contact who is already a contact.
122         $return = core_message_external::create_contacts(array($user2->id));
123         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
124         $this->assertEquals(array(), $return);
126         // Adding multiple contacts.
127         $return = core_message_external::create_contacts(array($user3->id, $user4->id));
128         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
129         $this->assertEquals(array(), $return);
131         // Adding a non-existing user.
132         $return = core_message_external::create_contacts(array(99999));
133         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
134         $this->assertCount(1, $return);
135         $return = array_pop($return);
136         $this->assertEquals($return['warningcode'], 'contactnotcreated');
137         $this->assertEquals($return['itemid'], 99999);
139         // Adding contacts with valid and invalid parameters.
140         $return = core_message_external::create_contacts(array($user5->id, 99999));
141         $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
142         $this->assertCount(1, $return);
143         $return = array_pop($return);
144         $this->assertEquals($return['warningcode'], 'contactnotcreated');
145         $this->assertEquals($return['itemid'], 99999);
146     }
148     /**
149      * Test delete_contacts.
150      */
151     public function test_delete_contacts() {
152         $this->resetAfterTest(true);
154         $user1 = self::getDataGenerator()->create_user();
155         $user2 = self::getDataGenerator()->create_user();
156         $user3 = self::getDataGenerator()->create_user();
157         $user4 = self::getDataGenerator()->create_user();
158         $user5 = self::getDataGenerator()->create_user();
159         $user6 = self::getDataGenerator()->create_user();
160         $this->setUser($user1);
161         $this->assertEquals(array(), core_message_external::create_contacts(
162             array($user3->id, $user4->id, $user5->id, $user6->id)));
164         // Removing a non-contact.
165         $return = core_message_external::delete_contacts(array($user2->id));
166         $this->assertNull($return);
168         // Removing one contact.
169         $return = core_message_external::delete_contacts(array($user3->id));
170         $this->assertNull($return);
172         // Removing multiple contacts.
173         $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
174         $this->assertNull($return);
176         // Removing contact from unexisting user.
177         $return = core_message_external::delete_contacts(array(99999));
178         $this->assertNull($return);
180         // Removing mixed valid and invalid data.
181         $return = core_message_external::delete_contacts(array($user6->id, 99999));
182         $this->assertNull($return);
183     }
185     /**
186      * Test block_contacts.
187      */
188     public function test_block_contacts() {
189         $this->resetAfterTest(true);
191         $user1 = self::getDataGenerator()->create_user();
192         $user2 = self::getDataGenerator()->create_user();
193         $user3 = self::getDataGenerator()->create_user();
194         $user4 = self::getDataGenerator()->create_user();
195         $user5 = self::getDataGenerator()->create_user();
196         $this->setUser($user1);
197         $this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
199         // Blocking 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 a contact who is already a contact.
205         $return = core_message_external::block_contacts(array($user2->id));
206         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
207         $this->assertEquals(array(), $return);
209         // Blocking multiple contacts.
210         $return = core_message_external::block_contacts(array($user3->id, $user4->id));
211         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
212         $this->assertEquals(array(), $return);
214         // Blocking a non-existing user.
215         $return = core_message_external::block_contacts(array(99999));
216         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
217         $this->assertCount(1, $return);
218         $return = array_pop($return);
219         $this->assertEquals($return['warningcode'], 'contactnotblocked');
220         $this->assertEquals($return['itemid'], 99999);
222         // Blocking contacts with valid and invalid parameters.
223         $return = core_message_external::block_contacts(array($user5->id, 99999));
224         $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
225         $this->assertCount(1, $return);
226         $return = array_pop($return);
227         $this->assertEquals($return['warningcode'], 'contactnotblocked');
228         $this->assertEquals($return['itemid'], 99999);
229     }
231     /**
232      * Test unblock_contacts.
233      */
234     public function test_unblock_contacts() {
235         $this->resetAfterTest(true);
237         $user1 = self::getDataGenerator()->create_user();
238         $user2 = self::getDataGenerator()->create_user();
239         $user3 = self::getDataGenerator()->create_user();
240         $user4 = self::getDataGenerator()->create_user();
241         $user5 = self::getDataGenerator()->create_user();
242         $user6 = self::getDataGenerator()->create_user();
243         $this->setUser($user1);
244         $this->assertEquals(array(), core_message_external::create_contacts(
245             array($user3->id, $user4->id, $user5->id, $user6->id)));
247         // Removing a non-contact.
248         $return = core_message_external::unblock_contacts(array($user2->id));
249         $this->assertNull($return);
251         // Removing one contact.
252         $return = core_message_external::unblock_contacts(array($user3->id));
253         $this->assertNull($return);
255         // Removing multiple contacts.
256         $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
257         $this->assertNull($return);
259         // Removing contact from unexisting user.
260         $return = core_message_external::unblock_contacts(array(99999));
261         $this->assertNull($return);
263         // Removing mixed valid and invalid data.
264         $return = core_message_external::unblock_contacts(array($user6->id, 99999));
265         $this->assertNull($return);
267     }
269     /**
270      * Test get_contacts.
271      */
272     public function test_get_contacts() {
273         $this->resetAfterTest(true);
275         $user1 = self::getDataGenerator()->create_user();
276         $user_stranger = self::getDataGenerator()->create_user();
277         $user_offline1 = self::getDataGenerator()->create_user();
278         $user_offline2 = self::getDataGenerator()->create_user();
279         $user_offline3 = self::getDataGenerator()->create_user();
280         $user_online = new stdClass();
281         $user_online->lastaccess = time();
282         $user_online = self::getDataGenerator()->create_user($user_online);
283         $user_blocked = self::getDataGenerator()->create_user();
285         // Login as user1.
286         $this->setUser($user1);
287         $this->assertEquals(array(), core_message_external::create_contacts(
288             array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
290         // User_stranger sends a couple of messages to user1.
291         $this->send_message($user_stranger, $user1, 'Hello there!');
292         $this->send_message($user_stranger, $user1, 'How you goin?');
293         $this->send_message($user_stranger, $user1, 'Cya!');
295         // User_blocked sends a message to user1.
296         $this->send_message($user_blocked, $user1, 'Here, have some spam.');
298         // Retrieve the contacts of the user.
299         $this->setUser($user1);
300         $contacts = core_message_external::get_contacts();
301         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
302         $this->assertCount(3, $contacts['offline']);
303         $this->assertCount(1, $contacts['online']);
304         $this->assertCount(2, $contacts['strangers']);
305         core_message_external::block_contacts(array($user_blocked->id));
306         $contacts = core_message_external::get_contacts();
307         $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
308         $this->assertCount(3, $contacts['offline']);
309         $this->assertCount(1, $contacts['online']);
310         $this->assertCount(1, $contacts['strangers']);
312         // Checking some of the fields returned.
313         $stranger = array_pop($contacts['strangers']);
314         $this->assertEquals($user_stranger->id, $stranger['id']);
315         $this->assertEquals(3, $stranger['unread']);
316     }
318     /**
319      * Test search_contacts.
320      */
321     public function test_search_contacts() {
322         global $DB;
323         $this->resetAfterTest(true);
325         $course1 = $this->getDataGenerator()->create_course();
326         $course2 = $this->getDataGenerator()->create_course();
328         $user1 = new stdClass();
329         $user1->firstname = 'X';
330         $user1->lastname = 'X';
331         $user1 = $this->getDataGenerator()->create_user($user1);
332         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
333         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
335         $user2 = new stdClass();
336         $user2->firstname = 'Eric';
337         $user2->lastname = 'Cartman';
338         $user2 = self::getDataGenerator()->create_user($user2);
339         $user3 = new stdClass();
340         $user3->firstname = 'Stan';
341         $user3->lastname = 'Marsh';
342         $user3 = self::getDataGenerator()->create_user($user3);
343         self::getDataGenerator()->enrol_user($user3->id, $course1->id);
344         $user4 = new stdClass();
345         $user4->firstname = 'Kyle';
346         $user4->lastname = 'Broflovski';
347         $user4 = self::getDataGenerator()->create_user($user4);
348         $user5 = new stdClass();
349         $user5->firstname = 'Kenny';
350         $user5->lastname = 'McCormick';
351         $user5 = self::getDataGenerator()->create_user($user5);
352         self::getDataGenerator()->enrol_user($user5->id, $course2->id);
354         // Searching for users, keep in mind that 'Admin User' and 'Guest user' can be returned for now.
355         // See MDL-37164 which should fix that. Once fixed, remove the +2's.
356         $this->setUser($user1);
357         $results = core_message_external::search_contacts('r');
358         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
359         $this->assertCount(4 + 2, $results);
360         $results = core_message_external::search_contacts('r', true);
361         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
362         $this->assertCount(2, $results);
363         $results = core_message_external::search_contacts('Kyle', false);
364         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
365         $this->assertCount(1, $results);
366         $result = reset($results);
367         $this->assertEquals($user4->id, $result['id']);
368         $results = core_message_external::search_contacts('y', false);
369         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
370         $this->assertCount(2, $results);
371         $results = core_message_external::search_contacts('y', true);
372         $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
373         $this->assertCount(1, $results);
374         $result = reset($results);
375         $this->assertEquals($user5->id, $result['id']);
377         // Empty query, will throw an exception.
378         $this->setExpectedException('moodle_exception');
379         $results = core_message_external::search_contacts('');
380     }