MDL-55393 message: Add some external functions to 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.
7d69958e 54 * @param int $notification is the message a notification.
d6731600 55 */
7d69958e 56 protected function send_message($userfrom, $userto, $message = 'Hello world!', $notification = 0) {
d6731600
FM
57 global $DB;
58 $record = new stdClass();
59 $record->useridfrom = $userfrom->id;
60 $record->useridto = $userto->id;
61 $record->subject = 'No subject';
62 $record->fullmessage = $message;
63 $record->timecreated = time();
7d69958e 64 $record->notification = $notification;
d6731600
FM
65 $insert = $DB->insert_record('message', $record);
66 }
67
e6432668
JM
68 /**
69 * Test send_instant_messages
70 */
71 public function test_send_instant_messages() {
72
73 global $DB, $USER, $CFG;
74
75 $this->resetAfterTest(true);
7356e732
EL
76 // Transactions used in tests, tell phpunit use alternative reset method.
77 $this->preventResetByRollback();
e6432668
JM
78
79 // Turn off all message processors (so nothing is really sent)
80 require_once($CFG->dirroot . '/message/lib.php');
81 $messageprocessors = get_message_processors();
82 foreach($messageprocessors as $messageprocessor) {
83 $messageprocessor->enabled = 0;
84 $DB->update_record('message_processors', $messageprocessor);
85 }
86
87 // Set the required capabilities by the external function
88 $contextid = context_system::instance()->id;
89 $roleid = $this->assignUserCapability('moodle/site:sendmessage', $contextid);
90
91 $user1 = self::getDataGenerator()->create_user();
92
93 // Create test message data.
94 $message1 = array();
95 $message1['touserid'] = $user1->id;
96 $message1['text'] = 'the message.';
97 $message1['clientmsgid'] = 4;
98 $messages = array($message1);
99
100 $sentmessages = core_message_external::send_instant_messages($messages);
101
fb695f6e
JM
102 // We need to execute the return values cleaning process to simulate the web service server.
103 $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
104
e6432668
JM
105 $themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));
106
107 // Confirm that the message was inserted correctly.
108 $this->assertEquals($themessage->useridfrom, $USER->id);
109 $this->assertEquals($themessage->useridto, $message1['touserid']);
110 $this->assertEquals($themessage->smallmessage, $message1['text']);
111 $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
112 }
d6731600
FM
113
114 /**
115 * Test create_contacts.
116 */
117 public function test_create_contacts() {
118 $this->resetAfterTest(true);
119
120 $user1 = self::getDataGenerator()->create_user();
121 $user2 = self::getDataGenerator()->create_user();
122 $user3 = self::getDataGenerator()->create_user();
123 $user4 = self::getDataGenerator()->create_user();
124 $user5 = self::getDataGenerator()->create_user();
125 $this->setUser($user1);
126
127 // Adding a contact.
128 $return = core_message_external::create_contacts(array($user2->id));
129 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
130 $this->assertEquals(array(), $return);
131
132 // Adding a contact who is already a contact.
133 $return = core_message_external::create_contacts(array($user2->id));
134 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
135 $this->assertEquals(array(), $return);
136
137 // Adding multiple contacts.
138 $return = core_message_external::create_contacts(array($user3->id, $user4->id));
139 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
140 $this->assertEquals(array(), $return);
141
142 // Adding a non-existing user.
143 $return = core_message_external::create_contacts(array(99999));
144 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
145 $this->assertCount(1, $return);
146 $return = array_pop($return);
147 $this->assertEquals($return['warningcode'], 'contactnotcreated');
148 $this->assertEquals($return['itemid'], 99999);
149
150 // Adding contacts with valid and invalid parameters.
151 $return = core_message_external::create_contacts(array($user5->id, 99999));
152 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
153 $this->assertCount(1, $return);
154 $return = array_pop($return);
155 $this->assertEquals($return['warningcode'], 'contactnotcreated');
156 $this->assertEquals($return['itemid'], 99999);
157 }
158
159 /**
160 * Test delete_contacts.
161 */
162 public function test_delete_contacts() {
163 $this->resetAfterTest(true);
164
165 $user1 = self::getDataGenerator()->create_user();
166 $user2 = self::getDataGenerator()->create_user();
167 $user3 = self::getDataGenerator()->create_user();
168 $user4 = self::getDataGenerator()->create_user();
169 $user5 = self::getDataGenerator()->create_user();
170 $user6 = self::getDataGenerator()->create_user();
171 $this->setUser($user1);
172 $this->assertEquals(array(), core_message_external::create_contacts(
173 array($user3->id, $user4->id, $user5->id, $user6->id)));
174
175 // Removing a non-contact.
176 $return = core_message_external::delete_contacts(array($user2->id));
177 $this->assertNull($return);
178
179 // Removing one contact.
180 $return = core_message_external::delete_contacts(array($user3->id));
181 $this->assertNull($return);
182
183 // Removing multiple contacts.
184 $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
185 $this->assertNull($return);
186
187 // Removing contact from unexisting user.
188 $return = core_message_external::delete_contacts(array(99999));
189 $this->assertNull($return);
190
191 // Removing mixed valid and invalid data.
192 $return = core_message_external::delete_contacts(array($user6->id, 99999));
193 $this->assertNull($return);
194 }
195
196 /**
197 * Test block_contacts.
198 */
199 public function test_block_contacts() {
200 $this->resetAfterTest(true);
201
202 $user1 = self::getDataGenerator()->create_user();
203 $user2 = self::getDataGenerator()->create_user();
204 $user3 = self::getDataGenerator()->create_user();
205 $user4 = self::getDataGenerator()->create_user();
206 $user5 = self::getDataGenerator()->create_user();
207 $this->setUser($user1);
208 $this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
209
210 // Blocking a contact.
211 $return = core_message_external::block_contacts(array($user2->id));
212 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
213 $this->assertEquals(array(), $return);
214
215 // Blocking a contact who is already a contact.
216 $return = core_message_external::block_contacts(array($user2->id));
217 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
218 $this->assertEquals(array(), $return);
219
220 // Blocking multiple contacts.
221 $return = core_message_external::block_contacts(array($user3->id, $user4->id));
222 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
223 $this->assertEquals(array(), $return);
224
225 // Blocking a non-existing user.
226 $return = core_message_external::block_contacts(array(99999));
227 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
228 $this->assertCount(1, $return);
229 $return = array_pop($return);
230 $this->assertEquals($return['warningcode'], 'contactnotblocked');
231 $this->assertEquals($return['itemid'], 99999);
232
233 // Blocking contacts with valid and invalid parameters.
234 $return = core_message_external::block_contacts(array($user5->id, 99999));
235 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
236 $this->assertCount(1, $return);
237 $return = array_pop($return);
238 $this->assertEquals($return['warningcode'], 'contactnotblocked');
239 $this->assertEquals($return['itemid'], 99999);
240 }
241
242 /**
243 * Test unblock_contacts.
244 */
245 public function test_unblock_contacts() {
246 $this->resetAfterTest(true);
247
248 $user1 = self::getDataGenerator()->create_user();
249 $user2 = self::getDataGenerator()->create_user();
250 $user3 = self::getDataGenerator()->create_user();
251 $user4 = self::getDataGenerator()->create_user();
252 $user5 = self::getDataGenerator()->create_user();
253 $user6 = self::getDataGenerator()->create_user();
254 $this->setUser($user1);
255 $this->assertEquals(array(), core_message_external::create_contacts(
256 array($user3->id, $user4->id, $user5->id, $user6->id)));
257
258 // Removing a non-contact.
259 $return = core_message_external::unblock_contacts(array($user2->id));
260 $this->assertNull($return);
261
262 // Removing one contact.
263 $return = core_message_external::unblock_contacts(array($user3->id));
264 $this->assertNull($return);
265
266 // Removing multiple contacts.
267 $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
268 $this->assertNull($return);
269
270 // Removing contact from unexisting user.
271 $return = core_message_external::unblock_contacts(array(99999));
272 $this->assertNull($return);
273
274 // Removing mixed valid and invalid data.
275 $return = core_message_external::unblock_contacts(array($user6->id, 99999));
276 $this->assertNull($return);
277
278 }
279
280 /**
281 * Test get_contacts.
282 */
283 public function test_get_contacts() {
284 $this->resetAfterTest(true);
285
286 $user1 = self::getDataGenerator()->create_user();
287 $user_stranger = self::getDataGenerator()->create_user();
288 $user_offline1 = self::getDataGenerator()->create_user();
289 $user_offline2 = self::getDataGenerator()->create_user();
290 $user_offline3 = self::getDataGenerator()->create_user();
291 $user_online = new stdClass();
292 $user_online->lastaccess = time();
293 $user_online = self::getDataGenerator()->create_user($user_online);
294 $user_blocked = self::getDataGenerator()->create_user();
0b074e88 295 $noreplyuser = core_user::get_user(core_user::NOREPLY_USER);
d6731600
FM
296
297 // Login as user1.
298 $this->setUser($user1);
299 $this->assertEquals(array(), core_message_external::create_contacts(
300 array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
301
302 // User_stranger sends a couple of messages to user1.
303 $this->send_message($user_stranger, $user1, 'Hello there!');
304 $this->send_message($user_stranger, $user1, 'How you goin?');
305 $this->send_message($user_stranger, $user1, 'Cya!');
0b074e88 306 $this->send_message($noreplyuser, $user1, 'I am not a real user');
d6731600
FM
307
308 // User_blocked sends a message to user1.
309 $this->send_message($user_blocked, $user1, 'Here, have some spam.');
310
311 // Retrieve the contacts of the user.
312 $this->setUser($user1);
313 $contacts = core_message_external::get_contacts();
314 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
315 $this->assertCount(3, $contacts['offline']);
316 $this->assertCount(1, $contacts['online']);
0b074e88 317 $this->assertCount(3, $contacts['strangers']);
d6731600
FM
318 core_message_external::block_contacts(array($user_blocked->id));
319 $contacts = core_message_external::get_contacts();
320 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
321 $this->assertCount(3, $contacts['offline']);
322 $this->assertCount(1, $contacts['online']);
0b074e88 323 $this->assertCount(2, $contacts['strangers']);
d6731600
FM
324
325 // Checking some of the fields returned.
326 $stranger = array_pop($contacts['strangers']);
01393790 327
0b074e88
JL
328 $this->assertEquals(core_user::NOREPLY_USER, $stranger['id']);
329 $this->assertEquals(1, $stranger['unread']);
01393790
JL
330
331 // Check that deleted users are not returned.
332 delete_user($user_offline1);
333 delete_user($user_stranger);
334 delete_user($user_online);
335 $contacts = core_message_external::get_contacts();
336 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
337 $this->assertCount(2, $contacts['offline']);
338 $this->assertCount(0, $contacts['online']);
339 $this->assertCount(1, $contacts['strangers']);
d6731600
FM
340 }
341
342 /**
343 * Test search_contacts.
52f3e060 344 * @expectedException moodle_exception
d6731600
FM
345 */
346 public function test_search_contacts() {
347 global $DB;
348 $this->resetAfterTest(true);
349
350 $course1 = $this->getDataGenerator()->create_course();
351 $course2 = $this->getDataGenerator()->create_course();
352
353 $user1 = new stdClass();
354 $user1->firstname = 'X';
355 $user1->lastname = 'X';
356 $user1 = $this->getDataGenerator()->create_user($user1);
357 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
358 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
359
360 $user2 = new stdClass();
361 $user2->firstname = 'Eric';
362 $user2->lastname = 'Cartman';
363 $user2 = self::getDataGenerator()->create_user($user2);
364 $user3 = new stdClass();
365 $user3->firstname = 'Stan';
366 $user3->lastname = 'Marsh';
367 $user3 = self::getDataGenerator()->create_user($user3);
368 self::getDataGenerator()->enrol_user($user3->id, $course1->id);
369 $user4 = new stdClass();
370 $user4->firstname = 'Kyle';
371 $user4->lastname = 'Broflovski';
372 $user4 = self::getDataGenerator()->create_user($user4);
373 $user5 = new stdClass();
374 $user5->firstname = 'Kenny';
375 $user5->lastname = 'McCormick';
376 $user5 = self::getDataGenerator()->create_user($user5);
377 self::getDataGenerator()->enrol_user($user5->id, $course2->id);
378
d6731600 379 $this->setUser($user1);
2e2d1977 380
d6731600
FM
381 $results = core_message_external::search_contacts('r');
382 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
2e2d1977
AD
383 $this->assertCount(5, $results); // Users 2 through 5 + admin
384
d6731600
FM
385 $results = core_message_external::search_contacts('r', true);
386 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
387 $this->assertCount(2, $results);
2e2d1977 388
d6731600
FM
389 $results = core_message_external::search_contacts('Kyle', false);
390 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
391 $this->assertCount(1, $results);
392 $result = reset($results);
393 $this->assertEquals($user4->id, $result['id']);
2e2d1977 394
d6731600
FM
395 $results = core_message_external::search_contacts('y', false);
396 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
397 $this->assertCount(2, $results);
2e2d1977 398
d6731600
FM
399 $results = core_message_external::search_contacts('y', true);
400 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
401 $this->assertCount(1, $results);
402 $result = reset($results);
403 $this->assertEquals($user5->id, $result['id']);
404
405 // Empty query, will throw an exception.
d6731600
FM
406 $results = core_message_external::search_contacts('');
407 }
6ff4464b
JL
408
409 /**
410 * Test get_messages.
411 */
412 public function test_get_messages() {
ea21d637 413 global $CFG, $DB;
6ff4464b
JL
414 $this->resetAfterTest(true);
415
416 $this->preventResetByRollback();
417 // This mark the messages as read!.
418 $sink = $this->redirectMessages();
419
420 $user1 = self::getDataGenerator()->create_user();
421 $user2 = self::getDataGenerator()->create_user();
422 $user3 = self::getDataGenerator()->create_user();
423
424 $course = self::getDataGenerator()->create_course();
425
426 // Send a message from one user to another.
427 message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
428 message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE);
429 message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
430 message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
431 message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE);
432
433 $this->setUser($user1);
434 // Get read conversations from user1 to user2.
435 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
436 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
437 $this->assertCount(1, $messages['messages']);
438
ea21d637
JL
439 // Delete the message.
440 $message = array_shift($messages['messages']);
441 $messagetobedeleted = $DB->get_record('message_read', array('id' => $message['id']));
442 message_delete_message($messagetobedeleted, $user1->id);
443
444 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
445 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
446 $this->assertCount(0, $messages['messages']);
447
6ff4464b
JL
448 // Get unread conversations from user1 to user2.
449 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0);
450 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
451 $this->assertCount(0, $messages['messages']);
452
453 // Get read messages send from user1.
454 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
455 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
ea21d637 456 $this->assertCount(1, $messages['messages']);
6ff4464b
JL
457
458 $this->setUser($user2);
459 // Get read conversations from any user to user2.
460 $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0);
461 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
462 $this->assertCount(2, $messages['messages']);
463
ea21d637
JL
464 // Conversations from user3 to user2.
465 $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
466 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
467 $this->assertCount(1, $messages['messages']);
468
469 // Delete the message.
470 $message = array_shift($messages['messages']);
471 $messagetobedeleted = $DB->get_record('message_read', array('id' => $message['id']));
472 message_delete_message($messagetobedeleted, $user2->id);
473
474 $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
475 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
476 $this->assertCount(0, $messages['messages']);
477
6ff4464b
JL
478 $this->setUser($user3);
479 // Get read notifications received by user3.
480 $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0);
481 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
482 $this->assertCount(0, $messages['messages']);
483
484 // Now, create some notifications...
485 // We are creating fake notifications but based on real ones.
486
127ef540 487 // This one omits notification = 1.
6ff4464b
JL
488 $eventdata = new stdClass();
489 $eventdata->modulename = 'moodle';
490 $eventdata->component = 'enrol_paypal';
491 $eventdata->name = 'paypal_enrolment';
492 $eventdata->userfrom = get_admin();
493 $eventdata->userto = $user1;
494 $eventdata->subject = "Moodle: PayPal payment";
495 $eventdata->fullmessage = "Your PayPal payment is pending.";
496 $eventdata->fullmessageformat = FORMAT_PLAIN;
497 $eventdata->fullmessagehtml = '';
498 $eventdata->smallmessage = '';
499 message_send($eventdata);
500
501 $message = new stdClass();
502 $message->notification = 1;
503 $message->component = 'enrol_manual';
504 $message->name = 'expiry_notification';
505 $message->userfrom = $user2;
506 $message->userto = $user1;
507 $message->subject = 'Enrolment expired';
508 $message->fullmessage = 'Enrolment expired blah blah blah';
509 $message->fullmessageformat = FORMAT_MARKDOWN;
510 $message->fullmessagehtml = markdown_to_html($message->fullmessage);
511 $message->smallmessage = $message->subject;
512 $message->contexturlname = $course->fullname;
513 $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id));
514 message_send($message);
515
516 $userfrom = core_user::get_noreply_user();
517 $userfrom->maildisplay = true;
518 $eventdata = new stdClass();
519 $eventdata->component = 'moodle';
520 $eventdata->name = 'badgecreatornotice';
521 $eventdata->userfrom = $userfrom;
522 $eventdata->userto = $user1;
523 $eventdata->notification = 1;
524 $eventdata->subject = 'New badge';
525 $eventdata->fullmessage = format_text_email($eventdata->subject, FORMAT_HTML);
526 $eventdata->fullmessageformat = FORMAT_PLAIN;
527 $eventdata->fullmessagehtml = $eventdata->subject;
528 $eventdata->smallmessage = $eventdata->subject;
529 message_send($eventdata);
530
531 $eventdata = new stdClass();
532 $eventdata->name = 'submission';
533 $eventdata->component = 'mod_feedback';
534 $eventdata->userfrom = $user1;
535 $eventdata->userto = $user2;
536 $eventdata->subject = 'Feedback submitted';
537 $eventdata->fullmessage = 'Feedback submitted from an user';
538 $eventdata->fullmessageformat = FORMAT_PLAIN;
539 $eventdata->fullmessagehtml = '<strong>Feedback submitted</strong>';
540 $eventdata->smallmessage = '';
541 message_send($eventdata);
542
543 $this->setUser($user1);
544 // Get read notifications from any user to user1.
545 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0);
546 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
547 $this->assertCount(3, $messages['messages']);
548
549 // Get one read notifications from any user to user1.
550 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1);
551 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
552 $this->assertCount(1, $messages['messages']);
553
554 // Get unread notifications from any user to user1.
555 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0);
556 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
557 $this->assertCount(0, $messages['messages']);
558
559 // Get read both type of messages from any user to user1.
560 $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0);
561 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
562 $this->assertCount(4, $messages['messages']);
563
564 // Get read notifications from no-reply-user to user1.
565 $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0);
566 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
567 $this->assertCount(1, $messages['messages']);
568
569 // Get notifications send by user1 to any user.
570 $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0);
571 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
572 $this->assertCount(1, $messages['messages']);
573
574 // Test warnings.
575 $CFG->messaging = 0;
576
577 $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0);
578 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
579 $this->assertCount(1, $messages['warnings']);
580
581 // Test exceptions.
582
583 // Messaging disabled.
584 try {
585 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
586 $this->fail('Exception expected due messaging disabled.');
587 } catch (moodle_exception $e) {
588 $this->assertEquals('disabled', $e->errorcode);
589 }
590
591 $CFG->messaging = 1;
592
593 // Invalid users.
594 try {
595 $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0);
596 $this->fail('Exception expected due invalid users.');
597 } catch (moodle_exception $e) {
598 $this->assertEquals('accessdenied', $e->errorcode);
599 }
600
601 // Invalid user ids.
602 try {
603 $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0);
604 $this->fail('Exception expected due invalid users.');
605 } catch (moodle_exception $e) {
606 $this->assertEquals('invaliduser', $e->errorcode);
607 }
608
609 // Invalid users (permissions).
610 $this->setUser($user2);
611 try {
612 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
613 $this->fail('Exception expected due invalid user.');
614 } catch (moodle_exception $e) {
615 $this->assertEquals('accessdenied', $e->errorcode);
616 }
617
618 }
ff1f3739
JL
619
620 /**
621 * Test get_blocked_users.
622 */
623 public function test_get_blocked_users() {
624 $this->resetAfterTest(true);
625
626 $user1 = self::getDataGenerator()->create_user();
627 $userstranger = self::getDataGenerator()->create_user();
628 $useroffline1 = self::getDataGenerator()->create_user();
629 $useroffline2 = self::getDataGenerator()->create_user();
630 $userblocked = self::getDataGenerator()->create_user();
631
632 // Login as user1.
633 $this->setUser($user1);
634 $this->assertEquals(array(), core_message_external::create_contacts(
635 array($useroffline1->id, $useroffline2->id)));
636
637 // The userstranger sends a couple of messages to user1.
638 $this->send_message($userstranger, $user1, 'Hello there!');
639 $this->send_message($userstranger, $user1, 'How you goin?');
640
641 // The userblocked sends a message to user1.
642 // Note that this user is not blocked at this point.
643 $this->send_message($userblocked, $user1, 'Here, have some spam.');
644
645 // Retrieve the list of blocked users.
646 $this->setUser($user1);
647 $blockedusers = core_message_external::get_blocked_users($user1->id);
648 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
649 $this->assertCount(0, $blockedusers['users']);
650
651 // Block the $userblocked and retrieve again the list.
652 core_message_external::block_contacts(array($userblocked->id));
653 $blockedusers = core_message_external::get_blocked_users($user1->id);
654 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
655 $this->assertCount(1, $blockedusers['users']);
656
01393790
JL
657 // Remove the $userblocked and check that the list now is empty.
658 delete_user($userblocked);
659 $blockedusers = core_message_external::get_blocked_users($user1->id);
660 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
661 $this->assertCount(0, $blockedusers['users']);
662
ff1f3739
JL
663 }
664
b6795827
JL
665 /**
666 * Test mark_message_read.
667 */
668 public function test_mark_message_read() {
669 $this->resetAfterTest(true);
670
671 $user1 = self::getDataGenerator()->create_user();
672 $user2 = self::getDataGenerator()->create_user();
673 $user3 = self::getDataGenerator()->create_user();
674
675 // Login as user1.
676 $this->setUser($user1);
677 $this->assertEquals(array(), core_message_external::create_contacts(
678 array($user2->id, $user3->id)));
679
680 // The user2 sends a couple of messages to user1.
681 $this->send_message($user2, $user1, 'Hello there!');
682 $this->send_message($user2, $user1, 'How you goin?');
683 $this->send_message($user3, $user1, 'How you goin?');
684 $this->send_message($user3, $user2, 'How you goin?');
685
686 // Retrieve all messages sent by user2 (they are currently unread).
687 $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
688
689 $messageids = array();
690 foreach ($lastmessages as $m) {
691 $messageid = core_message_external::mark_message_read($m->id, time());
692 $messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid);
693 }
694
695 // Retrieve all messages sent (they are currently read).
696 $lastmessages = message_get_messages($user1->id, $user2->id, 0, true);
697 $this->assertCount(2, $lastmessages);
698 $this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages);
699 $this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages);
700
701 // Retrieve all messages sent by any user (that are currently unread).
702 $lastmessages = message_get_messages($user1->id, 0, 0, false);
703 $this->assertCount(1, $lastmessages);
704
705 // Invalid message ids.
706 try {
707 $messageid = core_message_external::mark_message_read($messageids[0]['messageid'] * 2, time());
708 $this->fail('Exception expected due invalid messageid.');
709 } catch (dml_missing_record_exception $e) {
710 $this->assertEquals('invalidrecord', $e->errorcode);
711 }
712
713 // A message to a different user.
714 $lastmessages = message_get_messages($user2->id, $user3->id, 0, false);
715 $messageid = array_pop($lastmessages)->id;
716 try {
717 $messageid = core_message_external::mark_message_read($messageid, time());
718 $this->fail('Exception expected due invalid messageid.');
719 } catch (invalid_parameter_exception $e) {
720 $this->assertEquals('invalidparameter', $e->errorcode);
721 }
722
723 }
724
419b1128
JL
725 /**
726 * Test delete_message.
727 */
728 public function test_delete_message() {
729 global $DB;
730 $this->resetAfterTest(true);
731
732 $user1 = self::getDataGenerator()->create_user();
733 $user2 = self::getDataGenerator()->create_user();
734 $user3 = self::getDataGenerator()->create_user();
735 $user4 = self::getDataGenerator()->create_user();
736
737 // Login as user1.
738 $this->setUser($user1);
739 $this->assertEquals(array(), core_message_external::create_contacts(array($user2->id, $user3->id)));
740
741 // User user1 does not interchange messages with user3.
742 $m1to2 = message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
743 $m2to3 = message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
744 $m3to2 = message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
745 $m3to4 = message_post_message($user3, $user4, 'some random text 4', FORMAT_MOODLE);
746
747 // Retrieve all messages sent by user2 (they are currently unread).
748 $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
749
750 // Delete a message not read, as a user from.
751 $result = core_message_external::delete_message($m1to2, $user1->id, false);
752 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
753 $this->assertTrue($result['status']);
754 $this->assertCount(0, $result['warnings']);
755 $deletedmessage = $DB->get_record('message', array('id' => $m1to2));
756 $this->assertNotEquals(0, $deletedmessage->timeuserfromdeleted);
757
758 // Try to delete the same message again.
759 $result = core_message_external::delete_message($m1to2, $user1->id, false);
760 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
761 $this->assertFalse($result['status']);
762
763 // Try to delete a message that does not belong to me.
764 try {
765 $messageid = core_message_external::delete_message($m2to3, $user3->id, false);
766 $this->fail('Exception expected due invalid messageid.');
767 } catch (moodle_exception $e) {
768 $this->assertEquals('You do not have permission to delete this message', $e->errorcode);
769 }
770
771 $this->setUser($user3);
772 // Delete a message not read, as a user to.
773 $result = core_message_external::delete_message($m2to3, $user3->id, false);
774 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
775 $this->assertTrue($result['status']);
776 $this->assertCount(0, $result['warnings']);
777 $deletedmessage = $DB->get_record('message', array('id' => $m2to3));
778 $this->assertNotEquals(0, $deletedmessage->timeusertodeleted);
779
780 // Delete a message read.
781 $message = $DB->get_record('message', array('id' => $m3to2));
782 $messageid = message_mark_message_read($message, time());
783 $result = core_message_external::delete_message($messageid, $user3->id);
784 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
785 $this->assertTrue($result['status']);
786 $this->assertCount(0, $result['warnings']);
787 $deletedmessage = $DB->get_record('message_read', array('id' => $messageid));
788 $this->assertNotEquals(0, $deletedmessage->timeuserfromdeleted);
789
790 // Invalid message ids.
791 try {
792 $result = core_message_external::delete_message(-1, $user1->id);
793 $this->fail('Exception expected due invalid messageid.');
794 } catch (dml_missing_record_exception $e) {
795 $this->assertEquals('invalidrecord', $e->errorcode);
796 }
797
798 // Invalid user.
799 try {
800 $result = core_message_external::delete_message($m1to2, -1, false);
801 $this->fail('Exception expected due invalid user.');
802 } catch (moodle_exception $e) {
803 $this->assertEquals('invaliduser', $e->errorcode);
804 }
805
806 // Not active user.
807 delete_user($user2);
808 try {
809 $result = core_message_external::delete_message($m1to2, $user2->id, false);
810 $this->fail('Exception expected due invalid user.');
811 } catch (moodle_exception $e) {
812 $this->assertEquals('userdeleted', $e->errorcode);
813 }
814
815 // Now, as an admin, try to delete any message.
816 $this->setAdminUser();
817 $result = core_message_external::delete_message($m3to4, $user4->id, false);
818 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
819 $this->assertTrue($result['status']);
820 $this->assertCount(0, $result['warnings']);
821 $deletedmessage = $DB->get_record('message', array('id' => $m3to4));
822 $this->assertNotEquals(0, $deletedmessage->timeusertodeleted);
823
824 }
825
3274d5ca
RW
826 public function test_mark_all_notifications_as_read_invalid_user_exception() {
827 $this->resetAfterTest(true);
828
829 $this->setExpectedException('moodle_exception');
830 $result = core_message_external::mark_all_notifications_as_read(-2132131, 0);
831 }
832
833 public function test_mark_all_notifications_as_read_access_denied_exception() {
834 $this->resetAfterTest(true);
835
836 $sender = $this->getDataGenerator()->create_user();
837 $user = $this->getDataGenerator()->create_user();
838
839 $this->setUser($user);
840 $this->setExpectedException('moodle_exception');
841 $result = core_message_external::mark_all_notifications_as_read($sender->id, 0);
842 }
843
844 public function test_mark_all_notifications_as_read_missing_from_user_exception() {
845 $this->resetAfterTest(true);
846
847 $sender = $this->getDataGenerator()->create_user();
848
849 $this->setUser($sender);
850 $this->setExpectedException('moodle_exception');
851 $result = core_message_external::mark_all_notifications_as_read($sender->id, 99999);
852 }
853
854 public function test_mark_all_notifications_as_read() {
7d69958e
RW
855 global $DB;
856
3274d5ca
RW
857 $this->resetAfterTest(true);
858
859 $sender1 = $this->getDataGenerator()->create_user();
860 $sender2 = $this->getDataGenerator()->create_user();
861 $sender3 = $this->getDataGenerator()->create_user();
862 $recipient = $this->getDataGenerator()->create_user();
863
864 $this->setUser($recipient);
865
866 $notificationids = array(
7d69958e
RW
867 $this->send_message($sender1, $recipient, 'Notification', 1),
868 $this->send_message($sender1, $recipient, 'Notification', 1),
869 $this->send_message($sender2, $recipient, 'Notification', 1),
870 $this->send_message($sender2, $recipient, 'Notification', 1),
871 $this->send_message($sender3, $recipient, 'Notification', 1),
872 $this->send_message($sender3, $recipient, 'Notification', 1),
3274d5ca
RW
873 );
874
875 core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id);
7d69958e
RW
876 $readnotifications = $DB->get_recordset('message_read', ['useridto' => $recipient->id]);
877 $unreadnotifications = $DB->get_recordset('message', ['useridto' => $recipient->id]);
3274d5ca 878
7d69958e
RW
879 $this->assertCount(2, $readnotifications);
880 $this->assertCount(4, $unreadnotifications);
3274d5ca
RW
881
882 core_message_external::mark_all_notifications_as_read($recipient->id, 0);
7d69958e
RW
883 $readnotifications = $DB->get_recordset('message_read', ['useridto' => $recipient->id]);
884 $unreadnotifications = $DB->get_recordset('message', ['useridto' => $recipient->id]);
3274d5ca 885
7d69958e
RW
886 $this->assertCount(6, $readnotifications);
887 $this->assertCount(0, $unreadnotifications);
3274d5ca 888 }
d6731600 889}