MDL-49501 messages: Add mark_message_read to the mobile service
[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
6ff4464b
JL
35 /**
36 * Tests set up
37 */
38 protected function setUp() {
39 global $CFG;
40
41 require_once($CFG->dirroot . '/message/lib.php');
42 }
43
d6731600
FM
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 */
55 protected function send_message($userfrom, $userto, $message = 'Hello world!') {
56 global $DB;
57 $record = new stdClass();
58 $record->useridfrom = $userfrom->id;
59 $record->useridto = $userto->id;
60 $record->subject = 'No subject';
61 $record->fullmessage = $message;
62 $record->timecreated = time();
63 $insert = $DB->insert_record('message', $record);
64 }
65
e6432668
JM
66 /**
67 * Test send_instant_messages
68 */
69 public function test_send_instant_messages() {
70
71 global $DB, $USER, $CFG;
72
73 $this->resetAfterTest(true);
7356e732
EL
74 // Transactions used in tests, tell phpunit use alternative reset method.
75 $this->preventResetByRollback();
e6432668
JM
76
77 // Turn off all message processors (so nothing is really sent)
78 require_once($CFG->dirroot . '/message/lib.php');
79 $messageprocessors = get_message_processors();
80 foreach($messageprocessors as $messageprocessor) {
81 $messageprocessor->enabled = 0;
82 $DB->update_record('message_processors', $messageprocessor);
83 }
84
85 // Set the required capabilities by the external function
86 $contextid = context_system::instance()->id;
87 $roleid = $this->assignUserCapability('moodle/site:sendmessage', $contextid);
88
89 $user1 = self::getDataGenerator()->create_user();
90
91 // Create test message data.
92 $message1 = array();
93 $message1['touserid'] = $user1->id;
94 $message1['text'] = 'the message.';
95 $message1['clientmsgid'] = 4;
96 $messages = array($message1);
97
98 $sentmessages = core_message_external::send_instant_messages($messages);
99
fb695f6e
JM
100 // We need to execute the return values cleaning process to simulate the web service server.
101 $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
102
e6432668
JM
103 $themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));
104
105 // Confirm that the message was inserted correctly.
106 $this->assertEquals($themessage->useridfrom, $USER->id);
107 $this->assertEquals($themessage->useridto, $message1['touserid']);
108 $this->assertEquals($themessage->smallmessage, $message1['text']);
109 $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
110 }
d6731600
FM
111
112 /**
113 * Test create_contacts.
114 */
115 public function test_create_contacts() {
116 $this->resetAfterTest(true);
117
118 $user1 = self::getDataGenerator()->create_user();
119 $user2 = self::getDataGenerator()->create_user();
120 $user3 = self::getDataGenerator()->create_user();
121 $user4 = self::getDataGenerator()->create_user();
122 $user5 = self::getDataGenerator()->create_user();
123 $this->setUser($user1);
124
125 // Adding a contact.
126 $return = core_message_external::create_contacts(array($user2->id));
127 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
128 $this->assertEquals(array(), $return);
129
130 // Adding a contact who is already a contact.
131 $return = core_message_external::create_contacts(array($user2->id));
132 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
133 $this->assertEquals(array(), $return);
134
135 // Adding multiple contacts.
136 $return = core_message_external::create_contacts(array($user3->id, $user4->id));
137 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
138 $this->assertEquals(array(), $return);
139
140 // Adding a non-existing user.
141 $return = core_message_external::create_contacts(array(99999));
142 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
143 $this->assertCount(1, $return);
144 $return = array_pop($return);
145 $this->assertEquals($return['warningcode'], 'contactnotcreated');
146 $this->assertEquals($return['itemid'], 99999);
147
148 // Adding contacts with valid and invalid parameters.
149 $return = core_message_external::create_contacts(array($user5->id, 99999));
150 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
151 $this->assertCount(1, $return);
152 $return = array_pop($return);
153 $this->assertEquals($return['warningcode'], 'contactnotcreated');
154 $this->assertEquals($return['itemid'], 99999);
155 }
156
157 /**
158 * Test delete_contacts.
159 */
160 public function test_delete_contacts() {
161 $this->resetAfterTest(true);
162
163 $user1 = self::getDataGenerator()->create_user();
164 $user2 = self::getDataGenerator()->create_user();
165 $user3 = self::getDataGenerator()->create_user();
166 $user4 = self::getDataGenerator()->create_user();
167 $user5 = self::getDataGenerator()->create_user();
168 $user6 = self::getDataGenerator()->create_user();
169 $this->setUser($user1);
170 $this->assertEquals(array(), core_message_external::create_contacts(
171 array($user3->id, $user4->id, $user5->id, $user6->id)));
172
173 // Removing a non-contact.
174 $return = core_message_external::delete_contacts(array($user2->id));
175 $this->assertNull($return);
176
177 // Removing one contact.
178 $return = core_message_external::delete_contacts(array($user3->id));
179 $this->assertNull($return);
180
181 // Removing multiple contacts.
182 $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
183 $this->assertNull($return);
184
185 // Removing contact from unexisting user.
186 $return = core_message_external::delete_contacts(array(99999));
187 $this->assertNull($return);
188
189 // Removing mixed valid and invalid data.
190 $return = core_message_external::delete_contacts(array($user6->id, 99999));
191 $this->assertNull($return);
192 }
193
194 /**
195 * Test block_contacts.
196 */
197 public function test_block_contacts() {
198 $this->resetAfterTest(true);
199
200 $user1 = self::getDataGenerator()->create_user();
201 $user2 = self::getDataGenerator()->create_user();
202 $user3 = self::getDataGenerator()->create_user();
203 $user4 = self::getDataGenerator()->create_user();
204 $user5 = self::getDataGenerator()->create_user();
205 $this->setUser($user1);
206 $this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
207
208 // Blocking a contact.
209 $return = core_message_external::block_contacts(array($user2->id));
210 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
211 $this->assertEquals(array(), $return);
212
213 // Blocking a contact who is already a contact.
214 $return = core_message_external::block_contacts(array($user2->id));
215 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
216 $this->assertEquals(array(), $return);
217
218 // Blocking multiple contacts.
219 $return = core_message_external::block_contacts(array($user3->id, $user4->id));
220 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
221 $this->assertEquals(array(), $return);
222
223 // Blocking a non-existing user.
224 $return = core_message_external::block_contacts(array(99999));
225 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
226 $this->assertCount(1, $return);
227 $return = array_pop($return);
228 $this->assertEquals($return['warningcode'], 'contactnotblocked');
229 $this->assertEquals($return['itemid'], 99999);
230
231 // Blocking contacts with valid and invalid parameters.
232 $return = core_message_external::block_contacts(array($user5->id, 99999));
233 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
234 $this->assertCount(1, $return);
235 $return = array_pop($return);
236 $this->assertEquals($return['warningcode'], 'contactnotblocked');
237 $this->assertEquals($return['itemid'], 99999);
238 }
239
240 /**
241 * Test unblock_contacts.
242 */
243 public function test_unblock_contacts() {
244 $this->resetAfterTest(true);
245
246 $user1 = self::getDataGenerator()->create_user();
247 $user2 = self::getDataGenerator()->create_user();
248 $user3 = self::getDataGenerator()->create_user();
249 $user4 = self::getDataGenerator()->create_user();
250 $user5 = self::getDataGenerator()->create_user();
251 $user6 = self::getDataGenerator()->create_user();
252 $this->setUser($user1);
253 $this->assertEquals(array(), core_message_external::create_contacts(
254 array($user3->id, $user4->id, $user5->id, $user6->id)));
255
256 // Removing a non-contact.
257 $return = core_message_external::unblock_contacts(array($user2->id));
258 $this->assertNull($return);
259
260 // Removing one contact.
261 $return = core_message_external::unblock_contacts(array($user3->id));
262 $this->assertNull($return);
263
264 // Removing multiple contacts.
265 $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
266 $this->assertNull($return);
267
268 // Removing contact from unexisting user.
269 $return = core_message_external::unblock_contacts(array(99999));
270 $this->assertNull($return);
271
272 // Removing mixed valid and invalid data.
273 $return = core_message_external::unblock_contacts(array($user6->id, 99999));
274 $this->assertNull($return);
275
276 }
277
278 /**
279 * Test get_contacts.
280 */
281 public function test_get_contacts() {
282 $this->resetAfterTest(true);
283
284 $user1 = self::getDataGenerator()->create_user();
285 $user_stranger = self::getDataGenerator()->create_user();
286 $user_offline1 = self::getDataGenerator()->create_user();
287 $user_offline2 = self::getDataGenerator()->create_user();
288 $user_offline3 = self::getDataGenerator()->create_user();
289 $user_online = new stdClass();
290 $user_online->lastaccess = time();
291 $user_online = self::getDataGenerator()->create_user($user_online);
292 $user_blocked = self::getDataGenerator()->create_user();
293
294 // Login as user1.
295 $this->setUser($user1);
296 $this->assertEquals(array(), core_message_external::create_contacts(
297 array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
298
299 // User_stranger sends a couple of messages to user1.
300 $this->send_message($user_stranger, $user1, 'Hello there!');
301 $this->send_message($user_stranger, $user1, 'How you goin?');
302 $this->send_message($user_stranger, $user1, 'Cya!');
303
304 // User_blocked sends a message to user1.
305 $this->send_message($user_blocked, $user1, 'Here, have some spam.');
306
307 // Retrieve the contacts of the user.
308 $this->setUser($user1);
309 $contacts = core_message_external::get_contacts();
310 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
311 $this->assertCount(3, $contacts['offline']);
312 $this->assertCount(1, $contacts['online']);
313 $this->assertCount(2, $contacts['strangers']);
314 core_message_external::block_contacts(array($user_blocked->id));
315 $contacts = core_message_external::get_contacts();
316 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
317 $this->assertCount(3, $contacts['offline']);
318 $this->assertCount(1, $contacts['online']);
319 $this->assertCount(1, $contacts['strangers']);
320
321 // Checking some of the fields returned.
322 $stranger = array_pop($contacts['strangers']);
323 $this->assertEquals($user_stranger->id, $stranger['id']);
324 $this->assertEquals(3, $stranger['unread']);
325 }
326
327 /**
328 * Test search_contacts.
329 */
330 public function test_search_contacts() {
331 global $DB;
332 $this->resetAfterTest(true);
333
334 $course1 = $this->getDataGenerator()->create_course();
335 $course2 = $this->getDataGenerator()->create_course();
336
337 $user1 = new stdClass();
338 $user1->firstname = 'X';
339 $user1->lastname = 'X';
340 $user1 = $this->getDataGenerator()->create_user($user1);
341 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
342 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
343
344 $user2 = new stdClass();
345 $user2->firstname = 'Eric';
346 $user2->lastname = 'Cartman';
347 $user2 = self::getDataGenerator()->create_user($user2);
348 $user3 = new stdClass();
349 $user3->firstname = 'Stan';
350 $user3->lastname = 'Marsh';
351 $user3 = self::getDataGenerator()->create_user($user3);
352 self::getDataGenerator()->enrol_user($user3->id, $course1->id);
353 $user4 = new stdClass();
354 $user4->firstname = 'Kyle';
355 $user4->lastname = 'Broflovski';
356 $user4 = self::getDataGenerator()->create_user($user4);
357 $user5 = new stdClass();
358 $user5->firstname = 'Kenny';
359 $user5->lastname = 'McCormick';
360 $user5 = self::getDataGenerator()->create_user($user5);
361 self::getDataGenerator()->enrol_user($user5->id, $course2->id);
362
d6731600 363 $this->setUser($user1);
2e2d1977 364
d6731600
FM
365 $results = core_message_external::search_contacts('r');
366 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
2e2d1977
AD
367 $this->assertCount(5, $results); // Users 2 through 5 + admin
368
d6731600
FM
369 $results = core_message_external::search_contacts('r', true);
370 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
371 $this->assertCount(2, $results);
2e2d1977 372
d6731600
FM
373 $results = core_message_external::search_contacts('Kyle', false);
374 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
375 $this->assertCount(1, $results);
376 $result = reset($results);
377 $this->assertEquals($user4->id, $result['id']);
2e2d1977 378
d6731600
FM
379 $results = core_message_external::search_contacts('y', false);
380 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
381 $this->assertCount(2, $results);
2e2d1977 382
d6731600
FM
383 $results = core_message_external::search_contacts('y', true);
384 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
385 $this->assertCount(1, $results);
386 $result = reset($results);
387 $this->assertEquals($user5->id, $result['id']);
388
389 // Empty query, will throw an exception.
390 $this->setExpectedException('moodle_exception');
391 $results = core_message_external::search_contacts('');
392 }
6ff4464b
JL
393
394 /**
395 * Test get_messages.
396 */
397 public function test_get_messages() {
398 global $CFG;
399 $this->resetAfterTest(true);
400
401 $this->preventResetByRollback();
402 // This mark the messages as read!.
403 $sink = $this->redirectMessages();
404
405 $user1 = self::getDataGenerator()->create_user();
406 $user2 = self::getDataGenerator()->create_user();
407 $user3 = self::getDataGenerator()->create_user();
408
409 $course = self::getDataGenerator()->create_course();
410
411 // Send a message from one user to another.
412 message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
413 message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE);
414 message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
415 message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
416 message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE);
417
418 $this->setUser($user1);
419 // Get read conversations from user1 to user2.
420 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
421 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
422 $this->assertCount(1, $messages['messages']);
423
424 // Get unread conversations from user1 to user2.
425 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0);
426 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
427 $this->assertCount(0, $messages['messages']);
428
429 // Get read messages send from user1.
430 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
431 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
432 $this->assertCount(2, $messages['messages']);
433
434 $this->setUser($user2);
435 // Get read conversations from any user to user2.
436 $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0);
437 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
438 $this->assertCount(2, $messages['messages']);
439
440 $this->setUser($user3);
441 // Get read notifications received by user3.
442 $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0);
443 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
444 $this->assertCount(0, $messages['messages']);
445
446 // Now, create some notifications...
447 // We are creating fake notifications but based on real ones.
448
127ef540 449 // This one omits notification = 1.
6ff4464b
JL
450 $eventdata = new stdClass();
451 $eventdata->modulename = 'moodle';
452 $eventdata->component = 'enrol_paypal';
453 $eventdata->name = 'paypal_enrolment';
454 $eventdata->userfrom = get_admin();
455 $eventdata->userto = $user1;
456 $eventdata->subject = "Moodle: PayPal payment";
457 $eventdata->fullmessage = "Your PayPal payment is pending.";
458 $eventdata->fullmessageformat = FORMAT_PLAIN;
459 $eventdata->fullmessagehtml = '';
460 $eventdata->smallmessage = '';
461 message_send($eventdata);
462
463 $message = new stdClass();
464 $message->notification = 1;
465 $message->component = 'enrol_manual';
466 $message->name = 'expiry_notification';
467 $message->userfrom = $user2;
468 $message->userto = $user1;
469 $message->subject = 'Enrolment expired';
470 $message->fullmessage = 'Enrolment expired blah blah blah';
471 $message->fullmessageformat = FORMAT_MARKDOWN;
472 $message->fullmessagehtml = markdown_to_html($message->fullmessage);
473 $message->smallmessage = $message->subject;
474 $message->contexturlname = $course->fullname;
475 $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id));
476 message_send($message);
477
478 $userfrom = core_user::get_noreply_user();
479 $userfrom->maildisplay = true;
480 $eventdata = new stdClass();
481 $eventdata->component = 'moodle';
482 $eventdata->name = 'badgecreatornotice';
483 $eventdata->userfrom = $userfrom;
484 $eventdata->userto = $user1;
485 $eventdata->notification = 1;
486 $eventdata->subject = 'New badge';
487 $eventdata->fullmessage = format_text_email($eventdata->subject, FORMAT_HTML);
488 $eventdata->fullmessageformat = FORMAT_PLAIN;
489 $eventdata->fullmessagehtml = $eventdata->subject;
490 $eventdata->smallmessage = $eventdata->subject;
491 message_send($eventdata);
492
493 $eventdata = new stdClass();
494 $eventdata->name = 'submission';
495 $eventdata->component = 'mod_feedback';
496 $eventdata->userfrom = $user1;
497 $eventdata->userto = $user2;
498 $eventdata->subject = 'Feedback submitted';
499 $eventdata->fullmessage = 'Feedback submitted from an user';
500 $eventdata->fullmessageformat = FORMAT_PLAIN;
501 $eventdata->fullmessagehtml = '<strong>Feedback submitted</strong>';
502 $eventdata->smallmessage = '';
503 message_send($eventdata);
504
505 $this->setUser($user1);
506 // Get read notifications from any user to user1.
507 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0);
508 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
509 $this->assertCount(3, $messages['messages']);
510
511 // Get one read notifications from any user to user1.
512 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1);
513 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
514 $this->assertCount(1, $messages['messages']);
515
516 // Get unread notifications from any user to user1.
517 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0);
518 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
519 $this->assertCount(0, $messages['messages']);
520
521 // Get read both type of messages from any user to user1.
522 $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0);
523 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
524 $this->assertCount(4, $messages['messages']);
525
526 // Get read notifications from no-reply-user to user1.
527 $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0);
528 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
529 $this->assertCount(1, $messages['messages']);
530
531 // Get notifications send by user1 to any user.
532 $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0);
533 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
534 $this->assertCount(1, $messages['messages']);
535
536 // Test warnings.
537 $CFG->messaging = 0;
538
539 $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0);
540 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
541 $this->assertCount(1, $messages['warnings']);
542
543 // Test exceptions.
544
545 // Messaging disabled.
546 try {
547 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
548 $this->fail('Exception expected due messaging disabled.');
549 } catch (moodle_exception $e) {
550 $this->assertEquals('disabled', $e->errorcode);
551 }
552
553 $CFG->messaging = 1;
554
555 // Invalid users.
556 try {
557 $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0);
558 $this->fail('Exception expected due invalid users.');
559 } catch (moodle_exception $e) {
560 $this->assertEquals('accessdenied', $e->errorcode);
561 }
562
563 // Invalid user ids.
564 try {
565 $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0);
566 $this->fail('Exception expected due invalid users.');
567 } catch (moodle_exception $e) {
568 $this->assertEquals('invaliduser', $e->errorcode);
569 }
570
571 // Invalid users (permissions).
572 $this->setUser($user2);
573 try {
574 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
575 $this->fail('Exception expected due invalid user.');
576 } catch (moodle_exception $e) {
577 $this->assertEquals('accessdenied', $e->errorcode);
578 }
579
580 }
ff1f3739
JL
581
582 /**
583 * Test get_blocked_users.
584 */
585 public function test_get_blocked_users() {
586 $this->resetAfterTest(true);
587
588 $user1 = self::getDataGenerator()->create_user();
589 $userstranger = self::getDataGenerator()->create_user();
590 $useroffline1 = self::getDataGenerator()->create_user();
591 $useroffline2 = self::getDataGenerator()->create_user();
592 $userblocked = self::getDataGenerator()->create_user();
593
594 // Login as user1.
595 $this->setUser($user1);
596 $this->assertEquals(array(), core_message_external::create_contacts(
597 array($useroffline1->id, $useroffline2->id)));
598
599 // The userstranger sends a couple of messages to user1.
600 $this->send_message($userstranger, $user1, 'Hello there!');
601 $this->send_message($userstranger, $user1, 'How you goin?');
602
603 // The userblocked sends a message to user1.
604 // Note that this user is not blocked at this point.
605 $this->send_message($userblocked, $user1, 'Here, have some spam.');
606
607 // Retrieve the list of blocked users.
608 $this->setUser($user1);
609 $blockedusers = core_message_external::get_blocked_users($user1->id);
610 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
611 $this->assertCount(0, $blockedusers['users']);
612
613 // Block the $userblocked and retrieve again the list.
614 core_message_external::block_contacts(array($userblocked->id));
615 $blockedusers = core_message_external::get_blocked_users($user1->id);
616 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
617 $this->assertCount(1, $blockedusers['users']);
618
619 }
620
d6731600 621}