MDL-45374 messages: get_messages ws definition
[moodle.git] / message / tests / externallib_test.php
CommitLineData
e6432668
JM
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/>.
16
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 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31require_once($CFG->dirroot . '/message/externallib.php');
32
8252b7c2 33class core_message_externallib_testcase extends externallib_advanced_testcase {
e6432668 34
d6731600
FM
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 }
56
e6432668
JM
57 /**
58 * Test send_instant_messages
59 */
60 public function test_send_instant_messages() {
61
62 global $DB, $USER, $CFG;
63
64 $this->resetAfterTest(true);
7356e732
EL
65 // Transactions used in tests, tell phpunit use alternative reset method.
66 $this->preventResetByRollback();
e6432668
JM
67
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 }
75
76 // Set the required capabilities by the external function
77 $contextid = context_system::instance()->id;
78 $roleid = $this->assignUserCapability('moodle/site:sendmessage', $contextid);
79
80 $user1 = self::getDataGenerator()->create_user();
81
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);
88
89 $sentmessages = core_message_external::send_instant_messages($messages);
90
fb695f6e
JM
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);
93
e6432668
JM
94 $themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));
95
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 }
d6731600
FM
102
103 /**
104 * Test create_contacts.
105 */
106 public function test_create_contacts() {
107 $this->resetAfterTest(true);
108
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);
115
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);
120
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);
125
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);
130
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);
138
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 }
147
148 /**
149 * Test delete_contacts.
150 */
151 public function test_delete_contacts() {
152 $this->resetAfterTest(true);
153
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)));
163
164 // Removing a non-contact.
165 $return = core_message_external::delete_contacts(array($user2->id));
166 $this->assertNull($return);
167
168 // Removing one contact.
169 $return = core_message_external::delete_contacts(array($user3->id));
170 $this->assertNull($return);
171
172 // Removing multiple contacts.
173 $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
174 $this->assertNull($return);
175
176 // Removing contact from unexisting user.
177 $return = core_message_external::delete_contacts(array(99999));
178 $this->assertNull($return);
179
180 // Removing mixed valid and invalid data.
181 $return = core_message_external::delete_contacts(array($user6->id, 99999));
182 $this->assertNull($return);
183 }
184
185 /**
186 * Test block_contacts.
187 */
188 public function test_block_contacts() {
189 $this->resetAfterTest(true);
190
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)));
198
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);
203
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);
208
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);
213
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);
221
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 }
230
231 /**
232 * Test unblock_contacts.
233 */
234 public function test_unblock_contacts() {
235 $this->resetAfterTest(true);
236
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)));
246
247 // Removing a non-contact.
248 $return = core_message_external::unblock_contacts(array($user2->id));
249 $this->assertNull($return);
250
251 // Removing one contact.
252 $return = core_message_external::unblock_contacts(array($user3->id));
253 $this->assertNull($return);
254
255 // Removing multiple contacts.
256 $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
257 $this->assertNull($return);
258
259 // Removing contact from unexisting user.
260 $return = core_message_external::unblock_contacts(array(99999));
261 $this->assertNull($return);
262
263 // Removing mixed valid and invalid data.
264 $return = core_message_external::unblock_contacts(array($user6->id, 99999));
265 $this->assertNull($return);
266
267 }
268
269 /**
270 * Test get_contacts.
271 */
272 public function test_get_contacts() {
273 $this->resetAfterTest(true);
274
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();
284
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)));
289
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!');
294
295 // User_blocked sends a message to user1.
296 $this->send_message($user_blocked, $user1, 'Here, have some spam.');
297
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']);
311
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 }
317
318 /**
319 * Test search_contacts.
320 */
321 public function test_search_contacts() {
322 global $DB;
323 $this->resetAfterTest(true);
324
325 $course1 = $this->getDataGenerator()->create_course();
326 $course2 = $this->getDataGenerator()->create_course();
327
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);
334
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);
353
d6731600 354 $this->setUser($user1);
2e2d1977 355
d6731600
FM
356 $results = core_message_external::search_contacts('r');
357 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
2e2d1977
AD
358 $this->assertCount(5, $results); // Users 2 through 5 + admin
359
d6731600
FM
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);
2e2d1977 363
d6731600
FM
364 $results = core_message_external::search_contacts('Kyle', false);
365 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
366 $this->assertCount(1, $results);
367 $result = reset($results);
368 $this->assertEquals($user4->id, $result['id']);
2e2d1977 369
d6731600
FM
370 $results = core_message_external::search_contacts('y', false);
371 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
372 $this->assertCount(2, $results);
2e2d1977 373
d6731600
FM
374 $results = core_message_external::search_contacts('y', true);
375 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
376 $this->assertCount(1, $results);
377 $result = reset($results);
378 $this->assertEquals($user5->id, $result['id']);
379
380 // Empty query, will throw an exception.
381 $this->setExpectedException('moodle_exception');
382 $results = core_message_external::search_contacts('');
383 }
d6731600 384}