weekly release 3.0dev
[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();
0b074e88 293 $noreplyuser = core_user::get_user(core_user::NOREPLY_USER);
d6731600
FM
294
295 // Login as user1.
296 $this->setUser($user1);
297 $this->assertEquals(array(), core_message_external::create_contacts(
298 array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
299
300 // User_stranger sends a couple of messages to user1.
301 $this->send_message($user_stranger, $user1, 'Hello there!');
302 $this->send_message($user_stranger, $user1, 'How you goin?');
303 $this->send_message($user_stranger, $user1, 'Cya!');
0b074e88 304 $this->send_message($noreplyuser, $user1, 'I am not a real user');
d6731600
FM
305
306 // User_blocked sends a message to user1.
307 $this->send_message($user_blocked, $user1, 'Here, have some spam.');
308
309 // Retrieve the contacts of the user.
310 $this->setUser($user1);
311 $contacts = core_message_external::get_contacts();
312 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
313 $this->assertCount(3, $contacts['offline']);
314 $this->assertCount(1, $contacts['online']);
0b074e88 315 $this->assertCount(3, $contacts['strangers']);
d6731600
FM
316 core_message_external::block_contacts(array($user_blocked->id));
317 $contacts = core_message_external::get_contacts();
318 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
319 $this->assertCount(3, $contacts['offline']);
320 $this->assertCount(1, $contacts['online']);
0b074e88 321 $this->assertCount(2, $contacts['strangers']);
d6731600
FM
322
323 // Checking some of the fields returned.
324 $stranger = array_pop($contacts['strangers']);
0b074e88
JL
325 $this->assertEquals(core_user::NOREPLY_USER, $stranger['id']);
326 $this->assertEquals(1, $stranger['unread']);
d6731600
FM
327 }
328
329 /**
330 * Test search_contacts.
331 */
332 public function test_search_contacts() {
333 global $DB;
334 $this->resetAfterTest(true);
335
336 $course1 = $this->getDataGenerator()->create_course();
337 $course2 = $this->getDataGenerator()->create_course();
338
339 $user1 = new stdClass();
340 $user1->firstname = 'X';
341 $user1->lastname = 'X';
342 $user1 = $this->getDataGenerator()->create_user($user1);
343 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
344 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
345
346 $user2 = new stdClass();
347 $user2->firstname = 'Eric';
348 $user2->lastname = 'Cartman';
349 $user2 = self::getDataGenerator()->create_user($user2);
350 $user3 = new stdClass();
351 $user3->firstname = 'Stan';
352 $user3->lastname = 'Marsh';
353 $user3 = self::getDataGenerator()->create_user($user3);
354 self::getDataGenerator()->enrol_user($user3->id, $course1->id);
355 $user4 = new stdClass();
356 $user4->firstname = 'Kyle';
357 $user4->lastname = 'Broflovski';
358 $user4 = self::getDataGenerator()->create_user($user4);
359 $user5 = new stdClass();
360 $user5->firstname = 'Kenny';
361 $user5->lastname = 'McCormick';
362 $user5 = self::getDataGenerator()->create_user($user5);
363 self::getDataGenerator()->enrol_user($user5->id, $course2->id);
364
d6731600 365 $this->setUser($user1);
2e2d1977 366
d6731600
FM
367 $results = core_message_external::search_contacts('r');
368 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
2e2d1977
AD
369 $this->assertCount(5, $results); // Users 2 through 5 + admin
370
d6731600
FM
371 $results = core_message_external::search_contacts('r', true);
372 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
373 $this->assertCount(2, $results);
2e2d1977 374
d6731600
FM
375 $results = core_message_external::search_contacts('Kyle', false);
376 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
377 $this->assertCount(1, $results);
378 $result = reset($results);
379 $this->assertEquals($user4->id, $result['id']);
2e2d1977 380
d6731600
FM
381 $results = core_message_external::search_contacts('y', false);
382 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
383 $this->assertCount(2, $results);
2e2d1977 384
d6731600
FM
385 $results = core_message_external::search_contacts('y', true);
386 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
387 $this->assertCount(1, $results);
388 $result = reset($results);
389 $this->assertEquals($user5->id, $result['id']);
390
391 // Empty query, will throw an exception.
392 $this->setExpectedException('moodle_exception');
393 $results = core_message_external::search_contacts('');
394 }
6ff4464b
JL
395
396 /**
397 * Test get_messages.
398 */
399 public function test_get_messages() {
400 global $CFG;
401 $this->resetAfterTest(true);
402
403 $this->preventResetByRollback();
404 // This mark the messages as read!.
405 $sink = $this->redirectMessages();
406
407 $user1 = self::getDataGenerator()->create_user();
408 $user2 = self::getDataGenerator()->create_user();
409 $user3 = self::getDataGenerator()->create_user();
410
411 $course = self::getDataGenerator()->create_course();
412
413 // Send a message from one user to another.
414 message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
415 message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE);
416 message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
417 message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
418 message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE);
419
420 $this->setUser($user1);
421 // Get read conversations from user1 to user2.
422 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
423 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
424 $this->assertCount(1, $messages['messages']);
425
426 // Get unread conversations from user1 to user2.
427 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0);
428 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
429 $this->assertCount(0, $messages['messages']);
430
431 // Get read messages send from user1.
432 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
433 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
434 $this->assertCount(2, $messages['messages']);
435
436 $this->setUser($user2);
437 // Get read conversations from any user to user2.
438 $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0);
439 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
440 $this->assertCount(2, $messages['messages']);
441
442 $this->setUser($user3);
443 // Get read notifications received by user3.
444 $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0);
445 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
446 $this->assertCount(0, $messages['messages']);
447
448 // Now, create some notifications...
449 // We are creating fake notifications but based on real ones.
450
127ef540 451 // This one omits notification = 1.
6ff4464b
JL
452 $eventdata = new stdClass();
453 $eventdata->modulename = 'moodle';
454 $eventdata->component = 'enrol_paypal';
455 $eventdata->name = 'paypal_enrolment';
456 $eventdata->userfrom = get_admin();
457 $eventdata->userto = $user1;
458 $eventdata->subject = "Moodle: PayPal payment";
459 $eventdata->fullmessage = "Your PayPal payment is pending.";
460 $eventdata->fullmessageformat = FORMAT_PLAIN;
461 $eventdata->fullmessagehtml = '';
462 $eventdata->smallmessage = '';
463 message_send($eventdata);
464
465 $message = new stdClass();
466 $message->notification = 1;
467 $message->component = 'enrol_manual';
468 $message->name = 'expiry_notification';
469 $message->userfrom = $user2;
470 $message->userto = $user1;
471 $message->subject = 'Enrolment expired';
472 $message->fullmessage = 'Enrolment expired blah blah blah';
473 $message->fullmessageformat = FORMAT_MARKDOWN;
474 $message->fullmessagehtml = markdown_to_html($message->fullmessage);
475 $message->smallmessage = $message->subject;
476 $message->contexturlname = $course->fullname;
477 $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id));
478 message_send($message);
479
480 $userfrom = core_user::get_noreply_user();
481 $userfrom->maildisplay = true;
482 $eventdata = new stdClass();
483 $eventdata->component = 'moodle';
484 $eventdata->name = 'badgecreatornotice';
485 $eventdata->userfrom = $userfrom;
486 $eventdata->userto = $user1;
487 $eventdata->notification = 1;
488 $eventdata->subject = 'New badge';
489 $eventdata->fullmessage = format_text_email($eventdata->subject, FORMAT_HTML);
490 $eventdata->fullmessageformat = FORMAT_PLAIN;
491 $eventdata->fullmessagehtml = $eventdata->subject;
492 $eventdata->smallmessage = $eventdata->subject;
493 message_send($eventdata);
494
495 $eventdata = new stdClass();
496 $eventdata->name = 'submission';
497 $eventdata->component = 'mod_feedback';
498 $eventdata->userfrom = $user1;
499 $eventdata->userto = $user2;
500 $eventdata->subject = 'Feedback submitted';
501 $eventdata->fullmessage = 'Feedback submitted from an user';
502 $eventdata->fullmessageformat = FORMAT_PLAIN;
503 $eventdata->fullmessagehtml = '<strong>Feedback submitted</strong>';
504 $eventdata->smallmessage = '';
505 message_send($eventdata);
506
507 $this->setUser($user1);
508 // Get read notifications from any user to user1.
509 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0);
510 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
511 $this->assertCount(3, $messages['messages']);
512
513 // Get one read notifications from any user to user1.
514 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1);
515 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
516 $this->assertCount(1, $messages['messages']);
517
518 // Get unread notifications from any user to user1.
519 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0);
520 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
521 $this->assertCount(0, $messages['messages']);
522
523 // Get read both type of messages from any user to user1.
524 $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0);
525 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
526 $this->assertCount(4, $messages['messages']);
527
528 // Get read notifications from no-reply-user to user1.
529 $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0);
530 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
531 $this->assertCount(1, $messages['messages']);
532
533 // Get notifications send by user1 to any user.
534 $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0);
535 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
536 $this->assertCount(1, $messages['messages']);
537
538 // Test warnings.
539 $CFG->messaging = 0;
540
541 $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0);
542 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
543 $this->assertCount(1, $messages['warnings']);
544
545 // Test exceptions.
546
547 // Messaging disabled.
548 try {
549 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
550 $this->fail('Exception expected due messaging disabled.');
551 } catch (moodle_exception $e) {
552 $this->assertEquals('disabled', $e->errorcode);
553 }
554
555 $CFG->messaging = 1;
556
557 // Invalid users.
558 try {
559 $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0);
560 $this->fail('Exception expected due invalid users.');
561 } catch (moodle_exception $e) {
562 $this->assertEquals('accessdenied', $e->errorcode);
563 }
564
565 // Invalid user ids.
566 try {
567 $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0);
568 $this->fail('Exception expected due invalid users.');
569 } catch (moodle_exception $e) {
570 $this->assertEquals('invaliduser', $e->errorcode);
571 }
572
573 // Invalid users (permissions).
574 $this->setUser($user2);
575 try {
576 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
577 $this->fail('Exception expected due invalid user.');
578 } catch (moodle_exception $e) {
579 $this->assertEquals('accessdenied', $e->errorcode);
580 }
581
582 }
ff1f3739
JL
583
584 /**
585 * Test get_blocked_users.
586 */
587 public function test_get_blocked_users() {
588 $this->resetAfterTest(true);
589
590 $user1 = self::getDataGenerator()->create_user();
591 $userstranger = self::getDataGenerator()->create_user();
592 $useroffline1 = self::getDataGenerator()->create_user();
593 $useroffline2 = self::getDataGenerator()->create_user();
594 $userblocked = self::getDataGenerator()->create_user();
595
596 // Login as user1.
597 $this->setUser($user1);
598 $this->assertEquals(array(), core_message_external::create_contacts(
599 array($useroffline1->id, $useroffline2->id)));
600
601 // The userstranger sends a couple of messages to user1.
602 $this->send_message($userstranger, $user1, 'Hello there!');
603 $this->send_message($userstranger, $user1, 'How you goin?');
604
605 // The userblocked sends a message to user1.
606 // Note that this user is not blocked at this point.
607 $this->send_message($userblocked, $user1, 'Here, have some spam.');
608
609 // Retrieve the list of blocked users.
610 $this->setUser($user1);
611 $blockedusers = core_message_external::get_blocked_users($user1->id);
612 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
613 $this->assertCount(0, $blockedusers['users']);
614
615 // Block the $userblocked and retrieve again the list.
616 core_message_external::block_contacts(array($userblocked->id));
617 $blockedusers = core_message_external::get_blocked_users($user1->id);
618 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
619 $this->assertCount(1, $blockedusers['users']);
620
621 }
622
b6795827
JL
623 /**
624 * Test mark_message_read.
625 */
626 public function test_mark_message_read() {
627 $this->resetAfterTest(true);
628
629 $user1 = self::getDataGenerator()->create_user();
630 $user2 = self::getDataGenerator()->create_user();
631 $user3 = self::getDataGenerator()->create_user();
632
633 // Login as user1.
634 $this->setUser($user1);
635 $this->assertEquals(array(), core_message_external::create_contacts(
636 array($user2->id, $user3->id)));
637
638 // The user2 sends a couple of messages to user1.
639 $this->send_message($user2, $user1, 'Hello there!');
640 $this->send_message($user2, $user1, 'How you goin?');
641 $this->send_message($user3, $user1, 'How you goin?');
642 $this->send_message($user3, $user2, 'How you goin?');
643
644 // Retrieve all messages sent by user2 (they are currently unread).
645 $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
646
647 $messageids = array();
648 foreach ($lastmessages as $m) {
649 $messageid = core_message_external::mark_message_read($m->id, time());
650 $messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid);
651 }
652
653 // Retrieve all messages sent (they are currently read).
654 $lastmessages = message_get_messages($user1->id, $user2->id, 0, true);
655 $this->assertCount(2, $lastmessages);
656 $this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages);
657 $this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages);
658
659 // Retrieve all messages sent by any user (that are currently unread).
660 $lastmessages = message_get_messages($user1->id, 0, 0, false);
661 $this->assertCount(1, $lastmessages);
662
663 // Invalid message ids.
664 try {
665 $messageid = core_message_external::mark_message_read($messageids[0]['messageid'] * 2, time());
666 $this->fail('Exception expected due invalid messageid.');
667 } catch (dml_missing_record_exception $e) {
668 $this->assertEquals('invalidrecord', $e->errorcode);
669 }
670
671 // A message to a different user.
672 $lastmessages = message_get_messages($user2->id, $user3->id, 0, false);
673 $messageid = array_pop($lastmessages)->id;
674 try {
675 $messageid = core_message_external::mark_message_read($messageid, time());
676 $this->fail('Exception expected due invalid messageid.');
677 } catch (invalid_parameter_exception $e) {
678 $this->assertEquals('invalidparameter', $e->errorcode);
679 }
680
681 }
682
d6731600 683}